Salome HOME
Release version on WIndows
[modules/shaper.git] / src / XGUI / XGUI_PartDataModel.cpp
1 #include "XGUI_PartDataModel.h"
2
3 #include <ModelAPI_PluginManager.h>
4 #include <ModelAPI_Iterator.h>
5 #include <ModelAPI_Document.h>
6 #include <ModelAPI_Feature.h>
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_AttributeDocRef.h>
9
10 #include <QIcon>
11
12 XGUI_TopDataModel::XGUI_TopDataModel(const std::shared_ptr<ModelAPI_Document>& theDocument, QObject* theParent)
13   : XGUI_FeaturesModel(theDocument, theParent)
14 {
15 }
16   
17 XGUI_TopDataModel::~XGUI_TopDataModel()
18 {
19 }
20
21
22 QVariant XGUI_TopDataModel::data(const QModelIndex& theIndex, int theRole) const
23 {
24   switch (theRole) {
25   case Qt::DisplayRole:
26     // return a name
27     switch (theIndex.internalId()) {
28     case ParamsFolder:
29       return tr("Parameters");
30     case ParamObject:
31       {
32         std::shared_ptr<ModelAPI_Feature> aFeature = myDocument->feature(PARAMETERS_GROUP, theIndex.row());
33         if (aFeature)
34           return aFeature->data()->getName().c_str();
35       } 
36     case ConstructFolder:
37         return tr("Constructions");
38     case ConstructObject:
39       {
40         std::shared_ptr<ModelAPI_Feature> aFeature = myDocument->feature(CONSTRUCTIONS_GROUP, theIndex.row());
41         if (aFeature)
42           return aFeature->data()->getName().c_str();
43       }
44     }
45     break;
46
47   case Qt::DecorationRole:
48     // return an Icon
49     switch (theIndex.internalId()) {
50     case ParamsFolder:
51         return QIcon(":pictures/params_folder.png");
52     case ConstructFolder:
53         return QIcon(":pictures/constr_folder.png");
54     case ConstructObject:
55         return QIcon(":pictures/point_ico.png");
56     }
57     break;
58
59   case Qt::ToolTipRole:
60     // return Tooltip
61     break;
62   }
63   return QVariant();
64 }
65
66 QVariant XGUI_TopDataModel::headerData(int section, Qt::Orientation orientation, int role) const
67 {
68   return QVariant();
69 }
70
71 int XGUI_TopDataModel::rowCount(const QModelIndex& theParent) const
72 {
73   if (!theParent.isValid()) 
74     return 2;
75
76   if (theParent.internalId() == ParamsFolder)
77     return myDocument->featuresIterator(PARAMETERS_GROUP)->numIterationsLeft();
78
79   if (theParent.internalId() == ConstructFolder)
80     return myDocument->featuresIterator(CONSTRUCTIONS_GROUP)->numIterationsLeft();
81
82   return 0;
83 }
84
85 int XGUI_TopDataModel::columnCount(const QModelIndex &parent) const
86 {
87   return 1;
88 }
89
90 QModelIndex XGUI_TopDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
91 {
92   if (!theParent.isValid()) {
93     switch (theRow) {
94     case 0:
95       return createIndex(theRow, theColumn, (quintptr) ParamsFolder);
96     case 1:
97       return createIndex(theRow, theColumn, (quintptr) ConstructFolder);
98     }
99   } else {
100     if (theParent.internalId() == ParamsFolder)
101       return createIndex(theRow, theColumn, (quintptr) ParamObject);
102
103     if (theParent.internalId() == ConstructFolder)
104       return createIndex(theRow, theColumn, (quintptr) ConstructObject);
105   }
106   return QModelIndex();
107 }
108
109 QModelIndex XGUI_TopDataModel::parent(const QModelIndex& theIndex) const
110 {
111   int aId = (int)theIndex.internalId();
112   switch (aId) {
113   case ParamsFolder:
114   case ConstructFolder:
115     return QModelIndex();
116   case ParamObject:
117     return createIndex(0, 0, (quintptr) ParamsFolder);
118   case ConstructObject:
119     return createIndex(1, 0, (quintptr) ConstructFolder);
120   }
121   return QModelIndex();
122 }
123
124 bool XGUI_TopDataModel::hasChildren(const QModelIndex& theParent) const
125 {
126   return rowCount(theParent) > 0;
127 }
128
129 FeaturePtr XGUI_TopDataModel::feature(const QModelIndex& theIndex) const
130 {
131   switch (theIndex.internalId()) {
132   case ParamsFolder:
133   case ConstructFolder:
134     return 0;
135   case ParamObject:
136     return myDocument->feature(PARAMETERS_GROUP, theIndex.row());
137   case ConstructObject:
138     return myDocument->feature(CONSTRUCTIONS_GROUP, theIndex.row());
139   }
140   return 0;
141 }
142
143
144 QModelIndex XGUI_TopDataModel::findParent(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
145 {
146   QString aGroup(theFeature->getGroup().c_str());
147
148   if (theFeature->getGroup().compare(PARAMETERS_GROUP) == 0)
149     return createIndex(0, 0, (quintptr) ParamsFolder);
150   if (theFeature->getGroup().compare(CONSTRUCTIONS_GROUP) == 0)
151     return createIndex(1, 0, (quintptr) ConstructFolder);
152   return QModelIndex();
153 }
154
155
156 //******************************************************************
157 //******************************************************************
158 //******************************************************************
159 XGUI_PartDataModel::XGUI_PartDataModel(const std::shared_ptr<ModelAPI_Document>& theDocument, QObject* theParent)
160   : XGUI_PartModel(theDocument, theParent)
161 {
162 }
163
164
165 XGUI_PartDataModel::~XGUI_PartDataModel()
166 {
167 }
168
169 QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) const
170 {
171   switch (theRole) {
172   case Qt::DisplayRole:
173     // return a name
174     switch (theIndex.internalId()) {
175     case MyRoot:
176       {
177         std::shared_ptr<ModelAPI_Feature> aFeature = myDocument->feature(PARTS_GROUP, myId);
178         if (aFeature)
179           return aFeature->data()->getName().c_str();
180       }
181     case ParamsFolder:
182       return tr("Parameters");
183     case ConstructFolder:
184       return tr("Constructions");
185     case ParamObject:
186       {
187         std::shared_ptr<ModelAPI_Feature> aFeature = 
188           featureDocument()->feature(PARAMETERS_GROUP, theIndex.row());
189         if (aFeature)
190           return aFeature->data()->getName().c_str();
191       }
192     case ConstructObject:
193       {
194         std::shared_ptr<ModelAPI_Feature> aFeature = 
195           featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row());
196         if (aFeature)
197           return aFeature->data()->getName().c_str();
198       }
199     }
200     break;
201   case Qt::DecorationRole:
202     // return an Icon
203     switch (theIndex.internalId()) {
204     case MyRoot:
205       return QIcon(":pictures/part_ico.png");
206     case ParamsFolder:
207       return QIcon(":pictures/params_folder.png");
208     case ConstructFolder:
209       return QIcon(":pictures/constr_folder.png");
210     case ConstructObject:
211         return QIcon(":pictures/point_ico.png");
212     }
213    break;
214   case Qt::ToolTipRole:
215     // return Tooltip
216     break;
217   }
218   return QVariant();
219 }
220
221 QVariant XGUI_PartDataModel::headerData(int section, Qt::Orientation orientation, int role) const
222 {
223   return QVariant();
224 }
225
226 int XGUI_PartDataModel::rowCount(const QModelIndex& parent) const
227 {
228   if (!parent.isValid()) 
229     if (myDocument->feature(PARTS_GROUP, myId))
230       return 1;
231     else 
232       return 0;
233   switch (parent.internalId()) {
234   case MyRoot:
235     return 2;
236   case ParamsFolder:
237     return featureDocument()->featuresIterator(PARAMETERS_GROUP)->numIterationsLeft();
238   case ConstructFolder:
239     return featureDocument()->featuresIterator(CONSTRUCTIONS_GROUP)->numIterationsLeft();
240   }
241   return 0;
242 }
243
244 int XGUI_PartDataModel::columnCount(const QModelIndex &parent) const
245 {
246   return 1;
247 }
248
249 QModelIndex XGUI_PartDataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
250 {
251   if (!theParent.isValid())
252     return createIndex(theRow, 0, (quintptr) MyRoot);
253
254   int aId = (int)theParent.internalId();
255   switch (aId) {
256   case MyRoot:
257     switch (theRow) {
258     case 0:
259       return createIndex(0, 0, (quintptr) ParamsFolder);
260     case 1:
261       return createIndex(1, 0, (quintptr) ConstructFolder);
262     }
263   case ParamsFolder:
264     return createIndex(theRow, 0, (quintptr) ParamObject);
265   case ConstructFolder:
266     return createIndex(theRow, 0, (quintptr) ConstructObject);
267   }
268   return QModelIndex();
269 }
270
271 QModelIndex XGUI_PartDataModel::parent(const QModelIndex& theIndex) const
272 {
273   switch (theIndex.internalId()) {
274   case MyRoot:
275     return QModelIndex();
276   case ParamsFolder:
277   case ConstructFolder:
278     return createIndex(0, 0, (quintptr) MyRoot);
279   case ParamObject:
280     return createIndex(0, 0, (quintptr) ParamsFolder);
281   case ConstructObject:
282     return createIndex(1, 0, (quintptr) ConstructFolder);
283   }
284   return QModelIndex();
285 }
286
287 bool XGUI_PartDataModel::hasChildren(const QModelIndex& theParent) const
288 {
289   return rowCount(theParent) > 0;
290 }
291
292
293 std::shared_ptr<ModelAPI_Document> XGUI_PartDataModel::featureDocument() const
294 {
295   std::shared_ptr<ModelAPI_Feature> aFeature = myDocument->feature(PARTS_GROUP, myId);
296   return aFeature->data()->docRef("PartDocument")->value();
297 }
298
299 FeaturePtr XGUI_PartDataModel::feature(const QModelIndex& theIndex) const
300 {
301   switch (theIndex.internalId()) {
302   case MyRoot:
303     return myDocument->feature(PARTS_GROUP, myId);
304   case ParamsFolder:
305   case ConstructFolder:
306     return 0;
307   case ParamObject:
308     return featureDocument()->feature(PARAMETERS_GROUP, theIndex.row());
309   case ConstructObject:
310     return featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row());
311   }
312   return 0;
313 }
314
315 bool XGUI_PartDataModel::hasDocument(const std::shared_ptr<ModelAPI_Document>& theDoc) const
316 {
317   return (featureDocument() == theDoc);
318 }
319
320
321 QModelIndex XGUI_PartDataModel::findParent(const std::shared_ptr<ModelAPI_Feature>& theFeature) const
322 {
323   QString aGroup(theFeature->getGroup().c_str());
324
325   if (theFeature->getGroup().compare(PARAMETERS_GROUP) == 0)
326     return createIndex(0, 0, (quintptr) ParamsFolder);
327   if (theFeature->getGroup().compare(CONSTRUCTIONS_GROUP) == 0)
328     return createIndex(1, 0, (quintptr) ConstructFolder);
329   return QModelIndex();
330 }