Salome HOME
Refactoring: static constants are replaced by inline functions in:
[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(QObject* theParent)
25   : XGUI_FeaturesModel(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         DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
45         FeaturePtr aFeature = aRootDoc->feature(ModelAPI_Document::PARAMETERS_GROUP(), theIndex.row());
46         if (aFeature)
47           return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->getName().c_str();
48       } 
49     case ConstructFolder:
50         return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
51     case ConstructObject:
52       {
53         DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
54         FeaturePtr aFeature = aRootDoc->feature(ModelAPI_Document::CONSTRUCTIONS_GROUP(), theIndex.row());
55         if (aFeature)
56           return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->getName().c_str();
57       }
58     }
59     break;
60
61   case Qt::DecorationRole:
62     // return an Icon
63     switch (theIndex.internalId()) {
64     case ParamsFolder:
65       return QIcon(":pictures/params_folder.png");
66     case ConstructFolder:
67       return QIcon(":pictures/constr_folder.png");
68     case ConstructObject:
69       {
70         DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
71         FeaturePtr aFeature = aRootDoc->feature(ModelAPI_Document::CONSTRUCTIONS_GROUP(), theIndex.row());
72         if (aFeature)
73           return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
74       }
75     }
76     break;
77
78   case Qt::ToolTipRole:
79     // return Tooltip
80     break;
81   case Qt::ForegroundRole:
82     return QBrush(myItemsColor);
83     break;
84   }
85   return QVariant();
86 }
87
88 QVariant XGUI_TopDataModel::headerData(int section, Qt::Orientation orientation, int role) const
89 {
90   return QVariant();
91 }
92
93 int XGUI_TopDataModel::rowCount(const QModelIndex& theParent) const
94 {
95   if (!theParent.isValid()) 
96     return 2;
97
98   DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
99   if (theParent.internalId() == ParamsFolder)
100     return aRootDoc->size(ModelAPI_Document::PARAMETERS_GROUP());
101
102   if (theParent.internalId() == ConstructFolder)
103     return aRootDoc->size(ModelAPI_Document::CONSTRUCTIONS_GROUP());
104
105   return 0;
106 }
107
108 int XGUI_TopDataModel::columnCount(const QModelIndex &parent) const
109 {
110   return 1;
111 }
112
113 QModelIndex XGUI_TopDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
114 {
115   if (!theParent.isValid()) {
116     switch (theRow) {
117     case 0:
118       return createIndex(theRow, theColumn, (qint32) ParamsFolder);
119     case 1:
120       return createIndex(theRow, theColumn, (qint32) ConstructFolder);
121     }
122   } else {
123     if (theParent.internalId() == ParamsFolder)
124       return createIndex(theRow, theColumn, (qint32) ParamObject);
125
126     if (theParent.internalId() == ConstructFolder)
127       return createIndex(theRow, theColumn, (qint32) ConstructObject);
128   }
129   return QModelIndex();
130 }
131
132 QModelIndex XGUI_TopDataModel::parent(const QModelIndex& theIndex) const
133 {
134   int aId = (int)theIndex.internalId();
135   switch (aId) {
136   case ParamsFolder:
137   case ConstructFolder:
138     return QModelIndex();
139   case ParamObject:
140     return createIndex(0, 0, (qint32) ParamsFolder);
141   case ConstructObject:
142     return createIndex(1, 0, (qint32) ConstructFolder);
143   }
144   return QModelIndex();
145 }
146
147 bool XGUI_TopDataModel::hasChildren(const QModelIndex& theParent) const
148 {
149   return rowCount(theParent) > 0;
150 }
151
152 FeaturePtr XGUI_TopDataModel::feature(const QModelIndex& theIndex) const
153 {
154   switch (theIndex.internalId()) {
155   case ParamsFolder:
156   case ConstructFolder:
157     return FeaturePtr();
158   case ParamObject:
159     {
160       DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
161       return aRootDoc->feature(ModelAPI_Document::PARAMETERS_GROUP(), theIndex.row());
162     }
163   case ConstructObject:
164     {
165       DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
166       return aRootDoc->feature(ModelAPI_Document::CONSTRUCTIONS_GROUP(), theIndex.row());
167     }
168   }
169   return FeaturePtr();
170 }
171
172
173 QModelIndex XGUI_TopDataModel::findParent(const FeaturePtr& theFeature) const
174 {
175   return findGroup(theFeature->getGroup().c_str());
176 }
177
178 QModelIndex XGUI_TopDataModel::findGroup(const std::string& theGroup) const
179 {
180   if (theGroup.compare(ModelAPI_Document::PARAMETERS_GROUP()) == 0)
181     return createIndex(0, 0, (qint32) ParamsFolder);
182   if (theGroup.compare(ModelAPI_Document::CONSTRUCTIONS_GROUP()) == 0)
183     return createIndex(1, 0, (qint32) ConstructFolder);
184   return QModelIndex();
185 }
186
187 QModelIndex XGUI_TopDataModel::featureIndex(const FeaturePtr& theFeature) const
188 {
189   QModelIndex aIndex;
190   if (theFeature) {
191     DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
192     std::string aGroup = theFeature->getGroup();
193     int aNb = aRootDoc->size(aGroup);
194     int aRow = -1;
195     for (int i = 0; i < aNb; i++) {
196       if (aRootDoc->feature(aGroup, i) == theFeature) {
197         aRow = i;
198         break;
199       }
200     }
201     if (aRow != -1) {
202       if (aGroup.compare(ModelAPI_Document::PARAMETERS_GROUP()) == 0)
203         return createIndex(aRow, 0, (qint32) ParamObject);
204       if (aGroup.compare(ModelAPI_Document::CONSTRUCTIONS_GROUP()) == 0)
205         return createIndex(aRow, 0, (qint32) ConstructObject);
206     }
207   }
208   return aIndex;
209 }
210
211
212
213 //******************************************************************
214 //******************************************************************
215 //******************************************************************
216 XGUI_PartDataModel::XGUI_PartDataModel(QObject* theParent)
217   : XGUI_PartModel(theParent)
218 {
219 }
220
221
222 XGUI_PartDataModel::~XGUI_PartDataModel()
223 {
224 }
225
226 QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) const
227 {
228   switch (theRole) {
229   case Qt::DisplayRole:
230     // return a name
231     switch (theIndex.internalId()) {
232     case MyRoot:
233       {
234         DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
235         FeaturePtr aFeature = aRootDoc->feature(ModelAPI_Document::PARTS_GROUP(), myId);
236         if (aFeature)
237           return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->getName().c_str();
238       }
239     case ParamsFolder:
240       return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex));
241     case ConstructFolder:
242       return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
243     case BodiesFolder:
244       return tr("Bodies") + QString(" (%1)").arg(rowCount(theIndex));
245     case ParamObject:
246       {
247         FeaturePtr aFeature = featureDocument()->feature(ModelAPI_Document::PARAMETERS_GROUP(), theIndex.row());
248         if (aFeature)
249           return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->getName().c_str();
250       }
251     case ConstructObject:
252       {
253         FeaturePtr aFeature = featureDocument()->feature(ModelAPI_Document::CONSTRUCTIONS_GROUP(), theIndex.row());
254         if (aFeature)
255           return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->getName().c_str();
256       }
257     case HistoryObject:
258       {
259         FeaturePtr aFeature = featureDocument()->feature(ModelAPI_Document::FEATURES_GROUP(), theIndex.row() - 3);
260         if (aFeature)
261           return aFeature->data()->getName().c_str();
262       }
263     }
264     break;
265   case Qt::DecorationRole:
266     // return an Icon
267     switch (theIndex.internalId()) {
268     case MyRoot:
269       return QIcon(":pictures/part_ico.png");
270     case ParamsFolder:
271       return QIcon(":pictures/params_folder.png");
272     case ConstructFolder:
273     case BodiesFolder:
274       return QIcon(":pictures/constr_folder.png");
275     case ConstructObject:
276       {
277         FeaturePtr aFeature = featureDocument()->feature(ModelAPI_Document::CONSTRUCTIONS_GROUP(), theIndex.row());
278         if (aFeature)
279           return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
280       }
281     case HistoryObject:
282       {
283         FeaturePtr aFeature = featureDocument()->feature(ModelAPI_Document::FEATURES_GROUP(), theIndex.row() - 3);
284         if (aFeature)
285           return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
286       }
287     }
288    break;
289   case Qt::ToolTipRole:
290     // return Tooltip
291     break;
292   case Qt::ForegroundRole:
293     return QBrush(myItemsColor);
294     break;
295   }
296   return QVariant();
297 }
298
299 QVariant XGUI_PartDataModel::headerData(int section, Qt::Orientation orientation, int role) const
300 {
301   return QVariant();
302 }
303
304 int XGUI_PartDataModel::rowCount(const QModelIndex& parent) const
305 {
306   if (!parent.isValid()) {
307     DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
308     if (aRootDoc->feature(ModelAPI_Document::PARTS_GROUP(), myId))
309       return 1;
310     else 
311       return 0;
312   }
313   switch (parent.internalId()) {
314   case MyRoot:
315     return 3 + featureDocument()->size(ModelAPI_Document::FEATURES_GROUP());
316   case ParamsFolder:
317     return featureDocument()->size(ModelAPI_Document::PARAMETERS_GROUP());
318   case ConstructFolder:
319     return featureDocument()->size(ModelAPI_Document::CONSTRUCTIONS_GROUP());
320   case BodiesFolder:
321     return 0;
322   }
323   return 0;
324 }
325
326 int XGUI_PartDataModel::columnCount(const QModelIndex &parent) const
327 {
328   return 1;
329 }
330
331 QModelIndex XGUI_PartDataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
332 {
333   if (!theParent.isValid())
334     return createIndex(theRow, 0, (qint32) MyRoot);
335
336   int aId = (int)theParent.internalId();
337   switch (aId) {
338   case MyRoot:
339     switch (theRow) {
340     case 0:
341       return createIndex(0, 0, (qint32) ParamsFolder);
342     case 1:
343       return createIndex(1, 0, (qint32) ConstructFolder);
344     case 2:
345       return createIndex(2, 0, (qint32) BodiesFolder);
346     default:
347       return createIndex(theRow, theColumn, (qint32) HistoryObject);
348     }
349   case ParamsFolder:
350     return createIndex(theRow, 0, (qint32) ParamObject);
351   case ConstructFolder:
352     return createIndex(theRow, 0, (qint32) ConstructObject);
353   case BodiesFolder:
354     return createIndex(theRow, 0, (qint32) BodiesObject);
355   }
356   return QModelIndex();
357 }
358
359 QModelIndex XGUI_PartDataModel::parent(const QModelIndex& theIndex) const
360 {
361   switch (theIndex.internalId()) {
362   case MyRoot:
363     return QModelIndex();
364   case ParamsFolder:
365   case ConstructFolder:
366   case BodiesFolder:
367   case HistoryObject:
368     return createIndex(0, 0, (qint32) MyRoot);
369   case ParamObject:
370     return createIndex(0, 0, (qint32) ParamsFolder);
371   case ConstructObject:
372     return createIndex(1, 0, (qint32) ConstructFolder);
373   }
374   return QModelIndex();
375 }
376
377 bool XGUI_PartDataModel::hasChildren(const QModelIndex& theParent) const
378 {
379   return rowCount(theParent) > 0;
380 }
381
382
383 DocumentPtr XGUI_PartDataModel::featureDocument() const
384 {
385   DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
386   FeaturePtr aFeature = aRootDoc->feature(ModelAPI_Document::PARTS_GROUP(), myId, true);
387   return aFeature->data()->docRef("PartDocument")->value();
388 }
389  
390 FeaturePtr XGUI_PartDataModel::feature(const QModelIndex& theIndex) const
391 {
392   switch (theIndex.internalId()) {
393   case MyRoot:
394     {
395       DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
396       return aRootDoc->feature(ModelAPI_Document::PARTS_GROUP(), myId);
397     }
398   case ParamsFolder:
399   case ConstructFolder:
400   case BodiesFolder:
401     return FeaturePtr();
402   case ParamObject:
403     return featureDocument()->feature(ModelAPI_Document::PARAMETERS_GROUP(), theIndex.row());
404   case ConstructObject:
405     return featureDocument()->feature(ModelAPI_Document::CONSTRUCTIONS_GROUP(), theIndex.row());
406   //case BodiesObject:
407   //  return featureDocument()->feature(ModelAPI_Document::CONSTRUCTIONS_GROUP(), theIndex.row());
408   case HistoryObject:
409     return featureDocument()->feature(ModelAPI_Document::FEATURES_GROUP(), theIndex.row() - 3);
410   }
411   return FeaturePtr();
412 }
413
414 bool XGUI_PartDataModel::hasDocument(const DocumentPtr& theDoc) const
415 {
416   return (featureDocument() == theDoc);
417 }
418
419
420 QModelIndex XGUI_PartDataModel::findParent(const FeaturePtr& theFeature) const
421 {
422   return findGroup(theFeature->getGroup().c_str());
423 }
424
425 QModelIndex XGUI_PartDataModel::findGroup(const std::string& theGroup) const
426 {
427   if (theGroup.compare(ModelAPI_Document::PARAMETERS_GROUP()) == 0)
428     return createIndex(0, 0, (qint32) ParamsFolder);
429   if (theGroup.compare(ModelAPI_Document::CONSTRUCTIONS_GROUP()) == 0)
430     return createIndex(1, 0, (qint32) ConstructFolder);
431   return QModelIndex();
432 }
433
434 FeaturePtr XGUI_PartDataModel::part() const
435 {
436   DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
437   return aRootDoc->feature(ModelAPI_Document::PARTS_GROUP(), myId, true);
438 }
439
440 QModelIndex XGUI_PartDataModel::featureIndex(const FeaturePtr& theFeature) const
441 {
442   QModelIndex aIndex;
443   if (theFeature) {
444     if (part() == theFeature) 
445       return aIndex;
446
447     //std::string aGroup = theFeature->getGroup();
448     DocumentPtr aDoc = theFeature->document();
449     int aNb = aDoc->size(ModelAPI_Document::FEATURES_GROUP());
450     int aRow = -1;
451     for (int i = 0; i < aNb; i++) {
452       if (aDoc->feature(ModelAPI_Document::FEATURES_GROUP(), i) == theFeature) {
453         aRow = i;
454         break;
455       }
456     }
457     if (aRow != -1) {
458       return createIndex(aRow + 3, 0, (qint32) HistoryObject);
459 /*      if (aGroup.compare(PARAMETERS_GROUP) == 0)
460         return createIndex(aRow, 0, (qint32) ParamObject);
461       if (aGroup.compare(CONSTRUCTIONS_GROUP) == 0)
462         return createIndex(aRow, 0, (qint32) ConstructObject);*/
463     }
464   }
465   return aIndex;
466 }