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-2021 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 <gr_basic.h>
37
39#include "pl_editor_frame.h"
40#include "pl_editor_id.h"
41
42#include <wx/msgdlg.h>
43#include <wx/print.h>
44
48class PLEDITOR_PRINTOUT : public wxPrintout
49{
50private:
52
53public:
54 PLEDITOR_PRINTOUT( PL_EDITOR_FRAME* aParent, const wxString& aTitle ) :
55 wxPrintout( aTitle )
56 {
57 wxASSERT( aParent != nullptr );
58 m_parent = aParent;
59 }
60
61 bool OnPrintPage( int aPageNum ) override;
62 bool HasPage( int aPageNum ) override { return ( aPageNum <= 2 ); }
63 void GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo ) override;
64 void PrintPage( int aPageNum );
65};
66
67
71class PLEDITOR_PREVIEW_FRAME : public wxPreviewFrame
72{
74
75public:
76 PLEDITOR_PREVIEW_FRAME( wxPrintPreview* aPreview, PL_EDITOR_FRAME* aParent,
77 const wxString& aTitle, const wxPoint& aPos = wxDefaultPosition,
78 const wxSize& aSize = wxDefaultSize ) :
79 wxPreviewFrame( aPreview, aParent, aTitle, aPos, aSize )
80 {
81 m_parent = aParent;
82 }
83
84 bool Show( bool show ) override
85 {
86 bool ret;
87
88 // Show or hide the window. If hiding, save current position and size.
89 // If showing, use previous position and size.
90 if( show )
91 {
92 bool centre = false;
93
94 if( s_size.x == 0 || s_size.y == 0 )
95 {
96 s_size = ( m_parent->GetSize() * 3 ) / 4;
97 s_pos = wxDefaultPosition;
98 centre = true;
99 }
100
101 SetSize( s_pos.x, s_pos.y, s_size.x, s_size.y, 0 );
102
103 if( centre )
104 Center();
105
106 ret = wxPreviewFrame::Show( show );
107 }
108 else
109 {
110 // Save the dialog's position & size before hiding
111 s_size = GetSize();
112 s_pos = GetPosition();
113
114 ret = wxPreviewFrame::Show( show );
115 }
116
117 return ret;
118 }
119
120private:
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
134IMPLEMENT_CLASS( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
135
136
137BEGIN_EVENT_TABLE( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
138 EVT_CLOSE( PLEDITOR_PREVIEW_FRAME::OnCloseWindow )
139END_EVENT_TABLE()
140
141
142bool PLEDITOR_PRINTOUT::OnPrintPage( int aPageNum )
143{
144 PrintPage( aPageNum );
145 return true;
146}
147
148
149void PLEDITOR_PRINTOUT::GetPageInfo( int* minPage, int* maxPage,
150 int* selPageFrom, int* selPageTo )
151{
152 *minPage = *selPageFrom = 1;
153 *maxPage = *selPageTo = 2;
154}
155
156
158{
159 VECTOR2I tmp_startvisu;
160 wxSize pageSizeIU; // Page size in internal units
161 VECTOR2I old_org;
162 wxRect fitRect;
163 wxDC* dc = GetDC();
164 BASE_SCREEN* screen = m_parent->GetScreen();
165
166 // Save current offsets and clip box.
167 tmp_startvisu = screen->m_StartVisu;
168 old_org = screen->m_DrawOrg;
169
170 // Change scale factor and offset to print the whole page.
171
173 FitThisSizeToPaper( pageSizeIU );
174 fitRect = GetLogicalPaperRect();
175
176 int xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
177 int yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
178
179 OffsetLogicalOrigin( xoffset, yoffset );
180
181 GRResetPenAndBrush( dc );
182 GRForceBlackPen( true );
183
184 COLOR4D bg_color = m_parent->GetDrawBgColor();
186
187 screen->SetVirtualPageNumber( aPageNum );
188
189 KIGFX::DS_RENDER_SETTINGS renderSettings;
190 renderSettings.SetDefaultPenWidth( 1 );
191 renderSettings.SetLayerColor( LAYER_DRAWINGSHEET, COLOR4D( RED ) );
192 renderSettings.SetPrintDC( dc );
193
194 // Ensure the scaling factor (used only in printing) of bitmaps is up to date
196
197 for( DS_DATA_ITEM* dataItem : model.GetItems() )
198 {
199 if( dataItem->GetType() == DS_DATA_ITEM::DS_BITMAP )
200 {
201 BITMAP_BASE* bitmap = static_cast<DS_DATA_ITEM_BITMAP*>( dataItem )->m_ImageBitmap;
202 bitmap->SetPixelSizeIu( drawSheetIUScale.IU_PER_MILS * 1000 / bitmap->GetPPI() );
203 }
204 }
205
206 m_parent->PrintDrawingSheet( &renderSettings, screen, nullptr, drawSheetIUScale.IU_PER_MILS,
207 wxEmptyString );
208
209 m_parent->SetDrawBgColor( bg_color );
210
211 GRForceBlackPen( false );
212
213 screen->m_StartVisu = tmp_startvisu;
214 screen->m_DrawOrg = old_org;
215
216 // PrintDrawingSheet clears the current display list when calling BuildDrawItemsList()
217 // So rebuild and redraw it.
219}
220
221
222int InvokeDialogPrint( PL_EDITOR_FRAME* aCaller, wxPrintData* aPrintData,
223 wxPageSetupDialogData* aPageSetupData )
224{
225 int pageCount = 2;
226
227 wxPrintDialogData printDialogData( *aPrintData );
228 printDialogData.SetMaxPage( pageCount );
229
230 if( pageCount > 1 )
231 printDialogData.EnablePageNumbers( true );
232
233 wxPrinter printer( &printDialogData );
234 PLEDITOR_PRINTOUT printout( aCaller, _( "Print Drawing Sheet" ) );
235
236 if( !printer.Print( aCaller, &printout, true ) )
237 {
238 if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
239 wxMessageBox( _( "An error occurred attempting to print the drawing sheet." ),
240 _( "Printing" ), wxOK );
241 return 0;
242 }
243
244 *aPageSetupData = printer.GetPrintDialogData().GetPrintData();
245
246 return 1;
247}
248
249
250int InvokeDialogPrintPreview( PL_EDITOR_FRAME* aCaller, wxPrintData* aPrintData )
251{
252 // Pass two printout objects: for preview, and possible printing.
253 wxString title = _( "Preview" );
254 wxPrintPreview* preview = new wxPrintPreview( new PLEDITOR_PRINTOUT( aCaller, title ),
255 new PLEDITOR_PRINTOUT( aCaller, title ),
256 aPrintData );
257
258 preview->SetZoom( 70 );
259
260 PLEDITOR_PREVIEW_FRAME* frame = new PLEDITOR_PREVIEW_FRAME( preview, aCaller, title );
261
262 frame->Initialize();
263 frame->Show( true );
264
265 return 1;
266}
267
constexpr EDA_IU_SCALE drawSheetIUScale
Definition: base_units.h:110
Handles how to draw a screen (a board, a schematic ...)
Definition: base_screen.h:41
void SetVirtualPageNumber(int aPageNumber)
Definition: base_screen.h:76
VECTOR2I m_DrawOrg
offsets for drawing the circuit on the screen
Definition: base_screen.h:88
VECTOR2I m_StartVisu
Coordinates in drawing units of the current view position (upper left corner of device)
Definition: base_screen.h:93
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:51
void SetPixelSizeIu(double aPixSize)
Definition: bitmap_base.h:67
int GetPPI() const
Definition: bitmap_base.h:134
Drawing sheet structure type definitions.
Definition: ds_data_item.h:96
Handle the graphic items list to draw/plot the frame and title block.
Definition: ds_data_model.h:39
static DS_DATA_MODEL & GetTheInstance()
static function: returns the instance of DS_DATA_MODEL used in the application
std::vector< DS_DATA_ITEM * > & GetItems()
virtual BASE_SCREEN * GetScreen() const
Return a pointer to a BASE_SCREEN or one of its derivatives.
virtual void SetDrawBgColor(const COLOR4D &aColor)
void PrintDrawingSheet(const RENDER_SETTINGS *aSettings, BASE_SCREEN *aScreen, const std::map< wxString, wxString > *aProperties, double aMils2Iu, const wxString &aFilename, const wxString &aSheetLayer=wxEmptyString)
Prints the drawing-sheet (frame and title block).
virtual COLOR4D GetDrawBgColor() const
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
Store page-layout-specific render settings.
Definition: ds_painter.h:46
void SetDefaultPenWidth(int aWidth)
void SetLayerColor(int aLayer, const COLOR4D &aColor)
Change the color used to draw a layer.
void SetPrintDC(wxDC *aDC)
const wxSize GetSizeIU(double aIUScale) const
Gets the page size in internal units.
Definition: page_info.h:162
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
void DisplayDrawingSheet()
Build and update the list of WS_DRAW_ITEM_xxx showing the frame layout.
The main window used in the drawing sheet editor.
const PAGE_INFO & GetPageSettings() const override
PL_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
@ WHITE
Definition: color4d.h:48
@ RED
Definition: color4d.h:59
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:150
void GRResetPenAndBrush(wxDC *DC)
Definition: gr_basic.cpp:72
@ LAYER_DRAWINGSHEET
drawingsheet frame and titleblock
Definition: layer_ids.h:217
const double IU_PER_MILS
Definition: base_units.h:78