Salome HOME
Merge branch 'Dev_1.2.0' of newgeom:newgeom into Dev_1.2.0
[modules/shaper.git] / src / PartSet / PartSet_DocumentDataModel.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 #ifndef PartSet_DocumentDataModel_H
4 #define PartSet_DocumentDataModel_H
5
6 #include "PartSet.h"
7 #include <ModuleBase_Definitions.h>
8 #include <ModelAPI_ResultPart.h>
9 #include <ModelAPI_Feature.h>
10 #include <ModuleBase_IDocumentDataModel.h>
11
12 #include <Events_Listener.h>
13 #include <QList>
14
15 class ModelAPI_Document;
16 class PartSet_PartModel;
17 class PartSet_TopDataModel;
18
19 /**\class PartSet_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 PARTSET_EXPORT PartSet_DocumentDataModel : public ModuleBase_IDocumentDataModel, public Events_Listener
25 {
26 Q_OBJECT
27  public:
28    /// Constructor
29    /// \param theParent a parent object
30   PartSet_DocumentDataModel(QObject* theParent);
31   virtual ~PartSet_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   virtual 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   virtual ObjectPtr object(const QModelIndex& theIndex) const;
95
96   //! Returns index of the object
97   //! \param theObject object to find
98   virtual 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 activatePart(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   virtual void rebuildDataTree();
124
125   //! Clear internal data
126   virtual void clear();
127
128   //! Set an Index which will be considered as a last history index
129   //! \param theIndex a last index for history
130   void setLastHistoryItem(const QModelIndex& theIndex);
131
132   QModelIndex lastHistoryItem() const;
133
134   //! Returns icon name according to feature
135   static QIcon featureIcon(const FeaturePtr& theFeature);
136
137  private:
138
139   enum
140   {
141     PartsFolder,
142     HistoryNode
143   };
144
145   //! Converts QModelIndex of this model to QModelIndex of a one of sub-models.
146   QModelIndex* toSourceModelIndex(const QModelIndex& theProxy) const;
147
148   //! Finds a pointer on QModelIndex which is equal to the given one
149   QModelIndex* findModelIndex(const QModelIndex& theIndex) const;
150
151   //! Returns pointer on QModelIndex which is equal to the given one.
152   QModelIndex* getModelIndex(const QModelIndex& theIndex) const;
153
154   //! Deletes all saved pointers on QModelIndex objects.
155   void clearModelIndexes();
156
157   //! Deletes all saved pointers on QModelIndex objects.
158   void clearSubModels();
159
160   //! Removes sub-model on removing a part object. Also it removes QModelIndex-es which refer to this model
161   void removeSubModel(int theModelId);
162
163   //! Returns true if the given model is a one of sub-models (of both types)
164   bool isSubModel(const QAbstractItemModel* theModel) const;
165
166   //! Returns true if the given model is a one of sub-models of Part type
167   bool isPartSubModel(const QAbstractItemModel* theModel) const;
168
169   //! Returns Parts Folder node
170   //! \param theColumn an Id of column
171   QModelIndex partFolderNode(int theColumn) const;
172
173   int lastHistoryRow() const;
174
175   int historyOffset() const;
176
177   //! Data model of top part of data tree (not parts object)
178   PartSet_TopDataModel* myModel;
179
180   //! Data models for Parts data tree representation (one data model per a one part)
181   QList<PartSet_PartModel*> myPartModels;
182
183   //! Active part in part editing mode
184   PartSet_PartModel* myActivePart;
185
186   QModelIndex myActivePartIndex;
187
188   //! List of saved QModelIndexes created by sub-models
189   QList<QModelIndex*> myIndexes;
190
191   int myHistoryBackOffset;
192
193   static QMap<QString, QString> myIcons;
194 };
195
196 #endif