Merge pull request #105 from merlin1991/qdatastream
[quassel.git] / src / uisupport / action.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2015 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************
20  * Parts of this implementation are taken from KDE's kaction.cpp           *
21  ***************************************************************************/
22
23 #include "action.h"
24
25 #include <QApplication>
26
27 Action::Action(QObject *parent)
28 #ifdef HAVE_KDE4
29     : KAction(parent)
30 #else
31     : QWidgetAction(parent)
32 #endif
33 {
34     init();
35 }
36
37
38 Action::Action(const QString &text, QObject *parent, const QObject *receiver, const char *slot, const QKeySequence &shortcut)
39 #ifdef HAVE_KDE4
40     : KAction(parent)
41 #else
42     : QWidgetAction(parent)
43 #endif
44 {
45     init();
46     setText(text);
47     setShortcut(shortcut);
48     if (receiver && slot)
49         connect(this, SIGNAL(triggered()), receiver, slot);
50 }
51
52
53 Action::Action(const QIcon &icon, const QString &text, QObject *parent, const QObject *receiver, const char *slot, const QKeySequence &shortcut)
54 #ifdef HAVE_KDE4
55     : KAction(parent)
56 #else
57     : QWidgetAction(parent)
58 #endif
59 {
60     init();
61     setIcon(icon);
62     setText(text);
63     setShortcut(shortcut);
64     if (receiver && slot)
65         connect(this, SIGNAL(triggered()), receiver, slot);
66 }
67
68
69 #ifdef HAVE_KDE4
70 void Action::init() {}
71 #else
72 void Action::init()
73 {
74     connect(this, SIGNAL(triggered(bool)), this, SLOT(slotTriggered()));
75
76     setProperty("isShortcutConfigurable", true);
77 }
78
79
80 void Action::slotTriggered()
81 {
82     emit triggered(QApplication::mouseButtons(), QApplication::keyboardModifiers());
83 }
84
85
86 bool Action::isShortcutConfigurable() const
87 {
88     return property("isShortcutConfigurable").toBool();
89 }
90
91
92 void Action::setShortcutConfigurable(bool b)
93 {
94     setProperty("isShortcutConfigurable", b);
95 }
96
97
98 QKeySequence Action::shortcut(ShortcutTypes type) const
99 {
100     Q_ASSERT(type);
101     if (type == DefaultShortcut)
102         return property("defaultShortcut").value<QKeySequence>();
103
104     if (shortcuts().count()) return shortcuts().value(0);
105     return QKeySequence();
106 }
107
108
109 void Action::setShortcut(const QShortcut &shortcut, ShortcutTypes type)
110 {
111     setShortcut(shortcut.key(), type);
112 }
113
114
115 void Action::setShortcut(const QKeySequence &key, ShortcutTypes type)
116 {
117     Q_ASSERT(type);
118
119     if (type & DefaultShortcut)
120         setProperty("defaultShortcut", key);
121
122     if (type & ActiveShortcut)
123         QAction::setShortcut(key);
124 }
125
126
127 #endif /* HAVE_KDE4 */