KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_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 <schematic.h>
21#include <sch_sheet.h>
22#include <sch_view.h>
23#include <sch_screen.h>
26#include <math/vector2wx.h>
27#include <design_block.h>
28#include <sch_preview_panel.h>
29#include <pgm_base.h>
30#include <sch_painter.h>
31#include <eda_draw_frame.h>
32#include <project_sch.h>
33#include <eeschema_settings.h>
34#include <eeschema_helpers.h>
37#include <wx/log.h>
38#include <wx/stattext.h>
39#include <wx/panel.h>
40
41
44 bool aIncludeStatus ) :
46 m_preview( nullptr ),
47 m_status( nullptr ),
48 m_statusPanel( nullptr ),
49 m_statusSizer( nullptr ),
50 m_previewItem( nullptr )
51{
52 COMMON_SETTINGS* common_settings = Pgm().GetCommonSettings();
54
55 m_galDisplayOptions.ReadConfig( *common_settings, cfg->m_Window, this );
56 m_galDisplayOptions.m_forceDisplayCursor = false;
57
58 EDA_DRAW_PANEL_GAL::GAL_TYPE canvasType = aCanvasType;
59
60 // Allows only a CAIRO or OPENGL canvas:
62 {
64 }
65
66 m_preview = new SCH_PREVIEW_PANEL( this, wxID_ANY, wxDefaultPosition, wxSize( -1, -1 ), m_galDisplayOptions,
67 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...).
76 m_preview->GetGAL()->SetGridVisibility( true );
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
84 settings->LoadColors( cs );
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( SCH_DESIGN_BLOCK_PREVIEW_WIDGET::onSize ), nullptr, this );
134}
135
136
144
145
147{
148 wxCHECK( m_statusPanel, /* void */ );
149
150 m_status->SetLabel( aText );
151 m_preview->Hide();
152 m_statusPanel->Show();
153 Layout();
154}
155
156
158{
159 if( m_previewItem )
160 {
162 m_preview->ForceRefresh();
163 }
164
165 aEvent.Skip();
166}
167
168
170{
171 if( !m_previewItem )
172 return;
173
174 // set the view scale to fit the item on screen
175 KIGFX::VIEW* view = m_preview->GetView();
176
177 // Calculate the drawing area size, in internal units, for a scaling factor = 1.0
178 view->SetScale( 1.0 );
179 VECTOR2D clientSize = view->ToWorld( ToVECTOR2D( m_preview->GetClientSize() ), false );
180 // Calculate the draw scale to fit the drawing area
181 double scale =
182 std::min( fabs( clientSize.x / m_itemBBox.GetWidth() ), fabs( clientSize.y / m_itemBBox.GetHeight() ) );
183
184 // Above calculation will yield an exact fit; add a bit of whitespace around block
185 scale /= 1.2;
186
187 // Now fix the best scale
188 view->SetScale( scale );
189 view->SetCenter( m_itemBBox.Centre() );
190}
191
192
194{
195 KIGFX::VIEW* view = m_preview->GetView();
196
197 if( m_previewItem )
198 {
199 view->Clear();
200 delete m_previewItem;
201 m_previewItem = nullptr;
202 }
203
204 if( aDesignBlock && wxFileExists( aDesignBlock->GetSchematicFile() ) )
205 {
206 m_previewItem = EESCHEMA_HELPERS::LoadSchematic( aDesignBlock->GetSchematicFile(), SCH_IO_MGR::SCH_KICAD,
207 false, true, nullptr, false );
208 BOX2I bBox;
209
210 if( m_previewItem )
211 {
212 for( EDA_ITEM* item : m_previewItem->CurrentSheet().LastScreen()->Items() )
213 {
214 view->Add( item );
215
216 if( item->Type() == SCH_FIELD_T )
217 {
218 if( !static_cast<const SCH_FIELD*>( item )->IsVisible() )
219 continue;
220 }
221
222 bBox.Merge( item->GetBoundingBox() );
223 }
224 }
225
226 m_itemBBox = bBox;
227
228 if( !m_preview->IsShownOnScreen() )
229 {
230 m_preview->Show();
231
232 if( m_statusPanel )
233 m_statusPanel->Hide();
234
235 Layout(); // Ensure panel size is up to date.
236 }
237
238 // Calculate the draw scale to fit the drawing area
240 }
241
242 m_preview->ForceRefresh();
243 m_preview->Show();
244}
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
WINDOW_SETTINGS m_Window
wxString m_ColorTheme
Active color theme name.
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 & GetSchematicFile() const
static constexpr GAL_TYPE GAL_FALLBACK
@ GAL_TYPE_OPENGL
OpenGL implementation.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
static SCHEMATIC * LoadSchematic(const wxString &aFileName, bool aSetActive, bool aForceDefaultProject, PROJECT *aProject=nullptr, bool aCalculateConnectivity=true)
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:104
wxColour ToColour() const
Definition color4d.cpp:220
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
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:537
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.
BOX2I m_itemBBox
The bounding box of the current item.
SCH_DESIGN_BLOCK_PREVIEW_WIDGET(wxWindow *aParent, EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType, bool aIncludeStatus)
Construct a schematic design block preview widget.
void LoadColors(const COLOR_SETTINGS *aSettings) override
@ 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
@ SCH_FIELD_T
Definition typeinfo.h:154
VECTOR2< double > VECTOR2D
Definition vector2d.h:694
VECTOR2D ToVECTOR2D(const wxPoint &aPoint)
Definition vector2wx.h:40