Salome HOME
Merge branch 'Dev_0.7.1' of newgeom:newgeom into Dev_0.7.1
[modules/shaper.git] / src / XGUI / XGUI_DocumentDataModel.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 #ifndef XGUI_DocumentDataModel_H
4 #define XGUI_DocumentDataModel_H
5
6 #include "XGUI.h"
7 #include <ModuleBase_Definitions.h>
8 #include <ModelAPI_ResultPart.h>
9
10 #include <Events_Listener.h>
11
12 #include <QAbstractItemModel>
13 #include <QList>
14
15 class ModelAPI_Document;
16 class XGUI_PartModel;
17 class XGUI_TopDataModel;
18
19 /**\class XGUI_DocumentDataModel
20  * \ingroup GUI
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.
23  */
24 class XGUI_EXPORT XGUI_DocumentDataModel : public QAbstractItemModel, public Events_Listener
25 {
26 Q_OBJECT
27  public:
28    /// Constructor
29    /// \param theParent a parent object
30   XGUI_DocumentDataModel(QObject* theParent);
31   virtual ~XGUI_DocumentDataModel();
32
33   /// Event Listener method
34   /// \param theMessage an event message
35   virtual void processEvent(const std::shared_ptr<Events_Message>& theMessage);
36
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;
41
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;
48
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;
53
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;
58
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 =
64                                 QModelIndex()) const;
65
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;
70
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;
74
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());
81
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());
87
88   /// Returns the item flags for the given index.
89   /// \param theIndex a model index
90   Qt::ItemFlags flags(const QModelIndex& theIndex) const;
91
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;
95
96   //! Returns index of the object
97   //! \param theObject object to find
98   QModelIndex objectIndex(const ObjectPtr theObject) const;
99
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;
104
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);
109
110   //! Retrurns active part
111   ResultPartPtr activePart() const;
112
113   //! Retrurns QModelIndex of active part
114   QModelIndex activePartIndex() const
115   {
116     return myActivePartIndex;
117   }
118
119   //! Deactivates a Part
120   void deactivatePart();
121
122   //! Rebuild data tree
123   void rebuildDataTree();
124
125   //! Clear internal data
126   void clear();
127
128
129  private:
130
131   enum
132   {
133     PartsFolder,
134     HistoryNode
135   };
136
137   //! Converts QModelIndex of this model to QModelIndex of a one of sub-models.
138   QModelIndex* toSourceModelIndex(const QModelIndex& theProxy) const;
139
140   //! Finds a pointer on QModelIndex which is equal to the given one
141   QModelIndex* findModelIndex(const QModelIndex& theIndex) const;
142
143   //! Returns pointer on QModelIndex which is equal to the given one.
144   QModelIndex* getModelIndex(const QModelIndex& theIndex) const;
145
146   //! Deletes all saved pointers on QModelIndex objects.
147   void clearModelIndexes();
148
149   //! Deletes all saved pointers on QModelIndex objects.
150   void clearSubModels();
151
152   //! Removes sub-model on removing a part object. Also it removes QModelIndex-es which refer to this model
153   void removeSubModel(int theModelId);
154
155   //! Returns true if the given model is a one of sub-models (of both types)
156   bool isSubModel(const QAbstractItemModel* theModel) const;
157
158   //! Returns true if the given model is a one of sub-models of Part type
159   bool isPartSubModel(const QAbstractItemModel* theModel) const;
160
161   //! Returns Parts Folder node
162   QModelIndex partFolderNode() const;
163
164   int historyOffset() const;
165
166   //! Data model of top part of data tree (not parts object)
167   XGUI_TopDataModel* myModel;
168
169   //! Data models for Parts data tree representation (one data model per a one part)
170   QList<XGUI_PartModel*> myPartModels;
171
172   //! Active part in part editing mode
173   XGUI_PartModel* myActivePart;
174
175   QModelIndex myActivePartIndex;
176
177   //! List of saved QModelIndexes created by sub-models
178   QList<QModelIndex*> myIndexes;
179
180 };
181
182 #endif