1 | // Copyright (C) 2019 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QSHORTCUT_H |
5 | #define QSHORTCUT_H |
6 | |
7 | #include <QtGui/qtguiglobal.h> |
8 | #include <QtGui/qkeysequence.h> |
9 | #include <QtCore/qobject.h> |
10 | |
11 | QT_REQUIRE_CONFIG(shortcut); |
12 | |
13 | QT_BEGIN_NAMESPACE |
14 | |
15 | class QShortcutPrivate; |
16 | class QWindow; |
17 | |
18 | class Q_GUI_EXPORT QShortcut : public QObject |
19 | { |
20 | Q_OBJECT |
21 | Q_DECLARE_PRIVATE(QShortcut) |
22 | Q_PROPERTY(QKeySequence key READ key WRITE setKey) |
23 | Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled) |
24 | Q_PROPERTY(bool autoRepeat READ autoRepeat WRITE setAutoRepeat) |
25 | Q_PROPERTY(Qt::ShortcutContext context READ context WRITE setContext) |
26 | public: |
27 | explicit QShortcut(QObject *parent); |
28 | explicit QShortcut(const QKeySequence& key, QObject *parent, |
29 | const char *member = nullptr, const char *ambiguousMember = nullptr, |
30 | Qt::ShortcutContext context = Qt::WindowShortcut); |
31 | explicit QShortcut(QKeySequence::StandardKey key, QObject *parent, |
32 | const char *member = nullptr, const char *ambiguousMember = nullptr, |
33 | Qt::ShortcutContext context = Qt::WindowShortcut); |
34 | |
35 | #ifdef Q_QDOC |
36 | template<typename Functor> |
37 | QShortcut(const QKeySequence &key, QObject *parent, |
38 | Functor functor, |
39 | Qt::ShortcutContext shortcutContext = Qt::WindowShortcut); |
40 | template<typename Functor> |
41 | QShortcut(const QKeySequence &key, QObject *parent, |
42 | const QObject *context, Functor functor, |
43 | Qt::ShortcutContext shortcutContext = Qt::WindowShortcut); |
44 | template<typename Functor, typename FunctorAmbiguous> |
45 | QShortcut(const QKeySequence &key, QObject *parent, |
46 | const QObject *context1, Functor functor, |
47 | FunctorAmbiguous functorAmbiguous, |
48 | Qt::ShortcutContext shortcutContext = Qt::WindowShortcut); |
49 | template<typename Functor, typename FunctorAmbiguous> |
50 | QShortcut(const QKeySequence &key, QObject *parent, |
51 | const QObject *context1, Functor functor, |
52 | const QObject *context2, FunctorAmbiguous functorAmbiguous, |
53 | Qt::ShortcutContext shortcutContext = Qt::WindowShortcut); |
54 | |
55 | template<typename Functor> |
56 | QShortcut(QKeySequence::StandardKey key, QObject *parent, |
57 | Functor functor, |
58 | Qt::ShortcutContext shortcutContext = Qt::WindowShortcut); |
59 | template<typename Functor> |
60 | QShortcut(QKeySequence::StandardKey key, QObject *parent, |
61 | const QObject *context, Functor functor, |
62 | Qt::ShortcutContext shortcutContext = Qt::WindowShortcut); |
63 | template<typename Functor, typename FunctorAmbiguous> |
64 | QShortcut(QKeySequence::StandardKey key, QObject *parent, |
65 | const QObject *context1, Functor functor, |
66 | FunctorAmbiguous functorAmbiguous, |
67 | Qt::ShortcutContext shortcutContext = Qt::WindowShortcut); |
68 | template<typename Functor, typename FunctorAmbiguous> |
69 | QShortcut(QKeySequence::StandardKey key, QObject *parent, |
70 | const QObject *context1, Functor functor, |
71 | const QObject *context2, FunctorAmbiguous functorAmbiguous, |
72 | Qt::ShortcutContext shortcutContext = Qt::WindowShortcut); |
73 | #else |
74 | #ifndef QT_NO_CONTEXTLESS_CONNECT |
75 | template<typename Func1> |
76 | QShortcut(const QKeySequence &key, QObject *parent, |
77 | Func1 slot1, |
78 | Qt::ShortcutContext context = Qt::WindowShortcut) |
79 | : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context) |
80 | { |
81 | connect(this, &QShortcut::activated, std::move(slot1)); |
82 | } |
83 | #endif |
84 | template<class Obj1, typename Func1> |
85 | QShortcut(const QKeySequence &key, QObject *parent, |
86 | const Obj1 *object1, Func1 slot1, |
87 | Qt::ShortcutContext context = Qt::WindowShortcut, |
88 | typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj1*>::Value>::type* = 0) |
89 | : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context) |
90 | { |
91 | connect(this, &QShortcut::activated, object1, std::move(slot1)); |
92 | } |
93 | template<class Obj1, typename Func1, typename Func2> |
94 | QShortcut(const QKeySequence &key, QObject *parent, |
95 | const Obj1 *object1, Func1 slot1, Func2 slot2, |
96 | Qt::ShortcutContext context = Qt::WindowShortcut, |
97 | typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj1*>::Value>::type* = 0) |
98 | : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context) |
99 | { |
100 | connect(this, &QShortcut::activated, object1, std::move(slot1)); |
101 | connect(this, &QShortcut::activatedAmbiguously, object1, std::move(slot2)); |
102 | } |
103 | template<class Obj1, typename Func1, class Obj2, typename Func2> |
104 | QShortcut(const QKeySequence &key, QObject *parent, |
105 | const Obj1 *object1, Func1 slot1, |
106 | const Obj2 *object2, Func2 slot2, |
107 | Qt::ShortcutContext context = Qt::WindowShortcut, |
108 | typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj1*>::Value>::type* = 0, |
109 | typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj2*>::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, object2, std::move(slot2)); |
114 | } |
115 | |
116 | template<typename Func1> |
117 | QShortcut(QKeySequence::StandardKey key, QObject *parent, |
118 | Func1 slot1, |
119 | Qt::ShortcutContext context = Qt::WindowShortcut) |
120 | : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context) |
121 | { |
122 | connect(this, &QShortcut::activated, std::move(slot1)); |
123 | } |
124 | template<class Obj1, typename Func1> |
125 | QShortcut(QKeySequence::StandardKey key, QObject *parent, |
126 | const Obj1 *object1, Func1 slot1, |
127 | Qt::ShortcutContext context = Qt::WindowShortcut, |
128 | typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj1*>::Value>::type* = 0) |
129 | : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context) |
130 | { |
131 | connect(this, &QShortcut::activated, object1, std::move(slot1)); |
132 | } |
133 | template<class Obj1, typename Func1, typename Func2> |
134 | QShortcut(QKeySequence::StandardKey key, QObject *parent, |
135 | const Obj1 *object1, Func1 slot1, Func2 slot2, |
136 | Qt::ShortcutContext context = Qt::WindowShortcut, |
137 | typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj1*>::Value>::type* = 0) |
138 | : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context) |
139 | { |
140 | connect(this, &QShortcut::activated, object1, std::move(slot1)); |
141 | connect(this, &QShortcut::activatedAmbiguously, object1, std::move(slot2)); |
142 | } |
143 | template<class Obj1, typename Func1, class Obj2, typename Func2> |
144 | QShortcut(QKeySequence::StandardKey key, QObject *parent, |
145 | const Obj1 *object1, Func1 slot1, |
146 | const Obj2 *object2, Func2 slot2, |
147 | Qt::ShortcutContext context = Qt::WindowShortcut, |
148 | typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj1*>::Value>::type* = 0, |
149 | typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj2*>::Value>::type* = 0) |
150 | : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context) |
151 | { |
152 | connect(this, &QShortcut::activated, object1, std::move(slot1)); |
153 | connect(this, &QShortcut::activatedAmbiguously, object2, std::move(slot2)); |
154 | } |
155 | #endif |
156 | |
157 | ~QShortcut(); |
158 | |
159 | void setKey(const QKeySequence& key); |
160 | QKeySequence key() const; |
161 | void setKeys(QKeySequence::StandardKey key); |
162 | void setKeys(const QList<QKeySequence> &keys); |
163 | QList<QKeySequence> keys() const; |
164 | |
165 | void setEnabled(bool enable); |
166 | bool isEnabled() const; |
167 | |
168 | void setContext(Qt::ShortcutContext context); |
169 | Qt::ShortcutContext context() const; |
170 | |
171 | void setAutoRepeat(bool on); |
172 | bool autoRepeat() const; |
173 | |
174 | #if QT_DEPRECATED_SINCE(6,0) |
175 | QT_DEPRECATED_VERSION_6_0 int id() const; |
176 | #endif |
177 | |
178 | void setWhatsThis(const QString &text); |
179 | QString whatsThis() const; |
180 | |
181 | #if QT_DEPRECATED_SINCE(6,0) |
182 | #ifdef Q_QDOC |
183 | QWidget *parentWidget() const; |
184 | #else |
185 | template<typename T = QWidget*> |
186 | QT_DEPRECATED_VERSION_X_6_0("Use parent() and qobject_cast instead" ) |
187 | inline T parentWidget() const |
188 | { return static_cast<T>(QObject::parent()); } |
189 | #endif |
190 | #endif |
191 | |
192 | Q_SIGNALS: |
193 | void activated(); |
194 | void activatedAmbiguously(); |
195 | |
196 | protected: |
197 | bool event(QEvent *e) override; |
198 | }; |
199 | |
200 | QT_END_NAMESPACE |
201 | |
202 | #endif // QSHORTCUT_H |
203 | |