Add netclass color management / import functionality to PCB setup dialog

Fixes https://gitlab.com/kicad/code/kicad/-/issues/16908

ADDED Netclass color column in board setup dialog
ADDED Import netlist colors from schematic button in board setup dialog
This commit is contained in:
JamesJ 2024-02-27 20:56:50 +00:00
parent b8748c4ef8
commit 5f4c7a52e4
4 changed files with 159 additions and 59 deletions

View File

@ -30,10 +30,13 @@
#include <bitmaps.h>
#include <netclass.h>
#include <confirm.h>
#include <gal/painter.h>
#include <grid_tricks.h>
#include <dialogs/panel_setup_netclasses.h>
#include <tool/tool_manager.h>
#include <pcb_painter.h>
#include <string_utils.h>
#include <view/view.h>
#include <widgets/grid_color_swatch_helpers.h>
#include <widgets/grid_icon_text_helpers.h>
#include <widgets/wx_html_report_box.h>
@ -43,7 +46,8 @@
// columns of netclasses grid
enum {
enum
{
GRID_NAME = 0,
GRID_FIRST_PCBNEW,
@ -55,6 +59,7 @@ enum {
GRID_uVIADRILL,
GRID_DIFF_PAIR_WIDTH,
GRID_DIFF_PAIR_GAP,
GRID_PCB_COLOR,
GRID_FIRST_EESCHEMA,
GRID_WIREWIDTH = GRID_FIRST_EESCHEMA,
@ -163,15 +168,29 @@ PANEL_SETUP_NETCLASSES::PANEL_SETUP_NETCLASSES( wxWindow* aParentWindow, EDA_DRA
m_netclassGrid ) );
m_netclassGrid->SetColAttr( GRID_SCHEMATIC_COLOR, attr );
attr = new wxGridCellAttr;
attr->SetRenderer( new GRID_CELL_COLOR_RENDERER( PAGED_DIALOG::GetDialog( this ) ) );
attr->SetEditor(
new GRID_CELL_COLOR_SELECTOR( PAGED_DIALOG::GetDialog( this ), m_netclassGrid ) );
m_netclassGrid->SetColAttr( GRID_PCB_COLOR, attr );
attr = new wxGridCellAttr;
attr->SetRenderer( new GRID_CELL_ICON_TEXT_RENDERER( g_lineStyleIcons, g_lineStyleNames ) );
attr->SetEditor( new GRID_CELL_ICON_TEXT_POPUP( g_lineStyleIcons, g_lineStyleNames ) );
m_netclassGrid->SetColAttr( GRID_LINESTYLE, attr );
if( m_isEEschema )
m_colorDefaultHelpText->SetFont( KIUI::GetInfoFont( this ).Italic() );
{
m_importColorsButton->Hide();
}
else
m_colorDefaultHelpText->Hide();
{
m_colorDefaultHelpText->SetLabel(
_( "Set color to transparent to use layer default color." ) );
m_colorDefaultHelpText->GetParent()->Layout();
}
m_colorDefaultHelpText->SetFont( KIUI::GetInfoFont( this ).Italic() );
m_netclassGrid->SetAutoEvalCols( { GRID_WIREWIDTH,
GRID_BUSWIDTH,
@ -262,41 +281,70 @@ PANEL_SETUP_NETCLASSES::~PANEL_SETUP_NETCLASSES()
void PANEL_SETUP_NETCLASSES::loadNetclasses()
{
KIGFX::PCB_RENDER_SETTINGS* rs = nullptr;
if( !m_isEEschema )
{
rs = static_cast<KIGFX::PCB_RENDER_SETTINGS*>(
m_frame->GetCanvas()->GetView()->GetPainter()->GetSettings() );
}
int row = 0;
auto netclassToGridRow =
[&]( int aRow, const std::shared_ptr<NETCLASS>& nc )
auto netclassToGridRow = [&]( int aRow, const std::shared_ptr<NETCLASS>& nc, bool isDefault )
{
m_netclassGrid->SetCellValue( aRow, GRID_NAME, nc->GetName() );
m_netclassGrid->SetUnitValue( aRow, GRID_WIREWIDTH, nc->GetWireWidth() );
m_netclassGrid->SetUnitValue( aRow, GRID_BUSWIDTH, nc->GetBusWidth() );
wxString colorAsString = nc->GetSchematicColor().ToCSSString();
m_netclassGrid->SetCellValue( aRow, GRID_SCHEMATIC_COLOR, colorAsString );
int lineStyleIdx = std::max( 0, nc->GetLineStyle() );
if( lineStyleIdx >= (int) g_lineStyleNames.size() )
lineStyleIdx = 0;
m_netclassGrid->SetCellValue( aRow, GRID_LINESTYLE, g_lineStyleNames[lineStyleIdx] );
m_netclassGrid->SetUnitValue( aRow, GRID_CLEARANCE, nc->GetClearance() );
m_netclassGrid->SetUnitValue( aRow, GRID_TRACKSIZE, nc->GetTrackWidth() );
m_netclassGrid->SetUnitValue( aRow, GRID_VIASIZE, nc->GetViaDiameter() );
m_netclassGrid->SetUnitValue( aRow, GRID_VIADRILL, nc->GetViaDrill() );
m_netclassGrid->SetUnitValue( aRow, GRID_uVIASIZE, nc->GetuViaDiameter() );
m_netclassGrid->SetUnitValue( aRow, GRID_uVIADRILL, nc->GetuViaDrill() );
m_netclassGrid->SetUnitValue( aRow, GRID_DIFF_PAIR_WIDTH, nc->GetDiffPairWidth() );
m_netclassGrid->SetUnitValue( aRow, GRID_DIFF_PAIR_GAP, nc->GetDiffPairGap() );
colorAsString = KIGFX::COLOR4D( 0.0, 0.0, 0.0, 0.0 ).ToCSSString();
if( m_isEEschema )
{
colorAsString = nc->GetPcbColor().ToCSSString();
}
if( rs )
{
std::map<wxString, KIGFX::COLOR4D>& netclassColors = rs->GetNetclassColorMap();
if( netclassColors.find( nc->GetName() ) != netclassColors.end() )
{
m_netclassGrid->SetCellValue( aRow, GRID_NAME, nc->GetName() );
colorAsString = netclassColors[nc->GetName()].ToCSSString();
}
}
m_netclassGrid->SetUnitValue( aRow, GRID_WIREWIDTH, nc->GetWireWidth() );
m_netclassGrid->SetUnitValue( aRow, GRID_BUSWIDTH, nc->GetBusWidth() );
if( isDefault )
{
colorAsString = KIGFX::COLOR4D( 0.0, 0.0, 0.0, 0.0 ).ToCSSString();
m_netclassGrid->SetReadOnly( aRow, GRID_PCB_COLOR );
}
wxString colorAsString = nc->GetSchematicColor().ToCSSString();
m_netclassGrid->SetCellValue( aRow, GRID_SCHEMATIC_COLOR, colorAsString );
int lineStyleIdx = std::max( 0, nc->GetLineStyle() );
if( lineStyleIdx >= (int) g_lineStyleNames.size() )
lineStyleIdx = 0;
m_netclassGrid->SetCellValue( aRow, GRID_LINESTYLE,
g_lineStyleNames[ lineStyleIdx ] );
m_netclassGrid->SetUnitValue( aRow, GRID_CLEARANCE, nc->GetClearance() );
m_netclassGrid->SetUnitValue( aRow, GRID_TRACKSIZE, nc->GetTrackWidth() );
m_netclassGrid->SetUnitValue( aRow, GRID_VIASIZE, nc->GetViaDiameter() );
m_netclassGrid->SetUnitValue( aRow, GRID_VIADRILL, nc->GetViaDrill() );
m_netclassGrid->SetUnitValue( aRow, GRID_uVIASIZE, nc->GetuViaDiameter() );
m_netclassGrid->SetUnitValue( aRow, GRID_uVIADRILL, nc->GetuViaDrill() );
m_netclassGrid->SetUnitValue( aRow, GRID_DIFF_PAIR_WIDTH, nc->GetDiffPairWidth() );
m_netclassGrid->SetUnitValue( aRow, GRID_DIFF_PAIR_GAP, nc->GetDiffPairGap() );
};
m_netclassGrid->SetCellValue( aRow, GRID_PCB_COLOR, colorAsString );
};
m_netclassGrid->ClearRows();
// enter the Default NETCLASS.
m_netclassGrid->AppendRows( 1 );
netclassToGridRow( row++, m_netSettings->m_DefaultNetClass );
netclassToGridRow( row++, m_netSettings->m_DefaultNetClass, true );
// make the Default NETCLASS name read-only
wxGridCellAttr* cellAttr = m_netclassGrid->GetOrCreateCellAttr( 0, GRID_NAME );
@ -307,7 +355,7 @@ void PANEL_SETUP_NETCLASSES::loadNetclasses()
m_netclassGrid->AppendRows( (int) m_netSettings->m_NetClasses.size() );
for( const auto& [ name, netclass ] : m_netSettings->m_NetClasses )
netclassToGridRow( row++, netclass );
netclassToGridRow( row++, netclass, false );
m_assignmentGrid->ClearRows();
m_assignmentGrid->AppendRows( m_netSettings->m_NetClassPatternAssignments.size() );
@ -396,41 +444,67 @@ bool PANEL_SETUP_NETCLASSES::TransferDataFromWindow()
int row = 0;
auto gridRowToNetclass =
[&]( int aRow, const std::shared_ptr<NETCLASS>& nc )
auto gridRowToNetclass = [&]( int aRow, const std::shared_ptr<NETCLASS>& nc, bool isDefault )
{
nc->SetName( m_netclassGrid->GetCellValue( aRow, GRID_NAME ) );
nc->SetWireWidth( m_netclassGrid->GetUnitValue( aRow, GRID_WIREWIDTH ) );
nc->SetBusWidth( m_netclassGrid->GetUnitValue( aRow, GRID_BUSWIDTH ) );
wxString color = m_netclassGrid->GetCellValue( aRow, GRID_SCHEMATIC_COLOR );
nc->SetSchematicColor( wxColour( color ) );
wxString lineStyle = m_netclassGrid->GetCellValue( aRow, GRID_LINESTYLE );
nc->SetLineStyle( g_lineStyleNames.Index( lineStyle ) );
wxASSERT_MSG( nc->GetLineStyle() >= 0, "Line style name not found." );
nc->SetClearance( m_netclassGrid->GetUnitValue( aRow, GRID_CLEARANCE ) );
nc->SetTrackWidth( m_netclassGrid->GetUnitValue( aRow, GRID_TRACKSIZE ) );
nc->SetViaDiameter( m_netclassGrid->GetUnitValue( aRow, GRID_VIASIZE ) );
nc->SetViaDrill( m_netclassGrid->GetUnitValue( aRow, GRID_VIADRILL ) );
nc->SetuViaDiameter( m_netclassGrid->GetUnitValue( aRow, GRID_uVIASIZE ) );
nc->SetuViaDrill( m_netclassGrid->GetUnitValue( aRow, GRID_uVIADRILL ) );
nc->SetDiffPairWidth( m_netclassGrid->GetUnitValue( aRow, GRID_DIFF_PAIR_WIDTH ) );
nc->SetDiffPairGap( m_netclassGrid->GetUnitValue( aRow, GRID_DIFF_PAIR_GAP ) );
if( !isDefault )
{
color = m_netclassGrid->GetCellValue( aRow, GRID_PCB_COLOR );
KIGFX::COLOR4D newPcbColor( color );
if( newPcbColor != KIGFX::COLOR4D::UNSPECIFIED )
{
nc->SetName( m_netclassGrid->GetCellValue( aRow, GRID_NAME ) );
nc->SetPcbColor( newPcbColor );
}
nc->SetWireWidth( m_netclassGrid->GetUnitValue( aRow, GRID_WIREWIDTH ) );
nc->SetBusWidth( m_netclassGrid->GetUnitValue( aRow, GRID_BUSWIDTH ) );
if( !m_isEEschema )
{
KIGFX::PCB_RENDER_SETTINGS* rs = static_cast<KIGFX::PCB_RENDER_SETTINGS*>(
m_frame->GetCanvas()->GetView()->GetPainter()->GetSettings() );
std::map<wxString, KIGFX::COLOR4D>& netclassColors = rs->GetNetclassColorMap();
wxString color = m_netclassGrid->GetCellValue( aRow, GRID_SCHEMATIC_COLOR );
nc->SetSchematicColor( wxColour( color ) );
wxString lineStyle = m_netclassGrid->GetCellValue( aRow, GRID_LINESTYLE );
nc->SetLineStyle( g_lineStyleNames.Index( lineStyle ) );
wxASSERT_MSG( nc->GetLineStyle() >= 0, "Line style name not found." );
nc->SetClearance( m_netclassGrid->GetUnitValue( aRow, GRID_CLEARANCE ) );
nc->SetTrackWidth( m_netclassGrid->GetUnitValue( aRow, GRID_TRACKSIZE ) );
nc->SetViaDiameter( m_netclassGrid->GetUnitValue( aRow, GRID_VIASIZE ) );
nc->SetViaDrill( m_netclassGrid->GetUnitValue( aRow, GRID_VIADRILL ) );
nc->SetuViaDiameter( m_netclassGrid->GetUnitValue( aRow, GRID_uVIASIZE ) );
nc->SetuViaDrill( m_netclassGrid->GetUnitValue( aRow, GRID_uVIADRILL ) );
nc->SetDiffPairWidth( m_netclassGrid->GetUnitValue( aRow, GRID_DIFF_PAIR_WIDTH ) );
nc->SetDiffPairGap( m_netclassGrid->GetUnitValue( aRow, GRID_DIFF_PAIR_GAP ) );
};
if( newPcbColor != COLOR4D::UNSPECIFIED )
{
netclassColors[nc->GetName()] = newPcbColor;
}
else
{
netclassColors.erase( nc->GetName() );
}
}
}
};
m_netSettings->m_NetClasses.clear();
// Copy the default NetClass:
gridRowToNetclass( row++, m_netSettings->m_DefaultNetClass );
gridRowToNetclass( row++, m_netSettings->m_DefaultNetClass, true );
// Copy other NetClasses:
for( row = 1; row < m_netclassGrid->GetNumberRows(); ++row )
{
auto nc = std::make_shared<NETCLASS>( m_netclassGrid->GetCellValue( row, GRID_NAME ) );
gridRowToNetclass( row, nc );
gridRowToNetclass( row, nc, false );
m_netSettings->m_NetClasses[ nc->GetName() ] = nc;
}
@ -547,6 +621,7 @@ void PANEL_SETUP_NETCLASSES::OnNetclassGridMouseEvent( wxMouseEvent& aEvent )
case GRID_BUSWIDTH: tip = _( "Bus wire thickness" ); break;
case GRID_SCHEMATIC_COLOR: tip = _( "Schematic wire color" ); break;
case GRID_LINESTYLE: tip = _( "Schematic wire line style" ); break;
case GRID_PCB_COLOR: tip = _( "PCB netclass color" ); break;
}
m_netclassGrid->GetGridColLabelWindow()->UnsetToolTip();
@ -687,6 +762,23 @@ void PANEL_SETUP_NETCLASSES::OnRemoveAssignmentClick( wxCommandEvent& event )
}
void PANEL_SETUP_NETCLASSES::OnImportColorsClick( wxCommandEvent& event )
{
std::map<wxString, std::shared_ptr<NETCLASS>>& netclasses = m_netSettings->m_NetClasses;
for( int row = 1; row < m_netclassGrid->GetNumberRows(); ++row )
{
wxString netclassName = m_netclassGrid->GetCellValue( row, GRID_NAME );
if( netclasses.find( netclassName ) != netclasses.end() )
{
const KIGFX::COLOR4D ncColor = netclasses[netclassName]->GetSchematicColor();
m_netclassGrid->SetCellValue( row, GRID_PCB_COLOR, ncColor.ToCSSString() );
}
}
}
void PANEL_SETUP_NETCLASSES::AdjustAssignmentGridColumns( int aWidth )
{
// Account for scroll bars

View File

@ -39,7 +39,7 @@ PANEL_SETUP_NETCLASSES_BASE::PANEL_SETUP_NETCLASSES_BASE( wxWindow* parent, wxWi
m_netclassGrid = new WX_GRID( m_netclassesPane, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxHSCROLL|wxTAB_TRAVERSAL|wxVSCROLL );
// Grid
m_netclassGrid->CreateGrid( 3, 13 );
m_netclassGrid->CreateGrid( 3, 14 );
m_netclassGrid->EnableEditing( true );
m_netclassGrid->EnableGridLines( true );
m_netclassGrid->EnableDragGridSize( false );
@ -57,10 +57,11 @@ PANEL_SETUP_NETCLASSES_BASE::PANEL_SETUP_NETCLASSES_BASE( wxWindow* parent, wxWi
m_netclassGrid->SetColLabelValue( 6, _("uVia Hole") );
m_netclassGrid->SetColLabelValue( 7, _("DP Width") );
m_netclassGrid->SetColLabelValue( 8, _("DP Gap") );
m_netclassGrid->SetColLabelValue( 9, _("Wire Thickness") );
m_netclassGrid->SetColLabelValue( 10, _("Bus Thickness") );
m_netclassGrid->SetColLabelValue( 11, _("Color") );
m_netclassGrid->SetColLabelValue( 12, _("Line Style") );
m_netclassGrid->SetColLabelValue( 9, _("PCB Color") );
m_netclassGrid->SetColLabelValue( 10, _("Wire Thickness") );
m_netclassGrid->SetColLabelValue( 11, _("Bus Thickness") );
m_netclassGrid->SetColLabelValue( 12, _("Color") );
m_netclassGrid->SetColLabelValue( 13, _("Line Style") );
m_netclassGrid->SetColLabelSize( wxGRID_AUTOSIZE );
m_netclassGrid->SetColLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
@ -88,14 +89,16 @@ PANEL_SETUP_NETCLASSES_BASE::PANEL_SETUP_NETCLASSES_BASE( wxWindow* parent, wxWi
m_removeButton = new STD_BITMAP_BUTTON( m_netclassesPane, wxID_ANY, wxNullBitmap, wxDefaultPosition, wxSize( -1,-1 ), wxBU_AUTODRAW|0 );
buttonBoxSizer->Add( m_removeButton, 0, wxBOTTOM|wxRIGHT|wxLEFT, 5 );
buttonBoxSizer->Add( 60, 0, 1, wxEXPAND, 5 );
m_colorDefaultHelpText = new wxStaticText( m_netclassesPane, wxID_ANY, _("Set color to transparent to use KiCad default color."), wxDefaultPosition, wxDefaultSize, 0 );
m_colorDefaultHelpText->Wrap( -1 );
buttonBoxSizer->Add( m_colorDefaultHelpText, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT, 10 );
m_importColorsButton = new wxButton( m_netclassesPane, wxID_ANY, _("Import colors from schematic") );
buttonBoxSizer->Add( m_importColorsButton, 0, wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT, 5 );
bUpperSizer->Add( buttonBoxSizer, 0, wxEXPAND|wxLEFT|wxTOP, 5 );
@ -193,6 +196,7 @@ PANEL_SETUP_NETCLASSES_BASE::PANEL_SETUP_NETCLASSES_BASE( wxWindow* parent, wxWi
m_netclassGrid->Connect( wxEVT_SIZE, wxSizeEventHandler( PANEL_SETUP_NETCLASSES_BASE::OnSizeNetclassGrid ), NULL, this );
m_addButton->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( PANEL_SETUP_NETCLASSES_BASE::OnAddNetclassClick ), NULL, this );
m_removeButton->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( PANEL_SETUP_NETCLASSES_BASE::OnRemoveNetclassClick ), NULL, this );
m_importColorsButton->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( PANEL_SETUP_NETCLASSES_BASE::OnImportColorsClick ), NULL, this );
m_membershipPane->Connect( wxEVT_SIZE, wxSizeEventHandler( PANEL_SETUP_NETCLASSES_BASE::onmembershipPanelSize ), NULL, this );
m_assignmentGrid->Connect( wxEVT_SIZE, wxSizeEventHandler( PANEL_SETUP_NETCLASSES_BASE::OnSizeAssignmentGrid ), NULL, this );
m_assignmentGrid->Connect( wxEVT_UPDATE_UI, wxUpdateUIEventHandler( PANEL_SETUP_NETCLASSES_BASE::OnUpdateUI ), NULL, this );
@ -207,6 +211,7 @@ PANEL_SETUP_NETCLASSES_BASE::~PANEL_SETUP_NETCLASSES_BASE()
m_netclassGrid->Disconnect( wxEVT_SIZE, wxSizeEventHandler( PANEL_SETUP_NETCLASSES_BASE::OnSizeNetclassGrid ), NULL, this );
m_addButton->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( PANEL_SETUP_NETCLASSES_BASE::OnAddNetclassClick ), NULL, this );
m_removeButton->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( PANEL_SETUP_NETCLASSES_BASE::OnRemoveNetclassClick ), NULL, this );
m_importColorsButton->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( PANEL_SETUP_NETCLASSES_BASE::OnImportColorsClick ), NULL, this );
m_membershipPane->Disconnect( wxEVT_SIZE, wxSizeEventHandler( PANEL_SETUP_NETCLASSES_BASE::onmembershipPanelSize ), NULL, this );
m_assignmentGrid->Disconnect( wxEVT_SIZE, wxSizeEventHandler( PANEL_SETUP_NETCLASSES_BASE::OnSizeAssignmentGrid ), NULL, this );
m_assignmentGrid->Disconnect( wxEVT_UPDATE_UI, wxUpdateUIEventHandler( PANEL_SETUP_NETCLASSES_BASE::OnUpdateUI ), NULL, this );

