OpenASIP  2.0
KeyboardShortcut.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file KeyboardShortcut.cc
26  *
27  * Implementation of KeyboardShortcut class.
28  *
29  * @author Lasse Laasonen 2003 (lasse.laasonen-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include "Application.hh"
34 #include "KeyboardShortcut.hh"
35 #include "Conversion.hh"
36 #include "ObjectState.hh"
37 
38 using std::string;
39 
40 // initialization of static data members
41 const string KeyboardShortcut::OSNAME_KEYBOARD_SHORTCUT = "keybsc";
42 const string KeyboardShortcut::OSKEY_ACTION = "action";
43 const string KeyboardShortcut::OSKEY_FKEY = "fkey";
44 const string KeyboardShortcut::OSKEY_CTRL = "ctrl";
45 const string KeyboardShortcut::OSKEY_ALT = "alt";
46 const string KeyboardShortcut::OSKEY_KEY = "key";
47 
48 /**
49  * Constructor.
50  *
51  * @param action Name of the action performed by the key combination.
52  * @param fKey Number of the function key used in the shortcut, if the value
53  * is not between 1 and 12 it is ignored and any function key
54  * won't be used.
55  * @param ctrl True if the Ctrl button is used.
56  * @param alt True if the Alt button is used.
57  * @param key Character used in the key combination, NUL character if no
58  * character is used. If delete is used, apply ascii 127.
59  */
61  const std::string& action,
62  int fKey,
63  bool ctrl,
64  bool alt,
65  char key):
66  action_(action), fKey_(fKey), ctrl_(ctrl), alt_(alt), key_(key) {
67 
68  // validity checks
69  if (key < '0' && key != 0) {
70  assert(false);
71  }
72  if (key > '9' && key < 'A') {
73  assert(false);
74  }
75  if (key > 'Z' && key < 'a') {
76  assert(false);
77  }
78  if (key > 'z' && key != 127) {
79  assert(false);
80  }
81 
82  // if key is lower case letter, convert it to lower case
83  if (key_ >= 'a' && key_ <= 'z') {
84  key_ -= 32;
85  }
86 
87  // validity checks
88  if (this->fKey() != 0) {
89  assert(this->key() == 0);
90  }
91  if (this->key() != 0) {
92  assert(this->fKey() == 0);
93  assert(ctrl_ == true || alt_ == true);
94  }
95 }
96 
97 
98 /**
99  * Constructor.
100  *
101  * Loads the state of the object from the given ObjectState object.
102  *
103  * @param state ObjectState from which the state is loaded.
104  */
106  loadState(state);
107 }
108 
109 
110 /**
111  * Copy constructor.
112  */
114  const KeyboardShortcut& old): Serializable() {
115 
116  action_ = old.action_;
117  fKey_ = old.fKey_;
118  ctrl_ = old.ctrl_;
119  alt_ = old.alt_;
120  key_ = old.key_;
121 }
122 
123 
124 /**
125  * Destructor.
126  */
128 }
129 
130 
131 /**
132  * Returns the name of the action performed by this key combination.
133  *
134  * @return Name of the action performed by this key combination.
135  */
136 std::string
138  return action_;
139 }
140 
141 
142 /**
143  * Returns the number of the function key used in the key combination.
144  *
145  * @return Number of the function key used in the key combination, 0 if
146  * no function key is used.
147  */
148 int
150  if (fKey_ < 1 || fKey_ > 12) {
151  return 0;
152  } else {
153  return fKey_;
154  }
155 }
156 
157 
158 /**
159  * Returns true if Ctrl button is used in the key combination.
160  *
161  * @return True if Ctrl button is used in the key combination, otherwise
162  * false.
163  */
164 bool
166  return ctrl_;
167 }
168 
169 
170 /**
171  * Returns true if Alt button is used in the key combination.
172  *
173  * @return True if Alt button is used in the key combination, otherwise
174  * false.
175  */
176 bool
178  return alt_;
179 }
180 
181 
182 /**
183  * Returns the character used in the key combination or NUL character if no
184  * character is used.
185  *
186  * @return The character used in the key combination or NUL character if no
187  * character is used.
188  */
189 char
191  return key_;
192 }
193 
194 
195 /**
196  * Sets control button state of the shortcut.
197  *
198  * @param ctrl Ctrl-button state of the shortcut.
199  */
200 void
202  ctrl_ = ctrl;
203 }
204 
205 
206 /**
207  * Sets alt-button state of the shortcut.
208  *
209  * @param alt Alt-button state of the shortcut.
210  */
211 void
213  alt_ = alt;
214 }
215 
216 /**
217  * Sets the character key of the keyboard shortcut.
218  *
219  * @param key Character key of the shortcut.
220  */
221 void
223  key_ = key;
224 }
225 
226 /**
227  * Sets the function key of the keyboard shortcut. Set as 0 if the shortcut
228  * doesn't use function key.
229  *
230  * @param key Function key of the shortcut.
231  */
232 void
234  if (fKey < 0 || fKey > 12) {
235  fKey = 0;
236  } else {
237  fKey_ = fKey;
238  }
239 }
240 
241 /**
242  * Returns true if the given keyboard shortcut has the same key combination
243  * as this.
244  *
245  * @param sc Keyboard shortcut.
246  * @return True if the given keyboard shortcut has the same key combination
247  * as this.
248  */
249 bool
251  if (ctrl_ == sc.ctrl_ && alt_ == sc.alt_ && key_ == sc.key_ &&
252  fKey() == sc.fKey()) {
253  return true;
254  } else {
255  return false;
256  }
257 }
258 
259 
260 /**
261  * Loads the state of the object from the given ObjectState object.
262  *
263  * @param state ObjectState from which the state is loaded.
264  * @exception ObjectStateLoadingException If the given ObjectState instance
265  * is invalid.
266  */
267 void
269  const string procName = "KeyboardShortcut::loadState";
270 
271  if (state->name() != OSNAME_KEYBOARD_SHORTCUT) {
272  throw ObjectStateLoadingException(__FILE__, __LINE__, procName);
273  }
274 
275  try {
277 
278  if (state->hasAttribute(OSKEY_FKEY)) {
279  fKey_ = state->intAttribute(OSKEY_FKEY);
280  } else {
281  fKey_ = 0;
282  }
283 
284  ctrl_ = state->intAttribute(OSKEY_CTRL);
285  alt_ = state->intAttribute(OSKEY_ALT);
286 
287  if (state->hasAttribute(OSKEY_KEY)) {
288  key_ = state->intAttribute(OSKEY_KEY);
289 
290  // convert to upper case if lower case
291  if (key_ >= 'a' && key_ <= 'z') {
292  key_ -= 32;
293  }
294 
295  } else {
296  key_ = 0;
297  }
298 
299  } catch (...) {
300  throw ObjectStateLoadingException(__FILE__, __LINE__, procName);
301  }
302 }
303 
304 /**
305  * Creates an ObjectState object and saves the state of the object into it.
306  *
307  * @return The created ObjectState object.
308  */
311 
313 
315 
316  // if function key is used
317  if (fKey() != 0) {
318  state->setAttribute(OSKEY_FKEY, fKey());
319  }
320 
321  state->setAttribute(OSKEY_CTRL, ctrl_);
322  state->setAttribute(OSKEY_ALT, alt_);
323 
324  // if key is not nul character
325  if (key_ != 0) {
326  state->setAttribute(OSKEY_KEY, key_);
327  }
328 
329  return state;
330 }
ObjectState::hasAttribute
bool hasAttribute(const std::string &name) const
Definition: ObjectState.cc:205
KeyboardShortcut::ctrl_
bool ctrl_
True if Ctrl button is used in the key combination.
Definition: KeyboardShortcut.hh:97
ObjectState::stringAttribute
std::string stringAttribute(const std::string &name) const
Definition: ObjectState.cc:249
KeyboardShortcut::OSKEY_KEY
static const std::string OSKEY_KEY
ObjectState attribute key for normal letter or number key.
Definition: KeyboardShortcut.hh:63
KeyboardShortcut::OSKEY_ACTION
static const std::string OSKEY_ACTION
ObjectState attribute key for action name.
Definition: KeyboardShortcut.hh:55
KeyboardShortcut::OSNAME_KEYBOARD_SHORTCUT
static const std::string OSNAME_KEYBOARD_SHORTCUT
ObjectState name for keyboard shortcut.
Definition: KeyboardShortcut.hh:53
ObjectStateLoadingException
Definition: Exception.hh:551
KeyboardShortcut::key_
char key_
Definition: KeyboardShortcut.hh:104
Serializable
Definition: Serializable.hh:44
KeyboardShortcut::loadState
void loadState(const ObjectState *state)
Definition: KeyboardShortcut.cc:268
KeyboardShortcut::setKey
void setKey(char key)
Definition: KeyboardShortcut.cc:222
ObjectState
Definition: ObjectState.hh:59
KeyboardShortcut::key
char key() const
Definition: KeyboardShortcut.cc:190
KeyboardShortcut::~KeyboardShortcut
virtual ~KeyboardShortcut()
Definition: KeyboardShortcut.cc:127
assert
#define assert(condition)
Definition: Application.hh:86
KeyboardShortcut::OSKEY_ALT
static const std::string OSKEY_ALT
ObjectState attribute key for alt key.
Definition: KeyboardShortcut.hh:61
KeyboardShortcut.hh
KeyboardShortcut::action_
std::string action_
Name of the action which is performed by the key combination.
Definition: KeyboardShortcut.hh:93
Conversion.hh
Application.hh
KeyboardShortcut::equals
bool equals(const KeyboardShortcut &sc) const
Definition: KeyboardShortcut.cc:250
ObjectState.hh
KeyboardShortcut::action
std::string action() const
Definition: KeyboardShortcut.cc:137
KeyboardShortcut::OSKEY_CTRL
static const std::string OSKEY_CTRL
ObjectState attribute key for ctrl key.
Definition: KeyboardShortcut.hh:59
KeyboardShortcut::KeyboardShortcut
KeyboardShortcut(const std::string &action, int fKey, bool ctrl, bool alt, char key)
Definition: KeyboardShortcut.cc:60
KeyboardShortcut::setFKey
void setFKey(int fKey)
Definition: KeyboardShortcut.cc:233
ObjectState::name
std::string name() const
KeyboardShortcut::fKey_
int fKey_
Number of the function key if it is used, 0 otherwise.
Definition: KeyboardShortcut.hh:95
KeyboardShortcut
Definition: KeyboardShortcut.hh:50
KeyboardShortcut::fKey
int fKey() const
Definition: KeyboardShortcut.cc:149
KeyboardShortcut::OSKEY_FKEY
static const std::string OSKEY_FKEY
ObjectState attribute key for function key number.
Definition: KeyboardShortcut.hh:57
KeyboardShortcut::alt_
bool alt_
True if Alt button is used in the key combination.
Definition: KeyboardShortcut.hh:99
KeyboardShortcut::saveState
ObjectState * saveState() const
Definition: KeyboardShortcut.cc:310
ObjectState::intAttribute
int intAttribute(const std::string &name) const
Definition: ObjectState.cc:276
KeyboardShortcut::setAlt
void setAlt(bool alt)
Definition: KeyboardShortcut.cc:212
KeyboardShortcut::setCtrl
void setCtrl(bool ctrl)
Definition: KeyboardShortcut.cc:201
KeyboardShortcut::ctrl
bool ctrl() const
Definition: KeyboardShortcut.cc:165
KeyboardShortcut::alt
bool alt() const
Definition: KeyboardShortcut.cc:177
ObjectState::setAttribute
void setAttribute(const std::string &name, const std::string &value)
Definition: ObjectState.cc:100