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 (C) 2020-2023 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, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
31#include <base_units.h>
32#include <gr_basic.h>
37#include <math/vector2wx.h>
38
40#include "pl_editor_frame.h"
41#include "pl_editor_id.h"
42
43#include <wx/msgdlg.h>
44#include <wx/print.h>
45
49class PLEDITOR_PRINTOUT : public wxPrintout
50{
51public:
52 PLEDITOR_PRINTOUT( PL_EDITOR_FRAME* aParent, const wxString& aTitle ) :
53 wxPrintout( aTitle )
54 {
55 wxASSERT( aParent != nullptr );
56 m_parent = aParent;
57 }
58
59 bool OnPrintPage( int aPageNum ) override;
60 bool HasPage( int aPageNum ) override { return ( aPageNum <= 2 ); }
61 void GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo ) override;
62 void PrintPage( int aPageNum );
63
64private:
66};
67
68
72class PLEDITOR_PREVIEW_FRAME : public wxPreviewFrame
73{
74public:
75 PLEDITOR_PREVIEW_FRAME( wxPrintPreview* aPreview, PL_EDITOR_FRAME* aParent,
76 const wxString& aTitle, const wxPoint& aPos = wxDefaultPosition,
77 const wxSize& aSize = wxDefaultSize ) :
78 wxPreviewFrame( aPreview, aParent, aTitle, aPos, aSize )
79 {
80 m_parent = aParent;
81 }
82
83 bool Show( bool show ) override
84 {
85 bool ret;
86
87 // Show or hide the window. If hiding, save current position and size.
88 // If showing, use previous position and size.
89 if( show )
90 {
91 bool centre = false;
92
93 if( s_size.x == 0 || s_size.y == 0 )
94 {
95 s_size = ( m_parent->GetSize() * 3 ) / 4;
96 s_pos = wxDefaultPosition;
97 centre = true;
98 }
99
100 SetSize( s_pos.x, s_pos.y, s_size.x, s_size.y, 0 );
101
102 if( centre )
103 Center();
104
105 ret = wxPreviewFrame::Show( show );
106 }
107 else
108 {
109 // Save the dialog's position & size before hiding
110 s_size = GetSize();
111 s_pos = GetPosition();
112
113 ret = wxPreviewFrame::Show( show );
114 }
115
116 return ret;
117 }
118
119private:
121
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
135IMPLEMENT_CLASS( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
136
137
138BEGIN_EVENT_TABLE( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
139 EVT_CLOSE( PLEDITOR_PREVIEW_FRAME::OnCloseWindow )
140END_EVENT_TABLE()
141
142
143bool PLEDITOR_PRINTOUT::OnPrintPage( int aPageNum )
144{
145 PrintPage( aPageNum );
146 return true;
147}
148
149
150void PLEDITOR_PRINTOUT::GetPageInfo( int* minPage, int* maxPage,
151 int* selPageFrom, int* selPageTo )
152{
153 *minPage = *selPageFrom = 1;
154 *maxPage = *selPageTo = 2;
155}
156
157
159{
160 VECTOR2I tmp_startvisu;
161 wxSize pageSizeIU; // Page size in internal units
162 VECTOR2I old_org;
163 wxRect fitRect;
164 wxDC* dc = GetDC();
165 BASE_SCREEN* screen = m_parent->GetScreen();
166
167 // Save current offsets and clip box.
168 tmp_startvisu = screen->m_StartVisu;
169 old_org = screen->m_DrawOrg;
170
171 // Change scale factor and offset to print the whole page.
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:109
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:49
void SetPixelSizeIu(double aPixSize)
Definition: bitmap_base.h:66
int GetPPI() const
Definition: bitmap_base.h:118
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 VECTOR2D GetSizeIU(double aIUScale) const
Gets the page size in internal units.
Definition: page_info.h:171
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:159
void GRResetPenAndBrush(wxDC *DC)
Definition: gr_basic.cpp:73
@ LAYER_DRAWINGSHEET
drawingsheet frame and titleblock
Definition: layer_ids.h:218
const double IU_PER_MILS
Definition: base_units.h:77
wxSize ToWxSize(const VECTOR2I &aSize)
Definition: vector2wx.h:55