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)
 Set the currently displayed symbol. More...
 
void DisplayPart (LIB_PART *aPart, int aUnit)
 

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
 
EDA_ITEMm_previewItem
 a local copy of the LIB_ALIAS or 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 37 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 34 of file symbol_preview_widget.cpp.

35  :
36  wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0 ),
37  m_kiway( aKiway ),
38  m_preview( nullptr ), m_status( nullptr ), m_statusSizer( nullptr ), m_previewItem( nullptr )
39 {
40  wxString eeschemaFrameKey( SCH_EDIT_FRAME_NAME );
41 
42  std::unique_ptr<wxConfigBase> eeschemaConfig = GetNewConfig( Pgm().App().GetAppName() );
43  wxConfigBase& commonConfig = *Pgm().CommonSettings();
44 
45  m_galDisplayOptions.ReadConfig( commonConfig, *eeschemaConfig, eeschemaFrameKey, this );
46 
47  EDA_DRAW_PANEL_GAL::GAL_TYPE canvasType = aCanvasType;
48 
49  // Allows only a CAIRO or OPENGL canvas:
50  if( canvasType != EDA_DRAW_PANEL_GAL::GAL_TYPE_OPENGL &&
53 
54  m_preview = new SCH_PREVIEW_PANEL( aParent, wxID_ANY, wxDefaultPosition, wxSize( -1, -1 ),
55  m_galDisplayOptions, canvasType );
56  m_preview->SetStealsFocus( false );
57 
58  // Do not display the grid: the look is not good for a small canvas area.
59  // But mainly, due to some strange bug I (JPC) was unable to fix, the grid creates
60  // strange artifacts on Windows when eeschema is run from Kicad manager (but not in stand alone...).
61  m_preview->GetGAL()->SetGridVisibility( false );
62 
63  // Early initialization of the canvas background color,
64  // before any OnPaint event is fired for the canvas using a wrong bg color
65  KIGFX::VIEW* view = m_preview->GetView();
66  auto settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
67  m_preview->GetGAL()->SetClearColor( settings->GetBackgroundColor() );
68 
69  SetBackgroundColour( *wxWHITE );
70  SetForegroundColour( *wxBLACK );
71 
72  m_status = new wxStaticText( this, wxID_ANY, wxEmptyString );
73  m_statusSizer = new wxBoxSizer( wxVERTICAL );
74  m_statusSizer->Add( 0, 0, 1 ); // add a spacer
75  m_statusSizer->Add( m_status, 0, wxALIGN_CENTER );
76  m_statusSizer->Add( 0, 0, 1 ); // add a spacer
77 
78  auto outer_sizer = new wxBoxSizer( wxVERTICAL );
79  outer_sizer->Add( m_preview, 1, wxTOP | wxEXPAND, 5 );
80  outer_sizer->Add( m_statusSizer, 1, wxALIGN_CENTER );
81 
82  m_statusSizer->ShowItems( false );
83 
84  SetSizer( outer_sizer );
85 
86  Connect( wxEVT_SIZE, wxSizeEventHandler( SYMBOL_PREVIEW_WIDGET::onSize ), NULL, this );
87 }
void SetClearColor(const COLOR4D &aColor)
void onSize(wxSizeEvent &aEvent)
std::unique_ptr< wxConfigBase > GetNewConfig(const wxString &aProgName)
Create a new wxConfig so we can put configuration files in a more proper place for each platform.
Definition: common.cpp:256
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:66
void ReadConfig(wxConfigBase &aCommonConfig, wxConfigBase &aAppCondfig, const wxString &aBaseCfgName, wxWindow *aWindow)
Read application and common configs.
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
#define SCH_EDIT_FRAME_NAME
Definition: draw_frame.h:51
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.
EDA_ITEM * m_previewItem
a local copy of the LIB_ALIAS or the LIB_PART to display on the canvas
VTBL_ENTRY wxConfigBase * CommonSettings() const
Definition: pgm_base.h:190
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
void SetGridVisibility(bool aVisibility)
Sets the visibility setting of the grid.
Class VIEW.
Definition: view.h:61

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

91 {
92  if( m_previewItem )
94 
95  delete m_previewItem;
96 }
virtual void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:376
EDA_DRAW_PANEL_GAL * m_preview
EDA_ITEM * m_previewItem
a local copy of the LIB_ALIAS or the LIB_PART to display on the canvas
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.

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 
)

Definition at line 204 of file symbol_preview_widget.cpp.

205 {
206  KIGFX::VIEW* view = m_preview->GetView();
207 
208  if( m_previewItem )
209  {
210  view->Remove( m_previewItem );
211  delete m_previewItem;
212  m_previewItem = nullptr;
213  }
214 
215  if( aPart )
216  {
217  // If unit isn't specified for a multi-unit part, pick the first. (Otherwise we'll
218  // draw all of them.)
219  if( aPart->IsMulti() && aUnit == 0 )
220  aUnit = 1;
221 
222  // For symbols having a De Morgan body style, use the first style
223  auto settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
224  settings->m_ShowConvert = aPart->HasConversion() ? 1 : 0;
225  m_previewItem = new LIB_PART( *aPart );
226  view->Add( m_previewItem );
227 
228  // Get the symbole size, in internal units
229  m_itemBBox = aPart->GetUnitBoundingBox( aUnit, 0 );
230 
231  // Calculate the draw scale to fit the drawing area
232  fitOnDrawArea();
233  }
234 
236 
237  m_preview->Show();
238  m_statusSizer->ShowItems( false );
239 }
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:376
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.
EDA_ITEM * m_previewItem
a local copy of the LIB_ALIAS or the LIB_PART to display on the canvas
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
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:346
Class 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 
)

