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 
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 
54 bool TOOL_EVENT::IsAction( const TOOL_ACTION* aAction ) const
55 {
56  return Matches( aAction->MakeEvent() );
57 }
58 
59 
60 const std::string TOOL_EVENT::Format() const
61 {
62  std::string ev;
63 
64  const FlagString categories[] =
65  {
66  { TC_MOUSE, "mouse" },
67  { TC_KEYBOARD, "keyboard" },
68  { TC_COMMAND, "command" },
69  { TC_MESSAGE, "message" },
70  { TC_VIEW, "view" },
71  { 0, "" }
72  };
73 
74  const FlagString actions[] =
75  {
76  { TA_MOUSE_CLICK, "click" },
77  { TA_MOUSE_DBLCLICK, "double click" },
78  { TA_MOUSE_UP, "button-up" },
79  { TA_MOUSE_DOWN, "button-down" },
80  { TA_MOUSE_DRAG, "drag" },
81  { TA_MOUSE_MOTION, "motion" },
82  { TA_MOUSE_WHEEL, "wheel" },
83  { TA_KEY_PRESSED, "key-pressed" },
84  { TA_VIEW_REFRESH, "view-refresh" },
85  { TA_VIEW_ZOOM, "view-zoom" },
86  { TA_VIEW_PAN, "view-pan" },
87  { TA_VIEW_DIRTY, "view-dirty" },
88  { TA_CHANGE_LAYER, "change-layer" },
89  { TA_CANCEL_TOOL, "cancel-tool" },
90  { TA_CONTEXT_MENU_UPDATE, "context-menu-update" },
91  { TA_CONTEXT_MENU_CHOICE, "context-menu-choice" },
92  { TA_UNDO_REDO_PRE, "undo-redo-pre" },
93  { TA_UNDO_REDO_POST, "undo-redo-post" },
94  { TA_ACTION, "action" },
95  { TA_ACTIVATE, "activate" },
96  { 0, "" }
97  };
98 
99  const FlagString buttons[] =
100  {
101  { BUT_NONE, "none" },
102  { BUT_LEFT, "left" },
103  { BUT_RIGHT, "right" },
104  { BUT_MIDDLE, "middle" },
105  { 0, "" }
106  };
107 
108  const FlagString modifiers[] =
109  {
110  { MD_SHIFT, "shift" },
111  { MD_CTRL, "ctrl" },
112  { MD_ALT, "alt" },
113  { 0, "" }
114  };
115 
116  ev = "category: ";
117  ev += flag2string( m_category, categories );
118  ev += " action: ";
119  ev += flag2string( m_actions, actions );
120 
121  if( m_actions & TA_MOUSE )
122  {
123  ev += " btns: ";
124  ev += flag2string( m_mouseButtons, buttons );
125  }
126 
127  if( m_actions & TA_KEYBOARD )
128  {
129  char tmp[128];
130  sprintf( tmp, "key: %d", m_keyCode );
131  ev += tmp;
132  }
133 
134  if( m_actions & ( TA_MOUSE | TA_KEYBOARD ) )
135  {
136  ev += " mods: ";
137  ev += flag2string( m_modifiers, modifiers );
138  }
139 
140  if( m_commandId )
141  {
142  char tmp[128];
143  sprintf( tmp, "cmd-id: %d", *m_commandId );
144  ev += tmp;
145  }
146 
147  if( m_commandStr )
148  ev += "cmd-str: " + ( *m_commandStr );
149 
150  return ev;
151 }
152 
153 
154 const std::string TOOL_EVENT_LIST::Format() const
155 {
156  std::string s;
157 
158  for( TOOL_EVENT e : m_events )
159  s += e.Format() + " ";
160 
161  return s;
162 }
const std::string Format() const
Function Format() Returns information about event in form of a human-readable string.
Definition: tool_event.cpp:60
int m_modifiers
State of key modifierts (Ctrl/Alt/etc.)
Definition: tool_event.h:451
TOOL_ACTIONS m_actions
Definition: tool_event.h:431
int m_mouseButtons
State of mouse buttons
Definition: tool_event.h:445
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:54
int m_keyCode
Stores code of pressed/released key
Definition: tool_event.h:448
boost::optional< int > m_commandId
Definition: tool_event.h:456
std::deque< TOOL_EVENT > m_events
Definition: tool_event.h:575
boost::optional< std::string > m_commandStr
Definition: tool_event.h:457
Class TOOL_EVENT.
Definition: tool_event.h:162
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:333
static const std::string flag2string(int aFlag, const FlagString *aExps)
Definition: tool_event.cpp:40
TOOL_EVENT MakeEvent() const
Function HasHotKey() Checks if the action has a hot key assigned.
Definition: tool_action.h:104
std::string str
Definition: tool_event.cpp:36
Class TOOL_ACTION.
Definition: tool_action.h:46
TOOL_EVENT_CATEGORY m_category
Definition: tool_event.h:430
const std::string Format() const
Function Format() Returns information about event in form of a human-readable string.
Definition: tool_event.cpp:154