| 1 | /* |
| 2 | This file is part of the KDE libraries |
| 3 | SPDX-FileCopyrightText: 1999 Reginald Stadlbauer <reggie@kde.org> |
| 4 | SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org> |
| 5 | SPDX-FileCopyrightText: 2000 Nicolas Hadacek <haadcek@kde.org> |
| 6 | SPDX-FileCopyrightText: 2000 Kurt Granroth <granroth@kde.org> |
| 7 | SPDX-FileCopyrightText: 2000 Michael Koch <koch@kde.org> |
| 8 | SPDX-FileCopyrightText: 2001 Holger Freyther <freyther@kde.org> |
| 9 | SPDX-FileCopyrightText: 2002 Ellis Whitehead <ellis@kde.org> |
| 10 | SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <rodda@kde.org> |
| 11 | |
| 12 | SPDX-License-Identifier: LGPL-2.0-only |
| 13 | */ |
| 14 | |
| 15 | #ifndef KACTIONCOLLECTION_H |
| 16 | #define KACTIONCOLLECTION_H |
| 17 | |
| 18 | #include <KStandardAction> |
| 19 | #include <KStandardActions> |
| 20 | #include <kxmlgui_export.h> |
| 21 | |
| 22 | #include <QAction> |
| 23 | #include <QObject> |
| 24 | #include <memory> |
| 25 | |
| 26 | class KXMLGUIClient; |
| 27 | class KConfigGroup; |
| 28 | class QActionGroup; |
| 29 | class QString; |
| 30 | |
| 31 | /*! |
| 32 | * \class KActionCollection |
| 33 | * \inmodule KXmlGui |
| 34 | * |
| 35 | * \brief A container for a set of QAction objects. |
| 36 | * |
| 37 | * KActionCollection manages a set of QAction objects. It |
| 38 | * allows them to be grouped for organized presentation of configuration to the user, |
| 39 | * saving + loading of configuration, and optionally for automatic plugging into |
| 40 | * specified widget(s). |
| 41 | * |
| 42 | * Additionally, KActionCollection provides several convenience functions for locating |
| 43 | * named actions, and actions grouped by QActionGroup. |
| 44 | * |
| 45 | * \note If you create your own action collection and need to assign shortcuts |
| 46 | * to the actions within, you have to call associateWidget() or |
| 47 | * addAssociatedWidget() to have them working. |
| 48 | */ |
| 49 | class KXMLGUI_EXPORT KActionCollection : public QObject |
| 50 | { |
| 51 | friend class KXMLGUIClient; |
| 52 | |
| 53 | Q_OBJECT |
| 54 | |
| 55 | /*! |
| 56 | * \property KActionCollection::configGroup |
| 57 | */ |
| 58 | Q_PROPERTY(QString configGroup READ configGroup WRITE setConfigGroup) |
| 59 | |
| 60 | /*! |
| 61 | * \property KActionCollection::configIsGlobal |
| 62 | */ |
| 63 | Q_PROPERTY(bool configIsGlobal READ configIsGlobal WRITE setConfigGlobal) |
| 64 | |
| 65 | public: |
| 66 | /*! |
| 67 | * \brief Constructor. |
| 68 | * |
| 69 | * Allows specification of a component name other than the default |
| 70 | * application name, where needed (remember to call setComponentDisplayName() too). |
| 71 | */ |
| 72 | explicit KActionCollection(QObject *parent, const QString &cName = QString()); |
| 73 | |
| 74 | /*! |
| 75 | * \brief Destructor. |
| 76 | */ |
| 77 | ~KActionCollection() override; |
| 78 | |
| 79 | /*! |
| 80 | * \brief Access the list of all action collections in existence for this app. |
| 81 | */ |
| 82 | static const QList<KActionCollection *> &allCollections(); |
| 83 | |
| 84 | /*! |
| 85 | * \brief Clears the entire action collection, deleting all actions. |
| 86 | */ |
| 87 | void clear(); |
| 88 | |
| 89 | /*! |
| 90 | * \brief Associate all actions in this collection to the given \a widget. |
| 91 | * |
| 92 | * Unlike addAssociatedWidget(), this method only adds all current actions |
| 93 | * in the collection to the given widget. Any action added after this call |
| 94 | * will not be added to the given widget automatically. |
| 95 | * |
| 96 | * So this is just a shortcut for a foreach loop and a widget->addAction call. |
| 97 | */ |
| 98 | void associateWidget(QWidget *widget) const; |
| 99 | |
| 100 | /*! |
| 101 | * \brief Associate all actions in this collection to the given \a widget, |
| 102 | * including any actions added after this association is made. |
| 103 | * |
| 104 | * This does not change the action's shortcut context, so if you need to have the actions only |
| 105 | * trigger when the widget has focus, you'll need to set the shortcut context on each action |
| 106 | * to Qt::WidgetShortcut (or better still, Qt::WidgetWithChildrenShortcut with Qt 4.4+) |
| 107 | */ |
| 108 | void addAssociatedWidget(QWidget *widget); |
| 109 | |
| 110 | /*! |
| 111 | * \brief Remove an association between all actions in this collection |
| 112 | * and the given \a widget, i.e. remove those actions from the widget, |
| 113 | * and stop associating newly added actions as well. |
| 114 | */ |
| 115 | void removeAssociatedWidget(QWidget *widget); |
| 116 | |
| 117 | /*! |
| 118 | * \brief Return a list of all associated widgets. |
| 119 | */ |
| 120 | QList<QWidget *> associatedWidgets() const; |
| 121 | |
| 122 | /*! |
| 123 | * \brief Clear all associated widgets and remove the actions from those widgets. |
| 124 | */ |
| 125 | void clearAssociatedWidgets(); |
| 126 | |
| 127 | /*! |
| 128 | * \brief Returns the KConfig group with which settings will be loaded and saved. |
| 129 | */ |
| 130 | QString configGroup() const; |
| 131 | |
| 132 | /*! |
| 133 | * \brief Returns whether this action collection's configuration |
| 134 | * should be global to KDE ( \c true ), |
| 135 | * or specific to the application ( \c false ). |
| 136 | */ |
| 137 | bool configIsGlobal() const; |
| 138 | |
| 139 | /*! |
| 140 | * \brief Sets \a group as the KConfig group with which |
| 141 | * settings will be loaded and saved. |
| 142 | */ |
| 143 | void setConfigGroup(const QString &group); |
| 144 | |
| 145 | /*! |
| 146 | * \brief Set whether this action collection's configuration |
| 147 | * should be \a global to KDE ( \c true ), |
| 148 | * or specific to the application ( \c false ). |
| 149 | */ |
| 150 | void setConfigGlobal(bool global); |
| 151 | |
| 152 | /*! |
| 153 | * \brief Read all key associations from \a config. |
| 154 | * |
| 155 | * If \a config is zero, read all key associations from the |
| 156 | * application's configuration file KSharedConfig::openConfig(), |
| 157 | * in the group set by setConfigGroup(). |
| 158 | */ |
| 159 | void readSettings(KConfigGroup *config = nullptr); |
| 160 | |
| 161 | /*! |
| 162 | * \brief Import all configurable global key associations from \a config. |
| 163 | * |
| 164 | * \since 4.1 |
| 165 | */ |
| 166 | void importGlobalShortcuts(KConfigGroup *config); |
| 167 | |
| 168 | /*! |
| 169 | * \brief Export the current configurable global key associations to \a config. |
| 170 | * |
| 171 | * If \a writeDefaults is set to true, default settings will also be written. |
| 172 | * |
| 173 | * \since 4.1 |
| 174 | */ |
| 175 | void exportGlobalShortcuts(KConfigGroup *config, bool writeDefaults = false) const; |
| 176 | |
| 177 | /*! |
| 178 | * \brief Write the current configurable key associations to \a config. What the |
| 179 | * function does if \a config is zero depends. If this action collection |
| 180 | * belongs to a KXMLGUIClient the setting are saved to the kxmlgui |
| 181 | * definition file. If not the settings are written to the applications |
| 182 | * config file. |
| 183 | * |
| 184 | * \note \a oneAction and \a writeDefaults have no meaning for the kxmlgui |
| 185 | * configuration file. |
| 186 | * |
| 187 | * \a config Config object to save to, or null. |
| 188 | * |
| 189 | * \a writeDefaults set to true to write settings which are already at defaults. |
| 190 | * |
| 191 | * \a oneAction pass an action here if you just want to save the values for one action, eg. |
| 192 | * if you know that action is the only one which has changed. |
| 193 | */ |
| 194 | void writeSettings(KConfigGroup *config = nullptr, bool writeDefaults = false, QAction *oneAction = nullptr) const; |
| 195 | |
| 196 | /*! |
| 197 | * \brief The number of actions in the collection. |
| 198 | * |
| 199 | * This is equivalent to actions().count(). |
| 200 | */ |
| 201 | int count() const; |
| 202 | |
| 203 | /*! |
| 204 | * \brief Returns whether the action collection is empty or not. |
| 205 | */ |
| 206 | bool isEmpty() const; |
| 207 | |
| 208 | /*! |
| 209 | * \brief Returns the QAction* at position \a index in the action collection. |
| 210 | * |
| 211 | * This is equivalent to actions().value(index). |
| 212 | */ |
| 213 | QAction *action(int index) const; |
| 214 | |
| 215 | /*! |
| 216 | * \brief Get an action with the given \a name from the action collection. |
| 217 | * |
| 218 | * This won't return the action for the menus defined using a "<Menu>" tag |
| 219 | * in XMLGUI files (e.g. "<Menu name="menuId">" in "applicationNameui.rc"). |
| 220 | * |
| 221 | * To access menu actions defined like this, use for example... |
| 222 | * |
| 223 | * \code |
| 224 | * qobject_cast<QMenu *>(guiFactory()->container("menuId", this)); |
| 225 | * \endcode |
| 226 | * |
| 227 | * ...after having called setupGUI() or createGUI(). |
| 228 | * |
| 229 | * Returns a pointer to the QAction in the collection that matches the parameters or |
| 230 | * null if nothing matches. |
| 231 | */ |
| 232 | Q_INVOKABLE QAction *action(const QString &name) const; |
| 233 | |
| 234 | /*! |
| 235 | * \brief Returns the list of QActions that belong to this action collection. |
| 236 | * |
| 237 | * The list is guaranteed to be in the same order the action were put into |
| 238 | * the collection. |
| 239 | */ |
| 240 | QList<QAction *> actions() const; |
| 241 | |
| 242 | /*! |
| 243 | * \brief Returns the list of QActions without an QAction::actionGroup() |
| 244 | * that belong to this action collection. |
| 245 | */ |
| 246 | const QList<QAction *> actionsWithoutGroup() const; |
| 247 | |
| 248 | /*! |
| 249 | * \brief Returns the list of all QActionGroups associated |
| 250 | * with actions in this action collection. |
| 251 | */ |
| 252 | const QList<QActionGroup *> actionGroups() const; |
| 253 | |
| 254 | /*! |
| 255 | * \brief Sets the \a componentName associated with this action collection. |
| 256 | * |
| 257 | * \warning Don't call this method on a KActionCollection that contains |
| 258 | * actions. This is not supported. |
| 259 | * |
| 260 | * \a componentName The name which is to be associated with this action collection, |
| 261 | * or QString() to indicate the app name. This is used to load/save settings into XML files. |
| 262 | * KXMLGUIClient::setComponentName takes care of calling this. |
| 263 | */ |
| 264 | void setComponentName(const QString &componentName); |
| 265 | |
| 266 | /*! \brief The component name with which this class is associated. */ |
| 267 | QString componentName() const; |
| 268 | |
| 269 | /*! |
| 270 | * \brief Set the component \a displayName associated with this action collection. |
| 271 | * |
| 272 | * This can be done, for example, for the toolbar editor. |
| 273 | * |
| 274 | * KXMLGUIClient::setComponentName takes care of calling this. |
| 275 | */ |
| 276 | void setComponentDisplayName(const QString &displayName); |
| 277 | |
| 278 | /*! \brief The display name for the associated component. */ |
| 279 | QString componentDisplayName() const; |
| 280 | |
| 281 | /*! |
| 282 | * \brief The parent KXMLGUIClient, or null if not available. |
| 283 | */ |
| 284 | const KXMLGUIClient *parentGUIClient() const; |
| 285 | |
| 286 | Q_SIGNALS: |
| 287 | /*! |
| 288 | * \brief Indicates that \a action was inserted into this action collection. |
| 289 | */ |
| 290 | void inserted(QAction *action); |
| 291 | |
| 292 | /*! |
| 293 | * Emitted when an action has been inserted into, or removed from, this action collection. |
| 294 | * \since 5.66 |
| 295 | */ |
| 296 | void changed(); |
| 297 | |
| 298 | /*! |
| 299 | * \brief Indicates that \a action was hovered. |
| 300 | */ |
| 301 | void actionHovered(QAction *action); |
| 302 | |
| 303 | /*! |
| 304 | * \brief Indicates that \a action was triggered. |
| 305 | */ |
| 306 | void actionTriggered(QAction *action); |
| 307 | |
| 308 | protected: |
| 309 | /// Overridden to perform connections when someone wants to know whether an action was highlighted or triggered |
| 310 | void connectNotify(const QMetaMethod &signal) override; |
| 311 | |
| 312 | protected Q_SLOTS: |
| 313 | virtual void slotActionTriggered(); |
| 314 | |
| 315 | private Q_SLOTS: |
| 316 | KXMLGUI_NO_EXPORT void slotActionHovered(); |
| 317 | |
| 318 | public: |
| 319 | /*! |
| 320 | * \brief Add an \a action under the given \a name to the collection. |
| 321 | * |
| 322 | * Inserting an action that was previously inserted under a different name will replace the |
| 323 | * old entry, i.e. the action will not be available under the old name anymore but only under |
| 324 | * the new one. |
| 325 | * |
| 326 | * Inserting an action under a name that is already used for another action will replace |
| 327 | * the other action in the collection (but will not delete it). |
| 328 | * |
| 329 | * If KAuthorized::authorizeAction() reports that the action is not |
| 330 | * authorized, it will be disabled and hidden. |
| 331 | * |
| 332 | * The ownership of the action object is not transferred. |
| 333 | * If the action is destroyed it will be removed automatically from the KActionCollection. |
| 334 | * |
| 335 | * \a name The name by which the action be retrieved again from the collection. |
| 336 | * |
| 337 | * \a action The action to add. |
| 338 | * |
| 339 | * Returns the same as the action given as parameter. This is just for convenience |
| 340 | * (chaining calls) and consistency with the other addAction methods, you can also |
| 341 | * simply ignore the return value. |
| 342 | */ |
| 343 | Q_INVOKABLE QAction *addAction(const QString &name, QAction *action); |
| 344 | |
| 345 | /*! |
| 346 | * \brief Adds a list of \a actions to the collection. |
| 347 | * |
| 348 | * The objectName of the actions is used as their internal name in the collection. |
| 349 | * |
| 350 | * The ownership of the action objects is not transferred. |
| 351 | * If the action is destroyed it will be removed automatically from the KActionCollection. |
| 352 | * |
| 353 | * Uses addAction(const QString&, QAction*). |
| 354 | * |
| 355 | * \sa addAction() |
| 356 | * \since 5.0 |
| 357 | */ |
| 358 | void addActions(const QList<QAction *> &actions); |
| 359 | |
| 360 | /*! |
| 361 | * \brief Removes an \a action from the collection and deletes it. |
| 362 | */ |
| 363 | void removeAction(QAction *action); |
| 364 | |
| 365 | /*! |
| 366 | * \brief Removes an \a action from the collection. |
| 367 | * |
| 368 | * The ownership of the action object is not changed. |
| 369 | */ |
| 370 | QAction *takeAction(QAction *action); |
| 371 | |
| 372 | /*! |
| 373 | * \brief Creates a new standard action, adds it to the collection and connects the |
| 374 | * action's triggered(bool) signal to the specified receiver/member. The |
| 375 | * newly created action is also returned. |
| 376 | * |
| 377 | * \note Using KStandardAction::OpenRecent will cause a different signal than |
| 378 | * triggered(bool) to be used, see KStandardAction for more information. |
| 379 | * |
| 380 | * The action can be retrieved later from the collection by its standard name as per |
| 381 | * KStandardAction::stdName. |
| 382 | * |
| 383 | * The KActionCollection takes ownership of the action object. |
| 384 | * |
| 385 | * \a actionType The standard action type of the action to create. |
| 386 | * |
| 387 | * \a receiver The QObject to connect the triggered(bool) signal to. Leave nullptr if no |
| 388 | * connection is desired. |
| 389 | * |
| 390 | * \a member The SLOT to connect the triggered(bool) signal to. Leave nullptr if no |
| 391 | * connection is desired. |
| 392 | * |
| 393 | * Returns new action of the given type ActionType. |
| 394 | */ |
| 395 | QAction *addAction(KStandardAction::StandardAction actionType, const QObject *receiver = nullptr, const char *member = nullptr); |
| 396 | |
| 397 | /*! |
| 398 | * \brief Creates a new standard action, adds to the collection under the given name |
| 399 | * and connects the action's triggered(bool) signal to the specified |
| 400 | * receiver/member. The newly created action is also returned. |
| 401 | * |
| 402 | * \note Using KStandardAction::OpenRecent will cause a different signal than |
| 403 | * triggered(bool) to be used, see KStandardAction for more information. |
| 404 | * |
| 405 | * The action can be retrieved later from the collection by the specified name. |
| 406 | * |
| 407 | * The KActionCollection takes ownership of the action object. |
| 408 | * |
| 409 | * \a actionType The standard action type of the action to create. |
| 410 | * |
| 411 | * \a name The name by which the action be retrieved again from the collection. |
| 412 | * |
| 413 | * \a receiver The QObject to connect the triggered(bool) signal to. Leave nullptr if no |
| 414 | * connection is desired. |
| 415 | * |
| 416 | * \a member The SLOT to connect the triggered(bool) signal to. Leave nullptr if no |
| 417 | * connection is desired. |
| 418 | * |
| 419 | * Returns new action of the given type ActionType. |
| 420 | */ |
| 421 | QAction *addAction(KStandardAction::StandardAction actionType, const QString &name, const QObject *receiver = nullptr, const char *member = nullptr); |
| 422 | |
| 423 | /*! |
| 424 | * \brief This is the same as addAction(KStandardAction::StandardAction actionType, const QString &name, const QObject *receiver, const char *member) |
| 425 | * but using new style connect syntax. |
| 426 | * |
| 427 | * \a actionType The standard action type of the action to create. |
| 428 | * |
| 429 | * \a name The name by which the action be retrieved again from the collection. |
| 430 | * |
| 431 | * \a receiver The QObject to connect the triggered(bool) signal to. |
| 432 | * |
| 433 | * \a slot The slot or lambda to connect the triggered(bool) signal to. |
| 434 | * |
| 435 | * Returns new action of the given type ActionType. |
| 436 | * |
| 437 | * \sa addAction(KStandardAction::StandardAction, const QString &, const QObject *, const char *) |
| 438 | * \since 5.80 |
| 439 | */ |
| 440 | #ifdef Q_QDOC |
| 441 | template<class Receiver, class Func> |
| 442 | inline QAction *addAction(KStandardAction::StandardAction actionType, const QString &name, const Receiver *receiver, Func slot) |
| 443 | #else |
| 444 | template<class Receiver, class Func> |
| 445 | inline typename std::enable_if<!std::is_convertible<Func, const char *>::value, QAction>::type * |
| 446 | addAction(KStandardAction::StandardAction actionType, const QString &name, const Receiver *receiver, Func slot) |
| 447 | #endif |
| 448 | { |
| 449 | QAction *action = KStandardAction::create(actionType, receiver, slot, nullptr); |
| 450 | action->setParent(this); |
| 451 | action->setObjectName(name); |
| 452 | return addAction(name, action); |
| 453 | } |
| 454 | |
| 455 | /*! |
| 456 | * \brief This is the same as addAction(KStandardAction::StandardAction actionType, const QString &name, const Receiver *receiver, Func slot) |
| 457 | * but using KStandardActions from KConfigGui. |
| 458 | * |
| 459 | * \a actionType The standard action type of the action to create. |
| 460 | * |
| 461 | * \a name The name by which the action be retrieved again from the collection. |
| 462 | * |
| 463 | * \a receiver The QObject to connect the triggered(bool) signal to. |
| 464 | * |
| 465 | * \a slot The slot or lambda to connect the triggered(bool) signal to. |
| 466 | * |
| 467 | * Returns new action of the given type ActionType. |
| 468 | * \since 6.3 |
| 469 | */ |
| 470 | #ifdef Q_QDOC |
| 471 | template<class Receiver, class Func> |
| 472 | inline QAction *addAction(KStandardActions::StandardAction actionType, const QString &name, const Receiver *receiver, Func slot) |
| 473 | #else |
| 474 | template<class Receiver, class Func> |
| 475 | inline typename std::enable_if<!std::is_convertible<Func, const char *>::value, QAction>::type * |
| 476 | addAction(KStandardActions::StandardAction actionType, const QString &name, const Receiver *receiver, Func slot) |
| 477 | #endif |
| 478 | { |
| 479 | // Use implementation from KConfigWidgets instead of KConfigGui |
| 480 | // as it provides tighter integration with QtWidgets applications. |
| 481 | QAction *action = KStandardAction::create(static_cast<KStandardAction::StandardAction>(actionType), receiver, slot, nullptr); |
| 482 | action->setParent(this); |
| 483 | action->setObjectName(name); |
| 484 | return addAction(name, action); |
| 485 | } |
| 486 | |
| 487 | /*! |
| 488 | * Creates a new standard action and adds it to the collection. |
| 489 | * |
| 490 | * The newly created action is also returned. |
| 491 | * |
| 492 | * \a actionType The standard action type of the action to create. |
| 493 | * |
| 494 | * \a name The name by which the action be retrieved again from the collection. |
| 495 | * |
| 496 | * \return new action of the given type ActionType. |
| 497 | * |
| 498 | * \since 6.15 |
| 499 | */ |
| 500 | QAction *addAction(KStandardActions::StandardAction actionType, const QString &name); |
| 501 | |
| 502 | /*! |
| 503 | * Creates a new standard action, adds it to the collection and connects the |
| 504 | * action's triggered(bool) signal to the specified receiver/member. The |
| 505 | * newly created action is also returned. |
| 506 | * |
| 507 | * \note Using KStandardAction::OpenRecent will cause a different signal than |
| 508 | * triggered(bool) to be used, see KStandardAction for more information. |
| 509 | * |
| 510 | * The action can be retrieved later from the collection by its standard name as per |
| 511 | * KStandardAction::stdName. |
| 512 | * |
| 513 | * The KActionCollection takes ownership of the action object. |
| 514 | * |
| 515 | * \a actionType The standard action type of the action to create. |
| 516 | * |
| 517 | * \a receiver The QObject to connect the triggered(bool) signal to. Leave nullptr if no |
| 518 | * connection is desired. |
| 519 | * |
| 520 | * \a slot The slot or lambda to connect the triggered(bool) signal to. |
| 521 | * |
| 522 | * Returns new action of the given type ActionType. |
| 523 | * |
| 524 | * \since 6.3 |
| 525 | */ |
| 526 | #ifdef Q_QDOC |
| 527 | template<class Receiver, class Func> |
| 528 | inline QAction *addAction(KStandardActions::StandardAction actionType, const Receiver *receiver, Func slot) |
| 529 | #else |
| 530 | template<class Receiver, class Func> |
| 531 | inline typename std::enable_if<!std::is_convertible<Func, const char *>::value, QAction>::type * |
| 532 | addAction(KStandardActions::StandardAction actionType, const Receiver *receiver, Func slot) |
| 533 | #endif |
| 534 | { |
| 535 | // Use implementation from KConfigWidgets instead of KConfigGui |
| 536 | // as it provides tighter integration with QtWidgets applications. |
| 537 | // KStandardAction automatically adds it to the collection. |
| 538 | QAction *action = KStandardAction::create(static_cast<KStandardAction::StandardAction>(actionType), receiver, slot, this); |
| 539 | return action; |
| 540 | } |
| 541 | |
| 542 | /*! |
| 543 | * Creates a new standard action and adds it to the collection. |
| 544 | * The newly created action is also returned. |
| 545 | * |
| 546 | * The action can be retrieved later from the collection by its standard name as per |
| 547 | * KStandardAction::stdName. |
| 548 | * |
| 549 | * The KActionCollection takes ownership of the action object. |
| 550 | * |
| 551 | * \a actionType The standard action type of the action to create. |
| 552 | * |
| 553 | * Returns new action of the given type ActionType. |
| 554 | * |
| 555 | * \since 6.9 |
| 556 | */ |
| 557 | QAction *addAction(KStandardActions::StandardAction actionType); |
| 558 | |
| 559 | /*! |
| 560 | * Creates a new action under the given name to the collection and connects |
| 561 | * the action's triggered(bool) signal to the specified receiver/member. The |
| 562 | * newly created action is returned. |
| 563 | * |
| 564 | * \note KDE prior to 4.2 used the triggered() signal instead of the triggered(bool) |
| 565 | * signal. |
| 566 | * |
| 567 | * Inserting an action that was previously inserted under a different name will replace the |
| 568 | * old entry, i.e. the action will not be available under the old name anymore but only under |
| 569 | * the new one. |
| 570 | * |
| 571 | * Inserting an action under a name that is already used for another action will replace |
| 572 | * the other action in the collection. |
| 573 | * |
| 574 | * The KActionCollection takes ownership of the action object. |
| 575 | * |
| 576 | * \a name The name by which the action be retrieved again from the collection. |
| 577 | * |
| 578 | * \a receiver The QObject to connect the triggered(bool) signal to. Leave nullptr if no |
| 579 | * connection is desired. |
| 580 | * |
| 581 | * \a member The SLOT to connect the triggered(bool) signal to. Leave nullptr if no |
| 582 | * connection is desired. |
| 583 | * |
| 584 | * Returns new action of the given type ActionType. |
| 585 | */ |
| 586 | QAction *addAction(const QString &name, const QObject *receiver = nullptr, const char *member = nullptr); |
| 587 | |
| 588 | /*! |
| 589 | * \brief Creates a new action under the given name, adds it to the collection, |
| 590 | * and connects the action's triggered(bool) signal to the specified receiver/member. |
| 591 | * |
| 592 | * The receiver slot may accept either a bool or no |
| 593 | * parameters at all (i.e. slotTriggered(bool) or slotTriggered() ). |
| 594 | * The type of the action is specified by the template parameter ActionType. |
| 595 | * |
| 596 | * \note KDE prior to 4.2 connected the triggered() signal instead of the triggered(bool) |
| 597 | * signal. |
| 598 | * |
| 599 | * The KActionCollection takes ownership of the action object. |
| 600 | * |
| 601 | * \a name The internal name of the action (e.g. "file-open"). |
| 602 | * |
| 603 | * \a receiver The QObject to connect the triggered(bool) signal to. Leave nullptr if no |
| 604 | * connection is desired. |
| 605 | * |
| 606 | * \a member The SLOT to connect the triggered(bool) signal to. Leave nullptr if no |
| 607 | * connection is desired. |
| 608 | * |
| 609 | * Returns new action of the given type ActionType. |
| 610 | * |
| 611 | * \sa addAction() |
| 612 | */ |
| 613 | template<class ActionType> |
| 614 | ActionType *add(const QString &name, const QObject *receiver = nullptr, const char *member = nullptr) |
| 615 | { |
| 616 | ActionType *a = new ActionType(this); |
| 617 | if (receiver && member) { |
| 618 | connect(a, SIGNAL(triggered(bool)), receiver, member); |
| 619 | } |
| 620 | addAction(name, a); |
| 621 | return a; |
| 622 | } |
| 623 | |
| 624 | /*! |
| 625 | * |
| 626 | * |
| 627 | * \brief This is the same as add(const QString &name, const QObject *receiver, const char *member) |
| 628 | * but using new style connect syntax. |
| 629 | * |
| 630 | * \a name The internal name of the action (e.g. "file-open"). |
| 631 | * |
| 632 | * \a receiver The QObject to connect the triggered(bool) signal to. |
| 633 | * |
| 634 | * \a slot The slot or lambda to connect the triggered(bool) signal to. |
| 635 | * |
| 636 | * Returns new action of the given type ActionType. |
| 637 | * |
| 638 | * \sa add(const QString &, const QObject *, const char *) |
| 639 | * \since 5.28 |
| 640 | */ |
| 641 | #ifdef Q_QDOC |
| 642 | template<class ActionType, typename Receiver, typename Func> |
| 643 | ActionType *add(const QString &name, const Receiver *receiver, Func slot) |
| 644 | #else |
| 645 | template<class ActionType, class Receiver, class Func> |
| 646 | inline typename std::enable_if<!std::is_convertible<Func, const char *>::value, ActionType>::type * |
| 647 | add(const QString &name, const Receiver *receiver, Func slot) |
| 648 | #endif |
| 649 | { |
| 650 | ActionType *a = new ActionType(this); |
| 651 | connect(a, &QAction::triggered, receiver, slot); |
| 652 | addAction(name, a); |
| 653 | return a; |
| 654 | } |
| 655 | |
| 656 | /*! |
| 657 | * \brief This is the same as addAction(const QString &name, const QObject *receiver, const char *member) |
| 658 | * but using new style connect syntax. |
| 659 | * |
| 660 | * \a name The internal name of the action (e.g. "file-open"). |
| 661 | * |
| 662 | * \a receiver The QObject to connect the triggered(bool) signal to. |
| 663 | * |
| 664 | * \a slot The slot or lambda to connect the triggered(bool) signal to. |
| 665 | * |
| 666 | * Returns new action of the given type ActionType. |
| 667 | * |
| 668 | * \sa addAction(const QString &, const QObject *, const char *) |
| 669 | * \since 5.28 |
| 670 | */ |
| 671 | #ifdef Q_QDOC |
| 672 | template<class Receiver, class Func> |
| 673 | inline QAction *addAction(const QString &name, const Receiver *receiver, Func slot) |
| 674 | #else |
| 675 | template<class Receiver, class Func> |
| 676 | inline typename std::enable_if<!std::is_convertible<Func, const char *>::value, QAction>::type * |
| 677 | addAction(const QString &name, const Receiver *receiver, Func slot) |
| 678 | #endif |
| 679 | { |
| 680 | return add<QAction>(name, receiver, slot); |
| 681 | } |
| 682 | |
| 683 | /*! |
| 684 | * \brief Returns the default primary shortcut for the given \a action. |
| 685 | * |
| 686 | * \since 5.0 |
| 687 | */ |
| 688 | static QKeySequence defaultShortcut(QAction *action); |
| 689 | |
| 690 | /*! |
| 691 | * \brief Returns the default shortcuts for the given \a action. |
| 692 | * |
| 693 | * \since 5.0 |
| 694 | */ |
| 695 | static QList<QKeySequence> defaultShortcuts(QAction *action); |
| 696 | |
| 697 | /*! |
| 698 | * \brief Sets the default \a shortcut for the given \a action. |
| 699 | * |
| 700 | * Since 5.2, this also calls action->setShortcut(shortcut), |
| 701 | * i.e. the default shortcut is made active initially. |
| 702 | * |
| 703 | * \since 5.0 |
| 704 | */ |
| 705 | static void setDefaultShortcut(QAction *action, const QKeySequence &shortcut); |
| 706 | |
| 707 | /*! |
| 708 | * \brief Sets the default \a shortcuts in their specified shortcutContext() |
| 709 | * for the given \a action. |
| 710 | * |
| 711 | * Since 5.2, this also calls action->setShortcuts(shortcuts), |
| 712 | * i.e. the default shortcut is made active initially. |
| 713 | |
| 714 | * \since 5.0 |
| 715 | */ |
| 716 | Q_INVOKABLE static void setDefaultShortcuts(QAction *action, const QList<QKeySequence> &shortcuts); |
| 717 | |
| 718 | /*! |
| 719 | * \brief Returns true if the given \a action shortcuts may be configured by the user. |
| 720 | * |
| 721 | * \since 5.0 |
| 722 | */ |
| 723 | static bool isShortcutsConfigurable(QAction *action); |
| 724 | |
| 725 | /*! |
| 726 | * \brief Sets whether the \a action shortcuts may be \a configurable |
| 727 | * by the user. |
| 728 | * |
| 729 | * \since 5.0 |
| 730 | */ |
| 731 | static void setShortcutsConfigurable(QAction *action, bool configurable); |
| 732 | |
| 733 | private: |
| 734 | KXMLGUI_NO_EXPORT explicit KActionCollection(const KXMLGUIClient *parent); // used by KXMLGUIClient |
| 735 | |
| 736 | friend class KActionCollectionPrivate; |
| 737 | std::unique_ptr<class KActionCollectionPrivate> const d; |
| 738 | }; |
| 739 | |
| 740 | #endif |
| 741 | |