KiCad PCB EDA Suite
SYMBOL_PREVIEW_WIDGET Class Reference

#include <symbol_preview_widget.h>

Inheritance diagram for SYMBOL_PREVIEW_WIDGET:

Public Member Functions

 SYMBOL_PREVIEW_WIDGET (wxWindow *aParent, KIWAY &aKiway, EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType)
 Construct a symbol preview widget. More...
 
 ~SYMBOL_PREVIEW_WIDGET () override
 
void SetStatusText (wxString const &aText)
 Set the contents of the status label and display it. More...
 
void DisplaySymbol (const LIB_ID &aSymbolID, int aUnit, int aConvert=0)
 Set the currently displayed symbol. More...
 
void DisplayPart (LIB_PART *aPart, int aUnit, int aConvert=0)
 

Private Member Functions

void onSize (wxSizeEvent &aEvent)
 
void fitOnDrawArea ()
 

Private Attributes

KIWAYm_kiway
 
KIGFX::GAL_DISPLAY_OPTIONS m_galDisplayOptions
 
EDA_DRAW_PANEL_GALm_preview
 
wxStaticText * m_status
 
wxPanel * m_statusPanel
 
wxSizer * m_statusSizer
 
wxSizer * m_outerSizer
 
LIB_PARTm_previewItem
 A local copy of the LIB_PART to display on the canvas. More...
 
BOX2I m_itemBBox
 The bounding box of the current item. More...
 

Detailed Description

Definition at line 35 of file symbol_preview_widget.h.

Constructor & Destructor Documentation

◆ SYMBOL_PREVIEW_WIDGET()

SYMBOL_PREVIEW_WIDGET::SYMBOL_PREVIEW_WIDGET ( wxWindow *  aParent,
KIWAY aKiway,
EDA_DRAW_PANEL_GAL::GAL_TYPE  aCanvasType 
)

Construct a symbol preview widget.

Parameters
aParent- parent window
aKiway- an active Kiway instance
aCanvasType= the type of canvas (GAL_TYPE_OPENGL or GAL_TYPE_CAIRO only)

Definition at line 32 of file symbol_preview_widget.cpp.

