KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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
21#include <schematic.h>
22#include <sch_sheet.h>
23#include <sch_view.h>
24#include <sch_screen.h>
27#include <math/vector2wx.h>
29#include <design_block.h>
30#include <sch_preview_panel.h>
31#include <pgm_base.h>
32#include <sch_painter.h>
33#include <eda_draw_frame.h>
34#include <project_sch.h>
35#include <eeschema_settings.h>
36#include <eeschema_helpers.h>
38#include <wx/log.h>
39#include <wx/stattext.h>
40
41
42DESIGN_BLOCK_PREVIEW_WIDGET::DESIGN_BLOCK_PREVIEW_WIDGET( wxWindow* aParent, bool aIncludeStatus,
43 EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType ) :
44 wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0 ),
45 m_preview( nullptr ),
46 m_status( nullptr ),
47 m_statusPanel( nullptr ),
48 m_statusSizer( nullptr ),
49 m_previewItem( nullptr )
50{
52 COMMON_SETTINGS* common_settings = Pgm().GetCommonSettings();
53 EESCHEMA_SETTINGS* app_settings = mgr.GetAppSettings<EESCHEMA_SETTINGS>( "eeschema" );
54
55 m_galDisplayOptions.ReadConfig( *common_settings, app_settings->m_Window, this );
57
58 EDA_DRAW_PANEL_GAL::GAL_TYPE canvasType = aCanvasType;
59
60 // Allows only a CAIRO or OPENGL canvas:
62 && canvasType != EDA_DRAW_PANEL_GAL::GAL_FALLBACK )
63 {
65 }
66
67 m_preview = new SCH_PREVIEW_PANEL( this, wxID_ANY, wxDefaultPosition, wxSize( -1, -1 ),
68 m_galDisplayOptions, canvasType );
69 m_preview->SetStealsFocus( false );
70 m_preview->ShowScrollbars( wxSHOW_SB_NEVER, wxSHOW_SB_NEVER );
71 m_preview->GetGAL()->SetAxesEnabled( false );
72
73 // Do not display the grid: the look is not good for a small canvas area.
74 // But mainly, due to some strange bug I (JPC) was unable to fix, the grid creates
75 // strange artifacts on Windows when Eeschema is run from KiCad manager (but not in
76 // stand alone...).
78
79 // Early initialization of the canvas background color,
80 // before any OnPaint event is fired for the canvas using a wrong bg color
81 KIGFX::VIEW* view = m_preview->GetView();
82 auto settings = static_cast<SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
83
84 if( auto* theme = Pgm().GetSettingsManager().GetColorSettings( app_settings->m_ColorTheme ) )
85 settings->LoadColors( theme );
86
87 const COLOR4D& backgroundColor = settings->GetBackgroundColor();
88 const COLOR4D& foregroundColor = settings->GetCursorColor();
89
90 m_preview->GetGAL()->SetClearColor( backgroundColor );
91
92 settings->m_ShowPinsElectricalType = false;
93 settings->m_ShowPinNumbers = false;
94 settings->m_ShowHiddenPins = false;
95 settings->m_ShowHiddenFields = false;
96 settings->m_ShowPinAltIcons = false;
97
98 m_outerSizer = new wxBoxSizer( wxVERTICAL );
99
100 if( aIncludeStatus )
101 {
102 m_statusPanel = new wxPanel( this );
103 m_statusPanel->SetBackgroundColour( backgroundColor.ToColour() );
104 m_status = new wxStaticText( m_statusPanel, wxID_ANY, wxEmptyString );
105 m_status->SetForegroundColour( settings->GetLayerColor( LAYER_REFERENCEPART ).ToColour() );
106 m_statusSizer = new wxBoxSizer( wxVERTICAL );
107 m_statusSizer->Add( 0, 0, 1 ); // add a spacer
108 m_statusSizer->Add( m_status, 0, wxALIGN_CENTER );
109 m_statusSizer->Add( 0, 0, 1 ); // add a spacer
110 m_statusPanel->SetSizer( m_statusSizer );
111
112 // Give the status panel the same color scheme as the canvas so it isn't jarring when
113 // switched to.
114 m_statusPanel->SetBackgroundColour( backgroundColor.ToColour() );
115 m_statusPanel->SetForegroundColour( foregroundColor.ToColour() );
116
117 // Give the preview panel a small top border to align its top with the status panel,
118 // and give the status panel a small bottom border to align its bottom with the preview
119 // panel.
120 m_outerSizer->Add( m_preview, 1, wxTOP | wxEXPAND, 5 );
121 m_outerSizer->Add( m_statusPanel, 1, wxBOTTOM | wxEXPAND, 5 );
122
123 // Hide the status panel to start
124 m_statusPanel->Hide();
125 }
126 else
127 {
128 m_outerSizer->Add( m_preview, 1, wxEXPAND, 0 );
129 }
130
131 SetSizer( m_outerSizer );
132 Layout();
133
134 Connect( wxEVT_SIZE, wxSizeEventHandler( DESIGN_BLOCK_PREVIEW_WIDGET::onSize ), nullptr, this );
135}
136
137
139{
140 if( m_previewItem )
142
143 delete m_previewItem;
144}
145
146
148{
149 wxCHECK( m_statusPanel, /* void */ );
150
151 m_status->SetLabel( aText );
152 m_preview->Hide();
153 m_statusPanel->Show();
154 Layout();
155}
156
157
158void DESIGN_BLOCK_PREVIEW_WIDGET::onSize( wxSizeEvent& aEvent )
159{
160 if( m_previewItem )
161 {
164 }
165
166 aEvent.Skip();
167}
168
169
171{
172 if( !m_previewItem )
173 return;
174
175 // set the view scale to fit the item on screen
176 KIGFX::VIEW* view = m_preview->GetView();
177
178 // Calculate the drawing area size, in internal units, for a scaling factor = 1.0
179 view->SetScale( 1.0 );
180 VECTOR2D clientSize = view->ToWorld( ToVECTOR2D( m_preview->GetClientSize() ), false );
181 // Calculate the draw scale to fit the drawing area
182 double scale = std::min( fabs( clientSize.x / m_itemBBox.GetWidth() ),
183 fabs( clientSize.y / m_itemBBox.GetHeight() ) );
184
185 // Above calculation will yield an exact fit; add a bit of whitespace around block
186 scale /= 1.2;
187
188 // Now fix the best scale
189 view->SetScale( scale );
190 view->SetCenter( m_itemBBox.Centre() );
191}
192
193
195{
196 KIGFX::VIEW* view = m_preview->GetView();
197
198 if( m_previewItem )
199 {
200 view->Clear();
201 delete m_previewItem;
202 m_previewItem = nullptr;
203 }
204
205 if( aDesignBlock )
206 {
208 SCH_IO_MGR::SCH_KICAD, false, true );
209 BOX2I bBox;
210
211 if( m_previewItem )
212 {
213 for( EDA_ITEM* item : m_previewItem->CurrentSheet().LastScreen()->Items() )
214 {
215 view->Add( item );
216
217 if( item->Type() == SCH_FIELD_T )
218 {
219 if( !static_cast<const SCH_FIELD*>( item )->IsVisible() )
220 continue;
221 }
222
223 bBox.Merge( item->GetBoundingBox() );
224 }
225 }
226
227 m_itemBBox = bBox;
228
229 if( !m_preview->IsShownOnScreen() )
230 {
231 m_preview->Show();
232
233 if( m_statusPanel )
234 m_statusPanel->Hide();
235
236 Layout(); // Ensure panel size is up to date.
237 }
238
239 // Calculate the draw scale to fit the drawing area
241 }
242
244 m_preview->Show();
245}
WINDOW_SETTINGS m_Window
Definition: app_settings.h:196
wxString m_ColorTheme
Active color theme name.
Definition: app_settings.h:199
constexpr size_type GetWidth() const
Definition: box2.h:214
constexpr Vec Centre() const
Definition: box2.h:97
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
constexpr size_type GetHeight() const
Definition: box2.h:215
BOX2I m_itemBBox
The bounding box of the current item.
void DisplayDesignBlock(DESIGN_BLOCK *aDesignBlock)
Set the currently displayed symbol.
GAL_DISPLAY_OPTIONS_IMPL m_galDisplayOptions
void SetStatusText(const wxString &aText)
Set the contents of the status label and display it.
DESIGN_BLOCK_PREVIEW_WIDGET(wxWindow *aParent, bool aIncludeStatus, EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType)
Construct a symbol preview widget.
const wxString & GetSchematicFile() const
Definition: design_block.h:41
static constexpr GAL_TYPE GAL_FALLBACK
virtual KIGFX::VIEW * GetView() const
Return a pointer to the #VIEW instance used in the panel.
void ForceRefresh()
Force a redraw.
@ GAL_TYPE_OPENGL
OpenGL implementation.
KIGFX::GAL * GetGAL() const
Return a pointer to the GAL instance used in the panel.
void SetStealsFocus(bool aStealsFocus)
Set whether focus is taken on certain events (mouseover, keys, etc).
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
static SCHEMATIC * LoadSchematic(const wxString &aFileName, bool aSetActive, bool aForceDefaultProject, PROJECT *aProject=nullptr)
void ReadConfig(COMMON_SETTINGS &aCommonConfig, WINDOW_SETTINGS &aWindowConfig, wxWindow *aWindow)
Read application and common configs.
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
wxColour ToColour() const
Definition: color4d.cpp:220
bool m_forceDisplayCursor
The pixel scale factor (>1 for hi-DPI scaled displays)
void SetAxesEnabled(bool aAxesEnabled)
Enable drawing the axes.
void SetClearColor(const COLOR4D &aColor)
void SetGridVisibility(bool aVisibility)
Set the visibility setting of the grid.
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Set the scaling factor, zooming around a given anchor point.
Definition: view.cpp:560
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition: view.cpp:297
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition: view.cpp:332
void Clear()
Remove all items from the view.
Definition: view.cpp:1131
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:457
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:216
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition: view.cpp:586
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition: pgm_base.cpp:689
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:125
SCH_SHEET_PATH & CurrentSheet() const override
Definition: schematic.h:162
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:53
void LoadColors(const COLOR_SETTINGS *aSettings) override
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition: sch_screen.h:109
SCH_SCREEN * LastScreen()
COLOR_SETTINGS * GetColorSettings(const wxString &aName="user")
Retrieve a color settings object that applications can read colors from.
T * GetAppSettings(const wxString &aFilename)
Return a handle to the a given settings by type.
@ LAYER_REFERENCEPART
Definition: layer_ids.h:449
PGM_BASE & Pgm()
The global program "get" accessor.
Definition: pgm_base.cpp:1073
see class PGM_BASE
const int scale
@ SCH_FIELD_T
Definition: typeinfo.h:150
VECTOR2D ToVECTOR2D(const wxPoint &aPoint)
Definition: vector2wx.h:40