1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 #include "XGUI_PartDataModel.h"
4 #include "XGUI_Workshop.h"
6 #include <ModelAPI_Session.h>
7 #include <ModelAPI_Document.h>
8 #include <ModelAPI_Feature.h>
9 #include <ModelAPI_Result.h>
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_AttributeDocRef.h>
12 #include <ModelAPI_Object.h>
13 #include <ModelAPI_ResultPart.h>
14 #include <ModelAPI_ResultConstruction.h>
15 #include <ModelAPI_ResultParameter.h>
16 #include <ModelAPI_ResultBody.h>
17 #include <ModelAPI_ResultGroup.h>
18 #include <ModelAPI_AttributeDouble.h>
23 //ObjectPtr featureObj(const ObjectPtr& theFeature)
25 // ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
27 // return aObject->featureRef();
31 XGUI_TopDataModel::XGUI_TopDataModel(QObject* theParent)
32 : XGUI_FeaturesModel(theParent)
36 XGUI_TopDataModel::~XGUI_TopDataModel()
40 QVariant XGUI_TopDataModel::data(const QModelIndex& theIndex, int theRole) const
45 switch (theIndex.internalId()) {
47 return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex));
49 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
50 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultParameter::group(), theIndex.row());
52 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObject);
53 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
54 QString aVal = QString::number(aValueAttribute->value());
55 QString aTitle = QString(aObject->data()->name().c_str());
56 return aTitle + " = " + aVal;
61 return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
62 case ConstructObject: {
63 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
64 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultConstruction::group(),
67 return aObject->data()->name().c_str();
71 // return tr("Groups") + QString(" (%1)").arg(rowCount(theIndex));
73 // DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
74 // ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultGroup::group(),
77 // return aObject->data()->name().c_str();
83 case Qt::DecorationRole:
86 switch (theIndex.internalId()) {
88 return QIcon(":pictures/params_folder.png");
90 return QIcon(":pictures/constr_folder.png");
92 return QIcon(":pictures/constr_object.png");
94 // return QIcon(":pictures/constr_folder.png");
102 case Qt::ForegroundRole:
103 return QBrush(myItemsColor);
109 QVariant XGUI_TopDataModel::headerData(int section, Qt::Orientation orientation, int role) const
114 int XGUI_TopDataModel::rowCount(const QModelIndex& theParent) const
116 if (!theParent.isValid())
117 return 2; // In case of groups using it has to be +1
119 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
120 if (theParent.internalId() == ParamsFolder)
121 return aRootDoc->size(ModelAPI_ResultParameter::group());
123 if (theParent.internalId() == ConstructFolder)
124 return aRootDoc->size(ModelAPI_ResultConstruction::group());
126 //if (theParent.internalId() == GroupsFolder)
127 // return aRootDoc->size(ModelAPI_ResultGroup::group());
132 int XGUI_TopDataModel::columnCount(const QModelIndex &parent) const
137 QModelIndex XGUI_TopDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
139 if (!theParent.isValid()) {
142 return createIndex(theRow, theColumn, (qint32) ParamsFolder);
144 return createIndex(theRow, theColumn, (qint32) ConstructFolder);
146 // return createIndex(theRow, theColumn, (qint32) GroupsFolder);
149 if (theParent.internalId() == ParamsFolder)
150 return createIndex(theRow, theColumn, (qint32) ParamObject);
152 if (theParent.internalId() == ConstructFolder)
153 return createIndex(theRow, theColumn, (qint32) ConstructObject);
155 //if (theParent.internalId() == GroupsFolder)
156 // return createIndex(theRow, theColumn, (qint32) GroupObject);
158 return QModelIndex();
161 QModelIndex XGUI_TopDataModel::parent(const QModelIndex& theIndex) const
163 int aId = (int) theIndex.internalId();
166 case ConstructFolder:
168 return QModelIndex();
170 return createIndex(0, 0, (qint32) ParamsFolder);
171 case ConstructObject:
172 return createIndex(1, 0, (qint32) ConstructFolder);
174 // return createIndex(2, 0, (qint32) GroupsFolder);
176 return QModelIndex();
179 bool XGUI_TopDataModel::hasChildren(const QModelIndex& theParent) const
181 return rowCount(theParent) > 0;
184 ObjectPtr XGUI_TopDataModel::object(const QModelIndex& theIndex) const
186 switch (theIndex.internalId()) {
188 case ConstructFolder:
191 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
192 return aRootDoc->object(ModelAPI_ResultParameter::group(), theIndex.row());
194 case ConstructObject: {
195 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
196 return aRootDoc->object(ModelAPI_ResultConstruction::group(), theIndex.row());
198 //case GroupObject: {
199 // DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
200 // return aRootDoc->object(ModelAPI_ResultGroup::group(), theIndex.row());
206 QModelIndex XGUI_TopDataModel::findParent(const ObjectPtr& theObject) const
208 return findGroup(theObject->groupName().c_str());
211 QModelIndex XGUI_TopDataModel::findGroup(const std::string& theGroup) const
213 if (theGroup == ModelAPI_ResultParameter::group())
214 return createIndex(0, 0, (qint32) ParamsFolder);
215 if (theGroup == ModelAPI_ResultConstruction::group())
216 return createIndex(1, 0, (qint32) ConstructFolder);
217 //if (theGroup == ModelAPI_ResultGroup::group())
218 // return createIndex(2, 0, (qint32) ConstructFolder);
219 return QModelIndex();
222 QModelIndex XGUI_TopDataModel::objectIndex(const ObjectPtr& theObject) const
226 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
227 std::string aGroup = theObject->groupName();
228 int aNb = aRootDoc->size(aGroup);
230 for (int i = 0; i < aNb; i++) {
231 if (aRootDoc->object(aGroup, i) == theObject) {
237 if (aGroup == ModelAPI_ResultParameter::group())
238 return createIndex(aRow, 0, (qint32) ParamObject);
239 if (aGroup == ModelAPI_ResultConstruction::group())
240 return createIndex(aRow, 0, (qint32) ConstructObject);
241 //if (aGroup == ModelAPI_ResultGroup::group())
242 // return createIndex(aRow, 0, (qint32) GroupObject);
248 //******************************************************************
249 //******************************************************************
250 //******************************************************************
251 XGUI_PartDataModel::XGUI_PartDataModel(QObject* theParent)
252 : XGUI_PartModel(theParent)
256 XGUI_PartDataModel::~XGUI_PartDataModel()
260 QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) const
263 case Qt::DisplayRole:
265 switch (theIndex.internalId()) {
267 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
268 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), myId);
270 return std::dynamic_pointer_cast<ModelAPI_Object>(aObject)->data()->name().c_str();
273 return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex));
274 case ConstructFolder:
275 return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
277 return tr("Bodies") + QString(" (%1)").arg(rowCount(theIndex));
279 return tr("Groups") + QString(" (%1)").arg(rowCount(theIndex));
281 ObjectPtr aObject = partDocument()->object(ModelAPI_ResultParameter::group(),
284 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObject);
285 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
286 QString aVal = QString::number(aValueAttribute->value());
287 QString aTitle = QString(aObject->data()->name().c_str());
288 return aTitle + " = " + aVal;
292 case ConstructObject: {
293 ObjectPtr aObject = partDocument()->object(ModelAPI_ResultConstruction::group(),
296 return std::dynamic_pointer_cast<ModelAPI_Object>(aObject)->data()->name().c_str();
300 ObjectPtr aObject = partDocument()->object(ModelAPI_ResultBody::group(), theIndex.row());
302 return aObject->data()->name().c_str();
306 ObjectPtr aObject = partDocument()->object(ModelAPI_ResultGroup::group(), theIndex.row());
308 return aObject->data()->name().c_str();
310 case HistoryObject: {
311 ObjectPtr aObject = partDocument()->object(ModelAPI_Feature::group(), theIndex.row() - getRowsNumber());
313 return aObject->data()->name().c_str();
317 case Qt::DecorationRole:
319 switch (theIndex.internalId()) {
321 return QIcon(":pictures/part_ico.png");
323 return QIcon(":pictures/params_folder.png");
324 case ConstructFolder:
326 return QIcon(":pictures/constr_folder.png");
328 return QIcon(":pictures/constr_folder.png");
329 case ConstructObject:
332 std::string aGroup = theIndex.internalId() == ConstructObject ?
333 ModelAPI_ResultConstruction::group() : ModelAPI_ResultBody::group();
334 ObjectPtr anObject = partDocument()->object(aGroup, theIndex.row());
335 if (anObject && anObject->data() &&
336 anObject->data()->execState() == ModelAPI_StateMustBeUpdated) {
337 return QIcon(":pictures/constr_object_modified.png");
339 return QIcon(":pictures/constr_object.png");
341 case HistoryObject: {
342 ObjectPtr aObject = partDocument()->object(ModelAPI_Feature::group(), theIndex.row() - getRowsNumber());
343 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
345 return XGUI_Workshop::featureIcon(aFeature);
349 case Qt::ToolTipRole:
352 case Qt::ForegroundRole:
353 return QBrush(myItemsColor);
359 QVariant XGUI_PartDataModel::headerData(int section, Qt::Orientation orientation, int role) const
364 int XGUI_PartDataModel::rowCount(const QModelIndex& parent) const
366 if (!parent.isValid()) {
367 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
368 if (aRootDoc->object(ModelAPI_ResultPart::group(), myId))
373 switch (parent.internalId()) {
376 DocumentPtr aDoc = partDocument();
378 return getRowsNumber() + aDoc->size(ModelAPI_Feature::group());
383 return partDocument()->size(ModelAPI_ResultParameter::group());
384 case ConstructFolder:
385 return partDocument()->size(ModelAPI_ResultConstruction::group());
387 return partDocument()->size(ModelAPI_ResultBody::group());
389 return partDocument()->size(ModelAPI_ResultGroup::group());
394 int XGUI_PartDataModel::columnCount(const QModelIndex &parent) const
399 QModelIndex XGUI_PartDataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
401 if (!theParent.isValid())
402 return createIndex(theRow, 0, (qint32) MyRoot);
404 int aId = (int) theParent.internalId();
409 return createIndex(theRow, 0, (qint32) ParamsFolder);
411 return createIndex(theRow, 0, (qint32) ConstructFolder);
413 return createIndex(theRow, 0, (qint32) BodiesFolder);
416 int aSize = partDocument()->size(ModelAPI_ResultGroup::group());
418 return createIndex(theRow, 0, (qint32) GroupsFolder);
420 return createIndex(theRow, theColumn, (qint32) HistoryObject);
423 return createIndex(theRow, theColumn, (qint32) HistoryObject);
426 return createIndex(theRow, 0, (qint32) ParamObject);
427 case ConstructFolder:
428 return createIndex(theRow, 0, (qint32) ConstructObject);
430 return createIndex(theRow, 0, (qint32) BodiesObject);
432 return createIndex(theRow, 0, (qint32) GroupObject);
434 return QModelIndex();
437 QModelIndex XGUI_PartDataModel::parent(const QModelIndex& theIndex) const
439 switch (theIndex.internalId()) {
441 return QModelIndex();
443 case ConstructFolder:
447 return createIndex(0, 0, (qint32) MyRoot);
450 return createIndex(0, 0, (qint32) ParamsFolder);
451 case ConstructObject:
452 return createIndex(1, 0, (qint32) ConstructFolder);
454 return createIndex(2, 0, (qint32) BodiesFolder);
456 return createIndex(3, 0, (qint32) GroupsFolder);
458 return QModelIndex();
461 bool XGUI_PartDataModel::hasChildren(const QModelIndex& theParent) const
463 return rowCount(theParent) > 0;
466 DocumentPtr XGUI_PartDataModel::partDocument() const
468 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
469 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), myId);
470 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
472 return aPart->partDoc();
473 return DocumentPtr(); // null if not found
476 ObjectPtr XGUI_PartDataModel::object(const QModelIndex& theIndex) const
478 switch (theIndex.internalId()) {
480 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
481 return aRootDoc->object(ModelAPI_ResultPart::group(), myId);
484 case ConstructFolder:
490 return partDocument()->object(ModelAPI_ResultParameter::group(), theIndex.row());
491 case ConstructObject:
492 return partDocument()->object(ModelAPI_ResultConstruction::group(), theIndex.row());
494 return partDocument()->object(ModelAPI_ResultBody::group(), theIndex.row());
496 return partDocument()->object(ModelAPI_ResultGroup::group(), theIndex.row());
498 return partDocument()->object(ModelAPI_Feature::group(), theIndex.row() - getRowsNumber());
503 bool XGUI_PartDataModel::hasDocument(const DocumentPtr& theDoc) const
505 return (partDocument() == theDoc);
508 QModelIndex XGUI_PartDataModel::findParent(const ObjectPtr& theObject) const
510 return findGroup(theObject->groupName().c_str());
513 QModelIndex XGUI_PartDataModel::findGroup(const std::string& theGroup) const
515 if (theGroup == ModelAPI_ResultParameter::group())
516 return createIndex(0, 0, (qint32) ParamsFolder);
517 if (theGroup == ModelAPI_ResultConstruction::group())
518 return createIndex(1, 0, (qint32) ConstructFolder);
519 if (theGroup == ModelAPI_ResultBody::group())
520 return createIndex(2, 0, (qint32) BodiesFolder);
521 if (theGroup == ModelAPI_ResultGroup::group())
522 return createIndex(3, 0, (qint32) GroupsFolder);
523 return QModelIndex();
526 ResultPartPtr XGUI_PartDataModel::part() const
528 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
529 ObjectPtr aObj = aRootDoc->object(ModelAPI_ResultPart::group(), myId);
530 return std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
533 QModelIndex XGUI_PartDataModel::objectIndex(const ObjectPtr& theObject) const
537 if (part() == theObject)
540 std::string aGroup = theObject->groupName();
541 DocumentPtr aDoc = theObject->document();
542 int aNb = aDoc->size(aGroup);
544 for (int i = 0; i < aNb; i++) {
545 if (aDoc->object(aGroup, i) == theObject) {
552 if (aGroup == ModelAPI_ResultParameter::group())
553 return createIndex(aRow, 0, (qint32) ParamObject);
554 else if (aGroup == ModelAPI_ResultConstruction::group())
555 return createIndex(aRow, 0, (qint32) ConstructObject);
556 else if (aGroup == ModelAPI_ResultBody::group())
557 return createIndex(aRow, 0, (qint32) BodiesObject);
558 else if (aGroup == ModelAPI_ResultGroup::group())
559 return createIndex(aRow, 0, (qint32) GroupObject);
561 return createIndex(aRow + getRowsNumber(), 0, (qint32) HistoryObject);
567 int XGUI_PartDataModel::getRowsNumber() const
569 int aSize = partDocument()->size(ModelAPI_ResultGroup::group());
570 if (aSize == 0) // If there are no groups then do not show group folder