Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into SolveSpace
[modules/shaper.git] / src / XGUI / XGUI_PartDataModel.cpp
1 #include "XGUI_PartDataModel.h"
2 #include "XGUI_Workshop.h"
3
4 #include <ModelAPI_PluginManager.h>
5 #include <ModelAPI_Document.h>
6 #include <ModelAPI_Feature.h>
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_AttributeDocRef.h>
9 #include <ModelAPI_Object.h>
10
11 #include <QIcon>
12 #include <QBrush>
13
14
15 FeaturePtr featureObj(const FeaturePtr& theFeature)
16 {
17   ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
18   if (aObject)
19     return aObject->featureRef();
20   return theFeature;
21 }
22
23
24 XGUI_TopDataModel::XGUI_TopDataModel(const DocumentPtr& theDocument, QObject* theParent)
25   : XGUI_FeaturesModel(theDocument, theParent)
26 {
27 }
28   
29 XGUI_TopDataModel::~XGUI_TopDataModel()
30 {
31 }
32
33
34 QVariant XGUI_TopDataModel::data(const QModelIndex& theIndex, int theRole) const
35 {
36   switch (theRole) {
37   case Qt::DisplayRole:
38     // return a name
39     switch (theIndex.internalId()) {
40     case ParamsFolder:
41       return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex));
42     case ParamObject:
43       {
44         FeaturePtr aFeature = featureObj(myDocument->feature(PARAMETERS_GROUP, theIndex.row()));
45         if (aFeature)
46           return aFeature->data()->getName().c_str();
47       } 
48     case ConstructFolder:
49         return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
50     case ConstructObject:
51       {
52         FeaturePtr aFeature = featureObj(myDocument->feature(CONSTRUCTIONS_GROUP, theIndex.row()));
53         if (aFeature)
54           return aFeature->data()->getName().c_str();
55       }
56     }
57     break;
58
59   case Qt::DecorationRole:
60     // return an Icon
61     switch (theIndex.internalId()) {
62     case ParamsFolder:
63       return QIcon(":pictures/params_folder.png");
64     case ConstructFolder:
65       return QIcon(":pictures/constr_folder.png");
66     case ConstructObject:
67       {
68         FeaturePtr aFeature = featureObj(myDocument->feature(CONSTRUCTIONS_GROUP, theIndex.row()));
69         if (aFeature)
70           return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
71       }
72     }
73     break;
74
75   case Qt::ToolTipRole:
76     // return Tooltip
77     break;
78   case Qt::ForegroundRole:
79     return QBrush(myItemsColor);
80     break;
81   }
82   return QVariant();
83 }
84
85 QVariant XGUI_TopDataModel::headerData(int section, Qt::Orientation orientation, int role) const
86 {
87   return QVariant();
88 }
89
90 int XGUI_TopDataModel::rowCount(const QModelIndex& theParent) const
91 {
92   if (!theParent.isValid()) 
93     return 2;
94
95   if (theParent.internalId() == ParamsFolder)
96     return myDocument->size(PARAMETERS_GROUP);
97
98   if (theParent.internalId() == ConstructFolder)
99     return myDocument->size(CONSTRUCTIONS_GROUP);
100
101   return 0;
102 }
103
104 int XGUI_TopDataModel::columnCount(const QModelIndex &parent) const
105 {
106   return 1;
107 }
108
109 QModelIndex XGUI_TopDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
110 {
111   if (!theParent.isValid()) {
112     switch (theRow) {
113     case 0:
114       return createIndex(theRow, theColumn, (qint32) ParamsFolder);
115     case 1:
116       return createIndex(theRow, theColumn, (qint32) ConstructFolder);
117     }
118   } else {
119     if (theParent.internalId() == ParamsFolder)
120       return createIndex(theRow, theColumn, (qint32) ParamObject);
121
122     if (theParent.internalId() == ConstructFolder)
123       return createIndex(theRow, theColumn, (qint32) ConstructObject);
124   }
125   return QModelIndex();
126 }
127
128 QModelIndex XGUI_TopDataModel::parent(const QModelIndex& theIndex) const
129 {
130   int aId = (int)theIndex.internalId();
131   switch (aId) {
132   case ParamsFolder:
133   case ConstructFolder:
134     return QModelIndex();
135   case ParamObject:
136     return createIndex(0, 0, (qint32) ParamsFolder);
137   case ConstructObject:
138     return createIndex(1, 0, (qint32) ConstructFolder);
139   }
140   return QModelIndex();
141 }
142
143 bool XGUI_TopDataModel::hasChildren(const QModelIndex& theParent) const
144 {
145   return rowCount(theParent) > 0;
146 }
147
148 FeaturePtr XGUI_TopDataModel::feature(const QModelIndex& theIndex) const
149 {
150   switch (theIndex.internalId()) {
151   case ParamsFolder:
152   case ConstructFolder:
153     return FeaturePtr();
154   case ParamObject:
155     return featureObj(myDocument->feature(PARAMETERS_GROUP, theIndex.row()));
156   case ConstructObject:
157     return featureObj(myDocument->feature(CONSTRUCTIONS_GROUP, theIndex.row()));
158   }
159   return FeaturePtr();
160 }
161
162
163 QModelIndex XGUI_TopDataModel::findParent(const FeaturePtr& theFeature) const
164 {
165   QString aGroup(theFeature->getGroup().c_str());
166
167   if (theFeature->getGroup().compare(PARAMETERS_GROUP) == 0)
168     return createIndex(0, 0, (qint32) ParamsFolder);
169   if (theFeature->getGroup().compare(CONSTRUCTIONS_GROUP) == 0)
170     return createIndex(1, 0, (qint32) ConstructFolder);
171   return QModelIndex();
172 }
173
174 QModelIndex XGUI_TopDataModel::findGroup(const std::string& theGroup) const
175 {
176   if (theGroup.compare(PARAMETERS_GROUP) == 0)
177     return createIndex(0, 0, (qint32) ParamsFolder);
178   if (theGroup.compare(CONSTRUCTIONS_GROUP) == 0)
179     return createIndex(1, 0, (qint32) ConstructFolder);
180   return QModelIndex();
181 }
182
183
184 //******************************************************************
185 //******************************************************************
186 //******************************************************************
187 XGUI_PartDataModel::XGUI_PartDataModel(const DocumentPtr& theDocument, QObject* theParent)
188   : XGUI_PartModel(theDocument, theParent)
189 {
190 }
191
192
193 XGUI_PartDataModel::~XGUI_PartDataModel()
194 {
195 }
196
197 QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) const
198 {
199   switch (theRole) {
200   case Qt::DisplayRole:
201     // return a name
202     switch (theIndex.internalId()) {
203     case MyRoot:
204       {
205         FeaturePtr aFeature = featureObj(myDocument->feature(PARTS_GROUP, myId));
206         if (aFeature)
207           return aFeature->data()->getName().c_str();
208       }
209     case ParamsFolder:
210       return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex));
211     case ConstructFolder:
212       return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
213     case BodiesFolder:
214       return tr("Bodies") + QString(" (%1)").arg(rowCount(theIndex));
215     case ParamObject:
216       {
217         FeaturePtr aFeature = featureObj(featureDocument()->feature(PARAMETERS_GROUP, theIndex.row()));
218         if (aFeature)
219           return aFeature->data()->getName().c_str();
220       }
221     case ConstructObject:
222       {
223         FeaturePtr aFeature = featureObj(featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row()));
224         if (aFeature)
225           return aFeature->data()->getName().c_str();
226       }
227     case HistoryObject:
228       {
229         FeaturePtr aFeature = featureObj(featureDocument()->feature(FEATURES_GROUP, theIndex.row() - 3));
230         if (aFeature)
231           return aFeature->data()->getName().c_str();
232       }
233     }
234     break;
235   case Qt::DecorationRole:
236     // return an Icon
237     switch (theIndex.internalId()) {
238     case MyRoot:
239       return QIcon(":pictures/part_ico.png");
240     case ParamsFolder:
241       return QIcon(":pictures/params_folder.png");
242     case ConstructFolder:
243     case BodiesFolder:
244       return QIcon(":pictures/constr_folder.png");
245     case ConstructObject:
246       {
247         FeaturePtr aFeature = featureObj(featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row()));
248         if (aFeature)
249           return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
250       }
251     case HistoryObject:
252       {
253         FeaturePtr aFeature = featureDocument()->feature(FEATURES_GROUP, theIndex.row() - 3);
254         if (aFeature)
255           return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
256       }
257     }
258    break;
259   case Qt::ToolTipRole:
260     // return Tooltip
261     break;
262   case Qt::ForegroundRole:
263     return QBrush(myItemsColor);
264     break;
265   }
266   return QVariant();
267 }
268
269 QVariant XGUI_PartDataModel::headerData(int section, Qt::Orientation orientation, int role) const
270 {
271   return QVariant();
272 }
273
274 int XGUI_PartDataModel::rowCount(const QModelIndex& parent) const
275 {
276   if (!parent.isValid()) 
277     if (myDocument->feature(PARTS_GROUP, myId))
278       return 1;
279     else 
280       return 0;
281   switch (parent.internalId()) {
282   case MyRoot:
283     return 3 + featureDocument()->size(FEATURES_GROUP);
284   case ParamsFolder:
285     return featureDocument()->size(PARAMETERS_GROUP);
286   case ConstructFolder:
287     return featureDocument()->size(CONSTRUCTIONS_GROUP);
288   case BodiesFolder:
289     return 0;
290   }
291   return 0;
292 }
293
294 int XGUI_PartDataModel::columnCount(const QModelIndex &parent) const
295 {
296   return 1;
297 }
298
299 QModelIndex XGUI_PartDataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
300 {
301   if (!theParent.isValid())
302     return createIndex(theRow, 0, (qint32) MyRoot);
303
304   int aId = (int)theParent.internalId();
305   switch (aId) {
306   case MyRoot:
307     switch (theRow) {
308     case 0:
309       return createIndex(0, 0, (qint32) ParamsFolder);
310     case 1:
311       return createIndex(1, 0, (qint32) ConstructFolder);
312     case 2:
313       return createIndex(2, 0, (qint32) BodiesFolder);
314     default:
315       return createIndex(theRow, theColumn, (qint32) HistoryObject);
316     }
317   case ParamsFolder:
318     return createIndex(theRow, 0, (qint32) ParamObject);
319   case ConstructFolder:
320     return createIndex(theRow, 0, (qint32) ConstructObject);
321   case BodiesFolder:
322     return createIndex(theRow, 0, (qint32) BodieswObject);
323   }
324   return QModelIndex();
325 }
326
327 QModelIndex XGUI_PartDataModel::parent(const QModelIndex& theIndex) const
328 {
329   switch (theIndex.internalId()) {
330   case MyRoot:
331     return QModelIndex();
332   case ParamsFolder:
333   case ConstructFolder:
334   case BodiesFolder:
335   case HistoryObject:
336     return createIndex(0, 0, (qint32) MyRoot);
337   case ParamObject:
338     return createIndex(0, 0, (qint32) ParamsFolder);
339   case ConstructObject:
340     return createIndex(1, 0, (qint32) ConstructFolder);
341   }
342   return QModelIndex();
343 }
344
345 bool XGUI_PartDataModel::hasChildren(const QModelIndex& theParent) const
346 {
347   return rowCount(theParent) > 0;
348 }
349
350
351 DocumentPtr XGUI_PartDataModel::featureDocument() const
352 {
353   FeaturePtr aFeature = featureObj(myDocument->feature(PARTS_GROUP, myId));
354   return aFeature->data()->docRef("PartDocument")->value();
355 }
356  
357 FeaturePtr XGUI_PartDataModel::feature(const QModelIndex& theIndex) const
358 {
359   switch (theIndex.internalId()) {
360   case MyRoot:
361     if (theIndex.row() < 3) {
362       return featureObj(myDocument->feature(PARTS_GROUP, myId));
363     } else 
364       return featureDocument()->feature(FEATURES_GROUP, theIndex.row() - 3);
365   case ParamsFolder:
366   case ConstructFolder:
367     return FeaturePtr();
368   case ParamObject:
369     return featureObj(featureDocument()->feature(PARAMETERS_GROUP, theIndex.row()));
370   case ConstructObject:
371     return featureObj(featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row()));
372   }
373   return FeaturePtr();
374 }
375
376 bool XGUI_PartDataModel::hasDocument(const DocumentPtr& theDoc) const
377 {
378   return (featureDocument() == theDoc);
379 }
380
381
382 QModelIndex XGUI_PartDataModel::findParent(const FeaturePtr& theFeature) const
383 {
384   QString aGroup(theFeature->getGroup().c_str());
385
386   if (theFeature->getGroup().compare(PARAMETERS_GROUP) == 0)
387     return createIndex(0, 0, (qint32) ParamsFolder);
388   if (theFeature->getGroup().compare(CONSTRUCTIONS_GROUP) == 0)
389     return createIndex(1, 0, (qint32) ConstructFolder);
390   return QModelIndex();
391 }
392
393 QModelIndex XGUI_PartDataModel::findGroup(const std::string& theGroup) const
394 {
395   if (theGroup.compare(PARAMETERS_GROUP) == 0)
396     return createIndex(0, 0, (qint32) ParamsFolder);
397   if (theGroup.compare(CONSTRUCTIONS_GROUP) == 0)
398     return createIndex(1, 0, (qint32) ConstructFolder);
399   return QModelIndex();
400 }
401
402 FeaturePtr XGUI_PartDataModel::part() const
403 {
404   return featureObj(myDocument->feature(PARTS_GROUP, myId));
405 }