1 // Copyright (C) 2014-2024 CEA, EDF
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #ifndef XGUI_DataModel_H
21 #define XGUI_DataModel_H
24 #include <ModuleBase_Definitions.h>
25 #include <ModelAPI_Object.h>
26 #include <ModelAPI_Document.h>
27 #include <Events_Listener.h>
29 #include <QAbstractItemModel>
32 class ModuleBase_ITreeNode;
34 /**\class XGUI_DataModel
36 * \brief This is a data model for Object Browser (QTreeView).
37 * It uses XML file for definition of data tree.
38 * Some tips about organization of the model:
39 * - Non Valid Index - root index
40 * - An index with internal Id == -1 is a folder of root document
41 * - An index which contains internal pointer as ModelAPI_Object its the object
42 * - An index which contains internal pointer as ModelAPI_Document is
43 * a folder which belongs to this document
45 class XGUI_EXPORT XGUI_DataModel : public QAbstractItemModel, public Events_Listener
50 /// \param theParent a parent object
51 XGUI_DataModel(QObject* theParent);
54 virtual ~XGUI_DataModel();
57 void setRoot(ModuleBase_ITreeNode* theRoot) { myRoot = theRoot; }
59 ModuleBase_ITreeNode* root() const { return myRoot; }
61 /// Event Listener method
62 /// \param theMessage an event message
63 virtual void processEvent(const std::shared_ptr<Events_Message>& theMessage);
65 //! Returns an object by the given Model index.
66 //! Returns 0 if the given index is not index of an object
67 virtual ObjectPtr object(const QModelIndex& theIndex) const;
69 //! Returns index of the object
70 //! \param theObject object to find
71 virtual QModelIndex objectIndex(const ObjectPtr theObject, int theColumn = 1) const;
73 //! Clear internal data
77 virtual void rebuildDataTree();
79 /// Returns the data stored under the given role for the item referred to by the index.
80 /// \param theIndex a model index
81 /// \param theRole a data role (see Qt::ItemDataRole)
82 virtual QVariant data(const QModelIndex& theIndex, int theRole) const;
84 /// Returns the data for the given role and section in the header with the specified orientation.
85 /// \param theSection a section
86 /// \param theOrient an orientation
87 /// \param theRole a data role (see Qt::ItemDataRole)
88 virtual QVariant headerData(int theSection, Qt::Orientation theOrient, int theRole =
89 Qt::DisplayRole) const;
91 /// Returns the number of rows under the given parent. When the parent is valid it means that
92 /// rowCount is returning the number of children of parent.
93 /// \param theParent a parent model index
94 virtual int rowCount(const QModelIndex& theParent = QModelIndex()) const;
96 /// Returns the number of columns for the children of the given parent.
97 /// It has a one column
98 /// \param theParent a parent model index
99 virtual int columnCount(const QModelIndex& theParent = QModelIndex()) const;
101 /// Returns the index of the item in the model specified by the given row,
102 /// column and parent index.
103 /// \param theRow a row
104 /// \param theColumn a column
105 /// \param theParent a parent model index
106 virtual QModelIndex index(int theRow, int theColumn, const QModelIndex &theParent =
107 QModelIndex()) const;
109 /// Returns the parent of the model item with the given index.
110 /// If the item has no parent, an invalid QModelIndex is returned.
111 /// \param theIndex a model index
112 virtual QModelIndex parent(const QModelIndex& theIndex) const;
114 /// Returns true if parent has any children; otherwise returns false.
115 /// \param theParent a parent model index
116 virtual bool hasChildren(const QModelIndex& theParent = QModelIndex()) const;
118 /// Inserts count rows into the model before the given row.
119 /// Items in the new row will be children of the item represented by the parent model index.
120 /// \param theRow a start row
121 /// \param theCount a nember of rows to insert
122 /// \param theParent a parent model index
123 virtual bool insertRows(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
125 /// Removes count rows starting with the given row under parent parent from the model.
126 /// \param theRow a start row
127 /// \param theCount a nember of rows to remove
128 /// \param theParent a parent model index
129 virtual bool removeRows(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
131 /// Returns the item flags for the given index.
132 /// \param theIndex a model index
133 virtual Qt::ItemFlags flags(const QModelIndex& theIndex) const;
135 /// Returns true if a model can accept a drop of the data (used for drag and drop functionality).
136 virtual bool canDropMimeData(const QMimeData *theData, Qt::DropAction theAction,
137 int theRow, int theColumn, const QModelIndex &theParent) const override;
139 /// Converts the dragged items information into mime data format (to be encoded in dropMimeData)
140 virtual QMimeData* mimeData(const QModelIndexList &indexes) const override;
142 /// Performs a drag and drop of Part feature operation when it is droped.
143 virtual bool dropMimeData(const QMimeData *theData, Qt::DropAction theAction,
144 int theRow, int theColumn, const QModelIndex &theParent) override;
146 /// Returns an index which is root of the given document
147 /// \param theDoc a document
148 QModelIndex documentRootIndex(DocumentPtr theDoc, int theColumn = 1) const;
150 /// Returns true if the data model item has Hidden visual state
151 /// \param theIndex a tree model item
152 /// \return boolean value
153 bool hasHiddenState(const QModelIndex& theIndex);
155 /// Returns true if the given index exists in data tree
156 /// \param theIndex an index to check
157 bool hasIndex(const QModelIndex& theIndex) const;
159 /// Returns true if the given node is a parto of the data tree
160 /// \param theNode a node to check
161 bool hasNode(ModuleBase_ITreeNode* theNode) const;
163 /// Returns document of the given index
164 /// \param theIndex an index to check
165 DocumentPtr document(const QModelIndex& theIndex) const;
167 /// Returns index for the node and column
168 /// \param theNode a node
169 /// \param thCol a column
170 QModelIndex getIndex(ModuleBase_ITreeNode* theNode, int thCol) const;
172 /// Allows to support drag and drop of some model items
173 virtual Qt::DropActions supportedDropActions() const override
175 return Qt::MoveAction;
179 /// Signal send before tree rebuild
180 void beforeTreeRebuild();
182 /// Signal about tree had been rebuilt
186 enum VisibilityState {
193 QModelIndex getParentIndex(ModuleBase_ITreeNode* theNode, int thCol) const;
195 void updateSubTree(ModuleBase_ITreeNode* theParent);
197 XGUI_Workshop* myWorkshop;
198 QMap<DocumentPtr, QStringList> myShownFolders;
200 ModuleBase_ITreeNode* myRoot;