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