33  :
34  wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0 ),
35  m_kiway( aKiway ),
36  m_preview( nullptr ), m_status( nullptr ), m_statusSizer( nullptr ), m_previewItem( nullptr )
37 {
38  auto common_settings = Pgm().GetCommonSettings();
39  auto app_settings = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
40 
41  m_galDisplayOptions.ReadConfig( *common_settings, app_settings->m_Window, this );
43 
44  EDA_DRAW_PANEL_GAL::GAL_TYPE canvasType = aCanvasType;
45 
46  // Allows only a CAIRO or OPENGL canvas:
47  if( canvasType != EDA_DRAW_PANEL_GAL::GAL_TYPE_OPENGL
48  && canvasType != EDA_DRAW_PANEL_GAL::GAL_FALLBACK )
49  {
51  }
52 
53  m_preview = new SCH_PREVIEW_PANEL( aParent, wxID_ANY, wxDefaultPosition, wxSize( -1, -1 ),
54  m_galDisplayOptions, canvasType );
55  m_preview->SetStealsFocus( false );
56  m_preview->ShowScrollbars( wxSHOW_SB_NEVER, wxSHOW_SB_NEVER );
57  m_preview->GetGAL()->SetAxesEnabled( false );
58 
59  // Do not display the grid: the look is not good for a small canvas area.
60  // But mainly, due to some strange bug I (JPC) was unable to fix, the grid creates
61  // strange artifacts on Windows when eeschema is run from Kicad manager (but not in
62  // stand alone...).
63  m_preview->GetGAL()->SetGridVisibility( false );
64 
65  // Early initialization of the canvas background color,
66  // before any OnPaint event is fired for the canvas using a wrong bg color
67  KIGFX::VIEW* view = m_preview->GetView();
68  auto settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
69 
70  if( auto* theme = Pgm().GetSettingsManager().GetColorSettings( app_settings->m_ColorTheme ) )
71  settings->LoadColors( theme );
72 
73  const COLOR4D& backgroundColor = settings->GetBackgroundColor();
74  const COLOR4D& foregroundColor = settings->GetCursorColor();
75 
76  m_preview->GetGAL()->SetClearColor( backgroundColor );
77 
78  m_statusPanel = new wxPanel( this );
79  m_statusPanel->SetBackgroundColour( backgroundColor.ToColour() );
80  m_status = new wxStaticText( m_statusPanel, wxID_ANY, wxEmptyString );
81  m_status->SetForegroundColour( settings->GetLayerColor( LAYER_REFERENCEPART ).ToColour() );
82  m_statusSizer = new wxBoxSizer( wxVERTICAL );
83  m_statusSizer->Add( 0, 0, 1 ); // add a spacer
84  m_statusSizer->Add( m_status, 0, wxALIGN_CENTER );
85  m_statusSizer->Add( 0, 0, 1 ); // add a spacer
86  m_statusPanel->SetSizer( m_statusSizer );
87 
88  // Give the status panel the same color scheme as the canvas so it isn't jarring when switched to
89  m_statusPanel->SetBackgroundColour( backgroundColor.ToColour() );
90  m_statusPanel->SetForegroundColour( foregroundColor.ToColour() );
91 
92  // Give the preview panel a small top border to align its top with the status panel,
93  // and give the status panel a small bottom border to align its bottom with the preview
94  // panel.
95  m_outerSizer = new wxBoxSizer( wxVERTICAL );
96  m_outerSizer->Add( m_preview, 1, wxTOP | wxEXPAND, 5 );
97  m_outerSizer->Add( m_statusPanel, 1, wxBOTTOM | wxEXPAND, 5 );
98 
99  // Hide the status panel to start
100  m_statusPanel->Hide();
101 
102  SetSizer( m_outerSizer );
103  Layout();
104 
105  Connect( wxEVT_SIZE, wxSizeEventHandler( SYMBOL_PREVIEW_WIDGET::onSize ), NULL, this );
106 }
void SetClearColor(const COLOR4D &aColor)
void onSize(wxSizeEvent &aEvent)
static constexpr GAL_TYPE GAL_FALLBACK
bool m_forceDisplayCursor
The pixel scale factor (>1 for hi-DPI scaled displays)
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:207
#define NULL
KIGFX::GAL_DISPLAY_OPTIONS m_galDisplayOptions
EDA_DRAW_PANEL_GAL * m_preview
void SetStealsFocus(bool aStealsFocus)
Set whether focus is taken on certain events (mouseover, keys, etc).
KIGFX::GAL * GetGAL() const
Return a pointer to the GAL instance used in the panel.
virtual KIGFX::VIEW * GetView() const
Return a pointer to the #VIEW instance used in the panel.
void SetAxesEnabled(bool aAxesEnabled)
Enable drawing the axes.
LIB_PART * m_previewItem
A local copy of the LIB_PART to display on the canvas.
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
void ReadConfig(COMMON_SETTINGS &aCommonConfig, WINDOW_SETTINGS &aWindowConfig, wxWindow *aWindow)
Read application and common configs.
void SetGridVisibility(bool aVisibility)
Set the visibility setting of the grid.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:98

References EDA_DRAW_PANEL_GAL::GAL_FALLBACK, EDA_DRAW_PANEL_GAL::GAL_TYPE_OPENGL, EDA_DRAW_PANEL_GAL::GetGAL(), KIGFX::VIEW::GetPainter(), KIGFX::PAINTER::GetSettings(), EDA_DRAW_PANEL_GAL::GetView(), LAYER_REFERENCEPART, KIGFX::GAL_DISPLAY_OPTIONS::m_forceDisplayCursor, m_galDisplayOptions, m_outerSizer, m_preview, m_status, m_statusPanel, m_statusSizer, NULL, onSize(), Pgm(), KIGFX::GAL_DISPLAY_OPTIONS::ReadConfig(), KIGFX::GAL::SetAxesEnabled(), KIGFX::GAL::SetClearColor(), KIGFX::GAL::SetGridVisibility(), and EDA_DRAW_PANEL_GAL::SetStealsFocus().

◆ ~SYMBOL_PREVIEW_WIDGET()

SYMBOL_PREVIEW_WIDGET::~SYMBOL_PREVIEW_WIDGET ( )
override

Definition at line 109 of file symbol_preview_widget.cpp.

110 {
111  if( m_previewItem )
113 
114  delete m_previewItem;
115 }
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition: view.cpp:352
EDA_DRAW_PANEL_GAL * m_preview
virtual KIGFX::VIEW * GetView() const
Return a pointer to the #VIEW instance used in the panel.
LIB_PART * m_previewItem
A local copy of the LIB_PART to display on the canvas.

