KiCad PCB EDA Suite
cvpcb_control.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 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 
30 #include <cvpcb_mainframe.h>
33 #include <listboxes.h>
34 #include <tools/cvpcb_actions.h>
35 #include <tools/cvpcb_control.h>
36 #include <wx/settings.h>
37 
38 using namespace std::placeholders;
39 
40 
42  TOOL_INTERACTIVE( "cvpcb.Control" ),
43  m_frame( nullptr )
44 {
45 }
46 
47 
49 {
50  m_frame = getEditFrame<CVPCB_MAINFRAME>();
51 }
52 
53 
54 int CVPCB_CONTROL::Main( const TOOL_EVENT& aEvent )
55 {
56  // Main loop: keep receiving events
57  while( TOOL_EVENT* evt = Wait() )
58  {
59  bool handled = false;
60 
61  // The escape key maps to the cancel event, which is used to close the window
62  if( evt->IsCancel() )
63  {
64  m_frame->Close( false );
65  handled = true;
66  }
67  else if( evt->IsKeyPressed() )
68  {
69  switch( evt->KeyCode() )
70  {
71  // The right arrow moves focus to the focusable object to the right
72  case WXK_RIGHT:
74  handled = true;
75  break;
76 
77  // The left arrow moves focus to the focusable object to the left
78  case WXK_LEFT:
80  handled = true;
81  break;
82 
83  default:
84  // Let every other key continue processing to the controls of the window
85  break;
86  }
87  }
88 
89  if( !handled )
90  evt->SetPassEvent();
91  }
92 
93  return 0;
94 }
95 
96 
98 {
99  int tmp = aEvent.Parameter<intptr_t>();
101  static_cast<CVPCB_MAINFRAME::FOCUS_DIR>( tmp );
102 
103  switch( dir )
104  {
106  switch( m_frame->GetFocusedControl() )
107  {
110  break;
111 
114  break;
115 
118  break;
119 
121  default:
122  break;
123  }
124 
125  break;
126 
128  switch( m_frame->GetFocusedControl() )
129  {
132  break;
133 
136  break;
137 
140  break;
141 
143  default:
144  break;
145  }
146 
147  break;
148 
149  default:
150  break;
151  }
152 
153  return 0;
154 }
155 
156 
158 {
159 
161 
162  if( !fpframe )
163  {
165  FRAME_CVPCB_DISPLAY, true, m_frame );
166  fpframe->Show( true );
167  }
168  else
169  {
170  if( fpframe->IsIconized() )
171  fpframe->Iconize( false );
172 
173  // The display footprint window might be buried under some other
174  // windows, so CreateScreenCmp() on an existing window would not
175  // show any difference, leaving the user confused.
176  // So we want to put it to front, second after our CVPCB_MAINFRAME.
177  // We do this by a little dance of bringing it to front then the main
178  // frame back.
179  wxWindow* focus = m_frame->FindFocus();
180 
181  fpframe->Raise(); // Make sure that is visible.
182  m_frame->Raise(); // .. but still we want the focus.
183 
184  if( focus )
185  focus->SetFocus();
186  }
187 
188  fpframe->InitDisplay();
189 
190  return 0;
191 }
192 
193 
195 {
197  static_cast<FOOTPRINTS_LISTBOX::FP_FILTER_T>( aEvent.Parameter<intptr_t>() ),
199 
200  return 0;
201 }
202 
203 
205 {
207  dlg.ShowModal();
208 
209  return 0;
210 }
211 
212 
214 {
216  return 0;
217 }
218 
219 
221 {
223  return 0;
224 }
225 
226 
227 int CVPCB_CONTROL::ToNA( const TOOL_EVENT& aEvent )
228 {
229  int tmp = aEvent.Parameter<intptr_t>();
231  static_cast<CVPCB_MAINFRAME::ITEM_DIR>( tmp );
232 
233  std::vector<unsigned int> naComp = m_frame->GetComponentIndices( CVPCB_MAINFRAME::NA_COMPONENTS );
234  std::vector<unsigned int> tempSel = m_frame->GetComponentIndices( CVPCB_MAINFRAME::SEL_COMPONENTS );
235 
236  // No unassociated components
237  if( naComp.empty() )
238  return 0;
239 
240  // Extract the current selection
241  bool changeSel = false;
242  unsigned int newSel = UINT_MAX;
243 
244  switch( dir )
245  {
247  if( !tempSel.empty() )
248  newSel = tempSel.front();
249 
250  // Find the next index in the component list
251  for( unsigned int idx : naComp )
252  {
253  if( idx > newSel )
254  {
255  changeSel = true;
256  newSel = idx;
257  break;
258  }
259  }
260 
261  break;
262 
264  if( !tempSel.empty() )
265  {
266  newSel = tempSel.front();
267 
268  // Find the previous index in the component list
269  for( int jj = naComp.size()-1; jj >= 0; jj-- )
270  {
271  unsigned idx = naComp[jj];
272 
273  if( idx < newSel )
274  {
275  changeSel = true;
276  newSel = idx;
277  break;
278  }
279  }
280  }
281 
282  break;
283 
284  default:
285  wxASSERT_MSG( false, wxT( "Invalid direction" ) );
286  }
287 
288  // Set the new component selection
289  if( changeSel )
290  m_frame->SetSelectedComponent( newSel );
291 
292  return 0;
293 }
294 
295 
297 {
298  ACTION_MENU* actionMenu = aEvent.Parameter<ACTION_MENU*>();
299  CONDITIONAL_MENU* conditionalMenu = dynamic_cast<CONDITIONAL_MENU*>( actionMenu );
300  SELECTION dummySel;
301 
302  if( conditionalMenu )
303  conditionalMenu->Evaluate( dummySel );
304 
305  if( actionMenu )
306  actionMenu->UpdateAll();
307 
308  return 0;
309 }
310 
311 
313 {
314  // Control actions
319 
320  // Run the footprint viewer
322 
323  // Management actions
327 
328  // Navigation actions
331 
332  // Filter the footprints
336 }
int ShowEquFileTable(const TOOL_EVENT &aEvent)
Show the dialog to modify the included footprint association files (.equ)
static TOOL_ACTION showFootprintViewer
Open the footprint viewer.
Definition: cvpcb_actions.h:49
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
static TOOL_ACTION saveAssociationsToSchematic
Management actions.
Definition: cvpcb_actions.h:56
static TOOL_ACTION FilterFPbyFPFilters
Footprint Filtering actions.
Definition: cvpcb_actions.h:67
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:53
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Suspend execution of the tool until an event specified in aEventList arrives.
static TOOL_ACTION showEquFileTable
Definition: cvpcb_actions.h:58
static TOOL_ACTION gotoNextNA
Definition: cvpcb_actions.h:53
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:48
This file is part of the common library.
static TOOL_ACTION saveAssociationsToFile
Definition: cvpcb_actions.h:57
static TOOL_ACTION gotoPreviousNA
Navigate the component tree.
Definition: cvpcb_actions.h:52
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:214
CVPCB_MAINFRAME * m_frame
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:143
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:393
static TOOL_ACTION changeFocusLeft
Definition: cvpcb_actions.h:46
void SetFocusedControl(CVPCB_MAINFRAME::CONTROL_TYPE aControl)
Set the focus to a specific control.
static TOOL_ACTION changeFocusRight
Window control actions.
Definition: cvpcb_actions.h:45
void UpdateAll()
Run update handlers for the menu and its submenus.
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).
int ToNA(const TOOL_EVENT &aEvent)
Move the selected component to the not associated one in the specified direction.
static TOOL_ACTION FilterFPbyLibrary
Definition: cvpcb_actions.h:69
int ChangeFocus(const TOOL_EVENT &aEvent)
Rotate focus in the CVPCB window.
CVPCB_MAINFRAME::CONTROL_TYPE GetFocusedControl() const
Find out which control currently has focus.
void SetSelectedComponent(int aIndex, bool aSkipUpdate=false)
Set the currently selected component in the components listbox.
T Parameter() const
Return a non-standard parameter assigned to the event.
Definition: tool_event.h:432
Generic, UI-independent tool event.
Definition: tool_event.h:152
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
No controls have focus.
int ToggleFootprintFilter(const TOOL_EVENT &aEvent)
Filter the footprint list by toggling the given filter type.
int SaveAssociationsToSchematic(const TOOL_EVENT &aEvent)
Save the associations to the schematic.
static TOOL_ACTION updateMenu
Definition: actions.h:167
int SaveAssociationsToFile(const TOOL_EVENT &aEvent)
Save the associations to the schematic and save schematic to file.
FOCUS_DIR
Directions to rotate the focus through the listboxes is.
void InitDisplay()
Refresh the full display for this frame: Set the title, the status line and redraw the canvas Must be...
static TOOL_ACTION filterFPbyPin
Definition: cvpcb_actions.h:68
static TOOL_ACTION controlActivate
Definition: cvpcb_actions.h:42
ITEM_DIR
Directions to move when selecting items.
Toggle the filter state.
int ShowFootprintViewer(const TOOL_EVENT &aEvent)
Create or Update the frame showing the current highlighted footprint and (if showed) the 3D display f...
int Main(const TOOL_EVENT &aEvent)
Main processing loop for the CVPCB window.
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:77
void SetFootprintFilter(FOOTPRINTS_LISTBOX::FP_FILTER_T aFilter, CVPCB_MAINFRAME::CVPCB_FILTER_ACTION aAction)
Set a filter criteria to either on/off or toggle the criteria.
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.
Not associated components.
int UpdateMenu(const TOOL_EVENT &aEvent)
Update the menu to reflect the current tool states.
DISPLAY_FOOTPRINTS_FRAME * GetFootprintViewerFrame() const
bool SaveFootprintAssociation(bool doSaveSchematic)
Save the edits that the user has done by sending them back to Eeschema via the kiway.