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