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 
50 class PLEDITOR_PRINTOUT : public wxPrintout
51 {
52 private:
54 
55 public:
56  PLEDITOR_PRINTOUT( PL_EDITOR_FRAME* aParent, const wxString& aTitle ) :
57  wxPrintout( aTitle )
58  {
59  wxASSERT( aParent != NULL );
60  m_parent = aParent;
61  }
62 
63  bool OnPrintPage( int aPageNum ) override;
64  bool HasPage( int aPageNum ) override { return ( aPageNum <= 2 ); }
65  void GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo ) override;
66  void PrintPage( int aPageNum );
67 };
68 
69 
73 class PLEDITOR_PREVIEW_FRAME : public wxPreviewFrame
74 {
76 
77 public:
78  PLEDITOR_PREVIEW_FRAME( wxPrintPreview* aPreview, PL_EDITOR_FRAME* aParent,
79  const wxString& aTitle, const wxPoint& aPos = wxDefaultPosition,
80  const wxSize& aSize = wxDefaultSize ) :
81  wxPreviewFrame( aPreview, aParent, aTitle, aPos, aSize )
82  {
83  m_parent = aParent;
84  }
85 
86  bool Show( bool show ) override
87  {
88  bool ret;
89 
90  // Show or hide the window. If hiding, save current position and size.
91  // If showing, use previous position and size.
92  if( show )
93  {
94  bool centre = false;
95  if( s_size.x == 0 || s_size.y == 0 )
96  {
97  s_size = (m_parent->GetSize() * 3) / 4;
98  s_pos = wxDefaultPosition;
99  centre = true;
100  }
101 
102  SetSize( s_pos.x, s_pos.y, s_size.x, s_size.y, 0 );
103 
104  if( centre )
105  Center();
106 
107  ret = wxPreviewFrame::Show( show );
108  }
109  else
110  {
111  // Save the dialog's position & size before hiding
112  s_size = GetSize();
113  s_pos = GetPosition();
114 
115  ret = wxPreviewFrame::Show( show );
116  }
117  return ret;
118  }
119 
120 private:
121  static wxPoint s_pos;
122  static wxSize s_size;
123 
124  DECLARE_CLASS( PLEDITOR_PREVIEW_FRAME )
125  DECLARE_EVENT_TABLE()
126  DECLARE_NO_COPY_CLASS( PLEDITOR_PREVIEW_FRAME )
127 };
128 
129 
132 
133 
134 IMPLEMENT_CLASS( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
135 
136 
137 BEGIN_EVENT_TABLE( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
138  EVT_CLOSE( PLEDITOR_PREVIEW_FRAME::OnCloseWindow )
139 END_EVENT_TABLE()
140 
141 
142 bool PLEDITOR_PRINTOUT::OnPrintPage( int aPageNum )
143 {
144  PrintPage( aPageNum );
145  return true;
146 }
147 
148 
149 void PLEDITOR_PRINTOUT::GetPageInfo( int* minPage, int* maxPage,
150  int* selPageFrom, int* selPageTo )
151 {
152  *minPage = *selPageFrom = 1;
153  *maxPage = *selPageTo = 2;
154 }
155 
156 /*
157  * This is the real print function: print the active screen
158  */
159 void PLEDITOR_PRINTOUT::PrintPage( int aPageNum )
160 {
161  wxPoint tmp_startvisu;
162  wxSize pageSizeIU; // Page size in internal units
163  wxPoint old_org;
164  wxRect fitRect;
165  wxDC* dc = GetDC();
166  BASE_SCREEN* screen = m_parent->GetScreen();
167 
168  // Save current offsets and clip box.
169  tmp_startvisu = screen->m_StartVisu;
170  old_org = screen->m_DrawOrg;
171 
172  // Change scale factor and offset to print the whole page.
173 
174  pageSizeIU = m_parent->GetPageSettings().GetSizeIU();
175  FitThisSizeToPaper( pageSizeIU );
176  fitRect = GetLogicalPaperRect();
177 
178  int xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
179  int yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
180 
181  OffsetLogicalOrigin( xoffset, yoffset );
182 
183  GRResetPenAndBrush( dc );
184  GRForceBlackPen( true );
185 
186  COLOR4D bg_color = m_parent->GetDrawBgColor();
188 
189  screen->SetVirtualPageNumber( aPageNum );
190 
191  KIGFX::DS_RENDER_SETTINGS renderSettings;
192  renderSettings.SetDefaultPenWidth( 1 );
193  renderSettings.SetLayerColor( LAYER_DRAWINGSHEET, COLOR4D( RED ) );
194  renderSettings.SetPrintDC( dc );
195 
196  // Ensure the scaling factor (used only in printing) of bitmaps is up to date
198 
199  for( DS_DATA_ITEM* dataItem : model.GetItems() )
200  {
201  if( dataItem->GetType() == DS_DATA_ITEM::DS_BITMAP )
202  {
203  BITMAP_BASE* bitmap = static_cast<DS_DATA_ITEM_BITMAP*>( dataItem )->m_ImageBitmap;
204  bitmap->SetPixelSizeIu( IU_PER_MILS * 1000 / bitmap->GetPPI() );
205  }
206  }
207 
208  m_parent->PrintDrawingSheet( &renderSettings, screen, IU_PER_MILS, wxEmptyString );
209 
210  m_parent->SetDrawBgColor( bg_color );
211 
212  GRForceBlackPen( false );
213 
214  screen->m_StartVisu = tmp_startvisu;
215  screen->m_DrawOrg = old_org;
216 
217  // PrintDrawingSheet clears the current display list when calling BuildDrawItemsList()
218  // So rebuild and redraw it.
220 }
221 
222 
223 int InvokeDialogPrint( PL_EDITOR_FRAME* aCaller, wxPrintData* aPrintData,
224  wxPageSetupDialogData* aPageSetupData )
225 {
226  int pageCount = 2;
227 
228  wxPrintDialogData printDialogData( *aPrintData );
229  printDialogData.SetMaxPage( pageCount );
230 
231  if( pageCount > 1 )
232  printDialogData.EnablePageNumbers( true );
233 
234  wxPrinter printer( &printDialogData );
235  PLEDITOR_PRINTOUT printout( aCaller, _( "Print Page Layout" ) );
236 
237  if( !printer.Print( aCaller, &printout, true ) )
238  {
239  if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
240  wxMessageBox( _( "An error occurred attempting to print the page layout." ),
241  _( "Printing" ), wxOK );
242  return 0;
243  }
244 
245  *aPageSetupData = printer.GetPrintDialogData().GetPrintData();
246 
247  return 1;
248 }
249 
250 
251 int InvokeDialogPrintPreview( PL_EDITOR_FRAME* aCaller, wxPrintData* aPrintData )
252 {
253  // Pass two printout objects: for preview, and possible printing.
254  wxString title = _( "Preview" );
255  wxPrintPreview* preview = new wxPrintPreview( new PLEDITOR_PRINTOUT( aCaller, title ),
256  new PLEDITOR_PRINTOUT( aCaller, title ),
257  aPrintData );
258 
259  preview->SetZoom( 70 );
260 
261  PLEDITOR_PREVIEW_FRAME* frame = new PLEDITOR_PREVIEW_FRAME( preview, aCaller, title );
262 
263  frame->Initialize();
264  frame->Show( true );
265 
266  return 1;
267 }
268 
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:80
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
PL_EDITOR_FRAME is 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()
bool Show(bool show) override
void DisplayWorksheet()
Build and update the list of WS_DRAW_ITEM_xxx showing the frame layout.
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:97
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.
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
#define _(s)
Definition: 3d_actions.cpp:33
wxPoint m_DrawOrg
offsets for drawing the circuit on the screen
Definition: base_screen.h:92
#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:98