KiCad PCB EDA Suite
symbol_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-2020 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 "symbol_preview_widget.h"
21 #include <wx/stattext.h>
22 #include <wx/sizer.h>
23 #include <kiway.h>
24 #include <sch_view.h>
26 #include <class_libentry.h>
27 #include <symbol_lib_table.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 <eeschema_settings.h>
34 
35 
37  EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType ) :
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 }
94 
95 
97 {
98  if( m_previewItem )
100 
101  delete m_previewItem;
102 }
103 
104 
105 void SYMBOL_PREVIEW_WIDGET::SetStatusText( wxString const& aText )
106 {
107  m_status->SetLabel( aText );
108  m_statusSizer->ShowItems( true );
109  m_preview->Hide();
110  Layout();
111 }
112 
113 
114 void SYMBOL_PREVIEW_WIDGET::onSize( wxSizeEvent& aEvent )
115 {
116  if( m_previewItem )
117  {
118  fitOnDrawArea();
120  }
121 
122  aEvent.Skip();
123 }
124 
125 
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 }
148 
149 
150 void SYMBOL_PREVIEW_WIDGET::DisplaySymbol( const LIB_ID& aSymbolID, int aUnit, int aConvert )
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 }
210 
211 
212 void SYMBOL_PREVIEW_WIDGET::DisplayPart( LIB_PART* aPart, int aUnit, int aConvert )
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 }
SYMBOL_PREVIEW_WIDGET(wxWindow *aParent, KIWAY &aKiway, EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType)
Construct a symbol preview widget.
void SetClearColor(const COLOR4D &aColor)
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
void ForceRefresh()
Function ForceRefresh() Forces a redraw.
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
void onSize(wxSizeEvent &aEvent)
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 DisplayPart(LIB_PART *aPart, int aUnit, int aConvert=0)
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:376
bool m_forceDisplayCursor
Force cursor display
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
void SetStatusText(wxString const &aText)
Set the contents of the status label and display it.
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
void SetCenter(const VECTOR2D &aCenter)
Function SetCenter() Sets the center point of the VIEW (i.e.
Definition: view.cpp:604
#define NULL
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
KIGFX::GAL_DISPLAY_OPTIONS m_galDisplayOptions
Class LIB_PIN definition.
coord_type GetWidth() const
Definition: box2.h:196
EDA_DRAW_PANEL_GAL * m_preview
void SetStealsFocus(bool aStealsFocus)
Set whether focus is taken on certain events (mouseover, keys, etc).
Define a library symbol object.
KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the s...
Definition: kiway.h:273
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.
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 void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Function SetScale() Sets the scaling factor, zooming around a given anchor point.
Definition: view.cpp:578
virtual RENDER_SETTINGS * GetSettings()=0
Function GetAdapter Returns pointer to current settings that are going to be used when drawing items.
void DisplaySymbol(const LIB_ID &aSymbolID, int aUnit, int aConvert=0)
Set the currently displayed symbol.
const int scale
see class PGM_BASE
Vec Centre() const
Definition: box2.h:78
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
void ReadConfig(COMMON_SETTINGS &aCommonConfig, WINDOW_SETTINGS &aWindowConfig, wxWindow *aWindow)
Read application and common configs.
coord_type GetHeight() const
Definition: box2.h:197
bool IsMulti() const
void SetGridVisibility(bool aVisibility)
Sets the visibility setting of the grid.
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
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