modernize: Migrate action-related things to PMF connects
[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     setProperty("isShortcutConfigurable", true);
31     connect(this, &QAction::triggered, this, &Action::slotTriggered);
32 }
33
34
35 Action::Action(const QString &text, QObject *parent, const QKeySequence &shortcut)
36     : Action(parent)
37 {
38     setText(text);
39     setShortcut(shortcut);
40 }
41
42
43 Action::Action(const QIcon &icon, const QString &text, QObject *parent, const QKeySequence &shortcut)
44     : Action(text, parent, shortcut)
45 {
46     setIcon(icon);
47 }
48
49
50 void Action::slotTriggered()
51 {
52     emit triggered(QApplication::mouseButtons(), QApplication::keyboardModifiers());
53 }
54
55
56 bool Action::isShortcutConfigurable() const
57 {
58     return property("isShortcutConfigurable").toBool();
59 }
60
61
62 void Action::setShortcutConfigurable(bool b)
63 {
64     setProperty("isShortcutConfigurable", b);
65 }
66
67
68 QKeySequence Action::shortcut(ShortcutTypes type) const
69 {
70     Q_ASSERT(type);
71     if (type == DefaultShortcut) {
72         auto sequence = property("defaultShortcuts").value<QList<QKeySequence>>();
73         return sequence.isEmpty() ? QKeySequence() : sequence.first();
74     }
75
76     return shortcuts().isEmpty() ? QKeySequence() : shortcuts().first();
77 }
78
79
80 void Action::setShortcut(const QShortcut &shortcut, ShortcutTypes type)
81 {
82     setShortcut(shortcut.key(), type);
83 }
84
85
86 void Action::setShortcut(const QKeySequence &key, ShortcutTypes type)
87 {
88     Q_ASSERT(type);
89
90     if (type & DefaultShortcut) {
91         setProperty("defaultShortcuts", QVariant::fromValue(QList<QKeySequence>() << key));
92     }
93     if (type & ActiveShortcut)
94         QAction::setShortcut(key);
95 }