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 <QHash>
28 #include <QKeySequence>
29 #include <QString>
30 
31 #include "OverrideAction.h"
32 #include "ShortcutController.h"
33 
34 gui::ShortcutController::ShortcutController() : registeredActions(), shortcuts()
35 {
36 }
37 
38 gui::ShortcutController::ShortcutController(const QHash<QString, QKeySequence>& initialShortcuts) :
39  registeredActions(), shortcuts(initialShortcuts)
40 {
41 }
42 
44 {
45 }
46 
47 void
48 gui::ShortcutController::registerAction(const QString& name, const QPointer<QAction>& action)
49 {
50  if (registeredActions.contains(name))
51  {
52  throw std::invalid_argument("Action already registered");
53  }
54 
55  if (!shortcuts.contains(name))
56  {
57  QKeySequence keySequence("");
58  shortcuts.insert(name, keySequence);
59  }
60 
61  action->setShortcut(shortcuts.value(name));
62  registeredActions.insert(name, action);
63 }
64 
65 void
66 gui::ShortcutController::updateShortcut(const QString& name, const QKeySequence& keysequence)
67 {
68  if (keysequence.toString() != "" && shortcuts.key(keysequence) != NULL)
69  {
70  gui::ShortcutController::updateShortcut(shortcuts.key(keysequence), QKeySequence());
71  }
72 
73  if (shortcuts.contains(name))
74  {
75  shortcuts[name] = keysequence;
76 
77  if (registeredActions.contains(name))
78  {
79  registeredActions.value(name)->setShortcut(keysequence);
80  }
81  }
82  else
83  {
84  shortcuts.insert(name, keysequence);
85  }
86 
87  if (registeredActions.contains(name))
88  {
89  registeredActions.value(name)->setShortcut(keysequence);
90  }
91 }
92 
93 QHash<QString, QKeySequence>
95 {
96  return shortcuts;
97 }
98 
99 QHash<QString, QKeySequence>
101 {
102  QHash<QString, QKeySequence> tempShortcuts;
103  QHash<QString, QKeySequence>::iterator i;
104 
105  for (i = shortcuts.begin(); i != shortcuts.end(); ++i)
106  {
107  if (registeredActions.contains(i.key()))
108  {
109  tempShortcuts.insert(i.key(), i.value());
110  }
111  }
112 
113  return tempShortcuts;
114 }
115 
116 QHash<QString, QPointer<QAction>>
118 {
119  return registeredActions;
120 }
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:66
gui::ShortcutController::getAllRegisteredShortcuts
QHash< QString, QKeySequence > getAllRegisteredShortcuts()
Returns a Hashtable with all registered Shortcuts.
Definition: ShortcutController.cpp:100
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:48
gui::ShortcutController::getAllActions
QHash< QString, QPointer< QAction > > getAllActions()
Returns a Hashtable with all Actions saved.
Definition: ShortcutController.cpp:117
gui::ShortcutController::~ShortcutController
~ShortcutController()
A destructor.
Definition: ShortcutController.cpp:43