Set the currently displayed symbol.

Definition at line 144 of file symbol_preview_widget.cpp.

145 {
146  KIGFX::VIEW* view = m_preview->GetView();
147  auto settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
148  LIB_ALIAS* alias = nullptr;
149 
150  try
151  {
152  alias = m_kiway.Prj().SchSymbolLibTable()->LoadSymbol( aSymbolID );
153  }
154  catch( const IO_ERROR& ioe )
155  {
156  wxLogError( wxString::Format( _( "Error loading symbol %s from library %s.\n\n%s" ),
157  aSymbolID.GetLibItemName().wx_str(),
158  aSymbolID.GetLibNickname().wx_str(),
159  ioe.What() ) );
160  }
161 
162  if( m_previewItem )
163  {
164  view->Remove( m_previewItem );
165  delete m_previewItem;
166  m_previewItem = nullptr;
167  }
168 
169  if( alias )
170  {
171  LIB_PART* part = alias->GetPart();
172 
173  // If unit isn't specified for a multi-unit part, pick the first. (Otherwise we'll
174  // draw all of them.)
175  if( part->IsMulti() && aUnit == 0 )
176  aUnit = 1;
177 
178  settings->m_ShowUnit = aUnit;
179 
180  // For symbols having a De Morgan body style, use the first style
181  settings->m_ShowConvert = part->HasConversion() ? 1 : 0;
182 
183  m_previewItem = new LIB_ALIAS( *alias, part );
184  view->Add( m_previewItem );
185 
186  // Get the symbole size, in internal units
187  m_itemBBox = part->GetUnitBoundingBox( aUnit, 0 );
188 
189  if( !m_preview->IsShown() )
190  {
191  m_preview->Show();
192  m_statusSizer->ShowItems( false );
193  Layout(); // Ensure panel size is up to date.
194  }
195 
196  // Calculate the draw scale to fit the drawing area
197  fitOnDrawArea();
198  }
199 
201 }
Part library alias object definition.
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:163
virtual void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:376
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:33
EDA_DRAW_PANEL_GAL * m_preview
Define a library symbol object.
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
EDA_ITEM * m_previewItem
a local copy of the LIB_ALIAS or the LIB_PART to display on the canvas
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings 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
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
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:346
Class 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(), 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 120 of file symbol_preview_widget.cpp.

121 {
122  if( !m_previewItem )
123  return;
124 
125  // set the view scale to fit the item on screen
126  KIGFX::VIEW* view = m_preview->GetView();
127 
128  // Calculate the drawing area size, in internal units, for a scaling factor = 1.0
129  view->SetScale( 1.0 );
130  VECTOR2D clientSize = view->ToWorld( m_preview->GetClientSize(), false );
131  // Calculate the draw scale to fit the drawing area
132  double scale = std::min( fabs( clientSize.x / m_itemBBox.GetWidth() ),
133  fabs( clientSize.y / m_itemBBox.GetHeight() ) );
134 
135  // Above calculation will yield an exact fit; add a bit of whitespace around symbol
136  scale /= 1.2;
137 
138  // Now fix the best scale
139  view->SetScale( scale );
140  view->SetCenter( m_itemBBox.Centre() );
141 }
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:475
void SetScale(double aScale)
Function SetScale() Sets the scaling factor.
Definition: view.h:250
void SetCenter(const VECTOR2D &aCenter)
Function SetCenter() Sets the center point of the VIEW (i.e.
Definition: view.cpp:601
coord_type GetWidth() const
Definition: box2.h:195
EDA_DRAW_PANEL_GAL * m_preview
EDA_ITEM * m_previewItem
a local copy of the LIB_ALIAS or the LIB_PART to display on the canvas
const int scale
Vec Centre() const
Definition: box2.h:77
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
coord_type GetHeight() const
Definition: box2.h:196
BOX2I m_itemBBox
The bounding box of the current item.
Class VIEW.
Definition: view.h:61
#define min(a, b)
Definition: auxiliary.h:85

References BOX2< Vec >::Centre(), BOX2< Vec >::GetHeight(), EDA_DRAW_PANEL_GAL::GetView(), BOX2< Vec >::GetWidth(), m_itemBBox, m_preview, m_previewItem, min, 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 108 of file symbol_preview_widget.cpp.

109 {
110  if( m_previewItem )
111  {
112  fitOnDrawArea();
114  }
115 
116  aEvent.Skip();
117 }
void ForceRefresh()
Function ForceRefresh() Forces a redraw.
EDA_DRAW_PANEL_GAL * m_preview
EDA_ITEM * m_previewItem
a local copy of the LIB_ALIAS or 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 99 of file symbol_preview_widget.cpp.

100 {
101  m_status->SetLabel( aText );
102  m_statusSizer->ShowItems( true );
103  m_preview->Hide();
104  Layout();
105 }
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 72 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 83 of file symbol_preview_widget.h.

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

◆ m_kiway

KIWAY& SYMBOL_PREVIEW_WIDGET::m_kiway
private

Definition at line 70 of file symbol_preview_widget.h.

Referenced by DisplaySymbol().

◆ m_preview

EDA_DRAW_PANEL_GAL* SYMBOL_PREVIEW_WIDGET::m_preview
private

◆ m_previewItem

EDA_ITEM* SYMBOL_PREVIEW_WIDGET::m_previewItem
private

a local copy of the LIB_ALIAS or the LIB_PART to display on the canvas

Definition at line 80 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 75 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: