1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2019 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtGui module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QSHORTCUT_H |
41 | #define QSHORTCUT_H |
42 | |
43 | #include <QtGui/qtguiglobal.h> |
44 | #include <QtGui/qkeysequence.h> |
45 | #include <QtCore/qobject.h> |
46 | |
47 | QT_REQUIRE_CONFIG(shortcut); |
48 | |
49 | QT_BEGIN_NAMESPACE |
50 | |
51 | class QShortcutPrivate; |
52 | class QWindow; |
53 | |
54 | class Q_GUI_EXPORT QShortcut : public QObject |
55 | { |
56 | Q_OBJECT |
57 | Q_DECLARE_PRIVATE(QShortcut) |
58 | Q_PROPERTY(QKeySequence key READ key WRITE setKey) |
59 | Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled) |
60 | Q_PROPERTY(bool autoRepeat READ autoRepeat WRITE setAutoRepeat) |
61 | Q_PROPERTY(Qt::ShortcutContext context READ context WRITE setContext) |
62 | public: |
63 | explicit QShortcut(QObject *parent); |
64 | explicit QShortcut(const QKeySequence& key, QObject *parent, |
65 | const char *member = nullptr, const char *ambiguousMember = nullptr, |
66 | Qt::ShortcutContext context = Qt::WindowShortcut); |
67 | |
68 | #ifdef Q_CLANG_QDOC |
69 | template<typename Functor> |
70 | QShortcut(const QKeySequence &key, QObject *parent, |
71 | Functor functor, |
72 | Qt::ShortcutContext shortcutContext = Qt::WindowShortcut); |
73 | template<typename Functor> |
74 | QShortcut(const QKeySequence &key, QObject *parent, |
75 | const QObject *context, Functor functor, |
76 | Qt::ShortcutContext shortcutContext = Qt::WindowShortcut); |
77 | template<typename Functor, typename FunctorAmbiguous> |
78 | QShortcut(const QKeySequence &key, QObject *parent, |
79 | const QObject *context1, Functor functor, |
80 | FunctorAmbiguous functorAmbiguous, |
81 | Qt::ShortcutContext shortcutContext = Qt::WindowShortcut); |
82 | template<typename Functor, typename FunctorAmbiguous> |
83 | QShortcut(const QKeySequence &key, QObject *parent, |
84 | const QObject *context1, Functor functor, |
85 | const QObject *context2, FunctorAmbiguous functorAmbiguous, |
86 | Qt::ShortcutContext shortcutContext = Qt::WindowShortcut); |
87 | #else |
88 | template<typename Func1> |
89 | QShortcut(const QKeySequence &key, QObject *parent, |
90 | Func1 slot1, |
91 | Qt::ShortcutContext context = Qt::WindowShortcut) |
92 | : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context) |
93 | { |
94 | connect(this, &QShortcut::activated, std::move(slot1)); |
95 | } |
96 | template<class Obj1, typename Func1> |
97 | QShortcut(const QKeySequence &key, QObject *parent, |
98 | const Obj1 *object1, Func1 slot1, |
99 | Qt::ShortcutContext context = Qt::WindowShortcut, |
100 | typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj1*>::Value>::type* = 0) |
101 | : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context) |
102 | { |
103 | connect(this, &QShortcut::activated, object1, std::move(slot1)); |
104 | } |
105 | template<class Obj1, typename Func1, typename Func2> |
106 | QShortcut(const QKeySequence &key, QObject *parent, |
107 | const Obj1 *object1, Func1 slot1, Func2 slot2, |
108 | Qt::ShortcutContext context = Qt::WindowShortcut, |
109 | typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj1*>::Value>::type* = 0) |
110 | : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context) |
111 | { |
112 | connect(this, &QShortcut::activated, object1, std::move(slot1)); |
113 | connect(this, &QShortcut::activatedAmbiguously, object1, std::move(slot2)); |
114 | } |
115 | template<class Obj1, typename Func1, class Obj2, typename Func2> |
116 | QShortcut(const QKeySequence &key, QObject *parent, |
117 | const Obj1 *object1, Func1 slot1, |
118 | const Obj2 *object2, Func2 slot2, |
119 | Qt::ShortcutContext context = Qt::WindowShortcut, |
120 | typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj1*>::Value>::type* = 0, |
121 | typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj2*>::Value>::type* = 0) |
122 | : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context) |
123 | { |
124 | connect(this, &QShortcut::activated, object1, std::move(slot1)); |
125 | connect(this, &QShortcut::activatedAmbiguously, object2, std::move(slot2)); |
126 | } |
127 | #endif |
128 | |
129 | ~QShortcut(); |
130 | |
131 | void setKey(const QKeySequence& key); |
132 | QKeySequence key() const; |
133 | |
134 | void setEnabled(bool enable); |
135 | bool isEnabled() const; |
136 | |
137 | void setContext(Qt::ShortcutContext context); |
138 | Qt::ShortcutContext context() const; |
139 | |
140 | void setAutoRepeat(bool on); |
141 | bool autoRepeat() const; |
142 | |
143 | int id() const; |
144 | |
145 | void setWhatsThis(const QString &text); |
146 | QString whatsThis() const; |
147 | |
148 | #if QT_DEPRECATED_SINCE(6,0) |
149 | #ifdef Q_CLANG_QDOC |
150 | QWidget *parentWidget() const; |
151 | #else |
152 | template<typename T = QWidget*> |
153 | inline T parentWidget() const |
154 | { return static_cast<T>(QObject::parent()); } |
155 | #endif |
156 | #endif |
157 | |
158 | Q_SIGNALS: |
159 | void activated(); |
160 | void activatedAmbiguously(); |
161 | |
162 | protected: |
163 | bool event(QEvent *e) override; |
164 | }; |
165 | |
166 | QT_END_NAMESPACE |
167 | |
168 | #endif // QSHORTCUT_H |
169 | |