View File

@ -48,6 +48,7 @@ class PANEL_SETUP_NETCLASSES_BASE : public wxPanel
WX_GRID* m_netclassGrid;
STD_BITMAP_BUTTON* m_addButton;
STD_BITMAP_BUTTON* m_removeButton;
wxButton* m_importColorsButton;
wxStaticText* m_colorDefaultHelpText;
WX_PANEL* m_membershipPane;
wxStaticText* m_staticText5;
@ -61,6 +62,7 @@ class PANEL_SETUP_NETCLASSES_BASE : public wxPanel
virtual void OnSizeNetclassGrid( wxSizeEvent& event ) { event.Skip(); }
virtual void OnAddNetclassClick( wxCommandEvent& event ) { event.Skip(); }
virtual void OnRemoveNetclassClick( wxCommandEvent& event ) { event.Skip(); }
virtual void OnImportColorsClick( wxCommandEvent& event ) { event.Skip(); }
virtual void onmembershipPanelSize( wxSizeEvent& event ) { event.Skip(); }
virtual void OnSizeAssignmentGrid( wxSizeEvent& event ) { event.Skip(); }
virtual void OnAddAssignmentClick( wxCommandEvent& event ) { event.Skip(); }

View File

@ -51,6 +51,7 @@ public:
private:
void OnAddNetclassClick( wxCommandEvent& event ) override;
void OnRemoveNetclassClick( wxCommandEvent& event ) override;
void OnImportColorsClick( wxCommandEvent& event ) override;
void OnSizeNetclassGrid( wxSizeEvent& event ) override;
void OnSizeAssignmentGrid( wxSizeEvent& event ) override;
void OnAddAssignmentClick( wxCommandEvent& event ) override;