Action enhances QAction with a subset of KDE's KAction capabilities
[quassel.git] / src / uisupport / action.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but 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, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************
20  * Parts of this implementation are taken from KDE's kaction.cpp           *
21  ***************************************************************************/
22
23 #include <QApplication>
24
25 #include "action.h"
26
27 Action::Action(QObject *parent) : QWidgetAction(parent) {
28   init();
29 }
30
31 Action::Action(const QString &text, QObject *parent) : QWidgetAction(parent) {
32   init();
33   setText(text);
34 }
35
36 Action::Action(const QIcon &icon, const QString &text, QObject *parent) : QWidgetAction(parent) {
37   init();
38   setIcon(icon);
39   setText(text);
40 }
41
42 void Action::init() {
43   connect(this, SIGNAL(triggered(bool)), this, SLOT(slotTriggered()));
44
45   setProperty("isShortcutConfigurable", true);
46 }
47
48 void Action::slotTriggered() {
49   emit triggered(QApplication::mouseButtons(), QApplication::keyboardModifiers());
50 }
51
52 bool Action::isShortcutConfigurable() const {
53   return property("isShortcutConfigurable").toBool();
54 }
55
56 void Action::setShortcutConfigurable(bool b) {
57   setProperty("isShortcutConfigurable", b);
58 }
59
60 QKeySequence Action::shortcut(ShortcutTypes type) const {
61   Q_ASSERT(type);
62   if(type == DefaultShortcut)
63     return property("defaultShortcut").value<QKeySequence>();
64
65   if(shortcuts().count()) return shortcuts().value(0);
66   return QKeySequence();
67 }
68
69 void Action::setShortcut(const QShortcut &shortcut, ShortcutTypes type) {
70   setShortcut(shortcut.key(), type);
71 }
72
73 void Action::setShortcut(const QKeySequence &key, ShortcutTypes type) {
74   Q_ASSERT(type);
75
76   if(type & DefaultShortcut)
77     setProperty("defaultShortcut", key);
78
79   if(type & ActiveShortcut)
80     QAction::setShortcut(key);
81 }