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 
33 
35  EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType ) :
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 }
88 
89 
91 {
92  if( m_previewItem )
94 
95  delete m_previewItem;
96 }
97 
98 
99 void SYMBOL_PREVIEW_WIDGET::SetStatusText( wxString const& aText )
100 {
101  m_status->SetLabel( aText );
102  m_statusSizer->ShowItems( true );
103  m_preview->Hide();
104  Layout();
105 }
106 
107 
108 void SYMBOL_PREVIEW_WIDGET::onSize( wxSizeEvent& aEvent )
109 {
110  if( m_previewItem )
111  {
112  fitOnDrawArea();
114  }
115 
116  aEvent.Skip();
117 }
118 
119 
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 }
142 
143 
144 void SYMBOL_PREVIEW_WIDGET::DisplaySymbol( const LIB_ID& aSymbolID, int aUnit )
145 {
146  KIGFX::VIEW* view = m_preview->GetView();
147  auto settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
148  std::unique_ptr< LIB_PART > symbol;
149 
150  try
151  {
152  LIB_PART* tmp = m_kiway.Prj().SchSymbolLibTable()->LoadSymbol( aSymbolID );
153 
154  if( tmp )
155  symbol = tmp->Flatten();
156  }
157  catch( const IO_ERROR& ioe )
158  {
159  wxLogError( wxString::Format( _( "Error loading symbol %s from library %s.\n\n%s" ),
160  aSymbolID.GetLibItemName().wx_str(),
161  aSymbolID.GetLibNickname().wx_str(),
162  ioe.What() ) );
163  }
164 
165  if( m_previewItem )
166  {
167  view->Remove( m_previewItem );
168  delete m_previewItem;
169  m_previewItem = nullptr;
170  }
171 
172  if( symbol )
173  {
174  // This will flatten derived parts so that the correct final symbol can be shown.
175  m_previewItem = symbol.release();
176 
177  // If unit isn't specified for a multi-unit part, pick the first. (Otherwise we'll
178  // draw all of them.)
179  if( m_previewItem->IsMulti() && aUnit == 0 )
180  aUnit = 1;
181 
182  settings->m_ShowUnit = aUnit;
183 
184  // For symbols having a De Morgan body style, use the first style
185  settings->m_ShowConvert = m_previewItem->HasConversion() ? 1 : 0;
186 
187  view->Add( m_previewItem );
188 
189  // Get the symbole size, in internal units
191 
192  if( !m_preview->IsShown() )
193  {
194  m_preview->Show();
195  m_statusSizer->ShowItems( false );
196  Layout(); // Ensure panel size is up to date.
197  }
198 
199  // Calculate the draw scale to fit the drawing area
200  fitOnDrawArea();
201  }
202 
204 }
205 
206 
208 {
209  KIGFX::VIEW* view = m_preview->GetView();
210 
211  if( m_previewItem )
212  {
213  view->Remove( m_previewItem );
214  delete m_previewItem;
215  m_previewItem = nullptr;
216  }
217 
218  if( aPart )
219  {
220  m_previewItem = new LIB_PART( *aPart );
221 
222  // If unit isn't specified for a multi-unit part, pick the first. (Otherwise we'll
223  // draw all of them.)
224  if( m_previewItem->IsMulti() && aUnit == 0 )
225  aUnit = 1;
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  settings->m_ShowConvert = m_previewItem->HasConversion() ? 1 : 0;
230  view->Add( m_previewItem );
231 
232  // Get the symbole size, in internal units
233  m_itemBBox = aPart->GetUnitBoundingBox( aUnit, 0 );
234 
235  // Calculate the draw scale to fit the drawing area
236  fitOnDrawArea();
237  }
238 
240  m_preview->Show();
241  m_statusSizer->ShowItems( false );
242 }
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.
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
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
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:66
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.
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
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 SCH_EDIT_FRAME_NAME
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:195
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.
Class KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within...
Definition: kiway.h:274
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.
#define _(s)
VTBL_ENTRY wxConfigBase * CommonSettings() const
Definition: pgm_base.h:194
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:77
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
const EDA_RECT GetUnitBoundingBox(int aUnit, int aConvert) const
Get the bounding box for the symbol.
wxString wx_str() const
Definition: utf8.cpp:51
coord_type GetHeight() const
Definition: box2.h:196
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
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
#define min(a, b)
Definition: auxiliary.h:85