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
 
wxSizer * m_statusSizer
 
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 36 of file symbol_preview_widget.cpp.

37  :
38  wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0 ),
39  m_kiway( aKiway ),
40  m_preview( nullptr ), m_status( nullptr ), m_statusSizer( nullptr ), m_previewItem( nullptr )
41 {
42  auto common_settings = Pgm().GetCommonSettings();
43  auto app_settings = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
44 
45  m_galDisplayOptions.ReadConfig( *common_settings, app_settings->m_Window, this );
47 
48  EDA_DRAW_PANEL_GAL::GAL_TYPE canvasType = aCanvasType;
49 
50  // Allows only a CAIRO or OPENGL canvas:
51  if( canvasType != EDA_DRAW_PANEL_GAL::GAL_TYPE_OPENGL &&
54 
55  m_preview = new SCH_PREVIEW_PANEL( aParent, wxID_ANY, wxDefaultPosition, wxSize( -1, -1 ),
56  m_galDisplayOptions, canvasType );
57  m_preview->SetStealsFocus( 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  m_preview->GetGAL()->SetClearColor( settings->GetBackgroundColor() );
74 
75  SetBackgroundColour( *wxWHITE );
76  SetForegroundColour( *wxBLACK );
77 
78  m_status = new wxStaticText( this, wxID_ANY, wxEmptyString );
79  m_statusSizer = new wxBoxSizer( wxVERTICAL );
80  m_statusSizer->Add( 0, 0, 1 ); // add a spacer
81  m_statusSizer->Add( m_status, 0, wxALIGN_CENTER );
82  m_statusSizer->Add( 0, 0, 1 ); // add a spacer
83 
84  auto outer_sizer = new wxBoxSizer( wxVERTICAL );
85  outer_sizer->Add( m_preview, 1, wxTOP | wxEXPAND, 5 );
86  outer_sizer->Add( m_statusSizer, 1, wxALIGN_CENTER );
87 
88  m_statusSizer->ShowItems( false );
89 
90  SetSizer( outer_sizer );
91 
92  Connect( wxEVT_SIZE, wxSizeEventHandler( SYMBOL_PREVIEW_WIDGET::onSize ), NULL, this );
93 }
void SetClearColor(const COLOR4D &aColor)
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
void onSize(wxSizeEvent &aEvent)
bool m_forceDisplayCursor
Force cursor display
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
#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
Function GetGAL() Returns a pointer to the GAL instance used in the panel.
virtual KIGFX::VIEW * GetView() const
Function GetView() Returns 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
Function GetAdapter Returns 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)
Sets the visibility setting of the grid.
VIEW.
Definition: view.h:61

References EDA_DRAW_PANEL_GAL::GAL_TYPE_CAIRO, EDA_DRAW_PANEL_GAL::GAL_TYPE_OPENGL, EDA_DRAW_PANEL_GAL::GetGAL(), KIGFX::VIEW::GetPainter(), KIGFX::PAINTER::GetSettings(), EDA_DRAW_PANEL_GAL::GetView(), KIGFX::GAL_DISPLAY_OPTIONS::m_forceDisplayCursor, m_galDisplayOptions, m_preview, m_status, m_statusSizer, NULL, onSize(), Pgm(), KIGFX::GAL_DISPLAY_OPTIONS::ReadConfig(), 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 96 of file symbol_preview_widget.cpp.

97 {
98  if( m_previewItem )
100 
101  delete m_previewItem;
102 }
virtual void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:380
EDA_DRAW_PANEL_GAL * m_preview
virtual KIGFX::VIEW * GetView() const
Function GetView() Returns 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 212 of file symbol_preview_widget.cpp.

213 {
214  KIGFX::VIEW* view = m_preview->GetView();
215 
216  if( m_previewItem )
217  {
218  view->Remove( m_previewItem );
219  delete m_previewItem;
220  m_previewItem = nullptr;
221  }
222 
223  if( aPart )
224  {
225  m_previewItem = new LIB_PART( *aPart );
226 
227  // For symbols having a De Morgan body style, use the first style
228  auto settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
229  // If unit isn't specified for a multi-unit part, pick the first. (Otherwise we'll
230  // draw all of them.)
231  settings->m_ShowUnit = ( m_previewItem->IsMulti() && aUnit == 0 ) ? 1 : aUnit;
232 
233  settings->m_ShowConvert =
234  ( m_previewItem->HasConversion() && aConvert == 0 ) ? 1 : aConvert;
235 
236  view->Add( m_previewItem );
237 
238  // Get the symbole size, in internal units
239  m_itemBBox = aPart->GetUnitBoundingBox( settings->m_ShowUnit, settings->m_ShowConvert );
240 
241  // Calculate the draw scale to fit the drawing area
242  fitOnDrawArea();
243  }
244 
246  m_preview->Show();
247  m_statusSizer->ShowItems( false );
248 }
void ForceRefresh()
Function ForceRefresh() Forces a redraw.
bool HasConversion() const
Test if part has more than one body conversion type (DeMorgan).
virtual void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:380
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
EDA_DRAW_PANEL_GAL * m_preview
Define a library symbol object.
virtual KIGFX::VIEW * GetView() const
Function GetView() Returns 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
Function GetAdapter Returns 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.
bool IsMulti() const
BOX2I m_itemBBox
The bounding box of the current item.
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:350
VIEW.
Definition: view.h:61

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_statusSizer, 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 150 of file symbol_preview_widget.cpp.

151 {
152  KIGFX::VIEW* view = m_preview->GetView();
153  auto settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
154  std::unique_ptr< LIB_PART > symbol;
155 
156  try
157  {
158  LIB_PART* tmp = m_kiway.Prj().SchSymbolLibTable()->LoadSymbol( aSymbolID );
159 
160  if( tmp )
161  symbol = tmp->Flatten();
162  }
163  catch( const IO_ERROR& ioe )
164  {
165  wxLogError( wxString::Format( _( "Error loading symbol %s from library %s.\n\n%s" ),
166  aSymbolID.GetLibItemName().wx_str(),
167  aSymbolID.GetLibNickname().wx_str(),
168  ioe.What() ) );
169  }
170 
171  if( m_previewItem )
172  {
173  view->Remove( m_previewItem );
174  delete m_previewItem;
175  m_previewItem = nullptr;
176  }
177 
178  if( symbol )
179  {
180  // This will flatten derived parts so that the correct final symbol can be shown.
181  m_previewItem = symbol.release();
182 
183  // If unit isn't specified for a multi-unit part, pick the first. (Otherwise we'll
184  // draw all of them.)
185  settings->m_ShowUnit = ( m_previewItem->IsMulti() && aUnit == 0 ) ? 1 : aUnit;
186 
187  // For symbols having a De Morgan body style, use the first style
188  settings->m_ShowConvert =
189  ( m_previewItem->HasConversion() && aConvert == 0 ) ? 1 : aConvert;
190 
191  view->Add( m_previewItem );
192 
193  // Get the symbole size, in internal units
194  m_itemBBox = m_previewItem->GetUnitBoundingBox( settings->m_ShowUnit,
195  settings->m_ShowConvert );
196 
197  if( !m_preview->IsShown() )
198  {
199  m_preview->Show();
200  m_statusSizer->ShowItems( false );
201  Layout(); // Ensure panel size is up to date.
202  }
203 
204  // Calculate the draw scale to fit the drawing area
205  fitOnDrawArea();
206  }
207 
209 }
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
void ForceRefresh()
Function ForceRefresh() Forces a redraw.
bool HasConversion() const
Test if part has more than one body conversion type (DeMorgan).
VTBL_ENTRY PROJECT & Prj() const
Function Prj returns the PROJECT associated with this KIWAY.
Definition: kiway.cpp:172
virtual void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:380
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
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.
virtual KIGFX::VIEW * GetView() const
Function GetView() Returns 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:97
std::unique_ptr< LIB_PART > Flatten() const
Return a flattened symbol inheritance to the caller.
LIB_PART * m_previewItem
A local copy of the LIB_PART to display on the canvas.
virtual RENDER_SETTINGS * GetSettings()=0
Function GetAdapter Returns pointer to current settings that are going to be used when drawing items.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
#define _(s)
Definition: 3d_actions.cpp:33
const EDA_RECT GetUnitBoundingBox(int aUnit, int aConvert) const
Get the bounding box for the symbol.
wxString wx_str() const
Definition: utf8.cpp:51
bool IsMulti() const
BOX2I m_itemBBox
The bounding box of the current item.
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:350
VIEW.
Definition: view.h:61
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76

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_statusSizer, KIWAY::Prj(), KIGFX::VIEW::Remove(), IO_ERROR::What(), and UTF8::wx_str().

Referenced by DIALOG_CHOOSE_COMPONENT::OnComponentPreselected().

◆ fitOnDrawArea()

void SYMBOL_PREVIEW_WIDGET::fitOnDrawArea ( )
private

Definition at line 126 of file symbol_preview_widget.cpp.

127 {
128  if( !m_previewItem )
129  return;
130 
131  // set the view scale to fit the item on screen
132  KIGFX::VIEW* view = m_preview->GetView();
133 
134  // Calculate the drawing area size, in internal units, for a scaling factor = 1.0
135  view->SetScale( 1.0 );
136  VECTOR2D clientSize = view->ToWorld( m_preview->GetClientSize(), false );
137  // Calculate the draw scale to fit the drawing area
138  double scale = std::min( fabs( clientSize.x / m_itemBBox.GetWidth() ),
139  fabs( clientSize.y / m_itemBBox.GetHeight() ) );
140 
141  // Above calculation will yield an exact fit; add a bit of whitespace around symbol
142  scale /= 1.2;
143 
144  // Now fix the best scale
145  view->SetScale( scale );
146  view->SetCenter( m_itemBBox.Centre() );
147 }
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Function ToWorld() Converts a screen space point/vector to a point/vector in world space coordinates.
Definition: view.cpp:479
void SetCenter(const VECTOR2D &aCenter)
Function SetCenter() Sets the center point of the VIEW (i.e.
Definition: view.cpp:608
coord_type GetWidth() const
Definition: box2.h:197
EDA_DRAW_PANEL_GAL * m_preview
virtual KIGFX::VIEW * GetView() const
Function GetView() Returns 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 })
Function SetScale() Sets the scaling factor, zooming around a given anchor point.
Definition: view.cpp:582
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.
VIEW.
Definition: view.h:61

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 114 of file symbol_preview_widget.cpp.

115 {
116  if( m_previewItem )
117  {
118  fitOnDrawArea();
120  }
121 
122  aEvent.Skip();
123 }
void ForceRefresh()
Function ForceRefresh() Forces 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 105 of file symbol_preview_widget.cpp.

106 {
107  m_status->SetLabel( aText );
108  m_statusSizer->ShowItems( true );
109  m_preview->Hide();
110  Layout();
111 }
EDA_DRAW_PANEL_GAL * m_preview

References m_preview, m_status, and m_statusSizer.

Referenced by DIALOG_CHOOSE_COMPONENT::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 82 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_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 79 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_statusSizer

wxSizer* SYMBOL_PREVIEW_WIDGET::m_statusSizer
private

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