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;
104 if (aObjType == aRootType) {
105 insertRow(aRow + aNbFolders + 1);
107 int aFolderId = myXMLReader.rootFolderId(aObjType);
108 if (aFolderId != -1) {
109 insertRow(aRow, createIndex(aFolderId, 0, -1));
113 // Object created in sub-document
114 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
115 if (aDocRoot.isValid()) {
116 // Check that new folders could appear
117 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
118 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
119 if ((aNotEmptyFolder.toStdString() == aObjType) && (aDoc->size(aObjType) == 1))
120 // Appears first object in folder which can not be shown empty
121 insertRow(myXMLReader.subFolderId(aObjType), aDocRoot);
123 int aRow = aDoc->size(aObjType) - 1;
124 int aNbSubFolders = foldersCount(aDoc.get());
125 if (aObjType == aSubType) {
126 // List of objects under document root
127 insertRow(aRow + aNbSubFolders, aDocRoot);
129 // List of objects under a folder
131 int aFolderId = myXMLReader.subFolderId(aObjType);
132 if (aFolderId != -1) {
133 insertRow(aRow, createIndex(aFolderId, 0, aDoc.get()));
140 Events_Error::send("Problem with Data Model definition of sub-document");
144 // Deleted object event ***********************
145 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
146 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
147 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
148 DocumentPtr aDoc = aUpdMsg->document();
149 std::set<std::string> aGroups = aUpdMsg->groups();
150 std::set<std::string>::const_iterator aIt;
151 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
152 std::string aGroup = (*aIt);
153 if (aDoc == aRootDoc) { // If root objects
154 int aRow = aRootDoc->size(aGroup);
155 if (aGroup == aRootType) {
156 removeRow(aRow + aNbFolders);
158 int aFolderId = myXMLReader.rootFolderId(aGroup);
159 if (aFolderId != -1) {
160 QModelIndex aFolderIndex = createIndex(aFolderId, 0, -1);
161 removeRow(aRow, aFolderIndex);
164 // Check that some folders could erased
165 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
166 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
167 if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup) == 0))
168 // Appears first object in folder which can not be shown empty
169 removeRow(myXMLReader.rootFolderId(aGroup));
172 // Remove row for sub-document
173 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
174 if (aDocRoot.isValid()) {
175 int aRow = aDoc->size(aGroup);
176 int aNbSubFolders = foldersCount(aDoc.get());
177 if (aGroup == aSubType) {
178 // List of objects under document root
179 removeRow(aRow + aNbSubFolders, aDocRoot);
181 // List of objects under a folder
182 int aFolderId = myXMLReader.subFolderId(aGroup);
183 if (aFolderId != -1) {
184 removeRow(aRow, createIndex(aFolderId, 0, aDoc.get()));
187 // Check that some folders could disappear
188 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
189 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
190 if ((aNotEmptyFolder.toStdString() == aGroup) && (aDoc->size(aGroup) == 1))
191 // Appears first object in folder which can not be shown empty
192 removeRow(myXMLReader.subFolderId(aGroup), aDocRoot);
197 Events_Error::send("Problem with Data Model definition of sub-document");
201 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
202 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
203 if (aDoc != aRootDoc) {
204 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
205 if (aDocRoot.isValid())
206 emit dataChanged(aDocRoot, aDocRoot);
209 Events_Error::send("Problem with Data Model definition of sub-document");
215 //******************************************************
216 void XGUI_DataModel::clear()
221 //******************************************************
222 void XGUI_DataModel::rebuildDataTree()
227 //******************************************************
228 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
230 if (theIndex.internalId() < 0) // this is a folder
232 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
233 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
236 return aObj->data()->owner();
239 //******************************************************
240 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
242 std::string aType = theObject->groupName();
243 DocumentPtr aDoc = theObject->document();
244 int aRow = aDoc->index(theObject);
246 // it could be a part of complex object
247 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
248 if (aFeature.get()) {
249 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
250 if (aCompFea.get()) {
251 for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
252 if (aCompFea->subFeature(i, true) == theObject) {
259 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
261 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
262 if (aCompRes.get()) {
263 for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
264 if (aCompRes->subResult(i, true) == theObject) {
273 return QModelIndex();
275 return createIndex(aRow, 0, theObject.get());
277 SessionPtr aSession = ModelAPI_Session::get();
278 DocumentPtr aRootDoc = aSession->moduleDocument();
279 if (aDoc == aRootDoc && myXMLReader.rootType() == aType) {
280 // The object from root document
281 aRow += foldersCount();
282 } else if (myXMLReader.subType() == aType) {
283 // The object from sub document
284 aRow += foldersCount(aDoc.get());
286 return createIndex(aRow, 0, theObject.get());
289 //******************************************************
290 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
292 SessionPtr aSession = ModelAPI_Session::get();
293 DocumentPtr aRootDoc = aSession->moduleDocument();
294 int aNbFolders = foldersCount();
295 int theIndexRow = theIndex.row();
297 if ((theRole == Qt::DecorationRole) && (theIndex == lastHistoryIndex()))
298 return QIcon(":pictures/arrow.png");
300 if (theIndex.column() == 1)
303 int aParentId = theIndex.internalId();
304 if (aParentId == -1) { // root folders
306 case Qt::DisplayRole:
307 return QString(myXMLReader.rootFolderName(theIndexRow).c_str()) +
308 QString(" (%1)").arg(rowCount(theIndex));
309 case Qt::DecorationRole:
310 return QIcon(myXMLReader.rootFolderIcon(theIndexRow).c_str());
311 case Qt::ForegroundRole:
312 if ((flags(theIndex) & Qt::ItemIsEditable) == 0)
313 return QBrush(Qt::lightGray);
316 } else { // an object or sub-document
317 if (theRole == Qt::ForegroundRole) {
318 if ((flags(theIndex) & Qt::ItemIsEditable) == 0)
319 return QBrush(Qt::lightGray);
323 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
324 if (aSubDoc) { // this is a folder of sub document
325 QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
326 int aRow = theIndexRow;
327 while (aMissedIdx.contains(aRow))
331 case Qt::DisplayRole:
332 return QString(myXMLReader.subFolderName(aRow).c_str()) +
333 QString(" (%1)").arg(rowCount(theIndex));
334 case Qt::DecorationRole:
335 return QIcon(myXMLReader.subFolderIcon(aRow).c_str());
338 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
340 case Qt::DisplayRole:
342 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
343 ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
344 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
345 QString aVal = QString::number(aValueAttribute->value());
346 QString aTitle = QString(aObj->data()->name().c_str());
347 return aTitle + " = " + aVal;
350 if (aObj->groupName() == myXMLReader.subType()) {
351 ResultPartPtr aPartRes = getPartResult(aObj);
352 if (aPartRes.get()) {
353 if (aPartRes->partDoc().get() == NULL)
354 aPrefix = "Not loaded ";
357 return aPrefix + aObj->data()->name().c_str();
359 case Qt::DecorationRole:
360 return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
367 //******************************************************
368 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
373 //******************************************************
374 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
376 SessionPtr aSession = ModelAPI_Session::get();
377 if (!aSession->hasModuleDocument())
379 DocumentPtr aRootDoc = aSession->moduleDocument();
381 if (!theParent.isValid()) {
382 // Return number of items in root
383 int aNbFolders = foldersCount();
385 std::string aType = myXMLReader.rootType();
387 aNbItems = aRootDoc->size(aType);
388 return aNbFolders + aNbItems;
391 int aId = theParent.internalId();
393 // this is a folder under root
394 int aParentPos = theParent.row();
395 std::string aType = myXMLReader.rootFolderType(aParentPos);
396 return aRootDoc->size(aType);
398 // It is an object which could have children
399 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
401 // a folder of sub-document
402 QIntList aMissedIdx = missedFolderIndexes(aDoc);
403 int aRow = theParent.row();
404 while (aMissedIdx.contains(aRow))
406 std::string aType = myXMLReader.subFolderType(aRow);
407 return aDoc->size(aType);
409 ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
410 // Check for Part feature
411 ResultPartPtr aPartRes = getPartResult(aObj);
412 if (aPartRes.get()) {
413 DocumentPtr aSubDoc = aPartRes->partDoc();
417 int aNbSubFolders = foldersCount(aSubDoc.get());
419 std::string aSubType = myXMLReader.subType();
420 if (!aSubType.empty())
421 aNbSubItems = aSubDoc->size(aSubType);
422 return aNbSubItems + aNbSubFolders;
424 // Check for composite object
425 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
427 return aCompFeature->numberOfSubs(true);
428 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
430 return aCompRes->numberOfSubs(true);
437 //******************************************************
438 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
443 //******************************************************
444 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
446 SessionPtr aSession = ModelAPI_Session::get();
447 DocumentPtr aRootDoc = aSession->moduleDocument();
448 int aNbFolders = foldersCount();
452 if (!theParent.isValid()) {
453 if (theRow < aNbFolders) // Return first level folder index
454 return createIndex(theRow, theColumn, -1);
455 else { // return object under root index
456 std::string aType = myXMLReader.rootType();
457 int aObjId = theRow - aNbFolders;
458 if (aObjId < aRootDoc->size(aType)) {
459 ObjectPtr aObj = aRootDoc->object(aType, aObjId);
460 aIndex = objectIndex(aObj);
464 int aId = theParent.internalId();
465 int aParentPos = theParent.row();
466 if (aId == -1) { // return object index inside of first level of folders
467 std::string aType = myXMLReader.rootFolderType(aParentPos);
468 if (theRow < aRootDoc->size(aType)) {
469 ObjectPtr aObj = aRootDoc->object(aType, theRow);
470 aIndex = objectIndex(aObj);
473 // It is an object which could have children
474 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
476 // It is a folder of sub-document
477 std::string aType = myXMLReader.subFolderType(aParentPos);
478 if (theRow < aDoc->size(aType)) {
479 ObjectPtr aObj = aDoc->object(aType, theRow);
480 aIndex = objectIndex(aObj);
483 ModelAPI_Object* aParentObj = (ModelAPI_Object*)theParent.internalPointer();
485 // Check for Part feature
486 ResultPartPtr aPartRes = getPartResult(aParentObj);
487 if (aPartRes.get()) {
488 DocumentPtr aSubDoc = aPartRes->partDoc();
489 int aNbSubFolders = foldersCount(aSubDoc.get());
490 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
491 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
493 // this is an object under sub document root
494 std::string aType = myXMLReader.subType();
495 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
496 aIndex = objectIndex(aObj);
499 // Check for composite object
500 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
502 aIndex = objectIndex(aCompFeature->subFeature(theRow));
504 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
506 aIndex = objectIndex(aCompRes->subResult(theRow));
513 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
517 //******************************************************
518 static QModelIndex MYLastDeleted;
519 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
521 // To avoid additional request about index which was already deleted
522 if (theIndex == MYLastDeleted)
523 return QModelIndex();
525 int aId = theIndex.internalId();
526 if (aId != -1) { // The object is not a root folder
527 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
529 // It is a folder of sub-document
530 return findDocumentRootIndex(aDoc);
532 ObjectPtr aObj = object(theIndex);
534 // To avoid additional request about index which was already deleted
535 // If deleted it causes a crash on delete object from Part
536 MYLastDeleted = theIndex;
537 return QModelIndex();
539 // Check is it object a sub-object of a complex object
540 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
541 if (aFeature.get()) {
542 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
543 if (aCompFea.get()) {
544 return objectIndex(aCompFea);
547 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
549 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
550 if (aCompRes.get()) {
551 return objectIndex(aCompRes);
554 // Use as ordinary object
555 std::string aType = aObj->groupName();
556 SessionPtr aSession = ModelAPI_Session::get();
557 DocumentPtr aRootDoc = aSession->moduleDocument();
558 DocumentPtr aSubDoc = aObj->document();
559 if (aSubDoc == aRootDoc) {
560 if (aType == myXMLReader.rootType())
561 return QModelIndex();
563 // return first level of folder index
564 int aFolderId = myXMLReader.rootFolderId(aType);
565 // Items in a one row must have the same parent
566 return createIndex(aFolderId, 0, -1);
569 if (aType == myXMLReader.subType())
570 return findDocumentRootIndex(aSubDoc.get());
572 // return first level of folder index
573 int aFolderId = myXMLReader.subFolderId(aType);
574 // Items in a one row must have the same parent
575 return createIndex(aFolderId, 0, aSubDoc.get());
579 return QModelIndex();
582 //******************************************************
583 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
585 return rowCount(theParent) > 0;
588 //******************************************************
589 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
591 beginInsertRows(theParent, theRow, theRow + theCount - 1);
597 //******************************************************
598 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
600 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
605 //******************************************************
606 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
608 qint64 aIt = theIndex.internalId();
609 ModelAPI_Object* aObj = 0;
610 ModelAPI_Document* aDoc = 0;
611 SessionPtr aSession = ModelAPI_Session::get();
612 DocumentPtr aActiveDoc = aSession->activeDocument();
614 Qt::ItemFlags aNullFlag;
615 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
616 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
620 // Folders under root
621 DocumentPtr aRootDoc = aSession->moduleDocument();
622 if (aRootDoc != aActiveDoc)
625 aDoc = getSubDocument(theIndex.internalPointer());
627 aObj = (ModelAPI_Object*) theIndex.internalPointer();
632 if (aObj->isDisabled())
633 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
635 bool isCompositeSub = false;
636 // An object which is sub-object of a composite object can not be accessible in column 1
637 if (theIndex.column() == 1) {
638 ObjectPtr aObjPtr = aObj->data()->owner();
639 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
640 if (aFeature.get()) {
641 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
643 isCompositeSub = true;
645 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
647 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
649 isCompositeSub = true;
654 return Qt::ItemIsSelectable;
656 if (aObj->document() != aActiveDoc) {
657 // The object could be a root of sub-tree
658 ResultPartPtr aPartRes = getPartResult(aObj);
659 if (aPartRes.get()) {
660 if (aPartRes->partDoc() == aActiveDoc)
666 // A folder under sub-document
667 if (aActiveDoc.get() != aDoc)
673 //******************************************************
674 QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
676 SessionPtr aSession = ModelAPI_Session::get();
677 DocumentPtr aRootDoc = aSession->moduleDocument();
678 if (myXMLReader.isAttachToResult()) { // If document is attached to result
679 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
681 ResultPartPtr aPartRes;
682 for (int i = 0; i < aNb; i++) {
683 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
684 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
685 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
687 if (myXMLReader.rootType() == ModelAPI_Feature::group()) {
688 aRow += foldersCount();
690 return createIndex(aRow, 0, aObj.get());
693 } else { // If document is attached to feature
694 int aNb = aRootDoc->size(ModelAPI_Feature::group());
696 ResultPartPtr aPartRes;
697 for (int i = 0; i < aNb; i++) {
698 aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
699 aPartRes = getPartResult(aObj.get());
700 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
702 if (myXMLReader.rootType() == ModelAPI_Feature::group())
703 aRow += foldersCount();
704 return createIndex(aRow, 0, aObj.get());
708 return QModelIndex();
711 //******************************************************
712 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
714 SessionPtr aSession = ModelAPI_Session::get();
715 DocumentPtr aRootDoc = aSession->moduleDocument();
716 if (theDoc == aRootDoc)
717 return QModelIndex();
719 return findDocumentRootIndex(theDoc.get());
722 //******************************************************
723 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
726 SessionPtr aSession = ModelAPI_Session::get();
727 DocumentPtr aRootDoc = aSession->moduleDocument();
728 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
729 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
730 if (myXMLReader.rootShowEmpty(i))
733 if (aRootDoc->size(myXMLReader.rootFolderType(i)) > 0)
738 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
739 if (myXMLReader.subShowEmpty(i))
742 if (theDoc->size(myXMLReader.subFolderType(i)) > 0)
751 //******************************************************
752 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
755 SessionPtr aSession = ModelAPI_Session::get();
756 DocumentPtr aRootDoc = aSession->moduleDocument();
757 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
758 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
759 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)) {
767 if (theDoc->size(myXMLReader.subFolderType(i)) == 0)
776 //******************************************************
777 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
781 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
782 if (!myXMLReader.rootShowEmpty(i))
783 aResult << myXMLReader.rootFolderType(i).c_str();
786 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
787 if (!myXMLReader.subShowEmpty(i))
788 aResult << myXMLReader.subFolderType(i).c_str();
794 //******************************************************
795 QModelIndex XGUI_DataModel::lastHistoryIndex() const
797 SessionPtr aSession = ModelAPI_Session::get();
798 DocumentPtr aCurDoc = aSession->activeDocument();
799 FeaturePtr aFeature = aCurDoc->currentFeature(true);
800 if (aFeature.get()) {
801 QModelIndex aInd = objectIndex(aFeature);
802 return createIndex(aInd.row(), 1, aInd.internalPointer());
804 if (aCurDoc == aSession->moduleDocument())
805 return createIndex(foldersCount() - 1, 1, -1);
807 return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());