References EDA_DRAW_PANEL_GAL::GetView(), m_preview, m_previewItem, and KIGFX::VIEW::Remove().

Member Function Documentation

◆ DisplayPart()

void SYMBOL_PREVIEW_WIDGET::DisplayPart ( LIB_PART aPart,
int  aUnit,
int  aConvert = 0 
)

Definition at line 225 of file symbol_preview_widget.cpp.

226 {
227  KIGFX::VIEW* view = m_preview->GetView();
228 
229  if( m_previewItem )
230  {
231  view->Remove( m_previewItem );
232  delete m_previewItem;
233  m_previewItem = nullptr;
234  }
235 
236  if( aPart )
237  {
238  m_previewItem = new LIB_PART( *aPart );
239 
240  // For symbols having a De Morgan body style, use the first style
241  auto settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
242  // If unit isn't specified for a multi-unit part, pick the first. (Otherwise we'll
243  // draw all of them.)
244  settings->m_ShowUnit = ( m_previewItem->IsMulti() && aUnit == 0 ) ? 1 : aUnit;
245 
246  settings->m_ShowConvert =
247  ( m_previewItem->HasConversion() && aConvert == 0 ) ? 1 : aConvert;
248 
249  view->Add( m_previewItem );
250 
251  // Get the symbole size, in internal units
252  m_itemBBox = aPart->GetUnitBoundingBox( settings->m_ShowUnit, settings->m_ShowConvert );
253 
254  // Calculate the draw scale to fit the drawing area
255  fitOnDrawArea();
256  }
257 
259  m_preview->Show();
260  m_statusPanel->Hide();
261  Layout();
262 }
void ForceRefresh()
Force a redraw.
bool HasConversion() const
Test if part has more than one body conversion type (DeMorgan).
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition: view.cpp:352
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:207
EDA_DRAW_PANEL_GAL * m_preview
Define a library symbol object.
Definition: lib_symbol.h:93
virtual KIGFX::VIEW * GetView() const
Return a pointer to the #VIEW instance used in the panel.
LIB_PART * m_previewItem
A local copy of the LIB_PART to display on the canvas.
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
const EDA_RECT GetUnitBoundingBox(int aUnit, int aConvert) const
Get the bounding box for the symbol.
Definition: lib_symbol.cpp:803
bool IsMulti() const
Definition: lib_symbol.h:497
BOX2I m_itemBBox
The bounding box of the current item.
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition: view.cpp:322
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67

References KIGFX::VIEW::Add(), fitOnDrawArea(), EDA_DRAW_PANEL_GAL::ForceRefresh(), KIGFX::VIEW::GetPainter(), KIGFX::PAINTER::GetSettings(), LIB_PART::GetUnitBoundingBox(), EDA_DRAW_PANEL_GAL::GetView(), LIB_PART::HasConversion(), LIB_PART::IsMulti(), m_itemBBox, m_preview, m_previewItem, m_statusPanel, and KIGFX::VIEW::Remove().

Referenced by DIALOG_RESCUE_EACH::displayItemsInConflict().

◆ DisplaySymbol()

void SYMBOL_PREVIEW_WIDGET::DisplaySymbol ( const LIB_ID aSymbolID,
int  aUnit,
int  aConvert = 0 
)

Set the currently displayed symbol.

Definition at line 163 of file symbol_preview_widget.cpp.

