File indexing completed on 2024-04-21 15:02:57

0001 /***************************************************************************
0002  * actioncollection.h
0003  * This file is part of the KDE project
0004  * copyright (C)2004-2007 by Sebastian Sauer (mail@dipe.org)
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this program; see the file COPYING.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  ***************************************************************************/
0019 
0020 #ifndef KROSS_ACTIONCOLLECTION_H
0021 #define KROSS_ACTIONCOLLECTION_H
0022 
0023 #include "krossconfig.h"
0024 #include "action.h"
0025 
0026 #include <QObject>
0027 #include <QDir>
0028 
0029 class QDomElement;
0030 class QIODevice;
0031 
0032 namespace Kross
0033 {
0034 
0035 /**
0036  * The ActionCollection class manages collections of \a Action instances.
0037  * An ActionCollection can have both actions and other collections as children.
0038  * Child actions can be accessed using actions() which returns a list of Action pointers.
0039  * Child collections can be accessed using collections() which returns a list of collection names.
0040  * The collection can then be accessed with collection(name).
0041  * To add a child action, call addAction(), and to remove an action: removeAction().
0042  * To add a child collection, call setParentCollection(parent) in the collection you want to add to parent.
0043  * To remove a collection call setParentCollection(0).
0044  * NOTE: Do not use setParent().
0045  */
0046 class KROSSCORE_EXPORT ActionCollection : public QObject
0047 {
0048     Q_OBJECT
0049 
0050 public:
0051 
0052     /**
0053       * Constructor.
0054       *
0055       * \param name The objectName the ActionCollection has.
0056       * \param parent The parent ActionCollection this
0057       * ActionCollection will be child of. If parent is not
0058       * NULL, this \a ActionCollection instance will register
0059       * itself as child of the parent \p parent by using the
0060       * \a setParentCollection method.
0061       */
0062     explicit ActionCollection(const QString &name, ActionCollection *parent = nullptr);
0063 
0064     /**
0065      * Destructor.
0066      */
0067     ~ActionCollection() override;
0068 
0069     /**
0070      * \return the objectName for this ActionCollection.
0071      */
0072     QString name() const;
0073 
0074     /**
0075      * \return the display text
0076      */
0077     QString text() const;
0078 
0079     /**
0080      * Set the display text to \p text .
0081      */
0082     void setText(const QString &text);
0083 
0084     /**
0085      * \return the optional description for this ActionCollection.
0086      */
0087     QString description() const;
0088 
0089     /**
0090      * Set the optional description for this ActionCollection.
0091      */
0092     void setDescription(const QString &description);
0093 
0094     /**
0095      * \return the name of the icon.
0096      */
0097     QString iconName() const;
0098 
0099     /**
0100      * Set the name of the icon to \p iconname .
0101      */
0102     void setIconName(const QString &iconname);
0103 
0104     /**
0105      * \return the icon defined with \p setIconName() .
0106      */
0107     QIcon icon() const;
0108 
0109     /**
0110      * Return the enable this ActionCollection has.
0111      */
0112     bool isEnabled() const;
0113 
0114     /**
0115      * Enable or disable this ActionCollection.
0116      */
0117     void setEnabled(bool enabled);
0118 
0119     /**
0120      * \return the parent \a ActionCollection instance this
0121      * \collection is child of or NULL if this collection
0122      * does not have a parent.
0123      */
0124     ActionCollection *parentCollection() const;
0125     /// Set the parent to @p parent. NOTE: Do not use setParent().
0126     void setParentCollection(ActionCollection *parent);
0127     /**
0128      * \return true if this collection has a child \a ActionCollection
0129      * instance which objectName is \p name .
0130      */
0131     bool hasCollection(const QString &name) const;
0132 
0133     /**
0134      * \return the \a ActionCollection instance which objectName is
0135      * \p name or NULL if there exists no such \a ActionCollection .
0136      */
0137     ActionCollection *collection(const QString &name) const;
0138 
0139     /**
0140      * \return a list of names of child \a ActionCollection instances
0141      * this collection has
0142      */
0143     QStringList collections() const;
0144 
0145     QList<Action *> actions() const;
0146 
0147     /**
0148      * \return action with given name or 0 if it wasn't found
0149      */
0150     Action *action(const QString &name) const;
0151     void addAction(Action *action);
0152     void addAction(const QString &name, Action *action);
0153     void removeAction(const QString &name);
0154     void removeAction(Action *action);
0155 
0156     /**
0157      * Load child \a Action and \a ActionCollection instances this
0158      * collection has from the \p element .
0159      *
0160      * \param element The QDomElement that contains the XML.
0161      * \param directory The current directory used for relative paths
0162      * defined within a script-tag for the file-attribute. If the
0163      * directory is QDir() relative paths are not resolved.
0164      * \return true on success else false.
0165      */
0166     bool readXml(const QDomElement &element, const QDir &directory = QDir());
0167     bool readXml(const QDomElement &element, const QStringList &searchPath/* = QStringList()*/);
0168 
0169     /**
0170      * Read XML from the QIODevice \p device .
0171      */
0172     bool readXml(QIODevice *device, const QDir &directory = QDir());
0173     bool readXml(QIODevice *device, const QStringList &searchPath/* = QStringList()*/);
0174 
0175     /**
0176      * Read the XML from the file \p file .
0177      *
0178      * \param file The existing XML file that should be read.
0179      * \return true if reading was successful else false.
0180      */
0181     bool readXmlFile(const QString &file);
0182 
0183     /**
0184      * \return a QDomElement that represents the child \a Action
0185      * and \a ActionCollection instances this collection has.
0186      */
0187     QDomElement writeXml();
0188     QDomElement writeXml(const QStringList &searchPath/* = QStringList()*/);
0189 
0190     /**
0191      * Write XML to the QIODevice \p device and use a space-idention
0192      * of \p indent for the XML.
0193      */
0194     bool writeXml(QIODevice *device, int indent = 2);
0195     bool writeXml(QIODevice *device, int indent/* = 2*/, const QStringList &searchPath/* = QStringList()*/);
0196 
0197 Q_SIGNALS:
0198 
0199     /**
0200      * This signal is emitted if the content of the ActionCollection
0201      * was changed.
0202      */
0203     void updated();
0204 
0205     /// This signal is emitted when the data of a child action is changed
0206     void dataChanged(Action *);
0207     /// This signal is emitted when the data of the ActionCollection is changed
0208     void dataChanged(ActionCollection *);
0209 
0210     /// This signal is emitted just before @p child is added to @p parent
0211     void collectionToBeInserted(ActionCollection *child, ActionCollection *parent);
0212     /// This signal is emitted after @p child has been added to @p parent
0213     void collectionInserted(ActionCollection *child, ActionCollection *parent);
0214     /// This signal is emitted before @p child is removed from @p parent
0215     void collectionToBeRemoved(ActionCollection *child, ActionCollection *parent);
0216     /// This signal is emitted after @p child has been removed from @p parent
0217     void collectionRemoved(ActionCollection *child, ActionCollection *parent);
0218 
0219     /// This signal is emitted just before @p child is added to @p parent
0220     void actionToBeInserted(Action *child, ActionCollection *parent);
0221     /// This signal is emitted after @p child has been added to @p parent
0222     void actionInserted(Action *child, ActionCollection *parent);
0223     /// This signal is emitted before @p child is removed from @p parent
0224     void actionToBeRemoved(Action *child, ActionCollection *parent);
0225     /// This signal is emitted after @p child has been removed from @p parent
0226     void actionRemoved(Action *child, ActionCollection *parent);
0227 
0228 protected:
0229     void registerCollection(ActionCollection *collection);
0230     void unregisterCollection(const QString &name);
0231     void connectSignals(ActionCollection *collection, bool conn);
0232     void connectSignals(Action *collection, bool conn);
0233 
0234 private Q_SLOTS:
0235     void emitUpdated();
0236 
0237 private:
0238     /// \internal d-pointer class.
0239     class Private;
0240     /// \internal d-pointer instance.
0241     Private *const d;
0242 };
0243 
0244 }
0245 
0246 #endif
0247