1 /***************************************************************************
2 * Copyright (C) 2005-2018 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)
48 ActiveShortcut = 0x01,
49 DefaultShortcut = 0x02
51 Q_DECLARE_FLAGS(ShortcutTypes, ShortcutType)
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);
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)
61 static_assert(!std::is_same<Slot, const char*>::value, "Old-style connects not supported");
63 setShortcut(shortcut);
64 connect(this, &QAction::triggered, receiver, slot);
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)
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));
78 bool isShortcutConfigurable() const;
79 void setShortcutConfigurable(bool configurable);
82 void triggered(Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers);
89 Q_DECLARE_OPERATORS_FOR_FLAGS(Action::ShortcutTypes)