ShortcutController.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package MemoryX::gui-plugins::SceneEditor
19  * @date 2015
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 #include <stdexcept>
25 
26 // QT
27 #include <QString>
28 #include <QHash>
29 #include <QKeySequence>
30 #include "OverrideAction.h"
31 
32 #include "ShortcutController.h"
33 
35  registeredActions(),
36  shortcuts()
37 {
38 }
39 
40 gui::ShortcutController::ShortcutController(const QHash<QString, QKeySequence>& initialShortcuts) :
41  registeredActions(),
42  shortcuts(initialShortcuts)
43 {
44 }
45 
47 {
48 }
49 
50 void gui::ShortcutController::registerAction(const QString& name, const QPointer<QAction>& action)
51 {
52  if (registeredActions.contains(name))
53  {
54  throw std::invalid_argument("Action already registered");
55  }
56 
57  if (!shortcuts.contains(name))
58  {
59  QKeySequence keySequence("");
60  shortcuts.insert(name, keySequence);
61  }
62 
63  action->setShortcut(shortcuts.value(name));
64  registeredActions.insert(name, action);
65 }
66 
67 void gui::ShortcutController::updateShortcut(const QString& name, const QKeySequence& keysequence)
68 {
69  if (keysequence.toString() != "" && shortcuts.key(keysequence) != NULL)
70  {
71  gui::ShortcutController::updateShortcut(shortcuts.key(keysequence), QKeySequence());
72  }
73 
74  if (shortcuts.contains(name))
75  {
76  shortcuts[name] = keysequence;
77 
78  if (registeredActions.contains(name))
79  {
80  registeredActions.value(name)->setShortcut(keysequence);
81  }
82  }
83  else
84  {
85  shortcuts.insert(name, keysequence);
86  }
87 
88  if (registeredActions.contains(name))
89  {
90  registeredActions.value(name)->setShortcut(keysequence);
91  }
92 }
93 
94 QHash<QString, QKeySequence> gui::ShortcutController::getAllShortcuts()
95 {
96  return shortcuts;
97 }
98 
100 {
101  QHash<QString, QKeySequence> tempShortcuts;
102  QHash<QString, QKeySequence>::iterator i;
103 
104  for (i = shortcuts.begin(); i != shortcuts.end(); ++i)
105  {
106  if (registeredActions.contains(i.key()))
107  {
108  tempShortcuts.insert(i.key(), i.value());
109  }
110  }
111 
112  return tempShortcuts;
113 }
114 
115 QHash<QString, QPointer<QAction> > gui::ShortcutController::getAllActions()
116 {
117  return registeredActions;
118 }
gui::ShortcutController::getAllShortcuts
QHash< QString, QKeySequence > getAllShortcuts()
Returns a Hashtable with all existing Shortcuts.
Definition: ShortcutController.cpp:94
OverrideAction.h
ShortcutController.h
gui::ShortcutController::ShortcutController
ShortcutController()
A constructor.
Definition: ShortcutController.cpp:34
gui::ShortcutController::updateShortcut
void updateShortcut(const QString &name, const QKeySequence &keysequence)
Sets the given QKeySequence as Shortcut for the QAction specified by name.
Definition: ShortcutController.cpp:67
gui::ShortcutController::getAllRegisteredShortcuts
QHash< QString, QKeySequence > getAllRegisteredShortcuts()
Returns a Hashtable with all registered Shortcuts.
Definition: ShortcutController.cpp:99
gui::ShortcutController::registerAction
void registerAction(const QString &name, const QPointer< QAction > &action)
Registers a QAction, so this action can be given a shortcut.
Definition: ShortcutController.cpp:50
gui::ShortcutController::getAllActions
QHash< QString, QPointer< QAction > > getAllActions()
Returns a Hashtable with all Actions saved.
Definition: ShortcutController.cpp:115
gui::ShortcutController::~ShortcutController
~ShortcutController()
A destructor.
Definition: ShortcutController.cpp:46