File indexing completed on 2024-04-21 15:03:00
0001 /*************************************************************************** 0002 * actioncollectionmodel.h 0003 * This file is part of the KDE project 0004 * copyright (C) 2006-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_MODEL_H 0021 #define KROSS_MODEL_H 0022 0023 #include <kross/ui/krossui_export.h> 0024 0025 #include <QModelIndex> 0026 #include <QSortFilterProxyModel> 0027 0028 namespace Kross 0029 { 0030 0031 // Forward declarations. 0032 class Action; 0033 class ActionCollection; 0034 class ActionCollectionModelItem; 0035 0036 /** 0037 * The ActionCollectionModel class implements a QAbstractItemModel to provide 0038 * a model for views of a \a ActionCollection instance that manages a 0039 * collection of \a Action instances. 0040 * 0041 * Important implementation detatils: 0042 * \li An action can not have children. 0043 * \li A collection can have both collections and actions as children. 0044 * \li This model lists actions before collections. 0045 * \li The internalPointer() of QModelIndex is used to hold a pointer to the parent collection. 0046 */ 0047 class KROSSUI_EXPORT ActionCollectionModel : public QAbstractItemModel 0048 { 0049 Q_OBJECT 0050 public: 0051 enum Mode { 0052 None = 0, 0053 Icons = 1, 0054 ToolTips = 2, 0055 UserCheckable = 4 0056 //Editable = 8 0057 }; 0058 0059 explicit ActionCollectionModel(QObject *parent, ActionCollection *collection = nullptr, Mode mode = Mode(Icons | ToolTips)); 0060 ~ActionCollectionModel() override; 0061 0062 int columnCount(const QModelIndex &parent = QModelIndex()) const override; 0063 int rowCount(const QModelIndex &parent = QModelIndex()) const override; 0064 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; 0065 QModelIndex parent(const QModelIndex &index) const override; 0066 Qt::ItemFlags flags(const QModelIndex &index) const override; 0067 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; 0068 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; 0069 0070 bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; 0071 bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; 0072 bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override; 0073 bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override; 0074 0075 //Qt::DropActions supportedDragActions() const; 0076 QStringList mimeTypes() const override; 0077 QMimeData *mimeData(const QModelIndexList &indexes) const override; 0078 bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override; 0079 0080 Qt::DropActions supportedDropActions() const override; 0081 0082 QModelIndex indexForCollection(ActionCollection *collection) const; 0083 QModelIndex indexForAction(Action *action) const; 0084 /// Return the root collection 0085 ActionCollection *rootCollection() const; 0086 0087 /** 0088 * \return the \a Action instance the as argument passed QModelIndex 0089 * represents or NULL if the QModelIndex is not a \a Action . 0090 */ 0091 static Action *action(const QModelIndex &index); 0092 0093 /** 0094 * \return the \a ActionCollection instance the as argument passed QModelIndex 0095 * represents or NULL if the QModelIndex is not a \a ActionCollection . 0096 */ 0097 static ActionCollection *collection(const QModelIndex &index); 0098 0099 protected: 0100 /// @returns the row number of the @p collection 0101 int rowNumber(ActionCollection *collection) const; 0102 0103 private Q_SLOTS: 0104 void slotUpdated(); 0105 0106 void slotDataChanged(ActionCollection *); 0107 void slotDataChanged(Action *); 0108 0109 void slotCollectionToBeInserted(ActionCollection *child, ActionCollection *parent); 0110 void slotCollectionInserted(ActionCollection *child, ActionCollection *parent); 0111 void slotCollectionToBeRemoved(ActionCollection *child, ActionCollection *parent); 0112 void slotCollectionRemoved(ActionCollection *child, ActionCollection *parent); 0113 0114 void slotActionToBeInserted(Action *child, ActionCollection *parent); 0115 void slotActionInserted(Action *child, ActionCollection *parent); 0116 void slotActionToBeRemoved(Action *child, ActionCollection *parent); 0117 void slotActionRemoved(Action *child, ActionCollection *parent); 0118 private: 0119 /// \internal d-pointer class. 0120 class Private; 0121 /// \internal d-pointer instance. 0122 Private *const d; 0123 }; 0124 0125 /** 0126 * The ActionCollectionProxyModel class implements a QSortFilterProxyModel 0127 * for a \a ActionCollectionModel instance. 0128 */ 0129 class KROSSUI_EXPORT ActionCollectionProxyModel : public QSortFilterProxyModel 0130 { 0131 public: 0132 explicit ActionCollectionProxyModel(QObject *parent, ActionCollectionModel *model = nullptr); 0133 ~ActionCollectionProxyModel() override; 0134 0135 private: 0136 /// Set the \a ActionCollectionModel source model we are proxy for. 0137 void setSourceModel(QAbstractItemModel *sourceModel) override; 0138 /// Implements a filter for the QModelIndex instances. 0139 bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override; 0140 }; 0141 0142 } 0143 0144 #endif 0145