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