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> 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 if (aObject->data()->isValid()) {
234 QModelIndex aIndex = objectIndex(aObject);
235 if (aIndex.isValid()) {
236 emit dataChanged(aIndex, aIndex);
243 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
244 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aUpdMsg =
245 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
246 DocumentPtr aDoc = aUpdMsg->document();
247 std::string aGroup = aUpdMsg->group();
251 if (aDoc == aRootDoc) {
252 // Update a group under root
253 if (aGroup == myXMLReader->rootType()) // Update objects under root
254 aStartId = foldersCount();
255 else // Update objects in folder under root
256 aParent = createIndex(folderId(aGroup), 0, -1);
258 // Update a sub-document
259 if (aGroup == myXMLReader->subType()) {
260 // Update sub-document root
261 aParent = findDocumentRootIndex(aDoc.get());
262 aStartId = foldersCount(aDoc.get());
264 // update folder in sub-document
265 aParent = createIndex(folderId(aGroup, aDoc.get()), 0, aDoc.get());
267 int aChildNb = rowCount(aParent);
268 rebuildBranch(aStartId, aChildNb - aStartId, aParent);
269 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
270 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
271 if (aDoc != aRootDoc) {
272 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
273 if (aDocRoot.isValid())
274 emit dataChanged(aDocRoot, aDocRoot);
276 // We have got a new document
282 //******************************************************
283 void XGUI_DataModel::clear()
288 //******************************************************
289 void XGUI_DataModel::rebuildDataTree()
296 //******************************************************
297 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
299 if (theIndex.internalId() < 0) // this is a folder
301 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
302 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
305 return aObj->data()->owner();
308 //******************************************************
309 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
311 std::string aType = theObject->groupName();
312 DocumentPtr aDoc = theObject->document();
313 int aRow = aDoc->index(theObject);
315 // it could be a part of complex object
316 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
317 if (aFeature.get()) {
318 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
319 if (aCompFea.get()) {
320 for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
321 if (aCompFea->subFeature(i, true) == theObject) {
328 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
330 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
331 if (aCompRes.get()) {
332 for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
333 if (aCompRes->subResult(i, true) == theObject) {
342 return QModelIndex();
344 return createIndex(aRow, 0, theObject.get());
346 SessionPtr aSession = ModelAPI_Session::get();
347 DocumentPtr aRootDoc = aSession->moduleDocument();
348 if (aDoc == aRootDoc && myXMLReader->rootType() == aType) {
349 // The object from root document
350 aRow += foldersCount();
351 } else if (myXMLReader->subType() == aType) {
352 // The object from sub document
353 aRow += foldersCount(aDoc.get());
355 return createIndex(aRow, 0, theObject.get());
358 //******************************************************
359 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
361 SessionPtr aSession = ModelAPI_Session::get();
362 DocumentPtr aRootDoc = aSession->moduleDocument();
363 int aNbFolders = foldersCount();
364 int theIndexRow = theIndex.row();
366 if ((theRole == Qt::DecorationRole) && (theIndex == lastHistoryIndex()))
367 return QIcon(":pictures/arrow.png");
369 if (theIndex.column() == 1)
372 int aParentId = theIndex.internalId();
373 if (aParentId == -1) { // root folders
375 case Qt::DisplayRole:
376 return QString(myXMLReader->rootFolderName(theIndexRow).c_str()) +
377 QString(" (%1)").arg(rowCount(theIndex));
378 case Qt::DecorationRole:
379 return QIcon(myXMLReader->rootFolderIcon(theIndexRow).c_str());
380 case Qt::ForegroundRole:
382 Qt::ItemFlags aFlags = theIndex.flags();
383 if (aFlags == Qt::ItemFlags())
384 return QBrush(DISABLED_COLOR);
385 if (!aFlags.testFlag(Qt::ItemIsEditable))
386 return QBrush(SELECTABLE_COLOR);
390 } else { // an object or sub-document
391 if (theRole == Qt::ForegroundRole) {
392 Qt::ItemFlags aFlags = theIndex.flags();
393 if (aFlags == Qt::ItemFlags())
394 return QBrush(DISABLED_COLOR);
395 if (!aFlags.testFlag(Qt::ItemIsEditable))
396 return QBrush(SELECTABLE_COLOR);
400 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
401 if (aSubDoc) { // this is a folder of sub document
402 QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
403 int aRow = theIndexRow;
404 while (aMissedIdx.contains(aRow))
408 case Qt::DisplayRole:
409 return QString(myXMLReader->subFolderName(aRow).c_str()) +
410 QString(" (%1)").arg(rowCount(theIndex));
411 case Qt::DecorationRole:
412 return QIcon(myXMLReader->subFolderIcon(aRow).c_str());
415 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
417 case Qt::DisplayRole:
419 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
420 ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
421 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
422 QString aVal = QString::number(aValueAttribute->value());
423 QString aTitle = QString(aObj->data()->name().c_str());
424 return aTitle + " = " + aVal;
427 if (aObj->groupName() == myXMLReader->subType()) {
428 ResultPartPtr aPartRes = getPartResult(aObj);
429 if (aPartRes.get()) {
430 if (aPartRes->partDoc().get() == NULL)
431 aSuffix = " (Not loaded)";
434 return aObj->data()->name().c_str() + aSuffix;
436 case Qt::DecorationRole:
437 return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
444 //******************************************************
445 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
450 //******************************************************
451 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
453 SessionPtr aSession = ModelAPI_Session::get();
454 if (!aSession->hasModuleDocument())
456 DocumentPtr aRootDoc = aSession->moduleDocument();
458 if (!theParent.isValid()) {
459 // Return number of items in root
460 int aNbFolders = foldersCount();
462 std::string aType = myXMLReader->rootType();
464 aNbItems = aRootDoc->size(aType);
465 return aNbFolders + aNbItems;
468 int aId = theParent.internalId();
470 // this is a folder under root
471 int aParentPos = theParent.row();
472 std::string aType = myXMLReader->rootFolderType(aParentPos);
473 return aRootDoc->size(aType);
475 // It is an object which could have children
476 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
478 // a folder of sub-document
479 QIntList aMissedIdx = missedFolderIndexes(aDoc);
480 int aRow = theParent.row();
481 while (aMissedIdx.contains(aRow))
483 std::string aType = myXMLReader->subFolderType(aRow);
484 return aDoc->size(aType);
486 ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
487 // Check for Part feature
488 ResultPartPtr aPartRes = getPartResult(aObj);
489 if (aPartRes.get()) {
490 DocumentPtr aSubDoc = aPartRes->partDoc();
494 int aNbSubFolders = foldersCount(aSubDoc.get());
496 std::string aSubType = myXMLReader->subType();
497 if (!aSubType.empty())
498 aNbSubItems = aSubDoc->size(aSubType);
499 return aNbSubItems + aNbSubFolders;
501 // Check for composite object
502 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
504 return aCompFeature->numberOfSubs(true);
505 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
507 return aCompRes->numberOfSubs(true);
514 //******************************************************
515 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
520 //******************************************************
521 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
523 SessionPtr aSession = ModelAPI_Session::get();
524 DocumentPtr aRootDoc = aSession->moduleDocument();
525 int aNbFolders = foldersCount();
529 if (!theParent.isValid()) {
530 if (theRow < aNbFolders) // Return first level folder index
531 return createIndex(theRow, theColumn, -1);
532 else { // return object under root index
533 std::string aType = myXMLReader->rootType();
534 int aObjId = theRow - aNbFolders;
535 if (aObjId < aRootDoc->size(aType)) {
536 ObjectPtr aObj = aRootDoc->object(aType, aObjId);
537 aIndex = objectIndex(aObj);
541 int aId = theParent.internalId();
542 int aParentPos = theParent.row();
543 if (aId == -1) { // return object index inside of first level of folders
544 std::string aType = myXMLReader->rootFolderType(aParentPos);
545 if (theRow < aRootDoc->size(aType)) {
546 ObjectPtr aObj = aRootDoc->object(aType, theRow);
547 aIndex = objectIndex(aObj);
550 // It is an object which could have children
551 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
553 // It is a folder of sub-document
554 int aParentRow = aParentPos;
555 QIntList aMissedIdx = missedFolderIndexes(aDoc);
556 while (aMissedIdx.contains(aParentRow))
558 std::string aType = myXMLReader->subFolderType(aParentRow);
559 if (theRow < aDoc->size(aType)) {
560 ObjectPtr aObj = aDoc->object(aType, theRow);
561 aIndex = objectIndex(aObj);
564 ModelAPI_Object* aParentObj = (ModelAPI_Object*)theParent.internalPointer();
566 // Check for Part feature
567 ResultPartPtr aPartRes = getPartResult(aParentObj);
568 if (aPartRes.get()) {
569 DocumentPtr aSubDoc = aPartRes->partDoc();
570 int aNbSubFolders = foldersCount(aSubDoc.get());
571 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
572 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
574 // this is an object under sub document root
575 std::string aType = myXMLReader->subType();
576 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
577 aIndex = objectIndex(aObj);
580 // Check for composite object
581 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
583 aIndex = objectIndex(aCompFeature->subFeature(theRow));
585 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
587 aIndex = objectIndex(aCompRes->subResult(theRow));
594 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
598 //******************************************************
599 static QModelIndex MYLastDeleted;
600 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
602 // To avoid additional request about index which was already deleted
603 if (theIndex == MYLastDeleted)
604 return QModelIndex();
606 int aId = theIndex.internalId();
607 if (aId != -1) { // The object is not a root folder
608 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
610 // It is a folder of sub-document
611 return findDocumentRootIndex(aDoc);
613 ObjectPtr aObj = object(theIndex);
615 // To avoid additional request about index which was already deleted
616 // If deleted it causes a crash on delete object from Part
617 MYLastDeleted = theIndex;
618 return QModelIndex();
620 // Check is it object a sub-object of a complex object
621 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
622 if (aFeature.get()) {
623 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
624 if (aCompFea.get()) {
625 return objectIndex(aCompFea);
628 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
630 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
631 if (aCompRes.get()) {
632 return objectIndex(aCompRes);
635 // Use as ordinary object
636 std::string aType = aObj->groupName();
637 SessionPtr aSession = ModelAPI_Session::get();
638 DocumentPtr aRootDoc = aSession->moduleDocument();
639 DocumentPtr aSubDoc = aObj->document();
640 if (aSubDoc == aRootDoc) {
641 if (aType == myXMLReader->rootType())
642 return QModelIndex();
644 // return first level of folder index
645 int aFolderId = myXMLReader->rootFolderId(aType);
646 // Items in a one row must have the same parent
647 return createIndex(aFolderId, 0, -1);
650 if (aType == myXMLReader->subType())
651 return findDocumentRootIndex(aSubDoc.get());
653 // return first level of folder index
654 int aFolderId = myXMLReader->subFolderId(aType);
655 // Items in a one row must have the same parent
656 return createIndex(aFolderId, 0, aSubDoc.get());
660 return QModelIndex();
663 //******************************************************
664 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
666 return rowCount(theParent) > 0;
669 //******************************************************
670 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
672 beginInsertRows(theParent, theRow, theRow + theCount - 1);
678 //******************************************************
679 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
681 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
686 //******************************************************
687 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
689 qint64 aIt = theIndex.internalId();
690 ModelAPI_Object* aObj = 0;
691 ModelAPI_Document* aDoc = 0;
692 SessionPtr aSession = ModelAPI_Session::get();
693 DocumentPtr aActiveDoc = aSession->activeDocument();
695 Qt::ItemFlags aNullFlag;
696 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
697 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
701 // Folders under root
702 DocumentPtr aRootDoc = aSession->moduleDocument();
703 if (aRootDoc != aActiveDoc)
706 aDoc = getSubDocument(theIndex.internalPointer());
708 aObj = (ModelAPI_Object*) theIndex.internalPointer();
713 if (aObj->isDisabled())
714 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
716 bool isCompositeSub = false;
717 // An object which is sub-object of a composite object can not be accessible in column 1
718 if (theIndex.column() == 1) {
719 ObjectPtr aObjPtr = aObj->data()->owner();
720 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
721 if (aFeature.get()) {
722 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
724 isCompositeSub = true;
726 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
728 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
730 isCompositeSub = true;
735 return Qt::ItemIsSelectable;
737 if (aObj->document() != aActiveDoc) {
738 // The object could be a root of sub-tree
739 ResultPartPtr aPartRes = getPartResult(aObj);
740 if (aPartRes.get()) {
741 if (aPartRes->partDoc() == aActiveDoc)
747 // A folder under sub-document
748 if (aActiveDoc.get() != aDoc)
754 //******************************************************
755 QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
757 SessionPtr aSession = ModelAPI_Session::get();
758 DocumentPtr aRootDoc = aSession->moduleDocument();
759 if (myXMLReader->isAttachToResult()) { // If document is attached to result
760 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
762 ResultPartPtr aPartRes;
763 for (int i = 0; i < aNb; i++) {
764 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
765 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
766 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
768 if (myXMLReader->rootType() == ModelAPI_Feature::group()) {
769 aRow += foldersCount();
771 return createIndex(aRow, 0, aObj.get());
774 } else { // If document is attached to feature
775 int aNb = aRootDoc->size(ModelAPI_Feature::group());
777 ResultPartPtr aPartRes;
778 for (int i = 0; i < aNb; i++) {
779 aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
780 aPartRes = getPartResult(aObj.get());
781 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
783 if (myXMLReader->rootType() == ModelAPI_Feature::group())
784 aRow += foldersCount();
785 return createIndex(aRow, 0, aObj.get());
789 return QModelIndex();
792 //******************************************************
793 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
795 SessionPtr aSession = ModelAPI_Session::get();
796 DocumentPtr aRootDoc = aSession->moduleDocument();
797 if (theDoc == aRootDoc)
798 return QModelIndex();
800 return findDocumentRootIndex(theDoc.get());
803 //******************************************************
804 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
807 SessionPtr aSession = ModelAPI_Session::get();
808 DocumentPtr aRootDoc = aSession->moduleDocument();
809 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
810 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
811 if (myXMLReader->rootShowEmpty(i))
814 if (aRootDoc->size(myXMLReader->rootFolderType(i)) > 0)
819 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
820 if (myXMLReader->subShowEmpty(i))
823 if (theDoc->size(myXMLReader->subFolderType(i)) > 0)
832 //******************************************************
833 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
836 SessionPtr aSession = ModelAPI_Session::get();
837 DocumentPtr aRootDoc = aSession->moduleDocument();
838 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
839 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
840 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)) {
848 if (theDoc->size(myXMLReader->subFolderType(i)) == 0)
857 //******************************************************
858 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
862 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
863 if (!myXMLReader->rootShowEmpty(i))
864 aResult << myXMLReader->rootFolderType(i).c_str();
867 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
868 if (!myXMLReader->subShowEmpty(i))
869 aResult << myXMLReader->subFolderType(i).c_str();
875 //******************************************************
876 QModelIndex XGUI_DataModel::lastHistoryIndex() const
878 SessionPtr aSession = ModelAPI_Session::get();
879 DocumentPtr aCurDoc = aSession->activeDocument();
880 FeaturePtr aFeature = aCurDoc->currentFeature(true);
881 if (aFeature.get()) {
882 QModelIndex aInd = objectIndex(aFeature);
883 return createIndex(aInd.row(), 1, aInd.internalPointer());
885 if (aCurDoc == aSession->moduleDocument())
886 return createIndex(foldersCount() - 1, 1, -1);
888 return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());
892 //******************************************************
893 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc)
895 SessionPtr aSession = ModelAPI_Session::get();
896 ModelAPI_Document* aDoc = theDoc;
898 aDoc = aSession->moduleDocument().get();
900 bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
904 int aId = myXMLReader->subFolderId(theType);
906 for (int i = 0; i < aId; i++) {
907 if (!myXMLReader->subShowEmpty(i)) {
908 if (aDoc->size(myXMLReader->subFolderType(i)) == 0)
913 int aId = myXMLReader->rootFolderId(theType);
915 for (int i = 0; i < aId; i++) {
916 if (!myXMLReader->rootShowEmpty(i)) {
917 if (aDoc->size(myXMLReader->rootFolderType(i)) == 0)
925 //******************************************************
926 void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex& theParent)
929 removeRows(theRow, theCount, theParent);
930 insertRows(theRow, theCount, theParent);