KiCad PCB EDA Suite
dialogs_for_printing.cpp
Go to the documentation of this file.
1 
5 /*
6  * This program source code file is part of KiCad, a free EDA CAD application.
7  *
8  * Copyright (C) 2013 CERN
9  * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
10  *
11  * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, you may find one here:
25  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
26  * or you may search the http://www.gnu.org website for the version 2 license,
27  * or you may write to the Free Software Foundation, Inc.,
28  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
29  */
30 
31 #include <base_units.h>
32 #include <dialog_helpers.h>
33 #include <eda_item.h>
34 #include <gr_basic.h>
40 
42 #include "pl_editor_frame.h"
43 #include "pl_editor_id.h"
44 
45 #include <wx/msgdlg.h>
46 #include <wx/print.h>
47 
51 class PLEDITOR_PRINTOUT : public wxPrintout
52 {
53 private:
55 
56 public:
57  PLEDITOR_PRINTOUT( PL_EDITOR_FRAME* aParent, const wxString& aTitle ) :
58  wxPrintout( aTitle )
59  {
60  wxASSERT( aParent != NULL );
61  m_parent = aParent;
62  }
63 
64  bool OnPrintPage( int aPageNum ) override;
65  bool HasPage( int aPageNum ) override { return ( aPageNum <= 2 ); }
66  void GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo ) override;
67  void PrintPage( int aPageNum );
68 };
69 
70 
74 class PLEDITOR_PREVIEW_FRAME : public wxPreviewFrame
75 {
77 
78 public:
79  PLEDITOR_PREVIEW_FRAME( wxPrintPreview* aPreview, PL_EDITOR_FRAME* aParent,
80  const wxString& aTitle, const wxPoint& aPos = wxDefaultPosition,
81  const wxSize& aSize = wxDefaultSize ) :
82  wxPreviewFrame( aPreview, aParent, aTitle, aPos, aSize )
83  {
84  m_parent = aParent;
85  }
86 
87  bool Show( bool show ) override
88  {
89  bool ret;
90 
91  // Show or hide the window. If hiding, save current position and size.
92  // If showing, use previous position and size.
93  if( show )
94  {
95  bool centre = false;
96  if( s_size.x == 0 || s_size.y == 0 )
97  {
98  s_size = (m_parent->GetSize() * 3) / 4;
99  s_pos = wxDefaultPosition;
100  centre = true;
101  }
102 
103  SetSize( s_pos.x, s_pos.y, s_size.x, s_size.y, 0 );
104 
105  if( centre )
106  Center();
107 
108  ret = wxPreviewFrame::Show( show );
109  }
110  else
111  {
112  // Save the dialog's position & size before hiding
113  s_size = GetSize();
114  s_pos = GetPosition();
115 
116  ret = wxPreviewFrame::Show( show );
117  }
118  return ret;
119  }
120 
121 private:
122  static wxPoint s_pos;
123  static wxSize s_size;
124 
125  DECLARE_CLASS( PLEDITOR_PREVIEW_FRAME )
126  DECLARE_EVENT_TABLE()
127  DECLARE_NO_COPY_CLASS( PLEDITOR_PREVIEW_FRAME )
128 };
129 
130 
133 
134 
135 IMPLEMENT_CLASS( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
136 
137 
138 BEGIN_EVENT_TABLE( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
139  EVT_CLOSE( PLEDITOR_PREVIEW_FRAME::OnCloseWindow )
140 END_EVENT_TABLE()
141 
142 
143 bool PLEDITOR_PRINTOUT::OnPrintPage( int aPageNum )
144 {
145  PrintPage( aPageNum );
146  return true;
147 }
148 
149 
150 void PLEDITOR_PRINTOUT::GetPageInfo( int* minPage, int* maxPage,
151  int* selPageFrom, int* selPageTo )
152 {
153  *minPage = *selPageFrom = 1;
154  *maxPage = *selPageTo = 2;
155 }
156 
157 /*
158  * This is the real print function: print the active screen
159  */
160 void PLEDITOR_PRINTOUT::PrintPage( int aPageNum )
161 {
162  wxPoint tmp_startvisu;
163  wxSize pageSizeIU; // Page size in internal units
164  wxPoint old_org;
165  wxRect fitRect;
166  wxDC* dc = GetDC();
167  BASE_SCREEN* screen = m_parent->GetScreen();
168 
169  // Save current offsets and clip box.
170  tmp_startvisu = screen->m_StartVisu;
171  old_org = screen->m_DrawOrg;
172 
173  // Change scale factor and offset to print the whole page.
174 
175  pageSizeIU = m_parent->GetPageSettings().GetSizeIU();
176  FitThisSizeToPaper( pageSizeIU );
177  fitRect = GetLogicalPaperRect();
178 
179  int xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
180  int yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
181 
182  OffsetLogicalOrigin( xoffset, yoffset );
183 
184  GRResetPenAndBrush( dc );
185  GRForceBlackPen( true );
186 
187  COLOR4D bg_color = m_parent->GetDrawBgColor();
189 
190  screen->SetVirtualPageNumber( aPageNum );
191 
192  KIGFX::DS_RENDER_SETTINGS renderSettings;
193  renderSettings.SetDefaultPenWidth( 1 );
194  renderSettings.SetLayerColor( LAYER_DRAWINGSHEET, COLOR4D( RED ) );
195  renderSettings.SetPrintDC( dc );
196 
197  // Ensure the scaling factor (used only in printing) of bitmaps is up to date
199 
200  for( DS_DATA_ITEM* dataItem : model.GetItems() )
201  {
202  if( dataItem->GetType() == DS_DATA_ITEM::DS_BITMAP )
203  {
204  BITMAP_BASE* bitmap = static_cast<DS_DATA_ITEM_BITMAP*>( dataItem )->m_ImageBitmap;
205  bitmap->SetPixelSizeIu( IU_PER_MILS * 1000 / bitmap->GetPPI() );
206  }
207  }
208 
209  m_parent->PrintDrawingSheet( &renderSettings, screen, IU_PER_MILS, wxEmptyString );
210 
211  m_parent->SetDrawBgColor( bg_color );
212 
213  GRForceBlackPen( false );
214 
215  screen->m_StartVisu = tmp_startvisu;
216  screen->m_DrawOrg = old_org;
217 
218  // PrintDrawingSheet clears the current display list when calling BuildDrawItemsList()
219  // So rebuild and redraw it.
221 }
222 
223 
224 int InvokeDialogPrint( PL_EDITOR_FRAME* aCaller, wxPrintData* aPrintData,
225  wxPageSetupDialogData* aPageSetupData )
226 {
227  int pageCount = 2;
228 
229  wxPrintDialogData printDialogData( *aPrintData );
230  printDialogData.SetMaxPage( pageCount );
231 
232  if( pageCount > 1 )
233  printDialogData.EnablePageNumbers( true );
234 
235  wxPrinter printer( &printDialogData );
236  PLEDITOR_PRINTOUT printout( aCaller, _( "Print Drawing Sheet" ) );
237 
238  if( !printer.Print( aCaller, &printout, true ) )
239  {
240  if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
241  wxMessageBox( _( "An error occurred attempting to print the drawing sheet." ),
242  _( "Printing" ), wxOK );
243  return 0;
244  }
245 
246  *aPageSetupData = printer.GetPrintDialogData().GetPrintData();
247 
248  return 1;
249 }
250 
251 
252 int InvokeDialogPrintPreview( PL_EDITOR_FRAME* aCaller, wxPrintData* aPrintData )
253 {
254  // Pass two printout objects: for preview, and possible printing.
255  wxString title = _( "Preview" );
256  wxPrintPreview* preview = new wxPrintPreview( new PLEDITOR_PRINTOUT( aCaller, title ),
257  new PLEDITOR_PRINTOUT( aCaller, title ),
258  aPrintData );
259 
260  preview->SetZoom( 70 );
261 
262  PLEDITOR_PREVIEW_FRAME* frame = new PLEDITOR_PREVIEW_FRAME( preview, aCaller, title );
263 
264  frame->Initialize();
265  frame->Show( true );
266 
267  return 1;
268 }
269 
void GRResetPenAndBrush(wxDC *DC)
Definition: gr_basic.cpp:118
Handle the graphic items list to draw/plot the frame and title block.
Definition: ds_data_model.h:38
void SetVirtualPageNumber(int aPageNumber)
Definition: base_screen.h:76
PL_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
const PAGE_INFO & GetPageSettings() const override
Implementation of conversion functions that require both schematic and board internal units.
bool OnPrintPage(int aPageNum) override
void PrintPage(int aPageNum)
void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) override
The main window used in the drawing sheet editor.
int InvokeDialogPrint(PL_EDITOR_FRAME *aCaller, wxPrintData *aPrintData, wxPageSetupDialogData *aPageSetupData)
Create and show a print dialog returns 1 if OK, 0 , there is a problem.
virtual void SetDrawBgColor(COLOR4D aColor)
int InvokeDialogPrintPreview(PL_EDITOR_FRAME *aCaller, wxPrintData *aPrintData)
Create and show a print preview dialog returns 1 if OK, 0 , there is a problem.
std::vector< DS_DATA_ITEM * > & GetItems()
void DisplayDrawingSheet()
Build and update the list of WS_DRAW_ITEM_xxx showing the frame layout.
bool Show(bool show) override
Store page-layout-specific render settings.
Definition: ds_painter.h:46
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:51
wxPoint m_StartVisu
Coordinates in drawing units of the current view position (upper left corner of device)
Definition: base_screen.h:93
static DS_DATA_MODEL & GetTheInstance()
static function: returns the instance of DS_DATA_MODEL used in the application
#define NULL
void GRForceBlackPen(bool flagforce)
Function GRForceBlackPen.
Definition: gr_basic.cpp:199
Handles how to draw a screen (a board, a schematic ...)
Definition: base_screen.h:40
Custom print preview frame.
bool HasPage(int aPageNum) override
Helper dialog and control classes.
#define _(s)
void SetLayerColor(int aLayer, const COLOR4D &aColor)
Change the color used to draw a layer.
PLEDITOR_PRINTOUT(PL_EDITOR_FRAME *aParent, const wxString &aTitle)
void PrintDrawingSheet(const RENDER_SETTINGS *aSettings, BASE_SCREEN *aScreen, double aMils2Iu, const wxString &aFilename, const wxString &aSheetLayer=wxEmptyString)
Prints the drawing-sheet (frame and title block).
Definition: color4d.h:59
void SetPixelSizeIu(double aPixSize)
Definition: bitmap_base.h:68
Definition: color4d.h:48
PLEDITOR_PREVIEW_FRAME(wxPrintPreview *aPreview, PL_EDITOR_FRAME *aParent, const wxString &aTitle, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize)
PL_EDITOR_FRAME * m_parent
Drawing sheet structure type definitions.
Definition: ds_data_item.h:95
wxPoint m_DrawOrg
offsets for drawing the circuit on the screen
Definition: base_screen.h:88
#define IU_PER_MILS
Definition: plotter.cpp:137
Custom print out for printing schematics.
virtual COLOR4D GetDrawBgColor() const
drawingsheet frame and titleblock
void SetDefaultPenWidth(int aWidth)
void SetPrintDC(wxDC *aDC)
virtual BASE_SCREEN * GetScreen() const
Return a pointer to a BASE_SCREEN or one of its derivatives.
int GetPPI() const
Definition: bitmap_base.h:135
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103