a61339a3e64959617cc4b08367e87a6b88aec02d
[quassel.git] / src / uisupport / action.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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     : QWidgetAction(parent)
29 {
30     init();
31 }
32
33
34 Action::Action(const QString &text, QObject *parent, const QObject *receiver, const char *slot, const QKeySequence &shortcut)
35     : QWidgetAction(parent)
36 {
37     init();
38     setText(text);
39     setShortcut(shortcut);
40     if (receiver && slot)
41         connect(this, SIGNAL(triggered()), receiver, slot);
42 }
43
44
45 Action::Action(const QIcon &icon, const QString &text, QObject *parent, const QObject *receiver, const char *slot, const QKeySequence &shortcut)
46     : QWidgetAction(parent)
47 {
48     init();
49     setIcon(icon);
50     setText(text);
51     setShortcut(shortcut);
52     if (receiver && slot)
53         connect(this, SIGNAL(triggered()), receiver, slot);
54 }
55
56
57 void Action::init()
58 {
59     connect(this, &QAction::triggered, this, &Action::slotTriggered);
60
61     setProperty("isShortcutConfigurable", true);
62 }
63
64
65 void Action::slotTriggered()
66 {
67     emit triggered(QApplication::mouseButtons(), QApplication::keyboardModifiers());
68 }
69
70
71 bool Action::isShortcutConfigurable() const
72 {
73     return property("isShortcutConfigurable").toBool();
74 }
75
76
77 void Action::setShortcutConfigurable(bool b)
78 {
79     setProperty("isShortcutConfigurable", b);
80 }
81
82
83 QKeySequence Action::shortcut(ShortcutTypes type) const
84 {
85     Q_ASSERT(type);
86     if (type == DefaultShortcut) {
87         auto sequence = property("defaultShortcuts").value<QList<QKeySequence>>();
88         return sequence.isEmpty() ? QKeySequence() : sequence.first();
89     }
90
91     return shortcuts().isEmpty() ? QKeySequence() : shortcuts().first();
92 }
93
94
95 void Action::setShortcut(const QShortcut &shortcut, ShortcutTypes type)
96 {
97     setShortcut(shortcut.key(), type);
98 }
99
100
101 void Action::setShortcut(const QKeySequence &key, ShortcutTypes type)
102 {
103     Q_ASSERT(type);
104
105     if (type & DefaultShortcut) {
106         setProperty("defaultShortcuts", QVariant::fromValue(QList<QKeySequence>() << key));
107     }
108     if (type & ActiveShortcut)
109         QAction::setShortcut(key);
110 }