1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 #ifndef XGUI_DocumentDataModel_H
4 #define XGUI_DocumentDataModel_H
7 #include <ModuleBase_Definitions.h>
8 #include <ModelAPI_ResultPart.h>
10 #include <Events_Listener.h>
12 #include <QAbstractItemModel>
15 class ModelAPI_Document;
17 class XGUI_TopDataModel;
19 /**\class XGUI_DocumentDataModel
21 * \brief This is a proxy data model for Object Browser (QTreeView).
22 * It contains several sub-models for generation of each sub-part of data tree.
24 class XGUI_EXPORT XGUI_DocumentDataModel : public QAbstractItemModel, public Events_Listener
29 /// \param theParent a parent object
30 XGUI_DocumentDataModel(QObject* theParent);
31 virtual ~XGUI_DocumentDataModel();
33 /// Event Listener method
34 /// \param theMessage an event message
35 virtual void processEvent(const std::shared_ptr<Events_Message>& theMessage);
37 /// Returns the data stored under the given role for the item referred to by the index.
38 /// \param theIndex a model index
39 /// \param theRole a data role (see Qt::ItemDataRole)
40 virtual QVariant data(const QModelIndex& theIndex, int theRole) const;
42 /// Returns the data for the given role and section in the header with the specified orientation.
43 /// \param theSection a section
44 /// \param theOrient an orientation
45 /// \param theRole a data role (see Qt::ItemDataRole)
46 virtual QVariant headerData(int theSection, Qt::Orientation theOrient, int theRole =
47 Qt::DisplayRole) const;
49 /// Returns the number of rows under the given parent. When the parent is valid it means that
50 /// rowCount is returning the number of children of parent.
51 /// \param theParent a parent model index
52 virtual int rowCount(const QModelIndex& theParent = QModelIndex()) const;
54 /// Returns the number of columns for the children of the given parent.
55 /// It has a one column
56 /// \param theParent a parent model index
57 virtual int columnCount(const QModelIndex& theParent = QModelIndex()) const;
59 /// Returns the index of the item in the model specified by the given row, column and parent index.
60 /// \param theRow a row
61 /// \param theColumn a column
62 /// \param theParent a parent model index
63 virtual QModelIndex index(int theRow, int theColumn, const QModelIndex &theParent =
66 /// Returns the parent of the model item with the given index.
67 /// If the item has no parent, an invalid QModelIndex is returned.
68 /// \param theIndex a model index
69 virtual QModelIndex parent(const QModelIndex& theIndex) const;
71 /// Returns true if parent has any children; otherwise returns false.
72 /// \param theParent a parent model index
73 virtual bool hasChildren(const QModelIndex& theParent = QModelIndex()) const;
75 /// Inserts count rows into the model before the given row.
76 /// Items in the new row will be children of the item represented by the parent model index.
77 /// \param theRow a start row
78 /// \param theCount a nember of rows to insert
79 /// \param theParent a parent model index
80 bool insertRows(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
82 /// Removes count rows starting with the given row under parent parent from the model.
83 /// \param theRow a start row
84 /// \param theCount a nember of rows to remove
85 /// \param theParent a parent model index
86 bool removeRows(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
88 /// Returns the item flags for the given index.
89 /// \param theIndex a model index
90 Qt::ItemFlags flags(const QModelIndex& theIndex) const;
92 //! Returns an object by the given Model index.
93 //! Returns 0 if the given index is not index of an object
94 ObjectPtr object(const QModelIndex& theIndex) const;
96 //! Returns index of the object
97 //! \param theObject object to find
98 QModelIndex objectIndex(const ObjectPtr theObject) const;
100 //! Returns QModelIndex which corresponds to the given part
101 //! If the object is not found then index is not valid
102 //! \param thePart a part for analysis
103 QModelIndex partIndex(const ResultPartPtr& thePart) const;
105 //! Activates a part data model if the index is a Part node index.
106 //! Returns true if active part changed.
107 //! \param theIndex a model index
108 bool activatedIndex(const QModelIndex& theIndex);
110 //! Retrurns active part
111 ResultPartPtr activePart() const;
113 //! Retrurns QModelIndex of active part
114 QModelIndex activePartIndex() const
116 return myActivePartIndex;
119 //! Deactivates a Part
120 void deactivatePart();
122 //! Rebuild data tree
123 void rebuildDataTree();
125 //! Clear internal data
137 //! Converts QModelIndex of this model to QModelIndex of a one of sub-models.
138 QModelIndex* toSourceModelIndex(const QModelIndex& theProxy) const;
140 //! Finds a pointer on QModelIndex which is equal to the given one
141 QModelIndex* findModelIndex(const QModelIndex& theIndex) const;
143 //! Returns pointer on QModelIndex which is equal to the given one.
144 QModelIndex* getModelIndex(const QModelIndex& theIndex) const;
146 //! Deletes all saved pointers on QModelIndex objects.
147 void clearModelIndexes();
149 //! Deletes all saved pointers on QModelIndex objects.
150 void clearSubModels();
152 //! Removes sub-model on removing a part object. Also it removes QModelIndex-es which refer to this model
153 void removeSubModel(int theModelId);
155 //! Returns true if the given model is a one of sub-models (of both types)
156 bool isSubModel(const QAbstractItemModel* theModel) const;
158 //! Returns true if the given model is a one of sub-models of Part type
159 bool isPartSubModel(const QAbstractItemModel* theModel) const;
161 //! Returns Parts Folder node
162 QModelIndex partFolderNode() const;
164 int historyOffset() const;
166 //! Data model of top part of data tree (not parts object)
167 XGUI_TopDataModel* myModel;
169 //! Data models for Parts data tree representation (one data model per a one part)
170 QList<XGUI_PartModel*> myPartModels;
172 //! Active part in part editing mode
173 XGUI_PartModel* myActivePart;
175 QModelIndex myActivePartIndex;
177 //! List of saved QModelIndexes created by sub-models
178 QList<QModelIndex*> myIndexes;