1 /***************************************************************************
2 * Copyright (C) 2005-2020 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
25 #include "uisupport-export.h"
27 #include <type_traits>
30 #include <QWidgetAction>
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.
38 class UISUPPORT_EXPORT Action : public QWidgetAction
42 Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut)
43 Q_PROPERTY(bool shortcutConfigurable READ isShortcutConfigurable WRITE setShortcutConfigurable)
49 ActiveShortcut = 0x01,
50 DefaultShortcut = 0x02
52 Q_DECLARE_FLAGS(ShortcutTypes, ShortcutType)
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);
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)
62 static_assert(!std::is_same<Slot, const char*>::value, "Old-style connects not supported");
64 setShortcut(shortcut);
65 connect(this, &QAction::triggered, receiver, slot);
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)
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));
79 bool isShortcutConfigurable() const;
80 void setShortcutConfigurable(bool configurable);
83 void triggered(Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers);
89 Q_DECLARE_OPERATORS_FOR_FLAGS(Action::ShortcutTypes)