modernize: Migrate action-related things to PMF connects
[quassel.git] / src / uisupport / action.h
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 API have been shamelessly stolen from KDE's kaction.h     *
21  ***************************************************************************/
22
23 #pragma once
24
25 #include "uisupport-export.h"
26
27 #include <type_traits>
28
29 #include <QShortcut>
30 #include <QWidgetAction>
31
32 #include "util.h"
33
34 /// A specialized QWidgetAction, enhanced by some KDE features
35 /** This declares/implements a subset of KAction's API (notably we've left out global shortcuts
36  *  for now), which should make it easy to plug in KDE support later on.
37  */
38 class UISUPPORT_EXPORT Action : public QWidgetAction
39 {
40     Q_OBJECT
41
42     Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut)
43     Q_PROPERTY(bool shortcutConfigurable READ isShortcutConfigurable WRITE setShortcutConfigurable)
44     Q_FLAGS(ShortcutType)
45
46 public :
47         enum ShortcutType {
48         ActiveShortcut = 0x01,
49         DefaultShortcut = 0x02
50     };
51     Q_DECLARE_FLAGS(ShortcutTypes, ShortcutType)
52
53     explicit Action(QObject *parent);
54     Action(const QString &text, QObject *parent, const QKeySequence &shortcut = 0);
55     Action(const QIcon &icon, const QString &text, QObject *parent, const QKeySequence &shortcut = 0);
56
57     template<typename Receiver, typename Slot>
58     Action(const QString &text, QObject *parent, const Receiver *receiver, Slot slot, const QKeySequence &shortcut = 0)
59         : Action(text, parent, shortcut)
60     {
61         static_assert(!std::is_same<Slot, const char*>::value, "Old-style connects not supported");
62
63         setShortcut(shortcut);
64         connect(this, &QAction::triggered, receiver, slot);
65     }
66
67     template<typename Receiver, typename Slot>
68     Action(const QIcon &icon, const QString &text, QObject *parent, const Receiver *receiver, Slot slot, const QKeySequence &shortcut = 0)
69         : Action(text, parent, receiver, slot, shortcut)
70     {
71         setIcon(icon);
72     }
73
74     QKeySequence shortcut(ShortcutTypes types = ActiveShortcut) const;
75     void setShortcut(const QShortcut &shortcut, ShortcutTypes type = ShortcutTypes(ActiveShortcut | DefaultShortcut));
76     void setShortcut(const QKeySequence &shortcut, ShortcutTypes type = ShortcutTypes(ActiveShortcut | DefaultShortcut));
77
78     bool isShortcutConfigurable() const;
79     void setShortcutConfigurable(bool configurable);
80
81 signals:
82     void triggered(Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers);
83
84 private slots:
85     void slotTriggered();
86 };
87
88
89 Q_DECLARE_OPERATORS_FOR_FLAGS(Action::ShortcutTypes)