KiCad PCB EDA Suite
tool_manager.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) 2013-2018 CERN
5  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <map>
27 #include <stack>
28 #include <algorithm>
29 
30 #include <core/optional.h>
31 
32 #include <wx/event.h>
33 #include <wx/clipbrd.h>
34 
35 #include <view/view.h>
36 
37 #include <tool/tool_base.h>
38 #include <tool/tool_interactive.h>
39 #include <tool/tool_manager.h>
40 #include <tool/context_menu.h>
41 #include <tool/coroutine.h>
42 #include <tool/action_manager.h>
43 
44 #include <pcb_edit_frame.h>
45 #include <class_draw_panel_gal.h>
46 
49 {
50  TOOL_STATE( TOOL_BASE* aTool ) :
51  theTool( aTool )
52  {
53  clear();
54  }
55 
56  TOOL_STATE( const TOOL_STATE& aState )
57  {
58  theTool = aState.theTool;
59  idle = aState.idle;
60  pendingWait = aState.pendingWait;
62  contextMenu = aState.contextMenu;
64  cofunc = aState.cofunc;
65  wakeupEvent = aState.wakeupEvent;
66  waitEvents = aState.waitEvents;
67  transitions = aState.transitions;
68  vcSettings = aState.vcSettings;
69  // do not copy stateStack
70  }
71 
73  {
74  wxASSERT( stateStack.empty() );
75  }
76 
79 
81  bool idle;
82 
86 
89 
92 
95 
98 
101 
104 
107  std::vector<TRANSITION> transitions;
108 
111 
112  TOOL_STATE& operator=( const TOOL_STATE& aState )
113  {
114  theTool = aState.theTool;
115  idle = aState.idle;
116  pendingWait = aState.pendingWait;
117  pendingContextMenu = aState.pendingContextMenu;
118  contextMenu = aState.contextMenu;
119  contextMenuTrigger = aState.contextMenuTrigger;
120  cofunc = aState.cofunc;
121  wakeupEvent = aState.wakeupEvent;
122  waitEvents = aState.waitEvents;
123  transitions = aState.transitions;
124  vcSettings = aState.vcSettings;
125  // do not copy stateStack
126  return *this;
127  }
128 
129  bool operator==( const TOOL_MANAGER::TOOL_STATE& aRhs ) const
130  {
131  return aRhs.theTool == this->theTool;
132  }
133 
134  bool operator!=( const TOOL_MANAGER::TOOL_STATE& aRhs ) const
135  {
136  return aRhs.theTool != this->theTool;
137  }
138 
144  void Push()
145  {
146  auto state = std::make_unique<TOOL_STATE>( *this );
147  stateStack.push( std::move( state ) );
148  clear();
149  }
150 
157  bool Pop()
158  {
159  delete cofunc;
160 
161  if( !stateStack.empty() )
162  {
163  *this = *stateStack.top().get();
164  stateStack.pop();
165  return true;
166  }
167  else
168  {
169  cofunc = NULL;
170  return false;
171  }
172  }
173 
174 private:
176  std::stack<std::unique_ptr<TOOL_STATE>> stateStack;
177 
179  void clear()
180  {
181  idle = true;
182  pendingWait = false;
183  pendingContextMenu = false;
184  cofunc = NULL;
185  contextMenu = NULL;
186  contextMenuTrigger = CMENU_OFF;
187  vcSettings.Reset();
188  transitions.clear();
189  }
190 };
191 
192 
194  m_model( NULL ),
195  m_view( NULL ),
196  m_viewControls( NULL ),
197  m_editFrame( NULL ),
198  m_passEvent( false ),
199  m_menuActive( false ),
200  m_menuOwner( -1 ),
201  m_activeState( nullptr )
202 {
203  m_actionMgr = new ACTION_MANAGER( this );
204 }
205 
206 
208 {
209  std::map<TOOL_BASE*, TOOL_STATE*>::iterator it, it_end;
210 
211  for( it = m_toolState.begin(), it_end = m_toolState.end(); it != it_end; ++it )
212  {
213  delete it->second->cofunc; // delete cofunction
214  delete it->second; // delete TOOL_STATE
215  delete it->first; // delete the tool itself
216  }
217 
218  delete m_actionMgr;
219 }
220 
221 
223 {
224  wxASSERT_MSG( m_toolNameIndex.find( aTool->GetName() ) == m_toolNameIndex.end(),
225  wxT( "Adding two tools with the same name may result in unexpected behaviour.") );
226  wxASSERT_MSG( m_toolIdIndex.find( aTool->GetId() ) == m_toolIdIndex.end(),
227  wxT( "Adding two tools with the same ID may result in unexpected behaviour.") );
228  wxASSERT_MSG( m_toolTypes.find( typeid( *aTool ).name() ) == m_toolTypes.end(),
229  wxT( "Adding two tools of the same type may result in unexpected behaviour.") );
230 
231  TOOL_STATE* st = new TOOL_STATE( aTool );
232 
233  m_toolState[aTool] = st;
234  m_toolNameIndex[aTool->GetName()] = st;
235  m_toolIdIndex[aTool->GetId()] = st;
236  m_toolTypes[typeid( *aTool ).name()] = st->theTool;
237 
238  aTool->attachManager( this );
239 }
240 
241 
243 {
244  TOOL_BASE* tool = FindTool( aToolId );
245 
246  if( tool && tool->GetType() == INTERACTIVE )
247  return invokeTool( tool );
248 
249  return false; // there is no tool with the given id
250 }
251 
252 
253 bool TOOL_MANAGER::InvokeTool( const std::string& aToolName )
254 {
255  TOOL_BASE* tool = FindTool( aToolName );
256 
257  if( tool && tool->GetType() == INTERACTIVE )
258  return invokeTool( tool );
259 
260  return false; // there is no tool with the given name
261 }
262 
263 
265 {
266  m_actionMgr->RegisterAction( aAction );
267 }
268 
269 
271 {
272  m_actionMgr->UnregisterAction( aAction );
273 }
274 
275 
276 bool TOOL_MANAGER::RunAction( const std::string& aActionName, bool aNow, void* aParam )
277 {
278  TOOL_ACTION* action = m_actionMgr->FindAction( aActionName );
279 
280  if( !action )
281  {
282  wxASSERT_MSG( false, wxString::Format( wxT( "Could not find action %s." ), aActionName ) );
283  return false;
284  }
285 
286  RunAction( *action, aNow, aParam );
287 
288  return false;
289 }
290 
291 
292 void TOOL_MANAGER::RunAction( const TOOL_ACTION& aAction, bool aNow, void* aParam )
293 {
294  TOOL_EVENT event = aAction.MakeEvent();
295 
296  // Allow to override the action parameter
297  if( aParam )
298  event.SetParameter( aParam );
299 
300  if( aNow )
301  {
302  TOOL_STATE* current = m_activeState;
303  processEvent( event );
304  setActiveState( current );
305  }
306  else
307  {
308  PostEvent( event );
309  }
310 }
311 
312 
314 {
315  return m_actionMgr->GetHotKey( aAction );
316 }
317 
318 
320 {
322 }
323 
324 
326 {
327  wxASSERT( aTool != NULL );
328 
329  TOOL_EVENT evt( TC_COMMAND, TA_ACTIVATE, aTool->GetName() );
330  processEvent( evt );
331 
332  if( TOOL_STATE* active = GetCurrentToolState() )
333  setActiveState( active );
334 
335  return true;
336 }
337 
338 
340 {
341  TOOL_BASE* tool = FindTool( aToolId );
342 
343  if( tool && tool->GetType() == INTERACTIVE )
344  return runTool( tool );
345 
346  return false; // there is no tool with the given id
347 }
348 
349 
350 bool TOOL_MANAGER::runTool( const std::string& aToolName )
351 {
352  TOOL_BASE* tool = FindTool( aToolName );
353 
354  if( tool && tool->GetType() == INTERACTIVE )
355  return runTool( tool );
356 
357  return false; // there is no tool with the given name
358 }
359 
360 
362 {
363  wxASSERT( aTool != NULL );
364 
365  if( !isRegistered( aTool ) )
366  {
367  wxASSERT_MSG( false, wxT( "You cannot run unregistered tools" ) );
368  return false;
369  }
370 
371  TOOL_ID id = aTool->GetId();
372 
373  if( aTool->GetType() == INTERACTIVE )
374  static_cast<TOOL_INTERACTIVE*>( aTool )->resetTransitions();
375 
376  // If the tool is already active, bring it to the top of the active tools stack
377  if( isActive( aTool ) )
378  {
379  m_activeTools.erase( std::find( m_activeTools.begin(), m_activeTools.end(), id ) );
380  m_activeTools.push_front( id );
381  return false;
382  }
383 
385  aTool->Reset( TOOL_INTERACTIVE::RUN );
386 
387  // Add the tool on the front of the processing queue (it gets events first)
388  m_activeTools.push_front( id );
389 
390  return true;
391 }
392 
393 
395 {
396  std::map<TOOL_ID, TOOL_STATE*>::const_iterator it = m_toolIdIndex.find( aId );
397 
398  if( it != m_toolIdIndex.end() )
399  return it->second->theTool;
400 
401  return NULL;
402 }
403 
404 
405 TOOL_BASE* TOOL_MANAGER::FindTool( const std::string& aName ) const
406 {
407  std::map<std::string, TOOL_STATE*>::const_iterator it = m_toolNameIndex.find( aName );
408 
409  if( it != m_toolNameIndex.end() )
410  return it->second->theTool;
411 
412  return NULL;
413 }
414 
415 
417 {
418  // Deactivate the active tool, but do not run anything new
420  processEvent( evt );
421 }
422 
423 
425 {
426  DeactivateTool();
427 
428  for( auto& state : m_toolState )
429  {
430  TOOL_BASE* tool = state.first;
431  setActiveState( state.second );
432  tool->Reset( aReason );
433 
434  if( tool->GetType() == INTERACTIVE )
435  static_cast<TOOL_INTERACTIVE*>( tool )->resetTransitions();
436  }
437 }
438 
439 
441 {
442  for( auto it = m_toolState.begin(); it != m_toolState.end(); /* iteration in the loop */ )
443  {
444  TOOL_BASE* tool = it->first;
445  TOOL_STATE* state = it->second;
446  setActiveState( state );
447  ++it; // keep the iterator valid if the element is going to be erased
448 
449  if( !tool->Init() )
450  {
451  wxMessageBox(
452  wxString::Format( "Initialization of tool \"%s\" failed", tool->GetName() ) );
453 
454  // Unregister the tool
455  setActiveState( nullptr );
456  m_toolState.erase( tool );
457  m_toolNameIndex.erase( tool->GetName() );
458  m_toolIdIndex.erase( tool->GetId() );
459  m_toolTypes.erase( typeid( *tool ).name() );
460 
461  delete state;
462  delete tool;
463  }
464  }
465 
467 }
468 
469 
470 int TOOL_MANAGER::GetPriority( int aToolId ) const
471 {
472  int priority = 0;
473 
474  for( auto it = m_activeTools.begin(), itEnd = m_activeTools.end(); it != itEnd; ++it )
475  {
476  if( *it == aToolId )
477  return priority;
478 
479  ++priority;
480  }
481 
482  return -1;
483 }
484 
485 
487  const TOOL_EVENT_LIST& aConditions )
488 {
489  TOOL_STATE* st = m_toolState[aTool];
490 
491  st->transitions.push_back( TRANSITION( aConditions, aHandler ) );
492 }
493 
494 
496 {
497  m_toolState[aTool]->transitions.clear();
498 }
499 
500 
501 void TOOL_MANAGER::RunMainStack( TOOL_BASE* aTool, std::function<void()> aFunc )
502 {
503  TOOL_STATE* st = m_toolState[aTool];
504  setActiveState( st );
505  st->cofunc->RunMainStack( std::move( aFunc ) );
506 }
507 
508 
510 {
511  TOOL_STATE* st = m_toolState[aTool];
512 
513  wxASSERT( !st->pendingWait ); // everything collapses on two KiYield() in a row
514 
515  // indicate to the manager that we are going to sleep and we shall be
516  // woken up when an event matching aConditions arrive
517  st->pendingWait = true;
518  st->waitEvents = aConditions;
519 
520  // switch context back to event dispatcher loop
521  st->cofunc->KiYield();
522 
523  return st->wakeupEvent;
524 }
525 
526 
528 {
529  // iterate over all registered tools
530  for( auto it = m_activeTools.begin(); it != m_activeTools.end(); ++it )
531  {
532  TOOL_STATE* st = m_toolIdIndex[*it];
533 
534  // forward context menu events to the tool that created the menu
535  if( aEvent.IsMenu() )
536  {
537  if( *it != m_menuOwner )
538  continue;
539  }
540 
541  // the tool state handler is waiting for events (i.e. called Wait() method)
542  if( st->pendingWait )
543  {
544  if( st->waitEvents.Matches( aEvent ) )
545  {
546  // By default only messages are passed further
547  m_passEvent = ( aEvent.Category() == TC_MESSAGE );
548 
549  // got matching event? clear wait list and wake up the coroutine
550  st->wakeupEvent = aEvent;
551  st->pendingWait = false;
552  st->waitEvents.clear();
553 
554  if( st->cofunc )
555  {
556  setActiveState( st );
557  bool end = !st->cofunc->Resume();
558 
559  if( end )
560  it = finishTool( st );
561  }
562 
563  // If the tool did not request to propagate
564  // the event to other tools, we should stop it now
565  if( !m_passEvent )
566  break;
567  }
568  }
569  }
570 
571  for( auto& state : m_toolState )
572  {
573  TOOL_STATE* st = state.second;
574  bool finished = false;
575 
576  // no state handler in progress - check if there are any transitions (defined by
577  // Go() method that match the event.
578  if( !st->transitions.empty() )
579  {
580  for( TRANSITION& tr : st->transitions )
581  {
582  if( tr.first.Matches( aEvent ) )
583  {
584  auto func_copy = tr.second;
585 
586  // if there is already a context, then push it on the stack
587  // and transfer the previous view control settings to the new context
588  if( st->cofunc )
589  {
590  auto vc = st->vcSettings;
591  st->Push();
592  st->vcSettings = vc;
593  }
594 
595  st->cofunc = new COROUTINE<int, const TOOL_EVENT&>( std::move( func_copy ) );
596 
597  // as the state changes, the transition table has to be set up again
598  st->transitions.clear();
599 
600  // got match? Run the handler.
601  setActiveState( st );
602  st->idle = false;
603  st->cofunc->Call( aEvent );
604 
605  if( !st->cofunc->Running() )
606  finishTool( st ); // The couroutine has finished immediately?
607 
608  // if it is a message, continue processing
609  finished = !( aEvent.Category() == TC_MESSAGE );
610 
611  // there is no point in further checking, as transitions got cleared
612  break;
613  }
614  }
615  }
616 
617  if( finished )
618  break; // only the first tool gets the event
619  }
620 }
621 
622 
624 {
625  if( aEvent.Action() == TA_KEY_PRESSED )
626  {
627  // Check if there is a hotkey associated
628  if( m_actionMgr->RunHotKey( aEvent.Modifier() | aEvent.KeyCode() ) )
629  return false; // hotkey event was handled so it does not go any further
630  }
631 
632  return true;
633 }
634 
635 
637 {
638  if( aEvent.IsActivate() )
639  {
640  std::map<std::string, TOOL_STATE*>::iterator tool = m_toolNameIndex.find( *aEvent.GetCommandStr() );
641 
642  if( tool != m_toolNameIndex.end() )
643  {
644  runTool( tool->second->theTool );
645  return true;
646  }
647  }
648 
649  return false;
650 }
651 
653 {
654  for( TOOL_ID toolId : m_activeTools )
655  {
656  TOOL_STATE* st = m_toolIdIndex[toolId];
657 
658  // the tool requested a context menu. The menu is activated on RMB click (CMENU_BUTTON mode)
659  // or immediately (CMENU_NOW) mode. The latter is used for clarification lists.
660  if( st->contextMenuTrigger == CMENU_OFF )
661  continue;
662 
663  if( st->contextMenuTrigger == CMENU_BUTTON && !aEvent.IsClick( BUT_RIGHT ) )
664  break;
665 
666  st->pendingWait = true;
668 
669  // Store the menu pointer in case it is changed by the TOOL when handling menu events
670  CONTEXT_MENU* m = st->contextMenu;
671 
672  if( st->contextMenuTrigger == CMENU_NOW )
674 
675  // Store the cursor position, so the tools could execute actions
676  // using the point where the user has invoked a context menu
678 
679  // Save all tools cursor settings, as they will be overridden
680  for( auto idState : m_toolIdIndex )
681  {
682  TOOL_STATE* s = idState.second;
683  const auto& vc = s->vcSettings;
684 
685  if( vc.m_forceCursorPosition )
686  m_cursorSettings[idState.first] = vc.m_forcedPosition;
687  else
688  m_cursorSettings[idState.first] = NULLOPT;
689  }
690 
692 
693  // Display a copy of menu
694  std::unique_ptr<CONTEXT_MENU> menu( m->Clone() );
695 
696  // Run update handlers on the created copy
697  menu->UpdateAll();
698  m_menuOwner = toolId;
699  m_menuActive = true;
700 
701  auto frame = dynamic_cast<wxFrame*>( m_editFrame );
702 
703  if( frame )
704  frame->PopupMenu( menu.get() );
705 
706  // Warp the cursor as long as the menu wasn't clicked out of
707  if( menu->GetSelected() >= 0 )
708  m_viewControls->WarpCursor( m_menuCursor, true, false );
709  // Otherwise notify the tool of a cancelled menu
710  else
711  {
713  evt.SetParameter( m );
714  dispatchInternal( evt );
715  }
716 
717  // Notify the tools that menu has been closed
719  evt.SetParameter( m );
720  dispatchInternal( evt );
721 
722  m_menuActive = false;
723  m_menuOwner = -1;
724 
725  // Restore cursor settings
726  for( auto cursorSetting : m_cursorSettings )
727  {
728  auto it = m_toolIdIndex.find( cursorSetting.first );
729  wxASSERT( it != m_toolIdIndex.end() );
730 
731  if( it == m_toolIdIndex.end() )
732  continue;
733 
734  KIGFX::VC_SETTINGS& vc = it->second->vcSettings;
735  vc.m_forceCursorPosition = (bool) cursorSetting.second;
736  vc.m_forcedPosition = cursorSetting.second ? *cursorSetting.second : VECTOR2D( 0, 0 );
737  }
738 
739  m_cursorSettings.clear();
740  break;
741  }
742 }
743 
744 
745 TOOL_MANAGER::ID_LIST::iterator TOOL_MANAGER::finishTool( TOOL_STATE* aState )
746 {
747  auto it = std::find( m_activeTools.begin(), m_activeTools.end(), aState->theTool->GetId() );
748 
749  if( !aState->Pop() )
750  {
751  // Deactivate the tool if there are no other contexts saved on the stack
752  if( it != m_activeTools.end() )
753  it = m_activeTools.erase( it );
754 
755  aState->idle = true;
756  }
757 
758  if( aState == m_activeState )
759  setActiveState( nullptr );
760 
761  // Set transitions to be ready for future TOOL_EVENTs
762  TOOL_BASE* tool = aState->theTool;
763 
764  if( tool->GetType() == INTERACTIVE )
765  static_cast<TOOL_INTERACTIVE*>( tool )->resetTransitions();
766 
767  return it;
768 }
769 
770 
772 {
773  bool hotkey_handled = processEvent( aEvent );
774 
775  if( TOOL_STATE* active = GetCurrentToolState() )
776  setActiveState( active );
777 
778  if( m_view->IsDirty() )
779  {
780  auto f = dynamic_cast<EDA_DRAW_FRAME*>( GetEditFrame() );
781 
782  if( f )
783  f->GetGalCanvas()->Refresh(); // fixme: ugly hack, provide a method in TOOL_DISPATCHER.
784 
785 #if defined( __WXMAC__ ) || defined( __WINDOWS__ )
786  wxTheApp->ProcessPendingEvents(); // required for updating brightening behind a popup menu
787 #endif
788  }
789 
790  return hotkey_handled;
791 }
792 
793 
795  CONTEXT_MENU_TRIGGER aTrigger )
796 {
797  TOOL_STATE* st = m_toolState[aTool];
798 
799  st->contextMenu = aMenu;
800  st->contextMenuTrigger = aTrigger;
801 }
802 
803 
804 bool TOOL_MANAGER::SaveClipboard( const std::string& aText )
805 {
806  if( wxTheClipboard->Open() )
807  {
808  wxTheClipboard->SetData( new wxTextDataObject( wxString( aText.c_str(), wxConvUTF8 ) ) );
809  wxTheClipboard->Close();
810 
811  return true;
812  }
813 
814  return false;
815 }
816 
817 
818 std::string TOOL_MANAGER::GetClipboard() const
819 {
820  std::string result;
821 
822  if( wxTheClipboard->Open() )
823  {
824  if( wxTheClipboard->IsSupported( wxDF_TEXT ) )
825  {
826  wxTextDataObject data;
827  wxTheClipboard->GetData( data );
828 
829  result = data.GetText().mb_str();
830  }
831 
832  wxTheClipboard->Close();
833  }
834 
835  return result;
836 }
837 
838 
840 {
841  if( TOOL_STATE* active = GetCurrentToolState() )
842  return active->vcSettings;
843 
844  return m_viewControls->GetSettings();
845 }
846 
847 
848 TOOL_ID TOOL_MANAGER::MakeToolId( const std::string& aToolName )
849 {
850  static int currentId;
851 
852  return currentId++;
853 }
854 
855 
857  KIGFX::VIEW_CONTROLS* aViewControls, wxWindow* aFrame )
858 {
859  m_model = aModel;
860  m_view = aView;
861  m_viewControls = aViewControls;
862  m_editFrame = aFrame;
864 }
865 
866 
868 {
869  if( !isRegistered( aTool ) )
870  return false;
871 
872  // Just check if the tool is on the active tools stack
873  return std::find( m_activeTools.begin(), m_activeTools.end(), aTool->GetId() ) != m_activeTools.end();
874 }
875 
876 
878 {
880 
881  if( m_menuActive )
882  {
883  // Context menu is active, so the cursor settings are overridden (see dispatchContextMenu())
884  auto it = m_cursorSettings.find( aState->theTool->GetId() );
885 
886  if( it != m_cursorSettings.end() )
887  {
889 
890  // Tool has overridden the cursor position, so store the new settings
892  {
893  if( !curr.m_forceCursorPosition )
894  it->second = NULLOPT;
895  else
896  it->second = curr.m_forcedPosition;
897  }
898  else
899  {
900  OPT<VECTOR2D> cursor = it->second;
901 
902  if( cursor )
903  {
904  aState->vcSettings.m_forceCursorPosition = true;
905  aState->vcSettings.m_forcedPosition = *cursor;
906  }
907  else
908  {
909  aState->vcSettings.m_forceCursorPosition = false;
910  }
911  }
912  }
913  }
914 }
915 
916 
918 {
920 }
921 
922 
924 {
925  // Early dispatch of events destined for the TOOL_MANAGER
926  if( !dispatchStandardEvents( aEvent ) )
927  return true;
928 
929  dispatchInternal( aEvent );
930  dispatchActivation( aEvent );
931  dispatchContextMenu( aEvent );
932 
933  // Dispatch queue
934  while( !m_eventQueue.empty() )
935  {
936  TOOL_EVENT event = m_eventQueue.front();
937  m_eventQueue.pop_front();
938  processEvent( event );
939  }
940 
941  return false;
942 }
943 
944 
946 {
947  if( m_activeState )
949 
950  m_activeState = aState;
951 
952  if( m_activeState )
953  applyViewControls( aState );
954 }
955 
956 
958 {
959  auto it = m_toolIdIndex.find( aId );
960  return !it->second->idle;
961 }
void RunMainStack(std::function< void()> func)
Function RunMainStack()
Definition: coroutine.h:191
bool m_menuActive
Flag indicating whether a context menu is currently displayed.
Definition: tool_manager.h:535
const KIGFX::VC_SETTINGS & GetCurrentToolVC() const
Returns the view controls settings for the current tool or the general settings if there is no active...
bool operator==(const TOOL_MANAGER::TOOL_STATE &aRhs) const
VECTOR2D m_menuCursor
Right click context menu position.
Definition: tool_manager.h:532
bool IsToolActive(TOOL_ID aId) const
Function IsToolActive() Returns true if a tool with given id is active (executing) ...
virtual bool Init()
Function Init() Init() is called once upon a registration of the tool.
Definition: tool_base.h:93
TOOL_BASE * FindTool(int aId) const
Function FindTool() Searches for a tool with given ID.
void Reset()
Restores the default settings
TOOL_STATE(TOOL_BASE *aTool)
TOOL_STATE & operator=(const TOOL_STATE &aState)
int GetHotKey(const TOOL_ACTION &aAction)
>
TOOL_BASE * theTool
The tool itself.
ACTION_MANAGER * m_actionMgr
Instance of ACTION_MANAGER that handles TOOL_ACTIONs.
Definition: tool_manager.h:515
bool processEvent(const TOOL_EVENT &aEvent)
Main function for event processing.
static TOOL_ID MakeToolId(const std::string &aToolName)
Generates a unique ID from for a tool with given name.
std::vector< TRANSITION > transitions
List of possible transitions (ie.
ID_STATE_MAP m_toolIdIndex
Index of the registered tools current states, associated by tools&#39; ID numbers.
Definition: tool_manager.h:506
void ScheduleNextState(TOOL_BASE *aTool, TOOL_STATE_FUNC &aHandler, const TOOL_EVENT_LIST &aConditions)
Defines a state transition - the events that cause a given handler method in the tool to be called...
void RegisterAction(TOOL_ACTION *aAction)
Function RegisterAction() Registers an action that can be used to control tools (eg.
bool dispatchStandardEvents(const TOOL_EVENT &aEvent)
Function dispatchStandardEvents() Handles specific events, that are intended for TOOL_MANAGER rather ...
bool SaveClipboard(const std::string &aText)
Stores an information to the system clipboard.
bool Call(ArgType aArg)
Function Call()
Definition: coroutine.h:205
void UpdateHotKeys()
Function UpdateHotKeys() Updates TOOL_ACTIONs hot key assignment according to the current frame&#39;s Hot...
Class CONTEXT_MENU.
Definition: context_menu.h:44
void ApplySettings(const VC_SETTINGS &aSettings)
Applies VIEW_CONTROLS settings from an object
KIGFX::VC_SETTINGS vcSettings
VIEW_CONTROLS settings to preserve settings when the tools are switched.
wxWindow * GetEditFrame() const
Definition: tool_manager.h:267
std::map< TOOL_ID, OPT< VECTOR2D > > m_cursorSettings
Original cursor position, if overridden by the context menu handler.
Definition: tool_manager.h:518
Tool is invoked after being inactive.
Definition: tool_base.h:82
ID_LIST::iterator finishTool(TOOL_STATE *aState)
Function finishTool() Deactivates a tool and does the necessary clean up.
bool dispatchActivation(const TOOL_EVENT &aEvent)
Function dispatchActivation() Checks if it is a valid activation event and invokes a proper tool...
VECTOR2D m_forcedPosition
Forced cursor position (world coordinates)
Definition: view_controls.h:58
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:125
void attachManager(TOOL_MANAGER *aManager)
Function attachManager()
Definition: tool_base.cpp:59
Class ACTION_MANAGER.
void ScheduleContextMenu(TOOL_BASE *aTool, CONTEXT_MENU *aMenu, CONTEXT_MENU_TRIGGER aTrigger)
Sets behaviour of the tool&#39;s context popup menu.
int Modifier(int aMask=MD_MODIFIER_MASK) const
Returns information about key modifiers state (Ctrl, Alt, etc.)
Definition: tool_event.h:325
TOOL_ACTION * FindAction(const std::string &aActionName) const
Function FindAction() Finds an action with a given name (if there is one available).
void applyViewControls(TOOL_STATE *aState)
Function applyViewControls() Applies VIEW_CONTROLS settings stored in a TOOL_STATE object...
The base class for create windows for drawing purpose.
Definition: draw_frame.h:78
int KeyCode() const
Definition: tool_event.h:330
bool IsDirty() const
Function IsDirty() Returns true if any of the VIEW layers needs to be refreshened.
Definition: view.h:568
void Push()
Function Push() Stores the current state of the tool on stack.
void dispatchContextMenu(const TOOL_EVENT &aEvent)
Function dispatchContextMenu() Handles context menu related events.
OPT< TOOL_EVENT > ScheduleWait(TOOL_BASE *aTool, const TOOL_EVENT_LIST &aConditions)
Pauses execution of a given tool until one or more events matching aConditions arrives.
bool runTool(TOOL_ID aToolId)
Function runTool() Makes a tool active, so it can receive events and react to them.
void DeactivateTool()
Function DeactivateTool() Deactivates the currently active tool.
bool InvokeTool(TOOL_ID aToolId)
Function InvokeTool() Calls a tool by sending a tool activation event to tool of given ID...
bool m_passEvent
Flag saying if the currently processed event should be passed to other tools.
Definition: tool_manager.h:529
EDA_ITEM * m_model
Definition: tool_manager.h:520
bool pendingWait
Flag defining if the tool is waiting for any event (i.e.
void SetParameter(T aParam)
Function SetParameter() Sets a non-standard parameter assigned to the event.
Definition: tool_event.h:399
virtual void Reset(RESET_REASON aReason)=0
Function Reset() Brings the tool to a known, initial state.
bool pendingContextMenu
Is there a context menu being displayed.
NAME_STATE_MAP m_toolNameIndex
Index of the registered tools current states, associated by tools&#39; names.
Definition: tool_manager.h:503
int TOOL_ID
Unique identifier for tools.
Definition: tool_base.h:57
virtual void WarpCursor(const VECTOR2D &aPosition, bool aWorldCoordinates=false, bool aWarpView=false)=0
Function WarpCursor() If enabled (.
Class TOOL_EVENT_LIST.
Definition: tool_event.h:503
bool operator!=(const TOOL_MANAGER::TOOL_STATE &aRhs) const
Tool that interacts with the user
Definition: tool_base.h:50
CONTEXT_MENU * Clone() const
Creates a deep, recursive copy of this CONTEXT_MENU.
const auto NULLOPT
Definition: optional.h:9
bool m_forceCursorPosition
Is the forced cursor position enabled
Definition: view_controls.h:61
TOOL_ID m_menuOwner
Tool currently displaying a popup menu. It is negative when there is no menu displayed.
Definition: tool_manager.h:538
wxWindow * m_editFrame
Definition: tool_manager.h:523
void SetEnvironment(EDA_ITEM *aModel, KIGFX::VIEW *aView, KIGFX::VIEW_CONTROLS *aViewControls, wxWindow *aFrame)
Sets the work environment (model, view, view controls and the parent window).
void setActiveState(TOOL_STATE *aState)
Saves the previous active state and sets a new one.
bool ProcessEvent(const TOOL_EVENT &aEvent)
Propagates an event to tools that requested events of matching type(s).
CONTEXT_MENU_TRIGGER
Defines when a context menu is opened.
Definition: tool_event.h:156
VECTOR2D GetCursorPosition() const
Returns the current cursor position in world coordinates.
void dispatchInternal(const TOOL_EVENT &aEvent)
Function dispatchInternal Passes an event at first to the active tools, then to all others...
Class TOOL_EVENT.
Definition: tool_event.h:168
bool idle
Is the tool active (pending execution) or disabled at the moment.
bool IsActivate() const
Definition: tool_event.h:309
std::stack< std::unique_ptr< TOOL_STATE > > stateStack
Stack preserving previous states of a TOOL.
CONTEXT_MENU * contextMenu
Context menu currently used by the tool.
int GetHotKey(const TOOL_ACTION &aAction) const
Function GetHotKey() Returns the hot key associated with a given action or 0 if there is none...
bool RunHotKey(int aHotKey) const
Function RunHotKey() Runs an action associated with a hotkey (if there is one available).
Structure to keep VIEW_CONTROLS settings for easy store/restore operations
Definition: view_controls.h:44
void ResetTools(TOOL_BASE::RESET_REASON aReason)
Function ResetTools() Resets all tools (i.e.
OPT< std::string > GetCommandStr() const
Definition: tool_event.h:409
Class VIEW_CONTROLS is an interface for classes handling user events controlling the view behaviour (...
int GetPriority(int aToolId) const
Returns priority of a given tool.
virtual void ForceCursorPosition(bool aEnabled, const VECTOR2D &aPosition=VECTOR2D(0, 0))
Function ForceCursorPosition() Places the cursor immediately at a given point.
TOOL_ID GetId() const
Function GetId() Returns the unique identifier of the tool.
Definition: tool_base.h:122
Struct describing the current execution state of a TOOL.
KIGFX::VIEW_CONTROLS * m_viewControls
Definition: tool_manager.h:522
bool Resume()
Function Resume()
Definition: coroutine.h:239
TOOL_STATE * GetCurrentToolState() const
Returns the TOOL_STATE object representing the state of the active tool.
Definition: tool_manager.h:296
void UnregisterAction(TOOL_ACTION *aAction)
Function UnregisterAction() Removes a tool action from the manager and makes it unavailable for furth...
std::function< int(const TOOL_EVENT &)> TOOL_STATE_FUNC
Definition: tool_base.h:59
TOOL_ACTIONS Action() const
Returns more specific information about the type of an event.
Definition: tool_event.h:240
bool isActive(TOOL_BASE *aTool)
Function isActive() Returns information about a tool activation status.
KIGFX::VIEW * m_view
Definition: tool_manager.h:521
std::string GetClipboard() const
Returns the information currently stored in the system clipboard.
void RunMainStack(TOOL_BASE *aTool, std::function< void()> aFunc)
OPT< const TOOL_EVENT & > Matches(const TOOL_EVENT &aEvent) const
Definition: tool_event.h:528
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
TOOL_EVENT_CATEGORY Category() const
Returns the category (eg. mouse/keyboard/action) of an event..
Definition: tool_event.h:234
const VC_SETTINGS & GetSettings() const
Returns the current VIEW_CONTROLS settings
TOOL_EVENT wakeupEvent
The event that triggered the execution/wakeup of the tool after Wait() call.
bool IsMenu() const
Definition: tool_event.h:319
void RegisterAction(TOOL_ACTION *aAction)
Function RegisterAction() Adds a tool action to the manager and sets it up.
TOOL_TYPE GetType() const
Function GetType() Returns the type of the tool.
Definition: tool_base.h:111
TOOL_EVENT MakeEvent() const
Function HasHotKey() Checks if the action has a hot key assigned.
Definition: tool_action.h:104
std::list< TOOL_EVENT > m_eventQueue
Queue that stores events to be processed at the end of the event processing cycle.
Definition: tool_manager.h:526
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
bool Pop()
Function Pop() Restores state of the tool from stack.
TOOL_STATE * m_activeState
Pointer to the state object corresponding to the currently executed tool.
Definition: tool_manager.h:541
COROUTINE< int, const TOOL_EVENT & > * cofunc
Tool execution context.
Class TOOL_BASE.
Definition: tool_base.h:68
Class TOOL_ACTION.
Definition: tool_action.h:46
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:154
bool isRegistered(TOOL_BASE *aTool) const
Function isRegistered() Returns information about a tool registration status.
Definition: tool_manager.h:461
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:80
boost::optional< T > OPT
Definition: optional.h:7
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:918
CONTEXT_MENU_TRIGGER contextMenuTrigger
Defines when the context menu is opened.
void InitTools()
Function InitTools() Initializes all registered tools.
Class VIEW.
Definition: view.h:61
void ClearTransitions(TOOL_BASE *aTool)
Clears the state transition map for a tool.
ID_LIST m_activeTools
Stack of the active tools.
Definition: tool_manager.h:512
TOOL_EVENT_LIST waitEvents
List of events the tool is currently waiting for.
const std::string & GetName() const
Function GetName() Returns the name of the tool.
Definition: tool_base.h:133
bool Running() const
Function Running()
Definition: coroutine.h:280
bool IsClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.h:277
void clear()
Restores the initial state.
void PostEvent(const TOOL_EVENT &aEvent)
Puts an event to the event queue to be processed at the end of event processing cycle.
Definition: tool_manager.h:238
TOOL_STATE_MAP m_toolState
Index of registered tools current states, associated by tools&#39; objects.
Definition: tool_manager.h:500
TOOL_STATE(const TOOL_STATE &aState)
void saveViewControls(TOOL_STATE *aState)
Function saveViewControls() Saves the VIEW_CONTROLS settings to the tool state object.
void UpdateHotKeys()
>
bool invokeTool(TOOL_BASE *aTool)
Function invokeTool() Invokes a tool by sending a proper event (in contrary to runTool, which makes the tool run for real).
void RegisterTool(TOOL_BASE *aTool)
Function RegisterTool() Adds a tool to the manager set and sets it up.
void UnregisterAction(TOOL_ACTION *aAction)
Function UnregisterAction() Unregisters an action, so it is no longer active.
void KiYield()
Function KiYield()
Definition: coroutine.h:156
std::pair< TOOL_EVENT_LIST, TOOL_STATE_FUNC > TRANSITION
Definition: tool_manager.h:376
std::map< const char *, TOOL_BASE * > m_toolTypes
Index of the registered tools to easily lookup by their type.
Definition: tool_manager.h:509