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{
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 delete m_preview;
141 delete m_previewItem;
142}
143
144
146{
147 wxCHECK( m_statusPanel, /* void */ );
148
149 m_status->SetLabel( aText );
150 m_preview->Hide();
151 m_statusPanel->Show();
152 Layout();
153}
154
155
156void DESIGN_BLOCK_PREVIEW_WIDGET::onSize( wxSizeEvent& aEvent )
157{
158 if( m_previewItem )
159 {
162 }
163
164 aEvent.Skip();
165}
166
167
169{
170 if( !m_previewItem )
171 return;
172
173 // set the view scale to fit the item on screen
174 KIGFX::VIEW* view = m_preview->GetView();
175
176 // Calculate the drawing area size, in internal units, for a scaling factor = 1.0
177 view->SetScale( 1.0 );
178 VECTOR2D clientSize = view->ToWorld( ToVECTOR2D( m_preview->GetClientSize() ), false );
179 // Calculate the draw scale to fit the drawing area
180 double scale = std::min( fabs( clientSize.x / m_itemBBox.GetWidth() ),
181 fabs( clientSize.y / m_itemBBox.GetHeight() ) );
182
183 // Above calculation will yield an exact fit; add a bit of whitespace around block
184 scale /= 1.2;
185
186 // Now fix the best scale
187 view->SetScale( scale );
188 view->SetCenter( m_itemBBox.Centre() );
189}
190
191
193{
194 KIGFX::VIEW* view = m_preview->GetView();
195
196 if( m_previewItem )
197 {
198 view->Clear();
199 delete m_previewItem;
200 m_previewItem = nullptr;
201 }
202
203 if( aDesignBlock )
204 {
206 SCH_IO_MGR::SCH_KICAD, false, true );
207 BOX2I bBox;
208
209 if( m_previewItem )
210 {
211 for( EDA_ITEM* item : m_previewItem->CurrentSheet().LastScreen()->Items() )
212 {
213 view->Add( item );
214
215 if( item->Type() == SCH_FIELD_T )
216 {
217 if( !static_cast<const SCH_FIELD*>( item )->IsVisible() )
218 continue;
219 }
220
221 bBox.Merge( item->GetBoundingBox() );
222 }
223 }
224
225 m_itemBBox = bBox;
226
227 if( !m_preview->IsShownOnScreen() )
228 {
229 m_preview->Show();
230
231 if( m_statusPanel )
232 m_statusPanel->Hide();
233
234 Layout(); // Ensure panel size is up to date.
235 }
236
237 // Calculate the draw scale to fit the drawing area
239 }
240
242 m_preview->Show();
243}
WINDOW_SETTINGS m_Window
Definition: app_settings.h:194
wxString m_ColorTheme
Active color theme name.
Definition: app_settings.h:197
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:562
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition: view.cpp:299
void Clear()
Remove all items from the view.
Definition: view.cpp:1127
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:459
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:217
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition: view.cpp:588
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:156
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()
COLOR_SETTINGS * GetColorSettings(const wxString &aName="user")
Retrieves a color settings object that applications can read colors from.
T * GetAppSettings(const wxString &aFilename)
Returns a handle to the a given settings by type If the settings have already been loaded,...
@ 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