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) : QAbstractItemModel(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 does not need 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 // Process root folder
166 removeRow(aRow + aNbFolders);
167 rebuildBranch(aNbFolders, aRow);
169 // Process root sub-folder
170 int aFolderId = myXMLReader.rootFolderId(aGroup);
171 if (aFolderId != -1) {
172 QModelIndex aFolderIndex = createIndex(aFolderId, 0, -1);
173 removeRow(aRow, aFolderIndex);
174 //rebuildBranch(0, aRow);
177 // Check that some folders could erased
178 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
179 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
180 if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup) == 0)) {
181 // Appears first object in folder which can not be shown empty
182 removeRow(myXMLReader.rootFolderId(aGroup));
183 //rebuildBranch(0, aNbFolders + aDoc->size(myXMLReader.rootType()));
188 // Remove row for sub-document
189 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
190 if (aDocRoot.isValid()) {
191 int aRow = aDoc->size(aGroup);
192 int aNbSubFolders = foldersCount(aDoc.get());
193 if (aGroup == aSubType) {
194 // List of objects under document root
195 removeRow(aRow + aNbSubFolders, aDocRoot);
196 rebuildBranch(aNbSubFolders, aRow, aDocRoot);
198 // List of objects under a folder
199 int aFolderId = folderId(aGroup, aDoc.get());
200 if (aFolderId != -1) {
201 QModelIndex aFolderRoot = createIndex(aFolderId, 0, aDoc.get());
202 removeRow(aRow, aFolderRoot);
203 //rebuildBranch(0, aRow, aFolderRoot);
206 // Check that some folders could disappear
207 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
208 int aSize = aDoc->size(aGroup);
209 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
210 if ((aNotEmptyFolder.toStdString() == aGroup) && (aSize == 0)) {
211 // Appears first object in folder which can not be shown empty
212 removeRow(myXMLReader.subFolderId(aGroup), aDocRoot);
213 //rebuildBranch(0, aNbSubFolders + aDoc->size(myXMLReader.subType()), aDocRoot);
220 Events_Error::send("Problem with Data Model definition of sub-document");
224 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
225 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
226 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
227 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
229 std::set<ObjectPtr>::const_iterator aIt;
230 std::string aObjType;
231 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
232 ObjectPtr aObject = (*aIt);
233 QModelIndex aIndex = objectIndex(aObject);
234 if (aIndex.isValid())
235 emit dataChanged(aIndex, aIndex);
237 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
238 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aUpdMsg =
239 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
240 DocumentPtr aDoc = aUpdMsg->document();
241 std::string aGroup = aUpdMsg->group();
245 if (aDoc == aRootDoc) {
246 // Update a group under root
247 if (aGroup == myXMLReader.rootType()) // Update objects under root
248 aStartId = foldersCount();
249 else // Update objects in folder under root
250 aParent = createIndex(folderId(aGroup), 0, -1);
252 // Update a sub-document
253 if (aGroup == myXMLReader.subType()) {
254 // Update sub-document root
255 aParent = findDocumentRootIndex(aDoc.get());
256 aStartId = foldersCount(aDoc.get());
258 // update folder in sub-document
259 aParent = createIndex(folderId(aGroup, aDoc.get()), 0, aDoc.get());
261 int aChildNb = rowCount(aParent);
262 rebuildBranch(aStartId, aChildNb - aStartId);
263 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
264 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
265 if (aDoc != aRootDoc) {
266 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
267 if (aDocRoot.isValid())
268 emit dataChanged(aDocRoot, aDocRoot);
270 // We have got a new document
274 // Events_Error::send("Problem with Data Model definition of sub-document");
280 //******************************************************
281 void XGUI_DataModel::clear()
286 //******************************************************
287 void XGUI_DataModel::rebuildDataTree()
293 //******************************************************
294 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
296 if (theIndex.internalId() < 0) // this is a folder
298 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
299 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
302 return aObj->data()->owner();
305 //******************************************************
306 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
308 std::string aType = theObject->groupName();
309 DocumentPtr aDoc = theObject->document();
310 int aRow = aDoc->index(theObject);
312 // it could be a part of complex object
313 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
314 if (aFeature.get()) {
315 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
316 if (aCompFea.get()) {
317 for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
318 if (aCompFea->subFeature(i, true) == theObject) {
325 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
327 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
328 if (aCompRes.get()) {
329 for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
330 if (aCompRes->subResult(i, true) == theObject) {
339 return QModelIndex();
341 return createIndex(aRow, 0, theObject.get());
343 SessionPtr aSession = ModelAPI_Session::get();
344 DocumentPtr aRootDoc = aSession->moduleDocument();
345 if (aDoc == aRootDoc && myXMLReader.rootType() == aType) {
346 // The object from root document
347 aRow += foldersCount();
348 } else if (myXMLReader.subType() == aType) {
349 // The object from sub document
350 aRow += foldersCount(aDoc.get());
352 return createIndex(aRow, 0, theObject.get());
355 //******************************************************
356 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
358 SessionPtr aSession = ModelAPI_Session::get();
359 DocumentPtr aRootDoc = aSession->moduleDocument();
360 int aNbFolders = foldersCount();
361 int theIndexRow = theIndex.row();
363 if ((theRole == Qt::DecorationRole) && (theIndex == lastHistoryIndex()))
364 return QIcon(":pictures/arrow.png");
366 if (theIndex.column() == 1)
369 int aParentId = theIndex.internalId();
370 if (aParentId == -1) { // root folders
372 case Qt::DisplayRole:
373 return QString(myXMLReader.rootFolderName(theIndexRow).c_str()) +
374 QString(" (%1)").arg(rowCount(theIndex));
375 case Qt::DecorationRole:
376 return QIcon(myXMLReader.rootFolderIcon(theIndexRow).c_str());
377 case Qt::ForegroundRole:
378 if ((flags(theIndex) & Qt::ItemIsEditable) == 0)
379 return QBrush(Qt::lightGray);
382 } else { // an object or sub-document
383 if (theRole == Qt::ForegroundRole) {
384 if ((flags(theIndex) & Qt::ItemIsEditable) == 0)
385 return QBrush(Qt::lightGray);
389 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
390 if (aSubDoc) { // this is a folder of sub document
391 QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
392 int aRow = theIndexRow;
393 while (aMissedIdx.contains(aRow))
397 case Qt::DisplayRole:
398 return QString(myXMLReader.subFolderName(aRow).c_str()) +
399 QString(" (%1)").arg(rowCount(theIndex));
400 case Qt::DecorationRole:
401 return QIcon(myXMLReader.subFolderIcon(aRow).c_str());
404 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
406 case Qt::DisplayRole:
408 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
409 ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
410 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
411 QString aVal = QString::number(aValueAttribute->value());
412 QString aTitle = QString(aObj->data()->name().c_str());
413 return aTitle + " = " + aVal;
416 if (aObj->groupName() == myXMLReader.subType()) {
417 ResultPartPtr aPartRes = getPartResult(aObj);
418 if (aPartRes.get()) {
419 if (aPartRes->partDoc().get() == NULL)
420 aSuffix = " (Not loaded)";
423 return aObj->data()->name().c_str() + aSuffix;
425 case Qt::DecorationRole:
426 return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
433 //******************************************************
434 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
439 //******************************************************
440 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
442 SessionPtr aSession = ModelAPI_Session::get();
443 if (!aSession->hasModuleDocument())
445 DocumentPtr aRootDoc = aSession->moduleDocument();
447 if (!theParent.isValid()) {
448 // Return number of items in root
449 int aNbFolders = foldersCount();
451 std::string aType = myXMLReader.rootType();
453 aNbItems = aRootDoc->size(aType);
454 return aNbFolders + aNbItems;
457 int aId = theParent.internalId();
459 // this is a folder under root
460 int aParentPos = theParent.row();
461 std::string aType = myXMLReader.rootFolderType(aParentPos);
462 return aRootDoc->size(aType);
464 // It is an object which could have children
465 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
467 // a folder of sub-document
468 QIntList aMissedIdx = missedFolderIndexes(aDoc);
469 int aRow = theParent.row();
470 while (aMissedIdx.contains(aRow))
472 std::string aType = myXMLReader.subFolderType(aRow);
473 return aDoc->size(aType);
475 ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
476 // Check for Part feature
477 ResultPartPtr aPartRes = getPartResult(aObj);
478 if (aPartRes.get()) {
479 DocumentPtr aSubDoc = aPartRes->partDoc();
483 int aNbSubFolders = foldersCount(aSubDoc.get());
485 std::string aSubType = myXMLReader.subType();
486 if (!aSubType.empty())
487 aNbSubItems = aSubDoc->size(aSubType);
488 return aNbSubItems + aNbSubFolders;
490 // Check for composite object
491 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
493 return aCompFeature->numberOfSubs(true);
494 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
496 return aCompRes->numberOfSubs(true);
503 //******************************************************
504 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
509 //******************************************************
510 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
512 SessionPtr aSession = ModelAPI_Session::get();
513 DocumentPtr aRootDoc = aSession->moduleDocument();
514 int aNbFolders = foldersCount();
518 if (!theParent.isValid()) {
519 if (theRow < aNbFolders) // Return first level folder index
520 return createIndex(theRow, theColumn, -1);
521 else { // return object under root index
522 std::string aType = myXMLReader.rootType();
523 int aObjId = theRow - aNbFolders;
524 if (aObjId < aRootDoc->size(aType)) {
525 ObjectPtr aObj = aRootDoc->object(aType, aObjId);
526 aIndex = objectIndex(aObj);
530 int aId = theParent.internalId();
531 int aParentPos = theParent.row();
532 if (aId == -1) { // return object index inside of first level of folders
533 std::string aType = myXMLReader.rootFolderType(aParentPos);
534 if (theRow < aRootDoc->size(aType)) {
535 ObjectPtr aObj = aRootDoc->object(aType, theRow);
536 aIndex = objectIndex(aObj);
539 // It is an object which could have children
540 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
542 // It is a folder of sub-document
543 int aParentRow = aParentPos;
544 QIntList aMissedIdx = missedFolderIndexes(aDoc);
545 while (aMissedIdx.contains(aParentRow))
547 std::string aType = myXMLReader.subFolderType(aParentRow);
548 if (theRow < aDoc->size(aType)) {
549 ObjectPtr aObj = aDoc->object(aType, theRow);
550 aIndex = objectIndex(aObj);
553 ModelAPI_Object* aParentObj = (ModelAPI_Object*)theParent.internalPointer();
555 // Check for Part feature
556 ResultPartPtr aPartRes = getPartResult(aParentObj);
557 if (aPartRes.get()) {
558 DocumentPtr aSubDoc = aPartRes->partDoc();
559 int aNbSubFolders = foldersCount(aSubDoc.get());
560 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
561 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
563 // this is an object under sub document root
564 std::string aType = myXMLReader.subType();
565 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
566 aIndex = objectIndex(aObj);
569 // Check for composite object
570 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
572 aIndex = objectIndex(aCompFeature->subFeature(theRow));
574 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
576 aIndex = objectIndex(aCompRes->subResult(theRow));
583 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
587 //******************************************************
588 static QModelIndex MYLastDeleted;
589 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
591 // To avoid additional request about index which was already deleted
592 if (theIndex == MYLastDeleted)
593 return QModelIndex();
595 int aId = theIndex.internalId();
596 if (aId != -1) { // The object is not a root folder
597 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
599 // It is a folder of sub-document
600 return findDocumentRootIndex(aDoc);
602 ObjectPtr aObj = object(theIndex);
604 // To avoid additional request about index which was already deleted
605 // If deleted it causes a crash on delete object from Part
606 MYLastDeleted = theIndex;
607 return QModelIndex();
609 // Check is it object a sub-object of a complex object
610 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
611 if (aFeature.get()) {
612 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
613 if (aCompFea.get()) {
614 return objectIndex(aCompFea);
617 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
619 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
620 if (aCompRes.get()) {
621 return objectIndex(aCompRes);
624 // Use as ordinary object
625 std::string aType = aObj->groupName();
626 SessionPtr aSession = ModelAPI_Session::get();
627 DocumentPtr aRootDoc = aSession->moduleDocument();
628 DocumentPtr aSubDoc = aObj->document();
629 if (aSubDoc == aRootDoc) {
630 if (aType == myXMLReader.rootType())
631 return QModelIndex();
633 // return first level of folder index
634 int aFolderId = myXMLReader.rootFolderId(aType);
635 // Items in a one row must have the same parent
636 return createIndex(aFolderId, 0, -1);
639 if (aType == myXMLReader.subType())
640 return findDocumentRootIndex(aSubDoc.get());
642 // return first level of folder index
643 int aFolderId = myXMLReader.subFolderId(aType);
644 // Items in a one row must have the same parent
645 return createIndex(aFolderId, 0, aSubDoc.get());
649 return QModelIndex();
652 //******************************************************
653 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
655 return rowCount(theParent) > 0;
658 //******************************************************
659 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
661 beginInsertRows(theParent, theRow, theRow + theCount - 1);
667 //******************************************************
668 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
670 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
675 //******************************************************
676 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
678 qint64 aIt = theIndex.internalId();
679 ModelAPI_Object* aObj = 0;
680 ModelAPI_Document* aDoc = 0;
681 SessionPtr aSession = ModelAPI_Session::get();
682 DocumentPtr aActiveDoc = aSession->activeDocument();
684 Qt::ItemFlags aNullFlag;
685 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
686 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
690 // Folders under root
691 DocumentPtr aRootDoc = aSession->moduleDocument();
692 if (aRootDoc != aActiveDoc)
695 aDoc = getSubDocument(theIndex.internalPointer());
697 aObj = (ModelAPI_Object*) theIndex.internalPointer();
702 if (aObj->isDisabled())
703 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
705 bool isCompositeSub = false;
706 // An object which is sub-object of a composite object can not be accessible in column 1
707 if (theIndex.column() == 1) {
708 ObjectPtr aObjPtr = aObj->data()->owner();
709 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
710 if (aFeature.get()) {
711 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
713 isCompositeSub = true;
715 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
717 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
719 isCompositeSub = true;
724 return Qt::ItemIsSelectable;
726 if (aObj->document() != aActiveDoc) {
727 // The object could be a root of sub-tree
728 ResultPartPtr aPartRes = getPartResult(aObj);
729 if (aPartRes.get()) {
730 if (aPartRes->partDoc() == aActiveDoc)
736 // A folder under sub-document
737 if (aActiveDoc.get() != aDoc)
743 //******************************************************
744 QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
746 SessionPtr aSession = ModelAPI_Session::get();
747 DocumentPtr aRootDoc = aSession->moduleDocument();
748 if (myXMLReader.isAttachToResult()) { // If document is attached to result
749 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
751 ResultPartPtr aPartRes;
752 for (int i = 0; i < aNb; i++) {
753 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
754 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
755 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
757 if (myXMLReader.rootType() == ModelAPI_Feature::group()) {
758 aRow += foldersCount();
760 return createIndex(aRow, 0, aObj.get());
763 } else { // If document is attached to feature
764 int aNb = aRootDoc->size(ModelAPI_Feature::group());
766 ResultPartPtr aPartRes;
767 for (int i = 0; i < aNb; i++) {
768 aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
769 aPartRes = getPartResult(aObj.get());
770 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
772 if (myXMLReader.rootType() == ModelAPI_Feature::group())
773 aRow += foldersCount();
774 return createIndex(aRow, 0, aObj.get());
778 return QModelIndex();
781 //******************************************************
782 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
784 SessionPtr aSession = ModelAPI_Session::get();
785 DocumentPtr aRootDoc = aSession->moduleDocument();
786 if (theDoc == aRootDoc)
787 return QModelIndex();
789 return findDocumentRootIndex(theDoc.get());
792 //******************************************************
793 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
796 SessionPtr aSession = ModelAPI_Session::get();
797 DocumentPtr aRootDoc = aSession->moduleDocument();
798 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
799 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
800 if (myXMLReader.rootShowEmpty(i))
803 if (aRootDoc->size(myXMLReader.rootFolderType(i)) > 0)
808 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
809 if (myXMLReader.subShowEmpty(i))
812 if (theDoc->size(myXMLReader.subFolderType(i)) > 0)
821 //******************************************************
822 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
825 SessionPtr aSession = ModelAPI_Session::get();
826 DocumentPtr aRootDoc = aSession->moduleDocument();
827 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
828 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
829 if (!myXMLReader.rootShowEmpty(i)) {
830 if (aRootDoc->size(myXMLReader.rootFolderType(i)) == 0)
835 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
836 if (!myXMLReader.subShowEmpty(i)) {
837 if (theDoc->size(myXMLReader.subFolderType(i)) == 0)
846 //******************************************************
847 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
851 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
852 if (!myXMLReader.rootShowEmpty(i))
853 aResult << myXMLReader.rootFolderType(i).c_str();
856 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
857 if (!myXMLReader.subShowEmpty(i))
858 aResult << myXMLReader.subFolderType(i).c_str();
864 //******************************************************
865 QModelIndex XGUI_DataModel::lastHistoryIndex() const
867 SessionPtr aSession = ModelAPI_Session::get();
868 DocumentPtr aCurDoc = aSession->activeDocument();
869 FeaturePtr aFeature = aCurDoc->currentFeature(true);
870 if (aFeature.get()) {
871 QModelIndex aInd = objectIndex(aFeature);
872 return createIndex(aInd.row(), 1, aInd.internalPointer());
874 if (aCurDoc == aSession->moduleDocument())
875 return createIndex(foldersCount() - 1, 1, -1);
877 return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());
881 //******************************************************
882 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc)
884 SessionPtr aSession = ModelAPI_Session::get();
885 ModelAPI_Document* aDoc = theDoc;
887 aDoc = aSession->moduleDocument().get();
889 bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
893 int aId = myXMLReader.subFolderId(theType);
895 for (int i = 0; i < aId; i++) {
896 if (!myXMLReader.subShowEmpty(i)) {
897 if (aDoc->size(myXMLReader.subFolderType(i)) == 0)
902 int aId = myXMLReader.rootFolderId(theType);
904 for (int i = 0; i < aId; i++) {
905 if (!myXMLReader.rootShowEmpty(i)) {
906 if (aDoc->size(myXMLReader.rootFolderType(i)) == 0)
914 //******************************************************
915 void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex& theParent)
918 removeRows(theRow, theCount, theParent);
919 insertRows(theRow, theCount, theParent);