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/action_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  if( !stateStack.empty() )
75  wxFAIL;
76  }
77 
80 
82  bool idle;
83 
87 
90 
93 
96 
99 
102 
105 
108  std::vector<TRANSITION> transitions;
109 
112 
113  TOOL_STATE& operator=( const TOOL_STATE& aState )
114  {
115  theTool = aState.theTool;
116  idle = aState.idle;
117  pendingWait = aState.pendingWait;
119  contextMenu = aState.contextMenu;
121  cofunc = aState.cofunc;
122  wakeupEvent = aState.wakeupEvent;
123  waitEvents = aState.waitEvents;
124  transitions = aState.transitions;
125  vcSettings = aState.vcSettings;
126  // do not copy stateStack
127  return *this;
128  }
129 
130  bool operator==( const TOOL_MANAGER::TOOL_STATE& aRhs ) const
131  {
132  return aRhs.theTool == this->theTool;
133  }
134 
135  bool operator!=( const TOOL_MANAGER::TOOL_STATE& aRhs ) const
136  {
137  return aRhs.theTool != this->theTool;
138  }
139 
145  void Push()
146  {
147  auto state = std::make_unique<TOOL_STATE>( *this );
148  stateStack.push( std::move( state ) );
149  clear();
150  }
151 
158  bool Pop()
159  {
160  delete cofunc;
161 
162  if( !stateStack.empty() )
163  {
164  *this = *stateStack.top().get();
165  stateStack.pop();
166  return true;
167  }
168  else
169  {
170  cofunc = NULL;
171  return false;
172  }
173  }
174 
175 private:
177  std::stack<std::unique_ptr<TOOL_STATE>> stateStack;
178 
180  void clear()
181  {
182  idle = true;
183  pendingWait = false;
184  pendingContextMenu = false;
185  cofunc = NULL;
186  contextMenu = NULL;
188  vcSettings.Reset();
189  transitions.clear();
190  }
191 };
192 
193 
195  m_model( NULL ),
196  m_view( NULL ),
197  m_viewControls( NULL ),
198  m_editFrame( NULL ),
199  m_passEvent( false ),
200  m_menuActive( false ),
201  m_menuOwner( -1 ),
202  m_activeState( nullptr )
203 {
204  m_actionMgr = new ACTION_MANAGER( this );
205 }
206 
207 
209 {
210  std::map<TOOL_BASE*, TOOL_STATE*>::iterator it, it_end;
211 
212  for( it = m_toolState.begin(), it_end = m_toolState.end(); it != it_end; ++it )
213  {
214  delete it->second->cofunc; // delete cofunction
215  delete it->second; // delete TOOL_STATE
216  delete it->first; // delete the tool itself
217  }
218 
219  delete m_actionMgr;
220 }
221 
222 
224 {
225  wxASSERT_MSG( m_toolNameIndex.find( aTool->GetName() ) == m_toolNameIndex.end(),
226  wxT( "Adding two tools with the same name may result in unexpected behaviour.") );
227  wxASSERT_MSG( m_toolIdIndex.find( aTool->GetId() ) == m_toolIdIndex.end(),
228  wxT( "Adding two tools with the same ID may result in unexpected behaviour.") );
229  wxASSERT_MSG( m_toolTypes.find( typeid( *aTool ).name() ) == m_toolTypes.end(),
230  wxT( "Adding two tools of the same type may result in unexpected behaviour.") );
231 
232  TOOL_STATE* st = new TOOL_STATE( aTool );
233 
234  m_toolState[aTool] = st;
235  m_toolNameIndex[aTool->GetName()] = st;
236  m_toolIdIndex[aTool->GetId()] = st;
237  m_toolTypes[typeid( *aTool ).name()] = st->theTool;
238 
239  aTool->attachManager( this );
240 }
241 
242 
244 {
245  TOOL_BASE* tool = FindTool( aToolId );
246 
247  if( tool && tool->GetType() == INTERACTIVE )
248  return invokeTool( tool );
249 
250  return false; // there is no tool with the given id
251 }
252 
253 
254 bool TOOL_MANAGER::InvokeTool( const std::string& aToolName )
255 {
256  TOOL_BASE* tool = FindTool( aToolName );
257 
258  if( tool && tool->GetType() == INTERACTIVE )
259  return invokeTool( tool );
260 
261  return false; // there is no tool with the given name
262 }
263 
264 
266 {
267  m_actionMgr->RegisterAction( aAction );
268 }
269 
270 
272 {
273  m_actionMgr->UnregisterAction( aAction );
274 }
275 
276 
277 bool TOOL_MANAGER::RunAction( const std::string& aActionName, bool aNow, void* aParam )
278 {
279  TOOL_ACTION* action = m_actionMgr->FindAction( aActionName );
280 
281  if( !action )
282  {
283  wxASSERT_MSG( false, wxString::Format( wxT( "Could not find action %s." ), aActionName ) );
284  return false;
285  }
286 
287  RunAction( *action, aNow, aParam );
288 
289  return false;
290 }
291 
292 
293 void TOOL_MANAGER::RunAction( const TOOL_ACTION& aAction, bool aNow, void* aParam )
294 {
295  TOOL_EVENT event = aAction.MakeEvent();
296 
297  // Allow to override the action parameter
298  if( aParam )
299  event.SetParameter( aParam );
300 
301  if( aNow )
302  {
303  TOOL_STATE* current = m_activeState;
304  processEvent( event );
305  setActiveState( current );
306  UpdateUI();
307  }
308  else
309  {
310  PostEvent( event );
311  }
312 }
313 
314 
316 {
317  return m_actionMgr->GetHotKey( aAction );
318 }
319 
320 
322 {
324 }
325 
326 
328 {
329  wxASSERT( aTool != NULL );
330 
331  TOOL_EVENT evt( TC_COMMAND, TA_ACTIVATE, aTool->GetName() );
332  processEvent( evt );
333 
334  if( TOOL_STATE* active = GetCurrentToolState() )
335  setActiveState( active );
336 
337  return true;
338 }
339 
340 
342 {
343  TOOL_BASE* tool = FindTool( aToolId );
344 
345  if( tool && tool->GetType() == INTERACTIVE )
346  return runTool( tool );
347 
348  return false; // there is no tool with the given id
349 }
350 
351 
352 bool TOOL_MANAGER::runTool( const std::string& aToolName )
353 {
354  TOOL_BASE* tool = FindTool( aToolName );
355 
356  if( tool && tool->GetType() == INTERACTIVE )
357  return runTool( tool );
358 
359  return false; // there is no tool with the given name
360 }
361 
362 
364 {
365  wxASSERT( aTool != NULL );
366 
367  if( !isRegistered( aTool ) )
368  {
369  wxASSERT_MSG( false, wxT( "You cannot run unregistered tools" ) );
370  return false;
371  }
372 
373  TOOL_ID id = aTool->GetId();
374 
375  if( aTool->GetType() == INTERACTIVE )
376  static_cast<TOOL_INTERACTIVE*>( aTool )->resetTransitions();
377 
378  // If the tool is already active, bring it to the top of the active tools stack
379  if( isActive( aTool ) )
380  {
381  m_activeTools.erase( std::find( m_activeTools.begin(), m_activeTools.end(), id ) );
382  m_activeTools.push_front( id );
383  return false;
384  }
385 
387  aTool->Reset( TOOL_INTERACTIVE::RUN );
388 
389  // Add the tool on the front of the processing queue (it gets events first)
390  m_activeTools.push_front( id );
391 
392  return true;
393 }
394 
395 
397 {
398  std::map<TOOL_ID, TOOL_STATE*>::const_iterator it = m_toolIdIndex.find( aId );
399 
400  if( it != m_toolIdIndex.end() )
401  return it->second->theTool;
402 
403  return NULL;
404 }
405 
406 
407 TOOL_BASE* TOOL_MANAGER::FindTool( const std::string& aName ) const
408 {
409  std::map<std::string, TOOL_STATE*>::const_iterator it = m_toolNameIndex.find( aName );
410 
411  if( it != m_toolNameIndex.end() )
412  return it->second->theTool;
413 
414  return NULL;
415 }
416 
417 
419 {
420  // Deactivate the active tool, but do not run anything new
422  processEvent( evt );
423 }
424 
425 
427 {
428  DeactivateTool();
429 
430  for( auto& state : m_toolState )
431  {
432  TOOL_BASE* tool = state.first;
433  setActiveState( state.second );
434  tool->Reset( aReason );
435 
436  if( tool->GetType() == INTERACTIVE )
437  static_cast<TOOL_INTERACTIVE*>( tool )->resetTransitions();
438  }
439 }
440 
441 
443 {
444  for( auto it = m_toolState.begin(); it != m_toolState.end(); /* iteration in the loop */ )
445  {
446  TOOL_BASE* tool = it->first;
447  TOOL_STATE* state = it->second;
448  setActiveState( state );
449  ++it; // keep the iterator valid if the element is going to be erased
450 
451  if( !tool->Init() )
452  {
453  wxMessageBox(
454  wxString::Format( "Initialization of tool \"%s\" failed", tool->GetName() ) );
455 
456  // Unregister the tool
457  setActiveState( nullptr );
458  m_toolState.erase( tool );
459  m_toolNameIndex.erase( tool->GetName() );
460  m_toolIdIndex.erase( tool->GetId() );
461  m_toolTypes.erase( typeid( *tool ).name() );
462 
463  delete state;
464  delete tool;
465  }
466  }
467 
469 }
470 
471 
472 int TOOL_MANAGER::GetPriority( int aToolId ) const
473 {
474  int priority = 0;
475 
476  for( auto it = m_activeTools.begin(), itEnd = m_activeTools.end(); it != itEnd; ++it )
477  {
478  if( *it == aToolId )
479  return priority;
480 
481  ++priority;
482  }
483 
484  return -1;
485 }
486 
487 
489  const TOOL_EVENT_LIST& aConditions )
490 {
491  TOOL_STATE* st = m_toolState[aTool];
492 
493  st->transitions.push_back( TRANSITION( aConditions, aHandler ) );
494 }
495 
496 
498 {
499  m_toolState[aTool]->transitions.clear();
500 }
501 
502 
503 void TOOL_MANAGER::RunMainStack( TOOL_BASE* aTool, std::function<void()> aFunc )
504 {
505  TOOL_STATE* st = m_toolState[aTool];
506  setActiveState( st );
507  st->cofunc->RunMainStack( std::move( aFunc ) );
508 }
509 
510 
512 {
513  TOOL_STATE* st = m_toolState[aTool];
514 
515  wxASSERT( !st->pendingWait ); // everything collapses on two KiYield() in a row
516 
517  // indicate to the manager that we are going to sleep and we shall be
518  // woken up when an event matching aConditions arrive
519  st->pendingWait = true;
520  st->waitEvents = aConditions;
521 
522  // switch context back to event dispatcher loop
523  st->cofunc->KiYield();
524 
525  return st->wakeupEvent;
526 }
527 
528 
530 {
531  // iterate over all registered tools
532  for( auto it = m_activeTools.begin(); it != m_activeTools.end(); ++it )
533  {
534  TOOL_STATE* st = m_toolIdIndex[*it];
535 
536  // forward context menu events to the tool that created the menu
537  if( aEvent.IsMenu() )
538  {
539  if( *it != m_menuOwner )
540  continue;
541  }
542 
543  // the tool state handler is waiting for events (i.e. called Wait() method)
544  if( st->pendingWait )
545  {
546  if( st->waitEvents.Matches( aEvent ) )
547  {
548  // By default only messages are passed further
549  m_passEvent = ( aEvent.Category() == TC_MESSAGE );
550 
551  // got matching event? clear wait list and wake up the coroutine
552  st->wakeupEvent = aEvent;
553  st->pendingWait = false;
554  st->waitEvents.clear();
555 
556  if( st->cofunc )
557  {
558  setActiveState( st );
559  bool end = !st->cofunc->Resume();
560 
561  if( end )
562  it = finishTool( st );
563  }
564 
565  // If the tool did not request to propagate
566  // the event to other tools, we should stop it now
567  if( !m_passEvent )
568  break;
569  }
570  }
571  }
572 
573  for( auto& state : m_toolState )
574  {
575  TOOL_STATE* st = state.second;
576  bool finished = false;
577 
578  // no state handler in progress - check if there are any transitions (defined by
579  // Go() method that match the event.
580  if( !st->transitions.empty() )
581  {
582  for( TRANSITION& tr : st->transitions )
583  {
584  if( tr.first.Matches( aEvent ) )
585  {
586  auto func_copy = tr.second;
587 
588  // if there is already a context, then push it on the stack
589  // and transfer the previous view control settings to the new context
590  if( st->cofunc )
591  {
592  auto vc = st->vcSettings;
593  st->Push();
594  st->vcSettings = vc;
595  }
596 
597  st->cofunc = new COROUTINE<int, const TOOL_EVENT&>( std::move( func_copy ) );
598 
599  // as the state changes, the transition table has to be set up again
600  st->transitions.clear();
601 
602  // got match? Run the handler.
603  setActiveState( st );
604  st->idle = false;
605  st->cofunc->Call( aEvent );
606 
607  if( !st->cofunc->Running() )
608  finishTool( st ); // The couroutine has finished immediately?
609 
610  // if it is a message, continue processing
611  finished = !( aEvent.Category() == TC_MESSAGE );
612 
613  // there is no point in further checking, as transitions got cleared
614  break;
615  }
616  }
617  }
618 
619  if( finished )
620  break; // only the first tool gets the event
621  }
622 }
623 
624 
626 {
627  if( aEvent.Action() == TA_KEY_PRESSED )
628  {
629  // Check if there is a hotkey associated
630  if( m_actionMgr->RunHotKey( aEvent.Modifier() | aEvent.KeyCode() ) )
631  return false; // hotkey event was handled so it does not go any further
632  }
633 
634  return true;
635 }
636 
637 
639 {
640  if( aEvent.IsActivate() )
641  {
642  wxString cmdStr( *aEvent.GetCommandStr() );
643 
644  std::map<std::string, TOOL_STATE*>::iterator tool = m_toolNameIndex.find( *aEvent.GetCommandStr() );
645 
646  if( tool != m_toolNameIndex.end() )
647  {
648  runTool( tool->second->theTool );
649  return true;
650  }
651  }
652 
653  return false;
654 }
655 
657 {
658  for( TOOL_ID toolId : m_activeTools )
659  {
660  TOOL_STATE* st = m_toolIdIndex[toolId];
661 
662  // the tool requested a context menu. The menu is activated on RMB click (CMENU_BUTTON mode)
663  // or immediately (CMENU_NOW) mode. The latter is used for clarification lists.
664  if( st->contextMenuTrigger == CMENU_OFF )
665  continue;
666 
667  if( st->contextMenuTrigger == CMENU_BUTTON && !aEvent.IsClick( BUT_RIGHT ) )
668  break;
669 
670  st->pendingWait = true;
672 
673  // Store the menu pointer in case it is changed by the TOOL when handling menu events
674  ACTION_MENU* m = st->contextMenu;
675 
676  if( st->contextMenuTrigger == CMENU_NOW )
678 
679  // Store the cursor position, so the tools could execute actions
680  // using the point where the user has invoked a context menu
682 
683  // Save all tools cursor settings, as they will be overridden
684  for( auto idState : m_toolIdIndex )
685  {
686  TOOL_STATE* s = idState.second;
687  const auto& vc = s->vcSettings;
688 
689  if( vc.m_forceCursorPosition )
690  m_cursorSettings[idState.first] = vc.m_forcedPosition;
691  else
692  m_cursorSettings[idState.first] = NULLOPT;
693  }
694 
696 
697  // Display a copy of menu
698  std::unique_ptr<ACTION_MENU> menu( m->Clone() );
699 
700  m_menuOwner = toolId;
701  m_menuActive = true;
702 
703  auto frame = dynamic_cast<wxFrame*>( m_editFrame );
704 
705  if( frame )
706  frame->PopupMenu( menu.get() );
707 
708  // Warp the cursor if a menu item was selected
709  if( menu->GetSelected() >= 0 && m_warpMouseAfterContextMenu )
710  m_viewControls->WarpCursor( m_menuCursor, true, false );
711  // Otherwise notify the tool of a cancelled menu
712  else
713  {
715  evt.SetParameter( m );
716  dispatchInternal( evt );
717  }
718 
719  // Restore setting in case it was vetoed
721 
722  // Notify the tools that menu has been closed
724  evt.SetParameter( m );
725  dispatchInternal( evt );
726 
727  m_menuActive = false;
728  m_menuOwner = -1;
729 
730  // Restore cursor settings
731  for( auto cursorSetting : m_cursorSettings )
732  {
733  auto it = m_toolIdIndex.find( cursorSetting.first );
734  wxASSERT( it != m_toolIdIndex.end() );
735 
736  if( it == m_toolIdIndex.end() )
737  continue;
738 
739  KIGFX::VC_SETTINGS& vc = it->second->vcSettings;
740  vc.m_forceCursorPosition = (bool) cursorSetting.second;
741  vc.m_forcedPosition = cursorSetting.second ? *cursorSetting.second : VECTOR2D( 0, 0 );
742  }
743 
744  m_cursorSettings.clear();
745  break;
746  }
747 }
748 
749 
750 TOOL_MANAGER::ID_LIST::iterator TOOL_MANAGER::finishTool( TOOL_STATE* aState )
751 {
752  auto it = std::find( m_activeTools.begin(), m_activeTools.end(), aState->theTool->GetId() );
753 
754  if( !aState->Pop() )
755  {
756  // Deactivate the tool if there are no other contexts saved on the stack
757  if( it != m_activeTools.end() )
758  it = m_activeTools.erase( it );
759 
760  aState->idle = true;
761  }
762 
763  if( aState == m_activeState )
764  setActiveState( nullptr );
765 
766  // Set transitions to be ready for future TOOL_EVENTs
767  TOOL_BASE* tool = aState->theTool;
768 
769  if( tool->GetType() == INTERACTIVE )
770  static_cast<TOOL_INTERACTIVE*>( tool )->resetTransitions();
771 
772  return it;
773 }
774 
775 
777 {
778  bool hotkey_handled = processEvent( aEvent );
779 
780  if( TOOL_STATE* active = GetCurrentToolState() )
781  setActiveState( active );
782 
783  if( m_view->IsDirty() )
784  {
785  auto f = dynamic_cast<EDA_DRAW_FRAME*>( GetEditFrame() );
786 
787  if( f )
788  f->GetGalCanvas()->Refresh(); // fixme: ugly hack, provide a method in TOOL_DISPATCHER.
789 
790 #if defined( __WXMAC__ ) || defined( __WINDOWS__ )
791  wxTheApp->ProcessPendingEvents(); // required for updating brightening behind a popup menu
792 #endif
793  }
794 
795  UpdateUI();
796 
797  return hotkey_handled;
798 }
799 
800 
802  CONTEXT_MENU_TRIGGER aTrigger )
803 {
804  TOOL_STATE* st = m_toolState[aTool];
805 
806  st->contextMenu = aMenu;
807  st->contextMenuTrigger = aTrigger;
808 }
809 
810 
811 bool TOOL_MANAGER::SaveClipboard( const std::string& aText )
812 {
813  if( wxTheClipboard->Open() )
814  {
815  wxTheClipboard->SetData( new wxTextDataObject( wxString( aText.c_str(), wxConvUTF8 ) ) );
816  wxTheClipboard->Close();
817 
818  return true;
819  }
820 
821  return false;
822 }
823 
824 
825 std::string TOOL_MANAGER::GetClipboard() const
826 {
827  std::string result;
828 
829  if( wxTheClipboard->Open() )
830  {
831  if( wxTheClipboard->IsSupported( wxDF_TEXT ) )
832  {
833  wxTextDataObject data;
834  wxTheClipboard->GetData( data );
835 
836  result = data.GetText().mb_str();
837  }
838 
839  wxTheClipboard->Close();
840  }
841 
842  return result;
843 }
844 
845 
847 {
848  if( TOOL_STATE* active = GetCurrentToolState() )
849  return active->vcSettings;
850 
851  return m_viewControls->GetSettings();
852 }
853 
854 
855 TOOL_ID TOOL_MANAGER::MakeToolId( const std::string& aToolName )
856 {
857  static int currentId;
858 
859  return currentId++;
860 }
861 
862 
864  KIGFX::VIEW_CONTROLS* aViewControls, EDA_DRAW_FRAME* aFrame )
865 {
866  m_model = aModel;
867  m_view = aView;
868  m_viewControls = aViewControls;
869  m_editFrame = aFrame;
871 }
872 
873 
875 {
876  if( !isRegistered( aTool ) )
877  return false;
878 
879  // Just check if the tool is on the active tools stack
880  return std::find( m_activeTools.begin(), m_activeTools.end(), aTool->GetId() ) != m_activeTools.end();
881 }
882 
883 
885 {
887 
888  if( m_menuActive )
889  {
890  // Context menu is active, so the cursor settings are overridden (see dispatchContextMenu())
891  auto it = m_cursorSettings.find( aState->theTool->GetId() );
892 
893  if( it != m_cursorSettings.end() )
894  {
896 
897  // Tool has overridden the cursor position, so store the new settings
899  {
900  if( !curr.m_forceCursorPosition )
901  it->second = NULLOPT;
902  else
903  it->second = curr.m_forcedPosition;
904  }
905  else
906  {
907  OPT<VECTOR2D> cursor = it->second;
908 
909  if( cursor )
910  {
911  aState->vcSettings.m_forceCursorPosition = true;
912  aState->vcSettings.m_forcedPosition = *cursor;
913  }
914  else
915  {
916  aState->vcSettings.m_forceCursorPosition = false;
917  }
918  }
919  }
920  }
921 }
922 
923 
925 {
927 }
928 
929 
931 {
932  // Early dispatch of events destined for the TOOL_MANAGER
933  if( !dispatchStandardEvents( aEvent ) )
934  return true;
935 
936  dispatchInternal( aEvent );
937  dispatchActivation( aEvent );
938  dispatchContextMenu( aEvent );
939 
940  // Dispatch queue
941  while( !m_eventQueue.empty() )
942  {
943  TOOL_EVENT event = m_eventQueue.front();
944  m_eventQueue.pop_front();
945  processEvent( event );
946  }
947 
948  return false;
949 }
950 
951 
953 {
954  if( m_activeState )
956 
957  m_activeState = aState;
958 
959  if( m_activeState )
960  applyViewControls( aState );
961 }
962 
963 
965 {
966  auto it = m_toolIdIndex.find( aId );
967  return !it->second->idle;
968 }
969 
970 
972 {
973  EDA_DRAW_FRAME* frame = GetEditFrame();
974 
975  if( frame )
976  {
977  frame->UpdateStatusBar();
978  frame->SyncMenusAndToolbars();
979  }
980 }
void RunMainStack(std::function< void()> func)
Function RunMainStack()
Definition: coroutine.h:193
bool m_menuActive
Flag indicating whether a context menu is currently displayed.
Definition: tool_manager.h:553
TOOL_TYPE GetType() const
Function GetType() Returns the type of the tool.
Definition: tool_base.h:111
VECTOR2D m_menuCursor
Right click context menu position.
Definition: tool_manager.h:548
virtual bool Init()
Function Init() Init() is called once upon a registration of the tool.
Definition: tool_base.h:93
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:531
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.
int GetPriority(int aToolId) const
Returns priority of a given tool.
std::vector< TRANSITION > transitions
List of possible transitions (ie.
Class ACTION_MENU.
Definition: action_menu.h:43
ID_STATE_MAP m_toolIdIndex
Index of the registered tools current states, associated by tools' ID numbers.
Definition: tool_manager.h:522
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 ...
void UpdateUI()
Synchronizes toolbars.
OPT< const TOOL_EVENT & > Matches(const TOOL_EVENT &aEvent) const
Definition: tool_event.h:519
bool SaveClipboard(const std::string &aText)
Stores an information to the system clipboard.
bool Call(ArgType aArg)
Function Call()
Definition: coroutine.h:207
void UpdateHotKeys()
Function UpdateHotKeys() Updates TOOL_ACTIONs hot key assignment according to the current frame's Hot...
TOOL_ACTIONS Action() const
Returns more specific information about the type of an event.
Definition: tool_event.h:239
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.
TOOL_ID GetId() const
Function GetId() Returns the unique identifier of the tool.
Definition: tool_base.h:122
bool IsClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.cpp:165
std::map< TOOL_ID, OPT< VECTOR2D > > m_cursorSettings
Original cursor position, if overridden by the context menu handler.
Definition: tool_manager.h:534
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.
EDA_DRAW_FRAME * GetEditFrame() const
Definition: tool_manager.h:267
VECTOR2D m_forcedPosition
Forced cursor position (world coordinates)
Definition: view_controls.h:58
ACTION_MENU * contextMenu
Context menu currently used by the tool.
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:125
void SetEnvironment(EDA_ITEM *aModel, KIGFX::VIEW *aView, KIGFX::VIEW_CONTROLS *aViewControls, EDA_DRAW_FRAME *aFrame)
Sets the work environment (model, view, view controls and the parent window).
bool operator==(const TOOL_MANAGER::TOOL_STATE &aRhs) const
EDA_DRAW_FRAME * m_editFrame
Definition: tool_manager.h:539
TOOL_EVENT_CATEGORY Category() const
Returns the category (eg. mouse/keyboard/action) of an event..
Definition: tool_event.h:233
void attachManager(TOOL_MANAGER *aManager)
Function attachManager()
Definition: tool_base.cpp:59
Class ACTION_MANAGER.
TOOL_BASE * FindTool(int aId) const
Function FindTool() Searches for a tool with given ID.
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:82
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:545
TOOL_ACTION * FindAction(const std::string &aActionName) const
Function FindAction() Finds an action with a given name (if there is one available).
const VC_SETTINGS & GetSettings() const
Returns the current VIEW_CONTROLS settings
bool RunHotKey(int aHotKey) const
Function RunHotKey() Runs an action associated with a hotkey (if there is one available).
EDA_ITEM * m_model
Definition: tool_manager.h:536
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:390
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' names.
Definition: tool_manager.h:519
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:494
Tool that interacts with the user
Definition: tool_base.h:50
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:556
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:155
void dispatchInternal(const TOOL_EVENT &aEvent)
Function dispatchInternal Passes an event at first to the active tools, then to all others.
virtual void SyncMenusAndToolbars()
Update the toolbars and menus (mostly settings/check buttons/checkboxes) with the current controller ...
Definition: draw_frame.h:955
int GetHotKey(const TOOL_ACTION &aAction) const
Function GetHotKey() Returns the hot key associated with a given action or 0 if there is none.
Class TOOL_EVENT.
Definition: tool_event.h:167
bool idle
Is the tool active (pending execution) or disabled at the moment.
std::stack< std::unique_ptr< TOOL_STATE > > stateStack
Stack preserving previous states of a TOOL.
bool operator!=(const TOOL_MANAGER::TOOL_STATE &aRhs) const
const KIGFX::VC_SETTINGS & GetCurrentToolVC() const
Returns the view controls settings for the current tool or the general settings if there is no active...
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.
Class VIEW_CONTROLS is an interface for classes handling user events controlling the view behaviour (...
ACTION_MENU * Clone() const
Creates a deep, recursive copy of this ACTION_MENU.
virtual void ForceCursorPosition(bool aEnabled, const VECTOR2D &aPosition=VECTOR2D(0, 0))
Function ForceCursorPosition() Places the cursor immediately at a given point.
Struct describing the current execution state of a TOOL.
KIGFX::VIEW_CONTROLS * m_viewControls
Definition: tool_manager.h:538
bool Resume()
Function Resume()
Definition: coroutine.h:241
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_STATE * GetCurrentToolState() const
Returns the TOOL_STATE object representing the state of the active tool.
Definition: tool_manager.h:296
bool isActive(TOOL_BASE *aTool)
Function isActive() Returns information about a tool activation status.
KIGFX::VIEW * m_view
Definition: tool_manager.h:537
void RunMainStack(TOOL_BASE *aTool, std::function< void()> aFunc)
TOOL_EVENT wakeupEvent
The event that triggered the execution/wakeup of the tool after Wait() call.
int Modifier(int aMask=MD_MODIFIER_MASK) const
Returns information about key modifiers state (Ctrl, Alt, etc.)
Definition: tool_event.h:316
bool m_warpMouseAfterContextMenu
Definition: tool_manager.h:550
bool isRegistered(TOOL_BASE *aTool) const
Function isRegistered() Returns information about a tool registration status.
Definition: tool_manager.h:477
OPT< std::string > GetCommandStr() const
Definition: tool_event.h:400
void RegisterAction(TOOL_ACTION *aAction)
Function RegisterAction() Adds a tool action to the manager and sets it up.
bool IsActivate() const
Definition: tool_event.h:300
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:542
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:559
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
std::string GetClipboard() const
Returns the information currently stored in the system clipboard.
TOOL_EVENT MakeEvent() const
Function MakeEvent() Returns the event associated with the action (i.e.
Definition: tool_action.h:95
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:157
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:80
boost::optional< T > OPT
Definition: optional.h:7
bool Running() const
Function Running()
Definition: coroutine.h:282
CONTEXT_MENU_TRIGGER contextMenuTrigger
Defines when the context menu is opened.
bool IsMenu() const
Definition: tool_event.h:310
void InitTools()
Function InitTools() Initializes all registered tools.
bool IsToolActive(TOOL_ID aId) const
Function IsToolActive() Returns true if a tool with given id is active (executing)
const std::string & GetName() const
Function GetName() Returns the name of the tool.
Definition: tool_base.h:133
Class VIEW.
Definition: view.h:61
void ClearTransitions(TOOL_BASE *aTool)
Clears the state transition map for a tool.
int KeyCode() const
Definition: tool_event.h:321
ID_LIST m_activeTools
Stack of the active tools.
Definition: tool_manager.h:528
TOOL_EVENT_LIST waitEvents
List of events the tool is currently waiting for.
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' objects.
Definition: tool_manager.h:516
virtual void UpdateStatusBar()
Update the status bar information.
void ScheduleContextMenu(TOOL_BASE *aTool, ACTION_MENU *aMenu, CONTEXT_MENU_TRIGGER aTrigger)
Sets behaviour of the tool's context popup menu.
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,...
bool IsDirty() const
Function IsDirty() Returns true if any of the VIEW layers needs to be refreshened.
Definition: view.h:568
VECTOR2D GetCursorPosition() const
Returns the current cursor position in world coordinates.
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:158
std::pair< TOOL_EVENT_LIST, TOOL_STATE_FUNC > TRANSITION
Definition: tool_manager.h:392
std::map< const char *, TOOL_BASE * > m_toolTypes
Index of the registered tools to easily lookup by their type.
Definition: tool_manager.h:525