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-2017 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 <class_drawpanel.h>
28 #include <confirm.h>
29 
30 #include <class_library.h>
31 #include <eeschema_id.h>
32 #include <libeditframe.h>
33 #include <viewlib_frame.h>
34 #include <sch_base_frame.h>
35 #include <symbol_lib_table.h>
37 
38 
39 
40 LIB_ALIAS* SchGetLibAlias( const LIB_ID& aLibId, SYMBOL_LIB_TABLE* aLibTable, PART_LIB* aCacheLib,
41  wxWindow* aParent, bool aShowErrorMsg )
42 {
43  // wxCHECK_MSG( aLibId.IsValid(), NULL, "LIB_ID is not valid." );
44  wxCHECK_MSG( aLibTable, NULL, "Invalid symbol library table." );
45 
46  LIB_ALIAS* alias = NULL;
47 
48  try
49  {
50  alias = aLibTable->LoadSymbol( aLibId );
51 
52  if( !alias && aCacheLib )
53  alias = aCacheLib->FindAlias( aLibId );
54  }
55  catch( const IO_ERROR& ioe )
56  {
57  if( aShowErrorMsg )
58  {
59  wxString msg;
60 
61  msg.Printf( _( "Could not load symbol \"%s\" from library \"%s\"." ),
62  aLibId.GetLibItemName().wx_str(), aLibId.GetLibNickname().wx_str() );
63  DisplayErrorMessage( aParent, msg, ioe.What() );
64  }
65  }
66 
67  return alias;
68 }
69 
70 
71 LIB_PART* SchGetLibPart( const LIB_ID& aLibId, SYMBOL_LIB_TABLE* aLibTable, PART_LIB* aCacheLib,
72  wxWindow* aParent, bool aShowErrorMsg )
73 {
74  LIB_ALIAS* alias = SchGetLibAlias( aLibId, aLibTable, aCacheLib, aParent, aShowErrorMsg );
75 
76  return ( alias ) ? alias->GetPart() : NULL;
77 }
78 
79 
80 // Sttaic members:
81 
82 SCH_BASE_FRAME::SCH_BASE_FRAME( KIWAY* aKiway, wxWindow* aParent,
83  FRAME_T aWindowType, const wxString& aTitle,
84  const wxPoint& aPosition, const wxSize& aSize, long aStyle,
85  const wxString& aFrameName ) :
86  EDA_DRAW_FRAME( aKiway, aParent, aWindowType, aTitle, aPosition,
87  aSize, aStyle, aFrameName )
88 {
89  m_zoomLevelCoeff = 11.0; // Adjusted to roughly displays zoom level = 1
90  // when the screen shows a 1:1 image
91  // obviously depends on the monitor,
92  // but this is an acceptable value
95 }
96 
97 
98 
100 {
101 }
102 
103 
104 void SCH_BASE_FRAME::OnOpenLibraryViewer( wxCommandEvent& event )
105 {
106  LIB_VIEW_FRAME* viewlibFrame = (LIB_VIEW_FRAME*) Kiway().Player( FRAME_SCH_VIEWER, true );
107 
108  viewlibFrame->PushPreferences( m_canvas );
109 
110  // On Windows, Raise() does not bring the window on screen, when iconized
111  if( viewlibFrame->IsIconized() )
112  viewlibFrame->Iconize( false );
113 
114  viewlibFrame->Show( true );
115  viewlibFrame->Raise();
116 }
117 
118 
119 // Virtual from EDA_DRAW_FRAME
121 {
123 }
124 
125 
127 {
128  m_drawBgColor= aColor;
130 }
131 
132 
134 {
136 }
137 
138 
140 {
142 }
143 
144 
145 void SCH_BASE_FRAME::SetPageSettings( const PAGE_INFO& aPageSettings )
146 {
147  GetScreen()->SetPageSettings( aPageSettings );
148 }
149 
150 
152 {
153  return GetScreen()->GetPageSettings();
154 }
155 
156 
157 const wxSize SCH_BASE_FRAME::GetPageSizeIU() const
158 {
159  // GetSizeIU is compile time dependent:
160  return GetScreen()->GetPageSettings().GetSizeIU();
161 }
162 
163 
165 {
166  wxASSERT( GetScreen() );
167  return GetScreen()->GetAuxOrigin();
168 }
169 
170 
171 void SCH_BASE_FRAME::SetAuxOrigin( const wxPoint& aPosition )
172 {
173  wxASSERT( GetScreen() );
174  GetScreen()->SetAuxOrigin( aPosition );
175 }
176 
177 
179 {
180  wxASSERT( GetScreen() );
181  return GetScreen()->GetTitleBlock();
182 }
183 
184 
185 void SCH_BASE_FRAME::SetTitleBlock( const TITLE_BLOCK& aTitleBlock )
186 {
187  wxASSERT( GetScreen() );
188  GetScreen()->SetTitleBlock( aTitleBlock );
189 }
190 
191 
193 {
194  wxString line;
195  int dx, dy;
196  BASE_SCREEN* screen = GetScreen();
197 
198  if( !screen )
199  return;
200 
202 
203  // Display absolute coordinates:
204  double dXpos = To_User_Unit( g_UserUnit, GetCrossHairPosition().x );
205  double dYpos = To_User_Unit( g_UserUnit, GetCrossHairPosition().y );
206 
207  if ( g_UserUnit == MILLIMETRES )
208  {
209  dXpos = RoundTo0( dXpos, 100.0 );
210  dYpos = RoundTo0( dYpos, 100.0 );
211  }
212 
213  wxString absformatter;
214  wxString locformatter;
215 
216  switch( g_UserUnit )
217  {
218  case INCHES:
219  absformatter = wxT( "X %.3f Y %.3f" );
220  locformatter = wxT( "dx %.3f dy %.3f dist %.3f" );
221  break;
222 
223  case MILLIMETRES:
224  absformatter = wxT( "X %.2f Y %.2f" );
225  locformatter = wxT( "dx %.2f dy %.2f dist %.2f" );
226  break;
227 
228  case UNSCALED_UNITS:
229  absformatter = wxT( "X %f Y %f" );
230  locformatter = wxT( "dx %f dy %f dist %f" );
231  break;
232 
233  case DEGREES:
234  wxASSERT( false );
235  break;
236  }
237 
238  line.Printf( absformatter, dXpos, dYpos );
239  SetStatusText( line, 2 );
240 
241  // Display relative coordinates:
242  dx = GetCrossHairPosition().x - screen->m_O_Curseur.x;
243  dy = GetCrossHairPosition().y - screen->m_O_Curseur.y;
244 
245  dXpos = To_User_Unit( g_UserUnit, dx );
246  dYpos = To_User_Unit( g_UserUnit, dy );
247 
248  if( g_UserUnit == MILLIMETRES )
249  {
250  dXpos = RoundTo0( dXpos, 100.0 );
251  dYpos = RoundTo0( dYpos, 100.0 );
252  }
253 
254  // We already decided the formatter above
255  line.Printf( locformatter, dXpos, dYpos, hypot( dXpos, dYpos ) );
256  SetStatusText( line, 3 );
257 
258  // refresh units display
259  DisplayUnitsMsg();
260 }
261 
262 
263 void SCH_BASE_FRAME::OnEditSymbolLibTable( wxCommandEvent& aEvent )
264 {
266  Prj().SchSymbolLibTable() );
267 
268  if( dlg.ShowModal() == wxID_CANCEL )
269  return;
270 
271  saveSymbolLibTables( true, true );
272 
274 
275  if( this == editor )
276  {
277  // There may be no parent window so use KIWAY message to refresh the schematic editor
278  // in case any symbols have changed.
279  Kiway().ExpressMail( FRAME_SCH, MAIL_SCH_REFRESH, std::string( "" ), this );
280  }
281 
282  LIB_VIEW_FRAME* viewer = (LIB_VIEW_FRAME*) Kiway().Player( FRAME_SCH_VIEWER, false );
283 
284  if( viewer )
285  viewer->ReCreateListLib();
286 }
287 
288 
289 LIB_ALIAS* SCH_BASE_FRAME::GetLibAlias( const LIB_ID& aLibId, bool aUseCacheLib,
290  bool aShowErrorMsg )
291 {
292  // wxCHECK_MSG( aLibId.IsValid(), NULL, "LIB_ID is not valid." );
293 
294  PART_LIB* cache = ( aUseCacheLib ) ? Prj().SchLibs()->GetCacheLibrary() : NULL;
295 
296  return SchGetLibAlias( aLibId, Prj().SchSymbolLibTable(), cache, this, aShowErrorMsg );
297 }
298 
299 
300 LIB_PART* SCH_BASE_FRAME::GetLibPart( const LIB_ID& aLibId, bool aUseCacheLib, bool aShowErrorMsg )
301 {
302  // wxCHECK_MSG( aLibId.IsValid(), NULL, "LIB_ID is not valid." );
303 
304  PART_LIB* cache = ( aUseCacheLib ) ? Prj().SchLibs()->GetCacheLibrary() : NULL;
305 
306  return SchGetLibPart( aLibId, Prj().SchSymbolLibTable(), cache, this, aShowErrorMsg );
307 }
308 
309 
310 bool SCH_BASE_FRAME::saveSymbolLibTables( bool aGlobal, bool aProject )
311 {
312  bool success = true;
313 
314  if( aGlobal )
315  {
316  try
317  {
319 
321  }
322  catch( const IO_ERROR& ioe )
323  {
324  success = false;
325  wxString msg = wxString::Format( _( "Error occurred saving the global symbol library "
326  "table:\n\n%s" ),
327  GetChars( ioe.What().GetData() ) );
328  wxMessageBox( msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
329  }
330  }
331 
332  if( aProject && !Prj().GetProjectName().IsEmpty() )
333  {
334  wxFileName fn( Prj().GetProjectPath(), SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
335 
336  try
337  {
338  Prj().SchSymbolLibTable()->Save( fn.GetFullPath() );
339  }
340  catch( const IO_ERROR& ioe )
341  {
342  success = false;
343  wxString msg = wxString::Format( _( "Error occurred saving project specific "
344  "symbol library table:\n\n%s" ),
345  GetChars( ioe.What() ) );
346  wxMessageBox( msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
347  }
348  }
349 
350  return success;
351 }
LIB_ALIAS * LoadSymbol(const wxString &aNickname, const wxString &aAliasName)
Load a LIB_ALIAS having aAliasName from the library given by aNickname.
static const wxString & GetSymbolLibTableFileName()
int m_repeatDeltaLabel
the increment value of labels like bus members when they are repeated
static SYMBOL_LIB_TABLE & GetGlobalLibTable()
const wxPoint & GetAuxOrigin() const
Part library alias object definition.
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:62
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_player.h:60
virtual void OnEditSymbolLibTable(wxCommandEvent &aEvent)
void OnOpenLibraryViewer(wxCommandEvent &event)
Open the library viewer only to browse library contents.
void SetLayerColor(COLOR4D aColor, SCH_LAYER_ID aLayer)
Definition: eeschema.cpp:174
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:88
virtual const wxString GetZoomLevelIndicator() const
Function GetZoomLevelIndicator returns a human readable value which can be displayed as zoom level in...
Definition: draw_frame.cpp:697
Implementation of conversion functions that require both schematic and board internal units...
This file is part of the common library.
double RoundTo0(double x, double precision)
Round to the nearest precision.
Definition: common.cpp:182
#define DEFAULT_REPEAT_LABEL_INC
the default value (overwritten by the eeschema config)
Definition: general.h:49
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
LIB_ALIAS * SchGetLibAlias(const LIB_ID &aLibId, SYMBOL_LIB_TABLE *aLibTable, PART_LIB *aCacheLib, wxWindow *aParent, bool aShowErrorMsg)
Load symbol from symbol library table.
SCH_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
wxPoint m_repeatStep
the increment value of the position of an item when it is repeated
const wxPoint & GetAuxOrigin() const override
Function GetAuxOrigin returns the origin of the axis used for plotting and various exports...
void SetPageSettings(const PAGE_INFO &aPageSettings)
LIB_PART * GetLibPart(const LIB_ID &aLibId, bool aUseCacheLib=false, bool aShowErrorMsg=false)
void SetAuxOrigin(const wxPoint &aPosition)
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
Class EDA_DRAW_FRAME is the base class for create windows for drawing purpose.
Definition: draw_frame.h:54
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Definition: eeschema.cpp:167
void PushPreferences(const EDA_DRAW_PANEL *aParentCanvas)
Function PushPreferences Pushes a few preferences from a parent window to a child window...
Definition: draw_frame.cpp:826
COLOR4D m_drawBgColor
the background color of the draw canvas BLACK for Pcbnew, BLACK or WHITE for eeschema ...
Definition: draw_frame.h:84
Class TITLE_BLOCK holds the information shown in the lower right corner of a plot, printout, or editing view.
wxPoint m_O_Curseur
Relative Screen cursor coordinate (on grid) in user units.
const wxString GetZoomLevelIndicator() const override
Function GetZoomLevelIndicator returns a human readable value which can be displayed as zoom level in...
Symbol library viewer main window.
Definition: viewlib_frame.h:49
const wxSize GetPageSizeIU() const override
Function GetPageSizeIU works off of GetPageSettings() to return the size of the paper page in the int...
void DisplayUnitsMsg()
Function DisplayUnitsMsg displays current unit pane on the status bar.
Definition: draw_frame.cpp:520
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
wxString wx_str() const
Definition: utf8.cpp:48
virtual BASE_SCREEN * GetScreen() const
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
Definition: draw_frame.h:356
VTBL_ENTRY KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=NULL)
Function Player returns the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:300
void SetAuxOrigin(const wxPoint &aPosition) override
void SetTitleBlock(const TITLE_BLOCK &aTitleBlock) override
const UTF8 & GetLibItemName() const
Definition: lib_id.h:115
Class PAGE_INFO describes the page size and margins of a paper page on which to eventually print or p...
Class BASE_SCREEN handles how to draw a screen (a board, a schematic ...)
Definition: common.h:145
Define a library symbol object.
LIB_ALIAS * FindAlias(const wxString &aName) const
Find LIB_ALIAS by aName.
virtual ~SCH_BASE_FRAME()
Class KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within...
Definition: kiway.h:258
bool saveSymbolLibTables(bool aGlobal, bool aProject)
Saves Symbol Library Tables to disk.
#define DEFAULT_REPEAT_OFFSET_Y
the default Y value (overwritten by the eeschema config)
Definition: general.h:47
virtual void Format(OUTPUTFORMATTER *aOutput, int aIndentLevel) const override
Generate the table in s-expression format to aOutput with an indention level of aIndentLevel.
COLOR4D GetDrawBgColor() const override
void SetTitleBlock(const TITLE_BLOCK &aTitleBlock)
The the schematic editor to refresh the display.
Definition: mail_type.h:47
void SetPageSettings(const PAGE_INFO &aPageSettings) override
const TITLE_BLOCK & GetTitleBlock() const
#define DEFAULT_REPEAT_OFFSET_X
the default X value (overwritten by the eeschema config)
Definition: general.h:46
LIB_PART * GetPart() const
Get the shared LIB_PART.
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
const PAGE_INFO & GetPageSettings() const override
void UpdateStatusBar() override
Function UpdateStatusBar updates the status bar information.
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:93
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
double m_zoomLevelCoeff
a suitable value to convert the internal zoom scaling factor
Definition: draw_frame.h:86
The symbol library editor main window.
Definition: libeditframe.h:59
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
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
VTBL_ENTRY void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, const std::string &aPayload, wxWindow *aSource=NULL)
Function ExpressMail send aPayload to aDestination from aSource.
Definition: kiway.cpp:386
const TITLE_BLOCK & GetTitleBlock() const override
Class FILE_OUTPUTFORMATTER may be used for text file output.
Definition: richio.h:492
void SetDrawBgColor(COLOR4D aColor) override
const PAGE_INFO & GetPageSettings() const
double To_User_Unit(EDA_UNITS_T aUnit, double aValue)
Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit...
Definition: base_units.cpp:91
LIB_PART * SchGetLibPart(const LIB_ID &aLibId, SYMBOL_LIB_TABLE *aLibTable, PART_LIB *aCacheLib, wxWindow *aParent, bool aShowErrorMsg)
Definition for part library class.
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:98
bool ReCreateListLib()
Creates or recreates a sorted list of currently loaded libraries.
LIB_ALIAS * GetLibAlias(const LIB_ID &aLibId, bool aUseCacheLib=false, bool aShowErrorMsg=false)
Load symbol from symbol library table.
Object used to load, save, search, and otherwise manipulate symbol library files. ...
virtual void UpdateStatusBar()
Function UpdateStatusBar updates the status bar information.
Definition: draw_frame.cpp:686
wxPoint GetCrossHairPosition(bool aInvertY=false) const
Function GetCrossHairPosition return the current cross hair position in logical (drawing) coordinates...
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:47
static wxString GetGlobalTableFileName()
Fetch the global symbol library table file name.
Definition of class LIB_EDIT_FRAME.
Dialog to show and edit symbol library tables.
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39