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_ORDER_UPDATED));
68 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
71 //******************************************************
72 void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
74 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
75 std::string aRootType = myXMLReader.rootType();
76 std::string aSubType = myXMLReader.subType();
77 int aNbFolders = foldersCount();
79 // Created object event *******************
80 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
81 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
82 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
83 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
85 std::set<ObjectPtr>::const_iterator aIt;
87 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
88 ObjectPtr aObject = (*aIt);
89 // We do not show objects which not has to be shown in object browser
90 if (!aObject->isInHistory())
93 aObjType = aObject->groupName();
94 DocumentPtr aDoc = aObject->document();
95 if (aDoc == aRootDoc) {
96 // Check that new folders could appear
97 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
98 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
99 if ((aNotEmptyFolder.toStdString() == aObjType) && (aRootDoc->size(aObjType) == 1))
100 // Appears first object in folder which can not be shown empty
101 insertRow(myXMLReader.rootFolderId(aObjType));
104 int aRow = aRootDoc->size(aObjType) - 1;
106 if (aObjType == aRootType) {
107 insertRow(aRow + aNbFolders + 1);
109 int aFolderId = myXMLReader.rootFolderId(aObjType);
110 if (aFolderId != -1) {
111 insertRow(aRow, createIndex(aFolderId, 0, -1));
116 // Object created in sub-document
117 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
118 if (aDocRoot.isValid()) {
119 // Check that new folders could appear
120 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
121 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
122 if ((aNotEmptyFolder.toStdString() == aObjType) && (aDoc->size(aObjType) == 1))
123 // Appears first object in folder which can not be shown empty
124 insertRow(myXMLReader.subFolderId(aObjType), aDocRoot);
126 int aRow = aDoc->index(aObject);
128 int aNbSubFolders = foldersCount(aDoc.get());
129 if (aObjType == aSubType) {
130 // List of objects under document root
131 insertRow(aRow + aNbSubFolders, aDocRoot);
133 // List of objects under a folder
135 int aFolderId = folderId(aObjType, aDoc.get());
136 if (aFolderId != -1) {
137 QModelIndex aParentFolder = createIndex(aFolderId, 0, aDoc.get());
138 insertRow(aRow, aParentFolder);
139 emit dataChanged(aParentFolder, aParentFolder);
147 Events_Error::send("Problem with Data Model definition of sub-document");
151 // Deleted object event ***********************
152 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
153 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
154 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
155 DocumentPtr aDoc = aUpdMsg->document();
156 std::set<std::string> aGroups = aUpdMsg->groups();
157 std::set<std::string>::const_iterator aIt;
158 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
159 std::string aGroup = (*aIt);
160 if (aDoc == aRootDoc) { // If root objects
161 int aRow = aRootDoc->size(aGroup);
162 if (aGroup == aRootType) {
163 removeRow(aRow + aNbFolders);
165 int aFolderId = myXMLReader.rootFolderId(aGroup);
166 if (aFolderId != -1) {
167 QModelIndex aFolderIndex = createIndex(aFolderId, 0, -1);
168 removeRow(aRow, aFolderIndex);
171 // Check that some folders could erased
172 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
173 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
174 if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup) == 0))
175 // Appears first object in folder which can not be shown empty
176 removeRow(myXMLReader.rootFolderId(aGroup));
179 // Remove row for sub-document
180 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
181 if (aDocRoot.isValid()) {
182 int aRow = aDoc->size(aGroup);
183 int aNbSubFolders = foldersCount(aDoc.get());
184 if (aGroup == aSubType) {
185 // List of objects under document root
186 removeRow(aRow + aNbSubFolders, aDocRoot);
188 // List of objects under a folder
189 int aFolderId = folderId(aGroup, aDoc.get());
190 if (aFolderId != -1) {
191 removeRow(aRow, createIndex(aFolderId, 0, aDoc.get()));
194 // Check that some folders could disappear
195 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
196 int aSize = aDoc->size(aGroup);
197 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
198 if ((aNotEmptyFolder.toStdString() == aGroup) && (aSize == 0))
199 // Appears first object in folder which can not be shown empty
200 removeRow(myXMLReader.subFolderId(aGroup), aDocRoot);
205 Events_Error::send("Problem with Data Model definition of sub-document");
209 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
210 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
211 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
212 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
214 std::set<ObjectPtr>::const_iterator aIt;
215 std::string aObjType;
216 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
217 ObjectPtr aObject = (*aIt);
218 // We do not show objects which not has to be shown in object browser
219 if (!aObject->isInHistory())
221 QModelIndex aIndex = objectIndex(aObject);
222 QModelIndex aParent = parent(aIndex);
224 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
225 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
226 if (aDoc != aRootDoc) {
227 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
228 if (aDocRoot.isValid())
229 emit dataChanged(aDocRoot, aDocRoot);
232 Events_Error::send("Problem with Data Model definition of sub-document");
238 //******************************************************
239 void XGUI_DataModel::clear()
244 //******************************************************
245 void XGUI_DataModel::rebuildDataTree()
250 //******************************************************
251 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
253 if (theIndex.internalId() < 0) // this is a folder
255 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
256 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
259 return aObj->data()->owner();
262 //******************************************************
263 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
265 std::string aType = theObject->groupName();
266 DocumentPtr aDoc = theObject->document();
267 int aRow = aDoc->index(theObject);
269 // it could be a part of complex object
270 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
271 if (aFeature.get()) {
272 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
273 if (aCompFea.get()) {
274 for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
275 if (aCompFea->subFeature(i, true) == theObject) {
282 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
284 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
285 if (aCompRes.get()) {
286 for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
287 if (aCompRes->subResult(i, true) == theObject) {
296 return QModelIndex();
298 return createIndex(aRow, 0, theObject.get());
300 SessionPtr aSession = ModelAPI_Session::get();
301 DocumentPtr aRootDoc = aSession->moduleDocument();
302 if (aDoc == aRootDoc && myXMLReader.rootType() == aType) {
303 // The object from root document
304 aRow += foldersCount();
305 } else if (myXMLReader.subType() == aType) {
306 // The object from sub document
307 aRow += foldersCount(aDoc.get());
309 return createIndex(aRow, 0, theObject.get());
312 //******************************************************
313 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
315 SessionPtr aSession = ModelAPI_Session::get();
316 DocumentPtr aRootDoc = aSession->moduleDocument();
317 int aNbFolders = foldersCount();
318 int theIndexRow = theIndex.row();
320 if ((theRole == Qt::DecorationRole) && (theIndex == lastHistoryIndex()))
321 return QIcon(":pictures/arrow.png");
323 if (theIndex.column() == 1)
326 int aParentId = theIndex.internalId();
327 if (aParentId == -1) { // root folders
329 case Qt::DisplayRole:
330 return QString(myXMLReader.rootFolderName(theIndexRow).c_str()) +
331 QString(" (%1)").arg(rowCount(theIndex));
332 case Qt::DecorationRole:
333 return QIcon(myXMLReader.rootFolderIcon(theIndexRow).c_str());
334 case Qt::ForegroundRole:
335 if ((flags(theIndex) & Qt::ItemIsEditable) == 0)
336 return QBrush(Qt::lightGray);
339 } else { // an object or sub-document
340 if (theRole == Qt::ForegroundRole) {
341 if ((flags(theIndex) & Qt::ItemIsEditable) == 0)
342 return QBrush(Qt::lightGray);
346 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
347 if (aSubDoc) { // this is a folder of sub document
348 QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
349 int aRow = theIndexRow;
350 while (aMissedIdx.contains(aRow))
354 case Qt::DisplayRole:
355 return QString(myXMLReader.subFolderName(aRow).c_str()) +
356 QString(" (%1)").arg(rowCount(theIndex));
357 case Qt::DecorationRole:
358 return QIcon(myXMLReader.subFolderIcon(aRow).c_str());
361 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
363 case Qt::DisplayRole:
365 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
366 ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
367 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
368 QString aVal = QString::number(aValueAttribute->value());
369 QString aTitle = QString(aObj->data()->name().c_str());
370 return aTitle + " = " + aVal;
373 if (aObj->groupName() == myXMLReader.subType()) {
374 ResultPartPtr aPartRes = getPartResult(aObj);
375 if (aPartRes.get()) {
376 if (aPartRes->partDoc().get() == NULL)
377 aPrefix = "Not loaded ";
380 return aPrefix + aObj->data()->name().c_str();
382 case Qt::DecorationRole:
383 return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
390 //******************************************************
391 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
396 //******************************************************
397 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
399 SessionPtr aSession = ModelAPI_Session::get();
400 if (!aSession->hasModuleDocument())
402 DocumentPtr aRootDoc = aSession->moduleDocument();
404 if (!theParent.isValid()) {
405 // Return number of items in root
406 int aNbFolders = foldersCount();
408 std::string aType = myXMLReader.rootType();
410 aNbItems = aRootDoc->size(aType);
411 return aNbFolders + aNbItems;
414 int aId = theParent.internalId();
416 // this is a folder under root
417 int aParentPos = theParent.row();
418 std::string aType = myXMLReader.rootFolderType(aParentPos);
419 return aRootDoc->size(aType);
421 // It is an object which could have children
422 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
424 // a folder of sub-document
425 QIntList aMissedIdx = missedFolderIndexes(aDoc);
426 int aRow = theParent.row();
427 while (aMissedIdx.contains(aRow))
429 std::string aType = myXMLReader.subFolderType(aRow);
430 return aDoc->size(aType);
432 ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
433 // Check for Part feature
434 ResultPartPtr aPartRes = getPartResult(aObj);
435 if (aPartRes.get()) {
436 DocumentPtr aSubDoc = aPartRes->partDoc();
440 int aNbSubFolders = foldersCount(aSubDoc.get());
442 std::string aSubType = myXMLReader.subType();
443 if (!aSubType.empty())
444 aNbSubItems = aSubDoc->size(aSubType);
445 return aNbSubItems + aNbSubFolders;
447 // Check for composite object
448 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
450 return aCompFeature->numberOfSubs(true);
451 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
453 return aCompRes->numberOfSubs(true);
460 //******************************************************
461 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
466 //******************************************************
467 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
469 SessionPtr aSession = ModelAPI_Session::get();
470 DocumentPtr aRootDoc = aSession->moduleDocument();
471 int aNbFolders = foldersCount();
475 if (!theParent.isValid()) {
476 if (theRow < aNbFolders) // Return first level folder index
477 return createIndex(theRow, theColumn, -1);
478 else { // return object under root index
479 std::string aType = myXMLReader.rootType();
480 int aObjId = theRow - aNbFolders;
481 if (aObjId < aRootDoc->size(aType)) {
482 ObjectPtr aObj = aRootDoc->object(aType, aObjId);
483 aIndex = objectIndex(aObj);
487 int aId = theParent.internalId();
488 int aParentPos = theParent.row();
489 if (aId == -1) { // return object index inside of first level of folders
490 std::string aType = myXMLReader.rootFolderType(aParentPos);
491 if (theRow < aRootDoc->size(aType)) {
492 ObjectPtr aObj = aRootDoc->object(aType, theRow);
493 aIndex = objectIndex(aObj);
496 // It is an object which could have children
497 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
499 // It is a folder of sub-document
500 int aParentRow = aParentPos;
501 QIntList aMissedIdx = missedFolderIndexes(aDoc);
502 while (aMissedIdx.contains(aParentRow))
504 std::string aType = myXMLReader.subFolderType(aParentRow);
505 if (theRow < aDoc->size(aType)) {
506 ObjectPtr aObj = aDoc->object(aType, theRow);
507 aIndex = objectIndex(aObj);
510 ModelAPI_Object* aParentObj = (ModelAPI_Object*)theParent.internalPointer();
512 // Check for Part feature
513 ResultPartPtr aPartRes = getPartResult(aParentObj);
514 if (aPartRes.get()) {
515 DocumentPtr aSubDoc = aPartRes->partDoc();
516 int aNbSubFolders = foldersCount(aSubDoc.get());
517 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
518 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
520 // this is an object under sub document root
521 std::string aType = myXMLReader.subType();
522 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
523 aIndex = objectIndex(aObj);
526 // Check for composite object
527 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
529 aIndex = objectIndex(aCompFeature->subFeature(theRow));
531 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
533 aIndex = objectIndex(aCompRes->subResult(theRow));
540 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
544 //******************************************************
545 static QModelIndex MYLastDeleted;
546 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
548 // To avoid additional request about index which was already deleted
549 if (theIndex == MYLastDeleted)
550 return QModelIndex();
552 int aId = theIndex.internalId();
553 if (aId != -1) { // The object is not a root folder
554 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
556 // It is a folder of sub-document
557 return findDocumentRootIndex(aDoc);
559 ObjectPtr aObj = object(theIndex);
561 // To avoid additional request about index which was already deleted
562 // If deleted it causes a crash on delete object from Part
563 MYLastDeleted = theIndex;
564 return QModelIndex();
566 // Check is it object a sub-object of a complex object
567 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
568 if (aFeature.get()) {
569 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
570 if (aCompFea.get()) {
571 return objectIndex(aCompFea);
574 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
576 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
577 if (aCompRes.get()) {
578 return objectIndex(aCompRes);
581 // Use as ordinary object
582 std::string aType = aObj->groupName();
583 SessionPtr aSession = ModelAPI_Session::get();
584 DocumentPtr aRootDoc = aSession->moduleDocument();
585 DocumentPtr aSubDoc = aObj->document();
586 if (aSubDoc == aRootDoc) {
587 if (aType == myXMLReader.rootType())
588 return QModelIndex();
590 // return first level of folder index
591 int aFolderId = myXMLReader.rootFolderId(aType);
592 // Items in a one row must have the same parent
593 return createIndex(aFolderId, 0, -1);
596 if (aType == myXMLReader.subType())
597 return findDocumentRootIndex(aSubDoc.get());
599 // return first level of folder index
600 int aFolderId = myXMLReader.subFolderId(aType);
601 // Items in a one row must have the same parent
602 return createIndex(aFolderId, 0, aSubDoc.get());
606 return QModelIndex();
609 //******************************************************
610 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
612 return rowCount(theParent) > 0;
615 //******************************************************
616 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
618 beginInsertRows(theParent, theRow, theRow + theCount - 1);
624 //******************************************************
625 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
627 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
632 //******************************************************
633 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
635 qint64 aIt = theIndex.internalId();
636 ModelAPI_Object* aObj = 0;
637 ModelAPI_Document* aDoc = 0;
638 SessionPtr aSession = ModelAPI_Session::get();
639 DocumentPtr aActiveDoc = aSession->activeDocument();
641 Qt::ItemFlags aNullFlag;
642 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
643 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
647 // Folders under root
648 DocumentPtr aRootDoc = aSession->moduleDocument();
649 if (aRootDoc != aActiveDoc)
652 aDoc = getSubDocument(theIndex.internalPointer());
654 aObj = (ModelAPI_Object*) theIndex.internalPointer();
659 if (aObj->isDisabled())
660 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
662 bool isCompositeSub = false;
663 // An object which is sub-object of a composite object can not be accessible in column 1
664 if (theIndex.column() == 1) {
665 ObjectPtr aObjPtr = aObj->data()->owner();
666 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
667 if (aFeature.get()) {
668 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
670 isCompositeSub = true;
672 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
674 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
676 isCompositeSub = true;
681 return Qt::ItemIsSelectable;
683 if (aObj->document() != aActiveDoc) {
684 // The object could be a root of sub-tree
685 ResultPartPtr aPartRes = getPartResult(aObj);
686 if (aPartRes.get()) {
687 if (aPartRes->partDoc() == aActiveDoc)
693 // A folder under sub-document
694 if (aActiveDoc.get() != aDoc)
700 //******************************************************
701 QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
703 SessionPtr aSession = ModelAPI_Session::get();
704 DocumentPtr aRootDoc = aSession->moduleDocument();
705 if (myXMLReader.isAttachToResult()) { // If document is attached to result
706 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
708 ResultPartPtr aPartRes;
709 for (int i = 0; i < aNb; i++) {
710 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
711 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
712 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
714 if (myXMLReader.rootType() == ModelAPI_Feature::group()) {
715 aRow += foldersCount();
717 return createIndex(aRow, 0, aObj.get());
720 } else { // If document is attached to feature
721 int aNb = aRootDoc->size(ModelAPI_Feature::group());
723 ResultPartPtr aPartRes;
724 for (int i = 0; i < aNb; i++) {
725 aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
726 aPartRes = getPartResult(aObj.get());
727 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
729 if (myXMLReader.rootType() == ModelAPI_Feature::group())
730 aRow += foldersCount();
731 return createIndex(aRow, 0, aObj.get());
735 return QModelIndex();
738 //******************************************************
739 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
741 SessionPtr aSession = ModelAPI_Session::get();
742 DocumentPtr aRootDoc = aSession->moduleDocument();
743 if (theDoc == aRootDoc)
744 return QModelIndex();
746 return findDocumentRootIndex(theDoc.get());
749 //******************************************************
750 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
753 SessionPtr aSession = ModelAPI_Session::get();
754 DocumentPtr aRootDoc = aSession->moduleDocument();
755 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
756 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
757 if (myXMLReader.rootShowEmpty(i))
760 if (aRootDoc->size(myXMLReader.rootFolderType(i)) > 0)
765 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
766 if (myXMLReader.subShowEmpty(i))
769 if (theDoc->size(myXMLReader.subFolderType(i)) > 0)
778 //******************************************************
779 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
782 SessionPtr aSession = ModelAPI_Session::get();
783 DocumentPtr aRootDoc = aSession->moduleDocument();
784 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
785 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
786 if (!myXMLReader.rootShowEmpty(i)) {
787 if (aRootDoc->size(myXMLReader.rootFolderType(i)) == 0)
792 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
793 if (!myXMLReader.subShowEmpty(i)) {
794 if (theDoc->size(myXMLReader.subFolderType(i)) == 0)
803 //******************************************************
804 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
808 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
809 if (!myXMLReader.rootShowEmpty(i))
810 aResult << myXMLReader.rootFolderType(i).c_str();
813 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
814 if (!myXMLReader.subShowEmpty(i))
815 aResult << myXMLReader.subFolderType(i).c_str();
821 //******************************************************
822 QModelIndex XGUI_DataModel::lastHistoryIndex() const
824 SessionPtr aSession = ModelAPI_Session::get();
825 DocumentPtr aCurDoc = aSession->activeDocument();
826 FeaturePtr aFeature = aCurDoc->currentFeature(true);
827 if (aFeature.get()) {
828 QModelIndex aInd = objectIndex(aFeature);
829 return createIndex(aInd.row(), 1, aInd.internalPointer());
831 if (aCurDoc == aSession->moduleDocument())
832 return createIndex(foldersCount() - 1, 1, -1);
834 return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());
838 //******************************************************
839 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc)
841 SessionPtr aSession = ModelAPI_Session::get();
842 ModelAPI_Document* aDoc = theDoc;
844 aDoc = aSession->moduleDocument().get();
846 bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
850 int aId = myXMLReader.subFolderId(theType);
852 for (int i = 0; i < aId; i++) {
853 if (!myXMLReader.subShowEmpty(i)) {
854 if (aDoc->size(myXMLReader.subFolderType(i)) == 0)
859 int aId = myXMLReader.rootFolderId(theType);
861 for (int i = 0; i < aId; i++) {
862 if (!myXMLReader.rootShowEmpty(i)) {
863 if (aDoc->size(myXMLReader.rootFolderType(i)) == 0)