KiCad PCB EDA Suite
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 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 <grid_layer_box_helpers.h>
26 #include <class_drawsegment.h>
27 #include <class_track.h>
28 #include <view/view.h>
29 #include <widgets/wx_grid.h>
30 #include <class_zone.h>
31 #include <class_board.h>
32 #include "dialog_swap_layers.h"
33 
34 
35 class LAYER_GRID_TABLE : public wxGridTableBase
36 {
39 
40 public:
41  LAYER_GRID_TABLE( int layerCount ) : m_layerCount( layerCount )
42  { }
43 
44  int GetNumberRows() override { return m_layerCount; }
45  int GetNumberCols() override { return 2; }
46 
47  wxString GetColLabelValue( int aCol ) override
48  {
49  switch( aCol )
50  {
51  case 0: return _( "Move items on:" );
52  case 1: return _( "To layer:" );
53  default: return wxEmptyString;
54  }
55  }
56 
57  wxString GetValue( int row, int col ) override { return "undefined"; }
58  void SetValue( int row, int col, const wxString& value ) override { }
59 
60  long GetValueAsLong( int row, int col ) override
61  {
62  return m_layers[ row ][ col ];
63  }
64 
65  void SetValueAsLong( int row, int col, long value ) override
66  {
67  m_layers[ row ][ col ] = value;
68  }
69 };
70 
71 
73  DIALOG_SWAP_LAYERS_BASE( aParent ),
74  m_parent( aParent ),
75  m_layerDestinations( aArray )
76 {
79  m_grid->SetDefaultRowSize( m_grid->GetDefaultRowSize() + 4 );
80  m_grid->SetCellHighlightROPenWidth( 0 );
81 
82  m_sdbSizerOK->SetDefault();
83 
85 }
86 
87 
89 {
91 }
92 
93 
95 {
96  LSET enabledCopperLayers = LSET::AllCuMask( m_parent->GetBoard()->GetCopperLayerCount() );
97  int row = 0;
98 
99  for( size_t layer = 0; layer < PCB_LAYER_ID_COUNT; ++layer )
100  {
101  if( enabledCopperLayers.test( layer ) )
102  {
103  auto attr = new wxGridCellAttr;
104  attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( m_parent ) );
105  attr->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_MENU ) );
106  attr->SetReadOnly();
107  m_grid->SetAttr( row, 0, attr );
108 
109  attr = new wxGridCellAttr;
110  attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( m_parent ) );
111  attr->SetEditor( new GRID_CELL_LAYER_SELECTOR( m_parent, LSET::AllNonCuMask() ) );
112  m_grid->SetAttr( row, 1, attr );
113 
114  m_grid->GetTable()->SetValueAsLong( row, 0, (long) layer );
115  m_grid->GetTable()->SetValueAsLong( row, 1, (long) layer );
116 
117  ++row;
118  }
119  }
120 
121  return true;
122 }
123 
124 
126 {
127  if( !m_grid->CommitPendingChanges() )
128  return false;
129 
130  LSET enabledCopperLayers = LSET::AllCuMask( m_parent->GetBoard()->GetCopperLayerCount() );
131  wxGridTableBase* table = m_grid->GetTable();
132  int row = 0;
133 
134  for( size_t layer = 0; layer < PCB_LAYER_ID_COUNT; ++layer )
135  {
136  if( enabledCopperLayers.test( layer ) )
137  m_layerDestinations[ layer ] = (PCB_LAYER_ID) table->GetValueAsLong( row++, 1 );
138  else
139  m_layerDestinations[ layer ] = (PCB_LAYER_ID) layer;
140  }
141 
142  return true;
143 }
144 
145 
147 {
148  // Account for scroll bars
149  aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
150 
151  m_grid->SetColSize( 0, aWidth / 2 );
152  m_grid->SetColSize( 1, aWidth - m_grid->GetColSize( 0 ) );
153 }
154 
155 
156 void DIALOG_SWAP_LAYERS::OnSize( wxSizeEvent& event )
157 {
158  adjustGridColumns( event.GetSize().GetX() );
159 
160  event.Skip();
161 }
162 
163 
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Function AllCuMask returns a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:686
int GetNumberCols() override
PCB_LAYER_ID * m_layerDestinations
void SetValue(int row, int col, const wxString &value) override
wxString GetColLabelValue(int aCol) override
static LSET AllNonCuMask()
Function AllNonCuMask returns a mask holding all layer minus CU layers.
Definition: lset.cpp:709
int m_layers[MAX_CU_LAYERS][2]
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
LAYER_GRID_TABLE(int layerCount)
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:57
Definitions for tracks, vias and zones.
wxString GetValue(int row, int col) override
int GetNumberRows() override
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:88
long GetValueAsLong(int row, int col) override
void adjustGridColumns(int aWidth)
PCB_LAYER_ID
A quick note on layer IDs:
LSET is a set of PCB_LAYER_IDs.
Class DIALOG_SWAP_LAYERS_BASE.
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:172
DIALOG_SWAP_LAYERS(PCB_BASE_EDIT_FRAME *aParent, PCB_LAYER_ID *aArray)
Class to handle a graphic segment.
Common, abstract interface for edit frames.
#define _(s)
Definition: 3d_actions.cpp:31
int GetCopperLayerCount() const
Function GetCopperLayerCount.
bool TransferDataFromWindow() override
BOARD * GetBoard() const
void OnSize(wxSizeEvent &event) override
PCB_BASE_EDIT_FRAME * m_parent
bool TransferDataToWindow() override
LAYER_GRID_TABLE * m_gridTable
void SetValueAsLong(int row, int col, long value) override