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>
25 #include <Events_Error.h>
30 #define ACTIVE_COLOR Qt::black
31 //#define ACTIVE_COLOR QColor(0,72,140)
32 //#define PASSIVE_COLOR Qt::black
34 /// Returns ResultPart object if the given object is a Part feature
35 /// Otherwise returns NULL
37 #define SELECTABLE_COLOR QColor(80, 80, 80)
38 #define DISABLED_COLOR QColor(200, 200, 200)
40 ResultPartPtr getPartResult(ModelAPI_Object* theObj)
42 ModelAPI_Feature* aFeature = dynamic_cast<ModelAPI_Feature*>(theObj);
44 ResultPtr aRes = aFeature->firstResult();
45 if (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group())) {
46 ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aRes);
47 // Use only original parts, not a placement results
48 if (aPartRes == aPartRes->original())
52 return ResultPartPtr();
55 /// Returns pointer on document if the given object is document object
56 ModelAPI_Document* getSubDocument(void* theObj)
58 ModelAPI_Document* aDoc = dynamic_cast<ModelAPI_Document*>((ModelAPI_Entity*)theObj);
65 // Constructor *************************************************
66 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : QAbstractItemModel(theParent)
68 Events_Loop* aLoop = Events_Loop::loop();
69 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
70 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
71 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
72 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_ORDER_UPDATED));
73 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
76 XGUI_DataModel::~XGUI_DataModel()
80 //******************************************************
81 void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
83 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
84 std::string aRootType = myXMLReader->rootType();
85 std::string aSubType = myXMLReader->subType();
86 int aNbFolders = foldersCount();
88 // Created object event *******************
89 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
90 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
91 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
92 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
94 std::set<ObjectPtr>::const_iterator aIt;
96 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
97 ObjectPtr aObject = (*aIt);
98 // We do not show objects which does not need to be shown in object browser
99 if (!aObject->isInHistory())
102 aObjType = aObject->groupName();
103 DocumentPtr aDoc = aObject->document();
104 if (aDoc == aRootDoc) {
105 // Check that new folders could appear
106 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
107 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
108 if ((aNotEmptyFolder.toStdString() == aObjType) && (aRootDoc->size(aObjType) == 1))
109 // Appears first object in folder which can not be shown empty
110 insertRow(myXMLReader->rootFolderId(aObjType));
113 int aRow = aRootDoc->size(aObjType) - 1;
115 if (aObjType == aRootType) {
116 insertRow(aRow + aNbFolders + 1);
118 int aFolderId = myXMLReader->rootFolderId(aObjType);
119 if (aFolderId != -1) {
120 insertRow(aRow, createIndex(aFolderId, 0, -1));
125 // Object created in sub-document
126 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
127 if (aDocRoot.isValid()) {
128 // Check that new folders could appear
129 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
130 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
131 if ((aNotEmptyFolder.toStdString() == aObjType) && (aDoc->size(aObjType) == 1))
132 // Appears first object in folder which can not be shown empty
133 insertRow(myXMLReader->subFolderId(aObjType), aDocRoot);
135 int aRow = aDoc->index(aObject);
137 int aNbSubFolders = foldersCount(aDoc.get());
138 if (aObjType == aSubType) {
139 // List of objects under document root
140 insertRow(aRow + aNbSubFolders, aDocRoot);
142 // List of objects under a folder
144 int aFolderId = folderId(aObjType, aDoc.get());
145 if (aFolderId != -1) {
146 QModelIndex aParentFolder = createIndex(aFolderId, 0, aDoc.get());
147 insertRow(aRow, aParentFolder);
148 emit dataChanged(aParentFolder, aParentFolder);
157 // Deleted object event ***********************
158 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
159 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
160 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
161 DocumentPtr aDoc = aUpdMsg->document();
162 std::set<std::string> aMsgGroups = aUpdMsg->groups();
164 /// Sort groups because RootType deletion has to be done after others
165 std::string aType = (aDoc == aRootDoc)? aRootType : aSubType;
166 std::list<std::string> aGroups;
167 std::set<std::string>::const_iterator aSetIt;
168 for (aSetIt = aMsgGroups.begin(); aSetIt != aMsgGroups.end(); ++aSetIt) {
169 std::string aGroup = (*aSetIt);
171 aGroups.push_back(aGroup);
173 aGroups.push_front(aGroup);
176 std::list<std::string>::const_iterator aIt;
177 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
178 std::string aGroup = (*aIt);
179 if (aDoc == aRootDoc) { // If root objects
180 int aRow = aRootDoc->size(aGroup);
181 if (aGroup == aRootType) {
182 // Process root folder
183 removeRow(aRow + aNbFolders);
184 rebuildBranch(aNbFolders, aRow);
186 // Process root sub-folder
187 int aFolderId = myXMLReader->rootFolderId(aGroup);
188 if (aFolderId != -1) {
189 QModelIndex aFolderIndex = createIndex(aFolderId, 0, -1);
190 removeRow(aRow, aFolderIndex);
191 //rebuildBranch(0, aRow);
194 // Check that some folders could erased
195 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
196 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
197 if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup) == 0)) {
198 // Appears first object in folder which can not be shown empty
199 removeRow(myXMLReader->rootFolderId(aGroup));
200 //rebuildBranch(0, aNbFolders + aDoc->size(myXMLReader->rootType()));
205 // Remove row for sub-document
206 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
207 if (aDocRoot.isValid()) {
208 int aRow = aDoc->size(aGroup);
209 int aNbSubFolders = foldersCount(aDoc.get());
210 if (aGroup == aSubType) {
211 // List of objects under document root
212 removeRow(aRow + aNbSubFolders, aDocRoot);
213 rebuildBranch(aNbSubFolders, aRow, aDocRoot);
215 // List of objects under a folder
216 int aFolderId = folderId(aGroup, aDoc.get());
217 if (aFolderId != -1) {
218 QModelIndex aFolderRoot = createIndex(aFolderId, 0, aDoc.get());
219 removeRow(aRow, aFolderRoot);
220 //rebuildBranch(0, aRow, aFolderRoot);
223 // Check that some folders could disappear
224 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
225 int aSize = aDoc->size(aGroup);
226 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
227 if ((aNotEmptyFolder.toStdString() == aGroup) && (aSize == 0)) {
228 // Appears first object in folder which can not be shown empty
229 removeRow(myXMLReader->subFolderId(aGroup), aDocRoot);
230 //rebuildBranch(0, aNbSubFolders + aDoc->size(myXMLReader->subType()), aDocRoot);
237 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
238 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
239 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
240 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
242 std::set<ObjectPtr>::const_iterator aIt;
243 std::string aObjType;
244 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
245 ObjectPtr aObject = (*aIt);
246 if (aObject->data()->isValid()) {
247 QModelIndex aIndex = objectIndex(aObject);
248 if (aIndex.isValid()) {
249 emit dataChanged(aIndex, aIndex);
256 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
257 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aUpdMsg =
258 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
259 DocumentPtr aDoc = aUpdMsg->document();
260 std::string aGroup = aUpdMsg->group();
264 if (aDoc == aRootDoc) {
265 // Update a group under root
266 if (aGroup == myXMLReader->rootType()) // Update objects under root
267 aStartId = foldersCount();
268 else // Update objects in folder under root
269 aParent = createIndex(folderId(aGroup), 0, -1);
271 // Update a sub-document
272 if (aGroup == myXMLReader->subType()) {
273 // Update sub-document root
274 aParent = findDocumentRootIndex(aDoc.get());
275 aStartId = foldersCount(aDoc.get());
277 // update folder in sub-document
278 aParent = createIndex(folderId(aGroup, aDoc.get()), 0, aDoc.get());
280 int aChildNb = rowCount(aParent);
281 rebuildBranch(aStartId, aChildNb - aStartId, aParent);
282 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
283 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
284 if (aDoc != aRootDoc) {
285 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
286 if (aDocRoot.isValid())
287 emit dataChanged(aDocRoot, aDocRoot);
289 // We have got a new document
295 //******************************************************
296 void XGUI_DataModel::clear()
301 //******************************************************
302 void XGUI_DataModel::rebuildDataTree()
309 //******************************************************
310 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
312 if (theIndex.internalId() < 0) // this is a folder
314 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
315 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
318 return aObj->data()->owner();
321 //******************************************************
322 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
324 std::string aType = theObject->groupName();
325 DocumentPtr aDoc = theObject->document();
326 int aRow = aDoc->index(theObject);
328 // it could be a part of complex object
329 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
330 if (aFeature.get()) {
331 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
332 if (aCompFea.get()) {
333 for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
334 if (aCompFea->subFeature(i, true) == theObject) {
341 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
343 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
344 if (aCompRes.get()) {
345 for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
346 if (aCompRes->subResult(i, true) == theObject) {
355 return QModelIndex();
357 return createIndex(aRow, 0, theObject.get());
359 SessionPtr aSession = ModelAPI_Session::get();
360 DocumentPtr aRootDoc = aSession->moduleDocument();
361 if (aDoc == aRootDoc && myXMLReader->rootType() == aType) {
362 // The object from root document
363 aRow += foldersCount();
364 } else if (myXMLReader->subType() == aType) {
365 // The object from sub document
366 aRow += foldersCount(aDoc.get());
368 return createIndex(aRow, 0, theObject.get());
371 //******************************************************
372 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
374 SessionPtr aSession = ModelAPI_Session::get();
375 DocumentPtr aRootDoc = aSession->moduleDocument();
376 int aNbFolders = foldersCount();
377 int theIndexRow = theIndex.row();
379 if ((theRole == Qt::DecorationRole) && (theIndex == lastHistoryIndex()))
380 return QIcon(":pictures/arrow.png");
382 if (theIndex.column() == 1)
385 int aParentId = theIndex.internalId();
386 if (aParentId == -1) { // root folders
388 case Qt::DisplayRole:
389 return QString(myXMLReader->rootFolderName(theIndexRow).c_str()) +
390 QString(" (%1)").arg(rowCount(theIndex));
391 case Qt::DecorationRole:
392 return QIcon(myXMLReader->rootFolderIcon(theIndexRow).c_str());
393 case Qt::ForegroundRole:
395 Qt::ItemFlags aFlags = theIndex.flags();
396 if (aFlags == Qt::ItemFlags())
397 return QBrush(DISABLED_COLOR);
398 if (!aFlags.testFlag(Qt::ItemIsEditable))
399 return QBrush(SELECTABLE_COLOR);
403 } else { // an object or sub-document
404 if (theRole == Qt::ForegroundRole) {
405 Qt::ItemFlags aFlags = theIndex.flags();
406 if (aFlags == Qt::ItemFlags())
407 return QBrush(DISABLED_COLOR);
408 if (!aFlags.testFlag(Qt::ItemIsEditable))
409 return QBrush(SELECTABLE_COLOR);
413 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
414 if (aSubDoc) { // this is a folder of sub document
415 QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
416 int aRow = theIndexRow;
417 while (aMissedIdx.contains(aRow))
421 case Qt::DisplayRole:
422 return QString(myXMLReader->subFolderName(aRow).c_str()) +
423 QString(" (%1)").arg(rowCount(theIndex));
424 case Qt::DecorationRole:
425 return QIcon(myXMLReader->subFolderIcon(aRow).c_str());
428 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
430 case Qt::DisplayRole:
432 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
433 ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
434 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
435 QString aVal = QString::number(aValueAttribute->value());
436 QString aTitle = QString(aObj->data()->name().c_str());
437 return aTitle + " = " + aVal;
440 if (aObj->groupName() == myXMLReader->subType()) {
441 ResultPartPtr aPartRes = getPartResult(aObj);
442 if (aPartRes.get()) {
443 if (aPartRes->partDoc().get() == NULL)
444 aSuffix = " (Not loaded)";
447 return aObj->data()->name().c_str() + aSuffix;
449 case Qt::DecorationRole:
450 return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
457 //******************************************************
458 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
463 //******************************************************
464 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
466 SessionPtr aSession = ModelAPI_Session::get();
467 if (!aSession->hasModuleDocument())
469 DocumentPtr aRootDoc = aSession->moduleDocument();
471 if (!theParent.isValid()) {
472 // Return number of items in root
473 int aNbFolders = foldersCount();
475 std::string aType = myXMLReader->rootType();
477 aNbItems = aRootDoc->size(aType);
478 return aNbFolders + aNbItems;
481 int aId = theParent.internalId();
483 // this is a folder under root
484 int aParentPos = theParent.row();
485 std::string aType = myXMLReader->rootFolderType(aParentPos);
486 return aRootDoc->size(aType);
488 // It is an object which could have children
489 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
491 // a folder of sub-document
492 QIntList aMissedIdx = missedFolderIndexes(aDoc);
493 int aRow = theParent.row();
494 while (aMissedIdx.contains(aRow))
496 std::string aType = myXMLReader->subFolderType(aRow);
497 return aDoc->size(aType);
499 ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
500 // Check for Part feature
501 ResultPartPtr aPartRes = getPartResult(aObj);
502 if (aPartRes.get()) {
503 DocumentPtr aSubDoc = aPartRes->partDoc();
507 int aNbSubFolders = foldersCount(aSubDoc.get());
509 std::string aSubType = myXMLReader->subType();
510 if (!aSubType.empty())
511 aNbSubItems = aSubDoc->size(aSubType);
512 return aNbSubItems + aNbSubFolders;
514 // Check for composite object
515 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
517 return aCompFeature->numberOfSubs(true);
518 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
520 return aCompRes->numberOfSubs(true);
527 //******************************************************
528 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
533 //******************************************************
534 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
536 SessionPtr aSession = ModelAPI_Session::get();
537 DocumentPtr aRootDoc = aSession->moduleDocument();
538 int aNbFolders = foldersCount();
542 if (!theParent.isValid()) {
543 if (theRow < aNbFolders) // Return first level folder index
544 return createIndex(theRow, theColumn, -1);
545 else { // return object under root index
546 std::string aType = myXMLReader->rootType();
547 int aObjId = theRow - aNbFolders;
548 if (aObjId < aRootDoc->size(aType)) {
549 ObjectPtr aObj = aRootDoc->object(aType, aObjId);
550 aIndex = objectIndex(aObj);
554 int aId = theParent.internalId();
555 int aParentPos = theParent.row();
556 if (aId == -1) { // return object index inside of first level of folders
557 std::string aType = myXMLReader->rootFolderType(aParentPos);
558 if (theRow < aRootDoc->size(aType)) {
559 ObjectPtr aObj = aRootDoc->object(aType, theRow);
560 aIndex = objectIndex(aObj);
563 // It is an object which could have children
564 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
566 // It is a folder of sub-document
567 int aParentRow = aParentPos;
568 QIntList aMissedIdx = missedFolderIndexes(aDoc);
569 while (aMissedIdx.contains(aParentRow))
571 std::string aType = myXMLReader->subFolderType(aParentRow);
572 if (theRow < aDoc->size(aType)) {
573 ObjectPtr aObj = aDoc->object(aType, theRow);
574 aIndex = objectIndex(aObj);
577 ModelAPI_Object* aParentObj = (ModelAPI_Object*)theParent.internalPointer();
579 // Check for Part feature
580 ResultPartPtr aPartRes = getPartResult(aParentObj);
581 if (aPartRes.get()) {
582 DocumentPtr aSubDoc = aPartRes->partDoc();
583 int aNbSubFolders = foldersCount(aSubDoc.get());
584 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
585 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
587 // this is an object under sub document root
588 std::string aType = myXMLReader->subType();
589 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
590 aIndex = objectIndex(aObj);
593 // Check for composite object
594 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
596 aIndex = objectIndex(aCompFeature->subFeature(theRow));
598 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
600 aIndex = objectIndex(aCompRes->subResult(theRow));
607 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
611 //******************************************************
612 static QModelIndex MYLastDeleted;
613 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
615 if (!theIndex.isValid())
616 return QModelIndex();
617 // To avoid additional request about index which was already deleted
618 if (theIndex == MYLastDeleted)
619 return QModelIndex();
621 int aId = theIndex.internalId();
622 if (aId != -1) { // The object is not a root folder
623 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
625 // It is a folder of sub-document
626 return findDocumentRootIndex(aDoc);
628 ObjectPtr aObj = object(theIndex);
630 // To avoid additional request about index which was already deleted
631 // If deleted it causes a crash on delete object from Part
632 MYLastDeleted = theIndex;
633 return QModelIndex();
635 // Check is it object a sub-object of a complex object
636 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
637 if (aFeature.get()) {
638 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
639 if (aCompFea.get()) {
640 return objectIndex(aCompFea);
643 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
645 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
646 if (aCompRes.get()) {
647 return objectIndex(aCompRes);
650 // Use as ordinary object
651 std::string aType = aObj->groupName();
652 SessionPtr aSession = ModelAPI_Session::get();
653 DocumentPtr aRootDoc = aSession->moduleDocument();
654 DocumentPtr aSubDoc = aObj->document();
655 if (aSubDoc == aRootDoc) {
656 if (aType == myXMLReader->rootType())
657 return QModelIndex();
659 // return first level of folder index
660 int aFolderId = myXMLReader->rootFolderId(aType);
661 // Items in a one row must have the same parent
662 return createIndex(aFolderId, 0, -1);
665 if (aType == myXMLReader->subType())
666 return findDocumentRootIndex(aSubDoc.get());
668 // return first level of folder index
669 int aFolderId = myXMLReader->subFolderId(aType);
670 // Items in a one row must have the same parent
671 return createIndex(aFolderId, 0, aSubDoc.get());
675 return QModelIndex();
678 //******************************************************
679 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
681 return rowCount(theParent) > 0;
684 //******************************************************
685 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
687 beginInsertRows(theParent, theRow, theRow + theCount - 1);
693 //******************************************************
694 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
696 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
701 //******************************************************
702 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
704 qint64 aIt = theIndex.internalId();
705 ModelAPI_Object* aObj = 0;
706 ModelAPI_Document* aDoc = 0;
707 SessionPtr aSession = ModelAPI_Session::get();
708 DocumentPtr aActiveDoc = aSession->activeDocument();
710 Qt::ItemFlags aNullFlag;
711 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
712 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
716 // Folders under root
717 DocumentPtr aRootDoc = aSession->moduleDocument();
718 if (aRootDoc != aActiveDoc)
721 aDoc = getSubDocument(theIndex.internalPointer());
723 aObj = (ModelAPI_Object*) theIndex.internalPointer();
728 if (aObj->isDisabled())
729 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
731 if (aSession->moduleDocument() != aObj->document())
732 if (aActiveDoc != aObj->document())
733 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
735 bool isCompositeSub = false;
736 // An object which is sub-object of a composite object can not be accessible in column 1
737 if (theIndex.column() == 1) {
738 ObjectPtr aObjPtr = aObj->data()->owner();
739 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
740 if (aFeature.get()) {
741 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
743 isCompositeSub = true;
745 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
747 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
749 isCompositeSub = true;
754 return Qt::ItemIsSelectable;
756 if (aObj->document() != aActiveDoc) {
757 // The object could be a root of sub-tree
758 ResultPartPtr aPartRes = getPartResult(aObj);
759 if (aPartRes.get()) {
760 if (aPartRes->partDoc() == aActiveDoc)
766 // A folder under sub-document
767 if (aActiveDoc.get() != aDoc)
773 //******************************************************
774 QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
776 SessionPtr aSession = ModelAPI_Session::get();
777 DocumentPtr aRootDoc = aSession->moduleDocument();
778 if (myXMLReader->isAttachToResult()) { // If document is attached to result
779 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
781 ResultPartPtr aPartRes;
782 for (int i = 0; i < aNb; i++) {
783 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
784 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
785 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
787 if (myXMLReader->rootType() == ModelAPI_Feature::group()) {
788 aRow += foldersCount();
790 return createIndex(aRow, 0, aObj.get());
793 } else { // If document is attached to feature
794 int aNb = aRootDoc->size(ModelAPI_Feature::group());
796 ResultPartPtr aPartRes;
797 for (int i = 0; i < aNb; i++) {
798 aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
799 aPartRes = getPartResult(aObj.get());
800 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
802 if (myXMLReader->rootType() == ModelAPI_Feature::group())
803 aRow += foldersCount();
804 return createIndex(aRow, 0, aObj.get());
808 return QModelIndex();
811 //******************************************************
812 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
814 SessionPtr aSession = ModelAPI_Session::get();
815 DocumentPtr aRootDoc = aSession->moduleDocument();
816 if (theDoc == aRootDoc)
817 return QModelIndex();
819 return findDocumentRootIndex(theDoc.get());
822 //******************************************************
823 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
826 SessionPtr aSession = ModelAPI_Session::get();
827 DocumentPtr aRootDoc = aSession->moduleDocument();
828 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
829 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
830 if (myXMLReader->rootShowEmpty(i))
833 if (aRootDoc->size(myXMLReader->rootFolderType(i)) > 0)
838 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
839 if (myXMLReader->subShowEmpty(i))
842 if (theDoc->size(myXMLReader->subFolderType(i)) > 0)
851 //******************************************************
852 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
855 SessionPtr aSession = ModelAPI_Session::get();
856 DocumentPtr aRootDoc = aSession->moduleDocument();
857 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
858 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
859 if (!myXMLReader->rootShowEmpty(i)) {
860 if (aRootDoc->size(myXMLReader->rootFolderType(i)) == 0)
865 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
866 if (!myXMLReader->subShowEmpty(i)) {
867 if (theDoc->size(myXMLReader->subFolderType(i)) == 0)
876 //******************************************************
877 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
881 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
882 if (!myXMLReader->rootShowEmpty(i))
883 aResult << myXMLReader->rootFolderType(i).c_str();
886 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
887 if (!myXMLReader->subShowEmpty(i))
888 aResult << myXMLReader->subFolderType(i).c_str();
894 //******************************************************
895 QModelIndex XGUI_DataModel::lastHistoryIndex() const
897 SessionPtr aSession = ModelAPI_Session::get();
898 DocumentPtr aCurDoc = aSession->activeDocument();
899 FeaturePtr aFeature = aCurDoc->currentFeature(true);
900 if (aFeature.get()) {
901 QModelIndex aInd = objectIndex(aFeature);
902 return createIndex(aInd.row(), 1, aInd.internalPointer());
904 if (aCurDoc == aSession->moduleDocument())
905 return createIndex(foldersCount() - 1, 1, -1);
907 return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());
911 //******************************************************
912 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc)
914 SessionPtr aSession = ModelAPI_Session::get();
915 ModelAPI_Document* aDoc = theDoc;
917 aDoc = aSession->moduleDocument().get();
919 bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
923 int aId = myXMLReader->subFolderId(theType);
925 for (int i = 0; i < aId; i++) {
926 if (!myXMLReader->subShowEmpty(i)) {
927 if (aDoc->size(myXMLReader->subFolderType(i)) == 0)
932 int aId = myXMLReader->rootFolderId(theType);
934 for (int i = 0; i < aId; i++) {
935 if (!myXMLReader->rootShowEmpty(i)) {
936 if (aDoc->size(myXMLReader->rootFolderType(i)) == 0)
944 //******************************************************
945 void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex& theParent)
948 removeRows(theRow, theCount, theParent);
949 insertRows(theRow, theCount, theParent);