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 <project.h>
37 #include <fp_lib_table.h>
39 
40 
42  PCB_TOOL_BASE( "pcbnew.ModuleEditor" ),
43  m_frame( nullptr )
44 {
45 }
46 
47 
49 {
50 }
51 
52 
54 {
55  m_frame = getEditFrame<FOOTPRINT_EDIT_FRAME>();
56 }
57 
58 
60 {
61  // Build a context menu for the footprint tree
62  //
63  CONDITIONAL_MENU& ctxMenu = m_menu.GetMenu();
64 
65  auto libSelectedCondition = [ this ] ( const SELECTION& aSel ) {
66  LIB_ID sel = m_frame->GetTreeFPID();
67  return !sel.GetLibNickname().empty() && sel.GetLibItemName().empty();
68  };
69  auto pinnedLibSelectedCondition = [ this ] ( const SELECTION& aSel ) {
71  return current && current->m_Type == LIB_TREE_NODE::LIB && current->m_Pinned;
72  };
73  auto unpinnedLibSelectedCondition = [ this ] (const SELECTION& aSel ) {
75  return current && current->m_Type == LIB_TREE_NODE::LIB && !current->m_Pinned;
76  };
77  auto fpSelectedCondition = [ this ] ( const SELECTION& aSel ) {
78  LIB_ID sel = m_frame->GetTreeFPID();
79  return !sel.GetLibNickname().empty() && !sel.GetLibItemName().empty();
80  };
81 
82  ctxMenu.AddItem( ACTIONS::pinLibrary, unpinnedLibSelectedCondition );
83  ctxMenu.AddItem( ACTIONS::unpinLibrary, pinnedLibSelectedCondition );
84  ctxMenu.AddSeparator();
85 
88  ctxMenu.AddItem( ACTIONS::save, libSelectedCondition );
89  ctxMenu.AddItem( ACTIONS::saveAs, libSelectedCondition );
90  ctxMenu.AddItem( ACTIONS::revert, libSelectedCondition );
91 
92  ctxMenu.AddSeparator();
94 #ifdef KICAD_SCRIPTING
96 #endif
97  ctxMenu.AddItem( PCB_ACTIONS::editFootprint, fpSelectedCondition );
98 
99  ctxMenu.AddSeparator();
100  ctxMenu.AddItem( ACTIONS::save, fpSelectedCondition );
101  ctxMenu.AddItem( ACTIONS::saveCopyAs, fpSelectedCondition );
102  ctxMenu.AddItem( PCB_ACTIONS::deleteFootprint, fpSelectedCondition );
103  ctxMenu.AddItem( ACTIONS::revert, fpSelectedCondition );
104 
105  ctxMenu.AddSeparator();
106  ctxMenu.AddItem( PCB_ACTIONS::cutFootprint, fpSelectedCondition );
107  ctxMenu.AddItem( PCB_ACTIONS::copyFootprint, fpSelectedCondition );
109 
110  ctxMenu.AddSeparator();
112  ctxMenu.AddItem( PCB_ACTIONS::exportFootprint, fpSelectedCondition );
113 
114  return true;
115 }
116 
117 
119 {
120  wxCommandEvent evt( wxEVT_NULL, ID_MODEDIT_NEW_MODULE );
121  getEditFrame<FOOTPRINT_EDIT_FRAME>()->Process_Special_Functions( evt );
122  return 0;
123 }
124 
125 
127 {
128  wxCommandEvent evt( wxEVT_NULL, ID_MODEDIT_NEW_MODULE_FROM_WIZARD );
129  getEditFrame<FOOTPRINT_EDIT_FRAME>()->Process_Special_Functions( evt );
130  return 0;
131 }
132 
133 
135 {
136  wxCommandEvent evt( wxEVT_NULL, ID_MODEDIT_SAVE );
137  getEditFrame<FOOTPRINT_EDIT_FRAME>()->Process_Special_Functions( evt );
138  return 0;
139 }
140 
141 
143 {
144  wxCommandEvent evt( wxEVT_NULL, ID_MODEDIT_SAVE_AS );
145  getEditFrame<FOOTPRINT_EDIT_FRAME>()->Process_Special_Functions( evt );
146  return 0;
147 }
148 
149 
151 {
152  getEditFrame<FOOTPRINT_EDIT_FRAME>()->RevertFootprint();
153  return 0;
154 }
155 
156 
158 {
159  LIB_ID fpID = m_frame->GetTreeFPID();
160 
161  if( fpID == m_frame->GetLoadedFPID() )
162  m_copiedModule.reset( new MODULE( *m_frame->GetBoard()->GetFirstModule() ) );
163  else
164  m_copiedModule.reset( m_frame->LoadFootprint( fpID ) );
165 
166  if( aEvent.IsAction( &PCB_ACTIONS::cutFootprint ) )
167  DeleteFootprint(aEvent );
168 
169  return 0;
170 }
171 
172 
174 {
176  {
177  wxString newLib = m_frame->GetTreeFPID().GetLibNickname();
178  MODULE* newModule( m_copiedModule.get() );
179  wxString newName = newModule->GetFPID().GetLibItemName();
180 
181  while( m_frame->Prj().PcbFootprintLibs()->FootprintExists( newLib, newName ) )
182  newName += _( "_copy" );
183 
184  newModule->SetFPID( LIB_ID( newLib, newName ) );
185  m_frame->SaveFootprintInLibrary( newModule, newLib );
186 
187  m_frame->SyncLibraryTree( true );
188  m_frame->FocusOnLibID( newModule->GetFPID() );
189  }
190 
191  return 0;
192 }
193 
194 
196 {
197  FOOTPRINT_EDIT_FRAME* frame = getEditFrame<FOOTPRINT_EDIT_FRAME>();
198 
199  if( frame->DeleteModuleFromLibrary( frame->GetTargetFPID(), true ) )
200  {
201  if( frame->GetTargetFPID() == frame->GetLoadedFPID() )
202  frame->Clear_Pcb( false );
203 
204  frame->SyncLibraryTree( true );
205  }
206 
207  return 0;
208 }
209 
210 
212 {
213  if( !m_frame->Clear_Pcb( true ) )
214  return -1; // this command is aborted
215 
218 
219  if( m_frame->GetBoard()->GetFirstModule() )
221 
223 
225  m_frame->OnModify();
226  return 0;
227 }
228 
229 
231 {
232  LIB_ID fpID = m_frame->GetTreeFPID();
233  MODULE* fp;
234 
235  if( !fpID.IsValid() )
236  fp = m_frame->GetBoard()->GetFirstModule();
237  else
238  fp = m_frame->LoadFootprint( fpID );
239 
240  m_frame->Export_Module( fp );
241  return 0;
242 }
243 
244 
246 {
248  return 0;
249 }
250 
251 
253 {
254  LIB_TREE_NODE* currentNode = m_frame->GetCurrentTreeNode();
255 
256  if( currentNode && !currentNode->m_Pinned )
257  {
258  currentNode->m_Pinned = true;
260  }
261 
262  return 0;
263 }
264 
265 
267 {
268  LIB_TREE_NODE* currentNode = m_frame->GetCurrentTreeNode();
269 
270  if( currentNode && currentNode->m_Pinned )
271  {
272  currentNode->m_Pinned = false;
274  }
275 
276  return 0;
277 }
278 
279 
281 {
283  return 0;
284 }
285 
286 
288 {
289  MODULE* footprint = m_frame->GetBoard()->GetFirstModule();
290 
291  if( footprint )
292  {
293  getEditFrame<FOOTPRINT_EDIT_FRAME>()->OnEditItemRequest( footprint );
294  m_frame->GetCanvas()->Refresh();
295  }
296  return 0;
297 }
298 
299 
301 {
302  getEditFrame<FOOTPRINT_EDIT_FRAME>()->ShowPadPropertiesDialog( nullptr );
303  return 0;
304 }
305 
306 
308 {
309  FOOTPRINT_EDIT_FRAME* editFrame = getEditFrame<FOOTPRINT_EDIT_FRAME>();
310  DIALOG_CLEANUP_GRAPHICS dlg( editFrame, true );
311 
312  dlg.ShowModal();
313  return 0;
314 }
315 
316 
318 {
328 
333 
336 
338 
344 }
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
static TOOL_ACTION pinLibrary
Definition: actions.h:107
static TOOL_ACTION saveToBoard
Definition: pcb_actions.h:357
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:368
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:317
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:94
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:141
int DeleteFootprint(const TOOL_EVENT &aEvent)
static TOOL_ACTION unpinLibrary
Definition: actions.h:108
int ImportFootprint(const TOOL_EVENT &aEvent)
const LIB_ID & GetFPID() const
Definition: class_module.h:210
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:353
int Revert(const TOOL_EVENT &aEvent)
static TOOL_ACTION saveCopyAs
Definition: actions.h:56
static TOOL_ACTION cutFootprint
Definition: pcb_actions.h:362
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:363
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:346
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:366
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:360
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:347
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:365
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)
Adds a separator to the menu.
VTBL_ENTRY FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
Definition: project.cpp:283
bool SaveFootprintInLibrary(MODULE *aModule, const wxString &aLibraryName)
static TOOL_ACTION saveToLibrary
Definition: pcb_actions.h:358
int SaveAs(const TOOL_EVENT &aEvent)
static TOOL_ACTION defaultPadProperties
Definition: pcb_actions.h:369
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: eda_item.h:222
int ToggleFootprintTree(const TOOL_EVENT &aEvent)
static TOOL_ACTION pasteFootprint
Definition: pcb_actions.h:364
BOARD * GetBoard() const
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:42
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)
Adds a menu entry to run a TOOL_ACTION on selected items.
virtual void ClearUndoRedoList()
Function ClearUndoRedoList clear undo and redo list, using ClearUndoORRedoList() picked items are del...
static TOOL_ACTION deleteFootprint
Definition: pcb_actions.h:361
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:350
int EditFootprint(const TOOL_EVENT &aEvent)
bool empty() const
Definition: utf8.h:108