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 Qt::black
30 //#define ACTIVE_COLOR QColor(0,72,140)
31 //#define PASSIVE_COLOR Qt::black
33 /// Returns ResultPart object if the given object is a Part feature
34 /// Otherwise returns NULL
35 ResultPartPtr getPartResult(ModelAPI_Object* theObj)
37 ModelAPI_Feature* aFeature = dynamic_cast<ModelAPI_Feature*>(theObj);
39 ResultPtr aRes = aFeature->firstResult();
40 if (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group())) {
41 ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aRes);
42 // Use only original parts, not a placement results
43 if (aPartRes == aPartRes->original())
47 return ResultPartPtr();
50 /// Returns pointer on document if the given object is document object
51 ModelAPI_Document* getSubDocument(void* theObj)
53 ModelAPI_Document* aDoc = dynamic_cast<ModelAPI_Document*>((ModelAPI_Entity*)theObj);
60 // Constructor *************************************************
61 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : ModuleBase_IDocumentDataModel(theParent)
63 myXMLReader.readAll();
65 Events_Loop* aLoop = Events_Loop::loop();
66 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
67 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
68 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
69 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_ORDER_UPDATED));
70 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
73 //******************************************************
74 void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
76 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
77 std::string aRootType = myXMLReader.rootType();
78 std::string aSubType = myXMLReader.subType();
79 int aNbFolders = foldersCount();
81 // Created object event *******************
82 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
83 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
84 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
85 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
87 std::set<ObjectPtr>::const_iterator aIt;
89 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
90 ObjectPtr aObject = (*aIt);
91 // We do not show objects which not has to be shown in object browser
92 if (!aObject->isInHistory())
95 aObjType = aObject->groupName();
96 DocumentPtr aDoc = aObject->document();
97 if (aDoc == aRootDoc) {
98 // Check that new folders could appear
99 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
100 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
101 if ((aNotEmptyFolder.toStdString() == aObjType) && (aRootDoc->size(aObjType) == 1))
102 // Appears first object in folder which can not be shown empty
103 insertRow(myXMLReader.rootFolderId(aObjType));
106 int aRow = aRootDoc->size(aObjType) - 1;
108 if (aObjType == aRootType) {
109 insertRow(aRow + aNbFolders + 1);
111 int aFolderId = myXMLReader.rootFolderId(aObjType);
112 if (aFolderId != -1) {
113 insertRow(aRow, createIndex(aFolderId, 0, -1));
118 // Object created in sub-document
119 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
120 if (aDocRoot.isValid()) {
121 // Check that new folders could appear
122 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
123 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
124 if ((aNotEmptyFolder.toStdString() == aObjType) && (aDoc->size(aObjType) == 1))
125 // Appears first object in folder which can not be shown empty
126 insertRow(myXMLReader.subFolderId(aObjType), aDocRoot);
128 int aRow = aDoc->index(aObject);
130 int aNbSubFolders = foldersCount(aDoc.get());
131 if (aObjType == aSubType) {
132 // List of objects under document root
133 insertRow(aRow + aNbSubFolders, aDocRoot);
135 // List of objects under a folder
137 int aFolderId = folderId(aObjType, aDoc.get());
138 if (aFolderId != -1) {
139 QModelIndex aParentFolder = createIndex(aFolderId, 0, aDoc.get());
140 insertRow(aRow, aParentFolder);
141 emit dataChanged(aParentFolder, aParentFolder);
149 Events_Error::send("Problem with Data Model definition of sub-document");
153 // Deleted object event ***********************
154 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
155 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
156 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
157 DocumentPtr aDoc = aUpdMsg->document();
158 std::set<std::string> aGroups = aUpdMsg->groups();
159 std::set<std::string>::const_iterator aIt;
160 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
161 std::string aGroup = (*aIt);
162 if (aDoc == aRootDoc) { // If root objects
163 int aRow = aRootDoc->size(aGroup);
164 if (aGroup == aRootType) {
165 removeRow(aRow + aNbFolders);
167 int aFolderId = myXMLReader.rootFolderId(aGroup);
168 if (aFolderId != -1) {
169 QModelIndex aFolderIndex = createIndex(aFolderId, 0, -1);
170 removeRow(aRow, aFolderIndex);
173 // Check that some folders could erased
174 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
175 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
176 if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup) == 0))
177 // Appears first object in folder which can not be shown empty
178 removeRow(myXMLReader.rootFolderId(aGroup));
181 // Remove row for sub-document
182 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
183 if (aDocRoot.isValid()) {
184 int aRow = aDoc->size(aGroup);
185 int aNbSubFolders = foldersCount(aDoc.get());
186 if (aGroup == aSubType) {
187 // List of objects under document root
188 removeRow(aRow + aNbSubFolders, aDocRoot);
190 // List of objects under a folder
191 int aFolderId = folderId(aGroup, aDoc.get());
192 if (aFolderId != -1) {
193 removeRow(aRow, createIndex(aFolderId, 0, aDoc.get()));
196 // Check that some folders could disappear
197 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
198 int aSize = aDoc->size(aGroup);
199 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
200 if ((aNotEmptyFolder.toStdString() == aGroup) && (aSize == 0))
201 // Appears first object in folder which can not be shown empty
202 removeRow(myXMLReader.subFolderId(aGroup), aDocRoot);
207 Events_Error::send("Problem with Data Model definition of sub-document");
211 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
212 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
213 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
214 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
216 std::set<ObjectPtr>::const_iterator aIt;
217 std::string aObjType;
218 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
219 ObjectPtr aObject = (*aIt);
220 QModelIndex aIndex = objectIndex(aObject);
221 if (aIndex.isValid())
222 emit dataChanged(aIndex, aIndex);
224 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
225 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aUpdMsg =
226 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
227 DocumentPtr aDoc = aUpdMsg->document();
228 std::string aGroup = aUpdMsg->group();
232 if (aDoc == aRootDoc) {
233 // Update a group under root
234 if (aGroup == myXMLReader.rootType()) // Update objects under root
235 aSartId = foldersCount();
236 else // Update objects in folder under root
237 aParent = createIndex(folderId(aGroup), 0, -1);
239 // Update a sub-document
240 if (aGroup == myXMLReader.subType()) {
241 // Update sub-document root
242 aParent = findDocumentRootIndex(aDoc.get());
243 aSartId = foldersCount(aDoc.get());
245 // update folder in sub-document
246 aParent = createIndex(folderId(aGroup, aDoc.get()), 0, aDoc.get());
248 int aChildNb = rowCount(aParent);
249 // Rebuild all indexes
250 removeRows(aSartId, aChildNb - aSartId, aParent);
251 insertRows(aSartId, aChildNb - aSartId, aParent);
252 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
253 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
254 if (aDoc != aRootDoc) {
255 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
256 if (aDocRoot.isValid())
257 emit dataChanged(aDocRoot, aDocRoot);
260 Events_Error::send("Problem with Data Model definition of sub-document");
266 //******************************************************
267 void XGUI_DataModel::clear()
272 //******************************************************
273 void XGUI_DataModel::rebuildDataTree()
278 //******************************************************
279 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
281 if (theIndex.internalId() < 0) // this is a folder
283 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
284 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
287 return aObj->data()->owner();
290 //******************************************************
291 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
293 std::string aType = theObject->groupName();
294 DocumentPtr aDoc = theObject->document();
295 int aRow = aDoc->index(theObject);
297 // it could be a part of complex object
298 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
299 if (aFeature.get()) {
300 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
301 if (aCompFea.get()) {
302 for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
303 if (aCompFea->subFeature(i, true) == theObject) {
310 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
312 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
313 if (aCompRes.get()) {
314 for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
315 if (aCompRes->subResult(i, true) == theObject) {
324 return QModelIndex();
326 return createIndex(aRow, 0, theObject.get());
328 SessionPtr aSession = ModelAPI_Session::get();
329 DocumentPtr aRootDoc = aSession->moduleDocument();
330 if (aDoc == aRootDoc && myXMLReader.rootType() == aType) {
331 // The object from root document
332 aRow += foldersCount();
333 } else if (myXMLReader.subType() == aType) {
334 // The object from sub document
335 aRow += foldersCount(aDoc.get());
337 return createIndex(aRow, 0, theObject.get());
340 //******************************************************
341 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
343 SessionPtr aSession = ModelAPI_Session::get();
344 DocumentPtr aRootDoc = aSession->moduleDocument();
345 int aNbFolders = foldersCount();
346 int theIndexRow = theIndex.row();
348 if ((theRole == Qt::DecorationRole) && (theIndex == lastHistoryIndex()))
349 return QIcon(":pictures/arrow.png");
351 if (theIndex.column() == 1)
354 int aParentId = theIndex.internalId();
355 if (aParentId == -1) { // root folders
357 case Qt::DisplayRole:
358 return QString(myXMLReader.rootFolderName(theIndexRow).c_str()) +
359 QString(" (%1)").arg(rowCount(theIndex));
360 case Qt::DecorationRole:
361 return QIcon(myXMLReader.rootFolderIcon(theIndexRow).c_str());
362 case Qt::ForegroundRole:
363 if ((flags(theIndex) & Qt::ItemIsEditable) == 0)
364 return QBrush(Qt::lightGray);
367 } else { // an object or sub-document
368 if (theRole == Qt::ForegroundRole) {
369 if ((flags(theIndex) & Qt::ItemIsEditable) == 0)
370 return QBrush(Qt::lightGray);
374 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
375 if (aSubDoc) { // this is a folder of sub document
376 QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
377 int aRow = theIndexRow;
378 while (aMissedIdx.contains(aRow))
382 case Qt::DisplayRole:
383 return QString(myXMLReader.subFolderName(aRow).c_str()) +
384 QString(" (%1)").arg(rowCount(theIndex));
385 case Qt::DecorationRole:
386 return QIcon(myXMLReader.subFolderIcon(aRow).c_str());
389 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
391 case Qt::DisplayRole:
393 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
394 ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
395 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
396 QString aVal = QString::number(aValueAttribute->value());
397 QString aTitle = QString(aObj->data()->name().c_str());
398 return aTitle + " = " + aVal;
401 if (aObj->groupName() == myXMLReader.subType()) {
402 ResultPartPtr aPartRes = getPartResult(aObj);
403 if (aPartRes.get()) {
404 if (aPartRes->partDoc().get() == NULL)
405 aPrefix = "Not loaded ";
408 return aPrefix + aObj->data()->name().c_str();
410 case Qt::DecorationRole:
411 return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
418 //******************************************************
419 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
424 //******************************************************
425 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
427 SessionPtr aSession = ModelAPI_Session::get();
428 if (!aSession->hasModuleDocument())
430 DocumentPtr aRootDoc = aSession->moduleDocument();
432 if (!theParent.isValid()) {
433 // Return number of items in root
434 int aNbFolders = foldersCount();
436 std::string aType = myXMLReader.rootType();
438 aNbItems = aRootDoc->size(aType);
439 return aNbFolders + aNbItems;
442 int aId = theParent.internalId();
444 // this is a folder under root
445 int aParentPos = theParent.row();
446 std::string aType = myXMLReader.rootFolderType(aParentPos);
447 return aRootDoc->size(aType);
449 // It is an object which could have children
450 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
452 // a folder of sub-document
453 QIntList aMissedIdx = missedFolderIndexes(aDoc);
454 int aRow = theParent.row();
455 while (aMissedIdx.contains(aRow))
457 std::string aType = myXMLReader.subFolderType(aRow);
458 return aDoc->size(aType);
460 ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
461 // Check for Part feature
462 ResultPartPtr aPartRes = getPartResult(aObj);
463 if (aPartRes.get()) {
464 DocumentPtr aSubDoc = aPartRes->partDoc();
468 int aNbSubFolders = foldersCount(aSubDoc.get());
470 std::string aSubType = myXMLReader.subType();
471 if (!aSubType.empty())
472 aNbSubItems = aSubDoc->size(aSubType);
473 return aNbSubItems + aNbSubFolders;
475 // Check for composite object
476 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
478 return aCompFeature->numberOfSubs(true);
479 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
481 return aCompRes->numberOfSubs(true);
488 //******************************************************
489 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
494 //******************************************************
495 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
497 SessionPtr aSession = ModelAPI_Session::get();
498 DocumentPtr aRootDoc = aSession->moduleDocument();
499 int aNbFolders = foldersCount();
503 if (!theParent.isValid()) {
504 if (theRow < aNbFolders) // Return first level folder index
505 return createIndex(theRow, theColumn, -1);
506 else { // return object under root index
507 std::string aType = myXMLReader.rootType();
508 int aObjId = theRow - aNbFolders;
509 if (aObjId < aRootDoc->size(aType)) {
510 ObjectPtr aObj = aRootDoc->object(aType, aObjId);
511 aIndex = objectIndex(aObj);
515 int aId = theParent.internalId();
516 int aParentPos = theParent.row();
517 if (aId == -1) { // return object index inside of first level of folders
518 std::string aType = myXMLReader.rootFolderType(aParentPos);
519 if (theRow < aRootDoc->size(aType)) {
520 ObjectPtr aObj = aRootDoc->object(aType, theRow);
521 aIndex = objectIndex(aObj);
524 // It is an object which could have children
525 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
527 // It is a folder of sub-document
528 int aParentRow = aParentPos;
529 QIntList aMissedIdx = missedFolderIndexes(aDoc);
530 while (aMissedIdx.contains(aParentRow))
532 std::string aType = myXMLReader.subFolderType(aParentRow);
533 if (theRow < aDoc->size(aType)) {
534 ObjectPtr aObj = aDoc->object(aType, theRow);
535 aIndex = objectIndex(aObj);
538 ModelAPI_Object* aParentObj = (ModelAPI_Object*)theParent.internalPointer();
540 // Check for Part feature
541 ResultPartPtr aPartRes = getPartResult(aParentObj);
542 if (aPartRes.get()) {
543 DocumentPtr aSubDoc = aPartRes->partDoc();
544 int aNbSubFolders = foldersCount(aSubDoc.get());
545 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
546 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
548 // this is an object under sub document root
549 std::string aType = myXMLReader.subType();
550 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
551 aIndex = objectIndex(aObj);
554 // Check for composite object
555 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
557 aIndex = objectIndex(aCompFeature->subFeature(theRow));
559 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
561 aIndex = objectIndex(aCompRes->subResult(theRow));
568 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
572 //******************************************************
573 static QModelIndex MYLastDeleted;
574 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
576 // To avoid additional request about index which was already deleted
577 if (theIndex == MYLastDeleted)
578 return QModelIndex();
580 int aId = theIndex.internalId();
581 if (aId != -1) { // The object is not a root folder
582 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
584 // It is a folder of sub-document
585 return findDocumentRootIndex(aDoc);
587 ObjectPtr aObj = object(theIndex);
589 // To avoid additional request about index which was already deleted
590 // If deleted it causes a crash on delete object from Part
591 MYLastDeleted = theIndex;
592 return QModelIndex();
594 // Check is it object a sub-object of a complex object
595 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
596 if (aFeature.get()) {
597 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
598 if (aCompFea.get()) {
599 return objectIndex(aCompFea);
602 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
604 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
605 if (aCompRes.get()) {
606 return objectIndex(aCompRes);
609 // Use as ordinary object
610 std::string aType = aObj->groupName();
611 SessionPtr aSession = ModelAPI_Session::get();
612 DocumentPtr aRootDoc = aSession->moduleDocument();
613 DocumentPtr aSubDoc = aObj->document();
614 if (aSubDoc == aRootDoc) {
615 if (aType == myXMLReader.rootType())
616 return QModelIndex();
618 // return first level of folder index
619 int aFolderId = myXMLReader.rootFolderId(aType);
620 // Items in a one row must have the same parent
621 return createIndex(aFolderId, 0, -1);
624 if (aType == myXMLReader.subType())
625 return findDocumentRootIndex(aSubDoc.get());
627 // return first level of folder index
628 int aFolderId = myXMLReader.subFolderId(aType);
629 // Items in a one row must have the same parent
630 return createIndex(aFolderId, 0, aSubDoc.get());
634 return QModelIndex();
637 //******************************************************
638 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
640 return rowCount(theParent) > 0;
643 //******************************************************
644 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
646 beginInsertRows(theParent, theRow, theRow + theCount - 1);
652 //******************************************************
653 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
655 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
660 //******************************************************
661 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
663 qint64 aIt = theIndex.internalId();
664 ModelAPI_Object* aObj = 0;
665 ModelAPI_Document* aDoc = 0;
666 SessionPtr aSession = ModelAPI_Session::get();
667 DocumentPtr aActiveDoc = aSession->activeDocument();
669 Qt::ItemFlags aNullFlag;
670 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
671 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
675 // Folders under root
676 DocumentPtr aRootDoc = aSession->moduleDocument();
677 if (aRootDoc != aActiveDoc)
680 aDoc = getSubDocument(theIndex.internalPointer());
682 aObj = (ModelAPI_Object*) theIndex.internalPointer();
687 if (aObj->isDisabled())
688 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
690 bool isCompositeSub = false;
691 // An object which is sub-object of a composite object can not be accessible in column 1
692 if (theIndex.column() == 1) {
693 ObjectPtr aObjPtr = aObj->data()->owner();
694 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
695 if (aFeature.get()) {
696 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
698 isCompositeSub = true;
700 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
702 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
704 isCompositeSub = true;
709 return Qt::ItemIsSelectable;
711 if (aObj->document() != aActiveDoc) {
712 // The object could be a root of sub-tree
713 ResultPartPtr aPartRes = getPartResult(aObj);
714 if (aPartRes.get()) {
715 if (aPartRes->partDoc() == aActiveDoc)
721 // A folder under sub-document
722 if (aActiveDoc.get() != aDoc)
728 //******************************************************
729 QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
731 SessionPtr aSession = ModelAPI_Session::get();
732 DocumentPtr aRootDoc = aSession->moduleDocument();
733 if (myXMLReader.isAttachToResult()) { // If document is attached to result
734 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
736 ResultPartPtr aPartRes;
737 for (int i = 0; i < aNb; i++) {
738 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
739 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
740 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
742 if (myXMLReader.rootType() == ModelAPI_Feature::group()) {
743 aRow += foldersCount();
745 return createIndex(aRow, 0, aObj.get());
748 } else { // If document is attached to feature
749 int aNb = aRootDoc->size(ModelAPI_Feature::group());
751 ResultPartPtr aPartRes;
752 for (int i = 0; i < aNb; i++) {
753 aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
754 aPartRes = getPartResult(aObj.get());
755 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
757 if (myXMLReader.rootType() == ModelAPI_Feature::group())
758 aRow += foldersCount();
759 return createIndex(aRow, 0, aObj.get());
763 return QModelIndex();
766 //******************************************************
767 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
769 SessionPtr aSession = ModelAPI_Session::get();
770 DocumentPtr aRootDoc = aSession->moduleDocument();
771 if (theDoc == aRootDoc)
772 return QModelIndex();
774 return findDocumentRootIndex(theDoc.get());
777 //******************************************************
778 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
781 SessionPtr aSession = ModelAPI_Session::get();
782 DocumentPtr aRootDoc = aSession->moduleDocument();
783 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
784 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
785 if (myXMLReader.rootShowEmpty(i))
788 if (aRootDoc->size(myXMLReader.rootFolderType(i)) > 0)
793 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
794 if (myXMLReader.subShowEmpty(i))
797 if (theDoc->size(myXMLReader.subFolderType(i)) > 0)
806 //******************************************************
807 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
810 SessionPtr aSession = ModelAPI_Session::get();
811 DocumentPtr aRootDoc = aSession->moduleDocument();
812 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
813 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
814 if (!myXMLReader.rootShowEmpty(i)) {
815 if (aRootDoc->size(myXMLReader.rootFolderType(i)) == 0)
820 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
821 if (!myXMLReader.subShowEmpty(i)) {
822 if (theDoc->size(myXMLReader.subFolderType(i)) == 0)
831 //******************************************************
832 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
836 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
837 if (!myXMLReader.rootShowEmpty(i))
838 aResult << myXMLReader.rootFolderType(i).c_str();
841 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
842 if (!myXMLReader.subShowEmpty(i))
843 aResult << myXMLReader.subFolderType(i).c_str();
849 //******************************************************
850 QModelIndex XGUI_DataModel::lastHistoryIndex() const
852 SessionPtr aSession = ModelAPI_Session::get();
853 DocumentPtr aCurDoc = aSession->activeDocument();
854 FeaturePtr aFeature = aCurDoc->currentFeature(true);
855 if (aFeature.get()) {
856 QModelIndex aInd = objectIndex(aFeature);
857 return createIndex(aInd.row(), 1, aInd.internalPointer());
859 if (aCurDoc == aSession->moduleDocument())
860 return createIndex(foldersCount() - 1, 1, -1);
862 return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());
866 //******************************************************
867 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc)
869 SessionPtr aSession = ModelAPI_Session::get();
870 ModelAPI_Document* aDoc = theDoc;
872 aDoc = aSession->moduleDocument().get();
874 bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
878 int aId = myXMLReader.subFolderId(theType);
880 for (int i = 0; i < aId; i++) {
881 if (!myXMLReader.subShowEmpty(i)) {
882 if (aDoc->size(myXMLReader.subFolderType(i)) == 0)
887 int aId = myXMLReader.rootFolderId(theType);
889 for (int i = 0; i < aId; i++) {
890 if (!myXMLReader.rootShowEmpty(i)) {
891 if (aDoc->size(myXMLReader.rootFolderType(i)) == 0)