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_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  LIB_ID fpid;
52 
55  else if( m_frame->GetSelectedComponent() )
57  else
58  return 0;
59 
60  // if no valid fpid, then skip
61  if( !fpid.IsValid() )
62  return 0;
63 
64  wxLogNull raiiDoNotLog; // disable logging of failed clipboard actions
65 
66  if( wxTheClipboard->Open() )
67  {
68  wxTheClipboard->SetData( new wxTextDataObject( fpid.GetUniStringLibId() ) );
69  wxTheClipboard->Flush();
70  wxTheClipboard->Close();
71  }
72 
73  return 0;
74 }
75 
76 
78 {
79  // If using the keyboard, only cut in the component frame
82  {
83  return 0;
84  }
85 
86  // Get the selection, but only use the first one
88  std::vector<unsigned int> idx = m_frame->GetComponentIndices( CVPCB_MAINFRAME::SEL_COMPONENTS );
89 
90  if( idx.empty() || !comp )
91  return 0;
92 
93  // Get the fpid
94  LIB_ID fpid;
95  fpid = comp->GetFPID();
96 
97  // if no valid fpid, then skip
98  if( !fpid.IsValid() )
99  return 0;
100 
101  // Save it to the clipboard
102  {
103  wxLogNull doNotLog; // disable logging of failed clipboard actions
104 
105  if( wxTheClipboard->Open() )
106  {
107  if( !wxTheClipboard->SetData( new wxTextDataObject( fpid.GetUniStringLibId() ) ) )
108  {
109  wxTheClipboard->Close();
110  return 0;
111  }
112 
113  wxTheClipboard->Flush();
114  wxTheClipboard->Close();
115  }
116  else
117  {
118  return 0;
119  }
120  }
121 
122  // Remove the association
123  m_frame->AssociateFootprint( CVPCB_ASSOCIATION( idx.front(), "" ) );
124 
125  return 0;
126 }
127 
128 
130 {
131  // Get the selection
132  std::vector<unsigned int> idx = m_frame->GetComponentIndices( CVPCB_MAINFRAME::SEL_COMPONENTS );
133 
134  if( idx.empty() )
135  return 0;
136 
137  // Get the clipboard data and ensure it is valid
138  LIB_ID fpid;
139  wxTextDataObject data;
140 
141  {
142  wxLogNull doNotLog; // disable logging of failed clipboard actions
143 
144  if( wxTheClipboard->Open() )
145  {
146  wxTheClipboard->GetData( data );
147  wxTheClipboard->Close();
148  }
149  else
150  {
151  return 0;
152  }
153  }
154 
155  if( fpid.Parse( data.GetText() ) >= 0 )
156  return 0;
157 
158  // Assign the fpid to the selections
159  bool firstAssoc = true;
160  for( auto i : idx )
161  {
162  m_frame->AssociateFootprint( CVPCB_ASSOCIATION( i, fpid ), firstAssoc );
163  firstAssoc = false;
164  }
165 
166  return 0;
167 }
168 
169 
171 {
172  m_frame = getEditFrame<CVPCB_MAINFRAME>();
173 }
174 
175 
177 {
179 
180  return 0;
181 }
182 
183 
185 {
187 
188  return 0;
189 }
190 
191 
193 {
194  // Get the currently selected footprint
195  LIB_ID fpid;
196  wxString fp = m_frame->GetSelectedFootprint();
197  fpid.Parse( fp );
198 
199  // Ignore the action if the footprint is empty (nothing selected)
200  if( fpid.empty() )
201  return 0;
202 
203  // Test for validity of the requested footprint
204  if( !fpid.IsValid() )
205  {
206  wxString msg =
207  wxString::Format( _( "\"%s\" is not a valid footprint." ), fpid.Format().wx_str() );
209  }
210 
211  // Get all the components that are selected and associate them with the current footprint
212  std::vector<unsigned int> sel = m_frame->GetComponentIndices( CVPCB_MAINFRAME::SEL_COMPONENTS );
213 
214  bool firstAssoc = true;
215  for( auto i : sel )
216  {
217  CVPCB_ASSOCIATION newfp( i, fpid );
218  m_frame->AssociateFootprint( newfp, firstAssoc );
219  firstAssoc = false;
220  }
221 
222  // Move to the next not associated component
224 
225  return 0;
226 }
227 
228 
230 {
232 
233  return 0;
234 }
235 
236 
238 {
239  // Get all the components that are selected
240  std::vector<unsigned int> sel = m_frame->GetComponentIndices( CVPCB_MAINFRAME::SEL_COMPONENTS );
241 
242  // Delete the association
243  bool firstAssoc = true;
244  for( auto i : sel )
245  {
246  m_frame->AssociateFootprint( CVPCB_ASSOCIATION( i, LIB_ID() ), firstAssoc );
247  firstAssoc = false;
248  }
249 
250  return 0;
251 }
252 
253 
255 {
256  if( IsOK( m_frame, _( "Delete all associations?" ) ) )
257  {
258  // Remove all selections to avoid issues when setting the fpids
259  m_frame->SetSelectedComponent( -1, true );
260  std::vector<unsigned int> idx =
262 
263  bool firstAssoc = true;
264  for( auto i : idx )
265  {
266  m_frame->AssociateFootprint( CVPCB_ASSOCIATION( i, LIB_ID() ), firstAssoc );
267  firstAssoc = false;
268  }
269 
270  // Remove all selections after setting the fpids and select the first component
271  m_frame->SetSelectedComponent( -1, true );
273  }
274 
275  // Update the status display
277 
278  return 0;
279 }
280 
281 
283 {
284  // Association
287 
288  // Deletion
291 
292  // Helpers
295 
296  // Clipboard
300 }
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:265
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:215
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:141
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:168
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:189
int AutoAssociate(const TOOL_EVENT &aEvent)
Perform automatic footprint association.
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:132
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:70
CVPCB_MAINFRAME::CONTROL_TYPE GetFocusedControl() const
Find out which control currently has focus.
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.
Generic, UI-independent tool event.
Definition: tool_event.h:173
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
static TOOL_ACTION autoAssociate
Footprint Association actions.
Definition: cvpcb_actions.h:63
UTF8 Format() const
Definition: lib_id.cpp:233
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:83
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:68
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:122
#define _(s)
Definition: 3d_actions.cpp:33
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
Determine the reason of reset for a tool.
Definition: tool_base.h:78
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: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:142
static TOOL_ACTION associate
Definition: cvpcb_actions.h:64
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:296
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