KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_design_block_preview_widget.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <board.h>
21#include <confirm.h>
22#include <pcb_view.h>
23#include <pcb_screen.h>
26#include <math/vector2wx.h>
27#include <design_block.h>
29#include <pgm_base.h>
30#include <pcb_painter.h>
31#include <pcb_edit_frame.h>
32#include <pcb_field.h>
33#include <pcb_io/pcb_io.h>
34#include <pcb_io/pcb_io_mgr.h>
35#include <project_pcb.h>
36#include <pcbnew_settings.h>
37//#include <pcb_helpers.h>
41#include <wx/log.h>
42#include <wx/stattext.h>
43#include <wx/panel.h>
44
45
47 DESIGN_BLOCK_PREVIEW_WIDGET( aParent ), m_preview( nullptr ), m_status( nullptr ), m_statusPanel( nullptr ),
48 m_statusSizer( nullptr ), m_previewItem( nullptr )
49{
50 COMMON_SETTINGS* common_settings = Pgm().GetCommonSettings();
52
53 m_galDisplayOptions.ReadConfig( *common_settings, cfg->m_Window, this );
54 m_galDisplayOptions.m_forceDisplayCursor = false;
55
56 m_preview = FOOTPRINT_PREVIEW_PANEL::New( &aFrame->Kiway(), this, aFrame );
57 m_preview->SetStealsFocus( false );
58 m_preview->ShowScrollbars( wxSHOW_SB_NEVER, wxSHOW_SB_NEVER );
59 m_preview->GetGAL()->SetAxesEnabled( false );
60 m_preview->GetGAL()->SetGridVisibility( true );
61
62 // Early initialization of the canvas background color,
63 // before any OnPaint event is fired for the canvas using a wrong bg color
64 KIGFX::VIEW* view = m_preview->GetView();
65 auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
66
68 settings->LoadColors( cs );
69
70 const COLOR4D& backgroundColor = settings->GetBackgroundColor();
71 const COLOR4D& foregroundColor = settings->GetCursorColor();
72
73 m_preview->GetGAL()->SetClearColor( backgroundColor );
74
75 m_outerSizer = new wxBoxSizer( wxVERTICAL );
76
77 m_statusPanel = new wxPanel( this );
78 m_statusPanel->SetBackgroundColour( backgroundColor.ToColour() );
79 m_status = new wxStaticText( m_statusPanel, wxID_ANY, wxEmptyString );
80 m_status->SetForegroundColour( settings->GetLayerColor( LAYER_REFERENCEPART ).ToColour() );
81 m_statusSizer = new wxBoxSizer( wxVERTICAL );
82 m_statusSizer->Add( 0, 0, 1 ); // add a spacer
83 m_statusSizer->Add( m_status, 0, wxALIGN_CENTER );
84 m_statusSizer->Add( 0, 0, 1 ); // add a spacer
85 m_statusPanel->SetSizer( m_statusSizer );
86
87 // Give the status panel the same color scheme as the canvas so it isn't jarring when
88 // switched to.
89 m_statusPanel->SetBackgroundColour( backgroundColor.ToColour() );
90 m_statusPanel->SetForegroundColour( foregroundColor.ToColour() );
91
92 // Give the preview panel a small top border to align its top with the status panel,
93 // and give the status panel a small bottom border to align its bottom with the preview
94 // panel.
95 m_outerSizer->Add( m_preview, 1, wxTOP | wxEXPAND, 5 );
96 m_outerSizer->Add( m_statusPanel, 1, wxBOTTOM | wxEXPAND, 5 );
97
98 // Hide the status panel to start
99 m_statusPanel->Hide();
100
101 SetSizer( m_outerSizer );
102 Layout();
103
104 Connect( wxEVT_SIZE, wxSizeEventHandler( PCB_DESIGN_BLOCK_PREVIEW_WIDGET::onSize ), nullptr, this );
105}
106
107
115
116
118{
119 wxCHECK( m_statusPanel, /* void */ );
120
121 m_status->SetLabel( aText );
122 m_preview->Hide();
123 m_statusPanel->Show();
124 Layout();
125}
126
127
129{
130 if( m_previewItem )
131 {
133 m_preview->ForceRefresh();
134 }
135
136 aEvent.Skip();
137}
138
139
141{
142 if( !m_previewItem )
143 return;
144
145 // set the view scale to fit the item on screen
146 KIGFX::VIEW* view = m_preview->GetView();
147
148 // Calculate the drawing area size, in internal units, for a scaling factor = 1.0
149 view->SetScale( 1.0 );
150 VECTOR2D clientSize = view->ToWorld( ToVECTOR2D( m_preview->GetClientSize() ), false );
151 // Calculate the draw scale to fit the drawing area
152 double scale =
153 std::min( fabs( clientSize.x / m_itemBBox.GetWidth() ), fabs( clientSize.y / m_itemBBox.GetHeight() ) );
154
155 // Above calculation will yield an exact fit; add a bit of whitespace around block
156 scale /= 1.2;
157
158 // Now fix the best scale
159 view->SetScale( scale );
160 view->SetCenter( m_itemBBox.Centre() );
161}
162
163
165{
166 KIGFX::VIEW* view = m_preview->GetView();
167
168 if( m_previewItem )
169 {
170 view->Clear();
171 delete m_previewItem;
172 m_previewItem = nullptr;
173 }
174
175 if( aDesignBlock && wxFileExists( aDesignBlock->GetBoardFile() ) )
176 {
177 try
178 {
180 WX_PROGRESS_REPORTER progressReporter( this, _( "Load PCB" ), 1, PR_CAN_ABORT );
181
182 pi->SetProgressReporter( &progressReporter );
183
184 m_previewItem = pi->LoadBoard( aDesignBlock->GetBoardFile(), nullptr ).release();
185 }
186 catch( const IO_CANCELLED& )
187 {
188 // A user-cancelled load is not an error; the preview simply stays empty.
189 }
190 catch( const IO_ERROR& ioe )
191 {
192 // You wouldn't think boardFn.GetFullPath() would throw, but we get a stack buffer
193 // underflow from ASAN. While it's probably an ASAN error, a second try/catch doesn't
194 // cost us much.
195 try
196 {
197 wxString msg = wxString::Format( _( "Error loading board file:\n%s" ), aDesignBlock->GetBoardFile() );
198 DisplayErrorMessage( this, msg, ioe.What() );
199 }
200 catch( ... )
201 {
202 // That was already our best-efforts
203 }
204 }
205
206
207 BOX2I bBox;
208
209 if( m_previewItem )
210 {
211 // The preview panel was built around a 2-layer dummy board. Re-sync layer
212 // visibility to the loaded block so inner copper layers are shown.
213 static_cast<PCB_DRAW_PANEL_GAL*>( m_preview )->SyncLayersVisibility( m_previewItem );
214
215 for( BOARD_ITEM* item : m_previewItem->GetItemSet() )
216 {
217 // Geometry-free items (constraints) are never rendered; adding them stamps
218 // their view data on an empty layer set with no benefit.
219 if( item->Type() == PCB_CONSTRAINT_T )
220 continue;
221
222 view->Add( item );
223
224 if( item->Type() == PCB_FIELD_T )
225 {
226 if( !static_cast<const PCB_FIELD*>( item )->IsVisible() )
227 continue;
228 }
229
230 bBox.Merge( item->GetBoundingBox() );
231 }
232 }
233
234 m_itemBBox = bBox;
235
236 if( !m_preview->IsShownOnScreen() )
237 {
238 m_preview->Show();
239
240 if( m_statusPanel )
241 m_statusPanel->Hide();
242
243 Layout(); // Ensure panel size is up to date.
244 }
245
246 // Calculate the draw scale to fit the drawing area
248 }
249
250 m_preview->ForceRefresh();
251 m_preview->Show();
252}
BOX2< VECTOR2I > BOX2I
Definition box2.h:914
WINDOW_SETTINGS m_Window
wxString m_ColorTheme
Active color theme name.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:594
Color settings are a bit different than most of the settings objects in that there can be more than o...
DESIGN_BLOCK_PREVIEW_WIDGET(wxWindow *aParent)
Construct a design block preview widget.
const wxString & GetBoardFile() const
static FOOTPRINT_PREVIEW_PANEL * New(KIWAY *aKiway, wxWindow *aParent, UNITS_PROVIDER *aUnitsProvider)
An exception saying that the user cancelled an interactive part of a load, import,...
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
wxColour ToColour() const
Definition color4d.cpp:221
PCB specific render settings.
Definition pcb_painter.h:84
void LoadColors(const COLOR_SETTINGS *aSettings) override
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:63
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Set the scaling factor, zooming around a given anchor point.
Definition view.cpp:651
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition view.cpp:297
void Clear()
Remove all items from the view.
Definition view.cpp:1262
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Converts a screen space point/vector to a point/vector in world space coordinates.
Definition view.cpp:548
PAINTER * GetPainter() const
Return the painter object used by the view for drawing VIEW_ITEM objects.
Definition view.h:227
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition view.cpp:677
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
PCB_DESIGN_BLOCK_PREVIEW_WIDGET(wxWindow *aParent, PCB_EDIT_FRAME *aFrame)
Construct a schematic design block preview widget.
BOX2I m_itemBBox
The bounding box of the current item.
void SetStatusText(const wxString &aText) override
Set the contents of the status label and display it.
void DisplayDesignBlock(DESIGN_BLOCK *aDesignBlock) override
Set the currently displayed symbol.
The main frame for Pcbnew.
@ KICAD_SEXP
S-expression Pcbnew file format.
Definition pcb_io_mgr.h:54
static PCB_IO * FindPlugin(PCB_FILE_T aFileType)
Return a PCB_IO which the caller can use to import, export, save, or load design documents.
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:546
Multi-thread safe progress reporter dialog, intended for use of tasks that parallel reporting back of...
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
This file is part of the common library.
#define _(s)
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition io_mgr.h:33
@ LAYER_REFERENCEPART
Definition layer_ids.h:482
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
#define DEFAULT_THEME
COLOR_SETTINGS * GetColorSettings(const wxString &aName)
T * GetAppSettings(const char *aFilename)
const int scale
@ PCB_CONSTRAINT_T
a geometric constraint between board items
Definition typeinfo.h:237
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:82
VECTOR2< double > VECTOR2D
Definition vector2d.h:707
VECTOR2D ToVECTOR2D(const wxPoint &aPoint)
Definition vector2wx.h:36
#define PR_CAN_ABORT