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>
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  Bind( wxEVT_IDLE,
93  [this]( wxIdleEvent& aEvent )
94  {
95  // Handle cursor adjustments. While we can get motion and key events through
96  // wxWidgets, we can't get modifier-key-up events.
97  if( m_toolManager )
98  {
100 
101  if( selTool )
102  selTool->OnIdle( aEvent );
103  }
104  } );
105 }
106 
107 
109 {
110 }
111 
112 
114 {
116 }
117 
118 
120 {
121  return dynamic_cast<EESCHEMA_SETTINGS*>( config() );
122 }
123 
124 
126 {
127  return dynamic_cast<SYMBOL_EDITOR_SETTINGS*>( config() );
128 }
129 
130 
131 void SCH_BASE_FRAME::SetPageSettings( const PAGE_INFO& aPageSettings )
132 {
133  GetScreen()->SetPageSettings( aPageSettings );
134 }
135 
136 
138 {
139  return GetScreen()->GetPageSettings();
140 }
141 
142 
143 const wxSize SCH_BASE_FRAME::GetPageSizeIU() const
144 {
145  // GetSizeIU is compile time dependent:
146  return GetScreen()->GetPageSettings().GetSizeIU();
147 }
148 
149 
151 {
152  wxASSERT( GetScreen() );
153  return GetScreen()->GetTitleBlock();
154 }
155 
156 
157 void SCH_BASE_FRAME::SetTitleBlock( const TITLE_BLOCK& aTitleBlock )
158 {
159  wxASSERT( GetScreen() );
160  GetScreen()->SetTitleBlock( aTitleBlock );
161 }
162 
163 
165 {
166  wxString line;
167  BASE_SCREEN* screen = GetScreen();
168 
169  if( !screen )
170  return;
171 
173 
174  // Display absolute and relative coordinates
176  VECTOR2D d = cursorPos - screen->m_LocalOrigin;
177 
178  line.Printf( "X %s Y %s",
179  MessageTextFromValue( GetUserUnits(), cursorPos.x, false ),
180  MessageTextFromValue( GetUserUnits(), cursorPos.y, false ) );
181  SetStatusText( line, 2 );
182 
183  line.Printf( "dx %s dy %s dist %s",
184  MessageTextFromValue( GetUserUnits(), d.x, false ),
185  MessageTextFromValue( GetUserUnits(), d.y, false ),
186  MessageTextFromValue( GetUserUnits(), hypot( d.x, d.y ), false ) );
187  SetStatusText( line, 3 );
188 
189  // refresh grid display
190  DisplayGridMsg();
191 
192  // refresh units display
193  DisplayUnitsMsg();
194 }
195 
196 
197 LIB_PART* SCH_BASE_FRAME::GetLibPart( const LIB_ID& aLibId, bool aUseCacheLib, bool aShowErrorMsg )
198 {
199  PART_LIB* cache = ( aUseCacheLib ) ? Prj().SchLibs()->GetCacheLibrary() : NULL;
200 
201  return SchGetLibPart( aLibId, Prj().SchSymbolLibTable(), cache, this, aShowErrorMsg );
202 }
203 
204 
205 bool SCH_BASE_FRAME::saveSymbolLibTables( bool aGlobal, bool aProject )
206 {
207  wxString msg;
208  bool success = true;
209 
210  if( aGlobal )
211  {
212  try
213  {
215  }
216  catch( const IO_ERROR& ioe )
217  {
218  success = false;
219  msg.Printf( _( "Error saving global symbol library table:\n%s" ), ioe.What() );
220  wxMessageBox( msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
221  }
222  }
223 
224  if( aProject && !Prj().GetProjectName().IsEmpty() )
225  {
226  wxFileName fn( Prj().GetProjectPath(), SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
227 
228  try
229  {
230  Prj().SchSymbolLibTable()->Save( fn.GetFullPath() );
231  }
232  catch( const IO_ERROR& ioe )
233  {
234  success = false;
235  msg.Printf( _( "Error saving project-specific symbol library table:\n%s" ), ioe.What() );
236  wxMessageBox( msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
237  }
238  }
239 
240  return success;
241 }
242 
243 
244 void SCH_BASE_FRAME::RedrawScreen( const wxPoint& aCenterPoint, bool aWarpPointer )
245 {
246  GetCanvas()->GetView()->SetCenter( aCenterPoint );
247 
248  if( aWarpPointer )
250 
251  GetCanvas()->Refresh();
252 }
253 
254 
255 void SCH_BASE_FRAME::CenterScreen( const wxPoint& aCenterPoint, bool aWarpPointer )
256 {
257  GetCanvas()->GetView()->SetCenter( aCenterPoint );
258 
259  if( aWarpPointer )
260  GetCanvas()->GetViewControls()->WarpCursor( aCenterPoint, true );
261 
262  GetCanvas()->Refresh();
263 }
264 
265 
267 {
269  GetCanvas()->ForceRefresh();
270 }
271 
272 
274 {
275  return static_cast<SCH_DRAW_PANEL*>( EDA_DRAW_FRAME::GetCanvas() );
276 }
277 
278 
280 {
281  KIGFX::PAINTER* painter = GetCanvas()->GetView()->GetPainter();
282  return static_cast<KIGFX::SCH_RENDER_SETTINGS*>( painter->GetSettings() );
283 }
284 
285 
287 {
289 
290  // Allows only a CAIRO or OPENGL canvas:
293  {
295  }
296 
297  SetCanvas( new SCH_DRAW_PANEL( this, wxID_ANY, wxPoint( 0, 0 ), m_frameSize,
300 }
301 
302 
303 void SCH_BASE_FRAME::UpdateItem( EDA_ITEM* aItem, bool isAddOrDelete )
304 {
305  EDA_ITEM* parent = aItem->GetParent();
306 
307  if( aItem->Type() == SCH_SHEET_PIN_T )
308  {
309  // Sheet pins aren't in the view. Refresh their parent.
310  if( parent )
311  GetCanvas()->GetView()->Update( parent );
312  }
313  else
314  {
315  if( !isAddOrDelete )
316  GetCanvas()->GetView()->Update( aItem );
317 
318  // Some children are drawn from their parents. Mark them for re-paint.
319  static KICAD_T parentTypes[] = { SCH_COMPONENT_T, SCH_SHEET_T, SCH_GLOBAL_LABEL_T, EOT };
320 
321  if( parent && parent->IsType( parentTypes ) )
322  GetCanvas()->GetView()->Update( parent, KIGFX::REPAINT );
323  }
324 
325  // Calling Refresh() here introduces a bi-stable state: when doing operations on a
326  // large number of items if at some point the refresh timer times out and does a
327  // refresh it will take long enough that the next item will also time out, and the
328  // next, and the next, etc.
329  // GetCanvas()->Refresh();
330 }
331 
332 
334 {
335  if( m_toolManager )
336  {
338  SELECTION& selection = selectionTool->GetSelection();
339  KIGFX::SCH_VIEW* view = GetCanvas()->GetView();
340 
341  for( EDA_ITEM* item : selection )
342  {
343  EDA_ITEM* parent = item->GetParent();
344 
345  if( item->Type() == SCH_SHEET_PIN_T )
346  {
347  // Sheet pins aren't in the view. Refresh their parent.
348  if( parent )
349  GetCanvas()->GetView()->Update( parent );
350  }
351  else
352  {
353  view->Update( item, KIGFX::REPAINT );
354 
355  // Component children are drawn from their parents. Mark them for re-paint.
356  if( parent && parent->Type() == SCH_COMPONENT_T )
357  GetCanvas()->GetView()->Update( parent, KIGFX::REPAINT );
358  }
359  }
360  }
361 }
362 
363 
365 {
366  auto screen = aScreen;
367 
368  if( aScreen == nullptr )
369  screen = GetScreen();
370 
371  screen->Append( (SCH_ITEM*) aItem );
372 
373  if( screen == GetScreen() )
374  {
375  GetCanvas()->GetView()->Add( aItem );
376  UpdateItem( aItem, true ); // handle any additional parent semantics
377  }
378 }
379 
380 
382 {
383  auto screen = aScreen;
384 
385  if( aScreen == nullptr )
386  screen = GetScreen();
387 
388  if( screen == GetScreen() )
389  GetCanvas()->GetView()->Remove( aItem );
390 
391  screen->Remove( (SCH_ITEM*) aItem );
392 
393  if( screen == GetScreen() )
394  UpdateItem( aItem, true ); // handle any additional parent semantics
395 }
396 
397 
399 {
401 }
402 
403 
405 {
406  return GetColorSettings()->GetColor( aLayer );
407 }
408 
409 
410 void SCH_BASE_FRAME::CommonSettingsChanged( bool aEnvVarsChanged, bool aTextVarsChanged )
411 {
412  EDA_DRAW_FRAME::CommonSettingsChanged( aEnvVarsChanged, aTextVarsChanged );
413 
414  EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
415  m_colorSettings = Pgm().GetSettingsManager().GetColorSettings( cfg->m_ColorTheme );
416 }
417 
418 
420 {
421  if( !m_colorSettings )
422  {
423  EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
424  m_colorSettings = Pgm().GetSettingsManager().GetColorSettings( cfg->m_ColorTheme );
425  }
426 
427  return m_colorSettings;
428 }
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()
void OnIdle(wxIdleEvent &aEvent)
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aAddUnitLabel, EDA_DATA_TYPE aType)
Definition: base_units.cpp:123
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:60
void UpdateItem(EDA_ITEM *aItem, bool isAddOrDelete=false)
Mark an item for refresh.
void ForceRefresh()
Function ForceRefresh() Forces a redraw.
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:253
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 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:1417
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:357
static constexpr GAL_TYPE GAL_FALLBACK
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Helper to retrieve a layer color from the global color settings.
void RemoveFromScreen(EDA_ITEM *aItem, SCH_SCREEN *aScreen)
Remove an item from the screen (and view) aScreen is the screen the item is located on,...
LIB_PART * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_PART having aName from the library given by aNickname.
const TITLE_BLOCK & GetTitleBlock() const
Definition: sch_screen.h:191
void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: sch_screen.h:182
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.
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:63
The base class for create windows for drawing purpose.
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:102
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:181
search types array terminator (End Of Types)
Definition: typeinfo.h:82
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
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:201
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 (.
void CommonSettingsChanged(bool aEnvVarsChanged, bool aTextVarsChanged) override
Notification event that some of the common (suite-wide) settings have changed.
PAINTER contains all the knowledge about how to draw graphical object onto any particular output devi...
Definition: painter.h:57
Item is being added to the view.
Definition: view_item.h:62
void SetCenter(const VECTOR2D &aCenter)
Function SetCenter() Sets the center point of the VIEW (i.e.
Definition: view.cpp:585
#define NULL
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
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:41
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: eda_item.h:183
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
SYMBOL_EDITOR_SETTINGS * libeditconfig() const
virtual bool IsType(const KICAD_T aScanTypes[]) const
Function IsType Checks whether the item is one of the listed types.
Definition: eda_item.h:250
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:193
void SetPageSettings(const PAGE_INFO &aPageSettings) override
LIB_PART * FindPart(const wxString &aName) const
Find LIB_PART by aName.
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
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
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.
EDA_DRAW_PANEL_GAL::GAL_TYPE loadCanvasTypeSetting()
Returns the canvas type stored in the application settings.
COLOR4D GetColor(int aLayer) const
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:48
see class PGM_BASE
void AddToScreen(EDA_ITEM *aItem, SCH_SCREEN *aScreen)
Add an item to the screen (and view) aScreen is the screen the item is located on,...
#define _(s)
Definition: 3d_actions.cpp:33
const TITLE_BLOCK & GetTitleBlock() const override
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: eda_item.h:148
void CommonSettingsChanged(bool aEnvVarsChanged, bool aTextVarsChanged) override
Notification event that some of the common (suite-wide) settings have changed.
SCH_RENDER_SETTINGS Stores schematic-specific render settings.
Definition: sch_painter.h:70
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:327
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:194
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.
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: view.cpp:1513
VECTOR2D m_LocalOrigin
Relative Screen cursor coordinate (on grid) in user units.
Definition: base_screen.h:89
KICAD_T Type() const
Function Type()
Definition: eda_item.h:181
wxString m_ColorTheme
Active color theme name.
Definition: app_settings.h:176
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:100