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_OrderUpdatedMessage> aUpdMsg =
211 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
212 DocumentPtr aDoc = aUpdMsg->document();
213 std::string aGroup = aUpdMsg->group();
217 if (aDoc == aRootDoc) {
218 // Update a group under root
219 if (aGroup == myXMLReader.rootType()) // Update objects under root
220 aSartId = foldersCount();
221 else // Update objects in folder under root
222 aParent = createIndex(folderId(aGroup), 0, -1);
224 // Update a sub-document
225 if (aGroup == myXMLReader.subType()) {
226 // Update sub-document root
227 aParent = findDocumentRootIndex(aDoc.get());
228 aSartId = foldersCount(aDoc.get());
230 // update folder in sub-document
231 aParent = createIndex(folderId(aGroup, aDoc.get()), 0, aDoc.get());
233 int aChildNb = rowCount(aParent);
234 // Rebuild all indexes
235 removeRows(aSartId, aChildNb - aSartId, aParent);
236 insertRows(aSartId, aChildNb - aSartId, aParent);
237 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
238 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
239 if (aDoc != aRootDoc) {
240 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
241 if (aDocRoot.isValid())
242 emit dataChanged(aDocRoot, aDocRoot);
245 Events_Error::send("Problem with Data Model definition of sub-document");
251 //******************************************************
252 void XGUI_DataModel::clear()
257 //******************************************************
258 void XGUI_DataModel::rebuildDataTree()
263 //******************************************************
264 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
266 if (theIndex.internalId() < 0) // this is a folder
268 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
269 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
272 return aObj->data()->owner();
275 //******************************************************
276 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
278 std::string aType = theObject->groupName();
279 DocumentPtr aDoc = theObject->document();
280 int aRow = aDoc->index(theObject);
282 // it could be a part of complex object
283 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
284 if (aFeature.get()) {
285 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
286 if (aCompFea.get()) {
287 for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
288 if (aCompFea->subFeature(i, true) == theObject) {
295 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
297 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
298 if (aCompRes.get()) {
299 for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
300 if (aCompRes->subResult(i, true) == theObject) {
309 return QModelIndex();
311 return createIndex(aRow, 0, theObject.get());
313 SessionPtr aSession = ModelAPI_Session::get();
314 DocumentPtr aRootDoc = aSession->moduleDocument();
315 if (aDoc == aRootDoc && myXMLReader.rootType() == aType) {
316 // The object from root document
317 aRow += foldersCount();
318 } else if (myXMLReader.subType() == aType) {
319 // The object from sub document
320 aRow += foldersCount(aDoc.get());
322 return createIndex(aRow, 0, theObject.get());
325 //******************************************************
326 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
328 SessionPtr aSession = ModelAPI_Session::get();
329 DocumentPtr aRootDoc = aSession->moduleDocument();
330 int aNbFolders = foldersCount();
331 int theIndexRow = theIndex.row();
333 if ((theRole == Qt::DecorationRole) && (theIndex == lastHistoryIndex()))
334 return QIcon(":pictures/arrow.png");
336 if (theIndex.column() == 1)
339 int aParentId = theIndex.internalId();
340 if (aParentId == -1) { // root folders
342 case Qt::DisplayRole:
343 return QString(myXMLReader.rootFolderName(theIndexRow).c_str()) +
344 QString(" (%1)").arg(rowCount(theIndex));
345 case Qt::DecorationRole:
346 return QIcon(myXMLReader.rootFolderIcon(theIndexRow).c_str());
347 case Qt::ForegroundRole:
348 if ((flags(theIndex) & Qt::ItemIsEditable) == 0)
349 return QBrush(Qt::lightGray);
352 } else { // an object or sub-document
353 if (theRole == Qt::ForegroundRole) {
354 if ((flags(theIndex) & Qt::ItemIsEditable) == 0)
355 return QBrush(Qt::lightGray);
359 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
360 if (aSubDoc) { // this is a folder of sub document
361 QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
362 int aRow = theIndexRow;
363 while (aMissedIdx.contains(aRow))
367 case Qt::DisplayRole:
368 return QString(myXMLReader.subFolderName(aRow).c_str()) +
369 QString(" (%1)").arg(rowCount(theIndex));
370 case Qt::DecorationRole:
371 return QIcon(myXMLReader.subFolderIcon(aRow).c_str());
374 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
376 case Qt::DisplayRole:
378 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
379 ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
380 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
381 QString aVal = QString::number(aValueAttribute->value());
382 QString aTitle = QString(aObj->data()->name().c_str());
383 return aTitle + " = " + aVal;
386 if (aObj->groupName() == myXMLReader.subType()) {
387 ResultPartPtr aPartRes = getPartResult(aObj);
388 if (aPartRes.get()) {
389 if (aPartRes->partDoc().get() == NULL)
390 aPrefix = "Not loaded ";
393 return aPrefix + aObj->data()->name().c_str();
395 case Qt::DecorationRole:
396 return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
403 //******************************************************
404 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
409 //******************************************************
410 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
412 SessionPtr aSession = ModelAPI_Session::get();
413 if (!aSession->hasModuleDocument())
415 DocumentPtr aRootDoc = aSession->moduleDocument();
417 if (!theParent.isValid()) {
418 // Return number of items in root
419 int aNbFolders = foldersCount();
421 std::string aType = myXMLReader.rootType();
423 aNbItems = aRootDoc->size(aType);
424 return aNbFolders + aNbItems;
427 int aId = theParent.internalId();
429 // this is a folder under root
430 int aParentPos = theParent.row();
431 std::string aType = myXMLReader.rootFolderType(aParentPos);
432 return aRootDoc->size(aType);
434 // It is an object which could have children
435 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
437 // a folder of sub-document
438 QIntList aMissedIdx = missedFolderIndexes(aDoc);
439 int aRow = theParent.row();
440 while (aMissedIdx.contains(aRow))
442 std::string aType = myXMLReader.subFolderType(aRow);
443 return aDoc->size(aType);
445 ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
446 // Check for Part feature
447 ResultPartPtr aPartRes = getPartResult(aObj);
448 if (aPartRes.get()) {
449 DocumentPtr aSubDoc = aPartRes->partDoc();
453 int aNbSubFolders = foldersCount(aSubDoc.get());
455 std::string aSubType = myXMLReader.subType();
456 if (!aSubType.empty())
457 aNbSubItems = aSubDoc->size(aSubType);
458 return aNbSubItems + aNbSubFolders;
460 // Check for composite object
461 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
463 return aCompFeature->numberOfSubs(true);
464 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
466 return aCompRes->numberOfSubs(true);
473 //******************************************************
474 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
479 //******************************************************
480 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
482 SessionPtr aSession = ModelAPI_Session::get();
483 DocumentPtr aRootDoc = aSession->moduleDocument();
484 int aNbFolders = foldersCount();
488 if (!theParent.isValid()) {
489 if (theRow < aNbFolders) // Return first level folder index
490 return createIndex(theRow, theColumn, -1);
491 else { // return object under root index
492 std::string aType = myXMLReader.rootType();
493 int aObjId = theRow - aNbFolders;
494 if (aObjId < aRootDoc->size(aType)) {
495 ObjectPtr aObj = aRootDoc->object(aType, aObjId);
496 aIndex = objectIndex(aObj);
500 int aId = theParent.internalId();
501 int aParentPos = theParent.row();
502 if (aId == -1) { // return object index inside of first level of folders
503 std::string aType = myXMLReader.rootFolderType(aParentPos);
504 if (theRow < aRootDoc->size(aType)) {
505 ObjectPtr aObj = aRootDoc->object(aType, theRow);
506 aIndex = objectIndex(aObj);
509 // It is an object which could have children
510 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
512 // It is a folder of sub-document
513 int aParentRow = aParentPos;
514 QIntList aMissedIdx = missedFolderIndexes(aDoc);
515 while (aMissedIdx.contains(aParentRow))
517 std::string aType = myXMLReader.subFolderType(aParentRow);
518 if (theRow < aDoc->size(aType)) {
519 ObjectPtr aObj = aDoc->object(aType, theRow);
520 aIndex = objectIndex(aObj);
523 ModelAPI_Object* aParentObj = (ModelAPI_Object*)theParent.internalPointer();
525 // Check for Part feature
526 ResultPartPtr aPartRes = getPartResult(aParentObj);
527 if (aPartRes.get()) {
528 DocumentPtr aSubDoc = aPartRes->partDoc();
529 int aNbSubFolders = foldersCount(aSubDoc.get());
530 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
531 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
533 // this is an object under sub document root
534 std::string aType = myXMLReader.subType();
535 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
536 aIndex = objectIndex(aObj);
539 // Check for composite object
540 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
542 aIndex = objectIndex(aCompFeature->subFeature(theRow));
544 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
546 aIndex = objectIndex(aCompRes->subResult(theRow));
553 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
557 //******************************************************
558 static QModelIndex MYLastDeleted;
559 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
561 // To avoid additional request about index which was already deleted
562 if (theIndex == MYLastDeleted)
563 return QModelIndex();
565 int aId = theIndex.internalId();
566 if (aId != -1) { // The object is not a root folder
567 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
569 // It is a folder of sub-document
570 return findDocumentRootIndex(aDoc);
572 ObjectPtr aObj = object(theIndex);
574 // To avoid additional request about index which was already deleted
575 // If deleted it causes a crash on delete object from Part
576 MYLastDeleted = theIndex;
577 return QModelIndex();
579 // Check is it object a sub-object of a complex object
580 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
581 if (aFeature.get()) {
582 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
583 if (aCompFea.get()) {
584 return objectIndex(aCompFea);
587 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
589 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
590 if (aCompRes.get()) {
591 return objectIndex(aCompRes);
594 // Use as ordinary object
595 std::string aType = aObj->groupName();
596 SessionPtr aSession = ModelAPI_Session::get();
597 DocumentPtr aRootDoc = aSession->moduleDocument();
598 DocumentPtr aSubDoc = aObj->document();
599 if (aSubDoc == aRootDoc) {
600 if (aType == myXMLReader.rootType())
601 return QModelIndex();
603 // return first level of folder index
604 int aFolderId = myXMLReader.rootFolderId(aType);
605 // Items in a one row must have the same parent
606 return createIndex(aFolderId, 0, -1);
609 if (aType == myXMLReader.subType())
610 return findDocumentRootIndex(aSubDoc.get());
612 // return first level of folder index
613 int aFolderId = myXMLReader.subFolderId(aType);
614 // Items in a one row must have the same parent
615 return createIndex(aFolderId, 0, aSubDoc.get());
619 return QModelIndex();
622 //******************************************************
623 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
625 return rowCount(theParent) > 0;
628 //******************************************************
629 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
631 beginInsertRows(theParent, theRow, theRow + theCount - 1);
637 //******************************************************
638 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
640 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
645 //******************************************************
646 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
648 qint64 aIt = theIndex.internalId();
649 ModelAPI_Object* aObj = 0;
650 ModelAPI_Document* aDoc = 0;
651 SessionPtr aSession = ModelAPI_Session::get();
652 DocumentPtr aActiveDoc = aSession->activeDocument();
654 Qt::ItemFlags aNullFlag;
655 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
656 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
660 // Folders under root
661 DocumentPtr aRootDoc = aSession->moduleDocument();
662 if (aRootDoc != aActiveDoc)
665 aDoc = getSubDocument(theIndex.internalPointer());
667 aObj = (ModelAPI_Object*) theIndex.internalPointer();
672 if (aObj->isDisabled())
673 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
675 bool isCompositeSub = false;
676 // An object which is sub-object of a composite object can not be accessible in column 1
677 if (theIndex.column() == 1) {
678 ObjectPtr aObjPtr = aObj->data()->owner();
679 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
680 if (aFeature.get()) {
681 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
683 isCompositeSub = true;
685 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
687 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
689 isCompositeSub = true;
694 return Qt::ItemIsSelectable;
696 if (aObj->document() != aActiveDoc) {
697 // The object could be a root of sub-tree
698 ResultPartPtr aPartRes = getPartResult(aObj);
699 if (aPartRes.get()) {
700 if (aPartRes->partDoc() == aActiveDoc)
706 // A folder under sub-document
707 if (aActiveDoc.get() != aDoc)
713 //******************************************************
714 QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
716 SessionPtr aSession = ModelAPI_Session::get();
717 DocumentPtr aRootDoc = aSession->moduleDocument();
718 if (myXMLReader.isAttachToResult()) { // If document is attached to result
719 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
721 ResultPartPtr aPartRes;
722 for (int i = 0; i < aNb; i++) {
723 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
724 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
725 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
727 if (myXMLReader.rootType() == ModelAPI_Feature::group()) {
728 aRow += foldersCount();
730 return createIndex(aRow, 0, aObj.get());
733 } else { // If document is attached to feature
734 int aNb = aRootDoc->size(ModelAPI_Feature::group());
736 ResultPartPtr aPartRes;
737 for (int i = 0; i < aNb; i++) {
738 aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
739 aPartRes = getPartResult(aObj.get());
740 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
742 if (myXMLReader.rootType() == ModelAPI_Feature::group())
743 aRow += foldersCount();
744 return createIndex(aRow, 0, aObj.get());
748 return QModelIndex();
751 //******************************************************
752 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
754 SessionPtr aSession = ModelAPI_Session::get();
755 DocumentPtr aRootDoc = aSession->moduleDocument();
756 if (theDoc == aRootDoc)
757 return QModelIndex();
759 return findDocumentRootIndex(theDoc.get());
762 //******************************************************
763 int XGUI_DataModel::foldersCount(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))
773 if (aRootDoc->size(myXMLReader.rootFolderType(i)) > 0)
778 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
779 if (myXMLReader.subShowEmpty(i))
782 if (theDoc->size(myXMLReader.subFolderType(i)) > 0)
791 //******************************************************
792 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
795 SessionPtr aSession = ModelAPI_Session::get();
796 DocumentPtr aRootDoc = aSession->moduleDocument();
797 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
798 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
799 if (!myXMLReader.rootShowEmpty(i)) {
800 if (aRootDoc->size(myXMLReader.rootFolderType(i)) == 0)
805 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
806 if (!myXMLReader.subShowEmpty(i)) {
807 if (theDoc->size(myXMLReader.subFolderType(i)) == 0)
816 //******************************************************
817 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
821 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
822 if (!myXMLReader.rootShowEmpty(i))
823 aResult << myXMLReader.rootFolderType(i).c_str();
826 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
827 if (!myXMLReader.subShowEmpty(i))
828 aResult << myXMLReader.subFolderType(i).c_str();
834 //******************************************************
835 QModelIndex XGUI_DataModel::lastHistoryIndex() const
837 SessionPtr aSession = ModelAPI_Session::get();
838 DocumentPtr aCurDoc = aSession->activeDocument();
839 FeaturePtr aFeature = aCurDoc->currentFeature(true);
840 if (aFeature.get()) {
841 QModelIndex aInd = objectIndex(aFeature);
842 return createIndex(aInd.row(), 1, aInd.internalPointer());
844 if (aCurDoc == aSession->moduleDocument())
845 return createIndex(foldersCount() - 1, 1, -1);
847 return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());
851 //******************************************************
852 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc)
854 SessionPtr aSession = ModelAPI_Session::get();
855 ModelAPI_Document* aDoc = theDoc;
857 aDoc = aSession->moduleDocument().get();
859 bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
863 int aId = myXMLReader.subFolderId(theType);
865 for (int i = 0; i < aId; i++) {
866 if (!myXMLReader.subShowEmpty(i)) {
867 if (aDoc->size(myXMLReader.subFolderType(i)) == 0)
872 int aId = myXMLReader.rootFolderId(theType);
874 for (int i = 0; i < aId; i++) {
875 if (!myXMLReader.rootShowEmpty(i)) {
876 if (aDoc->size(myXMLReader.rootFolderType(i)) == 0)