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-2021 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>
34 #include <confirm.h>
36 #include <symbol_library.h>
37 #include <sch_base_frame.h>
38 #include <symbol_lib_table.h>
39 #include <tool/action_toolbar.h>
40 #include <tool/tool_manager.h>
41 #include <tool/tool_dispatcher.h>
42 #include <tools/ee_actions.h>
44 
45 
46 LIB_SYMBOL* SchGetLibSymbol( const LIB_ID& aLibId, SYMBOL_LIB_TABLE* aLibTable,
47  SYMBOL_LIB* aCacheLib, wxWindow* aParent, bool aShowErrorMsg )
48 {
49  wxCHECK_MSG( aLibTable, nullptr, "Invalid symbol library table." );
50 
51  LIB_SYMBOL* symbol = nullptr;
52 
53  try
54  {
55  symbol = aLibTable->LoadSymbol( aLibId );
56 
57  if( !symbol && aCacheLib )
58  {
59  wxCHECK_MSG( aCacheLib->IsCache(), nullptr, "Invalid cache library." );
60 
61  wxString cacheName = aLibId.GetLibNickname().wx_str();
62  cacheName += "_" + aLibId.GetLibItemName();
63  symbol = aCacheLib->FindSymbol( cacheName );
64  }
65  }
66  catch( const IO_ERROR& ioe )
67  {
68  if( aShowErrorMsg )
69  {
70  wxString msg = wxString::Format( _( "Error loading symbol '%s' from library '%s'." ),
71  aLibId.GetLibItemName().wx_str(),
72  aLibId.GetLibNickname().wx_str() );
73  DisplayErrorMessage( aParent, msg, ioe.What() );
74  }
75  }
76 
77  return symbol;
78 }
79 
80 
81 SCH_BASE_FRAME::SCH_BASE_FRAME( KIWAY* aKiway, wxWindow* aParent, FRAME_T aWindowType,
82  const wxString& aTitle, const wxPoint& aPosition,
83  const wxSize& aSize, long aStyle, const wxString& aFrameName ) :
84  EDA_DRAW_FRAME( aKiway, aParent, aWindowType, aTitle, aPosition, aSize, aStyle, aFrameName ),
85  m_base_frame_defaults( nullptr, "base_Frame_defaults" ),
86  m_defaults( &m_base_frame_defaults )
87 {
88  createCanvas();
89 
90  Bind( wxEVT_IDLE,
91  [this]( wxIdleEvent& aEvent )
92  {
93  // Handle cursor adjustments. While we can get motion and key events through
94  // wxWidgets, we can't get modifier-key-up events.
95  if( m_toolManager )
96  {
98 
99  if( selTool )
100  selTool->OnIdle( aEvent );
101  }
102  } );
103 }
104 
105 
107 {
108 }
109 
110 
112 {
114 }
115 
116 
118 {
119  return dynamic_cast<EESCHEMA_SETTINGS*>( config() );
120 }
121 
122 
124 {
125  return dynamic_cast<SYMBOL_EDITOR_SETTINGS*>( config() );
126 }
127 
128 
129 void SCH_BASE_FRAME::SetPageSettings( const PAGE_INFO& aPageSettings )
130 {
131  GetScreen()->SetPageSettings( aPageSettings );
132 }
133 
134 
136 {
137  return GetScreen()->GetPageSettings();
138 }
139 
140 
141 const wxSize SCH_BASE_FRAME::GetPageSizeIU() const
142 {
143  // GetSizeIU is compile time dependent:
144  return GetScreen()->GetPageSettings().GetSizeIU();
145 }
146 
147 
149 {
150  wxASSERT( GetScreen() );
151  return GetScreen()->GetTitleBlock();
152 }
153 
154 
155 void SCH_BASE_FRAME::SetTitleBlock( const TITLE_BLOCK& aTitleBlock )
156 {
157  wxASSERT( GetScreen() );
158  GetScreen()->SetTitleBlock( aTitleBlock );
159 }
160 
161 
163 {
164  wxString line;
165  BASE_SCREEN* screen = GetScreen();
166 
167  if( !screen )
168  return;
169 
171 
172  // Display absolute and relative coordinates
174  VECTOR2D d = cursorPos - screen->m_LocalOrigin;
175 
176  line.Printf( "X %s Y %s",
177  MessageTextFromValue( GetUserUnits(), cursorPos.x, false ),
178  MessageTextFromValue( GetUserUnits(), cursorPos.y, false ) );
179  SetStatusText( line, 2 );
180 
181  line.Printf( "dx %s dy %s dist %s",
182  MessageTextFromValue( GetUserUnits(), d.x, false ),
183  MessageTextFromValue( GetUserUnits(), d.y, false ),
184  MessageTextFromValue( GetUserUnits(), hypot( d.x, d.y ), false ) );
185  SetStatusText( line, 3 );
186 
187  // refresh grid display
188  DisplayGridMsg();
189 
190  // refresh units display
191  DisplayUnitsMsg();
192 }
193 
194 
195 LIB_SYMBOL* SCH_BASE_FRAME::GetLibSymbol( const LIB_ID& aLibId, bool aUseCacheLib,
196  bool aShowErrorMsg )
197 {
198  SYMBOL_LIB* cache = ( aUseCacheLib ) ? Prj().SchLibs()->GetCacheLibrary() : NULL;
199 
200  return SchGetLibSymbol( aLibId, Prj().SchSymbolLibTable(), cache, this, aShowErrorMsg );
201 }
202 
203 
204 bool SCH_BASE_FRAME::saveSymbolLibTables( bool aGlobal, bool aProject )
205 {
206  wxString msg;
207  bool success = true;
208 
209  if( aGlobal )
210  {
211  try
212  {
214  }
215  catch( const IO_ERROR& ioe )
216  {
217  success = false;
218  msg.Printf( _( "Error saving global symbol library table:\n%s" ), ioe.What() );
219  wxMessageBox( msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
220  }
221  }
222 
223  if( aProject && !Prj().GetProjectName().IsEmpty() )
224  {
225  wxFileName fn( Prj().GetProjectPath(), SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
226 
227  try
228  {
229  Prj().SchSymbolLibTable()->Save( fn.GetFullPath() );
230  }
231  catch( const IO_ERROR& ioe )
232  {
233  success = false;
234  msg.Printf( _( "Error saving project-specific symbol library table:\n%s" ),
235  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_SYMBOL_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  // Symbol children are drawn from their parents. Mark them for re-paint.
356  if( parent && parent->Type() == SCH_SYMBOL_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 
418  GetCanvas()->Refresh();
419 }
420 
421 
423 {
424  if( !m_colorSettings )
425  {
426  SETTINGS_MANAGER& settingsManager = Pgm().GetSettingsManager();
427  EESCHEMA_SETTINGS* cfg = settingsManager.GetAppSettings<EESCHEMA_SETTINGS>();
428  COLOR_SETTINGS* colorSettings = settingsManager.GetColorSettings( cfg->m_ColorTheme );
429 
430  const_cast<SCH_BASE_FRAME*>( this )->m_colorSettings = colorSettings;
431  }
432 
433  return m_colorSettings;
434 }
435 
436 
438 {
440 }
441 
void RefreshSelection()
Mark selected items for refresh.
KIGFX::SCH_VIEW * GetView() const override
Return 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)
Convert a value to a string using double notation.
Definition: base_units.cpp:103
bool IsCache() const
const UTF8 & GetLibItemName() const
Definition: lib_id.h:106
SCH_BASE_FRAME(KIWAY *aKiway, wxWindow *aParent, FRAME_T aWindowType, const wxString &aTitle, const wxPoint &aPosition, const wxSize &aSize, long aStyle, const wxString &aFrameName)
void UpdateItem(EDA_ITEM *aItem, bool isAddOrDelete=false)
Mark an item for refresh.
void ForceRefresh()
Force a redraw.
KIGFX::VIEW_CONTROLS * GetViewControls() const
Return 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:265
KIGFX::GAL_DISPLAY_OPTIONS & GetGalDisplayOptions()
Return a reference to the gal rendering options used by GAL for rendering.
LIB_SYMBOL * FindSymbol(const wxString &aName) const
Find LIB_SYMBOL by aName.
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.
Object used to load, save, search, and otherwise manipulate symbol library files.
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition: frame_type.h:32
EDA_DRAW_PANEL_GAL::GAL_TYPE m_canvasType
virtual EDA_DRAW_PANEL_GAL * GetCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
void UpdateAllItems(int aUpdateFlags)
Update all items in the view according to the given flags.
Definition: view.cpp:1416
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition: view.cpp:352
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,...
const TITLE_BLOCK & GetTitleBlock() const
Definition: sch_screen.h:142
void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: sch_screen.h:125
KIGFX::SCH_RENDER_SETTINGS * GetRenderSettings()
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
LIB_SYMBOL * SchGetLibSymbol(const LIB_ID &aLibId, SYMBOL_LIB_TABLE *aLibTable, SYMBOL_LIB *aCacheLib, wxWindow *aParent, bool aShowErrorMsg)
Load symbol from symbol library table.
All except INITIAL_ADD.
Definition: view_item.h:58
The base class for create windows for drawing purpose.
Define a library symbol object.
Definition: lib_symbol.h:96
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
Hold the information shown in the lower right corner of a plot, printout, or editing view.
Definition: title_block.h:40
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:124
search types array terminator (End Of Types)
Definition: typeinfo.h:81
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:77
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
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:207
void DisplayUnitsMsg()
Display current unit pane in the status bar.
EE_SELECTION & GetSelection()
Return the set of currently selected items.
EESCHEMA_SETTINGS * eeconfig() const
virtual void CenterOnCursor() const =0
Set the viewport center to the current cursor position and warps the cursor to the screen center.
virtual void WarpCursor(const VECTOR2D &aPosition, bool aWorldCoordinates=false, bool aWarpView=false)=0
If enabled (.
void CommonSettingsChanged(bool aEnvVarsChanged, bool aTextVarsChanged) override
Notification event that some of the common (suite-wide) settings have changed.
Contains all the knowledge about how to draw graphical object onto any particular output device.
Definition: painter.h:57
Item needs to be redrawn.
Definition: view_item.h:57
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition: view.cpp:580
#define NULL
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
void SetTitleBlock(const TITLE_BLOCK &aTitleBlock) override
void SyncView()
Mark all items for refresh.
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:53
Handles how to draw a screen (a board, a schematic ...)
Definition: base_screen.h:40
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
virtual ~SCH_BASE_FRAME()
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:258
Definition for symbol library class.
bool saveSymbolLibTables(bool aGlobal, bool aProject)
Save Symbol Library Tables to disk.
EDA_ITEM * GetParent() const
Definition: eda_item.h:115
#define _(s)
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:92
AppSettings * GetAppSettings(bool aLoadNow=true)
Returns a handle to the a given settings by type If the settings have already been loaded,...
SYMBOL_EDITOR_SETTINGS * libeditconfig() const
virtual bool IsType(const KICAD_T aScanTypes[]) const
Check whether the item is one of the listed types.
Definition: eda_item.h:183
SCH_LAYER_ID
Eeschema drawing layers.
virtual void ActivateGalCanvas()
Use to start up the GAL drawing canvas.
COLOR4D GetDrawBgColor() const override
void SetTitleBlock(const TITLE_BLOCK &aTitleBlock)
Definition: sch_screen.h:144
COLOR_SETTINGS * GetColorSettings() const override
Returns a pointer to the active color theme settings.
void SetPageSettings(const PAGE_INFO &aPageSettings) override
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
The current canvas type.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output 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)
COLOR_SETTINGS * GetColorSettings(const wxString &aName="user")
Retrieves a color settings object that applications can read colors from.
virtual RENDER_SETTINGS * GetSettings()=0
Return a 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:156
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,...
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.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
void CommonSettingsChanged(bool aEnvVarsChanged, bool aTextVarsChanged) override
Notification event that some of the common (suite-wide) settings have changed.
Store schematic specific render settings.
Definition: sch_painter.h:70
LIB_SYMBOL * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_SYMBOL having aName from the library given by aNickname.
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_SYMBOL * GetLibSymbol(const LIB_ID &aLibId, bool aUseCacheLib=false, bool aShowErrorMsg=false)
Load symbol from symbol library table.
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition: view.cpp:322
void UpdateStatusBar() override
Update the status bar information.
virtual void DisplayGridMsg()
Display current grid size in the status bar.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:197
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.
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
VECTOR2D GetCursorPosition() const
Return the current cursor position in world coordinates.
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: view.cpp:1504
VECTOR2D m_LocalOrigin
Relative Screen cursor coordinate (on grid) in user units.
Definition: base_screen.h:90
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:113
wxString m_ColorTheme
Active color theme name.
Definition: app_settings.h:184
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103