KiCad PCB EDA Suite
sch_base_frame.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) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 2015-2020 KiCad Developers, see change_log.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <base_units.h>
26 #include <kiway.h>
27 #include <pgm_base.h>
28 #include <eeschema_settings.h>
29 #include <libedit_settings.h>
30 #include <sch_draw_panel.h>
31 #include <sch_view.h>
32 #include <sch_painter.h>
35 #include <confirm.h>
37 #include <class_library.h>
38 #include <sch_base_frame.h>
39 #include <symbol_lib_table.h>
40 #include <tool/action_toolbar.h>
41 #include <tool/tool_manager.h>
42 #include <tool/tool_dispatcher.h>
43 #include <tools/ee_actions.h>
45 #include <default_values.h> // For some default values
46 
47 
48 LIB_PART* SchGetLibPart( const LIB_ID& aLibId, SYMBOL_LIB_TABLE* aLibTable, PART_LIB* aCacheLib,
49  wxWindow* aParent, bool aShowErrorMsg )
50 {
51  wxCHECK_MSG( aLibTable, nullptr, "Invalid symbol library table." );
52 
53  LIB_PART* symbol = nullptr;
54 
55  try
56  {
57  symbol = aLibTable->LoadSymbol( aLibId );
58 
59  if( !symbol && aCacheLib )
60  {
61  wxCHECK_MSG( aCacheLib->IsCache(), nullptr, "Invalid cache library." );
62 
63  wxString cacheName = aLibId.GetLibNickname().wx_str();
64  cacheName += "_" + aLibId.GetLibItemName();
65  symbol = aCacheLib->FindPart( cacheName );
66  }
67  }
68  catch( const IO_ERROR& ioe )
69  {
70  if( aShowErrorMsg )
71  {
72  wxString msg = wxString::Format( _( "Error loading symbol '%s' from library '%s'." ),
73  aLibId.GetLibItemName().wx_str(),
74  aLibId.GetLibNickname().wx_str() );
75  DisplayErrorMessage( aParent, msg, ioe.What() );
76  }
77  }
78 
79  return symbol;
80 }
81 
82 
83 SCH_BASE_FRAME::SCH_BASE_FRAME( KIWAY* aKiway, wxWindow* aParent, FRAME_T aWindowType,
84  const wxString& aTitle, const wxPoint& aPosition,
85  const wxSize& aSize, long aStyle, const wxString& aFrameName ) :
86  EDA_DRAW_FRAME( aKiway, aParent, aWindowType, aTitle, aPosition, aSize, aStyle, aFrameName ),
87  m_base_frame_defaults( nullptr, "base_Frame_defaults" ),
88  m_defaults( &m_base_frame_defaults )
89 {
90  createCanvas();
91 }
92 
93 
95 {
96 }
97 
98 
100 {
102 }
103 
104 
106 {
107  return dynamic_cast<EESCHEMA_SETTINGS*>( config() );
108 }
109 
110 
112 {
113  return dynamic_cast<LIBEDIT_SETTINGS*>( config() );
114 }
115 
116 
117 void SCH_BASE_FRAME::SetPageSettings( const PAGE_INFO& aPageSettings )
118 {
119  GetScreen()->SetPageSettings( aPageSettings );
120 }
121 
122 
124 {
125  return GetScreen()->GetPageSettings();
126 }
127 
128 
129 const wxSize SCH_BASE_FRAME::GetPageSizeIU() const
130 {
131  // GetSizeIU is compile time dependent:
132  return GetScreen()->GetPageSettings().GetSizeIU();
133 }
134 
135 
137 {
138  wxASSERT( GetScreen() );
139  return GetScreen()->GetTitleBlock();
140 }
141 
142 
143 void SCH_BASE_FRAME::SetTitleBlock( const TITLE_BLOCK& aTitleBlock )
144 {
145  wxASSERT( GetScreen() );
146  GetScreen()->SetTitleBlock( aTitleBlock );
147 }
148 
149 
151 {
152  wxString line;
153  BASE_SCREEN* screen = GetScreen();
154 
155  if( !screen )
156  return;
157 
159 
160  // Display absolute coordinates:
162  double dXpos = To_User_Unit( GetUserUnits(), cursorPos.x );
163  double dYpos = To_User_Unit( GetUserUnits(), cursorPos.y );
164 
165  wxString absformatter;
166  wxString locformatter;
167 
168  switch( GetUserUnits() )
169  {
170  case EDA_UNITS::INCHES:
171  absformatter = "X %.3f Y %.3f";
172  locformatter = "dx %.3f dy %.3f dist %.3f";
173  break;
174 
176  absformatter = "X %.4f Y %.4f";
177  locformatter = "dx %.4f dy %.4f dist %.4f";
178  break;
179 
180  case EDA_UNITS::UNSCALED:
181  absformatter = "X %f Y %f";
182  locformatter = "dx %f dy %f dist %f";
183  break;
184 
185  default:
186  wxASSERT( false );
187  break;
188  }
189 
190  line.Printf( absformatter, dXpos, dYpos );
191  SetStatusText( line, 2 );
192 
193  // Display relative coordinates:
194  double dx = cursorPos.x - screen->m_LocalOrigin.x;
195  double dy = cursorPos.y - screen->m_LocalOrigin.y;
196 
197  dXpos = To_User_Unit( GetUserUnits(), dx );
198  dYpos = To_User_Unit( GetUserUnits(), dy );
199 
200  // We already decided the formatter above
201  line.Printf( locformatter, dXpos, dYpos, hypot( dXpos, dYpos ) );
202  SetStatusText( line, 3 );
203 
204  // refresh grid display
205  DisplayGridMsg();
206 
207  // refresh units display
208  DisplayUnitsMsg();
209 }
210 
211 
212 LIB_PART* SCH_BASE_FRAME::GetLibPart( const LIB_ID& aLibId, bool aUseCacheLib, bool aShowErrorMsg )
213 {
214  PART_LIB* cache = ( aUseCacheLib ) ? Prj().SchLibs()->GetCacheLibrary() : NULL;
215 
216  return SchGetLibPart( aLibId, Prj().SchSymbolLibTable(), cache, this, aShowErrorMsg );
217 }
218 
219 
220 bool SCH_BASE_FRAME::saveSymbolLibTables( bool aGlobal, bool aProject )
221 {
222  wxString msg;
223  bool success = true;
224 
225  if( aGlobal )
226  {
227  try
228  {
230  }
231  catch( const IO_ERROR& ioe )
232  {
233  success = false;
234  msg.Printf( _( "Error saving global symbol library table:\n%s" ), ioe.What() );
235  wxMessageBox( msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
236  }
237  }
238 
239  if( aProject && !Prj().GetProjectName().IsEmpty() )
240  {
241  wxFileName fn( Prj().GetProjectPath(), SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
242 
243  try
244  {
245  Prj().SchSymbolLibTable()->Save( fn.GetFullPath() );
246  }
247  catch( const IO_ERROR& ioe )
248  {
249  success = false;
250  msg.Printf( _( "Error saving project-specific symbol library table:\n%s" ), ioe.What() );
251  wxMessageBox( msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
252  }
253  }
254 
255  return success;
256 }
257 
258 
259 void SCH_BASE_FRAME::RedrawScreen( const wxPoint& aCenterPoint, bool aWarpPointer )
260 {
261  GetCanvas()->GetView()->SetCenter( aCenterPoint );
262 
263  if( aWarpPointer )
265 
266  GetCanvas()->Refresh();
267 }
268 
269 
270 void SCH_BASE_FRAME::CenterScreen( const wxPoint& aCenterPoint, bool aWarpPointer )
271 {
272  GetCanvas()->GetView()->SetCenter( aCenterPoint );
273 
274  if( aWarpPointer )
275  GetCanvas()->GetViewControls()->WarpCursor( aCenterPoint, true );
276 
277  GetCanvas()->Refresh();
278 }
279 
280 
282 {
284  GetCanvas()->ForceRefresh();
285 }
286 
287 
289 {
290  return static_cast<SCH_DRAW_PANEL*>( EDA_DRAW_FRAME::GetCanvas() );
291 }
292 
293 
295 {
296  KIGFX::PAINTER* painter = GetCanvas()->GetView()->GetPainter();
297  return static_cast<KIGFX::SCH_RENDER_SETTINGS*>( painter->GetSettings() );
298 }
299 
300 
302 {
304 
305  // Allows only a CAIRO or OPENGL canvas:
308  {
310  }
311 
312  SetCanvas( new SCH_DRAW_PANEL( this, wxID_ANY, wxPoint( 0, 0 ), m_FrameSize,
315 }
316 
317 
318 void SCH_BASE_FRAME::RefreshItem( EDA_ITEM* aItem, bool isAddOrDelete )
319 {
320  EDA_ITEM* parent = aItem->GetParent();
321 
322  if( aItem->Type() == SCH_SHEET_PIN_T )
323  {
324  // Sheet pins aren't in the view. Refresh their parent.
325  if( parent )
326  GetCanvas()->GetView()->Update( parent );
327  }
328  else
329  {
330  if( !isAddOrDelete )
331  GetCanvas()->GetView()->Update( aItem );
332 
333  // Component children are drawn from their parents. Mark them for re-paint.
334  if( parent && parent->Type() == SCH_COMPONENT_T )
335  GetCanvas()->GetView()->Update( parent, KIGFX::REPAINT );
336  else if( parent && parent->Type() == SCH_SHEET_T )
337  GetCanvas()->GetView()->Update( parent, KIGFX::REPAINT );
338  }
339 
340  GetCanvas()->Refresh();
341 }
342 
343 
345 {
346  if( m_toolManager )
347  {
349  SELECTION& selection = selectionTool->GetSelection();
350  KIGFX::SCH_VIEW* view = GetCanvas()->GetView();
351 
352  for( EDA_ITEM* item : selection )
353  {
354  EDA_ITEM* parent = item->GetParent();
355 
356  if( item->Type() == SCH_SHEET_PIN_T )
357  {
358  // Sheet pins aren't in the view. Refresh their parent.
359  if( parent )
360  GetCanvas()->GetView()->Update( parent );
361  }
362  else
363  {
364  view->Update( item, KIGFX::REPAINT );
365 
366  // Component children are drawn from their parents. Mark them for re-paint.
367  if( parent && parent->Type() == SCH_COMPONENT_T )
368  GetCanvas()->GetView()->Update( parent, KIGFX::REPAINT );
369  }
370  }
371  }
372 }
373 
374 
376 {
377  auto screen = aScreen;
378 
379  if( aScreen == nullptr )
380  screen = GetScreen();
381 
382  screen->Append( (SCH_ITEM*) aItem );
383 
384  if( screen == GetScreen() )
385  {
386  GetCanvas()->GetView()->Add( aItem );
387  RefreshItem( aItem, true ); // handle any additional parent semantics
388  }
389 }
390 
391 
393 {
394  auto screen = aScreen;
395 
396  if( aScreen == nullptr )
397  screen = GetScreen();
398 
399  if( screen == GetScreen() )
400  GetCanvas()->GetView()->Remove( aItem );
401 
402  screen->Remove( (SCH_ITEM*) aItem );
403 
404  if( screen == GetScreen() )
405  RefreshItem( aItem, true ); // handle any additional parent semantics
406 }
407 
408 
410 {
412 }
413 
414 
416 {
417  return GetColorSettings()->GetColor( aLayer );
418 }
419 
420 
421 void SCH_BASE_FRAME::CommonSettingsChanged( bool aEnvVarsChanged )
422 {
423  EDA_DRAW_FRAME::CommonSettingsChanged( aEnvVarsChanged );
424 
425  EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
426  m_colorSettings = Pgm().GetSettingsManager().GetColorSettings( cfg->m_ColorTheme );
427 }
428 
429 
431 {
432  if( !m_colorSettings )
433  {
434  EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
435  m_colorSettings = Pgm().GetSettingsManager().GetColorSettings( cfg->m_ColorTheme );
436  }
437 
438  return m_colorSettings;
439 }
double To_User_Unit(EDA_UNITS aUnit, double aValue, bool aUseMils)
Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit...
Definition: base_units.cpp:92
void RefreshSelection()
Mark selected items for refresh.
KIGFX::SCH_VIEW * GetView() const override
Function GetView() Returns a pointer to the VIEW instance used in the panel.
static const wxString & GetSymbolLibTableFileName()
static SYMBOL_LIB_TABLE & GetGlobalLibTable()
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
SCH_BASE_FRAME(KIWAY *aKiway, wxWindow *aParent, FRAME_T aWindowType, const wxString &aTitle, const wxPoint &aPosition, const wxSize &aSize, long aStyle, const wxString &aFrameName)
static S3D_CACHE * cache
Definition: export_vrml.cpp:65
void ForceRefresh()
Function ForceRefresh() Forces a redraw.
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
KIGFX::VIEW_CONTROLS * GetViewControls() const
Function GetViewControls() Returns a pointer to the VIEW_CONTROLS instance used in the panel.
virtual void RedrawScreen(const wxPoint &aCenterPoint, bool aWarpPointer)
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:252
KIGFX::GAL_DISPLAY_OPTIONS & GetGalDisplayOptions()
Return a reference to the gal rendering options used by GAL for rendering.
virtual APP_SETTINGS_BASE * config() const
Returns the settings object used in SaveSettings(), and is overloaded in KICAD_MANAGER_FRAME.
void AddToScreen(EDA_ITEM *aItem, SCH_SCREEN *aScreen=nullptr)
Add an item to the screen (and view) aScreen is the screen the item is located on,...
void HardRedraw() override
Rebuild the GAL and redraws the screen.
Implementation of conversion functions that require both schematic and board internal units.
This file is part of the common library.
FRAME_T
Enum FRAME_T is the set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition: frame_type.h:34
EDA_DRAW_PANEL_GAL::GAL_TYPE m_canvasType
The current canvas type.
virtual EDA_DRAW_PANEL_GAL * GetCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
void UpdateAllItems(int aUpdateFlags)
Updates all items in the view according to the given flags.
Definition: view.cpp:1444
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
virtual void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:376
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Helper to retrieve a layer color from the global color settings.
LIB_PART * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_PART having aName from the library given by aNickname.
EDA_DRAW_PANEL_GAL::GAL_TYPE LoadCanvasTypeSetting()
Returns the canvas type stored in the application settings.
const TITLE_BLOCK & GetTitleBlock() const
Definition: sch_screen.h:194
void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: sch_screen.h:185
KIGFX::SCH_RENDER_SETTINGS * GetRenderSettings()
LIB_PART * GetLibPart(const LIB_ID &aLibId, bool aUseCacheLib=false, bool aShowErrorMsg=false)
Load symbol from symbol library table.
COLOR_SETTINGS * GetColorSettings() override
Returns a pointer to the active color theme settings.
void RemoveFromScreen(EDA_ITEM *aItem, SCH_SCREEN *aScreen=nullptr)
Remove an item from the screen (and view) aScreen is the screen the item is located on,...
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
Item needs to be redrawn.
Definition: view_item.h:61
The base class for create windows for drawing purpose.
TITLE_BLOCK holds the information shown in the lower right corner of a plot, printout,...
Definition: title_block.h:40
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:184
const wxSize GetPageSizeIU() const override
Works off of GetPageSettings() to return the size of the paper page in the internal units of this par...
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
void DisplayUnitsMsg()
Display current unit pane in the status bar.
EE_SELECTION & GetSelection()
Function GetSelection()
EESCHEMA_SETTINGS * eeconfig() const
virtual void CenterOnCursor() const =0
Function CenterOnCursor() Sets the viewport center to the current cursor position and warps the curso...
virtual void WarpCursor(const VECTOR2D &aPosition, bool aWorldCoordinates=false, bool aWarpView=false)=0
Function WarpCursor() If enabled (.
PAINTER contains all the knowledge about how to draw graphical object onto any particular output devi...
Definition: painter.h:58
Item is being added to the view.
Definition: view_item.h:60
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
void SetTitleBlock(const TITLE_BLOCK &aTitleBlock) override
void SyncView()
Mark all items for refresh.
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
PAGE_INFO describes the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:54
BASE_SCREEN handles how to draw a screen (a board, a schematic ...)
Definition: base_screen.h:44
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
Define a library symbol object.
virtual ~SCH_BASE_FRAME()
KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the s...
Definition: kiway.h:273
bool saveSymbolLibTables(bool aGlobal, bool aProject)
Saves Symbol Library Tables to disk.
EDA_ITEM * GetParent() const
Definition: base_struct.h:195
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
SCH_LAYER_ID
Eeschema drawing layers.
virtual void ActivateGalCanvas()
Use to start up the GAL drawing canvas.
void SetTitleBlock(const TITLE_BLOCK &aTitleBlock)
Definition: sch_screen.h:196
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags)
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: view.cpp:1540
void SetPageSettings(const PAGE_INFO &aPageSettings) override
LIB_PART * FindPart(const wxString &aName) const
Find LIB_PART by aName.
LIBEDIT_SETTINGS * libeditconfig() const
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
COLOR_SETTINGS * m_colorSettings
const PAGE_INFO & GetPageSettings() const override
void UpdateStatusBar() override
Update the status bar information.
void SetCanvas(EDA_DRAW_PANEL_GAL *aPanel)
virtual RENDER_SETTINGS * GetSettings()=0
Function GetAdapter Returns pointer to current settings that are going to be used when drawing items.
COLOR4D GetColor(int aLayer) const
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:48
see class PGM_BASE
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
void RefreshItem(EDA_ITEM *aItem, bool isAddOrDelete=false)
Mark an item for refresh.
#define _(s)
Definition: 3d_actions.cpp:33
const TITLE_BLOCK & GetTitleBlock() const override
void CommonSettingsChanged(bool aEnvVarsChanged) override
Notification event that some of the common (suite-wide) settings have changed.
wxString wx_str() const
Definition: utf8.cpp:51
void Save(const wxString &aFileName) const
Write this library table to aFileName in s-expression form.
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
SCH_RENDER_SETTINGS Stores schematic-specific render settings.
Definition: sch_painter.h:70
void CommonSettingsChanged(bool aEnvVarsChanged) override
Notification event that some of the common (suite-wide) settings have changed.
Color settings are a bit different than most of the settings objects in that there can be more than o...
virtual void CenterScreen(const wxPoint &aCenterPoint, bool aWarpPointer)
LIB_PART * SchGetLibPart(const LIB_ID &aLibId, SYMBOL_LIB_TABLE *aLibTable, PART_LIB *aCacheLib, wxWindow *aParent, bool aShowErrorMsg)
Load symbol from symbol library table.
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:346
void UpdateStatusBar() override
Update the status bar information.
Definition for part library class.
virtual void DisplayGridMsg()
Display current grid size in the status bar.
Object used to load, save, search, and otherwise manipulate symbol library files.
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
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:186
virtual BASE_SCREEN * GetScreen() const
Return a pointer to a BASE_SCREEN or one of its derivatives.
static wxString GetGlobalTableFileName()
Fetch the global symbol library table file name.
bool IsCache() const
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
VECTOR2D GetCursorPosition() const
Returns the current cursor position in world coordinates.
VECTOR2D m_LocalOrigin
Relative Screen cursor coordinate (on grid) in user units.
Definition: base_screen.h:64
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193
wxString m_ColorTheme
Active color theme name.
Definition: app_settings.h:156
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99