File indexing completed on 2024-04-14 14:29:57

0001 /*
0002     SPDX-FileCopyrightText: 2008 Michael Jansen <kde@michael-jansen.biz>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "kactioncategory.h"
0008 
0009 #include <QAction>
0010 
0011 struct KActionCategoryPrivate {
0012     KActionCategoryPrivate(KActionCategory *host);
0013 
0014     //! Our host
0015     KActionCategory *q;
0016 
0017     //! The text for this category
0018     QString text;
0019 
0020     //! List of actions
0021     QList<QAction *> actions;
0022 
0023 }; // class KActionCategoryPrivate
0024 
0025 KActionCategory::KActionCategory(const QString &text, KActionCollection *parent)
0026     : QObject(parent)
0027     , d(new KActionCategoryPrivate(this))
0028 {
0029     d->text = text;
0030 }
0031 
0032 KActionCategory::~KActionCategory() = default;
0033 
0034 const QList<QAction *> KActionCategory::actions() const
0035 {
0036     return d->actions;
0037 }
0038 
0039 QAction *KActionCategory::addAction(const QString &name, QAction *action)
0040 {
0041     collection()->addAction(name, action);
0042     addAction(action);
0043     return action;
0044 }
0045 
0046 QAction *KActionCategory::addAction(KStandardAction::StandardAction actionType, const QObject *receiver, const char *member)
0047 {
0048     QAction *action = collection()->addAction(actionType, receiver, member);
0049     addAction(action);
0050     return action;
0051 }
0052 
0053 QAction *KActionCategory::addAction(KStandardAction::StandardAction actionType, const QString &name, const QObject *receiver, const char *member)
0054 {
0055     QAction *action = collection()->addAction(actionType, name, receiver, member);
0056     addAction(action);
0057     return action;
0058 }
0059 
0060 QAction *KActionCategory::addAction(const QString &name, const QObject *receiver, const char *member)
0061 {
0062     QAction *action = collection()->addAction(name, receiver, member);
0063     addAction(action);
0064     return action;
0065 }
0066 
0067 void KActionCategory::addAction(QAction *action)
0068 {
0069     // Only add the action if wasn't added earlier.
0070     if (!d->actions.contains(action)) {
0071         d->actions.append(action);
0072     }
0073 }
0074 
0075 KActionCollection *KActionCategory::collection() const
0076 {
0077     return qobject_cast<KActionCollection *>(parent());
0078 }
0079 
0080 QString KActionCategory::text() const
0081 {
0082     return d->text;
0083 }
0084 
0085 void KActionCategory::setText(const QString &text)
0086 {
0087     d->text = text;
0088 }
0089 
0090 void KActionCategory::unlistAction(QAction *action)
0091 {
0092     // ATTENTION:
0093     //   This method is called from KActionCollection with an QObject formerly
0094     //   known as a QAction during _k_actionDestroyed(). So don't do fancy stuff
0095     //   here that needs a real QAction!
0096 
0097     // Get the index for the action
0098     int index = d->actions.indexOf(action);
0099 
0100     // Action not found.
0101     if (index == -1) {
0102         return;
0103     }
0104 
0105     // Remove the action
0106     d->actions.takeAt(index);
0107 }
0108 
0109 KActionCategoryPrivate::KActionCategoryPrivate(KActionCategory *host)
0110     : q(host)
0111 {
0112 }
0113 
0114 #include "moc_kactioncategory.cpp"