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 ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aRes);
41 // Use only original parts, not a placement results
42 if (aPartRes == aPartRes->original())
46 return ResultPartPtr();
49 /// Returns pointer on document if the given object is document object
50 ModelAPI_Document* getSubDocument(void* theObj)
52 ModelAPI_Document* aDoc = dynamic_cast<ModelAPI_Document*>((ModelAPI_Entity*)theObj);
59 // Constructor *************************************************
60 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : ModuleBase_IDocumentDataModel(theParent)
62 myXMLReader.readAll();
64 Events_Loop* aLoop = Events_Loop::loop();
65 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
66 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
67 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
70 //******************************************************
71 void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
73 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
74 std::string aRootType = myXMLReader.rootType();
75 std::string aSubType = myXMLReader.subType();
76 int aNbFolders = foldersCount();
78 // Created object event *******************
79 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
80 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
81 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
82 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
84 std::set<ObjectPtr>::const_iterator aIt;
86 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
87 ObjectPtr aObject = (*aIt);
88 // We do not show objects which not has to be shown in object browser
89 if (!aObject->isInHistory())
92 aObjType = aObject->groupName();
93 DocumentPtr aDoc = aObject->document();
94 if (aDoc == aRootDoc) {
95 // Check that new folders could appear
96 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
97 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
98 if ((aNotEmptyFolder.toStdString() == aObjType) && (aRootDoc->size(aObjType) == 1))
99 // Appears first object in folder which can not be shown empty
100 insertRow(myXMLReader.rootFolderId(aObjType));
103 int aRow = aRootDoc->size(aObjType) - 1;
105 if (aObjType == aRootType) {
106 insertRow(aRow + aNbFolders + 1);
108 int aFolderId = myXMLReader.rootFolderId(aObjType);
109 if (aFolderId != -1) {
110 insertRow(aRow, createIndex(aFolderId, 0, -1));
115 // Object created in sub-document
116 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
117 if (aDocRoot.isValid()) {
118 // Check that new folders could appear
119 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
120 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
121 if ((aNotEmptyFolder.toStdString() == aObjType) && (aDoc->size(aObjType) == 1))
122 // Appears first object in folder which can not be shown empty
123 insertRow(myXMLReader.subFolderId(aObjType), aDocRoot);
125 int aRow = aDoc->index(aObject);
127 int aNbSubFolders = foldersCount(aDoc.get());
128 if (aObjType == aSubType) {
129 // List of objects under document root
130 insertRow(aRow + aNbSubFolders, aDocRoot);
132 // List of objects under a folder
134 int aFolderId = folderId(aObjType, aDoc.get());
135 if (aFolderId != -1) {
136 QModelIndex aParentFolder = createIndex(aFolderId, 0, aDoc.get());
137 insertRow(aRow, aParentFolder);
138 emit dataChanged(aParentFolder, aParentFolder);
146 Events_Error::send("Problem with Data Model definition of sub-document");
150 // Deleted object event ***********************
151 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
152 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
153 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
154 DocumentPtr aDoc = aUpdMsg->document();
155 std::set<std::string> aGroups = aUpdMsg->groups();
156 std::set<std::string>::const_iterator aIt;
157 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
158 std::string aGroup = (*aIt);
159 if (aDoc == aRootDoc) { // If root objects
160 int aRow = aRootDoc->size(aGroup);
161 if (aGroup == aRootType) {
162 removeRow(aRow + aNbFolders);
164 int aFolderId = myXMLReader.rootFolderId(aGroup);
165 if (aFolderId != -1) {
166 QModelIndex aFolderIndex = createIndex(aFolderId, 0, -1);
167 removeRow(aRow, aFolderIndex);
170 // Check that some folders could erased
171 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
172 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
173 if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup) == 0))
174 // Appears first object in folder which can not be shown empty
175 removeRow(myXMLReader.rootFolderId(aGroup));
178 // Remove row for sub-document
179 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
180 if (aDocRoot.isValid()) {
181 int aRow = aDoc->size(aGroup);
182 int aNbSubFolders = foldersCount(aDoc.get());
183 if (aGroup == aSubType) {
184 // List of objects under document root
185 removeRow(aRow + aNbSubFolders, aDocRoot);
187 // List of objects under a folder
188 int aFolderId = folderId(aGroup, aDoc.get());
189 if (aFolderId != -1) {
190 removeRow(aRow, createIndex(aFolderId, 0, aDoc.get()));
193 // Check that some folders could disappear
194 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
195 int aSize = aDoc->size(aGroup);
196 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
197 if ((aNotEmptyFolder.toStdString() == aGroup) && (aSize == 0))
198 // Appears first object in folder which can not be shown empty
199 removeRow(myXMLReader.subFolderId(aGroup), aDocRoot);
204 Events_Error::send("Problem with Data Model definition of sub-document");
208 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
209 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
210 if (aDoc != aRootDoc) {
211 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
212 if (aDocRoot.isValid())
213 emit dataChanged(aDocRoot, aDocRoot);
216 Events_Error::send("Problem with Data Model definition of sub-document");
222 //******************************************************
223 void XGUI_DataModel::clear()
228 //******************************************************
229 void XGUI_DataModel::rebuildDataTree()
234 //******************************************************
235 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
237 if (theIndex.internalId() < 0) // this is a folder
239 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
240 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
243 return aObj->data()->owner();
246 //******************************************************
247 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
249 std::string aType = theObject->groupName();
250 DocumentPtr aDoc = theObject->document();
251 int aRow = aDoc->index(theObject);
253 // it could be a part of complex object
254 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
255 if (aFeature.get()) {
256 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
257 if (aCompFea.get()) {
258 for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
259 if (aCompFea->subFeature(i, true) == theObject) {
266 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
268 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
269 if (aCompRes.get()) {
270 for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
271 if (aCompRes->subResult(i, true) == theObject) {
280 return QModelIndex();
282 return createIndex(aRow, 0, theObject.get());
284 SessionPtr aSession = ModelAPI_Session::get();
285 DocumentPtr aRootDoc = aSession->moduleDocument();
286 if (aDoc == aRootDoc && myXMLReader.rootType() == aType) {
287 // The object from root document
288 aRow += foldersCount();
289 } else if (myXMLReader.subType() == aType) {
290 // The object from sub document
291 aRow += foldersCount(aDoc.get());
293 return createIndex(aRow, 0, theObject.get());
296 //******************************************************
297 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
299 SessionPtr aSession = ModelAPI_Session::get();
300 DocumentPtr aRootDoc = aSession->moduleDocument();
301 int aNbFolders = foldersCount();
302 int theIndexRow = theIndex.row();
304 if ((theRole == Qt::DecorationRole) && (theIndex == lastHistoryIndex()))
305 return QIcon(":pictures/arrow.png");
307 if (theIndex.column() == 1)
310 int aParentId = theIndex.internalId();
311 if (aParentId == -1) { // root folders
313 case Qt::DisplayRole:
314 return QString(myXMLReader.rootFolderName(theIndexRow).c_str()) +
315 QString(" (%1)").arg(rowCount(theIndex));
316 case Qt::DecorationRole:
317 return QIcon(myXMLReader.rootFolderIcon(theIndexRow).c_str());
318 case Qt::ForegroundRole:
319 if ((flags(theIndex) & Qt::ItemIsEditable) == 0)
320 return QBrush(Qt::lightGray);
323 } else { // an object or sub-document
324 if (theRole == Qt::ForegroundRole) {
325 if ((flags(theIndex) & Qt::ItemIsEditable) == 0)
326 return QBrush(Qt::lightGray);
330 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
331 if (aSubDoc) { // this is a folder of sub document
332 QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
333 int aRow = theIndexRow;
334 while (aMissedIdx.contains(aRow))
338 case Qt::DisplayRole:
339 return QString(myXMLReader.subFolderName(aRow).c_str()) +
340 QString(" (%1)").arg(rowCount(theIndex));
341 case Qt::DecorationRole:
342 return QIcon(myXMLReader.subFolderIcon(aRow).c_str());
345 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
347 case Qt::DisplayRole:
349 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
350 ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
351 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
352 QString aVal = QString::number(aValueAttribute->value());
353 QString aTitle = QString(aObj->data()->name().c_str());
354 return aTitle + " = " + aVal;
357 if (aObj->groupName() == myXMLReader.subType()) {
358 ResultPartPtr aPartRes = getPartResult(aObj);
359 if (aPartRes.get()) {
360 if (aPartRes->partDoc().get() == NULL)
361 aPrefix = "Not loaded ";
364 return aPrefix + aObj->data()->name().c_str();
366 case Qt::DecorationRole:
367 return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
374 //******************************************************
375 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
380 //******************************************************
381 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
383 SessionPtr aSession = ModelAPI_Session::get();
384 if (!aSession->hasModuleDocument())
386 DocumentPtr aRootDoc = aSession->moduleDocument();
388 if (!theParent.isValid()) {
389 // Return number of items in root
390 int aNbFolders = foldersCount();
392 std::string aType = myXMLReader.rootType();
394 aNbItems = aRootDoc->size(aType);
395 return aNbFolders + aNbItems;
398 int aId = theParent.internalId();
400 // this is a folder under root
401 int aParentPos = theParent.row();
402 std::string aType = myXMLReader.rootFolderType(aParentPos);
403 return aRootDoc->size(aType);
405 // It is an object which could have children
406 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
408 // a folder of sub-document
409 QIntList aMissedIdx = missedFolderIndexes(aDoc);
410 int aRow = theParent.row();
411 while (aMissedIdx.contains(aRow))
413 std::string aType = myXMLReader.subFolderType(aRow);
414 return aDoc->size(aType);
416 ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
417 // Check for Part feature
418 ResultPartPtr aPartRes = getPartResult(aObj);
419 if (aPartRes.get()) {
420 DocumentPtr aSubDoc = aPartRes->partDoc();
424 int aNbSubFolders = foldersCount(aSubDoc.get());
426 std::string aSubType = myXMLReader.subType();
427 if (!aSubType.empty())
428 aNbSubItems = aSubDoc->size(aSubType);
429 return aNbSubItems + aNbSubFolders;
431 // Check for composite object
432 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
434 return aCompFeature->numberOfSubs(true);
435 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
437 return aCompRes->numberOfSubs(true);
444 //******************************************************
445 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
450 //******************************************************
451 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
453 SessionPtr aSession = ModelAPI_Session::get();
454 DocumentPtr aRootDoc = aSession->moduleDocument();
455 int aNbFolders = foldersCount();
459 if (!theParent.isValid()) {
460 if (theRow < aNbFolders) // Return first level folder index
461 return createIndex(theRow, theColumn, -1);
462 else { // return object under root index
463 std::string aType = myXMLReader.rootType();
464 int aObjId = theRow - aNbFolders;
465 if (aObjId < aRootDoc->size(aType)) {
466 ObjectPtr aObj = aRootDoc->object(aType, aObjId);
467 aIndex = objectIndex(aObj);
471 int aId = theParent.internalId();
472 int aParentPos = theParent.row();
473 if (aId == -1) { // return object index inside of first level of folders
474 std::string aType = myXMLReader.rootFolderType(aParentPos);
475 if (theRow < aRootDoc->size(aType)) {
476 ObjectPtr aObj = aRootDoc->object(aType, theRow);
477 aIndex = objectIndex(aObj);
480 // It is an object which could have children
481 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
483 // It is a folder of sub-document
484 int aParentRow = aParentPos;
485 QIntList aMissedIdx = missedFolderIndexes(aDoc);
486 while (aMissedIdx.contains(aParentRow))
488 std::string aType = myXMLReader.subFolderType(aParentRow);
489 if (theRow < aDoc->size(aType)) {
490 ObjectPtr aObj = aDoc->object(aType, theRow);
491 aIndex = objectIndex(aObj);
494 ModelAPI_Object* aParentObj = (ModelAPI_Object*)theParent.internalPointer();
496 // Check for Part feature
497 ResultPartPtr aPartRes = getPartResult(aParentObj);
498 if (aPartRes.get()) {
499 DocumentPtr aSubDoc = aPartRes->partDoc();
500 int aNbSubFolders = foldersCount(aSubDoc.get());
501 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
502 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
504 // this is an object under sub document root
505 std::string aType = myXMLReader.subType();
506 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
507 aIndex = objectIndex(aObj);
510 // Check for composite object
511 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
513 aIndex = objectIndex(aCompFeature->subFeature(theRow));
515 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
517 aIndex = objectIndex(aCompRes->subResult(theRow));
524 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
528 //******************************************************
529 static QModelIndex MYLastDeleted;
530 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
532 // To avoid additional request about index which was already deleted
533 if (theIndex == MYLastDeleted)
534 return QModelIndex();
536 int aId = theIndex.internalId();
537 if (aId != -1) { // The object is not a root folder
538 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
540 // It is a folder of sub-document
541 return findDocumentRootIndex(aDoc);
543 ObjectPtr aObj = object(theIndex);
545 // To avoid additional request about index which was already deleted
546 // If deleted it causes a crash on delete object from Part
547 MYLastDeleted = theIndex;
548 return QModelIndex();
550 // Check is it object a sub-object of a complex object
551 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
552 if (aFeature.get()) {
553 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
554 if (aCompFea.get()) {
555 return objectIndex(aCompFea);
558 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
560 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
561 if (aCompRes.get()) {
562 return objectIndex(aCompRes);
565 // Use as ordinary object
566 std::string aType = aObj->groupName();
567 SessionPtr aSession = ModelAPI_Session::get();
568 DocumentPtr aRootDoc = aSession->moduleDocument();
569 DocumentPtr aSubDoc = aObj->document();
570 if (aSubDoc == aRootDoc) {
571 if (aType == myXMLReader.rootType())
572 return QModelIndex();
574 // return first level of folder index
575 int aFolderId = myXMLReader.rootFolderId(aType);
576 // Items in a one row must have the same parent
577 return createIndex(aFolderId, 0, -1);
580 if (aType == myXMLReader.subType())
581 return findDocumentRootIndex(aSubDoc.get());
583 // return first level of folder index
584 int aFolderId = myXMLReader.subFolderId(aType);
585 // Items in a one row must have the same parent
586 return createIndex(aFolderId, 0, aSubDoc.get());
590 return QModelIndex();
593 //******************************************************
594 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
596 return rowCount(theParent) > 0;
599 //******************************************************
600 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
602 beginInsertRows(theParent, theRow, theRow + theCount - 1);
608 //******************************************************
609 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
611 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
616 //******************************************************
617 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
619 qint64 aIt = theIndex.internalId();
620 ModelAPI_Object* aObj = 0;
621 ModelAPI_Document* aDoc = 0;
622 SessionPtr aSession = ModelAPI_Session::get();
623 DocumentPtr aActiveDoc = aSession->activeDocument();
625 Qt::ItemFlags aNullFlag;
626 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
627 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
631 // Folders under root
632 DocumentPtr aRootDoc = aSession->moduleDocument();
633 if (aRootDoc != aActiveDoc)
636 aDoc = getSubDocument(theIndex.internalPointer());
638 aObj = (ModelAPI_Object*) theIndex.internalPointer();
643 if (aObj->isDisabled())
644 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
646 bool isCompositeSub = false;
647 // An object which is sub-object of a composite object can not be accessible in column 1
648 if (theIndex.column() == 1) {
649 ObjectPtr aObjPtr = aObj->data()->owner();
650 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
651 if (aFeature.get()) {
652 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
654 isCompositeSub = true;
656 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
658 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
660 isCompositeSub = true;
665 return Qt::ItemIsSelectable;
667 if (aObj->document() != aActiveDoc) {
668 // The object could be a root of sub-tree
669 ResultPartPtr aPartRes = getPartResult(aObj);
670 if (aPartRes.get()) {
671 if (aPartRes->partDoc() == aActiveDoc)
677 // A folder under sub-document
678 if (aActiveDoc.get() != aDoc)
684 //******************************************************
685 QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
687 SessionPtr aSession = ModelAPI_Session::get();
688 DocumentPtr aRootDoc = aSession->moduleDocument();
689 if (myXMLReader.isAttachToResult()) { // If document is attached to result
690 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
692 ResultPartPtr aPartRes;
693 for (int i = 0; i < aNb; i++) {
694 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
695 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
696 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
698 if (myXMLReader.rootType() == ModelAPI_Feature::group()) {
699 aRow += foldersCount();
701 return createIndex(aRow, 0, aObj.get());
704 } else { // If document is attached to feature
705 int aNb = aRootDoc->size(ModelAPI_Feature::group());
707 ResultPartPtr aPartRes;
708 for (int i = 0; i < aNb; i++) {
709 aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
710 aPartRes = getPartResult(aObj.get());
711 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
713 if (myXMLReader.rootType() == ModelAPI_Feature::group())
714 aRow += foldersCount();
715 return createIndex(aRow, 0, aObj.get());
719 return QModelIndex();
722 //******************************************************
723 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
725 SessionPtr aSession = ModelAPI_Session::get();
726 DocumentPtr aRootDoc = aSession->moduleDocument();
727 if (theDoc == aRootDoc)
728 return QModelIndex();
730 return findDocumentRootIndex(theDoc.get());
733 //******************************************************
734 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
737 SessionPtr aSession = ModelAPI_Session::get();
738 DocumentPtr aRootDoc = aSession->moduleDocument();
739 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
740 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
741 if (myXMLReader.rootShowEmpty(i))
744 if (aRootDoc->size(myXMLReader.rootFolderType(i)) > 0)
749 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
750 if (myXMLReader.subShowEmpty(i))
753 if (theDoc->size(myXMLReader.subFolderType(i)) > 0)
762 //******************************************************
763 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
766 SessionPtr aSession = ModelAPI_Session::get();
767 DocumentPtr aRootDoc = aSession->moduleDocument();
768 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
769 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
770 if (!myXMLReader.rootShowEmpty(i)) {
771 if (aRootDoc->size(myXMLReader.rootFolderType(i)) == 0)
776 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
777 if (!myXMLReader.subShowEmpty(i)) {
778 if (theDoc->size(myXMLReader.subFolderType(i)) == 0)
787 //******************************************************
788 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
792 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
793 if (!myXMLReader.rootShowEmpty(i))
794 aResult << myXMLReader.rootFolderType(i).c_str();
797 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
798 if (!myXMLReader.subShowEmpty(i))
799 aResult << myXMLReader.subFolderType(i).c_str();
805 //******************************************************
806 QModelIndex XGUI_DataModel::lastHistoryIndex() const
808 SessionPtr aSession = ModelAPI_Session::get();
809 DocumentPtr aCurDoc = aSession->activeDocument();
810 FeaturePtr aFeature = aCurDoc->currentFeature(true);
811 if (aFeature.get()) {
812 QModelIndex aInd = objectIndex(aFeature);
813 return createIndex(aInd.row(), 1, aInd.internalPointer());
815 if (aCurDoc == aSession->moduleDocument())
816 return createIndex(foldersCount() - 1, 1, -1);
818 return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());
822 //******************************************************
823 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc)
825 SessionPtr aSession = ModelAPI_Session::get();
826 ModelAPI_Document* aDoc = theDoc;
828 aDoc = aSession->moduleDocument().get();
830 bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
834 int aId = myXMLReader.subFolderId(theType);
836 for (int i = 0; i < aId; i++) {
837 if (!myXMLReader.subShowEmpty(i)) {
838 if (aDoc->size(myXMLReader.subFolderType(i)) == 0)
843 int aId = myXMLReader.rootFolderId(theType);
845 for (int i = 0; i < aId; i++) {
846 if (!myXMLReader.rootShowEmpty(i)) {
847 if (aDoc->size(myXMLReader.rootFolderType(i)) == 0)