KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialogs_for_printing.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) 2013 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
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, see <https://www.gnu.org/licenses/>.
21 */
22
26
27#include <base_units.h>
28#include <gr_basic.h>
33#include <math/vector2wx.h>
34
36#include "pl_editor_frame.h"
37#include "pl_editor_id.h"
38
39#include <printing.h>
40
41#include <wx/msgdlg.h>
42#include <wx/print.h>
43
47class PLEDITOR_PRINTOUT : public wxPrintout
48{
49public:
50 PLEDITOR_PRINTOUT( PL_EDITOR_FRAME* aParent, const wxString& aTitle ) :
51 wxPrintout( aTitle )
52 {
53 wxASSERT( aParent != nullptr );
54 m_parent = aParent;
55 }
56
57 bool OnPrintPage( int aPageNum ) override;
58 bool HasPage( int aPageNum ) override { return ( aPageNum <= 2 ); }
59 void GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo ) override;
60 void PrintPage( int aPageNum );
61
62private:
64};
65
66
70class PLEDITOR_PREVIEW_FRAME : public wxPreviewFrame
71{
72public:
73 PLEDITOR_PREVIEW_FRAME( wxPrintPreview* aPreview, PL_EDITOR_FRAME* aParent,
74 const wxString& aTitle, const wxPoint& aPos = wxDefaultPosition,
75 const wxSize& aSize = wxDefaultSize ) :
76 wxPreviewFrame( aPreview, aParent, aTitle, aPos, aSize )
77 {
78 m_parent = aParent;
79 }
80
81 bool Show( bool show ) override
82 {
83 bool ret;
84
85 // Show or hide the window. If hiding, save current position and size.
86 // If showing, use previous position and size.
87 if( show )
88 {
89 bool centre = false;
90
91 if( s_size.x == 0 || s_size.y == 0 )
92 {
93 s_size = ( m_parent->GetSize() * 3 ) / 4;
94 s_pos = wxDefaultPosition;
95 centre = true;
96 }
97
98 SetSize( s_pos.x, s_pos.y, s_size.x, s_size.y, 0 );
99
100 if( centre )
101 Center();
102
103 ret = wxPreviewFrame::Show( show );
104 }
105 else
106 {
107 // Save the dialog's position & size before hiding
108 s_size = GetSize();
109 s_pos = GetPosition();
110
111 ret = wxPreviewFrame::Show( show );
112 }
113
114 return ret;
115 }
116
117private:
119
120 static wxPoint s_pos;
121 static wxSize s_size;
122
123 DECLARE_CLASS( PLEDITOR_PREVIEW_FRAME )
124 DECLARE_EVENT_TABLE()
125 DECLARE_NO_COPY_CLASS( PLEDITOR_PREVIEW_FRAME )
126};
127
128
131
132
133IMPLEMENT_CLASS( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
134
135
136BEGIN_EVENT_TABLE( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
137 EVT_CLOSE( PLEDITOR_PREVIEW_FRAME::OnCloseWindow )
138END_EVENT_TABLE()
139
140
141bool PLEDITOR_PRINTOUT::OnPrintPage( int aPageNum )
142{
143 PrintPage( aPageNum );
144 return true;
145}
146
147
148void PLEDITOR_PRINTOUT::GetPageInfo( int* minPage, int* maxPage,
149 int* selPageFrom, int* selPageTo )
150{
151 *minPage = *selPageFrom = 1;
152 *maxPage = *selPageTo = 2;
153}
154
155
157{
158 VECTOR2I tmp_startvisu;
159 wxSize pageSizeIU; // Page size in internal units
160 VECTOR2I old_org;
161 wxRect fitRect;
162 wxDC* dc = GetDC();
163 BASE_SCREEN* screen = m_parent->GetScreen();
164
165 // Save current offsets and clip box.
166 tmp_startvisu = screen->m_StartVisu;
167 old_org = screen->m_DrawOrg;
168
169 // Change scale factor and offset to print the whole page.
170 pageSizeIU = ToWxSize( m_parent->GetPageSettings().GetSizeIU( drawSheetIUScale.IU_PER_MILS ) );
171 FitThisSizeToPaper( pageSizeIU );
172 fitRect = GetLogicalPaperRect();
173
174 int xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
175 int yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
176
177 OffsetLogicalOrigin( xoffset, yoffset );
178
179 GRResetPenAndBrush( dc );
180 GRForceBlackPen( true );
181
182 COLOR4D bg_color = m_parent->GetDrawBgColor();
183 m_parent->SetDrawBgColor( WHITE );
184
185 screen->SetVirtualPageNumber( aPageNum );
186
187 KIGFX::DS_RENDER_SETTINGS renderSettings;
188 renderSettings.SetDefaultPenWidth( 1 );
189 renderSettings.SetLayerColor( LAYER_DRAWINGSHEET, COLOR4D( RED ) );
190 renderSettings.SetPrintDC( dc );
191
192 // Ensure the scaling factor (used only in printing) of bitmaps is up to date
194
195 for( DS_DATA_ITEM* dataItem : model.GetItems() )
196 {
197 if( dataItem->GetType() == DS_DATA_ITEM::DS_BITMAP )
198 {
199 BITMAP_BASE* bitmap = static_cast<DS_DATA_ITEM_BITMAP*>( dataItem )->m_ImageBitmap;
200 bitmap->SetPixelSizeIu( drawSheetIUScale.IU_PER_MILS * 1000 / bitmap->GetPPI() );
201 }
202 }
203
204 m_parent->PrintDrawingSheet( &renderSettings, screen, nullptr, drawSheetIUScale.IU_PER_MILS,
205 wxEmptyString );
206
207 m_parent->SetDrawBgColor( bg_color );
208
209 GRForceBlackPen( false );
210
211 screen->m_StartVisu = tmp_startvisu;
212 screen->m_DrawOrg = old_org;
213
214 // PrintDrawingSheet clears the current display list when calling BuildDrawItemsList()
215 // So rebuild and redraw it.
216 m_parent->GetCanvas()->DisplayDrawingSheet();
217}
218
219
220int InvokeDialogPrint( PL_EDITOR_FRAME* aCaller, wxPrintData* aPrintData,
221 wxPageSetupDialogData* aPageSetupData )
222{
223 int pageCount = 2;
224
225 wxPrintDialogData printDialogData( *aPrintData );
226 printDialogData.SetMaxPage( pageCount );
227
228 if( pageCount > 1 )
229 printDialogData.EnablePageNumbers( true );
230
231 wxPrinter printer( &printDialogData );
232 PLEDITOR_PRINTOUT printout( aCaller, _( "Print Drawing Sheet" ) );
233
234 if( !printer.Print( aCaller, &printout, true ) )
235 {
236 if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
237 wxMessageBox( _( "An error occurred attempting to print the drawing sheet." ),
238 _( "Printing" ), wxOK );
239 return 0;
240 }
241
242 *aPageSetupData = printer.GetPrintDialogData().GetPrintData();
243 KIPLATFORM::PRINTING::ResetPrintToFilePath( aPageSetupData->GetPrintData() );
244
245 return 1;
246}
247
248
249int InvokeDialogPrintPreview( PL_EDITOR_FRAME* aCaller, wxPrintData* aPrintData )
250{
251 // Pass two printout objects: for preview, and possible printing.
252 wxString title = _( "Preview" );
253 wxPrintPreview* preview = new wxPrintPreview( new PLEDITOR_PRINTOUT( aCaller, title ),
254 new PLEDITOR_PRINTOUT( aCaller, title ),
255 aPrintData );
256
257 preview->SetZoom( 70 );
258
259 PLEDITOR_PREVIEW_FRAME* frame = new PLEDITOR_PREVIEW_FRAME( preview, aCaller, title );
260
261 frame->Initialize();
262 frame->Show( true );
263
264 return 1;
265}
266
constexpr EDA_IU_SCALE drawSheetIUScale
Definition base_units.h:122
Handles how to draw a screen (a board, a schematic ...)
Definition base_screen.h:37
void SetVirtualPageNumber(int aPageNumber)
Definition base_screen.h:72
VECTOR2I m_DrawOrg
offsets for drawing the circuit on the screen
Definition base_screen.h:84
VECTOR2I m_StartVisu
Coordinates in drawing units of the current view position (upper left corner of device)
Definition base_screen.h:89
This class handle bitmap images in KiCad.
Definition bitmap_base.h:45
void SetPixelSizeIu(double aPixSize)
Definition bitmap_base.h:62
int GetPPI() const
Drawing sheet structure type definitions.
Handle the graphic items list to draw/plot the frame and title block.
static DS_DATA_MODEL & GetTheInstance()
Return the instance of DS_DATA_MODEL used in the application.
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
Store page-layout-specific render settings.
Definition ds_painter.h:42
void SetDefaultPenWidth(int aWidth)
void SetLayerColor(int aLayer, const COLOR4D &aColor)
Change the color used to draw a layer.
void SetPrintDC(wxDC *aDC)
Custom print preview frame.
PLEDITOR_PREVIEW_FRAME(wxPrintPreview *aPreview, PL_EDITOR_FRAME *aParent, const wxString &aTitle, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize)
bool Show(bool show) override
Custom print out for printing schematics.
bool OnPrintPage(int aPageNum) override
PL_EDITOR_FRAME * m_parent
void PrintPage(int aPageNum)
PLEDITOR_PRINTOUT(PL_EDITOR_FRAME *aParent, const wxString &aTitle)
bool HasPage(int aPageNum) override
void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) override
The main window used in the drawing sheet editor.
@ WHITE
Definition color4d.h:44
@ RED
Definition color4d.h:55
int InvokeDialogPrintPreview(PL_EDITOR_FRAME *aCaller, wxPrintData *aPrintData)
Create and show a print preview dialog returns 1 if OK, 0 , there is a problem.
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.
#define _(s)
void GRForceBlackPen(bool flagforce)
Definition gr_basic.cpp:159
void GRResetPenAndBrush(wxDC *DC)
Definition gr_basic.cpp:73
@ LAYER_DRAWINGSHEET
Sheet frame and title block.
Definition layer_ids.h:274
void ResetPrintToFilePath(wxPrintData &aData)
Clear any leftover "print to file" destination from aData.
KIBIS_MODEL * model
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
wxSize ToWxSize(const VECTOR2I &aSize)
Definition vector2wx.h:51