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 <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  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 }
202 
203 
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 }
SYMBOL_PREVIEW_WIDGET(wxWindow *aParent, KIWAY &aKiway, EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType)
Construct a symbol preview widget.
void SetClearColor(const COLOR4D &aColor)
Part library alias object definition.
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:163
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 SetScale(double aScale)
Function SetScale() Sets the scaling factor.
Definition: view.h:250
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
#define SCH_EDIT_FRAME_NAME
Definition: draw_frame.h:51
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:601
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
KIGFX::GAL_DISPLAY_OPTIONS m_galDisplayOptions
Class LIB_ITEM 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:258
KIGFX::GAL * GetGAL() const
Function GetGAL() Returns a pointer to the GAL instance used in the panel.
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
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...
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
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
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