KiCad PCB EDA Suite
lib_control.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) 2019 CERN
5  * Copyright (C) 2019 KiCad Developers, see AUTHORS.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 <fctsys.h>
26 #include <kiway.h>
27 #include <sch_painter.h>
28 #include <tool/tool_manager.h>
29 #include <tools/ee_actions.h>
30 #include <tools/lib_control.h>
31 #include <lib_edit_frame.h>
32 #include <lib_view_frame.h>
34 #include <gestfich.h>
35 #include <project.h>
36 #include <confirm.h>
37 
38 
40 {
41  m_frame = getEditFrame<SCH_BASE_FRAME>();
44 
45  if( m_isLibEdit )
46  {
47  CONDITIONAL_MENU& ctxMenu = m_menu.GetMenu();
48  LIB_EDIT_FRAME* editFrame = getEditFrame<LIB_EDIT_FRAME>();
49 
50  auto libSelectedCondition = [ editFrame ] ( const SELECTION& aSel ) {
51  LIB_ID sel = editFrame->GetTreeLIBID();
52  return !sel.GetLibNickname().empty() && sel.GetLibItemName().empty();
53  };
54  auto symbolSelectedCondition = [ editFrame ] ( const SELECTION& aSel ) {
55  LIB_ID sel = editFrame->GetTreeLIBID();
56  return !sel.GetLibNickname().empty() && !sel.GetLibItemName().empty();
57  };
58 
61  ctxMenu.AddItem( ACTIONS::save, libSelectedCondition );
62  ctxMenu.AddItem( ACTIONS::saveAs, libSelectedCondition );
63  ctxMenu.AddItem( ACTIONS::revert, libSelectedCondition );
64 
65  ctxMenu.AddSeparator();
67  ctxMenu.AddItem( EE_ACTIONS::editSymbol, symbolSelectedCondition );
68 
69  ctxMenu.AddSeparator();
70  ctxMenu.AddItem( ACTIONS::save, symbolSelectedCondition );
71  ctxMenu.AddItem( ACTIONS::saveCopyAs, symbolSelectedCondition );
72  ctxMenu.AddItem( EE_ACTIONS::duplicateSymbol, symbolSelectedCondition );
73  ctxMenu.AddItem( EE_ACTIONS::deleteSymbol, symbolSelectedCondition );
74  ctxMenu.AddItem( ACTIONS::revert, symbolSelectedCondition );
75 
76  ctxMenu.AddSeparator();
77  ctxMenu.AddItem( EE_ACTIONS::cutSymbol, symbolSelectedCondition );
78  ctxMenu.AddItem( EE_ACTIONS::copySymbol, symbolSelectedCondition );
80 
81  ctxMenu.AddSeparator();
83  ctxMenu.AddItem( EE_ACTIONS::exportSymbol, symbolSelectedCondition );
84  }
85 
86  return true;
87 }
88 
89 
90 int LIB_CONTROL::AddLibrary( const TOOL_EVENT& aEvent )
91 {
92  bool createNew = aEvent.IsAction( &ACTIONS::newLibrary );
93 
95  static_cast<LIB_EDIT_FRAME*>( m_frame )->AddLibraryFile( createNew );
96 
97  return 0;
98 }
99 
100 
102 {
104  {
105  LIB_EDIT_FRAME* editFrame = static_cast<LIB_EDIT_FRAME*>( m_frame );
106  int unit = 0;
107  LIB_ID partId = editFrame->GetTreeLIBID( &unit );
108 
109  editFrame->LoadPart( partId.GetLibItemName(), partId.GetLibNickname(), unit );
110  }
111 
112  return 0;
113 }
114 
115 
116 int LIB_CONTROL::AddSymbol( const TOOL_EVENT& aEvent )
117 {
119  {
120  LIB_EDIT_FRAME* editFrame = static_cast<LIB_EDIT_FRAME*>( m_frame );
121 
122  if( aEvent.IsAction( &EE_ACTIONS::newSymbol ) )
123  editFrame->CreateNewPart();
124  else if( aEvent.IsAction( &EE_ACTIONS::importSymbol ) )
125  editFrame->ImportPart();
126  }
127 
128  return 0;
129 }
130 
131 
132 int LIB_CONTROL::Save( const TOOL_EVENT& aEvt )
133 {
135  {
136  LIB_EDIT_FRAME* editFrame = static_cast<LIB_EDIT_FRAME*>( m_frame );
137 
138  if( aEvt.IsAction( &EE_ACTIONS::save ) )
139  editFrame->Save();
140  else if( aEvt.IsAction( &EE_ACTIONS::saveAs ) || aEvt.IsAction( &EE_ACTIONS::saveCopyAs ) )
141  editFrame->SaveAs();
142  else if( aEvt.IsAction( &EE_ACTIONS::saveAll ) )
143  editFrame->SaveAll();
144  }
145 
146  return 0;
147 }
148 
149 
150 int LIB_CONTROL::Revert( const TOOL_EVENT& aEvent )
151 {
153  static_cast<LIB_EDIT_FRAME*>( m_frame )->Revert();
154 
155  return 0;
156 }
157 
158 
160 {
162  static_cast<LIB_EDIT_FRAME*>( m_frame )->ExportPart();
163 
164  return 0;
165 }
166 
167 
169 {
171  {
172  LIB_EDIT_FRAME* editFrame = static_cast<LIB_EDIT_FRAME*>( m_frame );
173 
175  editFrame->CopyPartToClipboard();
176 
178  editFrame->DeletePartFromLibrary();
179  }
180 
181  return 0;
182 }
183 
184 
186 {
188  {
189  LIB_EDIT_FRAME* editFrame = static_cast<LIB_EDIT_FRAME*>( m_frame );
190  editFrame->DuplicatePart( aEvent.IsAction( &EE_ACTIONS::pasteSymbol ) );
191  }
192 
193  return 0;
194 }
195 
196 
198 {
199  int convert = aEvent.IsAction( &EE_ACTIONS::showDeMorganStandard ) ?
200  LIB_ITEM::LIB_CONVERT::BASE : LIB_ITEM::LIB_CONVERT::DEMORGAN;
201 
203  {
206 
207  LIB_EDIT_FRAME* libEditFrame = static_cast<LIB_EDIT_FRAME*>( m_frame );
208  libEditFrame->SetConvert( convert );
209 
211  libEditFrame->RebuildView();
212  }
214  {
215  LIB_VIEW_FRAME* libViewFrame = static_cast<LIB_VIEW_FRAME*>( m_frame );
216  libViewFrame->SetUnitAndConvert( libViewFrame->GetUnit(), convert );
217  }
218 
219  return 0;
220 }
221 
222 
224 {
226  {
227  wxCommandEvent dummy;
228  static_cast<LIB_EDIT_FRAME*>( m_frame )->OnToggleSearchTree( dummy );
229  }
230 
231  return 0;
232 }
233 
234 
236 {
238 
239  // Update canvas
242  m_frame->GetCanvas()->Refresh();
243 
244  return 0;
245 }
246 
247 
249 {
250  if( !m_isLibEdit )
251  return 0;
252 
253  LIB_EDIT_FRAME* editFrame = getEditFrame<LIB_EDIT_FRAME>();
254  editFrame->m_SyncPinEdit = !editFrame->m_SyncPinEdit;
255 
256  return 0;
257 }
258 
259 
261 {
262  if( !m_isLibEdit )
263  return 0;
264 
265  LIB_EDIT_FRAME* editFrame = getEditFrame<LIB_EDIT_FRAME>();
266  LIB_PART* part = editFrame->GetCurPart();
267 
268  if( !part )
269  {
270  wxMessageBox( _( "No symbol to export" ) );
271  return 0;
272  }
273 
274  wxString file_ext = wxT( "png" );
275  wxString mask = wxT( "*." ) + file_ext;
276  wxFileName fn( part->GetName() );
277  fn.SetExt( "png" );
278 
279  wxString projectPath = wxPathOnly( m_frame->Prj().GetProjectFullName() );
280 
281  wxFileDialog dlg( editFrame, _( "Image File Name" ), projectPath, fn.GetFullName(),
282  PngFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
283 
284  if( dlg.ShowModal() == wxID_OK && !dlg.GetPath().IsEmpty() )
285  {
286  // calling wxYield is mandatory under Linux, after closing the file selector dialog
287  // to refresh the screen before creating the PNG or JPEG image from screen
288  wxYield();
289 
290  if( !SaveCanvasImageToFile( editFrame, dlg.GetPath(), wxBITMAP_TYPE_PNG ) )
291  {
292  wxMessageBox( wxString::Format( _( "Can't save file \"%s\"." ), dlg.GetPath() ) );
293  }
294  }
295 
296  return 0;
297 }
298 
299 
301 {
302  if( !m_isLibEdit )
303  return 0;
304 
305  LIB_EDIT_FRAME* editFrame = getEditFrame<LIB_EDIT_FRAME>();
306  LIB_PART* part = editFrame->GetCurPart();
307 
308  if( !part )
309  {
310  wxMessageBox( _( "No symbol to export" ) );
311  return 0;
312  }
313 
314  wxString file_ext = wxT( "svg" );
315  wxString mask = wxT( "*." ) + file_ext;
316  wxFileName fn( part->GetName() );
317  fn.SetExt( file_ext );
318 
319  wxString pro_dir = wxPathOnly( m_frame->Prj().GetProjectFullName() );
320 
321  wxString fullFileName = EDA_FILE_SELECTOR( _( "Filename:" ), pro_dir, fn.GetFullName(),
322  file_ext, mask, m_frame, wxFD_SAVE, true );
323 
324  if( !fullFileName.IsEmpty() )
325  {
326  PAGE_INFO pageSave = editFrame->GetScreen()->GetPageSettings();
327  PAGE_INFO pageTemp = pageSave;
328 
329  wxSize componentSize = part->GetUnitBoundingBox( editFrame->GetUnit(),
330  editFrame->GetConvert() ).GetSize();
331 
332  // Add a small margin to the plot bounding box
333  pageTemp.SetWidthMils( int( componentSize.x * 1.2 ) );
334  pageTemp.SetHeightMils( int( componentSize.y * 1.2 ) );
335 
336  editFrame->GetScreen()->SetPageSettings( pageTemp );
337  editFrame->SVG_PlotComponent( fullFileName );
338  editFrame->GetScreen()->SetPageSettings( pageSave );
339  }
340 
341  return 0;
342 }
343 
344 
346 {
347  LIB_PART* part = nullptr;
348  LIB_ID libId;
349  int unit, convert;
350 
351  if( m_isLibEdit )
352  {
353  LIB_EDIT_FRAME* editFrame = getEditFrame<LIB_EDIT_FRAME>();
354 
355  part = editFrame->GetCurPart();
356  unit = editFrame->GetUnit();
357  convert = editFrame->GetConvert();
358 
359  if( part )
360  libId = part->GetLibId();
361  }
362  else
363  {
364  LIB_VIEW_FRAME* viewFrame = getEditFrame<LIB_VIEW_FRAME>();
365 
366  if( viewFrame->IsModal() )
367  {
368  // if we're modal then we just need to return the symbol selection; the caller is
369  // already in a EE_ACTIONS::placeSymbol coroutine.
370  viewFrame->FinishModal();
371  return 0;
372  }
373  else
374  {
375  part = viewFrame->GetSelectedSymbol();
376  unit = viewFrame->GetUnit();
377  convert = viewFrame->GetConvert();
378 
379  if( part )
380  libId = part->GetLibId();
381  }
382  }
383 
384  if( part )
385  {
386  SCH_EDIT_FRAME* schframe = (SCH_EDIT_FRAME*) m_frame->Kiway().Player( FRAME_SCH, false );
387 
388  if( !schframe ) // happens when the schematic editor is not active (or closed)
389  {
390  DisplayErrorMessage( m_frame, _( "No schematic currently open." ) );
391  return 0;
392  }
393 
394  SCH_COMPONENT* comp = new SCH_COMPONENT( *part, libId, g_CurrentSheet, unit, convert );
395 
396  // Be sure the link to the corresponding LIB_PART is OK:
397  comp->Resolve( *m_frame->Prj().SchSymbolLibTable() );
398 
399  if( schframe->GetAutoplaceFields() )
400  comp->AutoplaceFields( /* aScreen */ nullptr, /* aManual */ false );
401 
402  schframe->Raise();
403  schframe->GetToolManager()->RunAction( EE_ACTIONS::placeSymbol, true, comp );
404  }
405 
406  return 0;
407 }
408 
409 
411 {
417 
418  Go( &LIB_CONTROL::Save, ACTIONS::save.MakeEvent() );
419  Go( &LIB_CONTROL::Save, ACTIONS::saveAs.MakeEvent() ); // for libraries
420  Go( &LIB_CONTROL::Save, ACTIONS::saveCopyAs.MakeEvent() ); // for symbols
421  Go( &LIB_CONTROL::Save, ACTIONS::saveAll.MakeEvent() );
422  Go( &LIB_CONTROL::Revert, ACTIONS::revert.MakeEvent() );
423 
433 
436 
440 }
static bool ShowAlways(const SELECTION &aSelection)
Function ShowAlways The default condition function (always returns true).
void SVG_PlotComponent(const wxString &aFullFileName)
Creates the SVG print file for the current edited component.
KIGFX::SCH_VIEW * GetView() const override
Function GetView() Returns a pointer to the VIEW instance used in the panel.
void DuplicatePart(bool aFromClipboard)
Inserts a duplicate part.
int AddLibrary(const TOOL_EVENT &aEvent)
Definition: lib_control.cpp:90
LIB_PART * GetSelectedSymbol() const
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
TOOL_MENU m_menu
functions below are not yet implemented - their interface may change
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_holder.h:56
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
int EditSymbol(const TOOL_EVENT &aEvent)
LIB_ID GetLibId() const override
void SetUnitAndConvert(int aUnit, int aConvert)
Set unit and convert, and set flag preventing them from automatically resetting to 1.
static TOOL_ACTION pasteSymbol
Definition: ee_actions.h:171
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:249
This file is part of the common library TODO brief description.
int GetConvert() const
wxString GetName() const override
bool SaveCanvasImageToFile(EDA_DRAW_FRAME *aFrame, const wxString &aFileName, wxBitmapType aBitmapType)
Save the current view as an image file.
Definition: bitmap.cpp:195
Model changes (required full reload)
Definition: tool_base.h:82
This file is part of the common library.
int ToggleSyncedPinsMode(const TOOL_EVENT &aEvent)
static TOOL_ACTION duplicateSymbol
Definition: ee_actions.h:167
int AddSymbol(const TOOL_EVENT &aEvent)
bool Resolve(PART_LIBS *aLibs)
Assigns the current LIB_PART from aLibs which this symbol is based on.
void LoadPart(const wxString &aLibrary, const wxString &aPart, int Unit)
int ShowElectricalTypes(const TOOL_EVENT &aEvent)
CONDITIONAL_MENU & GetMenu()
Function GetMenu.
Definition: tool_menu.cpp:46
int GetUnit() const
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
static TOOL_ACTION cancelInteractive
Definition: actions.h:65
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.
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:109
void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: sch_screen.h:153
KIGFX::SCH_RENDER_SETTINGS * GetRenderSettings()
int ExportView(const TOOL_EVENT &aEvent)
Schematic editor (Eeschema) main window.
void SetShowElectricalType(bool aShow)
LIB_PART * GetCurPart()
Return the current part being edited or NULL if none selected.
static TOOL_ACTION showComponentTree
Definition: ee_actions.h:191
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
int ExportSymbol(const TOOL_EVENT &aEvent)
bool GetAutoplaceFields() const
static TOOL_ACTION showDeMorganAlternate
Definition: ee_actions.h:129
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:152
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
Symbol library viewer main window.
int GetConvert() const
static TOOL_ACTION saveCopyAs
Definition: actions.h:56
bool IsAction(const TOOL_ACTION *aAction) const
Function IsAction() Tests if the event contains an action issued upon activation of the given TOOL_AC...
Definition: tool_event.cpp:67
void Save()
Saves the selected part or library.
LIB_ID GetTreeLIBID(int *aUnit=nullptr) const
Return the LIB_ID of the library or symbol selected in the symbol tree.
void SaveAs()
Saves the selected part or library to a new name and/or location.
static TOOL_ACTION editSymbol
Definition: ee_actions.h:166
Item is being added to the view.
Definition: view_item.h:60
int GetUnit() const
SCH_SHEET_PATH * g_CurrentSheet
With the new connectivity algorithm, many more places than before want to know what the current sheet...
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:341
void SaveAll()
Saves all modified parts and libraries.
PAGE_INFO describes the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:54
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
static TOOL_ACTION save
Definition: actions.h:54
int Save(const TOOL_EVENT &aEvt)
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
TOOL_EVENT.
Definition: tool_event.h:171
wxString EDA_FILE_SELECTOR(const wxString &aTitle, const wxString &aPath, const wxString &aFileName, const wxString &aExtension, const wxString &aWildcard, wxWindow *aParent, int aStyle, const bool aKeepWorkingDirectory, const wxPoint &aPosition, wxString *aMruPath)
Function EDA_FILE_SELECTOR.
Definition: gestfich.cpp:52
static TOOL_ACTION deleteSymbol
Definition: ee_actions.h:168
Define a library symbol object.
static TOOL_ACTION cutSymbol
Definition: ee_actions.h:169
Definition of file extensions used in Kicad.
void ResetTools(TOOL_BASE::RESET_REASON aReason)
Function ResetTools() Resets all tools (i.e.
static TOOL_ACTION addLibrary
Definition: actions.h:52
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
EE_SELECTION_TOOL * m_selectionTool
Definition: ee_tool_base.h:153
static TOOL_ACTION importSymbol
Definition: ee_actions.h:172
static TOOL_ACTION clearSelection
Clears the current selection.
Definition: ee_actions.h:58
static TOOL_ACTION placeSymbol
Definition: ee_actions.h:79
VTBL_ENTRY const wxString GetProjectFullName() const
Function GetProjectFullName returns the full path and name of the project.
Definition: project.cpp:96
static TOOL_ACTION addSymbolToSchematic
Definition: ee_actions.h:162
int AddSymbolToSchematic(const TOOL_EVENT &aEvent)
bool IsModal()
Definition: kiway_player.h:163
void setTransitions() override
Sets up handlers for various events.
static TOOL_ACTION newLibrary
Definition: actions.h:51
static TOOL_ACTION showDeMorganStandard
Definition: ee_actions.h:128
static TOOL_ACTION showElectricalTypes
Definition: ee_actions.h:190
void SetHeightMils(int aHeightInMils)
Definition: page_info.cpp:253
static TOOL_ACTION exportSymbolView
Definition: ee_actions.h:194
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...
wxString PngFileWildcard()
void AddSeparator(int aOrder=ANY_ORDER)
Function AddSeparator()
void ImportPart()
Definition: lib_export.cpp:37
static TOOL_ACTION newSymbol
Definition: ee_actions.h:165
static TOOL_ACTION exportSymbol
Definition: ee_actions.h:173
static TOOL_ACTION copySymbol
Definition: ee_actions.h:170
int CutCopyDelete(const TOOL_EVENT &aEvent)
int Revert(const TOOL_EVENT &aEvent)
The symbol library editor main window.
bool m_SyncPinEdit
Set to true to synchronize pins at the same position when editing symbols with multiple units or mult...
void AutoplaceFields(SCH_SCREEN *aScreen, bool aManual)
Automatically orient all the fields in the component.
bool IsType(FRAME_T aType) const
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
static TOOL_ACTION saveAs
Definition: actions.h:55
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
#define _(s)
Definition: 3d_actions.cpp:31
void FinishModal()
Send the selected symbol back to the caller.
void SetWidthMils(int aWidthInMils)
Definition: page_info.cpp:239
const EDA_RECT GetUnitBoundingBox(int aUnit, int aConvert) const
Get the bounding box for the symbol.
void CreateNewPart()
Creates a new part in the selected library.
void SetConvert(int aConvert)
SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:89
static TOOL_ACTION toggleSyncedPinsMode
Definition: ee_actions.h:184
int ExportSymbolAsSVG(const TOOL_EVENT &aEvent)
static TOOL_ACTION saveAll
Definition: actions.h:57
int DuplicateSymbol(const TOOL_EVENT &aEvent)
static TOOL_ACTION exportSymbolAsSVG
Definition: ee_actions.h:195
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Function AddItem()
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
Definition: lib_control.cpp:39
int ShowComponentTree(const TOOL_EVENT &aEvent)
static TOOL_ACTION revert
Definition: actions.h:58
int OnDeMorgan(const TOOL_EVENT &aEvent)
bool GetShowElectricalType()
Allow some frames to show/hide pin electrical type names.
bool empty() const
Definition: utf8.h:108