1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: ModuleBase_IDocumentDataModel.cpp
4 // Created: 28 Apr 2015
5 // Author: Vitaly SMETANNIKOV
7 #include "XGUI_DataModel.h"
9 #include <ModuleBase_IconFactory.h>
11 #include <ModelAPI_Session.h>
12 #include <ModelAPI_Events.h>
13 #include <ModelAPI_ResultParameter.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_ResultPart.h>
16 #include <ModelAPI_Feature.h>
17 #include <ModelAPI_CompositeFeature.h>
18 #include <ModelAPI_ResultCompSolid.h>
19 #include <ModelAPI_Tools.h>
21 #include <Config_FeatureMessage.h>
23 #include <Events_Loop.h>
24 #include <Events_Error.h>
29 #define ACTIVE_COLOR QColor(0,72,140)
30 #define PASSIVE_COLOR Qt::black
32 /// Returns ResultPart object if the given object is a Part feature
33 /// Otherwise returns NULL
34 ResultPartPtr getPartResult(ModelAPI_Object* theObj)
36 ModelAPI_Feature* aFeature = dynamic_cast<ModelAPI_Feature*>(theObj);
38 ResultPtr aRes = aFeature->firstResult();
39 if (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group())) {
40 return std::dynamic_pointer_cast<ModelAPI_ResultPart>(aRes);
43 return ResultPartPtr();
46 /// Returns pointer on document if the given object is document object
47 ModelAPI_Document* getSubDocument(void* theObj)
49 ModelAPI_Document* aDoc = dynamic_cast<ModelAPI_Document*>((ModelAPI_Entity*)theObj);
56 // Constructor *************************************************
57 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : ModuleBase_IDocumentDataModel(theParent)
59 myXMLReader.readAll();
61 Events_Loop* aLoop = Events_Loop::loop();
62 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
63 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
64 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
67 //******************************************************
68 void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
70 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
71 std::string aRootType = myXMLReader.rootType();
72 std::string aSubType = myXMLReader.subType();
73 int aNbFolders = foldersCount();
75 // Created object event *******************
76 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
77 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
78 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
79 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
81 std::set<ObjectPtr>::const_iterator aIt;
83 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
84 ObjectPtr aObject = (*aIt);
85 // We do not show objects which not has to be shown in object browser
86 if (!aObject->isInHistory())
89 aObjType = aObject->groupName();
90 DocumentPtr aDoc = aObject->document();
91 if (aDoc == aRootDoc) {
92 // Check that new folders could appear
93 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
94 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
95 if ((aNotEmptyFolder.toStdString() == aObjType) && (aRootDoc->size(aObjType) == 1))
96 // Appears first object in folder which can not be shown empty
97 insertRow(myXMLReader.rootFolderId(aObjType));
100 int aRow = aRootDoc->size(aObjType) - 1;
101 if (aObjType == aRootType) {
102 insertRow(aRow + aNbFolders + 1);
104 int aFolderId = myXMLReader.rootFolderId(aObjType);
105 if (aFolderId != -1) {
106 insertRow(aRow, createIndex(aFolderId, 0, -1));
110 // Object created in sub-document
111 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
112 if (aDocRoot.isValid()) {
113 // Check that new folders could appear
114 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
115 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
116 if ((aNotEmptyFolder.toStdString() == aObjType) && (aDoc->size(aObjType) == 1))
117 // Appears first object in folder which can not be shown empty
118 insertRow(myXMLReader.subFolderId(aObjType), aDocRoot);
120 int aRow = aDoc->size(aObjType) - 1;
121 int aNbSubFolders = foldersCount(aDoc.get());
122 if (aObjType == aSubType) {
123 // List of objects under document root
124 insertRow(aRow + aNbSubFolders, aDocRoot);
126 // List of objects under a folder
128 int aFolderId = myXMLReader.subFolderId(aObjType);
129 if (aFolderId != -1) {
130 insertRow(aRow, createIndex(aFolderId, 0, aDoc.get()));
137 Events_Error::send("Problem with Data Model definition of sub-document");
141 // Deleted object event ***********************
142 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
143 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
144 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
145 DocumentPtr aDoc = aUpdMsg->document();
146 std::set<std::string> aGroups = aUpdMsg->groups();
147 std::set<std::string>::const_iterator aIt;
148 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
149 std::string aGroup = (*aIt);
150 if (aDoc == aRootDoc) { // If root objects
151 int aRow = aRootDoc->size(aGroup);
152 if (aGroup == aRootType) {
153 removeRow(aRow + aNbFolders);
155 int aFolderId = myXMLReader.rootFolderId(aGroup);
156 if (aFolderId != -1) {
157 QModelIndex aFolderIndex = createIndex(aFolderId, 0, -1);
158 removeRow(aRow, aFolderIndex);
161 // Check that some folders could erased
162 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
163 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
164 if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup) == 0))
165 // Appears first object in folder which can not be shown empty
166 removeRow(myXMLReader.rootFolderId(aGroup));
169 // Remove row for sub-document
170 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
171 if (aDocRoot.isValid()) {
172 int aRow = aDoc->size(aGroup);
173 int aNbSubFolders = foldersCount(aDoc.get());
174 if (aGroup == aSubType) {
175 // List of objects under document root
176 removeRow(aRow + aNbSubFolders, aDocRoot);
178 // List of objects under a folder
179 int aFolderId = myXMLReader.subFolderId(aGroup);
180 if (aFolderId != -1) {
181 removeRow(aRow, createIndex(aFolderId, 0, aDoc.get()));
184 // Check that some folders could disappear
185 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
186 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
187 if ((aNotEmptyFolder.toStdString() == aGroup) && (aDoc->size(aGroup) == 1))
188 // Appears first object in folder which can not be shown empty
189 removeRow(myXMLReader.subFolderId(aGroup), aDocRoot);
194 Events_Error::send("Problem with Data Model definition of sub-document");
198 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
199 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
200 if (aDoc != aRootDoc) {
201 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
202 if (aDocRoot.isValid())
203 emit dataChanged(aDocRoot, aDocRoot);
206 Events_Error::send("Problem with Data Model definition of sub-document");
212 //******************************************************
213 void XGUI_DataModel::clear()
218 //******************************************************
219 void XGUI_DataModel::rebuildDataTree()
224 //******************************************************
225 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
227 if (theIndex.internalId() < 0) // this is a folder
229 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
230 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
233 return aObj->data()->owner();
236 //******************************************************
237 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
239 std::string aType = theObject->groupName();
240 DocumentPtr aDoc = theObject->document();
241 int aRow = aDoc->index(theObject);
243 // it could be a part of complex object
244 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
245 if (aFeature.get()) {
246 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
247 if (aCompFea.get()) {
248 for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
249 if (aCompFea->subFeature(i, true) == theObject) {
256 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
258 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
259 if (aCompRes.get()) {
260 for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
261 if (aCompRes->subResult(i, true) == theObject) {
270 return QModelIndex();
272 return createIndex(aRow, 0, theObject.get());
274 SessionPtr aSession = ModelAPI_Session::get();
275 DocumentPtr aRootDoc = aSession->moduleDocument();
276 if (aDoc == aRootDoc && myXMLReader.rootType() == aType) {
277 // The object from root document
278 aRow += foldersCount();
279 } else if (myXMLReader.subType() == aType) {
280 // The object from sub document
281 aRow += foldersCount(aDoc.get());
283 return createIndex(aRow, 0, theObject.get());
286 //******************************************************
287 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
289 SessionPtr aSession = ModelAPI_Session::get();
290 DocumentPtr aRootDoc = aSession->moduleDocument();
291 int aNbFolders = foldersCount();
292 int theIndexRow = theIndex.row();
294 if ((theRole == Qt::DecorationRole) && (theIndex == lastHistoryIndex()))
295 return QIcon(":pictures/arrow.png");
297 if (theIndex.column() == 1)
300 int aParentId = theIndex.internalId();
301 if (aParentId == -1) { // root folders
303 case Qt::DisplayRole:
304 return QString(myXMLReader.rootFolderName(theIndexRow).c_str()) +
305 QString(" (%1)").arg(rowCount(theIndex));
306 case Qt::DecorationRole:
307 return QIcon(myXMLReader.rootFolderIcon(theIndexRow).c_str());
308 case Qt::ForegroundRole:
309 if (aSession->activeDocument() == aRootDoc)
310 return QBrush(ACTIVE_COLOR);
312 return QBrush(PASSIVE_COLOR);
314 } else { // an object or sub-document
315 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
317 if (theRole == Qt::ForegroundRole) {
318 bool aIsActive = false;
320 aIsActive = (aSession->activeDocument().get() == aSubDoc);
322 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
323 if (aObj->isDisabled())
324 return QBrush(Qt::lightGray);
325 aIsActive = (aSession->activeDocument() == aObj->document());
328 return QBrush(ACTIVE_COLOR);
330 return QBrush(PASSIVE_COLOR);
333 if (aSubDoc) { // this is a folder of sub document
335 case Qt::DisplayRole:
336 return QString(myXMLReader.subFolderName(theIndexRow).c_str()) +
337 QString(" (%1)").arg(rowCount(theIndex));
338 case Qt::DecorationRole:
339 return QIcon(myXMLReader.subFolderIcon(theIndexRow).c_str());
342 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
344 case Qt::DisplayRole:
345 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
346 ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
347 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
348 QString aVal = QString::number(aValueAttribute->value());
349 QString aTitle = QString(aObj->data()->name().c_str());
350 return aTitle + " = " + aVal;
352 return aObj->data()->name().c_str();
353 case Qt::DecorationRole:
354 return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
361 //******************************************************
362 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
367 //******************************************************
368 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
370 SessionPtr aSession = ModelAPI_Session::get();
371 if (!aSession->hasModuleDocument())
373 DocumentPtr aRootDoc = aSession->moduleDocument();
375 if (!theParent.isValid()) {
376 // Return number of items in root
377 int aNbFolders = foldersCount();
379 std::string aType = myXMLReader.rootType();
381 aNbItems = aRootDoc->size(aType);
382 return aNbFolders + aNbItems;
385 int aId = theParent.internalId();
387 // this is a folder under root
388 int aParentPos = theParent.row();
389 std::string aType = myXMLReader.rootFolderType(aParentPos);
390 //qDebug("### %s = %i\n", aType.c_str(), aRootDoc->size(aType));
391 return aRootDoc->size(aType);
393 // It is an object which could have children
394 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
396 // a folder of sub-document
397 std::string aType = myXMLReader.subFolderType(theParent.row());
398 return aDoc->size(aType);
400 ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
401 // Check for Part feature
402 ResultPartPtr aPartRes = getPartResult(aObj);
403 if (aPartRes.get()) {
404 DocumentPtr aSubDoc = aPartRes->partDoc();
405 int aNbSubFolders = foldersCount(aSubDoc.get());
407 std::string aSubType = myXMLReader.subType();
408 if (!aSubType.empty())
409 aNbSubItems = aSubDoc->size(aSubType);
410 return aNbSubItems + aNbSubFolders;
412 // Check for composite object
413 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
415 return aCompFeature->numberOfSubs(true);
416 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
418 return aCompRes->numberOfSubs(true);
425 //******************************************************
426 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
431 //******************************************************
432 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
434 SessionPtr aSession = ModelAPI_Session::get();
435 DocumentPtr aRootDoc = aSession->moduleDocument();
436 int aNbFolders = foldersCount();
440 if (!theParent.isValid()) {
441 if (theRow < aNbFolders) // Return first level folder index
442 return createIndex(theRow, theColumn, -1);
443 else { // return object under root index
444 std::string aType = myXMLReader.rootType();
445 int aObjId = theRow - aNbFolders;
446 if (aObjId < aRootDoc->size(aType)) {
447 ObjectPtr aObj = aRootDoc->object(aType, aObjId);
448 aIndex = objectIndex(aObj);
452 int aId = theParent.internalId();
453 int aParentPos = theParent.row();
454 if (aId == -1) { // return object index inside of first level of folders
455 std::string aType = myXMLReader.rootFolderType(aParentPos);
456 if (theRow < aRootDoc->size(aType)) {
457 ObjectPtr aObj = aRootDoc->object(aType, theRow);
458 aIndex = objectIndex(aObj);
461 // It is an object which could have children
462 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
464 // It is a folder of sub-document
465 std::string aType = myXMLReader.subFolderType(aParentPos);
466 if (theRow < aDoc->size(aType)) {
467 ObjectPtr aObj = aDoc->object(aType, theRow);
468 aIndex = objectIndex(aObj);
471 ModelAPI_Object* aParentObj = (ModelAPI_Object*)theParent.internalPointer();
473 // Check for Part feature
474 ResultPartPtr aPartRes = getPartResult(aParentObj);
475 if (aPartRes.get()) {
476 DocumentPtr aSubDoc = aPartRes->partDoc();
477 int aNbSubFolders = foldersCount(aSubDoc.get());
478 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
479 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
481 // this is an object under sub document root
482 std::string aType = myXMLReader.subType();
483 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
484 aIndex = objectIndex(aObj);
487 // Check for composite object
488 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
490 aIndex = objectIndex(aCompFeature->subFeature(theRow));
492 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
494 aIndex = objectIndex(aCompRes->subResult(theRow));
501 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
505 //******************************************************
506 static QModelIndex MYLastDeleted;
507 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
509 // To avoid additional request about index which was already deleted
510 if (theIndex == MYLastDeleted)
511 return QModelIndex();
513 int aId = theIndex.internalId();
514 if (aId != -1) { // The object is not a root folder
515 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
517 // It is a folder of sub-document
518 return findDocumentRootIndex(aDoc);
520 ObjectPtr aObj = object(theIndex);
522 // To avoid additional request about index which was already deleted
523 // If deleted it causes a crash on delete object from Part
524 MYLastDeleted = theIndex;
525 return QModelIndex();
527 // Check is it object a sub-object of a complex object
528 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
529 if (aFeature.get()) {
530 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
531 if (aCompFea.get()) {
532 return objectIndex(aCompFea);
535 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
537 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
538 if (aCompRes.get()) {
539 return objectIndex(aCompRes);
542 // Use as ordinary object
543 std::string aType = aObj->groupName();
544 SessionPtr aSession = ModelAPI_Session::get();
545 DocumentPtr aRootDoc = aSession->moduleDocument();
546 DocumentPtr aSubDoc = aObj->document();
547 if (aSubDoc == aRootDoc) {
548 if (aType == myXMLReader.rootType())
549 return QModelIndex();
551 // return first level of folder index
552 int aFolderId = myXMLReader.rootFolderId(aType);
553 // Items in a one row must have the same parent
554 return createIndex(aFolderId, 0, -1);
557 if (aType == myXMLReader.subType())
558 return findDocumentRootIndex(aSubDoc.get());
560 // return first level of folder index
561 int aFolderId = myXMLReader.subFolderId(aType);
562 // Items in a one row must have the same parent
563 return createIndex(aFolderId, 0, aSubDoc.get());
567 return QModelIndex();
570 //******************************************************
571 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
573 if (!theParent.isValid()) {
574 int aNbFolders = foldersCount();
577 SessionPtr aSession = ModelAPI_Session::get();
578 DocumentPtr aRootDoc = aSession->moduleDocument();
579 return aRootDoc->size(myXMLReader.rootType()) > 0;
581 if (theParent.internalId() == -1) {
582 std::string aType = myXMLReader.rootFolderType(theParent.row());
583 if (!aType.empty()) {
584 SessionPtr aSession = ModelAPI_Session::get();
585 DocumentPtr aRootDoc = aSession->moduleDocument();
586 return aRootDoc->size(aType) > 0;
589 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
591 // a folder of sub-document
592 std::string aType = myXMLReader.subFolderType(theParent.row());
593 return aDoc->size(aType) > 0;
595 // Check that it could be an object with children
596 ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
598 // Check for Part feature
599 ResultPartPtr aPartRes = getPartResult(aObj);
601 return aPartRes->partDoc().get() != NULL;
603 // Check for composite object
604 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
606 return aCompFeature->numberOfSubs(true) > 0;
607 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
609 return aCompRes->numberOfSubs(true) > 0;
616 //******************************************************
617 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
619 beginInsertRows(theParent, theRow, theRow + theCount - 1);
625 //******************************************************
626 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
628 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
633 //******************************************************
634 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
636 Qt::ItemFlags aFlags = Qt::ItemIsSelectable;
638 ModelAPI_Object* aObj = 0;
639 if (theIndex.internalId() != -1) {
640 if (!getSubDocument(theIndex.internalPointer()))
641 aObj = (ModelAPI_Object*) theIndex.internalPointer();
644 bool isCompositeSub = false;
645 if (theIndex.column() == 1) {
646 ObjectPtr aObjPtr = aObj->data()->owner();
647 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
648 if (aFeature.get()) {
649 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
651 isCompositeSub = true;
653 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
655 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
657 isCompositeSub = true;
661 // An object which is sub-object of a composite object can not be accessible in column 1
663 return Qt::ItemFlags();
665 aFlags |= Qt::ItemIsEditable;
666 if (!aObj->isDisabled())
667 aFlags |= Qt::ItemIsEnabled;
669 aFlags |= Qt::ItemIsEnabled;
673 //******************************************************
674 QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
676 SessionPtr aSession = ModelAPI_Session::get();
677 DocumentPtr aRootDoc = aSession->moduleDocument();
678 if (myXMLReader.isAttachToResult()) { // If document is attached to result
679 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
681 ResultPartPtr aPartRes;
682 for (int i = 0; i < aNb; i++) {
683 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
684 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
685 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
687 if (myXMLReader.rootType() == ModelAPI_Feature::group())
688 aRow += foldersCount();
689 return createIndex(aRow, 0, aObj.get());
692 } else { // If document is attached to feature
693 int aNb = aRootDoc->size(ModelAPI_Feature::group());
695 ResultPartPtr aPartRes;
696 for (int i = 0; i < aNb; i++) {
697 aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
698 aPartRes = getPartResult(aObj.get());
699 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
701 if (myXMLReader.rootType() == ModelAPI_Feature::group())
702 aRow += foldersCount();
703 return createIndex(aRow, 0, aObj.get());
707 return QModelIndex();
710 //******************************************************
711 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
713 SessionPtr aSession = ModelAPI_Session::get();
714 DocumentPtr aRootDoc = aSession->moduleDocument();
715 if (theDoc == aRootDoc)
716 return QModelIndex();
718 return findDocumentRootIndex(theDoc.get());
721 //******************************************************
722 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
725 SessionPtr aSession = ModelAPI_Session::get();
726 DocumentPtr aRootDoc = aSession->moduleDocument();
727 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
728 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
729 if (myXMLReader.rootShowEmpty(i))
732 if (aRootDoc->size(myXMLReader.rootFolderType(i)) > 0)
737 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
738 if (myXMLReader.subShowEmpty(i))
741 if (theDoc->size(myXMLReader.subFolderType(i)) > 0)
749 //******************************************************
750 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
754 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
755 if (!myXMLReader.rootShowEmpty(i))
756 aResult << myXMLReader.rootFolderType(i).c_str();
759 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
760 if (!myXMLReader.subShowEmpty(i))
761 aResult << myXMLReader.subFolderType(i).c_str();
767 //******************************************************
768 QModelIndex XGUI_DataModel::lastHistoryIndex() const
770 SessionPtr aSession = ModelAPI_Session::get();
771 DocumentPtr aCurDoc = aSession->activeDocument();
772 FeaturePtr aFeature = aCurDoc->currentFeature(true);
773 if (aFeature.get()) {
774 QModelIndex aInd = objectIndex(aFeature);
775 return createIndex(aInd.row(), 1, aInd.internalPointer());
777 if (aCurDoc == aSession->moduleDocument())
778 return createIndex(foldersCount() - 1, 1, -1);
780 return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());