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-2020 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_base.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 #include <wx/log.h>
31 
32 #include <cvpcb_association.h>
33 #include <cvpcb_mainframe.h>
36 #include <listboxes.h>
37 #include <tools/cvpcb_actions.h>
39 
40 using namespace std::placeholders;
41 
42 
44  TOOL_INTERACTIVE( "cvpcb.Association" ),
45  m_frame( nullptr )
46 {
47 }
48 
49 
51 {
52  LIB_ID fpid;
53 
56  else if( m_frame->GetSelectedComponent() )
58  else
59  return 0;
60 
61  // if no valid fpid, then skip
62  if( !fpid.IsValid() )
63  return 0;
64 
65  wxLogNull raiiDoNotLog; // disable logging of failed clipboard actions
66 
67  if( wxTheClipboard->Open() )
68  {
69  wxTheClipboard->SetData( new wxTextDataObject( fpid.GetUniStringLibId() ) );
70  wxTheClipboard->Flush();
71  wxTheClipboard->Close();
72  }
73 
74  return 0;
75 }
76 
77 
79 {
80  // If using the keyboard, only cut in the component frame
83  {
84  return 0;
85  }
86 
87  // Get the selection, but only use the first one
89  std::vector<unsigned int> idx = m_frame->GetComponentIndices( CVPCB_MAINFRAME::SEL_COMPONENTS );
90 
91  if( idx.empty() || !comp )
92  return 0;
93 
94  // Get the fpid
95  LIB_ID fpid;
96  fpid = comp->GetFPID();
97 
98  // if no valid fpid, then skip
99  if( !fpid.IsValid() )
100  return 0;
101 
102  // Save it to the clipboard
103  {
104  wxLogNull doNotLog; // disable logging of failed clipboard actions
105 
106  if( wxTheClipboard->Open() )
107  {
108  if( !wxTheClipboard->SetData( new wxTextDataObject( fpid.GetUniStringLibId() ) ) )
109  {
110  wxTheClipboard->Close();
111  return 0;
112  }
113 
114  wxTheClipboard->Flush();
115  wxTheClipboard->Close();
116  }
117  else
118  {
119  return 0;
120  }
121  }
122 
123  // Remove the association
124  m_frame->AssociateFootprint( CVPCB_ASSOCIATION( idx.front(), "" ) );
125 
126  return 0;
127 }
128 
129 
131 {
132  // Get the selection
133  std::vector<unsigned int> idx = m_frame->GetComponentIndices( CVPCB_MAINFRAME::SEL_COMPONENTS );
134 
135  if( idx.empty() )
136  return 0;
137 
138  // Get the clipboard data and ensure it is valid
139  LIB_ID fpid;
140  wxTextDataObject data;
141 
142  {
143  wxLogNull doNotLog; // disable logging of failed clipboard actions
144 
145  if( wxTheClipboard->Open() )
146  {
147  wxTheClipboard->GetData( data );
148  wxTheClipboard->Close();
149  }
150  else
151  {
152  return 0;
153  }
154  }
155 
156  if( fpid.Parse( data.GetText() ) >= 0 )
157  return 0;
158 
159  // Assign the fpid to the selections
160  bool firstAssoc = true;
161  for( auto i : idx )
162  {
163  m_frame->AssociateFootprint( CVPCB_ASSOCIATION( i, fpid ), firstAssoc );
164  firstAssoc = false;
165  }
166 
167  return 0;
168 }
169 
170 
172 {
173  m_frame = getEditFrame<CVPCB_MAINFRAME>();
174 }
175 
176 
178 {
180 
181  return 0;
182 }
183 
184 
186 {
188 
189  return 0;
190 }
191 
192 
194 {
195  // Get the currently selected footprint
196  LIB_ID fpid;
197  wxString fp = m_frame->GetSelectedFootprint();
198  fpid.Parse( fp );
199 
200  // Ignore the action if the footprint is empty (nothing selected)
201  if( fpid.empty() )
202  return 0;
203 
204  // Test for validity of the requested footprint
205  if( !fpid.IsValid() )
206  {
207  wxString msg = wxString::Format( _( "'%s' is not a valid footprint." ),
208  fpid.Format().wx_str() );
210  }
211 
212  // Get all the components that are selected and associate them with the current footprint
213  std::vector<unsigned int> sel = m_frame->GetComponentIndices( CVPCB_MAINFRAME::SEL_COMPONENTS );
214 
215  bool firstAssoc = true;
216  for( auto i : sel )
217  {
218  CVPCB_ASSOCIATION newfp( i, fpid );
219  m_frame->AssociateFootprint( newfp, firstAssoc );
220  firstAssoc = false;
221  }
222 
223  // Move to the next not associated component
225 
226  return 0;
227 }
228 
229 
231 {
233 
234  return 0;
235 }
236 
237 
239 {
240  // Get all the components that are selected
241  std::vector<unsigned int> sel = m_frame->GetComponentIndices( CVPCB_MAINFRAME::SEL_COMPONENTS );
242 
243  // Delete the association
244  bool firstAssoc = true;
245  for( auto i : sel )
246  {
247  m_frame->AssociateFootprint( CVPCB_ASSOCIATION( i, LIB_ID() ), firstAssoc );
248  firstAssoc = false;
249  }
250 
251  return 0;
252 }
253 
254 
256 {
257  if( IsOK( m_frame, _( "Delete all associations?" ) ) )
258  {
259  // Remove all selections to avoid issues when setting the fpids
260  m_frame->SetSelectedComponent( -1, true );
261  std::vector<unsigned int> idx =
263 
264  bool firstAssoc = true;
265  for( auto i : idx )
266  {
267  m_frame->AssociateFootprint( CVPCB_ASSOCIATION( i, LIB_ID() ), firstAssoc );
268  firstAssoc = false;
269  }
270 
271  // Remove all selections after setting the fpids and select the first component
272  m_frame->SetSelectedComponent( -1, true );
274  }
275 
276  // Update the status display
278 
279  return 0;
280 }
281 
282 
284 {
285  // Association
288 
289  // Deletion
292 
293  // Helpers
296 
297  // Clipboard
301 }
void AutomaticFootprintMatching()
Called by the automatic association button Read *.equ files to try to find corresponding footprint fo...
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:284
static TOOL_ACTION gotoNextNA
Definition: cvpcb_actions.h:53
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:214
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)
Run the specified action.
Definition: tool_manager.h:143
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
Check if this LID_ID is valid.
Definition: lib_id.h:159
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))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
bool empty() const
Definition: lib_id.h:180
int AutoAssociate(const TOOL_EVENT &aEvent)
Perform automatic footprint association.
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:133
int DeleteAll(const TOOL_EVENT &aEvent)
Delete all associations.
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
static TOOL_ACTION copy
Definition: actions.h:67
CVPCB_MAINFRAME::CONTROL_TYPE GetFocusedControl() const
Find out which control currently has focus.
static TOOL_ACTION deleteAll
Definition: cvpcb_actions.h:62
void SetSelectedComponent(int aIndex, bool aSkipUpdate=false)
Set the currently selected component in the components listbox.
Generic, UI-independent tool event.
Definition: tool_event.h:152
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:66
#define _(s)
static TOOL_ACTION deleteAssoc
Definition: cvpcb_actions.h:63
static TOOL_ACTION autoAssociate
Footprint Association actions.
Definition: cvpcb_actions.h:60
UTF8 Format() const
Definition: lib_id.cpp:116
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:84
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
wxString GetSelectedFootprint()
static TOOL_ACTION redo
Definition: actions.h:65
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:49
COMPONENT * GetSelectedComponent()
Get the selected component from the component listbox.
wxString wx_str() const
Definition: utf8.cpp:46
int Undo(const TOOL_EVENT &aEvent)
Undo the footprint associations most recently done.
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:77
A class to define a footprint association to be made in cvpcb.
int CopyAssoc(const TOOL_EVENT &aEvent)
Copy the selected associations to the clipboard.
static TOOL_ACTION undo
Definition: actions.h:64
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:134
static TOOL_ACTION associate
Definition: cvpcb_actions.h:61
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:315
void DisplayStatus()
Update the information displayed on the status bar at bottom of the main frame.
static TOOL_ACTION paste
Definition: actions.h:68