cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / src / uisupport / action.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2022 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     {
49         ActiveShortcut = 0x01,
50         DefaultShortcut = 0x02
51     };
52     Q_DECLARE_FLAGS(ShortcutTypes, ShortcutType)
53
54     explicit Action(QObject* parent);
55     Action(const QString& text, QObject* parent, const QKeySequence& shortcut = 0);
56     Action(const QIcon& icon, const QString& text, QObject* parent, const QKeySequence& shortcut = 0);
57
58     template<typename Receiver, typename Slot>
59     Action(const QString& text, QObject* parent, const Receiver* receiver, Slot slot, const QKeySequence& shortcut = 0)
60         : Action(text, parent, shortcut)
61     {
62         static_assert(!std::is_same<Slot, const char*>::value, "Old-style connects not supported");
63
64         setShortcut(shortcut);
65         connect(this, &QAction::triggered, receiver, slot);
66     }
67
68     template<typename Receiver, typename Slot>
69     Action(const QIcon& icon, const QString& text, QObject* parent, const Receiver* receiver, Slot slot, const QKeySequence& shortcut = 0)
70         : Action(text, parent, receiver, slot, shortcut)
71     {
72         setIcon(icon);
73     }
74
75     QKeySequence shortcut(ShortcutTypes types = ActiveShortcut) const;
76     void setShortcut(const QShortcut& shortcut, ShortcutTypes type = ShortcutTypes(ActiveShortcut | DefaultShortcut));
77     void setShortcut(const QKeySequence& shortcut, ShortcutTypes type = ShortcutTypes(ActiveShortcut | DefaultShortcut));
78
79     bool isShortcutConfigurable() const;
80     void setShortcutConfigurable(bool configurable);
81
82 signals:
83     void triggered(Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers);
84
85 private slots:
86     void slotTriggered();
87 };
88
89 Q_DECLARE_OPERATORS_FOR_FLAGS(Action::ShortcutTypes)