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 (C) 2018-2024 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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <pcb_base_edit_frame.h>
25#include <board.h>
27#include <kiplatform/ui.h>
28#include <widgets/wx_grid.h>
29#include "dialog_swap_layers.h"
30
31
32class LAYER_GRID_TABLE : public wxGridTableBase
33{
34 std::vector<std::pair<PCB_LAYER_ID, PCB_LAYER_ID>> m_layers;
36
37public:
38 LAYER_GRID_TABLE( int layerCount ) : m_layerCount( layerCount )
39 { }
40
41 int GetNumberRows() override { return m_layerCount; }
42 int GetNumberCols() override { return 2; }
43
44 wxString GetColLabelValue( int aCol ) override
45 {
46 switch( aCol )
47 {
48 case 0: return _( "Move items on:" );
49 case 1: return _( "To layer:" );
50 default: return wxEmptyString;
51 }
52 }
53
54 wxString GetValue( int row, int col ) override { return "undefined"; }
55 void SetValue( int row, int col, const wxString& value ) override { }
56
57 long GetValueAsLong( int row, int col ) override
58 {
59 if( row < 0 || row >= m_layerCount )
60 return -1;
61
62 if( col < 0 || col >= 2 )
63 return -1;
64
65 return col == 0 ? m_layers[ row ].first : m_layers[ row ].second;
66 }
67
68 void SetValueAsLong( int row, int col, long value ) override
69 {
70 if( row < 0 || col < 0 || col >= 2 )
71 return;
72
73 // Ensure there is room in m_layers to store value
74 while( row >= (int)m_layers.size() )
75 {
76 m_layers.emplace_back( F_Cu, F_Cu );
77 }
78
79 col == 0 ? m_layers[row].first = ToLAYER_ID( value )
80 : m_layers[row].second = ToLAYER_ID( value );
81 }
82};
83
84
86 std::map<PCB_LAYER_ID, PCB_LAYER_ID>& aLayerMap ) :
87 DIALOG_SWAP_LAYERS_BASE( aParent ),
88 m_parent( aParent ),
89 m_layerMap( aLayerMap )
90{
93 m_grid->SetMinSize( FromDIP( m_grid->GetMinSize() ) );
94 m_grid->SetDefaultRowSize( m_grid->GetDefaultRowSize() + FromDIP( 4 ) );
95 m_grid->SetCellHighlightROPenWidth( 0 );
96 m_grid->SetUseNativeColLabels();
97
99
101}
102
103
105{
107}
108
109
111{
112 LSET enabledCopperLayers = LSET::AllCuMask( m_parent->GetBoard()->GetCopperLayerCount() );
113 int row = 0;
114
115 LSEQ enabledCopperLayerUIList = enabledCopperLayers.UIOrder();
116
117 for( PCB_LAYER_ID layer : enabledCopperLayerUIList )
118 {
119 auto attr = new wxGridCellAttr;
120 attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( m_parent ) );
121 attr->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_MENU ) );
122 attr->SetReadOnly();
123 m_grid->SetAttr( row, 0, attr );
124
125 attr = new wxGridCellAttr;
126 attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( m_parent ) );
127 attr->SetEditor( new GRID_CELL_LAYER_SELECTOR( m_parent, LSET::AllNonCuMask() ) );
128 m_grid->SetAttr( row, 1, attr );
129 m_grid->GetTable()->SetValueAsLong( row, 0, (long) layer );
130 m_grid->GetTable()->SetValueAsLong( row, 1, (long) layer );
131
132 ++row;
133 }
134
135 return true;
136}
137
138
140{
142 return false;
143
144 LSET enabledCopperLayers = LSET::AllCuMask( m_parent->GetBoard()->GetCopperLayerCount() );
145 wxGridTableBase* table = m_grid->GetTable();
146 int row = 0;
147
148 LSEQ enabledCopperLayerUIList = enabledCopperLayers.UIOrder();
149
150 for( PCB_LAYER_ID layer : enabledCopperLayerUIList )
151 {
152 int dest = table->GetValueAsLong( row++, 1 );
153
154 if( dest >= 0 && dest < PCB_LAYER_ID_COUNT && enabledCopperLayers.test( dest ) )
155 m_layerMap[ layer ] = ToLAYER_ID( dest );
156 }
157
158 return true;
159}
160
161
163{
164 // Account for scroll bars
166
167 m_grid->SetColSize( 0, std::max( FromDIP( 40 ), width / 2 ) );
168 m_grid->SetColSize( 1, std::max( FromDIP( 40 ), width - m_grid->GetColSize( 0 ) ) );
169}
170
171
172void DIALOG_SWAP_LAYERS::OnSize( wxSizeEvent& event )
173{
175
176 event.Skip();
177}
178
179
int GetCopperLayerCount() const
Definition: board.cpp:741
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...
Class DIALOG_SWAP_LAYERS_BASE.
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:36
LSEQ UIOrder() const
Returns the copper, technical and user layers in the order shown in layer widget.
Definition: lset.cpp:809
static LSET AllNonCuMask()
Return a mask holding all layer minus CU layers.
Definition: lset.cpp:697
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:686
Common, abstract interface for edit frames.
BOARD * GetBoard() const
void SetTable(wxGridTableBase *table, bool aTakeOwnership=false)
Hide wxGrid's SetTable() method with one which doesn't mess up the grid column widths when setting th...
Definition: wx_grid.cpp:270
void DestroyTable(wxGridTableBase *aTable)
Work-around for a bug in wxGrid which crashes when deleting the table if the cell edit control was no...
Definition: wx_grid.cpp:443
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:637
#define _(s)
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ PCB_LAYER_ID_COUNT
Definition: layer_ids.h:135
@ F_Cu
Definition: layer_ids.h:64
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:820
wxSize GetUnobscuredSize(const wxWindow *aWindow)
Tries to determine the size of the viewport of a scrollable widget (wxDataViewCtrl,...
Definition: wxgtk/ui.cpp:195