164 {
165  KIGFX::VIEW* view = m_preview->GetView();
166  auto settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
167  std::unique_ptr< LIB_PART > symbol;
168 
169  try
170  {
171  LIB_PART* tmp = m_kiway.Prj().SchSymbolLibTable()->LoadSymbol( aSymbolID );
172 
173  if( tmp )
174  symbol = tmp->Flatten();
175  }
176  catch( const IO_ERROR& ioe )
177  {
178  wxLogError( wxString::Format( _( "Error loading symbol %s from library %s.\n\n%s" ),
179  aSymbolID.GetLibItemName().wx_str(),
180  aSymbolID.GetLibNickname().wx_str(),
181  ioe.What() ) );
182  }
183 
184  if( m_previewItem )
185  {
186  view->Remove( m_previewItem );
187  delete m_previewItem;
188  m_previewItem = nullptr;
189  }
190 
191  if( symbol )
192  {
193  // This will flatten derived parts so that the correct final symbol can be shown.
194  m_previewItem = symbol.release();
195 
196  // If unit isn't specified for a multi-unit part, pick the first. (Otherwise we'll
197  // draw all of them.)
198  settings->m_ShowUnit = ( m_previewItem->IsMulti() && aUnit == 0 ) ? 1 : aUnit;
199 
200  // For symbols having a De Morgan body style, use the first style
201  settings->m_ShowConvert =
202  ( m_previewItem->HasConversion() && aConvert == 0 ) ? 1 : aConvert;
203 
204  view->Add( m_previewItem );
205 
206  // Get the symbole size, in internal units
207  m_itemBBox = m_previewItem->GetUnitBoundingBox( settings->m_ShowUnit,
208  settings->m_ShowConvert );
209 
210  if( !m_preview->IsShown() )
211  {
212  m_preview->Show();
213  m_statusPanel->Hide();
214  Layout(); // Ensure panel size is up to date.
215  }
216 
217  // Calculate the draw scale to fit the drawing area
218  fitOnDrawArea();
219  }
220 
222 }
const UTF8 & GetLibItemName() const
Definition: lib_id.h:106
void ForceRefresh()
Force a redraw.
bool HasConversion() const
Test if part has more than one body conversion type (DeMorgan).
virtual PROJECT & Prj() const
Return the PROJECT associated with this KIWAY.
Definition: kiway.cpp:173
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition: view.cpp:352
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:207
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
EDA_DRAW_PANEL_GAL * m_preview
Define a library symbol object.
Definition: lib_symbol.h:93
virtual KIGFX::VIEW * GetView() const
Return a pointer to the #VIEW instance used in the panel.
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:92
std::unique_ptr< LIB_PART > Flatten() const
Return a flattened symbol inheritance to the caller.
Definition: lib_symbol.cpp:334
LIB_PART * m_previewItem
A local copy of the LIB_PART to display on the canvas.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
#define _(s)
Definition: 3d_actions.cpp:33
const EDA_RECT GetUnitBoundingBox(int aUnit, int aConvert) const
Get the bounding box for the symbol.
Definition: lib_symbol.cpp:803
wxString wx_str() const
Definition: utf8.cpp:51
bool IsMulti() const
Definition: lib_symbol.h:497
BOX2I m_itemBBox
The bounding box of the current item.
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition: view.cpp:322
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75

References _, KIGFX::VIEW::Add(), fitOnDrawArea(), LIB_PART::Flatten(), EDA_DRAW_PANEL_GAL::ForceRefresh(), Format(), LIB_ID::GetLibItemName(), LIB_ID::GetLibNickname(), KIGFX::VIEW::GetPainter(), KIGFX::PAINTER::GetSettings(), LIB_PART::GetUnitBoundingBox(), EDA_DRAW_PANEL_GAL::GetView(), LIB_PART::HasConversion(), LIB_PART::IsMulti(), m_itemBBox, m_kiway, m_preview, m_previewItem, m_statusPanel, KIWAY::Prj(), KIGFX::VIEW::Remove(), IO_ERROR::What(), and UTF8::wx_str().

Referenced by DIALOG_CHOOSE_SYMBOL::OnComponentPreselected().

◆ fitOnDrawArea()

void SYMBOL_PREVIEW_WIDGET::fitOnDrawArea ( )
private

Definition at line 139 of file symbol_preview_widget.cpp.

140 {
141  if( !m_previewItem )
142  return;
143 
144  // set the view scale to fit the item on screen
145  KIGFX::VIEW* view = m_preview->GetView();
146 
147  // Calculate the drawing area size, in internal units, for a scaling factor = 1.0
148  view->SetScale( 1.0 );
149  VECTOR2D clientSize = view->ToWorld( m_preview->GetClientSize(), false );
150  // Calculate the draw scale to fit the drawing area
151  double scale = std::min( fabs( clientSize.x / m_itemBBox.GetWidth() ),
152  fabs( clientSize.y / m_itemBBox.GetHeight() ) );
153 
154  // Above calculation will yield an exact fit; add a bit of whitespace around symbol
155  scale /= 1.2;
156 
157  // Now fix the best scale
158  view->SetScale( scale );
159  view->SetCenter( m_itemBBox.Centre() );
160 }
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:451
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition: view.cpp:580
coord_type GetWidth() const
Definition: box2.h:197
EDA_DRAW_PANEL_GAL * m_preview
virtual KIGFX::VIEW * GetView() const
Return a pointer to the #VIEW instance used in the panel.
LIB_PART * m_previewItem
A local copy of the LIB_PART to display on the canvas.
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Set the scaling factor, zooming around a given anchor point.
Definition: view.cpp:554
const int scale
Vec Centre() const
Definition: box2.h:79
coord_type GetHeight() const
Definition: box2.h:198
BOX2I m_itemBBox
The bounding box of the current item.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67

