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-2016 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 <fctsys.h>
28 
29 #include <kiface_i.h>
30 #include <common.h>
31 #include <class_drawpanel.h>
32 #include <confirm.h>
33 
35 #include <gerbview_printout.h>
36 
37 #include <gerbview.h>
38 #include <gerbview_frame.h>
39 #include <gerber_file_image.h>
40 #include <gerber_file_image_list.h>
41 
42 #include <tool/tool_manager.h>
43 #include <tools/gerbview_actions.h>
44 
47 
48 #define OPTKEY_LAYERBASE wxT( "PlotLayer_%d" )
49 #define OPTKEY_PRINT_X_FINESCALE_ADJ wxT( "PrintXFineScaleAdj" )
50 #define OPTKEY_PRINT_Y_FINESCALE_ADJ wxT( "PrintYFineScaleAdj" )
51 #define OPTKEY_PRINT_SCALE wxT( "PrintScale" )
52 #define OPTKEY_PRINT_PAGE_FRAME wxT( "PrintPageFrame" )
53 #define OPTKEY_PRINT_MONOCHROME_MODE wxT( "PrintMonochrome" )
54 
56 
57 
59 {
60 public:
63 
64 private:
66  {
67  wxASSERT( dynamic_cast<BOARD_PRINTOUT_SETTINGS*>( m_settings ) );
68  return static_cast<BOARD_PRINTOUT_SETTINGS*>( m_settings );
69  }
70 
71  bool TransferDataToWindow() override;
72 
73  void createExtraOptions();
74  void createLeftPanel();
75 
76  void onSelectAllClick( wxCommandEvent& event );
77  void onDeselectAllClick( wxCommandEvent& event );
78 
80  void setListBoxValue( wxCheckListBox* aList, bool aValue );
81 
83  bool isLayerEnabled( unsigned int aLayer ) const;
84 
86  void enableLayer( unsigned int aLayer, bool aValue );
87 
89  int setLayerSetFromList();
90 
91  void saveSettings() override;
92 
93  wxPrintout* createPrintout( const wxString& aTitle ) override
94  {
96  m_parent->GetGalCanvas()->GetView(), aTitle );
97  }
98 
100 
101  // Number of layers in each list
102  static constexpr unsigned int LAYER_PER_LIST = 16;
103 
104  // Number of layer list widgets
105  static constexpr unsigned int LAYER_LIST_COUNT = 2;
106 
107  // Extra widgets
108  wxCheckListBox* m_layerLists[LAYER_LIST_COUNT];
109  wxButton* m_buttonSelectAll;
111  wxCheckBox* m_checkboxMirror;
112 
113  // Map layer numbers to items on the list
114  std::unordered_map<int, int> m_layerToItemMap;
115 };
116 
117 
119  DIALOG_PRINT_GENERIC( aParent, aSettings ), m_parent( aParent )
120 {
122 
124  createLeftPanel();
125 }
126 
127 
129 {
131  return false;
132 
134  int itemIdx = 0;
135 
136  // Create layer list
137  for( unsigned ii = 0; ii < images->ImagesMaxCount(); ++ii )
138  {
139  wxString layerName;
140  unsigned int listIdx = itemIdx / LAYER_PER_LIST;
141 
142  if( listIdx >= LAYER_LIST_COUNT )
143  {
144  wxFAIL;
145  break;
146  }
147 
148  GERBER_FILE_IMAGE* gbrImage = images->GetGbrImage( ii );
149 
150  if( !gbrImage )
151  continue;
152 
153  wxFileName filename( gbrImage->m_FileName );
154  wxCheckListBox* listBox = m_layerLists[listIdx];
155  listBox->Append( filename.GetFullName() );
156 
157  if( settings()->m_layerSet.test(ii) )
158  listBox->Check( ii, true );
159 
160  wxASSERT( m_layerToItemMap.count( ii ) == 0 );
161  m_layerToItemMap[ii] = itemIdx;
162 
163  ++itemIdx;
164  }
165 
166  m_checkboxMirror->SetValue( settings()->m_mirror );
167 
168  // Update the dialog layout when layers are added
169  GetSizer()->Fit( this );
170 
171  return true;
172 }
173 
174 
176 {
177  wxGridBagSizer* optionsSizer = getOptionsSizer();
178  wxStaticBox* box = getOptionsBox();
179  int rows = optionsSizer->GetEffectiveRowsCount();
180  int cols = optionsSizer->GetEffectiveColsCount();
181 
182  // Print mirrored
183  m_checkboxMirror = new wxCheckBox( box, wxID_ANY, _( "Print mirrored" ) );
184  optionsSizer->Add( m_checkboxMirror, wxGBPosition( rows, 0 ), wxGBSpan( 1, cols ),
185  wxBOTTOM | wxRIGHT | wxLEFT, 5 );
186 }
187 
188 
190 {
191  wxStaticBoxSizer* sbLayersSizer = new wxStaticBoxSizer( new wxStaticBox( this,
192  wxID_ANY, _( "Included Layers" ) ), wxVERTICAL );
193 
194  // Layer lists
195  wxBoxSizer* bLayerListsSizer = new wxBoxSizer( wxHORIZONTAL );
196 
197  for( unsigned int i = 0; i < LAYER_LIST_COUNT; ++i )
198  {
199  m_layerLists[i] = new wxCheckListBox( sbLayersSizer->GetStaticBox(), wxID_ANY );
200  bLayerListsSizer->Add( m_layerLists[i], 1, wxEXPAND, 5 );
201  }
202 
203 
204  // Select/Unselect all buttons
205  m_buttonSelectAll = new wxButton( sbLayersSizer->GetStaticBox(), wxID_ANY, _( "Select all" ) );
206  m_buttonDeselectAll = new wxButton( sbLayersSizer->GetStaticBox(), wxID_ANY, _( "Deselect all" ) );
207 
208  m_buttonSelectAll->Connect( wxEVT_COMMAND_BUTTON_CLICKED,
209  wxCommandEventHandler( DIALOG_PRINT_GERBVIEW::onSelectAllClick ), NULL, this );
210  m_buttonDeselectAll->Connect( wxEVT_COMMAND_BUTTON_CLICKED,
211  wxCommandEventHandler( DIALOG_PRINT_GERBVIEW::onDeselectAllClick ), NULL, this );
212 
213  wxBoxSizer* buttonSizer = new wxBoxSizer( wxHORIZONTAL );
214  buttonSizer->Add( m_buttonSelectAll, 1, wxALL, 5 );
215  buttonSizer->Add( m_buttonDeselectAll, 1, wxALL, 5 );
216 
217  // Static box sizer layout
218  sbLayersSizer->Add( bLayerListsSizer, 1, wxALL | wxEXPAND, 5 );
219  sbLayersSizer->Add( buttonSizer, 0, wxALL | wxEXPAND, 5 );
220 
221  getMainSizer()->Insert( 0, sbLayersSizer, 1, wxEXPAND );
222 }
223 
224 
225 void DIALOG_PRINT_GERBVIEW::onSelectAllClick( wxCommandEvent& event )
226 {
227  for( unsigned int i = 0; i < LAYER_LIST_COUNT; ++i )
228  setListBoxValue( m_layerLists[i], true );
229 }
230 
231 
232 void DIALOG_PRINT_GERBVIEW::onDeselectAllClick( wxCommandEvent& event )
233 {
234  for( unsigned int i = 0; i < LAYER_LIST_COUNT; ++i )
235  setListBoxValue( m_layerLists[i], false );
236 }
237 
238 
239 void DIALOG_PRINT_GERBVIEW::setListBoxValue( wxCheckListBox* aList, bool aValue )
240 {
241  for( unsigned int i = 0; i < aList->GetCount(); ++i )
242  aList->Check( i, aValue );
243 }
244 
245 
246 bool DIALOG_PRINT_GERBVIEW::isLayerEnabled( unsigned int aLayer ) const
247 {
248  auto layerMapIt = m_layerToItemMap.find( aLayer );
249 
250  if( layerMapIt == m_layerToItemMap.end() )
251  return false;
252 
253  unsigned int itemNr = layerMapIt->second;
254  unsigned int listIdx = itemNr / LAYER_PER_LIST;
255  unsigned int itemIdx = itemNr % LAYER_PER_LIST;
256  wxCHECK( listIdx < LAYER_LIST_COUNT, false );
257  wxCheckListBox* listBox = m_layerLists[listIdx];
258 
259  return itemIdx < listBox->GetCount() && listBox->IsChecked( itemIdx );
260 }
261 
262 
263 void DIALOG_PRINT_GERBVIEW::enableLayer( unsigned int aLayer, bool aValue )
264 {
265  auto layerMapIt = m_layerToItemMap.find( aLayer );
266 
267  if( layerMapIt == m_layerToItemMap.end() )
268  return;
269 
270  unsigned int itemNr = layerMapIt->second;
271  unsigned int listIdx = itemNr / LAYER_PER_LIST;
272  unsigned int itemIdx = itemNr % LAYER_PER_LIST;
273  wxCHECK( listIdx < LAYER_LIST_COUNT, /* void */ );
274  wxCheckListBox* listBox = m_layerLists[listIdx];
275 
276  if( itemIdx < listBox->GetCount() )
277  listBox->Check( itemIdx, aValue );
278 }
279 
280 
282 {
283  settings()->m_layerSet = LSET();
284  int& pageCount = settings()->m_pageCount;
285  pageCount = 0;
286 
287  unsigned int layer = 0;
288 
289  for( unsigned int j = 0; j < LAYER_LIST_COUNT; ++j )
290  {
291  for( unsigned int i = 0; i < LAYER_PER_LIST; ++i )
292  {
293  if( isLayerEnabled( layer ) )
294  {
295  settings()->m_layerSet.set( layer );
296  ++pageCount;
297  }
298 
299  ++layer;
300  }
301  }
302 
303  return pageCount;
304 }
305 
306 
308 {
310 
311  settings()->m_mirror = m_checkboxMirror->GetValue();
312 
314 }
315 
316 
317 void GERBVIEW_FRAME::ToPrinter( wxCommandEvent& event )
318 {
319  // Selection affects the original item visibility
320  GetToolManager()->RunAction( GERBVIEW_ACTIONS::selectionClear, true );
321 
322  BOARD_PRINTOUT_SETTINGS settings( GetPageSettings() );
323  DIALOG_PRINT_GERBVIEW dlg( this, &settings );
324  dlg.ForcePrintBorder( false );
325  dlg.ShowModal();
326 }
bool isLayerEnabled(unsigned int aLayer) const
Check whether a layer is enabled in a listbox
void onDeselectAllClick(wxCommandEvent &event)
static TOOL_ACTION selectionClear
Clears the current selection.
static constexpr unsigned int LAYER_PER_LIST
This file is part of the common library.
Class GERBER_FILE_IMAGE holds the Image data and parameters for one gerber file and layer parameters ...
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
GERBER_FILE_IMAGE_LIST is a helper class to handle a list of GERBER_FILE_IMAGE files which are loaded...
wxStaticBox * getOptionsBox()
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
Class LSET is a set of PCB_LAYER_IDs.
wxPrintout * createPrintout(const wxString &aTitle) override
Create a printout with a requested title.
GERBER_FILE_IMAGE * GetGbrImage(int aIdx)
bool m_mirror
Print mirrored.
bool TransferDataToWindow() override
PRINTOUT_SETTINGS * m_settings
int setLayerSetFromList()
Update layerset basing on the selected layers
wxConfigBase * KifaceSettings() const
Definition: kiface_i.h:103
GBR_LAYOUT * GetGerberLayout() const
static constexpr unsigned int LAYER_LIST_COUNT
std::unordered_map< int, int > m_layerToItemMap
wxCheckListBox * m_layerLists[LAYER_LIST_COUNT]
void setListBoxValue(wxCheckListBox *aList, bool aValue)
(Un)check all items in a checklist box
DIALOG_PRINT_GERBVIEW(GERBVIEW_FRAME *aParent, BOARD_PRINTOUT_SETTINGS *aSettings)
void onSelectAllClick(wxCommandEvent &event)
bool TransferDataToWindow() override
int m_pageCount
Number of pages to print.
Definition: printout.h:47
GERBER_FILE_IMAGE_LIST * GetImagesList() const
Definition: gbr_layout.cpp:50
size_t i
Definition: json11.cpp:597
The common library.
void ToPrinter(wxCommandEvent &event)
Function ToPrinter Open a dialog frame to print layers.
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:921
BOARD_PRINTOUT_SETTINGS * settings() const
wxGridBagSizer * getOptionsSizer()
void ForcePrintBorder(bool aValue)
Set &#39;print border and title block&#39; to a requested value and hides the corresponding checkbox...
void enableLayer(unsigned int aLayer, bool aValue)
Enable/disable layer in a listbox
LSET m_layerSet
Layers to print.