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);
223 int aChildNb = rowCount(aParent);
224 QModelIndex aStartIndex = index(0, 0, aParent);
225 QModelIndex aEndIndex = index(aChildNb - 1, 0, aParent);
226 emit dataChanged(aStartIndex, aEndIndex);
228 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
229 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
230 if (aDoc != aRootDoc) {
231 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
232 if (aDocRoot.isValid())
233 emit dataChanged(aDocRoot, aDocRoot);
236 Events_Error::send("Problem with Data Model definition of sub-document");
242 //******************************************************
243 void XGUI_DataModel::clear()
248 //******************************************************
249 void XGUI_DataModel::rebuildDataTree()
254 //******************************************************
255 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
257 if (theIndex.internalId() < 0) // this is a folder
259 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
260 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
263 return aObj->data()->owner();
266 //******************************************************
267 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
269 std::string aType = theObject->groupName();
270 DocumentPtr aDoc = theObject->document();
271 int aRow = aDoc->index(theObject);
273 // it could be a part of complex object
274 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
275 if (aFeature.get()) {
276 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
277 if (aCompFea.get()) {
278 for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
279 if (aCompFea->subFeature(i, true) == theObject) {
286 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
288 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
289 if (aCompRes.get()) {
290 for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
291 if (aCompRes->subResult(i, true) == theObject) {
300 return QModelIndex();
302 return createIndex(aRow, 0, theObject.get());
304 SessionPtr aSession = ModelAPI_Session::get();
305 DocumentPtr aRootDoc = aSession->moduleDocument();
306 if (aDoc == aRootDoc && myXMLReader.rootType() == aType) {
307 // The object from root document
308 aRow += foldersCount();
309 } else if (myXMLReader.subType() == aType) {
310 // The object from sub document
311 aRow += foldersCount(aDoc.get());
313 return createIndex(aRow, 0, theObject.get());
316 //******************************************************
317 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
319 SessionPtr aSession = ModelAPI_Session::get();
320 DocumentPtr aRootDoc = aSession->moduleDocument();
321 int aNbFolders = foldersCount();
322 int theIndexRow = theIndex.row();
324 if ((theRole == Qt::DecorationRole) && (theIndex == lastHistoryIndex()))
325 return QIcon(":pictures/arrow.png");
327 if (theIndex.column() == 1)
330 int aParentId = theIndex.internalId();
331 if (aParentId == -1) { // root folders
333 case Qt::DisplayRole:
334 return QString(myXMLReader.rootFolderName(theIndexRow).c_str()) +
335 QString(" (%1)").arg(rowCount(theIndex));
336 case Qt::DecorationRole:
337 return QIcon(myXMLReader.rootFolderIcon(theIndexRow).c_str());
338 case Qt::ForegroundRole:
339 if ((flags(theIndex) & Qt::ItemIsEditable) == 0)
340 return QBrush(Qt::lightGray);
343 } else { // an object or sub-document
344 if (theRole == Qt::ForegroundRole) {
345 if ((flags(theIndex) & Qt::ItemIsEditable) == 0)
346 return QBrush(Qt::lightGray);
350 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
351 if (aSubDoc) { // this is a folder of sub document
352 QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
353 int aRow = theIndexRow;
354 while (aMissedIdx.contains(aRow))
358 case Qt::DisplayRole:
359 return QString(myXMLReader.subFolderName(aRow).c_str()) +
360 QString(" (%1)").arg(rowCount(theIndex));
361 case Qt::DecorationRole:
362 return QIcon(myXMLReader.subFolderIcon(aRow).c_str());
365 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
367 case Qt::DisplayRole:
369 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
370 ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
371 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
372 QString aVal = QString::number(aValueAttribute->value());
373 QString aTitle = QString(aObj->data()->name().c_str());
374 return aTitle + " = " + aVal;
377 if (aObj->groupName() == myXMLReader.subType()) {
378 ResultPartPtr aPartRes = getPartResult(aObj);
379 if (aPartRes.get()) {
380 if (aPartRes->partDoc().get() == NULL)
381 aPrefix = "Not loaded ";
384 return aPrefix + aObj->data()->name().c_str();
386 case Qt::DecorationRole:
387 return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
394 //******************************************************
395 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
400 //******************************************************
401 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
403 SessionPtr aSession = ModelAPI_Session::get();
404 if (!aSession->hasModuleDocument())
406 DocumentPtr aRootDoc = aSession->moduleDocument();
408 if (!theParent.isValid()) {
409 // Return number of items in root
410 int aNbFolders = foldersCount();
412 std::string aType = myXMLReader.rootType();
414 aNbItems = aRootDoc->size(aType);
415 return aNbFolders + aNbItems;
418 int aId = theParent.internalId();
420 // this is a folder under root
421 int aParentPos = theParent.row();
422 std::string aType = myXMLReader.rootFolderType(aParentPos);
423 return aRootDoc->size(aType);
425 // It is an object which could have children
426 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
428 // a folder of sub-document
429 QIntList aMissedIdx = missedFolderIndexes(aDoc);
430 int aRow = theParent.row();
431 while (aMissedIdx.contains(aRow))
433 std::string aType = myXMLReader.subFolderType(aRow);
434 return aDoc->size(aType);
436 ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
437 // Check for Part feature
438 ResultPartPtr aPartRes = getPartResult(aObj);
439 if (aPartRes.get()) {
440 DocumentPtr aSubDoc = aPartRes->partDoc();
444 int aNbSubFolders = foldersCount(aSubDoc.get());
446 std::string aSubType = myXMLReader.subType();
447 if (!aSubType.empty())
448 aNbSubItems = aSubDoc->size(aSubType);
449 return aNbSubItems + aNbSubFolders;
451 // Check for composite object
452 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
454 return aCompFeature->numberOfSubs(true);
455 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
457 return aCompRes->numberOfSubs(true);
464 //******************************************************
465 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
470 //******************************************************
471 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
473 SessionPtr aSession = ModelAPI_Session::get();
474 DocumentPtr aRootDoc = aSession->moduleDocument();
475 int aNbFolders = foldersCount();
479 if (!theParent.isValid()) {
480 if (theRow < aNbFolders) // Return first level folder index
481 return createIndex(theRow, theColumn, -1);
482 else { // return object under root index
483 std::string aType = myXMLReader.rootType();
484 int aObjId = theRow - aNbFolders;
485 if (aObjId < aRootDoc->size(aType)) {
486 ObjectPtr aObj = aRootDoc->object(aType, aObjId);
487 aIndex = objectIndex(aObj);
491 int aId = theParent.internalId();
492 int aParentPos = theParent.row();
493 if (aId == -1) { // return object index inside of first level of folders
494 std::string aType = myXMLReader.rootFolderType(aParentPos);
495 if (theRow < aRootDoc->size(aType)) {
496 ObjectPtr aObj = aRootDoc->object(aType, theRow);
497 aIndex = objectIndex(aObj);
500 // It is an object which could have children
501 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
503 // It is a folder of sub-document
504 int aParentRow = aParentPos;
505 QIntList aMissedIdx = missedFolderIndexes(aDoc);
506 while (aMissedIdx.contains(aParentRow))
508 std::string aType = myXMLReader.subFolderType(aParentRow);
509 if (theRow < aDoc->size(aType)) {
510 ObjectPtr aObj = aDoc->object(aType, theRow);
511 aIndex = objectIndex(aObj);
514 ModelAPI_Object* aParentObj = (ModelAPI_Object*)theParent.internalPointer();
516 // Check for Part feature
517 ResultPartPtr aPartRes = getPartResult(aParentObj);
518 if (aPartRes.get()) {
519 DocumentPtr aSubDoc = aPartRes->partDoc();
520 int aNbSubFolders = foldersCount(aSubDoc.get());
521 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
522 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
524 // this is an object under sub document root
525 std::string aType = myXMLReader.subType();
526 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
527 aIndex = objectIndex(aObj);
530 // Check for composite object
531 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
533 aIndex = objectIndex(aCompFeature->subFeature(theRow));
535 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
537 aIndex = objectIndex(aCompRes->subResult(theRow));
544 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
548 //******************************************************
549 static QModelIndex MYLastDeleted;
550 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
552 // To avoid additional request about index which was already deleted
553 if (theIndex == MYLastDeleted)
554 return QModelIndex();
556 int aId = theIndex.internalId();
557 if (aId != -1) { // The object is not a root folder
558 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
560 // It is a folder of sub-document
561 return findDocumentRootIndex(aDoc);
563 ObjectPtr aObj = object(theIndex);
565 // To avoid additional request about index which was already deleted
566 // If deleted it causes a crash on delete object from Part
567 MYLastDeleted = theIndex;
568 return QModelIndex();
570 // Check is it object a sub-object of a complex object
571 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
572 if (aFeature.get()) {
573 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
574 if (aCompFea.get()) {
575 return objectIndex(aCompFea);
578 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
580 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
581 if (aCompRes.get()) {
582 return objectIndex(aCompRes);
585 // Use as ordinary object
586 std::string aType = aObj->groupName();
587 SessionPtr aSession = ModelAPI_Session::get();
588 DocumentPtr aRootDoc = aSession->moduleDocument();
589 DocumentPtr aSubDoc = aObj->document();
590 if (aSubDoc == aRootDoc) {
591 if (aType == myXMLReader.rootType())
592 return QModelIndex();
594 // return first level of folder index
595 int aFolderId = myXMLReader.rootFolderId(aType);
596 // Items in a one row must have the same parent
597 return createIndex(aFolderId, 0, -1);
600 if (aType == myXMLReader.subType())
601 return findDocumentRootIndex(aSubDoc.get());
603 // return first level of folder index
604 int aFolderId = myXMLReader.subFolderId(aType);
605 // Items in a one row must have the same parent
606 return createIndex(aFolderId, 0, aSubDoc.get());
610 return QModelIndex();
613 //******************************************************
614 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
616 return rowCount(theParent) > 0;
619 //******************************************************
620 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
622 beginInsertRows(theParent, theRow, theRow + theCount - 1);
628 //******************************************************
629 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
631 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
636 //******************************************************
637 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
639 qint64 aIt = theIndex.internalId();
640 ModelAPI_Object* aObj = 0;
641 ModelAPI_Document* aDoc = 0;
642 SessionPtr aSession = ModelAPI_Session::get();
643 DocumentPtr aActiveDoc = aSession->activeDocument();
645 Qt::ItemFlags aNullFlag;
646 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
647 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
651 // Folders under root
652 DocumentPtr aRootDoc = aSession->moduleDocument();
653 if (aRootDoc != aActiveDoc)
656 aDoc = getSubDocument(theIndex.internalPointer());
658 aObj = (ModelAPI_Object*) theIndex.internalPointer();
663 if (aObj->isDisabled())
664 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
666 bool isCompositeSub = false;
667 // An object which is sub-object of a composite object can not be accessible in column 1
668 if (theIndex.column() == 1) {
669 ObjectPtr aObjPtr = aObj->data()->owner();
670 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
671 if (aFeature.get()) {
672 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
674 isCompositeSub = true;
676 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
678 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
680 isCompositeSub = true;
685 return Qt::ItemIsSelectable;
687 if (aObj->document() != aActiveDoc) {
688 // The object could be a root of sub-tree
689 ResultPartPtr aPartRes = getPartResult(aObj);
690 if (aPartRes.get()) {
691 if (aPartRes->partDoc() == aActiveDoc)
697 // A folder under sub-document
698 if (aActiveDoc.get() != aDoc)
704 //******************************************************
705 QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
707 SessionPtr aSession = ModelAPI_Session::get();
708 DocumentPtr aRootDoc = aSession->moduleDocument();
709 if (myXMLReader.isAttachToResult()) { // If document is attached to result
710 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
712 ResultPartPtr aPartRes;
713 for (int i = 0; i < aNb; i++) {
714 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
715 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
716 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
718 if (myXMLReader.rootType() == ModelAPI_Feature::group()) {
719 aRow += foldersCount();
721 return createIndex(aRow, 0, aObj.get());
724 } else { // If document is attached to feature
725 int aNb = aRootDoc->size(ModelAPI_Feature::group());
727 ResultPartPtr aPartRes;
728 for (int i = 0; i < aNb; i++) {
729 aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
730 aPartRes = getPartResult(aObj.get());
731 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
733 if (myXMLReader.rootType() == ModelAPI_Feature::group())
734 aRow += foldersCount();
735 return createIndex(aRow, 0, aObj.get());
739 return QModelIndex();
742 //******************************************************
743 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
745 SessionPtr aSession = ModelAPI_Session::get();
746 DocumentPtr aRootDoc = aSession->moduleDocument();
747 if (theDoc == aRootDoc)
748 return QModelIndex();
750 return findDocumentRootIndex(theDoc.get());
753 //******************************************************
754 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
757 SessionPtr aSession = ModelAPI_Session::get();
758 DocumentPtr aRootDoc = aSession->moduleDocument();
759 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
760 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
761 if (myXMLReader.rootShowEmpty(i))
764 if (aRootDoc->size(myXMLReader.rootFolderType(i)) > 0)
769 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
770 if (myXMLReader.subShowEmpty(i))
773 if (theDoc->size(myXMLReader.subFolderType(i)) > 0)
782 //******************************************************
783 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
786 SessionPtr aSession = ModelAPI_Session::get();
787 DocumentPtr aRootDoc = aSession->moduleDocument();
788 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
789 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
790 if (!myXMLReader.rootShowEmpty(i)) {
791 if (aRootDoc->size(myXMLReader.rootFolderType(i)) == 0)
796 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
797 if (!myXMLReader.subShowEmpty(i)) {
798 if (theDoc->size(myXMLReader.subFolderType(i)) == 0)
807 //******************************************************
808 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
812 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
813 if (!myXMLReader.rootShowEmpty(i))
814 aResult << myXMLReader.rootFolderType(i).c_str();
817 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
818 if (!myXMLReader.subShowEmpty(i))
819 aResult << myXMLReader.subFolderType(i).c_str();
825 //******************************************************
826 QModelIndex XGUI_DataModel::lastHistoryIndex() const
828 SessionPtr aSession = ModelAPI_Session::get();
829 DocumentPtr aCurDoc = aSession->activeDocument();
830 FeaturePtr aFeature = aCurDoc->currentFeature(true);
831 if (aFeature.get()) {
832 QModelIndex aInd = objectIndex(aFeature);
833 return createIndex(aInd.row(), 1, aInd.internalPointer());
835 if (aCurDoc == aSession->moduleDocument())
836 return createIndex(foldersCount() - 1, 1, -1);
838 return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());
842 //******************************************************
843 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc)
845 SessionPtr aSession = ModelAPI_Session::get();
846 ModelAPI_Document* aDoc = theDoc;
848 aDoc = aSession->moduleDocument().get();
850 bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
854 int aId = myXMLReader.subFolderId(theType);
856 for (int i = 0; i < aId; i++) {
857 if (!myXMLReader.subShowEmpty(i)) {
858 if (aDoc->size(myXMLReader.subFolderType(i)) == 0)
863 int aId = myXMLReader.rootFolderId(theType);
865 for (int i = 0; i < aId; i++) {
866 if (!myXMLReader.rootShowEmpty(i)) {
867 if (aDoc->size(myXMLReader.rootFolderType(i)) == 0)