KiCad PCB EDA Suite
tool_event.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 CERN
5  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <cstring>
26 #include <string>
27 
28 #include <tool/tool_event.h>
29 #include <tool/tool_action.h>
30 #include <tool/tool_manager.h>
31 #include <tool/actions.h>
32 
33 struct FlagString
34 {
35  int flag;
36  std::string str;
37 };
38 
39 
40 static const std::string flag2string( int aFlag, const FlagString* aExps )
41 {
42  std::string rv;
43 
44  for( int i = 0; aExps[i].str.length(); i++ )
45  {
46  if( aExps[i].flag & aFlag )
47  rv += aExps[i].str + " ";
48  }
49 
50  return rv;
51 }
52 
53 
55 {
56  // By default only MESSAGEs and Cancels are passed to multiple recipients
58 
60 
61  // Cancel tool doesn't contain a position
62  if( IsCancel() )
63  m_hasPosition = false;
64 
65  m_forceImmediate = false;
66  m_reactivate = false;
67 }
68 
69 
70 bool TOOL_EVENT::IsAction( const TOOL_ACTION* aAction ) const
71 {
72  return Matches( aAction->MakeEvent() );
73 }
74 
75 
76 const std::string TOOL_EVENT::Format() const
77 {
78  std::string ev;
79 
80  const FlagString categories[] =
81  {
82  { TC_MOUSE, "mouse" },
83  { TC_KEYBOARD, "keyboard" },
84  { TC_COMMAND, "command" },
85  { TC_MESSAGE, "message" },
86  { TC_VIEW, "view" },
87  { 0, "" }
88  };
89 
90  const FlagString actions[] =
91  {
92  { TA_MOUSE_CLICK, "click" },
93  { TA_MOUSE_DBLCLICK, "double click" },
94  { TA_MOUSE_UP, "button-up" },
95  { TA_MOUSE_DOWN, "button-down" },
96  { TA_MOUSE_DRAG, "drag" },
97  { TA_MOUSE_MOTION, "motion" },
98  { TA_MOUSE_WHEEL, "wheel" },
99  { TA_KEY_PRESSED, "key-pressed" },
100  { TA_VIEW_REFRESH, "view-refresh" },
101  { TA_VIEW_ZOOM, "view-zoom" },
102  { TA_VIEW_PAN, "view-pan" },
103  { TA_VIEW_DIRTY, "view-dirty" },
104  { TA_CHANGE_LAYER, "change-layer" },
105  { TA_CANCEL_TOOL, "cancel-tool" },
106  { TA_CHOICE_MENU_UPDATE, "choice-menu-update" },
107  { TA_CHOICE_MENU_CHOICE, "choice-menu-choice" },
108  { TA_UNDO_REDO_PRE, "undo-redo-pre" },
109  { TA_UNDO_REDO_POST, "undo-redo-post" },
110  { TA_ACTION, "action" },
111  { TA_ACTIVATE, "activate" },
112  { 0, "" }
113  };
114 
115  const FlagString buttons[] =
116  {
117  { BUT_NONE, "none" },
118  { BUT_LEFT, "left" },
119  { BUT_RIGHT, "right" },
120  { BUT_MIDDLE, "middle" },
121  { 0, "" }
122  };
123 
124  const FlagString modifiers[] =
125  {
126  { MD_SHIFT, "shift" },
127  { MD_CTRL, "ctrl" },
128  { MD_ALT, "alt" },
129  { 0, "" }
130  };
131 
132  ev = "category: ";
133  ev += flag2string( m_category, categories );
134  ev += " action: ";
135  ev += flag2string( m_actions, actions );
136 
137  if( m_actions & TA_MOUSE )
138  {
139  ev += " btns: ";
140  ev += flag2string( m_mouseButtons, buttons );
141  }
142 
143  if( m_actions & TA_KEYBOARD )
144  {
145  char tmp[128];
146  sprintf( tmp, "key: %d", m_keyCode );
147  ev += tmp;
148  }
149 
150  if( m_actions & ( TA_MOUSE | TA_KEYBOARD ) )
151  {
152  ev += " mods: ";
153  ev += flag2string( m_modifiers, modifiers );
154  }
155 
156  if( m_commandId )
157  {
158  char tmp[128];
159  sprintf( tmp, "cmd-id: %d", *m_commandId );
160  ev += tmp;
161  }
162 
163  if( m_commandStr )
164  ev += "cmd-str: " + ( *m_commandStr );
165 
166  return ev;
167 }
168 
169 
170 const std::string TOOL_EVENT_LIST::Format() const
171 {
172  std::string s;
173 
174  for( const TOOL_EVENT& e : m_events )
175  s += e.Format() + " ";
176 
177  return s;
178 }
179 
180 
181 bool TOOL_EVENT::IsClick( int aButtonMask ) const
182 {
183  return ( m_actions & TA_MOUSE_CLICK ) && ( m_mouseButtons & aButtonMask ) == m_mouseButtons;
184 }
185 
186 
187 bool TOOL_EVENT::IsDblClick( int aButtonMask ) const
188 {
189  return m_actions == TA_MOUSE_DBLCLICK && ( m_mouseButtons & aButtonMask ) == m_mouseButtons;
190 }
191 
192 
194 {
195  return( ( m_commandStr.is_initialized()
197  || ( m_commandId.is_initialized()
199  || ( m_actions == TA_CANCEL_TOOL ) );
200 }
201 
202 
204 {
205  return Matches( EVENTS::ClearedEvent )
208 }
209 
210 
212 {
213  return( ( m_commandStr.is_initialized()
214  && m_commandStr.get().find( "PointEditor" ) != GetCommandStr()->npos )
215  || ( m_commandId.is_initialized()
217 }
218 
219 
221 {
222  return( m_commandStr.is_initialized()
223  && m_commandStr.get().find( "InteractiveMove" ) != GetCommandStr()->npos );
224 }
225 
226 
228 {
229  return( m_commandStr.is_initialized()
230  && m_commandStr.get().find( "Simulation" ) != GetCommandStr()->npos );
231 }
bool IsCancel() const
Definition: tool_event.h:326
bool m_hasPosition
Definition: tool_event.h:534
static const TOOL_EVENT SelectedEvent
Definition: actions.h:209
bool m_forceImmediate
True when the tool is being re-activated from the stack.
Definition: tool_event.h:535
const std::string Format() const
Return information about event in form of a human-readable string.
Definition: tool_event.cpp:76
static TOOL_ACTION activatePointEditor
Definition: actions.h:171
bool IsSelectionEvent() const
Indicate an selection-changed notification event.
Definition: tool_event.cpp:203
void init()
Definition: tool_event.cpp:54
static const TOOL_EVENT UnselectedEvent
Definition: actions.h:210
int m_modifiers
Generic parameter used for passing non-standard data.
Definition: tool_event.h:557
bool IsClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.cpp:181
TOOL_ACTIONS m_actions
Definition: tool_event.h:531
static TOOL_ACTION cancelInteractive
Definition: actions.h:65
int m_mouseButtons
Stores code of pressed/released key.
Definition: tool_event.h:551
OPT< std::string > m_commandStr
Definition: tool_event.h:566
bool IsMoveTool() const
Indicate if the event is from one of the move tools.
Definition: tool_event.cpp:220
int m_keyCode
State of key modifiers (Ctrl/Alt/etc.)
Definition: tool_event.h:554
OPT< int > m_commandId
Definition: tool_event.h:565
bool IsCancelInteractive() const
Indicate the event should restart/end an ongoing interactive tool's event loop (eg esc key,...
Definition: tool_event.cpp:193
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
Definition: tool_event.cpp:70
std::deque< TOOL_EVENT > m_events
Definition: tool_event.h:684
bool m_passEvent
Definition: tool_event.h:533
bool IsSimulator() const
Indicate if the event is from the simulator.
Definition: tool_event.cpp:227
bool IsDblClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.cpp:187
Generic, UI-independent tool event.
Definition: tool_event.h:173
bool IsPointEditor() const
Indicate if the event is from one of the point editors.
Definition: tool_event.cpp:211
static const TOOL_EVENT ClearedEvent
Selected item had a property changed (except movement)
Definition: actions.h:211
bool Matches(const TOOL_EVENT &aEvent) const
Test whether two events match in terms of category & action or command.
Definition: tool_event.h:373
bool m_reactivate
Difference between mouse cursor position and the point where dragging event has started.
Definition: tool_event.h:538
const std::string Format() const
Function Format() Returns information about event in form of a human-readable string.
Definition: tool_event.cpp:170
static const std::string flag2string(int aFlag, const FlagString *aExps)
Definition: tool_event.cpp:40
OPT< std::string > GetCommandStr() const
Definition: tool_event.h:471
bool IsActivate() const
Definition: tool_event.h:331
int GetId() const
Return the unique id of the TOOL_ACTION object.
Definition: tool_action.h:103
std::string str
Definition: tool_event.cpp:36
Represent a single user action.
Definition: tool_action.h:49
TOOL_EVENT MakeEvent() const
Return the event associated with the action (i.e.
Definition: tool_action.h:123
TOOL_EVENT_CATEGORY m_category
Definition: tool_event.h:530
const std::string & GetName() const
Return name of the action.
Definition: tool_action.h:83