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
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 myXMLReader.readAll();
69 Events_Loop* aLoop = Events_Loop::loop();
70 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
71 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
72 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
73 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_ORDER_UPDATED));
74 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
77 //******************************************************
78 void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
80 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
81 std::string aRootType = myXMLReader.rootType();
82 std::string aSubType = myXMLReader.subType();
83 int aNbFolders = foldersCount();
85 // Created object event *******************
86 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
87 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
88 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
89 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
91 std::set<ObjectPtr>::const_iterator aIt;
93 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
94 ObjectPtr aObject = (*aIt);
95 // We do not show objects which does not need to be shown in object browser
96 if (!aObject->isInHistory())
99 aObjType = aObject->groupName();
100 DocumentPtr aDoc = aObject->document();
101 if (aDoc == aRootDoc) {
102 // Check that new folders could appear
103 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
104 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
105 if ((aNotEmptyFolder.toStdString() == aObjType) && (aRootDoc->size(aObjType) == 1))
106 // Appears first object in folder which can not be shown empty
107 insertRow(myXMLReader.rootFolderId(aObjType));
110 int aRow = aRootDoc->size(aObjType) - 1;
112 if (aObjType == aRootType) {
113 insertRow(aRow + aNbFolders + 1);
115 int aFolderId = myXMLReader.rootFolderId(aObjType);
116 if (aFolderId != -1) {
117 insertRow(aRow, createIndex(aFolderId, 0, -1));
122 // Object created in sub-document
123 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
124 if (aDocRoot.isValid()) {
125 // Check that new folders could appear
126 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
127 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
128 if ((aNotEmptyFolder.toStdString() == aObjType) && (aDoc->size(aObjType) == 1))
129 // Appears first object in folder which can not be shown empty
130 insertRow(myXMLReader.subFolderId(aObjType), aDocRoot);
132 int aRow = aDoc->index(aObject);
134 int aNbSubFolders = foldersCount(aDoc.get());
135 if (aObjType == aSubType) {
136 // List of objects under document root
137 insertRow(aRow + aNbSubFolders, aDocRoot);
139 // List of objects under a folder
141 int aFolderId = folderId(aObjType, aDoc.get());
142 if (aFolderId != -1) {
143 QModelIndex aParentFolder = createIndex(aFolderId, 0, aDoc.get());
144 insertRow(aRow, aParentFolder);
145 emit dataChanged(aParentFolder, aParentFolder);
153 Events_Error::send("Problem with Data Model definition of sub-document");
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> aGroups = aUpdMsg->groups();
163 std::set<std::string>::const_iterator aIt;
164 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
165 std::string aGroup = (*aIt);
166 if (aDoc == aRootDoc) { // If root objects
167 int aRow = aRootDoc->size(aGroup);
168 if (aGroup == aRootType) {
169 // Process root folder
170 removeRow(aRow + aNbFolders);
171 rebuildBranch(aNbFolders, aRow);
173 // Process root sub-folder
174 int aFolderId = myXMLReader.rootFolderId(aGroup);
175 if (aFolderId != -1) {
176 QModelIndex aFolderIndex = createIndex(aFolderId, 0, -1);
177 removeRow(aRow, aFolderIndex);
178 //rebuildBranch(0, aRow);
181 // Check that some folders could erased
182 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
183 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
184 if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup) == 0)) {
185 // Appears first object in folder which can not be shown empty
186 removeRow(myXMLReader.rootFolderId(aGroup));
187 //rebuildBranch(0, aNbFolders + aDoc->size(myXMLReader.rootType()));
192 // Remove row for sub-document
193 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
194 if (aDocRoot.isValid()) {
195 int aRow = aDoc->size(aGroup);
196 int aNbSubFolders = foldersCount(aDoc.get());
197 if (aGroup == aSubType) {
198 // List of objects under document root
199 removeRow(aRow + aNbSubFolders, aDocRoot);
200 rebuildBranch(aNbSubFolders, aRow, aDocRoot);
202 // List of objects under a folder
203 int aFolderId = folderId(aGroup, aDoc.get());
204 if (aFolderId != -1) {
205 QModelIndex aFolderRoot = createIndex(aFolderId, 0, aDoc.get());
206 removeRow(aRow, aFolderRoot);
207 //rebuildBranch(0, aRow, aFolderRoot);
210 // Check that some folders could disappear
211 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
212 int aSize = aDoc->size(aGroup);
213 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
214 if ((aNotEmptyFolder.toStdString() == aGroup) && (aSize == 0)) {
215 // Appears first object in folder which can not be shown empty
216 removeRow(myXMLReader.subFolderId(aGroup), aDocRoot);
217 //rebuildBranch(0, aNbSubFolders + aDoc->size(myXMLReader.subType()), aDocRoot);
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, aParent);
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:
379 Qt::ItemFlags aFlags = theIndex.flags();
380 if (aFlags == Qt::ItemFlags())
381 return QBrush(DISABLED_COLOR);
382 if (!aFlags.testFlag(Qt::ItemIsEditable))
383 return QBrush(SELECTABLE_COLOR);
387 } else { // an object or sub-document
388 if (theRole == Qt::ForegroundRole) {
389 Qt::ItemFlags aFlags = theIndex.flags();
390 if (aFlags == Qt::ItemFlags())
391 return QBrush(DISABLED_COLOR);
392 if (!aFlags.testFlag(Qt::ItemIsEditable))
393 return QBrush(SELECTABLE_COLOR);
397 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
398 if (aSubDoc) { // this is a folder of sub document
399 QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
400 int aRow = theIndexRow;
401 while (aMissedIdx.contains(aRow))
405 case Qt::DisplayRole:
406 return QString(myXMLReader.subFolderName(aRow).c_str()) +
407 QString(" (%1)").arg(rowCount(theIndex));
408 case Qt::DecorationRole:
409 return QIcon(myXMLReader.subFolderIcon(aRow).c_str());
412 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
414 case Qt::DisplayRole:
416 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
417 ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
418 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
419 QString aVal = QString::number(aValueAttribute->value());
420 QString aTitle = QString(aObj->data()->name().c_str());
421 return aTitle + " = " + aVal;
424 if (aObj->groupName() == myXMLReader.subType()) {
425 ResultPartPtr aPartRes = getPartResult(aObj);
426 if (aPartRes.get()) {
427 if (aPartRes->partDoc().get() == NULL)
428 aSuffix = " (Not loaded)";
431 return aObj->data()->name().c_str() + aSuffix;
433 case Qt::DecorationRole:
434 return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
441 //******************************************************
442 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
447 //******************************************************
448 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
450 SessionPtr aSession = ModelAPI_Session::get();
451 if (!aSession->hasModuleDocument())
453 DocumentPtr aRootDoc = aSession->moduleDocument();
455 if (!theParent.isValid()) {
456 // Return number of items in root
457 int aNbFolders = foldersCount();
459 std::string aType = myXMLReader.rootType();
461 aNbItems = aRootDoc->size(aType);
462 return aNbFolders + aNbItems;
465 int aId = theParent.internalId();
467 // this is a folder under root
468 int aParentPos = theParent.row();
469 std::string aType = myXMLReader.rootFolderType(aParentPos);
470 return aRootDoc->size(aType);
472 // It is an object which could have children
473 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
475 // a folder of sub-document
476 QIntList aMissedIdx = missedFolderIndexes(aDoc);
477 int aRow = theParent.row();
478 while (aMissedIdx.contains(aRow))
480 std::string aType = myXMLReader.subFolderType(aRow);
481 return aDoc->size(aType);
483 ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
484 // Check for Part feature
485 ResultPartPtr aPartRes = getPartResult(aObj);
486 if (aPartRes.get()) {
487 DocumentPtr aSubDoc = aPartRes->partDoc();
491 int aNbSubFolders = foldersCount(aSubDoc.get());
493 std::string aSubType = myXMLReader.subType();
494 if (!aSubType.empty())
495 aNbSubItems = aSubDoc->size(aSubType);
496 return aNbSubItems + aNbSubFolders;
498 // Check for composite object
499 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
501 return aCompFeature->numberOfSubs(true);
502 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
504 return aCompRes->numberOfSubs(true);
511 //******************************************************
512 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
517 //******************************************************
518 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
520 SessionPtr aSession = ModelAPI_Session::get();
521 DocumentPtr aRootDoc = aSession->moduleDocument();
522 int aNbFolders = foldersCount();
526 if (!theParent.isValid()) {
527 if (theRow < aNbFolders) // Return first level folder index
528 return createIndex(theRow, theColumn, -1);
529 else { // return object under root index
530 std::string aType = myXMLReader.rootType();
531 int aObjId = theRow - aNbFolders;
532 if (aObjId < aRootDoc->size(aType)) {
533 ObjectPtr aObj = aRootDoc->object(aType, aObjId);
534 aIndex = objectIndex(aObj);
538 int aId = theParent.internalId();
539 int aParentPos = theParent.row();
540 if (aId == -1) { // return object index inside of first level of folders
541 std::string aType = myXMLReader.rootFolderType(aParentPos);
542 if (theRow < aRootDoc->size(aType)) {
543 ObjectPtr aObj = aRootDoc->object(aType, theRow);
544 aIndex = objectIndex(aObj);
547 // It is an object which could have children
548 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
550 // It is a folder of sub-document
551 int aParentRow = aParentPos;
552 QIntList aMissedIdx = missedFolderIndexes(aDoc);
553 while (aMissedIdx.contains(aParentRow))
555 std::string aType = myXMLReader.subFolderType(aParentRow);
556 if (theRow < aDoc->size(aType)) {
557 ObjectPtr aObj = aDoc->object(aType, theRow);
558 aIndex = objectIndex(aObj);
561 ModelAPI_Object* aParentObj = (ModelAPI_Object*)theParent.internalPointer();
563 // Check for Part feature
564 ResultPartPtr aPartRes = getPartResult(aParentObj);
565 if (aPartRes.get()) {
566 DocumentPtr aSubDoc = aPartRes->partDoc();
567 int aNbSubFolders = foldersCount(aSubDoc.get());
568 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
569 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
571 // this is an object under sub document root
572 std::string aType = myXMLReader.subType();
573 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
574 aIndex = objectIndex(aObj);
577 // Check for composite object
578 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
580 aIndex = objectIndex(aCompFeature->subFeature(theRow));
582 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
584 aIndex = objectIndex(aCompRes->subResult(theRow));
591 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
595 //******************************************************
596 static QModelIndex MYLastDeleted;
597 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
599 // To avoid additional request about index which was already deleted
600 if (theIndex == MYLastDeleted)
601 return QModelIndex();
603 int aId = theIndex.internalId();
604 if (aId != -1) { // The object is not a root folder
605 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
607 // It is a folder of sub-document
608 return findDocumentRootIndex(aDoc);
610 ObjectPtr aObj = object(theIndex);
612 // To avoid additional request about index which was already deleted
613 // If deleted it causes a crash on delete object from Part
614 MYLastDeleted = theIndex;
615 return QModelIndex();
617 // Check is it object a sub-object of a complex object
618 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
619 if (aFeature.get()) {
620 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
621 if (aCompFea.get()) {
622 return objectIndex(aCompFea);
625 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
627 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
628 if (aCompRes.get()) {
629 return objectIndex(aCompRes);
632 // Use as ordinary object
633 std::string aType = aObj->groupName();
634 SessionPtr aSession = ModelAPI_Session::get();
635 DocumentPtr aRootDoc = aSession->moduleDocument();
636 DocumentPtr aSubDoc = aObj->document();
637 if (aSubDoc == aRootDoc) {
638 if (aType == myXMLReader.rootType())
639 return QModelIndex();
641 // return first level of folder index
642 int aFolderId = myXMLReader.rootFolderId(aType);
643 // Items in a one row must have the same parent
644 return createIndex(aFolderId, 0, -1);
647 if (aType == myXMLReader.subType())
648 return findDocumentRootIndex(aSubDoc.get());
650 // return first level of folder index
651 int aFolderId = myXMLReader.subFolderId(aType);
652 // Items in a one row must have the same parent
653 return createIndex(aFolderId, 0, aSubDoc.get());
657 return QModelIndex();
660 //******************************************************
661 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
663 return rowCount(theParent) > 0;
666 //******************************************************
667 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
669 beginInsertRows(theParent, theRow, theRow + theCount - 1);
675 //******************************************************
676 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
678 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
683 //******************************************************
684 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
686 qint64 aIt = theIndex.internalId();
687 ModelAPI_Object* aObj = 0;
688 ModelAPI_Document* aDoc = 0;
689 SessionPtr aSession = ModelAPI_Session::get();
690 DocumentPtr aActiveDoc = aSession->activeDocument();
692 Qt::ItemFlags aNullFlag;
693 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
694 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
698 // Folders under root
699 DocumentPtr aRootDoc = aSession->moduleDocument();
700 if (aRootDoc != aActiveDoc)
703 aDoc = getSubDocument(theIndex.internalPointer());
705 aObj = (ModelAPI_Object*) theIndex.internalPointer();
710 if (aObj->isDisabled())
711 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
713 bool isCompositeSub = false;
714 // An object which is sub-object of a composite object can not be accessible in column 1
715 if (theIndex.column() == 1) {
716 ObjectPtr aObjPtr = aObj->data()->owner();
717 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
718 if (aFeature.get()) {
719 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
721 isCompositeSub = true;
723 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
725 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
727 isCompositeSub = true;
732 return Qt::ItemIsSelectable;
734 if (aObj->document() != aActiveDoc) {
735 // The object could be a root of sub-tree
736 ResultPartPtr aPartRes = getPartResult(aObj);
737 if (aPartRes.get()) {
738 if (aPartRes->partDoc() == aActiveDoc)
744 // A folder under sub-document
745 if (aActiveDoc.get() != aDoc)
751 //******************************************************
752 QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
754 SessionPtr aSession = ModelAPI_Session::get();
755 DocumentPtr aRootDoc = aSession->moduleDocument();
756 if (myXMLReader.isAttachToResult()) { // If document is attached to result
757 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
759 ResultPartPtr aPartRes;
760 for (int i = 0; i < aNb; i++) {
761 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
762 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
763 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
765 if (myXMLReader.rootType() == ModelAPI_Feature::group()) {
766 aRow += foldersCount();
768 return createIndex(aRow, 0, aObj.get());
771 } else { // If document is attached to feature
772 int aNb = aRootDoc->size(ModelAPI_Feature::group());
774 ResultPartPtr aPartRes;
775 for (int i = 0; i < aNb; i++) {
776 aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
777 aPartRes = getPartResult(aObj.get());
778 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
780 if (myXMLReader.rootType() == ModelAPI_Feature::group())
781 aRow += foldersCount();
782 return createIndex(aRow, 0, aObj.get());
786 return QModelIndex();
789 //******************************************************
790 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
792 SessionPtr aSession = ModelAPI_Session::get();
793 DocumentPtr aRootDoc = aSession->moduleDocument();
794 if (theDoc == aRootDoc)
795 return QModelIndex();
797 return findDocumentRootIndex(theDoc.get());
800 //******************************************************
801 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
804 SessionPtr aSession = ModelAPI_Session::get();
805 DocumentPtr aRootDoc = aSession->moduleDocument();
806 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
807 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
808 if (myXMLReader.rootShowEmpty(i))
811 if (aRootDoc->size(myXMLReader.rootFolderType(i)) > 0)
816 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
817 if (myXMLReader.subShowEmpty(i))
820 if (theDoc->size(myXMLReader.subFolderType(i)) > 0)
829 //******************************************************
830 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
833 SessionPtr aSession = ModelAPI_Session::get();
834 DocumentPtr aRootDoc = aSession->moduleDocument();
835 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
836 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
837 if (!myXMLReader.rootShowEmpty(i)) {
838 if (aRootDoc->size(myXMLReader.rootFolderType(i)) == 0)
843 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
844 if (!myXMLReader.subShowEmpty(i)) {
845 if (theDoc->size(myXMLReader.subFolderType(i)) == 0)
854 //******************************************************
855 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
859 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
860 if (!myXMLReader.rootShowEmpty(i))
861 aResult << myXMLReader.rootFolderType(i).c_str();
864 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
865 if (!myXMLReader.subShowEmpty(i))
866 aResult << myXMLReader.subFolderType(i).c_str();
872 //******************************************************
873 QModelIndex XGUI_DataModel::lastHistoryIndex() const
875 SessionPtr aSession = ModelAPI_Session::get();
876 DocumentPtr aCurDoc = aSession->activeDocument();
877 FeaturePtr aFeature = aCurDoc->currentFeature(true);
878 if (aFeature.get()) {
879 QModelIndex aInd = objectIndex(aFeature);
880 return createIndex(aInd.row(), 1, aInd.internalPointer());
882 if (aCurDoc == aSession->moduleDocument())
883 return createIndex(foldersCount() - 1, 1, -1);
885 return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());
889 //******************************************************
890 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc)
892 SessionPtr aSession = ModelAPI_Session::get();
893 ModelAPI_Document* aDoc = theDoc;
895 aDoc = aSession->moduleDocument().get();
897 bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
901 int aId = myXMLReader.subFolderId(theType);
903 for (int i = 0; i < aId; i++) {
904 if (!myXMLReader.subShowEmpty(i)) {
905 if (aDoc->size(myXMLReader.subFolderType(i)) == 0)
910 int aId = myXMLReader.rootFolderId(theType);
912 for (int i = 0; i < aId; i++) {
913 if (!myXMLReader.rootShowEmpty(i)) {
914 if (aDoc->size(myXMLReader.rootFolderType(i)) == 0)
922 //******************************************************
923 void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex& theParent)
926 removeRows(theRow, theCount, theParent);
927 insertRows(theRow, theCount, theParent);