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 
62 
63 bool TOOL_EVENT::IsAction( const TOOL_ACTION* aAction ) const
64 {
65  return Matches( aAction->MakeEvent() );
66 }
67 
68 
69 const std::string TOOL_EVENT::Format() const
70 {
71  std::string ev;
72 
73  const FlagString categories[] =
74  {
75  { TC_MOUSE, "mouse" },
76  { TC_KEYBOARD, "keyboard" },
77  { TC_COMMAND, "command" },
78  { TC_MESSAGE, "message" },
79  { TC_VIEW, "view" },
80  { 0, "" }
81  };
82 
83  const FlagString actions[] =
84  {
85  { TA_MOUSE_CLICK, "click" },
86  { TA_MOUSE_DBLCLICK, "double click" },
87  { TA_MOUSE_UP, "button-up" },
88  { TA_MOUSE_DOWN, "button-down" },
89  { TA_MOUSE_DRAG, "drag" },
90  { TA_MOUSE_MOTION, "motion" },
91  { TA_MOUSE_WHEEL, "wheel" },
92  { TA_KEY_PRESSED, "key-pressed" },
93  { TA_VIEW_REFRESH, "view-refresh" },
94  { TA_VIEW_ZOOM, "view-zoom" },
95  { TA_VIEW_PAN, "view-pan" },
96  { TA_VIEW_DIRTY, "view-dirty" },
97  { TA_CHANGE_LAYER, "change-layer" },
98  { TA_CANCEL_TOOL, "cancel-tool" },
99  { TA_CHOICE_MENU_UPDATE, "choice-menu-update" },
100  { TA_CHOICE_MENU_CHOICE, "choice-menu-choice" },
101  { TA_UNDO_REDO_PRE, "undo-redo-pre" },
102  { TA_UNDO_REDO_POST, "undo-redo-post" },
103  { TA_ACTION, "action" },
104  { TA_ACTIVATE, "activate" },
105  { 0, "" }
106  };
107 
108  const FlagString buttons[] =
109  {
110  { BUT_NONE, "none" },
111  { BUT_LEFT, "left" },
112  { BUT_RIGHT, "right" },
113  { BUT_MIDDLE, "middle" },
114  { 0, "" }
115  };
116 
117  const FlagString modifiers[] =
118  {
119  { MD_SHIFT, "shift" },
120  { MD_CTRL, "ctrl" },
121  { MD_ALT, "alt" },
122  { 0, "" }
123  };
124 
125  ev = "category: ";
126  ev += flag2string( m_category, categories );
127  ev += " action: ";
128  ev += flag2string( m_actions, actions );
129 
130  if( m_actions & TA_MOUSE )
131  {
132  ev += " btns: ";
133  ev += flag2string( m_mouseButtons, buttons );
134  }
135 
136  if( m_actions & TA_KEYBOARD )
137  {
138  char tmp[128];
139  sprintf( tmp, "key: %d", m_keyCode );
140  ev += tmp;
141  }
142 
143  if( m_actions & ( TA_MOUSE | TA_KEYBOARD ) )
144  {
145  ev += " mods: ";
146  ev += flag2string( m_modifiers, modifiers );
147  }
148 
149  if( m_commandId )
150  {
151  char tmp[128];
152  sprintf( tmp, "cmd-id: %d", *m_commandId );
153  ev += tmp;
154  }
155 
156  if( m_commandStr )
157  ev += "cmd-str: " + ( *m_commandStr );
158 
159  return ev;
160 }
161 
162 
163 const std::string TOOL_EVENT_LIST::Format() const
164 {
165  std::string s;
166 
167  for( const TOOL_EVENT& e : m_events )
168  s += e.Format() + " ";
169 
170  return s;
171 }
172 
173 
174 bool TOOL_EVENT::IsClick( int aButtonMask ) const
175 {
176  return m_actions == TA_MOUSE_CLICK && ( m_mouseButtons & aButtonMask ) == m_mouseButtons;
177 }
178 
179 
180 bool TOOL_EVENT::IsDblClick( int aButtonMask ) const
181 {
182  return m_actions == TA_MOUSE_DBLCLICK && ( m_mouseButtons & aButtonMask ) == m_mouseButtons;
183 }
184 
185 
187 {
188  return( ( m_commandStr.is_initialized()
190  || ( m_commandId.is_initialized()
192  || ( m_actions == TA_CANCEL_TOOL ) );
193 }
194 
195 
197 {
198  return Matches( EVENTS::ClearedEvent )
201 }
202 
203 
205 {
206  return( ( m_commandStr.is_initialized()
207  && m_commandStr.get().find( "PointEditor" ) != GetCommandStr()->npos )
208  || ( m_commandId.is_initialized()
210 }
211 
212 
214 {
215  return( m_commandStr.is_initialized()
216  && m_commandStr.get().find( "InteractiveMove" ) != GetCommandStr()->npos );
217 }
bool m_hasPosition
Definition: tool_event.h:517
static const TOOL_EVENT SelectedEvent
Definition: actions.h:196
const std::string Format() const
Function Format() Returns information about event in form of a human-readable string.
Definition: tool_event.cpp:69
static TOOL_ACTION activatePointEditor
Definition: actions.h:158
void init()
Definition: tool_event.cpp:54
static const TOOL_EVENT UnselectedEvent
Definition: actions.h:197
int m_modifiers
State of key modifierts (Ctrl/Alt/etc.)
Definition: tool_event.h:536
bool IsClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.cpp:174
TOOL_ACTIONS m_actions
Definition: tool_event.h:514
bool IsPointEditor()
Function IsPointEditor.
Definition: tool_event.cpp:204
static TOOL_ACTION cancelInteractive
Definition: actions.h:65
int m_mouseButtons
State of mouse buttons
Definition: tool_event.h:530
OPT< std::string > m_commandStr
Definition: tool_event.h:545
int m_keyCode
Stores code of pressed/released key
Definition: tool_event.h:533
OPT< int > m_commandId
Definition: tool_event.h:544
bool IsAction(const TOOL_ACTION *aAction) const
Function IsAction() Tests if the event contains an action issued upon activation of the given TOOL_AC...
Definition: tool_event.cpp:63
std::deque< TOOL_EVENT > m_events
Definition: tool_event.h:662
bool m_passEvent
Definition: tool_event.h:516
bool IsMoveTool()
Function IsMoveTool.
Definition: tool_event.cpp:213
bool IsDblClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.cpp:180
Class TOOL_EVENT.
Definition: tool_event.h:168
static const TOOL_EVENT ClearedEvent
Definition: actions.h:198
bool Matches(const TOOL_EVENT &aEvent) const
Function Matches() Tests whether two events match in terms of category & action or command.
Definition: tool_event.h:356
const std::string Format() const
Function Format() Returns information about event in form of a human-readable string.
Definition: tool_event.cpp:163
bool IsCancelInteractive()
Function IsCancelInteractive()
Definition: tool_event.cpp:186
bool IsSelectionEvent()
Function IsSelectionEvent()
Definition: tool_event.cpp:196
static const std::string flag2string(int aFlag, const FlagString *aExps)
Definition: tool_event.cpp:40
OPT< std::string > GetCommandStr() const
Definition: tool_event.h:455
bool IsActivate() const
Definition: tool_event.h:318
int GetId() const
Function GetId() Returns the unique id of the TOOL_ACTION object.
Definition: tool_action.h:100
std::string str
Definition: tool_event.cpp:36
Class TOOL_ACTION.
Definition: tool_action.h:46
size_t i
Definition: json11.cpp:597
TOOL_EVENT MakeEvent() const
Function MakeEvent() Returns the event associated with the action (i.e.
Definition: tool_action.h:107
TOOL_EVENT_CATEGORY m_category
Definition: tool_event.h:513
const std::string & GetName() const
Function GetName() Returns name of the action.
Definition: tool_action.h:78