1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "XGUI_DataModel.h"
22 #include <ModuleBase_IconFactory.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_ResultParameter.h>
27 #include <ModelAPI_AttributeDouble.h>
28 #include <ModelAPI_ResultPart.h>
29 #include <ModelAPI_Feature.h>
30 #include <ModelAPI_CompositeFeature.h>
31 #include <ModelAPI_ResultCompSolid.h>
32 #include <ModelAPI_ResultField.h>
33 #include <ModelAPI_Tools.h>
35 #include <Config_FeatureMessage.h>
36 #include <Config_DataModelReader.h>
38 #include <Events_Loop.h>
43 #define ACTIVE_COLOR QColor(Qt::black)
44 //#define ACTIVE_COLOR QColor(0,72,140)
45 //#define PASSIVE_COLOR Qt::black
47 /// Returns ResultPart object if the given object is a Part feature
48 /// Otherwise returns NULL
50 #define SELECTABLE_COLOR QColor(80, 80, 80)
51 #define DISABLED_COLOR QColor(200, 200, 200)
54 ResultPartPtr getPartResult(ModelAPI_Object* theObj)
56 ModelAPI_Feature* aFeature = dynamic_cast<ModelAPI_Feature*>(theObj);
58 ResultPtr aRes = aFeature->firstResult();
59 if (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group())) {
60 ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aRes);
61 // Use only original parts, not a placement results
62 if (aPartRes == aPartRes->original())
66 return ResultPartPtr();
69 /// Returns pointer on document if the given object is document object
70 ModelAPI_Document* getSubDocument(void* theObj)
72 ModelAPI_Document* aDoc = dynamic_cast<ModelAPI_Document*>((ModelAPI_Entity*)theObj);
79 // Constructor *************************************************
80 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : QAbstractItemModel(theParent)//,
81 //myIsEventsProcessingBlocked(false)
83 Events_Loop* aLoop = Events_Loop::loop();
84 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
85 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
86 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
87 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_ORDER_UPDATED));
88 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
91 XGUI_DataModel::~XGUI_DataModel()
96 //******************************************************
97 void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
99 //if (myIsEventsProcessingBlocked)
101 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
102 std::string aRootType = myXMLReader->rootType();
103 std::string aSubType = myXMLReader->subType();
104 int aNbFolders = foldersCount();
106 // Created object event *******************
107 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
108 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
109 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
110 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
112 std::set<ObjectPtr>::const_iterator aIt;
113 std::string aObjType;
114 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
115 ObjectPtr aObject = (*aIt);
116 // We do not show objects which does not need to be shown in object browser
117 if (!aObject->isInHistory())
120 aObjType = aObject->groupName();
121 DocumentPtr aDoc = aObject->document();
122 if (aDoc == aRootDoc) {
123 // Check that new folders could appear
124 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
125 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
126 if ((aNotEmptyFolder.toStdString() == aObjType) && (aRootDoc->size(aObjType) == 1))
127 // Appears first object in folder which can not be shown empty
128 insertRow(myXMLReader->rootFolderId(aObjType));
131 int aRow = aRootDoc->size(aObjType) - 1;
133 if (aObjType == aRootType) {
134 insertRow(aRow + aNbFolders + 1);
136 int aFolderId = myXMLReader->rootFolderId(aObjType);
137 if (aFolderId != -1) {
138 insertRow(aRow, createIndex(aFolderId, 0, (void*)Q_NULLPTR));
143 // Object created in sub-document
144 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
145 if (aDocRoot.isValid()) {
146 // Check that new folders could appear
147 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
148 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
149 if ((aNotEmptyFolder.toStdString() == aObjType) && (aDoc->size(aObjType) == 1))
150 // Appears first object in folder which can not be shown empty
151 insertRow(myXMLReader->subFolderId(aObjType), aDocRoot);
153 int aRow = aDoc->index(aObject);
155 int aNbSubFolders = foldersCount(aDoc.get());
156 if (aObjType == aSubType) {
157 // List of objects under document root
158 insertRow(aRow + aNbSubFolders, aDocRoot);
160 // List of objects under a folder
162 int aFolderId = folderId(aObjType, aDoc.get());
163 if (aFolderId != -1) {
164 QModelIndex aParentFolder = createIndex(aFolderId, 0, aDoc.get());
165 insertRow(aRow, aParentFolder);
166 emit dataChanged(aParentFolder, aParentFolder);
180 // Deleted object event ***********************
181 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
182 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
183 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
184 DocumentPtr aDoc = aUpdMsg->document();
185 std::set<std::string> aMsgGroups = aUpdMsg->groups();
187 /// Sort groups because RootType deletion has to be done after others
188 std::string aType = (aDoc == aRootDoc)? aRootType : aSubType;
189 std::list<std::string> aGroups;
190 std::set<std::string>::const_iterator aSetIt;
191 for (aSetIt = aMsgGroups.begin(); aSetIt != aMsgGroups.end(); ++aSetIt) {
192 std::string aGroup = (*aSetIt);
194 aGroups.push_back(aGroup);
196 aGroups.push_front(aGroup);
199 std::list<std::string>::const_iterator aIt;
200 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
201 std::string aGroup = (*aIt);
202 if (aDoc == aRootDoc) { // If root objects
203 int aRow = aRootDoc->size(aGroup);
204 if (aGroup == aRootType) {
205 // Process root folder
206 removeRow(aRow + aNbFolders);
207 rebuildBranch(aNbFolders, aRow);
209 // Process root sub-folder
210 int aFolderId = myXMLReader->rootFolderId(aGroup);
211 if (aFolderId != -1) {
212 QModelIndex aFolderIndex = createIndex(aFolderId, 0, (void*)Q_NULLPTR);
213 removeRow(aRow, aFolderIndex);
214 //rebuildBranch(0, aRow);
217 // Check that some folders could erased
218 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
219 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
220 if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup) == 0)) {
221 // Appears first object in folder which can not be shown empty
222 removeRow(myXMLReader->rootFolderId(aGroup));
223 //rebuildBranch(0, aNbFolders + aDoc->size(myXMLReader->rootType()));
228 // Remove row for sub-document
229 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
230 if (aDocRoot.isValid()) {
231 int aRow = aDoc->size(aGroup);
232 int aNbSubFolders = foldersCount(aDoc.get());
233 if (aGroup == aSubType) {
234 // List of objects under document root
235 removeRow(aRow + aNbSubFolders, aDocRoot);
236 rebuildBranch(aNbSubFolders, aRow, aDocRoot);
238 // List of objects under a folder
239 int aFolderId = folderId(aGroup, aDoc.get());
240 if (aFolderId != -1) {
241 QModelIndex aFolderRoot = createIndex(aFolderId, 0, aDoc.get());
242 removeRow(aRow, aFolderRoot);
243 //rebuildBranch(0, aRow, aFolderRoot);
246 // Check that some folders could disappear
247 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
248 int aSize = aDoc->size(aGroup);
249 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
250 if ((aNotEmptyFolder.toStdString() == aGroup) && (aSize == 0)) {
251 // Appears first object in folder which can not be shown empty
252 removeRow(myXMLReader->subFolderId(aGroup), aDocRoot);
253 //rebuildBranch(0, aNbSubFolders + aDoc->size(myXMLReader->subType()), aDocRoot);
263 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
264 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
265 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
266 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
268 std::set<ObjectPtr>::const_iterator aIt;
269 std::string aObjType;
270 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
271 ObjectPtr aObject = (*aIt);
272 if (aObject->data()->isValid()) {
273 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
274 if (aFeature.get() && aFeature->firstResult().get()
275 && (aFeature->firstResult()->groupName() == ModelAPI_ResultField::group())) {
276 ResultFieldPtr aResult =
277 std::dynamic_pointer_cast<ModelAPI_ResultField>(aFeature->firstResult());
278 QModelIndex aIndex = objectIndex(aResult);
279 removeRows(0, aResult->stepsSize(), aIndex);
281 QModelIndex aIndex = objectIndex(aObject);
282 if (aIndex.isValid()) {
283 emit dataChanged(aIndex, aIndex);
291 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
292 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aUpdMsg =
293 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
294 if (aUpdMsg->reordered().get()) {
295 DocumentPtr aDoc = aUpdMsg->reordered()->document();
296 std::string aGroup = aUpdMsg->reordered()->group();
300 if (aDoc == aRootDoc) {
301 // Update a group under root
302 if (aGroup == myXMLReader->rootType()) // Update objects under root
303 aStartId = foldersCount();
304 else // Update objects in folder under root
305 aParent = createIndex(folderId(aGroup), 0, (void*)Q_NULLPTR);
307 // Update a sub-document
308 if (aGroup == myXMLReader->subType()) {
309 // Update sub-document root
310 aParent = findDocumentRootIndex(aDoc.get());
311 aStartId = foldersCount(aDoc.get());
313 // update folder in sub-document
314 aParent = createIndex(folderId(aGroup, aDoc.get()), 0, aDoc.get());
316 int aChildNb = rowCount(aParent);
317 rebuildBranch(aStartId, aChildNb - aStartId, aParent);
321 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
322 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
323 if (aDoc != aRootDoc) {
324 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
325 if (aDocRoot.isValid())
326 emit dataChanged(aDocRoot, aDocRoot);
328 // We have got a new document
334 //******************************************************
335 void XGUI_DataModel::clear()
341 //******************************************************
342 void XGUI_DataModel::rebuildDataTree()
349 //******************************************************
350 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
352 if (theIndex.internalId() == 0) // this is a folder
354 ModelAPI_Object* aObj =
355 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
358 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
361 return aObj->data()->owner();
364 //******************************************************
365 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
367 std::string aType = theObject->groupName();
368 DocumentPtr aDoc = theObject->document();
369 int aRow = aDoc->index(theObject);
371 // it could be a part of complex object
372 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
373 if (aFeature.get()) {
374 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
375 if (aCompFea.get()) {
376 for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
377 if (aCompFea->subFeature(i, true) == theObject) {
384 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
386 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
387 if (aCompRes.get()) {
388 for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
389 if (aCompRes->subResult(i, true) == theObject) {
398 return QModelIndex();
400 return createIndex(aRow, 0, theObject.get());
402 SessionPtr aSession = ModelAPI_Session::get();
403 DocumentPtr aRootDoc = aSession->moduleDocument();
404 if (aDoc == aRootDoc && myXMLReader->rootType() == aType) {
405 // The object from root document
406 aRow += foldersCount();
407 } else if (myXMLReader->subType() == aType) {
408 // The object from sub document
409 aRow += foldersCount(aDoc.get());
411 return createIndex(aRow, 0, theObject.get());
414 //******************************************************
415 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
417 SessionPtr aSession = ModelAPI_Session::get();
418 DocumentPtr aRootDoc = aSession->moduleDocument();
419 int aNbFolders = foldersCount();
420 int theIndexRow = theIndex.row();
422 if ((theRole == Qt::DecorationRole) && (theIndex == lastHistoryIndex()))
423 return QIcon(":pictures/arrow.png");
425 if (theIndex.column() == 1)
428 quintptr aParentId = theIndex.internalId();
429 if (aParentId == 0) { // root folders
431 case Qt::DisplayRole:
432 return QString(myXMLReader->rootFolderName(theIndexRow).c_str()) +
433 QString(" (%1)").arg(rowCount(theIndex));
434 case Qt::DecorationRole:
435 return QIcon(myXMLReader->rootFolderIcon(theIndexRow).c_str());
436 case Qt::ForegroundRole:
438 Qt::ItemFlags aFlags = theIndex.flags();
439 if (aFlags == Qt::ItemFlags())
440 return QBrush(DISABLED_COLOR);
441 if (!aFlags.testFlag(Qt::ItemIsEditable))
442 return QBrush(SELECTABLE_COLOR);
446 } else { // an object or sub-document
447 if (theRole == Qt::ForegroundRole) {
448 Qt::ItemFlags aFlags = theIndex.flags();
449 if (aFlags == Qt::ItemFlags())
450 return QBrush(DISABLED_COLOR);
451 if (!aFlags.testFlag(Qt::ItemIsEditable))
452 return QBrush(SELECTABLE_COLOR);
456 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
457 if (aSubDoc) { // this is a folder of sub document
458 QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
459 int aRow = theIndexRow;
460 while (aMissedIdx.contains(aRow))
462 if (aRow < myXMLReader->subFoldersNumber()) {
464 case Qt::DisplayRole:
465 return QString(myXMLReader->subFolderName(aRow).c_str()) +
466 QString(" (%1)").arg(rowCount(theIndex));
467 case Qt::DecorationRole:
468 return QIcon(myXMLReader->subFolderIcon(aRow).c_str());
472 ModelAPI_Object* aObj =
473 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
476 case Qt::DisplayRole:
478 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
479 ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
480 AttributeDoublePtr aValueAttribute =
481 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
482 QString aVal = QString::number(aValueAttribute->value());
483 QString aTitle = QString(aObj->data()->name().c_str());
484 return aTitle + " = " + aVal;
487 if (aObj->groupName() == myXMLReader->subType()) {
488 ResultPartPtr aPartRes = getPartResult(aObj);
489 if (aPartRes.get()) {
490 if (aPartRes->partDoc().get() == NULL)
491 aSuffix = " (Not loaded)";
494 return aObj->data()->name().c_str() + aSuffix;
496 case Qt::DecorationRole:
497 return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
501 case Qt::DisplayRole:
503 ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
504 dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>
505 ((ModelAPI_Entity*)theIndex.internalPointer());
507 return "Step " + QString::number(aStep->id() + 1) + " " +
508 aStep->field()->textLine(aStep->id()).c_str();
519 //******************************************************
520 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
525 //******************************************************
526 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
528 SessionPtr aSession = ModelAPI_Session::get();
529 if (!aSession->hasModuleDocument())
531 DocumentPtr aRootDoc = aSession->moduleDocument();
533 if (!theParent.isValid()) {
534 // Return number of items in root
535 int aNbFolders = foldersCount();
537 std::string aType = myXMLReader->rootType();
539 aNbItems = aRootDoc->size(aType);
540 return aNbFolders + aNbItems;
543 quintptr aId = theParent.internalId();
545 // this is a folder under root
546 int aParentPos = theParent.row();
547 std::string aType = myXMLReader->rootFolderType(aParentPos);
548 return aRootDoc->size(aType);
550 // It is an object which could have children
551 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
553 // a folder of sub-document
554 QIntList aMissedIdx = missedFolderIndexes(aDoc);
555 int aRow = theParent.row();
556 while (aMissedIdx.contains(aRow))
558 if (aRow < myXMLReader->subFoldersNumber()) {
559 std::string aType = myXMLReader->subFolderType(aRow);
560 return aDoc->size(aType);
563 ModelAPI_Object* aObj =
564 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theParent.internalPointer());
565 // Check for Part feature
566 ResultPartPtr aPartRes = getPartResult(aObj);
567 if (aPartRes.get()) {
568 DocumentPtr aSubDoc = aPartRes->partDoc();
572 int aNbSubFolders = foldersCount(aSubDoc.get());
574 std::string aSubType = myXMLReader->subType();
575 if (!aSubType.empty())
576 aNbSubItems = aSubDoc->size(aSubType);
577 return aNbSubItems + aNbSubFolders;
579 // Check for composite object
580 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
582 return aCompFeature->numberOfSubs(true);
583 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
585 return aCompRes->numberOfSubs(true);
586 ModelAPI_ResultField* aFieldRes = dynamic_cast<ModelAPI_ResultField*>(aObj);
588 return aFieldRes->stepsSize();
595 //******************************************************
596 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
601 //******************************************************
602 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
604 SessionPtr aSession = ModelAPI_Session::get();
605 DocumentPtr aRootDoc = aSession->moduleDocument();
606 int aNbFolders = foldersCount();
610 if (!theParent.isValid()) {
611 if (theRow < aNbFolders) // Return first level folder index
612 return createIndex(theRow, theColumn, (void*)Q_NULLPTR);
613 else { // return object under root index
614 std::string aType = myXMLReader->rootType();
615 int aObjId = theRow - aNbFolders;
616 if (aObjId < aRootDoc->size(aType)) {
617 ObjectPtr aObj = aRootDoc->object(aType, aObjId);
618 aIndex = objectIndex(aObj);
622 quintptr aId = theParent.internalId();
623 int aParentPos = theParent.row();
624 if (aId == 0) { // return object index inside of first level of folders
625 std::string aType = myXMLReader->rootFolderType(aParentPos);
626 if (theRow < aRootDoc->size(aType)) {
627 ObjectPtr aObj = aRootDoc->object(aType, theRow);
628 aIndex = objectIndex(aObj);
631 // It is an object which could have children
632 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
634 // It is a folder of sub-document
635 int aParentRow = aParentPos;
636 QIntList aMissedIdx = missedFolderIndexes(aDoc);
637 while (aMissedIdx.contains(aParentRow))
639 if (aParentRow < myXMLReader->subFoldersNumber()) {
640 std::string aType = myXMLReader->subFolderType(aParentRow);
641 if (theRow < aDoc->size(aType)) {
642 ObjectPtr aObj = aDoc->object(aType, theRow);
643 aIndex = objectIndex(aObj);
647 ModelAPI_Object* aParentObj =
648 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theParent.internalPointer());
650 // Check for Part feature
651 ResultPartPtr aPartRes = getPartResult(aParentObj);
652 if (aPartRes.get()) {
653 DocumentPtr aSubDoc = aPartRes->partDoc();
654 int aNbSubFolders = foldersCount(aSubDoc.get());
655 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
656 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
658 // this is an object under sub document root
659 std::string aType = myXMLReader->subType();
660 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
661 aIndex = objectIndex(aObj);
664 // Check for composite object
665 ModelAPI_CompositeFeature* aCompFeature =
666 dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
668 aIndex = objectIndex(aCompFeature->subFeature(theRow));
670 ModelAPI_ResultCompSolid* aCompRes =
671 dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
673 aIndex = objectIndex(aCompRes->subResult(theRow));
675 ModelAPI_ResultField* aFieldRes =
676 dynamic_cast<ModelAPI_ResultField*>(aParentObj);
678 aIndex = createIndex(theRow, 0, aFieldRes->step(theRow));
687 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
691 //******************************************************
692 static QModelIndex MYLastDeleted;
693 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
695 if (!theIndex.isValid())
696 return QModelIndex();
697 // To avoid additional request about index which was already deleted
698 if (theIndex == MYLastDeleted)
699 return QModelIndex();
701 SessionPtr aSession = ModelAPI_Session::get();
702 quintptr aId = theIndex.internalId();
703 if (aId != 0) { // The object is not a root folder
704 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
706 // It is a folder of sub-document
707 return findDocumentRootIndex(aDoc);
709 ObjectPtr aObj = object(theIndex);
711 // It can b e a step of a field
712 ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
713 dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>
714 ((ModelAPI_Entity*)theIndex.internalPointer());
716 ModelAPI_ResultField* aField = aStep->field();
717 DocumentPtr aDoc = aSession->activeDocument();
719 for(int i = 0; i < aDoc->size(ModelAPI_ResultField::group()); i++) {
720 aFld = aDoc->object(ModelAPI_ResultField::group(), i);
721 if (aFld.get() == aField)
722 return objectIndex(aFld);
725 // To avoid additional request about index which was already deleted
726 // If deleted it causes a crash on delete object from Part
727 MYLastDeleted = theIndex;
728 return QModelIndex();
730 // Check is it object a sub-object of a complex object
731 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
732 if (aFeature.get()) {
733 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
734 if (aCompFea.get()) {
735 return objectIndex(aCompFea);
738 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
740 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
741 if (aCompRes.get()) {
742 return objectIndex(aCompRes);
745 // Use as ordinary object
746 std::string aType = aObj->groupName();
747 DocumentPtr aRootDoc = aSession->moduleDocument();
748 DocumentPtr aSubDoc = aObj->document();
749 if (aSubDoc == aRootDoc) {
750 if (aType == myXMLReader->rootType())
751 return QModelIndex();
753 // return first level of folder index
754 int aFolderId = myXMLReader->rootFolderId(aType);
755 // Items in a one row must have the same parent
756 return createIndex(aFolderId, 0, (void*)Q_NULLPTR);
759 if (aType == myXMLReader->subType())
760 return findDocumentRootIndex(aSubDoc.get());
762 // return first level of folder index
763 int aFolderId = folderId(aType, aSubDoc.get());
764 // Items in a one row must have the same parent
765 return createIndex(aFolderId, 0, aSubDoc.get());
769 return QModelIndex();
772 //******************************************************
773 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
775 return rowCount(theParent) > 0;
778 //******************************************************
779 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
781 beginInsertRows(theParent, theRow, theRow + theCount - 1);
787 //******************************************************
788 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
790 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
795 //******************************************************
796 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
798 quintptr aIt = theIndex.internalId();
799 ModelAPI_Object* aObj = 0;
800 ModelAPI_Document* aDoc = 0;
801 SessionPtr aSession = ModelAPI_Session::get();
802 DocumentPtr aActiveDoc = aSession->activeDocument();
804 Qt::ItemFlags aNullFlag;
805 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
806 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
810 // Folders under root
811 DocumentPtr aRootDoc = aSession->moduleDocument();
812 if (aRootDoc != aActiveDoc)
815 aDoc = getSubDocument(theIndex.internalPointer());
817 aObj = dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
822 if (aObj->isDisabled())
823 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
825 if (aSession->moduleDocument() != aObj->document())
826 if (aActiveDoc != aObj->document())
827 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
829 bool isCompositeSub = false;
830 // An object which is sub-object of a composite object can not be accessible in column 1
831 if (theIndex.column() == 1) {
832 ObjectPtr aObjPtr = aObj->data()->owner();
833 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
834 if (aFeature.get()) {
835 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
837 isCompositeSub = true;
839 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
841 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
843 isCompositeSub = true;
848 return Qt::ItemIsSelectable;
850 if (aObj->document() != aActiveDoc) {
851 // The object could be a root of sub-tree
852 ResultPartPtr aPartRes = getPartResult(aObj);
853 if (aPartRes.get()) {
854 if (aPartRes->partDoc() == aActiveDoc)
860 // A folder under sub-document
861 if (aActiveDoc.get() != aDoc)
867 //******************************************************
868 QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
870 SessionPtr aSession = ModelAPI_Session::get();
871 DocumentPtr aRootDoc = aSession->moduleDocument();
872 if (myXMLReader->isAttachToResult()) { // If document is attached to result
873 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
875 ResultPartPtr aPartRes;
876 for (int i = 0; i < aNb; i++) {
877 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
878 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
879 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
881 if (myXMLReader->rootType() == ModelAPI_Feature::group()) {
882 aRow += foldersCount();
884 return createIndex(aRow, 0, aObj.get());
887 } else { // If document is attached to feature
888 int aNb = aRootDoc->size(ModelAPI_Feature::group());
890 ResultPartPtr aPartRes;
891 for (int i = 0; i < aNb; i++) {
892 aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
893 aPartRes = getPartResult(aObj.get());
894 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
896 if (myXMLReader->rootType() == ModelAPI_Feature::group())
897 aRow += foldersCount();
898 return createIndex(aRow, 0, aObj.get());
902 return QModelIndex();
905 //******************************************************
906 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
908 SessionPtr aSession = ModelAPI_Session::get();
909 DocumentPtr aRootDoc = aSession->moduleDocument();
910 if (theDoc == aRootDoc)
911 return QModelIndex();
913 return findDocumentRootIndex(theDoc.get());
916 //******************************************************
917 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
920 SessionPtr aSession = ModelAPI_Session::get();
921 DocumentPtr aRootDoc = aSession->moduleDocument();
922 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
923 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
924 if (myXMLReader->rootShowEmpty(i))
927 if (aRootDoc->size(myXMLReader->rootFolderType(i)) > 0)
932 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
933 if (myXMLReader->subShowEmpty(i))
936 if (theDoc->size(myXMLReader->subFolderType(i)) > 0)
945 //******************************************************
946 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
949 SessionPtr aSession = ModelAPI_Session::get();
950 DocumentPtr aRootDoc = aSession->moduleDocument();
951 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
952 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
953 if (!myXMLReader->rootShowEmpty(i)) {
954 if (aRootDoc->size(myXMLReader->rootFolderType(i)) == 0)
959 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
960 if (!myXMLReader->subShowEmpty(i)) {
961 if (theDoc->size(myXMLReader->subFolderType(i)) == 0)
970 //******************************************************
971 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
975 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
976 if (!myXMLReader->rootShowEmpty(i))
977 aResult << myXMLReader->rootFolderType(i).c_str();
980 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
981 if (!myXMLReader->subShowEmpty(i))
982 aResult << myXMLReader->subFolderType(i).c_str();
988 //******************************************************
989 QModelIndex XGUI_DataModel::lastHistoryIndex() const
991 SessionPtr aSession = ModelAPI_Session::get();
992 DocumentPtr aCurDoc = aSession->activeDocument();
993 FeaturePtr aFeature = aCurDoc->currentFeature(true);
994 if (aFeature.get()) {
995 QModelIndex aInd = objectIndex(aFeature);
996 return createIndex(aInd.row(), 1, aInd.internalPointer());
998 if (aCurDoc == aSession->moduleDocument())
999 return createIndex(foldersCount() - 1, 1, -1);
1001 return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());
1005 //******************************************************
1006 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc) const
1008 SessionPtr aSession = ModelAPI_Session::get();
1009 ModelAPI_Document* aDoc = theDoc;
1011 aDoc = aSession->moduleDocument().get();
1013 bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
1017 int aId = myXMLReader->subFolderId(theType);
1019 for (int i = 0; i < aId; i++) {
1020 if (!myXMLReader->subShowEmpty(i)) {
1021 if (aDoc->size(myXMLReader->subFolderType(i)) == 0)
1026 int aId = myXMLReader->rootFolderId(theType);
1028 for (int i = 0; i < aId; i++) {
1029 if (!myXMLReader->rootShowEmpty(i)) {
1030 if (aDoc->size(myXMLReader->rootFolderType(i)) == 0)
1038 //******************************************************
1039 void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex& theParent)
1042 removeRows(theRow, theCount, theParent);
1043 insertRows(theRow, theCount, theParent);
1047 //******************************************************
1048 //bool XGUI_DataModel::blockEventsProcessing(const bool theState)
1050 // bool aPreviousState = myIsEventsProcessingBlocked;
1051 // myIsEventsProcessingBlocked = theState;
1052 // return aPreviousState;