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  auto eeschemaConfig = GetNewConfig( Pgm().App().GetAppName() );
42  m_galDisplayOptions.ReadConfig( eeschemaConfig.get(), eeschemaFrameKey + GAL_DISPLAY_OPTIONS_KEY );
43 
44  EDA_DRAW_PANEL_GAL::GAL_TYPE canvasType = aCanvasType;
45 
46  // Allows only a CAIRO or OPENGL canvas:
47  if( canvasType != EDA_DRAW_PANEL_GAL::GAL_TYPE_OPENGL &&
50 
51  m_preview = new SCH_PREVIEW_PANEL( aParent, wxID_ANY, wxDefaultPosition, wxSize( -1, -1 ),
52  m_galDisplayOptions, canvasType );
53  m_preview->SetStealsFocus( false );
54 
55  // Do not display the grid: the look is not good for a small canvas area.
56  // But mainly, due to some strange bug I (JPC) was unable to fix, the grid creates
57  // strange artifacts on Windows when eeschema is run from Kicad manager (but not in stand alone...).
58  m_preview->GetGAL()->SetGridVisibility( false );
59 
60  // Early initialization of the canvas background color,
61  // before any OnPaint event is fired for the canvas using a wrong bg color
62  KIGFX::VIEW* view = m_preview->GetView();
63  auto settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
64  m_preview->GetGAL()->SetClearColor( settings->GetBackgroundColor() );
65 
66  SetBackgroundColour( *wxWHITE );
67  SetForegroundColour( *wxBLACK );
68 
69  m_status = new wxStaticText( this, wxID_ANY, wxEmptyString );
70  m_statusSizer = new wxBoxSizer( wxVERTICAL );
71  m_statusSizer->Add( 0, 0, 1 ); // add a spacer
72  m_statusSizer->Add( m_status, 0, wxALIGN_CENTER );
73  m_statusSizer->Add( 0, 0, 1 ); // add a spacer
74 
75  auto outer_sizer = new wxBoxSizer( wxVERTICAL );
76  outer_sizer->Add( m_preview, 1, wxTOP | wxEXPAND, 5 );
77  outer_sizer->Add( m_statusSizer, 1, wxALIGN_CENTER );
78 
79  m_statusSizer->ShowItems( false );
80 
81  SetSizer( outer_sizer );
82 
83  Connect( wxEVT_SIZE, wxSizeEventHandler( SYMBOL_PREVIEW_WIDGET::onSize ), NULL, this );
84 }
85 
86 
88 {
89  if( m_previewItem )
91 
92  delete m_previewItem;
93 }
94 
95 
96 void SYMBOL_PREVIEW_WIDGET::SetStatusText( wxString const& aText )
97 {
98  m_status->SetLabel( aText );
99  m_statusSizer->ShowItems( true );
100  m_preview->Hide();
101  Layout();
102 }
103 
104 
105 void SYMBOL_PREVIEW_WIDGET::onSize( wxSizeEvent& aEvent )
106 {
107  if( m_previewItem )
108  {
109  fitOnDrawArea();
111  }
112 
113  aEvent.Skip();
114 }
115 
116 
118 {
119  if( !m_previewItem )
120  return;
121 
122  // set the view scale to fit the item on screen
123  KIGFX::VIEW* view = m_preview->GetView();
124 
125  // Calculate the drawing area size, in internal units, for a scaling factor = 1.0
126  view->SetScale( 1.0 );
127  VECTOR2D clientSize = view->ToWorld( m_preview->GetClientSize(), false );
128  // Calculate the draw scale to fit the drawing area
129  double scale = std::min( fabs( clientSize.x / m_itemBBox.GetWidth() ),
130  fabs( clientSize.y / m_itemBBox.GetHeight() ) );
131 
132  // Above calculation will yield an exact fit; add a bit of whitespace around symbol
133  scale /= 1.2;
134 
135  // Now fix the best scale
136  view->SetScale( scale );
137  view->SetCenter( m_itemBBox.Centre() );
138 }
139 
140 
141 void SYMBOL_PREVIEW_WIDGET::DisplaySymbol( const LIB_ID& aSymbolID, int aUnit )
142 {
143  KIGFX::VIEW* view = m_preview->GetView();
144  auto settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
145  LIB_ALIAS* alias = nullptr;
146 
147  try
148  {
149  alias = m_kiway.Prj().SchSymbolLibTable()->LoadSymbol( aSymbolID );
150  }
151  catch( const IO_ERROR& ioe )
152  {
153  wxLogError( wxString::Format( _( "Error loading symbol %s from library %s.\n\n%s" ),
154  aSymbolID.GetLibItemName().wx_str(),
155  aSymbolID.GetLibNickname().wx_str(),
156  ioe.What() ) );
157  }
158 
159  if( m_previewItem )
160  {
161  view->Remove( m_previewItem );
162  delete m_previewItem;
163  m_previewItem = nullptr;
164  }
165 
166  if( alias )
167  {
168  LIB_PART* part = alias->GetPart();
169 
170  // If unit isn't specified for a multi-unit part, pick the first. (Otherwise we'll
171  // draw all of them.)
172  if( part->IsMulti() && aUnit == 0 )
173  aUnit = 1;
174 
175  settings->m_ShowUnit = aUnit;
176 
177  // For symbols having a De Morgan body style, use the first style
178  settings->m_ShowConvert = part->HasConversion() ? 1 : 0;
179 
180  m_previewItem = new LIB_ALIAS( *alias, part );
181  view->Add( m_previewItem );
182 
183  // Get the symbole size, in internal units
184  m_itemBBox = part->GetUnitBoundingBox( aUnit, 0 );
185 
186  if( !m_preview->IsShown() )
187  {
188  m_preview->Show();
189  m_statusSizer->ShowItems( false );
190  Layout(); // Ensure panel size is up to date.
191  }
192 
193  // Calculate the draw scale to fit the drawing area
194  fitOnDrawArea();
195  }
196 
198 }
199 
200 
202 {
203  KIGFX::VIEW* view = m_preview->GetView();
204 
205  if( m_previewItem )
206  {
207  view->Remove( m_previewItem );
208  delete m_previewItem;
209  m_previewItem = nullptr;
210  }
211 
212  if( aPart )
213  {
214  // If unit isn't specified for a multi-unit part, pick the first. (Otherwise we'll
215  // draw all of them.)
216  if( aPart->IsMulti() && aUnit == 0 )
217  aUnit = 1;
218 
219  // For symbols having a De Morgan body style, use the first style
220  auto settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
221  settings->m_ShowConvert = aPart->HasConversion() ? 1 : 0;
222  m_previewItem = new LIB_PART( *aPart );
223  view->Add( m_previewItem );
224 
225  // Get the symbole size, in internal units
226  m_itemBBox = aPart->GetUnitBoundingBox( aUnit, 0 );
227 
228  // Calculate the draw scale to fit the drawing area
229  fitOnDrawArea();
230  }
231 
233 
234  m_preview->Show();
235  m_statusSizer->ShowItems( false );
236 }
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:255
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:142
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.
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:50
void DisplaySymbol(const LIB_ID &aSymbolID, int aUnit)
Set the currently displayed symbol.
#define GAL_DISPLAY_OPTIONS_KEY
Definition: pgm_base.h:53
void ReadConfig(wxConfigBase *aCfg, const wxString &aBaseName)
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
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