KiCad PCB EDA Suite
footprint_editor_tools.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) 2014-2019 CERN
5  * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include "footprint_editor_tools.h"
27 #include "kicad_clipboard.h"
28 #include <tool/tool_manager.h>
29 #include <tools/pcb_actions.h>
30 #include <view/view_controls.h>
31 #include <footprint_edit_frame.h>
32 #include <pcbnew_id.h>
33 #include <confirm.h>
34 #include <bitmaps.h>
35 #include <class_module.h>
36 #include <class_edge_mod.h>
37 #include <project.h>
38 #include <fp_lib_table.h>
40 
41 
43  PCB_TOOL_BASE( "pcbnew.ModuleEditor" ),
44  m_frame( nullptr )
45 {
46 }
47 
48 
50 {
51 }
52 
53 
55 {
56  m_frame = getEditFrame<FOOTPRINT_EDIT_FRAME>();
57 }
58 
59 
61 {
62  // Build a context menu for the footprint tree
63  //
64  CONDITIONAL_MENU& ctxMenu = m_menu.GetMenu();
65 
66  auto libSelectedCondition = [ this ] ( const SELECTION& aSel ) {
67  LIB_ID sel = m_frame->GetTreeFPID();
68  return !sel.GetLibNickname().empty() && sel.GetLibItemName().empty();
69  };
70  auto pinnedLibSelectedCondition = [ this ] ( const SELECTION& aSel ) {
72  return current && current->m_Type == LIB_TREE_NODE::LIB && current->m_Pinned;
73  };
74  auto unpinnedLibSelectedCondition = [ this ] (const SELECTION& aSel ) {
76  return current && current->m_Type == LIB_TREE_NODE::LIB && !current->m_Pinned;
77  };
78  auto fpSelectedCondition = [ this ] ( const SELECTION& aSel ) {
79  LIB_ID sel = m_frame->GetTreeFPID();
80  return !sel.GetLibNickname().empty() && !sel.GetLibItemName().empty();
81  };
82 
83  ctxMenu.AddItem( ACTIONS::pinLibrary, unpinnedLibSelectedCondition );
84  ctxMenu.AddItem( ACTIONS::unpinLibrary, pinnedLibSelectedCondition );
85  ctxMenu.AddSeparator();
86 
89  ctxMenu.AddItem( ACTIONS::save, libSelectedCondition );
90  ctxMenu.AddItem( ACTIONS::saveAs, libSelectedCondition );
91  ctxMenu.AddItem( ACTIONS::revert, libSelectedCondition );
92 
93  ctxMenu.AddSeparator();
95 #ifdef KICAD_SCRIPTING
97 #endif
98  ctxMenu.AddItem( PCB_ACTIONS::editFootprint, fpSelectedCondition );
99 
100  ctxMenu.AddSeparator();
101  ctxMenu.AddItem( ACTIONS::save, fpSelectedCondition );
102  ctxMenu.AddItem( ACTIONS::saveCopyAs, fpSelectedCondition );
103  ctxMenu.AddItem( PCB_ACTIONS::deleteFootprint, fpSelectedCondition );
104  ctxMenu.AddItem( ACTIONS::revert, fpSelectedCondition );
105 
106  ctxMenu.AddSeparator();
107  ctxMenu.AddItem( PCB_ACTIONS::cutFootprint, fpSelectedCondition );
108  ctxMenu.AddItem( PCB_ACTIONS::copyFootprint, fpSelectedCondition );
110 
111  ctxMenu.AddSeparator();
113  ctxMenu.AddItem( PCB_ACTIONS::exportFootprint, fpSelectedCondition );
114 
115  return true;
116 }
117 
118 
120 {
121  wxCommandEvent evt( wxEVT_NULL, ID_MODEDIT_NEW_MODULE );
122  getEditFrame<FOOTPRINT_EDIT_FRAME>()->Process_Special_Functions( evt );
123  return 0;
124 }
125 
126 
128 {
129  wxCommandEvent evt( wxEVT_NULL, ID_MODEDIT_NEW_MODULE_FROM_WIZARD );
130  getEditFrame<FOOTPRINT_EDIT_FRAME>()->Process_Special_Functions( evt );
131  return 0;
132 }
133 
134 
136 {
137  wxCommandEvent evt( wxEVT_NULL, ID_MODEDIT_SAVE );
138  getEditFrame<FOOTPRINT_EDIT_FRAME>()->Process_Special_Functions( evt );
139  return 0;
140 }
141 
142 
144 {
145  wxCommandEvent evt( wxEVT_NULL, ID_MODEDIT_SAVE_AS );
146  getEditFrame<FOOTPRINT_EDIT_FRAME>()->Process_Special_Functions( evt );
147  return 0;
148 }
149 
150 
152 {
153  getEditFrame<FOOTPRINT_EDIT_FRAME>()->RevertFootprint();
154  return 0;
155 }
156 
157 
159 {
160  LIB_ID fpID = m_frame->GetTreeFPID();
161 
162  if( fpID == m_frame->GetLoadedFPID() )
163  m_copiedModule.reset( new MODULE( *m_frame->GetBoard()->GetFirstModule() ) );
164  else
165  m_copiedModule.reset( m_frame->LoadFootprint( fpID ) );
166 
167  if( aEvent.IsAction( &PCB_ACTIONS::cutFootprint ) )
168  DeleteFootprint(aEvent );
169 
170  return 0;
171 }
172 
173 
175 {
177  {
178  wxString newLib = m_frame->GetTreeFPID().GetLibNickname();
179  MODULE* newModule( m_copiedModule.get() );
180  wxString newName = newModule->GetFPID().GetLibItemName();
181 
182  while( m_frame->Prj().PcbFootprintLibs()->FootprintExists( newLib, newName ) )
183  newName += _( "_copy" );
184 
185  newModule->SetFPID( LIB_ID( newLib, newName ) );
186  m_frame->SaveFootprintInLibrary( newModule, newLib );
187 
188  m_frame->SyncLibraryTree( true );
189  m_frame->FocusOnLibID( newModule->GetFPID() );
190  }
191 
192  return 0;
193 }
194 
195 
197 {
198  FOOTPRINT_EDIT_FRAME* frame = getEditFrame<FOOTPRINT_EDIT_FRAME>();
199 
200  if( frame->DeleteModuleFromLibrary( frame->GetTargetFPID(), true ) )
201  {
202  if( frame->GetTargetFPID() == frame->GetLoadedFPID() )
203  frame->Clear_Pcb( false );
204 
205  frame->SyncLibraryTree( true );
206  }
207 
208  return 0;
209 }
210 
211 
213 {
214  if( !m_frame->Clear_Pcb( true ) )
215  return -1; // this command is aborted
216 
219 
220  if( m_frame->GetBoard()->GetFirstModule() )
222 
224 
226  m_frame->OnModify();
227  return 0;
228 }
229 
230 
232 {
233  LIB_ID fpID = m_frame->GetTreeFPID();
234  MODULE* fp;
235 
236  if( !fpID.IsValid() )
237  fp = m_frame->GetBoard()->GetFirstModule();
238  else
239  fp = m_frame->LoadFootprint( fpID );
240 
241  m_frame->Export_Module( fp );
242  return 0;
243 }
244 
245 
247 {
249  return 0;
250 }
251 
252 
254 {
255  LIB_TREE_NODE* currentNode = m_frame->GetCurrentTreeNode();
256 
257  if( currentNode && !currentNode->m_Pinned )
258  {
259  currentNode->m_Pinned = true;
261  }
262 
263  return 0;
264 }
265 
266 
268 {
269  LIB_TREE_NODE* currentNode = m_frame->GetCurrentTreeNode();
270 
271  if( currentNode && currentNode->m_Pinned )
272  {
273  currentNode->m_Pinned = false;
275  }
276 
277  return 0;
278 }
279 
280 
282 {
284  return 0;
285 }
286 
287 
289 {
290  MODULE* footprint = m_frame->GetBoard()->GetFirstModule();
291 
292  if( footprint )
293  {
294  getEditFrame<FOOTPRINT_EDIT_FRAME>()->OnEditItemRequest( footprint );
295  m_frame->GetCanvas()->Refresh();
296  }
297  return 0;
298 }
299 
300 
302 {
303  getEditFrame<FOOTPRINT_EDIT_FRAME>()->InstallPadOptionsFrame( nullptr );
304  return 0;
305 }
306 
307 
309 {
310  FOOTPRINT_EDIT_FRAME* editFrame = getEditFrame<FOOTPRINT_EDIT_FRAME>();
311  DIALOG_CLEANUP_GRAPHICS dlg( editFrame, true );
312 
313  dlg.ShowModal();
314  return 0;
315 }
316 
317 
319 {
329 
334 
337 
339 
345 }
static bool ShowAlways(const SELECTION &aSelection)
Function ShowAlways The default condition function (always returns true).
static TOOL_ACTION pinLibrary
Definition: actions.h:105
static TOOL_ACTION saveToBoard
Definition: pcb_actions.h:340
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
TOOL_MENU m_menu
functions below are not yet implemented - their interface may change
static TOOL_ACTION footprintProperties
Definition: pcb_actions.h:351
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
int ExportFootprint(const TOOL_EVENT &aEvent)
This file is part of the common library.
VIEW_CONTROLS class definition.
static TOOL_ACTION cleanupGraphics
Definition: pcb_actions.h:300
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
CONDITIONAL_MENU & GetMenu()
Function GetMenu.
Definition: tool_menu.cpp:46
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
int Properties(const TOOL_EVENT &aEvent)
void LoadModuleFromLibrary(LIB_ID aFPID)
static TOOL_ACTION zoomFitScreen
Definition: actions.h:93
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:140
int DeleteFootprint(const TOOL_EVENT &aEvent)
static TOOL_ACTION unpinLibrary
Definition: actions.h:106
int ImportFootprint(const TOOL_EVENT &aEvent)
const LIB_ID & GetFPID() const
Definition: class_module.h:225
int UnpinLibrary(const TOOL_EVENT &aEvent)
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
bool IsValid() const
Definition: lib_id.h:171
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
int CleanupGraphics(const TOOL_EVENT &aEvent)
void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
static TOOL_ACTION createFootprint
Definition: pcb_actions.h:336
int Revert(const TOOL_EVENT &aEvent)
static TOOL_ACTION saveCopyAs
Definition: actions.h:56
static TOOL_ACTION cutFootprint
Definition: pcb_actions.h:345
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
static TOOL_ACTION copyFootprint
Definition: pcb_actions.h:346
PCB_BASE_EDIT_FRAME * frame() const
int Save(const TOOL_EVENT &aEvent)
void SyncLibraryTree(bool aProgress)
Synchronize the footprint library tree to the current state of the footprint library table.
void RegenerateLibraryTree()
Filter, sort, and redisplay the library tree.
bool Clear_Pcb(bool aQuery)
Delete all and reinitialize the current board.
Definition: initpcb.cpp:94
static TOOL_ACTION toggleFootprintTree
Definition: pcb_actions.h:329
int CutCopyFootprint(const TOOL_EVENT &aEvent)
std::unique_ptr< MODULE > m_copiedModule
VECTOR2< double > VECTOR2D
Definition: vector2d.h:593
void OnModify() override
Must be called after a footprint change in order to set the "modify" flag of the current screen and p...
FOOTPRINT_EDIT_FRAME * m_frame
static TOOL_ACTION exportFootprint
Definition: pcb_actions.h:349
void FocusOnLibID(const LIB_ID &aLibID)
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
static TOOL_ACTION editFootprint
Definition: pcb_actions.h:343
static TOOL_ACTION save
Definition: actions.h:54
TOOL_EVENT.
Definition: tool_event.h:171
void setTransitions() override
Sets up handlers for various events.
MODULE * GetFirstModule() const
Gets the first module in the list (used in footprint viewer/editor) or NULL if none.
Definition: class_board.h:330
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
int NewFootprint(const TOOL_EVENT &aEvent)
int PinLibrary(const TOOL_EVENT &aEvent)
virtual void SetCrossHairCursorPosition(const VECTOR2D &aPosition, bool aWarpView=true)=0
Moves the graphic crosshair cursor to the requested position expressed in world coordinates.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
static TOOL_ACTION newLibrary
Definition: actions.h:51
LIB_TREE_NODE * GetCurrentTreeNode() const
static TOOL_ACTION importFootprint
Definition: pcb_actions.h:348
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...
int PasteFootprint(const TOOL_EVENT &aEvent)
void Export_Module(MODULE *aModule)
Create a file containing only one footprint.
void AddSeparator(int aOrder=ANY_ORDER)
Function AddSeparator()
VTBL_ENTRY FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
Definition: project.cpp:291
bool SaveFootprintInLibrary(MODULE *aModule, const wxString &aLibraryName)
static TOOL_ACTION saveToLibrary
Definition: pcb_actions.h:341
int SaveAs(const TOOL_EVENT &aEvent)
static TOOL_ACTION defaultPadProperties
Definition: pcb_actions.h:352
MODULE * LoadFootprint(const LIB_ID &aFootprintId)
Function LoadFootprint attempts to load aFootprintId from the footprint library table.
static TOOL_ACTION saveAs
Definition: actions.h:55
LIB_ID GetLoadedFPID() const
Return the LIB_ID of the part being edited.
#define _(s)
Definition: 3d_actions.cpp:33
enum TYPE m_Type
int CreateFootprint(const TOOL_EVENT &aEvent)
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:79
int DefaultPadProperties(const TOOL_EVENT &aEvent)
Edit the properties used for new pad creation.
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:233
int ToggleFootprintTree(const TOOL_EVENT &aEvent)
static TOOL_ACTION pasteFootprint
Definition: pcb_actions.h:347
BOARD * GetBoard() const
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:42
EDGE_MODULE class definition.
MODULE * Import_Module(const wxString &aName=wxT(""))
Read a file containing only one footprint.
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Function AddItem()
virtual void ClearUndoRedoList()
Function ClearUndoRedoList clear undo and redo list, using ClearUndoORRedoList() picked items are del...
static TOOL_ACTION deleteFootprint
Definition: pcb_actions.h:344
bool FootprintExists(const wxString &aNickname, const wxString &aFootprintName)
Function FootprintExists.
LIB_ID GetTreeFPID() const
Return the LIB_ID of the part or library selected in the footprint tree.
static TOOL_ACTION revert
Definition: actions.h:58
static TOOL_ACTION newFootprint
Definition: pcb_actions.h:333
int EditFootprint(const TOOL_EVENT &aEvent)
bool empty() const
Definition: utf8.h:108