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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "XGUI_DataModel.h"
23 #include <ModuleBase_IconFactory.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_Events.h>
27 #include <ModelAPI_ResultParameter.h>
28 #include <ModelAPI_AttributeDouble.h>
29 #include <ModelAPI_ResultPart.h>
30 #include <ModelAPI_Feature.h>
31 #include <ModelAPI_CompositeFeature.h>
32 #include <ModelAPI_ResultCompSolid.h>
33 #include <ModelAPI_ResultField.h>
34 #include <ModelAPI_Tools.h>
36 #include <Config_FeatureMessage.h>
37 #include <Config_DataModelReader.h>
39 #include <Events_Loop.h>
44 #define ACTIVE_COLOR QColor(Qt::black)
45 //#define ACTIVE_COLOR QColor(0,72,140)
46 //#define PASSIVE_COLOR Qt::black
48 /// Returns ResultPart object if the given object is a Part feature
49 /// Otherwise returns NULL
51 #define SELECTABLE_COLOR QColor(80, 80, 80)
52 #define DISABLED_COLOR QColor(200, 200, 200)
55 ResultPartPtr getPartResult(ModelAPI_Object* theObj)
57 ModelAPI_Feature* aFeature = dynamic_cast<ModelAPI_Feature*>(theObj);
59 ResultPtr aRes = aFeature->firstResult();
60 if (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group())) {
61 ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aRes);
62 // Use only original parts, not a placement results
63 if (aPartRes == aPartRes->original())
67 return ResultPartPtr();
70 /// Returns pointer on document if the given object is document object
71 ModelAPI_Document* getSubDocument(void* theObj)
73 ModelAPI_Document* aDoc = dynamic_cast<ModelAPI_Document*>((ModelAPI_Entity*)theObj);
80 // Constructor *************************************************
81 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : QAbstractItemModel(theParent)//,
82 //myIsEventsProcessingBlocked(false)
84 Events_Loop* aLoop = Events_Loop::loop();
85 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
86 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
87 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
88 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_ORDER_UPDATED));
89 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
92 XGUI_DataModel::~XGUI_DataModel()
97 //******************************************************
98 void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
100 //if (myIsEventsProcessingBlocked)
102 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
103 std::string aRootType = myXMLReader->rootType();
104 std::string aSubType = myXMLReader->subType();
105 int aNbFolders = foldersCount();
107 // Created object event *******************
108 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
109 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
110 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
111 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
113 std::set<ObjectPtr>::const_iterator aIt;
114 std::string aObjType;
115 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
116 ObjectPtr aObject = (*aIt);
117 // We do not show objects which does not need to be shown in object browser
118 if (!aObject->isInHistory())
121 aObjType = aObject->groupName();
122 DocumentPtr aDoc = aObject->document();
123 if (aDoc == aRootDoc) {
124 // Check that new folders could appear
125 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
126 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
127 if ((aNotEmptyFolder.toStdString() == aObjType) && (aRootDoc->size(aObjType) == 1))
128 // Appears first object in folder which can not be shown empty
129 insertRow(myXMLReader->rootFolderId(aObjType));
132 int aRow = aRootDoc->size(aObjType) - 1;
134 if (aObjType == aRootType) {
135 insertRow(aRow + aNbFolders + 1);
137 int aFolderId = myXMLReader->rootFolderId(aObjType);
138 if (aFolderId != -1) {
139 insertRow(aRow, createIndex(aFolderId, 0, (void*)Q_NULLPTR));
144 // Object created in sub-document
145 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
146 if (aDocRoot.isValid()) {
147 // Check that new folders could appear
148 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
149 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
150 if ((aNotEmptyFolder.toStdString() == aObjType) && (aDoc->size(aObjType) == 1))
151 // Appears first object in folder which can not be shown empty
152 insertRow(myXMLReader->subFolderId(aObjType), aDocRoot);
154 int aRow = aDoc->index(aObject);
156 int aNbSubFolders = foldersCount(aDoc.get());
157 if (aObjType == aSubType) {
158 // List of objects under document root
159 insertRow(aRow + aNbSubFolders, aDocRoot);
161 // List of objects under a folder
163 int aFolderId = folderId(aObjType, aDoc.get());
164 if (aFolderId != -1) {
165 QModelIndex aParentFolder = createIndex(aFolderId, 0, aDoc.get());
166 insertRow(aRow, aParentFolder);
167 emit dataChanged(aParentFolder, aParentFolder);
181 // Deleted object event ***********************
182 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
183 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
184 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
185 DocumentPtr aDoc = aUpdMsg->document();
186 std::set<std::string> aMsgGroups = aUpdMsg->groups();
188 /// Sort groups because RootType deletion has to be done after others
189 std::string aType = (aDoc == aRootDoc)? aRootType : aSubType;
190 std::list<std::string> aGroups;
191 std::set<std::string>::const_iterator aSetIt;
192 for (aSetIt = aMsgGroups.begin(); aSetIt != aMsgGroups.end(); ++aSetIt) {
193 std::string aGroup = (*aSetIt);
195 aGroups.push_back(aGroup);
197 aGroups.push_front(aGroup);
200 std::list<std::string>::const_iterator aIt;
201 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
202 std::string aGroup = (*aIt);
203 if (aDoc == aRootDoc) { // If root objects
204 int aRow = aRootDoc->size(aGroup);
205 if (aGroup == aRootType) {
206 // Process root folder
207 removeRow(aRow + aNbFolders);
208 rebuildBranch(aNbFolders, aRow);
210 // Process root sub-folder
211 int aFolderId = myXMLReader->rootFolderId(aGroup);
212 if (aFolderId != -1) {
213 QModelIndex aFolderIndex = createIndex(aFolderId, 0, (void*)Q_NULLPTR);
214 removeRow(aRow, aFolderIndex);
215 //rebuildBranch(0, aRow);
218 // Check that some folders could erased
219 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
220 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
221 if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup) == 0)) {
222 // Appears first object in folder which can not be shown empty
223 removeRow(myXMLReader->rootFolderId(aGroup));
224 //rebuildBranch(0, aNbFolders + aDoc->size(myXMLReader->rootType()));
229 // Remove row for sub-document
230 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
231 if (aDocRoot.isValid()) {
232 int aRow = aDoc->size(aGroup);
233 int aNbSubFolders = foldersCount(aDoc.get());
234 if (aGroup == aSubType) {
235 // List of objects under document root
236 removeRow(aRow + aNbSubFolders, aDocRoot);
237 rebuildBranch(aNbSubFolders, aRow, aDocRoot);
239 // List of objects under a folder
240 int aFolderId = folderId(aGroup, aDoc.get());
241 if (aFolderId != -1) {
242 QModelIndex aFolderRoot = createIndex(aFolderId, 0, aDoc.get());
243 removeRow(aRow, aFolderRoot);
244 //rebuildBranch(0, aRow, aFolderRoot);
247 // Check that some folders could disappear
248 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
249 int aSize = aDoc->size(aGroup);
250 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
251 if ((aNotEmptyFolder.toStdString() == aGroup) && (aSize == 0)) {
252 // Appears first object in folder which can not be shown empty
253 removeRow(myXMLReader->subFolderId(aGroup), aDocRoot);
254 //rebuildBranch(0, aNbSubFolders + aDoc->size(myXMLReader->subType()), aDocRoot);
264 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
265 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
266 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
267 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
269 std::set<ObjectPtr>::const_iterator aIt;
270 std::string aObjType;
271 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
272 ObjectPtr aObject = (*aIt);
273 if (aObject->data()->isValid()) {
274 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
275 if (aFeature.get() && aFeature->firstResult().get()
276 && (aFeature->firstResult()->groupName() == ModelAPI_ResultField::group())) {
277 ResultFieldPtr aResult =
278 std::dynamic_pointer_cast<ModelAPI_ResultField>(aFeature->firstResult());
279 QModelIndex aIndex = objectIndex(aResult);
280 removeRows(0, aResult->stepsSize(), aIndex);
282 QModelIndex aIndex = objectIndex(aObject);
283 if (aIndex.isValid()) {
284 emit dataChanged(aIndex, aIndex);
292 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
293 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aUpdMsg =
294 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
295 if (aUpdMsg->reordered().get()) {
296 DocumentPtr aDoc = aUpdMsg->reordered()->document();
297 std::string aGroup = aUpdMsg->reordered()->group();
301 if (aDoc == aRootDoc) {
302 // Update a group under root
303 if (aGroup == myXMLReader->rootType()) // Update objects under root
304 aStartId = foldersCount();
305 else // Update objects in folder under root
306 aParent = createIndex(folderId(aGroup), 0, (void*)Q_NULLPTR);
308 // Update a sub-document
309 if (aGroup == myXMLReader->subType()) {
310 // Update sub-document root
311 aParent = findDocumentRootIndex(aDoc.get());
312 aStartId = foldersCount(aDoc.get());
314 // update folder in sub-document
315 aParent = createIndex(folderId(aGroup, aDoc.get()), 0, aDoc.get());
317 int aChildNb = rowCount(aParent);
318 rebuildBranch(aStartId, aChildNb - aStartId, aParent);
322 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
323 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
324 if (aDoc != aRootDoc) {
325 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
326 if (aDocRoot.isValid())
327 emit dataChanged(aDocRoot, aDocRoot);
329 // We have got a new document
335 //******************************************************
336 void XGUI_DataModel::clear()
342 //******************************************************
343 void XGUI_DataModel::rebuildDataTree()
350 //******************************************************
351 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
353 if (theIndex.internalId() == 0) // this is a folder
355 ModelAPI_Object* aObj =
356 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
359 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
362 return aObj->data()->owner();
365 //******************************************************
366 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
368 std::string aType = theObject->groupName();
369 DocumentPtr aDoc = theObject->document();
370 int aRow = aDoc->index(theObject);
372 // it could be a part of complex object
373 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
374 if (aFeature.get()) {
375 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
376 if (aCompFea.get()) {
377 for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
378 if (aCompFea->subFeature(i, true) == theObject) {
385 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
387 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
388 if (aCompRes.get()) {
389 aRow = ModelAPI_Tools::compSolidIndex(aResult);
394 return QModelIndex();
396 return createIndex(aRow, 0, theObject.get());
398 SessionPtr aSession = ModelAPI_Session::get();
399 DocumentPtr aRootDoc = aSession->moduleDocument();
400 if (aDoc == aRootDoc && myXMLReader->rootType() == aType) {
401 // The object from root document
402 aRow += foldersCount();
403 } else if (myXMLReader->subType() == aType) {
404 // The object from sub document
405 aRow += foldersCount(aDoc.get());
407 return createIndex(aRow, 0, theObject.get());
410 //******************************************************
411 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
413 SessionPtr aSession = ModelAPI_Session::get();
414 DocumentPtr aRootDoc = aSession->moduleDocument();
415 int aNbFolders = foldersCount();
416 int theIndexRow = theIndex.row();
418 if ((theRole == Qt::DecorationRole) && (theIndex == lastHistoryIndex()))
419 return QIcon(":pictures/arrow.png");
421 if (theIndex.column() == 1)
424 quintptr aParentId = theIndex.internalId();
425 if (aParentId == 0) { // root folders
427 case Qt::DisplayRole:
428 return QString(myXMLReader->rootFolderName(theIndexRow).c_str()) +
429 QString(" (%1)").arg(rowCount(theIndex));
430 case Qt::DecorationRole:
431 return QIcon(myXMLReader->rootFolderIcon(theIndexRow).c_str());
432 case Qt::ForegroundRole:
434 Qt::ItemFlags aFlags = theIndex.flags();
435 if (aFlags == Qt::ItemFlags())
436 return QBrush(DISABLED_COLOR);
437 if (!aFlags.testFlag(Qt::ItemIsEditable))
438 return QBrush(SELECTABLE_COLOR);
442 } else { // an object or sub-document
443 if (theRole == Qt::ForegroundRole) {
444 Qt::ItemFlags aFlags = theIndex.flags();
445 if (aFlags == Qt::ItemFlags())
446 return QBrush(DISABLED_COLOR);
447 if (!aFlags.testFlag(Qt::ItemIsEditable))
448 return QBrush(SELECTABLE_COLOR);
452 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
453 if (aSubDoc) { // this is a folder of sub document
454 QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
455 int aRow = theIndexRow;
456 while (aMissedIdx.contains(aRow))
458 if (aRow < myXMLReader->subFoldersNumber()) {
460 case Qt::DisplayRole:
461 return QString(myXMLReader->subFolderName(aRow).c_str()) +
462 QString(" (%1)").arg(rowCount(theIndex));
463 case Qt::DecorationRole:
464 return QIcon(myXMLReader->subFolderIcon(aRow).c_str());
468 ModelAPI_Object* aObj =
469 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
472 case Qt::DisplayRole:
474 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
475 ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
476 AttributeDoublePtr aValueAttribute =
477 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
478 QString aVal = QString::number(aValueAttribute->value());
479 QString aTitle = QString(aObj->data()->name().c_str());
480 return aTitle + " = " + aVal;
483 if (aObj->groupName() == myXMLReader->subType()) {
484 ResultPartPtr aPartRes = getPartResult(aObj);
485 if (aPartRes.get()) {
486 if (aPartRes->partDoc().get() == NULL)
487 aSuffix = " (Not loaded)";
490 return aObj->data()->name().c_str() + aSuffix;
492 case Qt::DecorationRole:
493 return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
497 case Qt::DisplayRole:
499 ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
500 dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>
501 ((ModelAPI_Entity*)theIndex.internalPointer());
503 return "Step " + QString::number(aStep->id() + 1) + " " +
504 aStep->field()->textLine(aStep->id()).c_str();
515 //******************************************************
516 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
521 //******************************************************
522 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
524 SessionPtr aSession = ModelAPI_Session::get();
525 if (!aSession->hasModuleDocument())
527 DocumentPtr aRootDoc = aSession->moduleDocument();
529 if (!theParent.isValid()) {
530 // Return number of items in root
531 int aNbFolders = foldersCount();
533 std::string aType = myXMLReader->rootType();
535 aNbItems = aRootDoc->size(aType);
536 return aNbFolders + aNbItems;
539 quintptr aId = theParent.internalId();
541 // this is a folder under root
542 int aParentPos = theParent.row();
543 std::string aType = myXMLReader->rootFolderType(aParentPos);
544 return aRootDoc->size(aType);
546 // It is an object which could have children
547 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
549 // a folder of sub-document
550 QIntList aMissedIdx = missedFolderIndexes(aDoc);
551 int aRow = theParent.row();
552 while (aMissedIdx.contains(aRow))
554 if (aRow < myXMLReader->subFoldersNumber()) {
555 std::string aType = myXMLReader->subFolderType(aRow);
556 return aDoc->size(aType);
559 ModelAPI_Object* aObj =
560 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theParent.internalPointer());
561 // Check for Part feature
562 ResultPartPtr aPartRes = getPartResult(aObj);
563 if (aPartRes.get()) {
564 DocumentPtr aSubDoc = aPartRes->partDoc();
568 int aNbSubFolders = foldersCount(aSubDoc.get());
570 std::string aSubType = myXMLReader->subType();
571 if (!aSubType.empty())
572 aNbSubItems = aSubDoc->size(aSubType);
573 return aNbSubItems + aNbSubFolders;
575 // Check for composite object
576 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
578 return aCompFeature->numberOfSubs(true);
579 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
581 return aCompRes->numberOfSubs(true);
582 ModelAPI_ResultField* aFieldRes = dynamic_cast<ModelAPI_ResultField*>(aObj);
584 return aFieldRes->stepsSize();
591 //******************************************************
592 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
597 //******************************************************
598 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
600 SessionPtr aSession = ModelAPI_Session::get();
601 DocumentPtr aRootDoc = aSession->moduleDocument();
602 int aNbFolders = foldersCount();
606 if (!theParent.isValid()) {
607 if (theRow < aNbFolders) // Return first level folder index
608 return createIndex(theRow, theColumn, (void*)Q_NULLPTR);
609 else { // return object under root index
610 std::string aType = myXMLReader->rootType();
611 int aObjId = theRow - aNbFolders;
612 if (aObjId < aRootDoc->size(aType)) {
613 ObjectPtr aObj = aRootDoc->object(aType, aObjId);
614 aIndex = objectIndex(aObj);
618 quintptr aId = theParent.internalId();
619 int aParentPos = theParent.row();
620 if (aId == 0) { // return object index inside of first level of folders
621 std::string aType = myXMLReader->rootFolderType(aParentPos);
622 if (theRow < aRootDoc->size(aType)) {
623 ObjectPtr aObj = aRootDoc->object(aType, theRow);
624 aIndex = objectIndex(aObj);
627 // It is an object which could have children
628 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
630 // It is a folder of sub-document
631 int aParentRow = aParentPos;
632 QIntList aMissedIdx = missedFolderIndexes(aDoc);
633 while (aMissedIdx.contains(aParentRow))
635 if (aParentRow < myXMLReader->subFoldersNumber()) {
636 std::string aType = myXMLReader->subFolderType(aParentRow);
637 if (theRow < aDoc->size(aType)) {
638 ObjectPtr aObj = aDoc->object(aType, theRow);
639 aIndex = objectIndex(aObj);
643 ModelAPI_Object* aParentObj =
644 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theParent.internalPointer());
646 // Check for Part feature
647 ResultPartPtr aPartRes = getPartResult(aParentObj);
648 if (aPartRes.get()) {
649 DocumentPtr aSubDoc = aPartRes->partDoc();
650 int aNbSubFolders = foldersCount(aSubDoc.get());
651 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
652 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
654 // this is an object under sub document root
655 std::string aType = myXMLReader->subType();
656 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
657 aIndex = objectIndex(aObj);
660 // Check for composite object
661 ModelAPI_CompositeFeature* aCompFeature =
662 dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
664 aIndex = objectIndex(aCompFeature->subFeature(theRow));
666 ModelAPI_ResultCompSolid* aCompRes =
667 dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
669 aIndex = objectIndex(aCompRes->subResult(theRow));
671 ModelAPI_ResultField* aFieldRes =
672 dynamic_cast<ModelAPI_ResultField*>(aParentObj);
674 aIndex = createIndex(theRow, 0, aFieldRes->step(theRow));
683 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
687 //******************************************************
688 static QModelIndex MYLastDeleted;
689 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
691 if (!theIndex.isValid())
692 return QModelIndex();
693 // To avoid additional request about index which was already deleted
694 if (theIndex == MYLastDeleted)
695 return QModelIndex();
697 SessionPtr aSession = ModelAPI_Session::get();
698 quintptr aId = theIndex.internalId();
699 if (aId != 0) { // The object is not a root folder
700 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
702 // It is a folder of sub-document
703 return findDocumentRootIndex(aDoc);
705 ObjectPtr aObj = object(theIndex);
707 // It can b e a step of a field
708 ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
709 dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>
710 ((ModelAPI_Entity*)theIndex.internalPointer());
712 ModelAPI_ResultField* aField = aStep->field();
713 DocumentPtr aDoc = aSession->activeDocument();
715 for(int i = 0; i < aDoc->size(ModelAPI_ResultField::group()); i++) {
716 aFld = aDoc->object(ModelAPI_ResultField::group(), i);
717 if (aFld.get() == aField)
718 return objectIndex(aFld);
721 // To avoid additional request about index which was already deleted
722 // If deleted it causes a crash on delete object from Part
723 MYLastDeleted = theIndex;
724 return QModelIndex();
726 // Check is it object a sub-object of a complex object
727 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
728 if (aFeature.get()) {
729 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
730 if (aCompFea.get()) {
731 return objectIndex(aCompFea);
734 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
736 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
737 if (aCompRes.get()) {
738 return objectIndex(aCompRes);
741 // Use as ordinary object
742 std::string aType = aObj->groupName();
743 DocumentPtr aRootDoc = aSession->moduleDocument();
744 DocumentPtr aSubDoc = aObj->document();
745 if (aSubDoc == aRootDoc) {
746 if (aType == myXMLReader->rootType())
747 return QModelIndex();
749 // return first level of folder index
750 int aFolderId = myXMLReader->rootFolderId(aType);
751 // Items in a one row must have the same parent
752 return createIndex(aFolderId, 0, (void*)Q_NULLPTR);
755 if (aType == myXMLReader->subType())
756 return findDocumentRootIndex(aSubDoc.get());
758 // return first level of folder index
759 int aFolderId = folderId(aType, aSubDoc.get());
760 // Items in a one row must have the same parent
761 return createIndex(aFolderId, 0, aSubDoc.get());
765 return QModelIndex();
768 //******************************************************
769 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
771 return rowCount(theParent) > 0;
774 //******************************************************
775 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
777 beginInsertRows(theParent, theRow, theRow + theCount - 1);
783 //******************************************************
784 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
786 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
791 //******************************************************
792 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
794 quintptr aIt = theIndex.internalId();
795 ModelAPI_Object* aObj = 0;
796 ModelAPI_Document* aDoc = 0;
797 SessionPtr aSession = ModelAPI_Session::get();
798 DocumentPtr aActiveDoc = aSession->activeDocument();
800 Qt::ItemFlags aNullFlag;
801 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
802 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
806 // Folders under root
807 DocumentPtr aRootDoc = aSession->moduleDocument();
808 if (aRootDoc != aActiveDoc)
811 aDoc = getSubDocument(theIndex.internalPointer());
813 aObj = dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
818 if (aObj->isDisabled())
819 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
821 if (aSession->moduleDocument() != aObj->document())
822 if (aActiveDoc != aObj->document())
823 return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
825 bool isCompositeSub = false;
826 // An object which is sub-object of a composite object can not be accessible in column 1
827 if (theIndex.column() == 1) {
828 ObjectPtr aObjPtr = aObj->data()->owner();
829 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
830 if (aFeature.get()) {
831 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
833 isCompositeSub = true;
835 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
837 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
839 isCompositeSub = true;
844 return Qt::ItemIsSelectable;
846 if (aObj->document() != aActiveDoc) {
847 // The object could be a root of sub-tree
848 ResultPartPtr aPartRes = getPartResult(aObj);
849 if (aPartRes.get()) {
850 if (aPartRes->partDoc() == aActiveDoc)
856 // A folder under sub-document
857 if (aActiveDoc.get() != aDoc)
863 //******************************************************
864 QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
866 SessionPtr aSession = ModelAPI_Session::get();
867 DocumentPtr aRootDoc = aSession->moduleDocument();
868 if (myXMLReader->isAttachToResult()) { // If document is attached to result
869 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
871 ResultPartPtr aPartRes;
872 for (int i = 0; i < aNb; i++) {
873 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
874 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
875 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
877 if (myXMLReader->rootType() == ModelAPI_Feature::group()) {
878 aRow += foldersCount();
880 return createIndex(aRow, 0, aObj.get());
883 } else { // If document is attached to feature
884 int aNb = aRootDoc->size(ModelAPI_Feature::group());
886 ResultPartPtr aPartRes;
887 for (int i = 0; i < aNb; i++) {
888 aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
889 aPartRes = getPartResult(aObj.get());
890 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
892 if (myXMLReader->rootType() == ModelAPI_Feature::group())
893 aRow += foldersCount();
894 return createIndex(aRow, 0, aObj.get());
898 return QModelIndex();
901 //******************************************************
902 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
904 SessionPtr aSession = ModelAPI_Session::get();
905 DocumentPtr aRootDoc = aSession->moduleDocument();
906 if (theDoc == aRootDoc)
907 return QModelIndex();
909 return findDocumentRootIndex(theDoc.get());
912 //******************************************************
913 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
916 SessionPtr aSession = ModelAPI_Session::get();
917 DocumentPtr aRootDoc = aSession->moduleDocument();
918 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
919 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
920 if (myXMLReader->rootShowEmpty(i))
923 if (aRootDoc->size(myXMLReader->rootFolderType(i)) > 0)
928 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
929 if (myXMLReader->subShowEmpty(i))
932 if (theDoc->size(myXMLReader->subFolderType(i)) > 0)
941 //******************************************************
942 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
945 SessionPtr aSession = ModelAPI_Session::get();
946 DocumentPtr aRootDoc = aSession->moduleDocument();
947 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
948 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
949 if (!myXMLReader->rootShowEmpty(i)) {
950 if (aRootDoc->size(myXMLReader->rootFolderType(i)) == 0)
955 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
956 if (!myXMLReader->subShowEmpty(i)) {
957 if (theDoc->size(myXMLReader->subFolderType(i)) == 0)
966 //******************************************************
967 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
971 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
972 if (!myXMLReader->rootShowEmpty(i))
973 aResult << myXMLReader->rootFolderType(i).c_str();
976 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
977 if (!myXMLReader->subShowEmpty(i))
978 aResult << myXMLReader->subFolderType(i).c_str();
984 //******************************************************
985 QModelIndex XGUI_DataModel::lastHistoryIndex() const
987 SessionPtr aSession = ModelAPI_Session::get();
988 DocumentPtr aCurDoc = aSession->activeDocument();
989 FeaturePtr aFeature = aCurDoc->currentFeature(true);
990 if (aFeature.get()) {
991 QModelIndex aInd = objectIndex(aFeature);
992 return createIndex(aInd.row(), 1, aInd.internalPointer());
994 if (aCurDoc == aSession->moduleDocument())
995 return createIndex(foldersCount() - 1, 1, -1);
997 return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());
1001 //******************************************************
1002 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc) const
1004 SessionPtr aSession = ModelAPI_Session::get();
1005 ModelAPI_Document* aDoc = theDoc;
1007 aDoc = aSession->moduleDocument().get();
1009 bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
1013 int aId = myXMLReader->subFolderId(theType);
1015 for (int i = 0; i < aId; i++) {
1016 if (!myXMLReader->subShowEmpty(i)) {
1017 if (aDoc->size(myXMLReader->subFolderType(i)) == 0)
1022 int aId = myXMLReader->rootFolderId(theType);
1024 for (int i = 0; i < aId; i++) {
1025 if (!myXMLReader->rootShowEmpty(i)) {
1026 if (aDoc->size(myXMLReader->rootFolderType(i)) == 0)
1034 //******************************************************
1035 void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex& theParent)
1038 removeRows(theRow, theCount, theParent);
1039 insertRows(theRow, theCount, theParent);
1043 //******************************************************
1044 //bool XGUI_DataModel::blockEventsProcessing(const bool theState)
1046 // bool aPreviousState = myIsEventsProcessingBlocked;
1047 // myIsEventsProcessingBlocked = theState;
1048 // return aPreviousState;