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 (C) 2018-2024 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{
51 auto common_settings = Pgm().GetCommonSettings();
52 auto app_settings = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
53
54 m_galDisplayOptions.ReadConfig( *common_settings, app_settings->m_Window, this );
56
57 EDA_DRAW_PANEL_GAL::GAL_TYPE canvasType = aCanvasType;
58
59 // Allows only a CAIRO or OPENGL canvas:
61 && canvasType != EDA_DRAW_PANEL_GAL::GAL_FALLBACK )
62 {
64 }
65
66 m_preview = new SCH_PREVIEW_PANEL( this, wxID_ANY, wxDefaultPosition, wxSize( -1, -1 ),
67 m_galDisplayOptions, canvasType );
68 m_preview->SetStealsFocus( false );
69 m_preview->ShowScrollbars( wxSHOW_SB_NEVER, wxSHOW_SB_NEVER );
70 m_preview->GetGAL()->SetAxesEnabled( false );
71
72 // Do not display the grid: the look is not good for a small canvas area.
73 // But mainly, due to some strange bug I (JPC) was unable to fix, the grid creates
74 // strange artifacts on Windows when Eeschema is run from KiCad manager (but not in
75 // stand alone...).
77
78 // Early initialization of the canvas background color,
79 // before any OnPaint event is fired for the canvas using a wrong bg color
80 KIGFX::VIEW* view = m_preview->GetView();
81 auto settings = static_cast<SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
82
83 if( auto* theme = Pgm().GetSettingsManager().GetColorSettings( app_settings->m_ColorTheme ) )
84 settings->LoadColors( theme );
85
86 const COLOR4D& backgroundColor = settings->GetBackgroundColor();
87 const COLOR4D& foregroundColor = settings->GetCursorColor();
88
89 m_preview->GetGAL()->SetClearColor( backgroundColor );
90
91 settings->m_ShowPinsElectricalType = false;
92 settings->m_ShowPinNumbers = false;
93 settings->m_ShowHiddenPins = false;
94 settings->m_ShowHiddenFields = false;
95 settings->m_ShowPinAltIcons = false;
96
97 m_outerSizer = new wxBoxSizer( wxVERTICAL );
98
99 if( aIncludeStatus )
100 {
101 m_statusPanel = new wxPanel( this );
102 m_statusPanel->SetBackgroundColour( backgroundColor.ToColour() );
103 m_status = new wxStaticText( m_statusPanel, wxID_ANY, wxEmptyString );
104 m_status->SetForegroundColour( settings->GetLayerColor( LAYER_REFERENCEPART ).ToColour() );
105 m_statusSizer = new wxBoxSizer( wxVERTICAL );
106 m_statusSizer->Add( 0, 0, 1 ); // add a spacer
107 m_statusSizer->Add( m_status, 0, wxALIGN_CENTER );
108 m_statusSizer->Add( 0, 0, 1 ); // add a spacer
109 m_statusPanel->SetSizer( m_statusSizer );
110
111 // Give the status panel the same color scheme as the canvas so it isn't jarring when
112 // switched to.
113 m_statusPanel->SetBackgroundColour( backgroundColor.ToColour() );
114 m_statusPanel->SetForegroundColour( foregroundColor.ToColour() );
115
116 // Give the preview panel a small top border to align its top with the status panel,
117 // and give the status panel a small bottom border to align its bottom with the preview
118 // panel.
119 m_outerSizer->Add( m_preview, 1, wxTOP | wxEXPAND, 5 );
120 m_outerSizer->Add( m_statusPanel, 1, wxBOTTOM | wxEXPAND, 5 );
121
122 // Hide the status panel to start
123 m_statusPanel->Hide();
124 }
125 else
126 {
127 m_outerSizer->Add( m_preview, 1, wxEXPAND, 0 );
128 }
129
130 SetSizer( m_outerSizer );
131 Layout();
132
133 Connect( wxEVT_SIZE, wxSizeEventHandler( DESIGN_BLOCK_PREVIEW_WIDGET::onSize ), nullptr, this );
134}
135
136
138{
139 delete m_preview;
140 delete m_previewItem;
141}
142
143
145{
146 wxCHECK( m_statusPanel, /* void */ );
147
148 m_status->SetLabel( aText );
149 m_preview->Hide();
150 m_statusPanel->Show();
151 Layout();
152}
153
154
155void DESIGN_BLOCK_PREVIEW_WIDGET::onSize( wxSizeEvent& aEvent )
156{
157 if( m_previewItem )
158 {
161 }
162
163 aEvent.Skip();
164}
165
166
168{
169 if( !m_previewItem )
170 return;
171
172 // set the view scale to fit the item on screen
173 KIGFX::VIEW* view = m_preview->GetView();
174
175 // Calculate the drawing area size, in internal units, for a scaling factor = 1.0
176 view->SetScale( 1.0 );
177 VECTOR2D clientSize = view->ToWorld( ToVECTOR2D( m_preview->GetClientSize() ), false );
178 // Calculate the draw scale to fit the drawing area
179 double scale = std::min( fabs( clientSize.x / m_itemBBox.GetWidth() ),
180 fabs( clientSize.y / m_itemBBox.GetHeight() ) );
181
182 // Above calculation will yield an exact fit; add a bit of whitespace around block
183 scale /= 1.2;
184
185 // Now fix the best scale
186 view->SetScale( scale );
187 view->SetCenter( m_itemBBox.Centre() );
188}
189
190
192{
193 KIGFX::VIEW* view = m_preview->GetView();
194
195 if( m_previewItem )
196 {
197 view->Clear();
198 delete m_previewItem;
199 m_previewItem = nullptr;
200 }
201
202 if( aDesignBlock )
203 {
205 SCH_IO_MGR::SCH_KICAD, false, true );
206 BOX2I bBox;
207
208 if( m_previewItem )
209 {
210 for( EDA_ITEM* item : m_previewItem->CurrentSheet().LastScreen()->Items() )
211 {
212 view->Add( item );
213
214 if( item->Type() == SCH_FIELD_T )
215 {
216 if( !static_cast<const SCH_FIELD*>( item )->IsVisible() )
217 continue;
218 }
219
220 bBox.Merge( item->GetBoundingBox() );
221 }
222 }
223
224 m_itemBBox = bBox;
225
226 if( !m_preview->IsShownOnScreen() )
227 {
228 m_preview->Show();
229
230 if( m_statusPanel )
231 m_statusPanel->Hide();
232
233 Layout(); // Ensure panel size is up to date.
234 }
235
236 // Calculate the draw scale to fit the drawing area
238 }
239
241 m_preview->Show();
242}
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:68
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Set the scaling factor, zooming around a given anchor point.
Definition: view.cpp:587
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition: view.cpp:317
void Clear()
Remove all items from the view.
Definition: view.cpp:1168
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:484
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:221
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition: view.cpp:613
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition: pgm_base.cpp:679
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:142
SCH_SHEET_PATH & CurrentSheet() const override
Definition: schematic.h:152
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:51
void LoadColors(const COLOR_SETTINGS *aSettings) override
EE_RTREE & Items()
Gets the full RTree, usually for iterating.
Definition: sch_screen.h:108
SCH_SCREEN * LastScreen()
T * GetAppSettings()
Returns a handle to the a given settings by type If the settings have already been loaded,...
COLOR_SETTINGS * GetColorSettings(const wxString &aName="user")
Retrieves a color settings object that applications can read colors from.
@ LAYER_REFERENCEPART
Definition: layer_ids.h:364
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1060
see class PGM_BASE
const int scale
@ SCH_FIELD_T
Definition: typeinfo.h:150
VECTOR2D ToVECTOR2D(const wxPoint &aPoint)
Definition: vector2wx.h:40