References BOX2< Vec >::Centre(), BOX2< Vec >::GetHeight(), EDA_DRAW_PANEL_GAL::GetView(), BOX2< Vec >::GetWidth(), m_itemBBox, m_preview, m_previewItem, scale, KIGFX::VIEW::SetCenter(), KIGFX::VIEW::SetScale(), KIGFX::VIEW::ToWorld(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by DisplayPart(), DisplaySymbol(), and onSize().

◆ onSize()

void SYMBOL_PREVIEW_WIDGET::onSize ( wxSizeEvent &  aEvent)
private

Definition at line 127 of file symbol_preview_widget.cpp.

128 {
129  if( m_previewItem )
130  {
131  fitOnDrawArea();
133  }
134 
135  aEvent.Skip();
136 }
void ForceRefresh()
Force a redraw.
EDA_DRAW_PANEL_GAL * m_preview
LIB_PART * m_previewItem
A local copy of the LIB_PART to display on the canvas.

References fitOnDrawArea(), EDA_DRAW_PANEL_GAL::ForceRefresh(), m_preview, and m_previewItem.

Referenced by SYMBOL_PREVIEW_WIDGET().

◆ SetStatusText()

void SYMBOL_PREVIEW_WIDGET::SetStatusText ( wxString const &  aText)

Set the contents of the status label and display it.

Definition at line 118 of file symbol_preview_widget.cpp.

119 {
120  m_status->SetLabel( aText );
121  m_preview->Hide();
122  m_statusPanel->Show();
123  Layout();
124 }
EDA_DRAW_PANEL_GAL * m_preview

References m_preview, m_status, and m_statusPanel.

Referenced by DIALOG_CHOOSE_SYMBOL::OnComponentPreselected().

Member Data Documentation

◆ m_galDisplayOptions

KIGFX::GAL_DISPLAY_OPTIONS SYMBOL_PREVIEW_WIDGET::m_galDisplayOptions
private

Definition at line 70 of file symbol_preview_widget.h.

Referenced by SYMBOL_PREVIEW_WIDGET().

◆ m_itemBBox

BOX2I SYMBOL_PREVIEW_WIDGET::m_itemBBox
private

The bounding box of the current item.

Definition at line 84 of file symbol_preview_widget.h.

Referenced by DisplayPart(), DisplaySymbol(), and fitOnDrawArea().

◆ m_kiway

KIWAY& SYMBOL_PREVIEW_WIDGET::m_kiway
private

Definition at line 68 of file symbol_preview_widget.h.

Referenced by DisplaySymbol().

◆ m_outerSizer

wxSizer* SYMBOL_PREVIEW_WIDGET::m_outerSizer
private

Definition at line 76 of file symbol_preview_widget.h.

Referenced by SYMBOL_PREVIEW_WIDGET().

◆ m_preview

EDA_DRAW_PANEL_GAL* SYMBOL_PREVIEW_WIDGET::m_preview
private

◆ m_previewItem

LIB_PART* SYMBOL_PREVIEW_WIDGET::m_previewItem
private

A local copy of the LIB_PART to display on the canvas.

Definition at line 81 of file symbol_preview_widget.h.

Referenced by DisplayPart(), DisplaySymbol(), fitOnDrawArea(), onSize(), and ~SYMBOL_PREVIEW_WIDGET().

◆ m_status

wxStaticText* SYMBOL_PREVIEW_WIDGET::m_status
private

Definition at line 73 of file symbol_preview_widget.h.

Referenced by SetStatusText(), and SYMBOL_PREVIEW_WIDGET().

◆ m_statusPanel

wxPanel* SYMBOL_PREVIEW_WIDGET::m_statusPanel
private

◆ m_statusSizer

wxSizer* SYMBOL_PREVIEW_WIDGET::m_statusSizer
private

Definition at line 75 of file symbol_preview_widget.h.

Referenced by SYMBOL_PREVIEW_WIDGET().


The documentation for this class was generated from the following files: