KiCad PCB EDA Suite
dialog_print_gerbview.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) 2010-2016 Jean-Pierre Charras jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  * Copyright (C) 2018 CERN
7  * Author: Maciej Suminski <maciej.suminski@cern.ch>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #include <kiface_base.h>
28 #include <confirm.h>
30 #include <gerbview_printout.h>
31 #include <gerbview.h>
32 #include <gerbview_frame.h>
33 #include <gerber_file_image.h>
34 #include <gerber_file_image_list.h>
35 #include <tool/tool_manager.h>
36 #include <tools/gerbview_actions.h>
37 #include <tools/gerbview_control.h>
38 #include <wx/checklst.h>
39 
40 // TODO(JE)
41 #define OPTKEY_LAYERBASE wxT( "PlotLayer_%d" )
42 
43 
45 {
46 public:
49 
50 private:
52  {
53  wxASSERT( dynamic_cast<BOARD_PRINTOUT_SETTINGS*>( m_settings ) );
54  return static_cast<BOARD_PRINTOUT_SETTINGS*>( m_settings );
55  }
56 
57  bool TransferDataToWindow() override;
58 
59  void createExtraOptions();
60  void createLeftPanel();
61 
62  void onSelectAllClick( wxCommandEvent& event );
63  void onDeselectAllClick( wxCommandEvent& event );
64 
66  void setListBoxValue( wxCheckListBox* aList, bool aValue );
67 
69  bool isLayerEnabled( unsigned int aLayer ) const;
70 
72  void enableLayer( unsigned int aLayer, bool aValue );
73 
75  int setLayerSetFromList();
76 
77  void saveSettings() override;
78 
79  wxPrintout* createPrintout( const wxString& aTitle ) override
80  {
82  m_parent->GetCanvas()->GetView(), aTitle );
83  }
84 
86 
87  // Number of layers in each list
88  static constexpr unsigned int LAYER_PER_LIST = 16;
89 
90  // Number of layer list widgets
91  static constexpr unsigned int LAYER_LIST_COUNT = 2;
92 
93  // Extra widgets
94  wxCheckListBox* m_layerLists[LAYER_LIST_COUNT];
95  wxButton* m_buttonSelectAll;
97  wxCheckBox* m_checkboxMirror;
98 
99  // Map layer numbers to items on the list
100  std::unordered_map<int, int> m_layerToItemMap;
101 };
102 
103 
105  BOARD_PRINTOUT_SETTINGS* aSettings ) :
106  DIALOG_PRINT_GENERIC( aParent, aSettings ),
107  m_parent( aParent )
108 {
110 
112  createLeftPanel();
113 }
114 
115 
117 {
119  return false;
120 
122  int itemIdx = 0;
123 
124  // Create layer list
125  for( unsigned ii = 0; ii < images->ImagesMaxCount(); ++ii )
126  {
127  wxString layerName;
128  unsigned int listIdx = itemIdx / LAYER_PER_LIST;
129 
130  if( listIdx >= LAYER_LIST_COUNT )
131  {
132  wxFAIL;
133  break;
134  }
135 
136  GERBER_FILE_IMAGE* gbrImage = images->GetGbrImage( ii );
137 
138  if( !gbrImage )
139  continue;
140 
141  wxFileName filename( gbrImage->m_FileName );
142  wxCheckListBox* listBox = m_layerLists[listIdx];
143  listBox->Append( filename.GetFullName() );
144 
145  if( settings()->m_LayerSet.test( ii) )
146  listBox->Check( ii, true );
147 
148  wxASSERT( m_layerToItemMap.count( ii ) == 0 );
149  m_layerToItemMap[ii] = itemIdx;
150 
151  ++itemIdx;
152  }
153 
154  m_checkboxMirror->SetValue( settings()->m_Mirror );
155 
156  // Update the dialog layout when layers are added
157  GetSizer()->Fit( this );
158 
159  return true;
160 }
161 
162 
164 {
165  wxGridBagSizer* optionsSizer = getOptionsSizer();
166  wxStaticBox* box = getOptionsBox();
167  int rows = optionsSizer->GetEffectiveRowsCount();
168  int cols = optionsSizer->GetEffectiveColsCount();
169 
170  // Print mirrored
171  m_checkboxMirror = new wxCheckBox( box, wxID_ANY, _( "Print mirrored" ) );
172  optionsSizer->Add( m_checkboxMirror, wxGBPosition( rows, 0 ), wxGBSpan( 1, cols ),
173  wxBOTTOM | wxRIGHT | wxLEFT, 5 );
174 }
175 
176 
178 {
179  wxStaticBoxSizer* sbLayersSizer =
180  new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, _( "Included Layers" ) ),
181  wxVERTICAL );
182 
183  // Layer lists
184  wxBoxSizer* bLayerListsSizer = new wxBoxSizer( wxHORIZONTAL );
185 
186  for( unsigned int i = 0; i < LAYER_LIST_COUNT; ++i )
187  {
188  m_layerLists[i] = new wxCheckListBox( sbLayersSizer->GetStaticBox(), wxID_ANY );
189  bLayerListsSizer->Add( m_layerLists[i], 1, wxEXPAND, 5 );
190  }
191 
192 
193  // Select/Unselect all buttons
194  m_buttonSelectAll = new wxButton( sbLayersSizer->GetStaticBox(), wxID_ANY, _( "Select all" ) );
195  m_buttonDeselectAll = new wxButton( sbLayersSizer->GetStaticBox(), wxID_ANY,
196  _( "Deselect all" ) );
197 
198  m_buttonSelectAll->Connect( wxEVT_COMMAND_BUTTON_CLICKED,
199  wxCommandEventHandler( DIALOG_PRINT_GERBVIEW::onSelectAllClick ),
200  nullptr, this );
201  m_buttonDeselectAll->Connect( wxEVT_COMMAND_BUTTON_CLICKED,
202  wxCommandEventHandler( DIALOG_PRINT_GERBVIEW::onDeselectAllClick ),
203  nullptr, this );
204 
205  wxBoxSizer* buttonSizer = new wxBoxSizer( wxHORIZONTAL );
206  buttonSizer->Add( m_buttonSelectAll, 1, wxALL, 5 );
207  buttonSizer->Add( m_buttonDeselectAll, 1, wxALL, 5 );
208 
209  // Static box sizer layout
210  sbLayersSizer->Add( bLayerListsSizer, 1, wxALL | wxEXPAND, 5 );
211  sbLayersSizer->Add( buttonSizer, 0, wxALL | wxEXPAND, 5 );
212 
213  getMainSizer()->Insert( 0, sbLayersSizer, 1, wxEXPAND );
214 }
215 
216 
217 void DIALOG_PRINT_GERBVIEW::onSelectAllClick( wxCommandEvent& event )
218 {
219  for( unsigned int i = 0; i < LAYER_LIST_COUNT; ++i )
220  setListBoxValue( m_layerLists[i], true );
221 }
222 
223 
224 void DIALOG_PRINT_GERBVIEW::onDeselectAllClick( wxCommandEvent& event )
225 {
226  for( unsigned int i = 0; i < LAYER_LIST_COUNT; ++i )
227  setListBoxValue( m_layerLists[i], false );
228 }
229 
230 
231 void DIALOG_PRINT_GERBVIEW::setListBoxValue( wxCheckListBox* aList, bool aValue )
232 {
233  for( unsigned int i = 0; i < aList->GetCount(); ++i )
234  aList->Check( i, aValue );
235 }
236 
237 
238 bool DIALOG_PRINT_GERBVIEW::isLayerEnabled( unsigned int aLayer ) const
239 {
240  auto layerMapIt = m_layerToItemMap.find( aLayer );
241 
242  if( layerMapIt == m_layerToItemMap.end() )
243  return false;
244 
245  unsigned int itemNr = layerMapIt->second;
246  unsigned int listIdx = itemNr / LAYER_PER_LIST;
247  unsigned int itemIdx = itemNr % LAYER_PER_LIST;
248  wxCHECK( listIdx < LAYER_LIST_COUNT, false );
249  wxCheckListBox* listBox = m_layerLists[listIdx];
250 
251  return itemIdx < listBox->GetCount() && listBox->IsChecked( itemIdx );
252 }
253 
254 
255 void DIALOG_PRINT_GERBVIEW::enableLayer( unsigned int aLayer, bool aValue )
256 {
257  auto layerMapIt = m_layerToItemMap.find( aLayer );
258 
259  if( layerMapIt == m_layerToItemMap.end() )
260  return;
261 
262  unsigned int itemNr = layerMapIt->second;
263  unsigned int listIdx = itemNr / LAYER_PER_LIST;
264  unsigned int itemIdx = itemNr % LAYER_PER_LIST;
265  wxCHECK( listIdx < LAYER_LIST_COUNT, /* void */ );
266  wxCheckListBox* listBox = m_layerLists[listIdx];
267 
268  if( itemIdx < listBox->GetCount() )
269  listBox->Check( itemIdx, aValue );
270 }
271 
272 
274 {
275  settings()->m_LayerSet = LSET();
276  int& pageCount = settings()->m_pageCount;
277  pageCount = 0;
278 
279  unsigned int layer = 0;
280 
281  for( unsigned int j = 0; j < LAYER_LIST_COUNT; ++j )
282  {
283  for( unsigned int i = 0; i < LAYER_PER_LIST; ++i )
284  {
285  if( isLayerEnabled( layer ) )
286  {
287  settings()->m_LayerSet.set( layer );
288  ++pageCount;
289  }
290 
291  ++layer;
292  }
293  }
294 
295  return pageCount;
296 }
297 
298 
300 {
302 
303  settings()->m_Mirror = m_checkboxMirror->GetValue();
304 
306 }
307 
308 
310 {
311  // Selection affects the original item visibility
313 
316  DIALOG_PRINT_GERBVIEW dlg( m_frame, &settings );
317  dlg.ForcePrintBorder( false );
318  dlg.ShowModal();
319 
320  return 0;
321 }
void onDeselectAllClick(wxCommandEvent &event)
(Un)check all items in a checklist box
static TOOL_ACTION selectionClear
Clear the current selection.
static constexpr unsigned int LAYER_PER_LIST
GERBVIEW_FRAME * m_frame
This file is part of the common library.
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
BOARD_PRINTOUT_SETTINGS * settings() const
int Print(const TOOL_EVENT &aEvent)
Set up handlers for various events.
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:214
virtual EDA_DRAW_PANEL_GAL * GetCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Hold the image data and parameters for one gerber file and layer parameters.
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:143
LSET m_LayerSet
Layers to print.
GERBER_FILE_IMAGE_LIST is a helper class to handle a list of GERBER_FILE_IMAGE files which are loaded...
wxStaticBox * getOptionsBox()
COLOR_SETTINGS * m_colorSettings
The color settings to be used for printing.
Definition: printout.h:66
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:502
wxPrintout * createPrintout(const wxString &aTitle) override
Create a printout with a requested title.
GERBER_FILE_IMAGE * GetGbrImage(int aIdx)
bool TransferDataToWindow() override
PRINTOUT_SETTINGS * m_settings
Generic, UI-independent tool event.
Definition: tool_event.h:152
#define _(s)
virtual KIGFX::VIEW * GetView() const
Return a pointer to the #VIEW instance used in the panel.
static constexpr unsigned int LAYER_LIST_COUNT
std::unordered_map< int, int > m_layerToItemMap
wxCheckListBox * m_layerLists[LAYER_LIST_COUNT]
GBR_LAYOUT * GetGerberLayout() const
void setListBoxValue(wxCheckListBox *aList, bool aValue)
Check whether a layer is enabled in a listbox.
DIALOG_PRINT_GERBVIEW(GERBVIEW_FRAME *aParent, BOARD_PRINTOUT_SETTINGS *aSettings)
void onSelectAllClick(wxCommandEvent &event)
const PAGE_INFO & GetPageSettings() const override
bool TransferDataToWindow() override
int m_pageCount
Number of pages to print.
Definition: printout.h:61
GERBER_FILE_IMAGE_LIST * GetImagesList() const
Definition: gbr_layout.cpp:41
bool isLayerEnabled(unsigned int aLayer) const
Enable/disable layer in a listbox.
APP_SETTINGS_BASE * KifaceSettings() const
Definition: kiface_base.h:92
wxGridBagSizer * getOptionsSizer()
virtual COLOR_SETTINGS * GetColorSettings() const
Returns a pointer to the active color theme settings.
void ForcePrintBorder(bool aValue)
Set 'print border and title block' to a requested value and hides the corresponding checkbox.
bool m_Mirror
Print mirrored.
APP_SETTINGS_BASE * m_config
void enableLayer(unsigned int aLayer, bool aValue)
Update layerset basing on the selected layers.