| 1 | // Copyright (C) 2020 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 QACTION_H |
| 5 | #define QACTION_H |
| 6 | |
| 7 | #include <QtGui/qtguiglobal.h> |
| 8 | #if QT_CONFIG(shortcut) |
| 9 | # include <QtGui/qkeysequence.h> |
| 10 | #endif |
| 11 | #include <QtGui/qicon.h> |
| 12 | #include <QtCore/qstring.h> |
| 13 | #include <QtCore/qvariant.h> |
| 14 | #include <QtCore/qobject.h> |
| 15 | |
| 16 | QT_REQUIRE_CONFIG(action); |
| 17 | |
| 18 | QT_BEGIN_NAMESPACE |
| 19 | |
| 20 | class QActionEvent; |
| 21 | class QActionGroup; |
| 22 | class QActionPrivate; |
| 23 | class ; |
| 24 | #if QT_DEPRECATED_SINCE(6,0) |
| 25 | class QWidget; |
| 26 | class QGraphicsWidget; |
| 27 | #endif |
| 28 | |
| 29 | class Q_GUI_EXPORT QAction : public QObject |
| 30 | { |
| 31 | Q_OBJECT |
| 32 | Q_DECLARE_PRIVATE(QAction) |
| 33 | |
| 34 | Q_PROPERTY(bool checkable READ isCheckable WRITE setCheckable NOTIFY checkableChanged FINAL) |
| 35 | Q_PROPERTY(bool checked READ isChecked WRITE setChecked NOTIFY toggled) |
| 36 | Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged |
| 37 | RESET resetEnabled FINAL) |
| 38 | Q_PROPERTY(QIcon icon READ icon WRITE setIcon NOTIFY changed) |
| 39 | Q_PROPERTY(QString text READ text WRITE setText NOTIFY changed) |
| 40 | Q_PROPERTY(QString iconText READ iconText WRITE setIconText NOTIFY changed) |
| 41 | Q_PROPERTY(QString toolTip READ toolTip WRITE setToolTip NOTIFY changed) |
| 42 | Q_PROPERTY(QString statusTip READ statusTip WRITE setStatusTip NOTIFY changed) |
| 43 | Q_PROPERTY(QString whatsThis READ whatsThis WRITE setWhatsThis NOTIFY changed) |
| 44 | Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY changed) |
| 45 | #if QT_CONFIG(shortcut) |
| 46 | Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut NOTIFY changed) |
| 47 | Q_PROPERTY(Qt::ShortcutContext shortcutContext READ shortcutContext WRITE setShortcutContext |
| 48 | NOTIFY changed) |
| 49 | Q_PROPERTY(bool autoRepeat READ autoRepeat WRITE setAutoRepeat NOTIFY changed) |
| 50 | #endif // QT_CONFIG(shortcut) |
| 51 | Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged FINAL) |
| 52 | Q_PROPERTY(MenuRole menuRole READ menuRole WRITE setMenuRole NOTIFY changed) |
| 53 | Q_PROPERTY(bool iconVisibleInMenu READ isIconVisibleInMenu WRITE setIconVisibleInMenu |
| 54 | NOTIFY changed) |
| 55 | Q_PROPERTY(bool shortcutVisibleInContextMenu READ isShortcutVisibleInContextMenu |
| 56 | WRITE setShortcutVisibleInContextMenu NOTIFY changed) |
| 57 | Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY changed) |
| 58 | |
| 59 | public: |
| 60 | // note this is copied into qplatformmenu.h, which must stay in sync |
| 61 | enum { NoRole = 0, TextHeuristicRole, ApplicationSpecificRole, AboutQtRole, |
| 62 | AboutRole, PreferencesRole, QuitRole }; |
| 63 | Q_ENUM(MenuRole) |
| 64 | enum Priority { LowPriority = 0, |
| 65 | NormalPriority = 128, |
| 66 | HighPriority = 256}; |
| 67 | Q_ENUM(Priority) |
| 68 | explicit QAction(QObject *parent = nullptr); |
| 69 | explicit QAction(const QString &text, QObject *parent = nullptr); |
| 70 | explicit QAction(const QIcon &icon, const QString &text, QObject *parent = nullptr); |
| 71 | |
| 72 | ~QAction(); |
| 73 | |
| 74 | QList<QObject *> associatedObjects() const; |
| 75 | |
| 76 | #if QT_DEPRECATED_SINCE(6,0) |
| 77 | #ifdef Q_QDOC |
| 78 | QWidget *parentWidget() const; |
| 79 | QList<QWidget*> associatedWidgets() const; |
| 80 | QList<QGraphicsWidget*> associatedGraphicsWidgets() const; |
| 81 | #else |
| 82 | /* |
| 83 | These are templates so that instantiation happens only in calling code, when |
| 84 | QWidget, QMenu, and QGraphicsWidget can be expected to be fully defined. |
| 85 | */ |
| 86 | template<typename T = QWidget*> |
| 87 | QT_DEPRECATED_VERSION_X_6_0("Use parent() with qobject_cast() instead" ) |
| 88 | T parentWidget() const |
| 89 | { |
| 90 | auto result = parent(); |
| 91 | while (result && !qobject_cast<T>(result)) |
| 92 | result = result->parent(); |
| 93 | return static_cast<T>(result); |
| 94 | } |
| 95 | |
| 96 | template<typename T = QWidget*> |
| 97 | QT_DEPRECATED_VERSION_X_6_0("Use associatedObjects() with qobject_cast() instead" ) |
| 98 | QList<T> associatedWidgets() const |
| 99 | { |
| 100 | QList<T> result; |
| 101 | for (auto object : associatedObjects()) |
| 102 | if (auto widget = qobject_cast<T>(object)) |
| 103 | result.append(widget); |
| 104 | return result; |
| 105 | } |
| 106 | template<typename T = QGraphicsWidget*> |
| 107 | QT_DEPRECATED_VERSION_X_6_0("Use associatedObjects() with qobject_cast() instead" ) |
| 108 | QList<T> associatedGraphicsWidgets() const |
| 109 | { |
| 110 | QList<T> result; |
| 111 | for (auto object : associatedObjects()) |
| 112 | if (auto graphicsWidget = qobject_cast<T>(object)) |
| 113 | result.append(graphicsWidget); |
| 114 | return result; |
| 115 | } |
| 116 | #endif |
| 117 | #endif |
| 118 | |
| 119 | void setActionGroup(QActionGroup *group); |
| 120 | QActionGroup *actionGroup() const; |
| 121 | void setIcon(const QIcon &icon); |
| 122 | QIcon icon() const; |
| 123 | |
| 124 | void setText(const QString &text); |
| 125 | QString text() const; |
| 126 | |
| 127 | void setIconText(const QString &text); |
| 128 | QString iconText() const; |
| 129 | |
| 130 | void setToolTip(const QString &tip); |
| 131 | QString toolTip() const; |
| 132 | |
| 133 | void setStatusTip(const QString &statusTip); |
| 134 | QString statusTip() const; |
| 135 | |
| 136 | void setWhatsThis(const QString &what); |
| 137 | QString whatsThis() const; |
| 138 | |
| 139 | void setPriority(Priority priority); |
| 140 | Priority priority() const; |
| 141 | |
| 142 | void setSeparator(bool b); |
| 143 | bool isSeparator() const; |
| 144 | |
| 145 | #if QT_CONFIG(shortcut) |
| 146 | void setShortcut(const QKeySequence &shortcut); |
| 147 | QKeySequence shortcut() const; |
| 148 | |
| 149 | void setShortcuts(const QList<QKeySequence> &shortcuts); |
| 150 | void setShortcuts(QKeySequence::StandardKey); |
| 151 | QList<QKeySequence> shortcuts() const; |
| 152 | |
| 153 | void setShortcutContext(Qt::ShortcutContext context); |
| 154 | Qt::ShortcutContext shortcutContext() const; |
| 155 | |
| 156 | void setAutoRepeat(bool); |
| 157 | bool autoRepeat() const; |
| 158 | #endif // QT_CONFIG(shortcut) |
| 159 | |
| 160 | void setFont(const QFont &font); |
| 161 | QFont font() const; |
| 162 | |
| 163 | void setCheckable(bool); |
| 164 | bool isCheckable() const; |
| 165 | |
| 166 | QVariant data() const; |
| 167 | void setData(const QVariant &var); |
| 168 | |
| 169 | bool isChecked() const; |
| 170 | |
| 171 | bool isEnabled() const; |
| 172 | |
| 173 | bool isVisible() const; |
| 174 | |
| 175 | enum ActionEvent { Trigger, Hover }; |
| 176 | void activate(ActionEvent event); |
| 177 | |
| 178 | void (MenuRole ); |
| 179 | MenuRole () const; |
| 180 | |
| 181 | #ifdef Q_QDOC |
| 182 | QMenu *menu() const; |
| 183 | void setMenu(QMenu *menu); |
| 184 | #else |
| 185 | template<typename T = QMenu*> |
| 186 | T () const |
| 187 | { |
| 188 | return qobject_cast<T>(menuObject()); |
| 189 | } |
| 190 | template<typename T = QMenu*> |
| 191 | void (T m) |
| 192 | { |
| 193 | setMenuObject(m); |
| 194 | } |
| 195 | #endif |
| 196 | |
| 197 | void (bool visible); |
| 198 | bool () const; |
| 199 | |
| 200 | void (bool show); |
| 201 | bool () const; |
| 202 | |
| 203 | bool showStatusText(QObject *object = nullptr); |
| 204 | |
| 205 | protected: |
| 206 | bool event(QEvent *) override; |
| 207 | QAction(QActionPrivate &dd, QObject *parent); |
| 208 | |
| 209 | public Q_SLOTS: |
| 210 | void trigger() { activate(event: Trigger); } |
| 211 | void hover() { activate(event: Hover); } |
| 212 | void setChecked(bool); |
| 213 | void toggle(); |
| 214 | void setEnabled(bool); |
| 215 | void resetEnabled(); |
| 216 | inline void setDisabled(bool b) { setEnabled(!b); } |
| 217 | void setVisible(bool); |
| 218 | |
| 219 | Q_SIGNALS: |
| 220 | void changed(); |
| 221 | void enabledChanged(bool enabled); |
| 222 | void checkableChanged(bool checkable); |
| 223 | void visibleChanged(); |
| 224 | void triggered(bool checked = false); |
| 225 | void hovered(); |
| 226 | void toggled(bool); |
| 227 | |
| 228 | private: |
| 229 | Q_DISABLE_COPY(QAction) |
| 230 | friend class QActionGroup; |
| 231 | friend class QWidget; |
| 232 | friend class QMenu; |
| 233 | friend class ; |
| 234 | friend class QToolButton; |
| 235 | friend class QGraphicsWidget; |
| 236 | |
| 237 | QObject *() const; |
| 238 | void (QObject *object); |
| 239 | }; |
| 240 | |
| 241 | #ifndef QT_NO_DEBUG_STREAM |
| 242 | Q_GUI_EXPORT QDebug operator<<(QDebug, const QAction *); |
| 243 | #endif |
| 244 | |
| 245 | QT_END_NAMESPACE |
| 246 | |
| 247 | #endif // QACTION_H |
| 248 | |