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>
22 #include <Config_DataModelReader.h>
24 #include <Events_Loop.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
36 #define SELECTABLE_COLOR QColor(80, 80, 80)
37 #define DISABLED_COLOR QColor(200, 200, 200)
39 ResultPartPtr getPartResult(ModelAPI_Object* theObj)
41 ModelAPI_Feature* aFeature = dynamic_cast<ModelAPI_Feature*>(theObj);
43 ResultPtr aRes = aFeature->firstResult();
44 if (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group())) {
45 ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aRes);
46 // Use only original parts, not a placement results
47 if (aPartRes == aPartRes->original())
51 return ResultPartPtr();
54 /// Returns pointer on document if the given object is document object
55 ModelAPI_Document* getSubDocument(void* theObj)
57 ModelAPI_Document* aDoc = dynamic_cast<ModelAPI_Document*>((ModelAPI_Entity*)theObj);
64 // Constructor *************************************************
65 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : QAbstractItemModel(theParent)
67 Events_Loop* aLoop = Events_Loop::loop();
68 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
69 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
70 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
71 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_ORDER_UPDATED));
72 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
75 XGUI_DataModel::~XGUI_DataModel()
79 //******************************************************
80 void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
82 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
83 std::string aRootType = myXMLReader->rootType();
84 std::string aSubType = myXMLReader->subType();
85 int aNbFolders = foldersCount();
87 // Created object event *******************
88 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
89 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
90 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
91 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
93 std::set<ObjectPtr>::const_iterator aIt;
95 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
96 ObjectPtr aObject = (*aIt);
97 // We do not show objects which does not need to be shown in object browser
98 if (!aObject->isInHistory())
101 aObjType = aObject->groupName();
102 DocumentPtr aDoc = aObject->document();
103 if (aDoc == aRootDoc) {
104 // Check that new folders could appear
105 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
106 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
107 if ((aNotEmptyFolder.toStdString() == aObjType) && (aRootDoc->size(aObjType) == 1))
108 // Appears first object in folder which can not be shown empty
109 insertRow(myXMLReader->rootFolderId(aObjType));
112 int aRow = aRootDoc->size(aObjType) - 1;
114 if (aObjType == aRootType) {
115 insertRow(aRow + aNbFolders + 1);
117 int aFolderId = myXMLReader->rootFolderId(aObjType);
118 if (aFolderId != -1) {
119 insertRow(aRow, createIndex(aFolderId, 0, -1));
124 // Object created in sub-document
125 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
126 if (aDocRoot.isValid()) {
127 // Check that new folders could appear
128 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
129 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
130 if ((aNotEmptyFolder.toStdString() == aObjType) && (aDoc->size(aObjType) == 1))
131 // Appears first object in folder which can not be shown empty
132 insertRow(myXMLReader->subFolderId(aObjType), aDocRoot);
134 int aRow = aDoc->index(aObject);
136 int aNbSubFolders = foldersCount(aDoc.get());
137 if (aObjType == aSubType) {
138 // List of objects under document root
139 insertRow(aRow + aNbSubFolders, aDocRoot);
141 // List of objects under a folder
143 int aFolderId = folderId(aObjType, aDoc.get());
144 if (aFolderId != -1) {
145 QModelIndex aParentFolder = createIndex(aFolderId, 0, aDoc.get());
146 insertRow(aRow, aParentFolder);
147 emit dataChanged(aParentFolder, aParentFolder);
158 // Deleted object event ***********************
159 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
160 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
161 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
162 DocumentPtr aDoc = aUpdMsg->document();
163 std::set<std::string> aMsgGroups = aUpdMsg->groups();
165 /// Sort groups because RootType deletion has to be done after others
166 std::string aType = (aDoc == aRootDoc)? aRootType : aSubType;
167 std::list<std::string> aGroups;
168 std::set<std::string>::const_iterator aSetIt;
169 for (aSetIt = aMsgGroups.begin(); aSetIt != aMsgGroups.end(); ++aSetIt) {
170 std::string aGroup = (*aSetIt);
172 aGroups.push_back(aGroup);
174 aGroups.push_front(aGroup);
177 std::list<std::string>::const_iterator aIt;
178 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
179 std::string aGroup = (*aIt);
180 if (aDoc == aRootDoc) { // If root objects
181 int aRow = aRootDoc->size(aGroup);
182 if (aGroup == aRootType) {
183 // Process root folder
184 removeRow(aRow + aNbFolders);
185 rebuildBranch(aNbFolders, aRow);
187 // Process root sub-folder
188 int aFolderId = myXMLReader->rootFolderId(aGroup);
189 if (aFolderId != -1) {
190 QModelIndex aFolderIndex = createIndex(aFolderId, 0, -1);
191 removeRow(aRow, aFolderIndex);
192 //rebuildBranch(0, aRow);
195 // Check that some folders could erased
196 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
197 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
198 if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup) == 0)) {
199 // Appears first object in folder which can not be shown empty
200 removeRow(myXMLReader->rootFolderId(aGroup));
201 //rebuildBranch(0, aNbFolders + aDoc->size(myXMLReader->rootType()));
206 // Remove row for sub-document
207 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
208 if (aDocRoot.isValid()) {
209 int aRow = aDoc->size(aGroup);
210 int aNbSubFolders = foldersCount(aDoc.get());
211 if (aGroup == aSubType) {
212 // List of objects under document root
213 removeRow(aRow + aNbSubFolders, aDocRoot);
214 rebuildBranch(aNbSubFolders, aRow, aDocRoot);
216 // List of objects under a folder
217 int aFolderId = folderId(aGroup, aDoc.get());
218 if (aFolderId != -1) {
219 QModelIndex aFolderRoot = createIndex(aFolderId, 0, aDoc.get());
220 removeRow(aRow, aFolderRoot);
221 //rebuildBranch(0, aRow, aFolderRoot);
224 // Check that some folders could disappear
225 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
226 int aSize = aDoc->size(aGroup);
227 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
228 if ((aNotEmptyFolder.toStdString() == aGroup) && (aSize == 0)) {
229 // Appears first object in folder which can not be shown empty
230 removeRow(myXMLReader->subFolderId(aGroup), aDocRoot);
231 //rebuildBranch(0, aNbSubFolders + aDoc->size(myXMLReader->subType()), aDocRoot);
241 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
242 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
243 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
244 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
246 std::set<ObjectPtr>::const_iterator aIt;
247 std::string aObjType;
248 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
249 ObjectPtr aObject = (*aIt);
250 if (aObject->data()->isValid()) {
251 QModelIndex aIndex = objectIndex(aObject);
252 if (aIndex.isValid()) {
253 emit dataChanged(aIndex, aIndex);
260 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
261 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aUpdMsg =
262 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
263 if (aUpdMsg->reordered().get()) {
264 DocumentPtr aDoc = aUpdMsg->reordered()->document();
265 std::string aGroup = aUpdMsg->reordered()->group();
269 if (aDoc == aRootDoc) {
270 // Update a group under root
271 if (aGroup == myXMLReader->rootType()) // Update objects under root
272 aStartId = foldersCount();
273 else // Update objects in folder under root
274 aParent = createIndex(folderId(aGroup), 0, -1);
276 // Update a sub-document
277 if (aGroup == myXMLReader->subType()) {
278 // Update sub-document root
279 aParent = findDocumentRootIndex(aDoc.get());
280 aStartId = foldersCount(aDoc.get());
282 // update folder in sub-document
283 aParent = createIndex(folderId(aGroup, aDoc.get()), 0, aDoc.get());
285 int aChildNb = rowCount(aParent);
286 rebuildBranch(aStartId, aChildNb - aStartId, aParent);
290 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
291 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
292 if (aDoc != aRootDoc) {
293 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
294 if (aDocRoot.isValid())
295 emit dataChanged(aDocRoot, aDocRoot);
297 // We have got a new document
303 //******************************************************
304 void XGUI_DataModel::clear()
309 //******************************************************
310 void XGUI_DataModel::rebuildDataTree()
317 //******************************************************
318 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
320 if (theIndex.internalId() < 0) // this is a folder
322 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
323 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
326 return aObj->data()->owner();
329 //******************************************************
330 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
332 std::string aType = theObject->groupName();
333 DocumentPtr aDoc = theObject->document();
334 int aRow = aDoc->index(theObject);
336 // it could be a part of complex object
337 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
338 if (aFeature.get()) {
339 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
340 if (aCompFea.get()) {
341 for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
342 if (aCompFea->subFeature(i, true) == theObject) {
349 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
351 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
352 if (aCompRes.get()) {
353 for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
354 if (aCompRes->subResult(i, true) == theObject) {
363 return QModelIndex();
365 return createIndex(aRow, 0, theObject.get());
367 SessionPtr aSession = ModelAPI_Session::get();
368 DocumentPtr aRootDoc = aSession->moduleDocument();
369 if (aDoc == aRootDoc && myXMLReader->rootType() == aType) {
370 // The object from root document
371 aRow += foldersCount();
372 } else if (myXMLReader->subType() == aType) {
373 // The object from sub document
374 aRow += foldersCount(aDoc.get());
376 return createIndex(aRow, 0, theObject.get());
379 //******************************************************
380 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
382 SessionPtr aSession = ModelAPI_Session::get();
383 DocumentPtr aRootDoc = aSession->moduleDocument();
384 int aNbFolders = foldersCount();
385 int theIndexRow = theIndex.row();
387 if ((theRole == Qt::DecorationRole) && (theIndex == lastHistoryIndex()))
388 return QIcon(":pictures/arrow.png");
390 if (theIndex.column() == 1)
393 int aParentId = theIndex.internalId();
394 if (aParentId == -1) { // root folders
396 case Qt::DisplayRole:
397 return QString(myXMLReader->rootFolderName(theIndexRow).c_str()) +
398 QString(" (%1)").arg(rowCount(theIndex));
399 case Qt::DecorationRole:
400 return QIcon(myXMLReader->rootFolderIcon(theIndexRow).c_str());
401 case Qt::ForegroundRole:
403 Qt::ItemFlags aFlags = theIndex.flags();
404 if (aFlags == Qt::ItemFlags())
405 return QBrush(DISABLED_COLOR);
406 if (!aFlags.testFlag(Qt::ItemIsEditable))
407 return QBrush(SELECTABLE_COLOR);
411 } else { // an object or sub-document
412 if (theRole == Qt::ForegroundRole) {
413 Qt::ItemFlags aFlags = theIndex.flags();
414 if (aFlags == Qt::ItemFlags())
415 return QBrush(DISABLED_COLOR);
416 if (!aFlags.testFlag(Qt::ItemIsEditable))
417 return QBrush(SELECTABLE_COLOR);
421 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
422 if (aSubDoc) { // this is a folder of sub document
423 QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
424 int aRow = theIndexRow;
425 while (aMissedIdx.contains(aRow))
429 case Qt::DisplayRole:
430 return QString(myXMLReader->subFolderName(aRow).c_str()) +
431 QString(" (%1)").arg(rowCount(theIndex));
432 case Qt::DecorationRole:
433 return QIcon(myXMLReader->subFolderIcon(aRow).c_str());
436 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
438 case Qt::DisplayRole:
440 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
441 ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
442 AttributeDoublePtr aValueAttribute =
443 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
444 QString aVal = QString::number(aValueAttribute->value());
445 QString aTitle = QString(aObj->data()->name().c_str());
446 return aTitle + " = " + aVal;
449 if (aObj->groupName() == myXMLReader->subType()) {
450 ResultPartPtr aPartRes = getPartResult(aObj);
451 if (aPartRes.get()) {
452 if (aPartRes->partDoc().get() == NULL)
453 aSuffix = " (Not loaded)";
456 return aObj->data()->name().c_str() + aSuffix;
458 case Qt::DecorationRole:
459 return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
466 //******************************************************
467 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
472 //******************************************************
473 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
475 SessionPtr aSession = ModelAPI_Session::get();
476 if (!aSession->hasModuleDocument())
478 DocumentPtr aRootDoc = aSession->moduleDocument();
480 if (!theParent.isValid()) {
481 // Return number of items in root
482 int aNbFolders = foldersCount();
484 std::string aType = myXMLReader->rootType();
486 aNbItems = aRootDoc->size(aType);
487 return aNbFolders + aNbItems;
490 int aId = theParent.internalId();
492 // this is a folder under root
493 int aParentPos = theParent.row();
494 std::string aType = myXMLReader->rootFolderType(aParentPos);
495 return aRootDoc->size(aType);
497 // It is an object which could have children
498 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
500 // a folder of sub-document
501 QIntList aMissedIdx = missedFolderIndexes(aDoc);
502 int aRow = theParent.row();
503 while (aMissedIdx.contains(aRow))
505 std::string aType = myXMLReader->subFolderType(aRow);
506 return aDoc->size(aType);
508 ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
509 // Check for Part feature
510 ResultPartPtr aPartRes = getPartResult(aObj);
511 if (aPartRes.get()) {
512 DocumentPtr aSubDoc = aPartRes->partDoc();
516 int aNbSubFolders = foldersCount(aSubDoc.get());
518 std::string aSubType = myXMLReader->subType();
519 if (!aSubType.empty())
520 aNbSubItems = aSubDoc->size(aSubType);
521 return aNbSubItems + aNbSubFolders;
523 // Check for composite object
524 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
526 return aCompFeature->numberOfSubs(true);
527 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
529 return aCompRes->numberOfSubs(true);
536 //******************************************************
537 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
542 //******************************************************
543 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
545 SessionPtr aSession = ModelAPI_Session::get();
546 DocumentPtr aRootDoc = aSession->moduleDocument();
547 int aNbFolders = foldersCount();
551 if (!theParent.isValid()) {
552 if (theRow < aNbFolders) // Return first level folder index
553 return createIndex(theRow, theColumn, -1);
554 else { // return object under root index
555 std::string aType = myXMLReader->rootType();
556 int aObjId = theRow - aNbFolders;
557 if (aObjId < aRootDoc->size(aType)) {
558 ObjectPtr aObj = aRootDoc->object(aType, aObjId);
559 aIndex = objectIndex(aObj);
563 int aId = theParent.internalId();
564 int aParentPos = theParent.row();
565 if (aId == -1) { // return object index inside of first level of folders
566 std::string aType = myXMLReader->rootFolderType(aParentPos);
567 if (theRow < aRootDoc->size(aType)) {
568 ObjectPtr aObj = aRootDoc->object(aType, theRow);
569 aIndex = objectIndex(aObj);
572 // It is an object which could have children
573 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
575 // It is a folder of sub-document
576 int aParentRow = aParentPos;
577 QIntList aMissedIdx = missedFolderIndexes(aDoc);
578 while (aMissedIdx.contains(aParentRow))
580 std::string aType = myXMLReader->subFolderType(aParentRow);
581 if (theRow < aDoc->size(aType)) {
582 ObjectPtr aObj = aDoc->object(aType, theRow);
583 aIndex = objectIndex(aObj);
586 ModelAPI_Object* aParentObj = (ModelAPI_Object*)theParent.internalPointer();
588 // Check for Part feature
589 ResultPartPtr aPartRes = getPartResult(aParentObj);
590 if (aPartRes.get()) {
591 DocumentPtr aSubDoc = aPartRes->partDoc();
592 int aNbSubFolders = foldersCount(aSubDoc.get());
593 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
594 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
596 // this is an object under sub document root
597 std::string aType = myXMLReader->subType();
598 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
599 aIndex = objectIndex(aObj);
602 // Check for composite object
603 ModelAPI_CompositeFeature* aCompFeature =
604 dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
606 aIndex = objectIndex(aCompFeature->subFeature(theRow));
608 ModelAPI_ResultCompSolid* aCompRes =
609 dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
611 aIndex = objectIndex(aCompRes->subResult(theRow));
618 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
622 //******************************************************
623 static QModelIndex MYLastDeleted;
624 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
626 if (!theIndex.isValid())
627 return QModelIndex();
628 // To avoid additional request about index which was already deleted
629 if (theIndex == MYLastDeleted)
630 return QModelIndex();
632 int aId = theIndex.internalId();
633 if (aId != -1) { // The object is not a root folder
634 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
636 // It is a folder of sub-document
637 return findDocumentRootIndex(aDoc);
639 ObjectPtr aObj = object(theIndex);
641 // To avoid additional request about index which was already deleted
642 // If deleted it causes a crash on delete object from Part
643 MYLastDeleted = theIndex;
644 return QModelIndex();
646 // Check is it object a sub-object of a complex object
647 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
648 if (aFeature.get()) {
649 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
650 if (aCompFea.get()) {
651 return objectIndex(aCompFea);
654 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
656 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
657 if (aCompRes.get()) {
658 return objectIndex(aCompRes);
661 // Use as ordinary object
662 std::string aType = aObj->groupName();
663 SessionPtr aSession = ModelAPI_Session::get();
664 DocumentPtr aRootDoc = aSession->moduleDocument();
665 DocumentPtr aSubDoc = aObj->document();
666 if (aSubDoc == aRootDoc) {
667 if (aType == myXMLReader->rootType())
668 return QModelIndex();
670 // return first level of folder index
671 int aFolderId = myXMLReader->rootFolderId(aType);
672 // Items in a one row must have the same parent
673 return createIndex(aFolderId, 0, -1);
676 if (aType == myXMLReader->subType())
677 return findDocumentRootIndex(aSubDoc.get());
679 // return first level of folder index
680 int aFolderId = myXMLReader->subFolderId(aType);
681 // Items in a one row must have the same parent
682 return createIndex(aFolderId, 0, aSubDoc.get());
686 return QModelIndex();
689 //******************************************************
690 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
692 return rowCount(theParent) > 0;
695 //******************************************************
696 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
698 beginInsertRows(theParent, theRow, theRow + theCount - 1);
704 //******************************************************
705 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
707 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
712 //******************************************************
713 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
715 qint64 aIt = theIndex.internalId();
716 ModelAPI_Object* aObj = 0;
717 ModelAPI_Document* aDoc = 0;
718 SessionPtr aSession = ModelAPI_Session::get();
719 DocumentPtr aActiveDoc = aSession->activeDocument();
721 Qt::ItemFlags aNullFlag;
722 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
723 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
727 // Folders under root
728 DocumentPtr aRootDoc = aSession->moduleDocument();
729 if (aRootDoc != aActiveDoc)
732 aDoc = getSubDocument(theIndex.internalPointer());
734 aObj = (ModelAPI_Object*) theIndex.internalPointer();
739 if (aObj->isDisabled())
740 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
742 if (aSession->moduleDocument() != aObj->document())
743 if (aActiveDoc != aObj->document())
744 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
746 bool isCompositeSub = false;
747 // An object which is sub-object of a composite object can not be accessible in column 1
748 if (theIndex.column() == 1) {
749 ObjectPtr aObjPtr = aObj->data()->owner();
750 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
751 if (aFeature.get()) {
752 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
754 isCompositeSub = true;
756 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
758 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
760 isCompositeSub = true;
765 return Qt::ItemIsSelectable;
767 if (aObj->document() != aActiveDoc) {
768 // The object could be a root of sub-tree
769 ResultPartPtr aPartRes = getPartResult(aObj);
770 if (aPartRes.get()) {
771 if (aPartRes->partDoc() == aActiveDoc)
777 // A folder under sub-document
778 if (aActiveDoc.get() != aDoc)
784 //******************************************************
785 QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
787 SessionPtr aSession = ModelAPI_Session::get();
788 DocumentPtr aRootDoc = aSession->moduleDocument();
789 if (myXMLReader->isAttachToResult()) { // If document is attached to result
790 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
792 ResultPartPtr aPartRes;
793 for (int i = 0; i < aNb; i++) {
794 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
795 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
796 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
798 if (myXMLReader->rootType() == ModelAPI_Feature::group()) {
799 aRow += foldersCount();
801 return createIndex(aRow, 0, aObj.get());
804 } else { // If document is attached to feature
805 int aNb = aRootDoc->size(ModelAPI_Feature::group());
807 ResultPartPtr aPartRes;
808 for (int i = 0; i < aNb; i++) {
809 aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
810 aPartRes = getPartResult(aObj.get());
811 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
813 if (myXMLReader->rootType() == ModelAPI_Feature::group())
814 aRow += foldersCount();
815 return createIndex(aRow, 0, aObj.get());
819 return QModelIndex();
822 //******************************************************
823 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
825 SessionPtr aSession = ModelAPI_Session::get();
826 DocumentPtr aRootDoc = aSession->moduleDocument();
827 if (theDoc == aRootDoc)
828 return QModelIndex();
830 return findDocumentRootIndex(theDoc.get());
833 //******************************************************
834 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
837 SessionPtr aSession = ModelAPI_Session::get();
838 DocumentPtr aRootDoc = aSession->moduleDocument();
839 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
840 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
841 if (myXMLReader->rootShowEmpty(i))
844 if (aRootDoc->size(myXMLReader->rootFolderType(i)) > 0)
849 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
850 if (myXMLReader->subShowEmpty(i))
853 if (theDoc->size(myXMLReader->subFolderType(i)) > 0)
862 //******************************************************
863 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
866 SessionPtr aSession = ModelAPI_Session::get();
867 DocumentPtr aRootDoc = aSession->moduleDocument();
868 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
869 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
870 if (!myXMLReader->rootShowEmpty(i)) {
871 if (aRootDoc->size(myXMLReader->rootFolderType(i)) == 0)
876 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
877 if (!myXMLReader->subShowEmpty(i)) {
878 if (theDoc->size(myXMLReader->subFolderType(i)) == 0)
887 //******************************************************
888 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
892 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
893 if (!myXMLReader->rootShowEmpty(i))
894 aResult << myXMLReader->rootFolderType(i).c_str();
897 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
898 if (!myXMLReader->subShowEmpty(i))
899 aResult << myXMLReader->subFolderType(i).c_str();
905 //******************************************************
906 QModelIndex XGUI_DataModel::lastHistoryIndex() const
908 SessionPtr aSession = ModelAPI_Session::get();
909 DocumentPtr aCurDoc = aSession->activeDocument();
910 FeaturePtr aFeature = aCurDoc->currentFeature(true);
911 if (aFeature.get()) {
912 QModelIndex aInd = objectIndex(aFeature);
913 return createIndex(aInd.row(), 1, aInd.internalPointer());
915 if (aCurDoc == aSession->moduleDocument())
916 return createIndex(foldersCount() - 1, 1, -1);
918 return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());
922 //******************************************************
923 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc)
925 SessionPtr aSession = ModelAPI_Session::get();
926 ModelAPI_Document* aDoc = theDoc;
928 aDoc = aSession->moduleDocument().get();
930 bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
934 int aId = myXMLReader->subFolderId(theType);
936 for (int i = 0; i < aId; i++) {
937 if (!myXMLReader->subShowEmpty(i)) {
938 if (aDoc->size(myXMLReader->subFolderType(i)) == 0)
943 int aId = myXMLReader->rootFolderId(theType);
945 for (int i = 0; i < aId; i++) {
946 if (!myXMLReader->rootShowEmpty(i)) {
947 if (aDoc->size(myXMLReader->rootFolderType(i)) == 0)
955 //******************************************************
956 void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex& theParent)
959 removeRows(theRow, theCount, theParent);
960 insertRows(theRow, theCount, theParent);