Salome HOME
Compsolid creation in extrusion
[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 #include <QMap>
15
16 class ModelAPI_Document;
17 class PartSet_PartModel;
18 class PartSet_TopDataModel;
19
20 /**\class PartSet_DocumentDataModel
21  * \ingroup GUI
22  * \brief This is a proxy data model for Object Browser (QTreeView).
23  * It contains several sub-models for generation of each sub-part of data tree.
24  */
25 class PARTSET_EXPORT PartSet_DocumentDataModel : public ModuleBase_IDocumentDataModel, public Events_Listener
26 {
27 Q_OBJECT
28  public:
29    /// Constructor
30    /// \param theParent a parent object
31   PartSet_DocumentDataModel(QObject* theParent);
32   virtual ~PartSet_DocumentDataModel();
33
34   /// Event Listener method
35   /// \param theMessage an event message
36   virtual void processEvent(const std::shared_ptr<Events_Message>& theMessage);
37
38   /// Returns the data stored under the given role for the item referred to by the index.
39   /// \param theIndex a model index
40   /// \param theRole a data role (see Qt::ItemDataRole)
41   virtual QVariant data(const QModelIndex& theIndex, int theRole) const;
42
43   /// Returns the data for the given role and section in the header with the specified orientation.
44   /// \param theSection a section
45   /// \param theOrient an orientation
46   /// \param theRole a data role (see Qt::ItemDataRole)
47   virtual QVariant headerData(int theSection, Qt::Orientation theOrient, int theRole =
48                                   Qt::DisplayRole) const;
49
50   /// Returns the number of rows under the given parent. When the parent is valid it means that 
51   /// rowCount is returning the number of children of parent.
52   /// \param theParent a parent model index
53   virtual int rowCount(const QModelIndex& theParent = QModelIndex()) const;
54
55   /// Returns the number of columns for the children of the given parent.
56   /// It has a one column
57   /// \param theParent a parent model index
58   virtual int columnCount(const QModelIndex& theParent = QModelIndex()) const;
59
60   /// Returns the index of the item in the model specified by the given row, column and parent index.
61   /// \param theRow a row
62   /// \param theColumn a column
63   /// \param theParent a parent model index
64   virtual QModelIndex index(int theRow, int theColumn, const QModelIndex &theParent =
65                                 QModelIndex()) const;
66
67   /// Returns the parent of the model item with the given index. 
68   /// If the item has no parent, an invalid QModelIndex is returned.
69   /// \param theIndex a model index
70   virtual QModelIndex parent(const QModelIndex& theIndex) const;
71
72   /// Returns true if parent has any children; otherwise returns false.
73   /// \param theParent a parent model index
74   virtual bool hasChildren(const QModelIndex& theParent = QModelIndex()) const;
75
76   /// Inserts count rows into the model before the given row. 
77   /// Items in the new row will be children of the item represented by the parent model index.
78   /// \param theRow a start row
79   /// \param theCount a nember of rows to insert
80   /// \param theParent a parent model index
81   bool insertRows(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
82
83   /// Removes count rows starting with the given row under parent parent from the model.
84   /// \param theRow a start row
85   /// \param theCount a nember of rows to remove
86   /// \param theParent a parent model index
87   bool removeRows(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
88
89   /// Returns the item flags for the given index.
90   /// \param theIndex a model index
91   virtual Qt::ItemFlags flags(const QModelIndex& theIndex) const;
92
93   //! Returns an object by the given Model index.
94   //! Returns 0 if the given index is not index of an object
95   virtual ObjectPtr object(const QModelIndex& theIndex) const;
96
97   //! Returns index of the object
98   //! \param theObject object to find
99   virtual QModelIndex objectIndex(const ObjectPtr theObject) const;
100
101   //! Returns QModelIndex which corresponds to the given part
102   //! If the object is not found then index is not valid
103   //! \param thePart a part for analysis
104   QModelIndex partIndex(const ResultPartPtr& thePart) const;
105
106   //! Activates a part data model if the index is a Part node index. 
107   //! Returns true if active part changed.
108   //! \param theIndex a model index
109   bool activatePart(const QModelIndex& theIndex);
110
111   //! Retrurns active part
112   ResultPartPtr activePart() const;
113
114   //! Retrurns QModelIndex of active part
115   QModelIndex activePartIndex() const
116   {
117     return myActivePartIndex;
118   }
119
120   //! Returns parent index of active part tree (index of Part feature) 
121   QModelIndex activePartTree() const;
122
123   //! Deactivates a Part
124   void deactivatePart();
125
126   //! Rebuild data tree
127   virtual void rebuildDataTree();
128
129   //! Clear internal data
130   virtual void clear();
131
132   //! Set an Index which will be considered as a last history index
133   //! \param theIndex a last index for history
134   void setLastHistoryItem(const QModelIndex& theIndex);
135
136   //! Returns last history item
137   QModelIndex lastHistoryItem() const;
138
139   //! Returns icon name according to feature
140   static QIcon featureIcon(const FeaturePtr& theFeature);
141
142  public slots:
143    void onMouseDoubleClick(const QModelIndex& theIndex);
144
145  private:
146
147   enum
148   {
149     PartsFolder,
150     HistoryNode,
151     PartResult
152   };
153
154   //! Converts QModelIndex of this model to QModelIndex of a one of sub-models.
155   QModelIndex* toSourceModelIndex(const QModelIndex& theProxy) const;
156
157   //! Finds a pointer on QModelIndex which is equal to the given one
158   QModelIndex* findModelIndex(const QModelIndex& theIndex) const;
159
160   //! Returns pointer on QModelIndex which is equal to the given one.
161   QModelIndex* getModelIndex(const QModelIndex& theIndex) const;
162
163   //! Deletes all saved pointers on QModelIndex objects.
164   void clearModelIndexes();
165
166   //! Deletes all saved pointers on QModelIndex objects.
167   void clearSubModels();
168
169   //! Removes sub-model on removing a part object. Also it removes QModelIndex-es which refer to this model
170   void removeSubModel(int theModelId);
171
172   //! Removes sub-model on removing a part object. Also it removes QModelIndex-es which refer to this model
173   void removeSubModel(PartSet_PartModel* theModel);
174
175   //! Returns true if the given model is a one of sub-models (of both types)
176   bool isSubModel(const QAbstractItemModel* theModel) const;
177
178   //! Returns true if the given model is a one of sub-models of Part type
179   bool isPartSubModel(const QAbstractItemModel* theModel) const;
180
181   //! Returns Parts Folder node
182   //! \param theColumn an Id of column
183   QModelIndex partFolderNode(int theColumn) const;
184
185   int lastHistoryRow() const;
186
187   int historyOffset() const;
188
189   PartSet_PartModel* findPartModel(FeaturePtr thePart) const;
190
191   PartSet_PartModel* findPartModel(int thePosition) const;
192
193   //! Data model of top part of data tree (not parts object)
194   PartSet_TopDataModel* myModel;
195
196   //! Data models for Parts data tree representation (one data model per a one part)
197   QList<PartSet_PartModel*> myPartModels;
198
199   //! Active part in part editing mode
200   PartSet_PartModel* myActivePartModel;
201
202   QModelIndex myActivePartIndex;
203
204   //! List of saved QModelIndexes created by sub-models
205   QList<QModelIndex*> myIndexes;
206
207   static QMap<QString, QString> myIcons;
208 };
209
210 #endif