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 = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
443 QString aVal = QString::number(aValueAttribute->value());
444 QString aTitle = QString(aObj->data()->name().c_str());
445 return aTitle + " = " + aVal;
448 if (aObj->groupName() == myXMLReader->subType()) {
449 ResultPartPtr aPartRes = getPartResult(aObj);
450 if (aPartRes.get()) {
451 if (aPartRes->partDoc().get() == NULL)
452 aSuffix = " (Not loaded)";
455 return aObj->data()->name().c_str() + aSuffix;
457 case Qt::DecorationRole:
458 return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
465 //******************************************************
466 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
471 //******************************************************
472 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
474 SessionPtr aSession = ModelAPI_Session::get();
475 if (!aSession->hasModuleDocument())
477 DocumentPtr aRootDoc = aSession->moduleDocument();
479 if (!theParent.isValid()) {
480 // Return number of items in root
481 int aNbFolders = foldersCount();
483 std::string aType = myXMLReader->rootType();
485 aNbItems = aRootDoc->size(aType);
486 return aNbFolders + aNbItems;
489 int aId = theParent.internalId();
491 // this is a folder under root
492 int aParentPos = theParent.row();
493 std::string aType = myXMLReader->rootFolderType(aParentPos);
494 return aRootDoc->size(aType);
496 // It is an object which could have children
497 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
499 // a folder of sub-document
500 QIntList aMissedIdx = missedFolderIndexes(aDoc);
501 int aRow = theParent.row();
502 while (aMissedIdx.contains(aRow))
504 std::string aType = myXMLReader->subFolderType(aRow);
505 return aDoc->size(aType);
507 ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
508 // Check for Part feature
509 ResultPartPtr aPartRes = getPartResult(aObj);
510 if (aPartRes.get()) {
511 DocumentPtr aSubDoc = aPartRes->partDoc();
515 int aNbSubFolders = foldersCount(aSubDoc.get());
517 std::string aSubType = myXMLReader->subType();
518 if (!aSubType.empty())
519 aNbSubItems = aSubDoc->size(aSubType);
520 return aNbSubItems + aNbSubFolders;
522 // Check for composite object
523 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
525 return aCompFeature->numberOfSubs(true);
526 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
528 return aCompRes->numberOfSubs(true);
535 //******************************************************
536 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
541 //******************************************************
542 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
544 SessionPtr aSession = ModelAPI_Session::get();
545 DocumentPtr aRootDoc = aSession->moduleDocument();
546 int aNbFolders = foldersCount();
550 if (!theParent.isValid()) {
551 if (theRow < aNbFolders) // Return first level folder index
552 return createIndex(theRow, theColumn, -1);
553 else { // return object under root index
554 std::string aType = myXMLReader->rootType();
555 int aObjId = theRow - aNbFolders;
556 if (aObjId < aRootDoc->size(aType)) {
557 ObjectPtr aObj = aRootDoc->object(aType, aObjId);
558 aIndex = objectIndex(aObj);
562 int aId = theParent.internalId();
563 int aParentPos = theParent.row();
564 if (aId == -1) { // return object index inside of first level of folders
565 std::string aType = myXMLReader->rootFolderType(aParentPos);
566 if (theRow < aRootDoc->size(aType)) {
567 ObjectPtr aObj = aRootDoc->object(aType, theRow);
568 aIndex = objectIndex(aObj);
571 // It is an object which could have children
572 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
574 // It is a folder of sub-document
575 int aParentRow = aParentPos;
576 QIntList aMissedIdx = missedFolderIndexes(aDoc);
577 while (aMissedIdx.contains(aParentRow))
579 std::string aType = myXMLReader->subFolderType(aParentRow);
580 if (theRow < aDoc->size(aType)) {
581 ObjectPtr aObj = aDoc->object(aType, theRow);
582 aIndex = objectIndex(aObj);
585 ModelAPI_Object* aParentObj = (ModelAPI_Object*)theParent.internalPointer();
587 // Check for Part feature
588 ResultPartPtr aPartRes = getPartResult(aParentObj);
589 if (aPartRes.get()) {
590 DocumentPtr aSubDoc = aPartRes->partDoc();
591 int aNbSubFolders = foldersCount(aSubDoc.get());
592 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
593 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
595 // this is an object under sub document root
596 std::string aType = myXMLReader->subType();
597 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
598 aIndex = objectIndex(aObj);
601 // Check for composite object
602 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
604 aIndex = objectIndex(aCompFeature->subFeature(theRow));
606 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
608 aIndex = objectIndex(aCompRes->subResult(theRow));
615 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
619 //******************************************************
620 static QModelIndex MYLastDeleted;
621 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
623 if (!theIndex.isValid())
624 return QModelIndex();
625 // To avoid additional request about index which was already deleted
626 if (theIndex == MYLastDeleted)
627 return QModelIndex();
629 int aId = theIndex.internalId();
630 if (aId != -1) { // The object is not a root folder
631 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
633 // It is a folder of sub-document
634 return findDocumentRootIndex(aDoc);
636 ObjectPtr aObj = object(theIndex);
638 // To avoid additional request about index which was already deleted
639 // If deleted it causes a crash on delete object from Part
640 MYLastDeleted = theIndex;
641 return QModelIndex();
643 // Check is it object a sub-object of a complex object
644 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
645 if (aFeature.get()) {
646 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
647 if (aCompFea.get()) {
648 return objectIndex(aCompFea);
651 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
653 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
654 if (aCompRes.get()) {
655 return objectIndex(aCompRes);
658 // Use as ordinary object
659 std::string aType = aObj->groupName();
660 SessionPtr aSession = ModelAPI_Session::get();
661 DocumentPtr aRootDoc = aSession->moduleDocument();
662 DocumentPtr aSubDoc = aObj->document();
663 if (aSubDoc == aRootDoc) {
664 if (aType == myXMLReader->rootType())
665 return QModelIndex();
667 // return first level of folder index
668 int aFolderId = myXMLReader->rootFolderId(aType);
669 // Items in a one row must have the same parent
670 return createIndex(aFolderId, 0, -1);
673 if (aType == myXMLReader->subType())
674 return findDocumentRootIndex(aSubDoc.get());
676 // return first level of folder index
677 int aFolderId = myXMLReader->subFolderId(aType);
678 // Items in a one row must have the same parent
679 return createIndex(aFolderId, 0, aSubDoc.get());
683 return QModelIndex();
686 //******************************************************
687 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
689 return rowCount(theParent) > 0;
692 //******************************************************
693 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
695 beginInsertRows(theParent, theRow, theRow + theCount - 1);
701 //******************************************************
702 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
704 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
709 //******************************************************
710 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
712 qint64 aIt = theIndex.internalId();
713 ModelAPI_Object* aObj = 0;
714 ModelAPI_Document* aDoc = 0;
715 SessionPtr aSession = ModelAPI_Session::get();
716 DocumentPtr aActiveDoc = aSession->activeDocument();
718 Qt::ItemFlags aNullFlag;
719 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
720 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
724 // Folders under root
725 DocumentPtr aRootDoc = aSession->moduleDocument();
726 if (aRootDoc != aActiveDoc)
729 aDoc = getSubDocument(theIndex.internalPointer());
731 aObj = (ModelAPI_Object*) theIndex.internalPointer();
736 if (aObj->isDisabled())
737 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
739 if (aSession->moduleDocument() != aObj->document())
740 if (aActiveDoc != aObj->document())
741 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
743 bool isCompositeSub = false;
744 // An object which is sub-object of a composite object can not be accessible in column 1
745 if (theIndex.column() == 1) {
746 ObjectPtr aObjPtr = aObj->data()->owner();
747 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
748 if (aFeature.get()) {
749 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
751 isCompositeSub = true;
753 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
755 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
757 isCompositeSub = true;
762 return Qt::ItemIsSelectable;
764 if (aObj->document() != aActiveDoc) {
765 // The object could be a root of sub-tree
766 ResultPartPtr aPartRes = getPartResult(aObj);
767 if (aPartRes.get()) {
768 if (aPartRes->partDoc() == aActiveDoc)
774 // A folder under sub-document
775 if (aActiveDoc.get() != aDoc)
781 //******************************************************
782 QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
784 SessionPtr aSession = ModelAPI_Session::get();
785 DocumentPtr aRootDoc = aSession->moduleDocument();
786 if (myXMLReader->isAttachToResult()) { // If document is attached to result
787 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
789 ResultPartPtr aPartRes;
790 for (int i = 0; i < aNb; i++) {
791 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
792 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
793 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
795 if (myXMLReader->rootType() == ModelAPI_Feature::group()) {
796 aRow += foldersCount();
798 return createIndex(aRow, 0, aObj.get());
801 } else { // If document is attached to feature
802 int aNb = aRootDoc->size(ModelAPI_Feature::group());
804 ResultPartPtr aPartRes;
805 for (int i = 0; i < aNb; i++) {
806 aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
807 aPartRes = getPartResult(aObj.get());
808 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
810 if (myXMLReader->rootType() == ModelAPI_Feature::group())
811 aRow += foldersCount();
812 return createIndex(aRow, 0, aObj.get());
816 return QModelIndex();
819 //******************************************************
820 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
822 SessionPtr aSession = ModelAPI_Session::get();
823 DocumentPtr aRootDoc = aSession->moduleDocument();
824 if (theDoc == aRootDoc)
825 return QModelIndex();
827 return findDocumentRootIndex(theDoc.get());
830 //******************************************************
831 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
834 SessionPtr aSession = ModelAPI_Session::get();
835 DocumentPtr aRootDoc = aSession->moduleDocument();
836 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
837 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
838 if (myXMLReader->rootShowEmpty(i))
841 if (aRootDoc->size(myXMLReader->rootFolderType(i)) > 0)
846 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
847 if (myXMLReader->subShowEmpty(i))
850 if (theDoc->size(myXMLReader->subFolderType(i)) > 0)
859 //******************************************************
860 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
863 SessionPtr aSession = ModelAPI_Session::get();
864 DocumentPtr aRootDoc = aSession->moduleDocument();
865 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
866 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
867 if (!myXMLReader->rootShowEmpty(i)) {
868 if (aRootDoc->size(myXMLReader->rootFolderType(i)) == 0)
873 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
874 if (!myXMLReader->subShowEmpty(i)) {
875 if (theDoc->size(myXMLReader->subFolderType(i)) == 0)
884 //******************************************************
885 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
889 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
890 if (!myXMLReader->rootShowEmpty(i))
891 aResult << myXMLReader->rootFolderType(i).c_str();
894 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
895 if (!myXMLReader->subShowEmpty(i))
896 aResult << myXMLReader->subFolderType(i).c_str();
902 //******************************************************
903 QModelIndex XGUI_DataModel::lastHistoryIndex() const
905 SessionPtr aSession = ModelAPI_Session::get();
906 DocumentPtr aCurDoc = aSession->activeDocument();
907 FeaturePtr aFeature = aCurDoc->currentFeature(true);
908 if (aFeature.get()) {
909 QModelIndex aInd = objectIndex(aFeature);
910 return createIndex(aInd.row(), 1, aInd.internalPointer());
912 if (aCurDoc == aSession->moduleDocument())
913 return createIndex(foldersCount() - 1, 1, -1);
915 return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());
919 //******************************************************
920 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc)
922 SessionPtr aSession = ModelAPI_Session::get();
923 ModelAPI_Document* aDoc = theDoc;
925 aDoc = aSession->moduleDocument().get();
927 bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
931 int aId = myXMLReader->subFolderId(theType);
933 for (int i = 0; i < aId; i++) {
934 if (!myXMLReader->subShowEmpty(i)) {
935 if (aDoc->size(myXMLReader->subFolderType(i)) == 0)
940 int aId = myXMLReader->rootFolderId(theType);
942 for (int i = 0; i < aId; i++) {
943 if (!myXMLReader->rootShowEmpty(i)) {
944 if (aDoc->size(myXMLReader->rootFolderType(i)) == 0)
952 //******************************************************
953 void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex& theParent)
956 removeRows(theRow, theCount, theParent);
957 insertRows(theRow, theCount, theParent);