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 along
17 * with this program. If not, see <http://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
61 // Do not display the grid: the look is not good for a small canvas area.
62 // But mainly, due to some strange bug I (JPC) was unable to fix, the grid creates
63 // strange artifacts on Windows when Pcb is run from KiCad manager (but not in
64 // stand alone...).
65 m_preview->GetGAL()->SetGridVisibility( true );
66
67 // Early initialization of the canvas background color,
68 // before any OnPaint event is fired for the canvas using a wrong bg color
69 KIGFX::VIEW* view = m_preview->GetView();
70 auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
71
73 settings->LoadColors( cs );
74
75 const COLOR4D& backgroundColor = settings->GetBackgroundColor();
76 const COLOR4D& foregroundColor = settings->GetCursorColor();
77
78 m_preview->GetGAL()->SetClearColor( backgroundColor );
79
80 m_outerSizer = new wxBoxSizer( wxVERTICAL );
81
82 m_statusPanel = new wxPanel( this );
83 m_statusPanel->SetBackgroundColour( backgroundColor.ToColour() );
84 m_status = new wxStaticText( m_statusPanel, wxID_ANY, wxEmptyString );
85 m_status->SetForegroundColour( settings->GetLayerColor( LAYER_REFERENCEPART ).ToColour() );
86 m_statusSizer = new wxBoxSizer( wxVERTICAL );
87 m_statusSizer->Add( 0, 0, 1 ); // add a spacer
88 m_statusSizer->Add( m_status, 0, wxALIGN_CENTER );
89 m_statusSizer->Add( 0, 0, 1 ); // add a spacer
90 m_statusPanel->SetSizer( m_statusSizer );
91
92 // Give the status panel the same color scheme as the canvas so it isn't jarring when
93 // switched to.
94 m_statusPanel->SetBackgroundColour( backgroundColor.ToColour() );
95 m_statusPanel->SetForegroundColour( foregroundColor.ToColour() );
96
97 // Give the preview panel a small top border to align its top with the status panel,
98 // and give the status panel a small bottom border to align its bottom with the preview
99 // panel.
100 m_outerSizer->Add( m_preview, 1, wxTOP | wxEXPAND, 5 );
101 m_outerSizer->Add( m_statusPanel, 1, wxBOTTOM | wxEXPAND, 5 );
102
103 // Hide the status panel to start
104 m_statusPanel->Hide();
105
106 SetSizer( m_outerSizer );
107 Layout();
108
109 Connect( wxEVT_SIZE, wxSizeEventHandler( PCB_DESIGN_BLOCK_PREVIEW_WIDGET::onSize ), nullptr, this );
110}
111
112
120
121
123{
124 wxCHECK( m_statusPanel, /* void */ );
125
126 m_status->SetLabel( aText );
127 m_preview->Hide();
128 m_statusPanel->Show();
129 Layout();
130}
131
132
134{
135 if( m_previewItem )
136 {
138 m_preview->ForceRefresh();
139 }
140
141 aEvent.Skip();
142}
143
144
146{
147 if( !m_previewItem )
148 return;
149
150 // set the view scale to fit the item on screen
151 KIGFX::VIEW* view = m_preview->GetView();
152
153 // Calculate the drawing area size, in internal units, for a scaling factor = 1.0
154 view->SetScale( 1.0 );
155 VECTOR2D clientSize = view->ToWorld( ToVECTOR2D( m_preview->GetClientSize() ), false );
156 // Calculate the draw scale to fit the drawing area
157 double scale =
158 std::min( fabs( clientSize.x / m_itemBBox.GetWidth() ), fabs( clientSize.y / m_itemBBox.GetHeight() ) );
159
160 // Above calculation will yield an exact fit; add a bit of whitespace around block
161 scale /= 1.2;
162
163 // Now fix the best scale
164 view->SetScale( scale );
165 view->SetCenter( m_itemBBox.Centre() );
166}
167
168
170{
171 KIGFX::VIEW* view = m_preview->GetView();
172
173 if( m_previewItem )
174 {
175 view->Clear();
176 delete m_previewItem;
177 m_previewItem = nullptr;
178 }
179
180 if( aDesignBlock && wxFileExists( aDesignBlock->GetBoardFile() ) )
181 {
182 try
183 {
185 WX_PROGRESS_REPORTER progressReporter( this, _( "Load PCB" ), 1, PR_CAN_ABORT );
186
187 pi->SetProgressReporter( &progressReporter );
188
189 m_previewItem = pi->LoadBoard( aDesignBlock->GetBoardFile(), nullptr );
190 }
191 catch( const IO_ERROR& ioe )
192 {
193 // You wouldn't think boardFn.GetFullPath() would throw, but we get a stack buffer
194 // underflow from ASAN. While it's probably an ASAN error, a second try/catch doesn't
195 // cost us much.
196 try
197 {
198 if( ioe.Problem() != wxT( "CANCEL" ) )
199 {
200 wxString msg =
201 wxString::Format( _( "Error loading board file:\n%s" ), aDesignBlock->GetBoardFile() );
202 DisplayErrorMessage( this, msg, ioe.What() );
203 }
204 }
205 catch( ... )
206 {
207 // That was already our best-efforts
208 }
209 }
210
211
212 BOX2I bBox;
213
214 if( m_previewItem )
215 {
216 for( BOARD_ITEM* item : m_previewItem->GetItemSet() )
217 {
218 view->Add( item );
219
220 if( item->Type() == PCB_FIELD_T )
221 {
222 if( !static_cast<const PCB_FIELD*>( item )->IsVisible() )
223 continue;
224 }
225
226 bBox.Merge( item->GetBoundingBox() );
227 }
228 }
229
230 m_itemBBox = bBox;
231
232 if( !m_preview->IsShownOnScreen() )
233 {
234 m_preview->Show();
235
236 if( m_statusPanel )
237 m_statusPanel->Hide();
238
239 Layout(); // Ensure panel size is up to date.
240 }
241
242 // Calculate the draw scale to fit the drawing area
244 }
245
246 m_preview->ForceRefresh();
247 m_preview->Show();
248}
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
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:79
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:658
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)
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()
virtual const wxString Problem() const
what was the problem?
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:104
wxColour ToColour() const
Definition color4d.cpp:220
PCB specific render settings.
Definition pcb_painter.h:82
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:66
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Set the scaling factor, zooming around a given anchor point.
Definition view.cpp:570
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition view.cpp:298
void Clear()
Remove all items from the view.
Definition view.cpp:1143
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:467
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition view.h:220
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition view.cpp:596
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.
static PCB_IO * PluginFind(PCB_FILE_T aFileType)
Return a #PLUGIN which the caller can use to import, export, save, or load design documents.
@ KICAD_SEXP
S-expression Pcbnew file format.
Definition pcb_io_mgr.h:58
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:537
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:202
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:460
PGM_BASE & Pgm()
The global program "get" accessor.
Definition pgm_base.cpp:946
see class PGM_BASE
#define DEFAULT_THEME
COLOR_SETTINGS * GetColorSettings(const wxString &aName)
T * GetAppSettings(const char *aFilename)
const int scale
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:90
VECTOR2< double > VECTOR2D
Definition vector2d.h:694
VECTOR2D ToVECTOR2D(const wxPoint &aPoint)
Definition vector2wx.h:40
#define PR_CAN_ABORT