KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_swap_layers.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <pcb_base_edit_frame.h>
21#include <board.h>
23#include <kiplatform/ui.h>
24#include <widgets/wx_grid.h>
25#include "dialog_swap_layers.h"
26
27
28class LAYER_GRID_TABLE : public wxGridTableBase
29{
30 std::vector<std::pair<PCB_LAYER_ID, PCB_LAYER_ID>> m_layers;
32
33public:
34 LAYER_GRID_TABLE( int layerCount ) : m_layerCount( layerCount )
35 { }
36
37 int GetNumberRows() override { return m_layerCount; }
38 int GetNumberCols() override { return 2; }
39
40 wxString GetColLabelValue( int aCol ) override
41 {
42 switch( aCol )
43 {
44 case 0: return _( "Move items on:" );
45 case 1: return _( "To layer:" );
46 default: return wxEmptyString;
47 }
48 }
49
50 wxString GetValue( int row, int col ) override { return "undefined"; }
51 void SetValue( int row, int col, const wxString& value ) override { }
52
53 long GetValueAsLong( int row, int col ) override
54 {
55 if( row < 0 || row >= m_layerCount )
56 return -1;
57
58 if( col < 0 || col >= 2 )
59 return -1;
60
61 return col == 0 ? m_layers[ row ].first : m_layers[ row ].second;
62 }
63
64 void SetValueAsLong( int row, int col, long value ) override
65 {
66 if( row < 0 || col < 0 || col >= 2 )
67 return;
68
69 // Ensure there is room in m_layers to store value
70 while( row >= (int)m_layers.size() )
71 {
72 m_layers.emplace_back( F_Cu, F_Cu );
73 }
74
75 col == 0 ? m_layers[row].first = ToLAYER_ID( value )
76 : m_layers[row].second = ToLAYER_ID( value );
77 }
78};
79
80
82 std::map<PCB_LAYER_ID, PCB_LAYER_ID>& aLayerMap ) :
83 DIALOG_SWAP_LAYERS_BASE( aParent ),
84 m_parent( aParent ),
85 m_layerMap( aLayerMap )
86{
87 m_gridTable = new LAYER_GRID_TABLE( m_parent->GetBoard()->GetCopperLayerCount() );
88 m_grid->SetTable( m_gridTable );
89 m_grid->SetMinSize( FromDIP( m_grid->GetMinSize() ) );
90 m_grid->SetCellHighlightROPenWidth( 0 );
91 m_grid->SetUseNativeColLabels();
92
94
96}
97
98
103
104
106{
107 LSET enabledCopperLayers = LSET::AllCuMask( m_parent->GetBoard()->GetCopperLayerCount() );
108 int row = 0;
109
110 LSEQ enabledCopperLayerUIList = enabledCopperLayers.UIOrder();
111
112 for( PCB_LAYER_ID layer : enabledCopperLayerUIList )
113 {
114 auto attr = new wxGridCellAttr;
115 attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( m_parent ) );
116 attr->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_MENU ) );
117 attr->SetReadOnly();
118 m_grid->SetAttr( row, 0, attr );
119
120 attr = new wxGridCellAttr;
121 attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( m_parent ) );
122 attr->SetEditor( new GRID_CELL_LAYER_SELECTOR( m_parent, LSET::AllNonCuMask() ) );
123 m_grid->SetAttr( row, 1, attr );
124 m_grid->GetTable()->SetValueAsLong( row, 0, (long) layer );
125 m_grid->GetTable()->SetValueAsLong( row, 1, (long) layer );
126
127 ++row;
128 }
129
130 return true;
131}
132
133
135{
136 if( !m_grid->CommitPendingChanges() )
137 return false;
138
139 LSET enabledCopperLayers = LSET::AllCuMask( m_parent->GetBoard()->GetCopperLayerCount() );
140 wxGridTableBase* table = m_grid->GetTable();
141 int row = 0;
142
143 LSEQ enabledCopperLayerUIList = enabledCopperLayers.UIOrder();
144
145 for( PCB_LAYER_ID layer : enabledCopperLayerUIList )
146 {
147 int dest = table->GetValueAsLong( row++, 1 );
148
149 if( dest >= 0 && dest < PCB_LAYER_ID_COUNT && enabledCopperLayers.test( dest ) )
150 m_layerMap[ layer ] = ToLAYER_ID( dest );
151 }
152
153 return true;
154}
155
156
158{
159 // Account for scroll bars
161
162 m_grid->SetColSize( 0, std::max( FromDIP( 40 ), width / 2 ) );
163 m_grid->SetColSize( 1, std::max( FromDIP( 40 ), width - m_grid->GetColSize( 0 ) ) );
164}
165
166
167void DIALOG_SWAP_LAYERS::OnSize( wxSizeEvent& event )
168{
170
171 event.Skip();
172}
173
174
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
DIALOG_SWAP_LAYERS_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Swap Layers"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
bool TransferDataToWindow() override
bool TransferDataFromWindow() override
std::map< PCB_LAYER_ID, PCB_LAYER_ID > & m_layerMap
PCB_BASE_EDIT_FRAME * m_parent
DIALOG_SWAP_LAYERS(PCB_BASE_EDIT_FRAME *aParent, std::map< PCB_LAYER_ID, PCB_LAYER_ID > &aArray)
void OnSize(wxSizeEvent &event) override
LAYER_GRID_TABLE * m_gridTable
void SetValue(int row, int col, const wxString &value) override
void SetValueAsLong(int row, int col, long value) override
int GetNumberRows() override
std::vector< std::pair< PCB_LAYER_ID, PCB_LAYER_ID > > m_layers
wxString GetValue(int row, int col) override
LAYER_GRID_TABLE(int layerCount)
wxString GetColLabelValue(int aCol) override
int GetNumberCols() override
long GetValueAsLong(int row, int col) override
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
Definition lseq.h:47
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static const LSET & AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:604
LSEQ UIOrder() const
Return the copper, technical and user layers in the order shown in layer widget.
Definition lset.cpp:739
static LSET AllNonCuMask()
Return a mask holding all layer minus CU layers.
Definition lset.cpp:623
Common, abstract interface for edit frames.
#define _(s)
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ PCB_LAYER_ID_COUNT
Definition layer_ids.h:167
@ F_Cu
Definition layer_ids.h:60
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition lset.cpp:750
wxSize GetUnobscuredSize(const wxWindow *aWindow)
Tries to determine the size of the viewport of a scrollable widget (wxDataViewCtrl,...
Definition wxgtk/ui.cpp:294
std::vector< std::vector< std::string > > table