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 <[email protected]>
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
40using 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
162 for( unsigned int i : idx )
163 {
164 m_frame->AssociateFootprint( CVPCB_ASSOCIATION( i, fpid ), firstAssoc );
165 firstAssoc = false;
166 }
167
168 return 0;
169}
170
171
173{
174 m_frame = getEditFrame<CVPCB_MAINFRAME>();
175}
176
177
179{
181
182 return 0;
183}
184
185
187{
189
190 return 0;
191}
192
193
195{
196 // Get the currently selected footprint
197 LIB_ID fpid;
198 wxString fp = m_frame->GetSelectedFootprint();
199 fpid.Parse( fp );
200
201 // Ignore the action if the footprint is empty (nothing selected)
202 if( fpid.empty() )
203 return 0;
204
205 // Test for validity of the requested footprint
206 if( !fpid.IsValid() )
207 {
208 wxString msg = wxString::Format( _( "'%s' is not a valid footprint." ),
209 fpid.Format().wx_str() );
211 }
212
213 // Get all the components that are selected and associate them with the current footprint
214 bool firstAssoc = true;
215
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 // Delete all the selected components' associations
241 bool firstAssoc = true;
242
244 {
245 m_frame->AssociateFootprint( CVPCB_ASSOCIATION( i, LIB_ID() ), firstAssoc );
246 firstAssoc = false;
247 }
248
249 return 0;
250}
251
252
254{
255 if( IsOK( m_frame, _( "Delete all associations?" ) ) )
256 {
257 // Remove all selections to avoid issues when setting the fpids
258 m_frame->SetSelectedComponent( -1, true );
259
260 bool firstAssoc = true;
261
263 {
264 m_frame->AssociateFootprint( CVPCB_ASSOCIATION( i, LIB_ID() ), firstAssoc );
265 firstAssoc = false;
266 }
267
268 // Remove all selections after setting the fpids and select the first component
269 m_frame->SetSelectedComponent( -1, true );
271 }
272
273 // Update the status display
275
276 return 0;
277}
278
279
281{
282 // Association
285
286 // Deletion
289
290 // Helpers
293
294 // Clipboard
298}
static TOOL_ACTION paste
Definition: actions.h:69
static TOOL_ACTION copy
Definition: actions.h:68
static TOOL_ACTION undo
Definition: actions.h:65
static TOOL_ACTION redo
Definition: actions.h:66
static TOOL_ACTION cut
Definition: actions.h:67
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:85
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:138
static TOOL_ACTION deleteAll
Definition: cvpcb_actions.h:63
static TOOL_ACTION gotoNextNA
Definition: cvpcb_actions.h:53
static TOOL_ACTION deleteAssoc
Definition: cvpcb_actions.h:64
static TOOL_ACTION associate
Definition: cvpcb_actions.h:62
static TOOL_ACTION autoAssociate
Footprint Association actions.
Definition: cvpcb_actions.h:61
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
int DeleteAssoc(const TOOL_EVENT &aEvent)
Delete the selected associations.
int Associate(const TOOL_EVENT &aEvent)
Associate the selected footprint with the currently selected components.
int Undo(const TOOL_EVENT &aEvent)
Undo the footprint associations most recently done.
int PasteAssoc(const TOOL_EVENT &aEvent)
Paste the clipboard onto the current selection.
int CutAssoc(const TOOL_EVENT &aEvent)
Cut the selected associations to the clipboard.
int Redo(const TOOL_EVENT &aEvent)
Redo the footprint associations most recently done.
int AutoAssociate(const TOOL_EVENT &aEvent)
Perform automatic footprint association.
int CopyAssoc(const TOOL_EVENT &aEvent)
Copy the selected associations to the clipboard.
int DeleteAll(const TOOL_EVENT &aEvent)
Delete all associations.
A class to define a footprint association to be made in cvpcb.
CVPCB_MAINFRAME::CONTROL_TYPE GetFocusedControl() const
Find out which control currently has focus.
void RedoAssociation()
Redo the most recently undone association.
wxString GetSelectedFootprint()
void SetSelectedComponent(int aIndex, bool aSkipUpdate=false)
Set the currently selected component in the components listbox.
@ CONTROL_FOOTPRINT
Footprint listbox.
@ CONTROL_COMPONENT
Component listbox.
void UndoAssociation()
Undo the most recent associations that were performed.
void DisplayStatus()
Update the information displayed on the status bar at bottom of the main frame.
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.
void AssociateFootprint(const CVPCB_ASSOCIATION &aAssociation, bool aNewEntry=true, bool aAddUndoItem=true)
Associate a footprint with a specific component in the list.
@ ALL_COMPONENTS
All components.
@ SEL_COMPONENTS
Selected components.
COMPONENT * GetSelectedComponent()
Get the selected component from the component listbox.
void AutomaticFootprintMatching()
Called by the automatic association button Read *.equ files to try to find corresponding footprint fo...
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:50
bool IsValid() const
Check if this LID_ID is valid.
Definition: lib_id.h:172
bool empty() const
Definition: lib_id.h:193
wxString GetUniStringLibId() const
Definition: lib_id.h:148
UTF8 Format() const
Definition: lib_id.cpp:117
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:215
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
Generic, UI-independent tool event.
Definition: tool_event.h:156
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 RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:142
wxString wx_str() const
Definition: utf8.cpp:46
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:380
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:325
This file is part of the common library.
#define _(s)
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