File indexing completed on 2024-04-28 11:44:16

0001 /***************************************************************************
0002  * actioncollectionview.h
0003  * This file is part of the KDE project
0004  * copyright (c) 2005-2006 Cyrille Berger <cberger@cberger.net>
0005  * copyright (C) 2006-2007 Sebastian Sauer <mail@dipe.org>
0006  *
0007  * This program is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU Library General Public
0009  * License as published by the Free Software Foundation; either
0010  * version 2 of the License, or (at your option) any later version.
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Library General Public License for more details.
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this program; see the file COPYING.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  ***************************************************************************/
0020 
0021 #ifndef KROSS_VIEW_H
0022 #define KROSS_VIEW_H
0023 
0024 #include <kross/ui/krossui_export.h>
0025 
0026 #include <QObject>
0027 #include <QWidget>
0028 #include <QModelIndex>
0029 #include <QItemSelection>
0030 #include <QTreeView>
0031 
0032 class KLineEdit;
0033 class QComboBox;
0034 class QPushButton;
0035 class QItemSelection;
0036 class KActionCollection;
0037 class KUrlRequester;
0038 
0039 namespace Kross
0040 {
0041 
0042 class Action;
0043 class ActionCollection;
0044 
0045 /**
0046 * The ActionCollectionEditor class implements a general editor
0047 * for \a Action and \a ActionCollection instances.
0048 *
0049 * Example that shows how to display the editor with an \a Action ;
0050 * \code
0051 * KDialog d(this);
0052 * Action* a = new Action();
0053 * new ActionCollectionEditor(a, d.mainWidget());
0054 * d.exec();
0055 * \endcode
0056 *
0057 * Example that shows how to display the editor with an \a ActionCollection ;
0058 * \code
0059 * KDialog d(this);
0060 * ActionCollection* c = new ActionCollection();
0061 * new ActionCollectionEditor(c, d.mainWidget());
0062 * d.exec();
0063 * \endcode
0064 */
0065 class KROSSUI_EXPORT ActionCollectionEditor : public QWidget
0066 {
0067 public:
0068 
0069     /**
0070     * Constructor.
0071     * \param action The \a Action instance this editor should
0072     * operate on.
0073     * \param parent The optional parent widget this widget is child of.
0074     */
0075     explicit ActionCollectionEditor(Action *action, QWidget *parent = nullptr);
0076 
0077     /**
0078     * Constructor.
0079     * \param collection The \a ActionCollection instance this
0080     * editor should operate on.
0081     * \param parent The optional parent widget this widget is child of.
0082     */
0083     explicit ActionCollectionEditor(ActionCollection *collection, QWidget *parent = nullptr);
0084 
0085     /**
0086     * Destructor.
0087     */
0088     ~ActionCollectionEditor() override;
0089 
0090     /**
0091     * \return the \a Action instance this editor operates on or
0092     * NULL if this editor does not operate on a \a Action instance.
0093     */
0094     Action *action() const;
0095 
0096     /**
0097     * \return the \a ActionCollection instance this editor operates on or
0098     * NULL if this editor does not operate on a \a ActionCollection instance.
0099     */
0100     ActionCollection *collection() const;
0101 
0102     /**
0103     * \return true if the values within the editor fields are valid.
0104     */
0105     virtual bool isValid();
0106 
0107     /**
0108     * This method got called if the changes done in the editor
0109     * should be saved aka committed to the \a Action or
0110     * \a ActionCollection instance.
0111     */
0112     virtual void commit();
0113 
0114     /**
0115     * Following getters are providing access to the edit-widgets once the
0116     * \a initGui() was called by the constructors. Some widgets like e.g.
0117     * the \a fileEdit() may return NULL if they are not used / displayed
0118     * (what is for the fileEdit the case if this \a ActionCollectionEditor
0119     * instance has no \a Action ).
0120     */
0121     QLineEdit *nameEdit() const;
0122     QLineEdit *textEdit() const;
0123     QLineEdit *commentEdit() const;
0124     QLineEdit *iconEdit() const;
0125     QComboBox *interpreterEdit() const;
0126     KUrlRequester *fileEdit() const;
0127 
0128 protected:
0129 
0130     /**
0131     * Initialize the GUI. Called by the both constructors and does
0132     * create e.g. the \a nameEdit() and the \a textEdit() widgets.
0133     */
0134     virtual void initGui();
0135 
0136 private:
0137     /// \internal d-pointer class.
0138     class Private;
0139     /// \internal d-pointer instance.
0140     Private *const d;
0141 };
0142 
0143 /**
0144 * The ActionCollectionView class shows a QTreeView where the content of a
0145 * \a ActionCollection is displayed and optional actions to run, stop,
0146 * add, edit and remove scripts are provided.
0147 *
0148 * Example how to create, fill and use an instance of a ActionCollectionView;
0149 * \code
0150 * // We like to show the widget in a dialog.
0151 * KDialog d(this);
0152 * // Create the view.
0153 * ActionCollectionView* v = new ActionCollectionView( d.mainWidget() );
0154 * // Create the model.
0155 * ActionCollectionModel::Mode mode( ActionCollectionModel::Icons | ActionCollectionModel::ToolTips | ActionCollectionModel::UserCheckable );
0156 * ActionCollectionModel* m = new ActionCollectionModel(view, Manager::self().actionCollection(), mode);
0157 * // Set the model the view should use.
0158 * v->setModel(m);
0159 * // Show the dialog.
0160 * d.exec();
0161 * \endcode
0162 */
0163 class KROSSUI_EXPORT ActionCollectionView : public QTreeView
0164 {
0165     Q_OBJECT
0166 public:
0167 
0168     /**
0169     * Constructor.
0170     * \param parent The optional parent widget this widget is child of.
0171     */
0172     explicit ActionCollectionView(QWidget *parent = nullptr);
0173 
0174     /**
0175     * Destructor.
0176     */
0177     ~ActionCollectionView() override;
0178 
0179     /**
0180     * Set the model this view should use to \p model . Use an instance of
0181     * \a ActionCollectionModel or \a ActionCollectionProxyModel as model.
0182     */
0183     void setModel(QAbstractItemModel *model) override;
0184 
0185     /**
0186     * \return true if the collection was modified.
0187     */
0188     bool isModified() const;
0189 
0190     /**
0191     * Set the internal modified state of the collection to \p modified .
0192     */
0193     void setModified(bool modified);
0194 
0195     /**
0196     * \return the KActionCollection which is filled with KAction
0197     * instances this view provides. Per default there are the
0198     * actions "run" to run a script, "stop" to stop execution, "edit"
0199     * to edit the selected item, "add" to add a new item or resource,
0200     * "remove" to remove the selected item and "manager" to call and
0201     * show the modal Script Manager dialog.
0202     */
0203     KActionCollection *actionCollection() const;
0204 
0205     /**
0206     * \return the QPushButton instance which has the actionname \p actionname
0207     * or NULL if there is not such button.
0208     */
0209     QPushButton *button(const QString &actionname) const;
0210 
0211     /**
0212     * Create and return a new QPushButton instance for the given actionname.
0213     * \param parentWidget The parent widget.
0214     * \param actionname The name of the action. Each button points to an action
0215     * from within the \a actionCollection() and triggers that action if the
0216     * button got clicked.
0217     * \return The new QPushButton instance or NULL if e.g. there exist no
0218     * such action with \p actionname .
0219     */
0220     QPushButton *createButton(QWidget *parentWidget, const QString &actionname);
0221 
0222 public Q_SLOTS:
0223 
0224     /**
0225     * Called if the "run" action was triggered and the selected script
0226     * should be executed.
0227     */
0228     virtual void slotRun();
0229 
0230     /**
0231     * Called if the "stop" action was triggered and the selected script
0232     * stops execution if running.
0233     */
0234     virtual void slotStop();
0235 
0236     /**
0237     * Called if the "edit" action was triggered and the select item should
0238     * be edited via the scripts manager editor dialog.
0239     */
0240     virtual void slotEdit();
0241 
0242     /**
0243     * Called if the "add" action was triggered and a new item should be added.
0244     */
0245     virtual void slotAdd();
0246 
0247     /**
0248     * Called if the "remove" action was triggered and the selected item
0249     * should be removed.
0250     */
0251     virtual void slotRemove();
0252 
0253 Q_SIGNALS:
0254 
0255     /**
0256     * This signal is emitted if the enabled/disabled state of an action
0257     * changed. This happens for example if the slotSelectionChanged() above
0258     * got called cause another item was selected.
0259     * \param actionname The name of the action that changed. You are able
0260     * to use \a actionCollection() to receive the to the name matching
0261     * \a KAction instance. You are able to use e.g. a QSignalMapper here to
0262     * map such changes direct to your e.g. \a QPushButton instances used
0263     * to display some of the actions provided with \a actionCollection() .
0264     */
0265     void enabledChanged(const QString &actionname);
0266 
0267 protected Q_SLOTS:
0268 
0269     /**
0270     * This slot got called if the data changed.
0271     */
0272     virtual void slotDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
0273 
0274     /**
0275     * This slot got called if the enable/disable state of an action changed.
0276     */
0277     virtual void slotEnabledChanged(const QString &actionname);
0278 
0279     /**
0280     * This slot got called if the selected item changed.
0281     */
0282     virtual void slotSelectionChanged();
0283 
0284 protected:
0285 
0286     /**
0287     * This method provides us access to the QItemSelection. Compared to
0288     * the selectionModel()->selection() method this method does also
0289     * map the selection to the source-model for the case e.g. the
0290     * \a ActionCollectionProxyModel proxy-model was used rather then
0291     * a \a ActionCollectionModel direct.
0292     */
0293     QItemSelection itemSelection() const;
0294 
0295 private:
0296     /// \internal d-pointer class.
0297     class Private;
0298     /// \internal d-pointer instance.
0299     Private *const d;
0300 };
0301 
0302 }
0303 
0304 #endif