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 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 stand alone...).
62  m_preview->GetGAL()->SetGridVisibility( false );
63 
64  // Early initialization of the canvas background color,
65  // before any OnPaint event is fired for the canvas using a wrong bg color
66  KIGFX::VIEW* view = m_preview->GetView();
67  auto settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
68  m_preview->GetGAL()->SetClearColor( settings->GetBackgroundColor() );
69 
70  SetBackgroundColour( *wxWHITE );
71  SetForegroundColour( *wxBLACK );
72 
73  m_status = new wxStaticText( this, wxID_ANY, wxEmptyString );
74  m_statusSizer = new wxBoxSizer( wxVERTICAL );
75  m_statusSizer->Add( 0, 0, 1 ); // add a spacer
76  m_statusSizer->Add( m_status, 0, wxALIGN_CENTER );
77  m_statusSizer->Add( 0, 0, 1 ); // add a spacer
78 
79  auto outer_sizer = new wxBoxSizer( wxVERTICAL );
80  outer_sizer->Add( m_preview, 1, wxTOP | wxEXPAND, 5 );
81  outer_sizer->Add( m_statusSizer, 1, wxALIGN_CENTER );
82 
83  m_statusSizer->ShowItems( false );
84 
85  SetSizer( outer_sizer );
86 
87  Connect( wxEVT_SIZE, wxSizeEventHandler( SYMBOL_PREVIEW_WIDGET::onSize ), NULL, this );
88 }
89 
90 
92 {
93  if( m_previewItem )
95 
96  delete m_previewItem;
97 }
98 
99 
100 void SYMBOL_PREVIEW_WIDGET::SetStatusText( wxString const& aText )
101 {
102  m_status->SetLabel( aText );
103  m_statusSizer->ShowItems( true );
104  m_preview->Hide();
105  Layout();
106 }
107 
108 
109 void SYMBOL_PREVIEW_WIDGET::onSize( wxSizeEvent& aEvent )
110 {
111  if( m_previewItem )
112  {
113  fitOnDrawArea();
115  }
116 
117  aEvent.Skip();
118 }
119 
120 
122 {
123  if( !m_previewItem )
124  return;
125 
126  // set the view scale to fit the item on screen
127  KIGFX::VIEW* view = m_preview->GetView();
128 
129  // Calculate the drawing area size, in internal units, for a scaling factor = 1.0
130  view->SetScale( 1.0 );
131  VECTOR2D clientSize = view->ToWorld( m_preview->GetClientSize(), false );
132  // Calculate the draw scale to fit the drawing area
133  double scale = std::min( fabs( clientSize.x / m_itemBBox.GetWidth() ),
134  fabs( clientSize.y / m_itemBBox.GetHeight() ) );
135 
136  // Above calculation will yield an exact fit; add a bit of whitespace around symbol
137  scale /= 1.2;
138 
139  // Now fix the best scale
140  view->SetScale( scale );
141  view->SetCenter( m_itemBBox.Centre() );
142 }
143 
144 
145 void SYMBOL_PREVIEW_WIDGET::DisplaySymbol( const LIB_ID& aSymbolID, int aUnit )
146 {
147  KIGFX::VIEW* view = m_preview->GetView();
148  auto settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
149  std::unique_ptr< LIB_PART > symbol;
150 
151  try
152  {
153  LIB_PART* tmp = m_kiway.Prj().SchSymbolLibTable()->LoadSymbol( aSymbolID );
154 
155  if( tmp )
156  symbol = tmp->Flatten();
157  }
158  catch( const IO_ERROR& ioe )
159  {
160  wxLogError( wxString::Format( _( "Error loading symbol %s from library %s.\n\n%s" ),
161  aSymbolID.GetLibItemName().wx_str(),
162  aSymbolID.GetLibNickname().wx_str(),
163  ioe.What() ) );
164  }
165 
166  if( m_previewItem )
167  {
168  view->Remove( m_previewItem );
169  delete m_previewItem;
170  m_previewItem = nullptr;
171  }
172 
173  if( symbol )
174  {
175  // This will flatten derived parts so that the correct final symbol can be shown.
176  m_previewItem = symbol.release();
177 
178  // If unit isn't specified for a multi-unit part, pick the first. (Otherwise we'll
179  // draw all of them.)
180  if( m_previewItem->IsMulti() && aUnit == 0 )
181  aUnit = 1;
182 
183  settings->m_ShowUnit = aUnit;
184 
185  // For symbols having a De Morgan body style, use the first style
186  settings->m_ShowConvert = m_previewItem->HasConversion() ? 1 : 0;
187 
188  view->Add( m_previewItem );
189 
190  // Get the symbole size, in internal units
192 
193  if( !m_preview->IsShown() )
194  {
195  m_preview->Show();
196  m_statusSizer->ShowItems( false );
197  Layout(); // Ensure panel size is up to date.
198  }
199 
200  // Calculate the draw scale to fit the drawing area
201  fitOnDrawArea();
202  }
203 
205 }
206 
207 
209 {
210  KIGFX::VIEW* view = m_preview->GetView();
211 
212  if( m_previewItem )
213  {
214  view->Remove( m_previewItem );
215  delete m_previewItem;
216  m_previewItem = nullptr;
217  }
218 
219  if( aPart )
220  {
221  m_previewItem = new LIB_PART( *aPart );
222 
223  // If unit isn't specified for a multi-unit part, pick the first. (Otherwise we'll
224  // draw all of them.)
225  if( m_previewItem->IsMulti() && aUnit == 0 )
226  aUnit = 1;
227 
228  // For symbols having a De Morgan body style, use the first style
229  auto settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
230  settings->m_ShowConvert = m_previewItem->HasConversion() ? 1 : 0;
231  view->Add( m_previewItem );
232 
233  // Get the symbole size, in internal units
234  m_itemBBox = aPart->GetUnitBoundingBox( aUnit, 0 );
235 
236  // Calculate the draw scale to fit the drawing area
237  fitOnDrawArea();
238  }
239 
241  m_preview->Show();
242  m_statusSizer->ShowItems( false );
243 }
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:103
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
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:171
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 DisplaySymbol(const LIB_ID &aSymbolID, int aUnit)
Set the currently displayed symbol.
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.
void DisplayPart(LIB_PART *aPart, int aUnit)
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 GetSettings Returns pointer to current settings that are going to be used when drawing items...
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