KiCad PCB EDA Suite
cvpcb_association_tool.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 Ian McInerney <Ian.S.McInerney@ieee.org>
5  * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <confirm.h>
22 #include <cstdint>
23 #include <functional>
24 #include <kiface_i.h>
25 #include <kiway_express.h>
26 #include <lib_id.h>
27 #include <tool/actions.h>
28 #include <tool/tool_manager.h>
29 #include <wx/clipbrd.h>
30 
31 #include <cvpcb_association.h>
32 #include <cvpcb_mainframe.h>
35 #include <listboxes.h>
36 #include <tools/cvpcb_actions.h>
38 
39 using namespace std::placeholders;
40 
41 
43  TOOL_INTERACTIVE( "cvpcb.Association" ),
44  m_frame( nullptr )
45 {
46 }
47 
48 
50 {
51  COMPONENT* comp;
52  LIB_ID fpid;
53 
54  // Default to using the component control as the source of the copy
56 
57  // If using the keyboard to copy, find out what control we are in and use that.
58  if( aEvent.HasPosition() )
59  copyControl = m_frame->GetFocusedControl();
60 
61  switch( copyControl )
62  {
65  break;
66 
68  // Get the selection
69  comp = m_frame->GetSelectedComponent();
70 
71  if( !comp )
72  return 0;
73 
74  // Get the fpid and save it to the clipboard
75  fpid = comp->GetFPID();
76  break;
77 
78  default:
79  // Do nothing
80  break;
81  }
82 
83  // if no valid fpid, then skip
84  if( !fpid.IsValid() )
85  return 0;
86 
87  if( wxTheClipboard->Open() )
88  {
89  if( !wxTheClipboard->SetData( new wxTextDataObject( fpid.GetUniStringLibId() ) ) )
90  wxLogDebug( "Failed to copy data to clipboard" );
91 
92  wxTheClipboard->Flush();
93  wxTheClipboard->Close();
94  }
95  else
96  wxLogDebug( "Failed to open the clipboard" );
97 
98  return 0;
99 }
100 
101 
103 {
104  // If using the keyboard, only cut in the component frame
105  if( aEvent.HasPosition()
107  return 0;
108 
109  // Get the selection, but only use the first one
111  std::vector<unsigned int> idx = m_frame->GetComponentIndices( CVPCB_MAINFRAME::SEL_COMPONENTS );
112 
113  if( idx.empty() || !comp )
114  return 0;
115 
116  // Get the fpid
117  LIB_ID fpid;
118  fpid = comp->GetFPID();
119 
120  // if no valid fpid, then skip
121  if( !fpid.IsValid() )
122  return 0;
123 
124  // Save it to the clipboard
125  if( wxTheClipboard->Open() )
126  {
127  if( !wxTheClipboard->SetData( new wxTextDataObject( fpid.GetUniStringLibId() ) ) )
128  {
129  wxLogDebug( "Failed to cut data to clipboard" );
130  wxTheClipboard->Close();
131  return 0;
132  }
133 
134  wxTheClipboard->Flush();
135  wxTheClipboard->Close();
136  }
137  else
138  {
139  wxLogDebug( "Failed to open the clipboard" );
140  return 0;
141  }
142 
143  // Remove the association
144  m_frame->AssociateFootprint( CVPCB_ASSOCIATION( idx.front(), "" ) );
145 
146  return 0;
147 }
148 
149 
151 {
152  // Get the selection
153  std::vector<unsigned int> idx = m_frame->GetComponentIndices( CVPCB_MAINFRAME::SEL_COMPONENTS );
154 
155  if( idx.empty() )
156  return 0;
157 
158  // Get the clipboard data and ensure it is valid
159  LIB_ID fpid;
160  wxTextDataObject data;
161 
162  if( wxTheClipboard->Open() )
163  {
164  wxTheClipboard->GetData( data );
165  wxTheClipboard->Close();
166  }
167  else
168  {
169  wxLogDebug( "Failed to open the clipboard" );
170  return 0;
171  }
172 
173  if( fpid.Parse( data.GetText(), LIB_ID::ID_PCB ) >= 0 )
174  return 0;
175 
176  // Assign the fpid to the selections
177  bool firstAssoc = true;
178  for( auto i : idx )
179  {
180  m_frame->AssociateFootprint( CVPCB_ASSOCIATION( i, fpid ), firstAssoc );
181  firstAssoc = false;
182  }
183 
184  return 0;
185 }
186 
188 {
189  m_frame = getEditFrame<CVPCB_MAINFRAME>();
190 }
191 
192 
194 {
196 
197  return 0;
198 }
199 
200 
202 {
204 
205  return 0;
206 }
207 
208 
210 {
211  // Get the currently selected footprint
212  LIB_ID fpid;
213  wxString fp = m_frame->GetSelectedFootprint();
214  fpid.Parse( fp, LIB_ID::ID_PCB );
215 
216  // Ignore the action if the footprint is empty (nothing selected)
217  if( fpid.empty() )
218  return 0;
219 
220  // Test for validity of the requested footprint
221  if( !fpid.IsValid() )
222  {
223  wxString msg =
224  wxString::Format( _( "\"%s\" is not a valid footprint." ), fpid.Format().wx_str() );
226  }
227 
228  // Get all the components that are selected and associate them with the current footprint
229  std::vector<unsigned int> sel = m_frame->GetComponentIndices( CVPCB_MAINFRAME::SEL_COMPONENTS );
230 
231  bool firstAssoc = true;
232  for( auto i : sel )
233  {
234  CVPCB_ASSOCIATION newfp( i, fpid );
235  m_frame->AssociateFootprint( newfp, firstAssoc );
236  firstAssoc = false;
237  }
238 
239  // Move to the next not associated component
241 
242  return 0;
243 }
244 
245 
247 {
249 
250  return 0;
251 }
252 
253 
255 {
256  // Get all the components that are selected
257  std::vector<unsigned int> sel = m_frame->GetComponentIndices( CVPCB_MAINFRAME::SEL_COMPONENTS );
258 
259  // Delete the association
260  bool firstAssoc = true;
261  for( auto i : sel )
262  {
263  m_frame->AssociateFootprint( CVPCB_ASSOCIATION( i, LIB_ID() ), firstAssoc );
264  firstAssoc = false;
265  }
266 
267  return 0;
268 }
269 
270 
272 {
273  if( IsOK( m_frame, _( "Delete all associations?" ) ) )
274  {
275  // Remove all selections to avoid issues when setting the fpids
276  m_frame->SetSelectedComponent( -1, true );
277  std::vector<unsigned int> idx =
279 
280  bool firstAssoc = true;
281  for( auto i : idx )
282  {
283  m_frame->AssociateFootprint( CVPCB_ASSOCIATION( i, LIB_ID() ), firstAssoc );
284  firstAssoc = false;
285  }
286 
287  // Remove all selections after setting the fpids and select the first component
288  m_frame->SetSelectedComponent( -1, true );
290  }
291 
292  // Update the status display
294 
295  return 0;
296 }
297 
298 
300 {
301  // Association
304 
305  // Deletion
308 
309  // Helpers
312 
313  // Clipboard
317 }
void AutomaticFootprintMatching()
Called by the automatic association button Read *.equ files to try to find corresponding footprint fo...
CONTROL_TYPE
The type of the controls present in the application.
int Associate(const TOOL_EVENT &aEvent)
Associate the selected footprint with the currently selected components.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:249
static TOOL_ACTION gotoNextNA
Definition: cvpcb_actions.h:56
This file is part of the common library.
int PasteAssoc(const TOOL_EVENT &aEvent)
Paste the clipboard onto the current selection.
void UndoAssociation()
Undo the most recent associations that were performed.
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
void AssociateFootprint(const CVPCB_ASSOCIATION &aAssociation, bool aNewEntry=true, bool aAddUndoItem=true)
Associate a footprint with a specific component in the list.
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:109
void RedoAssociation()
Redo the most recently undone association.
int CutAssoc(const TOOL_EVENT &aEvent)
Cut the selected associations to the clipboard.
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 setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
bool empty() const
Definition: lib_id.h:186
int AutoAssociate(const TOOL_EVENT &aEvent)
Perform automatic footprint association.
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:166
int DeleteAll(const TOOL_EVENT &aEvent)
Delete all associations.
void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
static TOOL_ACTION copy
Definition: actions.h:70
static TOOL_ACTION deleteAll
Definition: cvpcb_actions.h:65
void SetSelectedComponent(int aIndex, bool aSkipUpdate=false)
Set the currently selected component in the components listbox.
Class TOOL_EVENT.
Definition: tool_event.h:171
int Redo(const TOOL_EVENT &aEvent)
Redo the footprint associations most recently done.
int DeleteAssoc(const TOOL_EVENT &aEvent)
Delete the selected associations.
static TOOL_ACTION cut
Definition: actions.h:69
static TOOL_ACTION deleteAssoc
Definition: cvpcb_actions.h:66
#define _(s)
static TOOL_ACTION autoAssociate
Footprint Association actions.
Definition: cvpcb_actions.h:63
UTF8 Format() const
Definition: lib_id.cpp:237
Class COMPONENT is used to store components and all of their related information found in a netlist.
Definition: pcb_netlist.h:83
wxString GetSelectedFootprint()
static TOOL_ACTION redo
Definition: actions.h:68
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
size_t i
Definition: json11.cpp:649
COMPONENT * GetSelectedComponent()
Get the selected component from the component listbox.
wxString wx_str() const
Definition: utf8.cpp:51
int Undo(const TOOL_EVENT &aEvent)
Undo the footprint associations most recently done.
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:79
A class to define a footprint association to be made in cvpcb.
CVPCB_MAINFRAME::CONTROL_TYPE GetFocusedControl()
Find out which control currently has focus.
bool HasPosition() const
Returns if it this event has a valid position (true for mouse events and context-menu or hotkey-based...
Definition: tool_event.h:260
int CopyAssoc(const TOOL_EVENT &aEvent)
Copy the selected associations to the clipboard.
static TOOL_ACTION undo
Definition: actions.h:67
std::vector< unsigned int > GetComponentIndices(CVPCB_MAINFRAME::CRITERIA aCriteria=CVPCB_MAINFRAME::ALL_COMPONENTS)
Get the indices for all the components meeting the specified criteria in the components listbox.
wxString GetUniStringLibId() const
Definition: lib_id.h:148
static TOOL_ACTION associate
Definition: cvpcb_actions.h:64
int Parse(const UTF8 &aId, LIB_ID_TYPE aType, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:122
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:281
void DisplayStatus()
Function DisplayStatus updates the information displayed on the status bar at bottom of the main fram...
static TOOL_ACTION paste
Definition: actions.h:71