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"
22 #include "XGUI_Workshop.h"
23 #include "XGUI_ObjectsBrowser.h"
24 #include "XGUI_Displayer.h"
26 #include <ModuleBase_IconFactory.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Events.h>
30 #include <ModelAPI_ResultParameter.h>
31 #include <ModelAPI_AttributeDouble.h>
32 #include <ModelAPI_ResultPart.h>
33 #include <ModelAPI_Feature.h>
34 #include <ModelAPI_CompositeFeature.h>
35 #include <ModelAPI_ResultCompSolid.h>
36 #include <ModelAPI_ResultField.h>
37 #include <ModelAPI_Tools.h>
38 #include <ModelAPI_Folder.h>
39 #include <ModelAPI_AttributeReference.h>
41 #include <Config_FeatureMessage.h>
42 #include <Config_DataModelReader.h>
44 #include <Events_Loop.h>
49 #define ACTIVE_COLOR QColor(Qt::black)
50 //#define ACTIVE_COLOR QColor(0,72,140)
51 //#define PASSIVE_COLOR Qt::black
53 /// Returns ResultPart object if the given object is a Part feature
54 /// Otherwise returns NULL
56 #define SELECTABLE_COLOR QColor(80, 80, 80)
57 #define DISABLED_COLOR QColor(200, 200, 200)
60 ResultPartPtr getPartResult(ModelAPI_Object* theObj)
62 ModelAPI_Feature* aFeature = dynamic_cast<ModelAPI_Feature*>(theObj);
64 ResultPtr aRes = aFeature->firstResult();
65 if (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group())) {
66 ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aRes);
67 // Use only original parts, not a placement results
68 if (aPartRes == aPartRes->original())
72 return ResultPartPtr();
75 /// Returns pointer on document if the given object is document object
76 ModelAPI_Document* getSubDocument(void* theObj)
78 ModelAPI_Document* aDoc = 0;
80 aDoc = dynamic_cast<ModelAPI_Document*>((ModelAPI_Entity*)theObj);
88 // Constructor *************************************************
89 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : QAbstractItemModel(theParent)//,
90 //myIsEventsProcessingBlocked(false)
92 XGUI_ObjectsBrowser* aOB = qobject_cast<XGUI_ObjectsBrowser*>(theParent);
93 myWorkshop = aOB->workshop();
95 Events_Loop* aLoop = Events_Loop::loop();
96 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
97 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
98 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
99 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_ORDER_UPDATED));
100 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
103 XGUI_DataModel::~XGUI_DataModel()
108 //******************************************************
109 void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
111 //if (myIsEventsProcessingBlocked)
113 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
114 std::string aRootType = myXMLReader->rootType();
115 std::string aSubType = myXMLReader->subType();
116 int aNbFolders = foldersCount();
118 // Created object event *******************
119 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
120 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
121 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
122 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
124 std::set<ObjectPtr>::const_iterator aIt;
125 std::string aObjType;
126 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
127 ObjectPtr aObject = (*aIt);
128 // We do not show objects which does not need to be shown in object browser
129 if (!aObject->isInHistory())
132 aObjType = aObject->groupName();
133 DocumentPtr aDoc = aObject->document();
134 if (aDoc == aRootDoc) {
135 // Check that new folders could appear
136 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
137 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
138 if ((aNotEmptyFolder.toStdString() == aObjType) && (aRootDoc->size(aObjType) > 0)) {
139 // Appears first object in folder which can not be shown empty
140 if (!hasShownFolder(aRootDoc, aNotEmptyFolder)) {
141 insertRow(myXMLReader->rootFolderId(aObjType));
142 addShownFolder(aRootDoc, aNotEmptyFolder);
147 int aRow = aRootDoc->size(aObjType) - 1;
149 if ((aObjType == aRootType) || (aObjType == ModelAPI_Folder::group())) {
150 insertRow(aRow + aNbFolders + 1);
152 int aFolderId = myXMLReader->rootFolderId(aObjType);
153 if (aFolderId != -1) {
154 insertRow(aRow, createIndex(aFolderId, 0, (void*)Q_NULLPTR));
159 // Object created in sub-document
160 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get(), 0);
161 if (aDocRoot.isValid()) {
162 // Check that new folders could appear
163 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
164 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
165 if ((aNotEmptyFolder.toStdString() == aObjType) && (aDoc->size(aObjType) > 0)) {
166 // Appears first object in folder which can not be shown empty
167 if (!hasShownFolder(aDoc, aNotEmptyFolder)) {
168 insertRow(myXMLReader->subFolderId(aObjType), aDocRoot);
169 addShownFolder(aDoc, aNotEmptyFolder);
173 int aRow = aDoc->index(aObject, true);
175 int aNbSubFolders = foldersCount(aDoc.get());
176 if ((aObjType == aSubType) || (aObjType == ModelAPI_Folder::group())) {
177 // List of objects under document root
178 insertRow(aRow + aNbSubFolders, aDocRoot);
180 // List of objects under a folder
182 int aFolderId = folderId(aObjType, aDoc.get());
183 if (aFolderId != -1) {
184 QModelIndex aParentFolder = createIndex(aFolderId, 0, aDoc.get());
185 insertRow(aRow, aParentFolder);
186 emit dataChanged(aParentFolder, aParentFolder);
200 // Deleted object event ***********************
201 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
202 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
203 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
204 DocumentPtr aDoc = aUpdMsg->document();
205 std::set<std::string> aMsgGroups = aUpdMsg->groups();
207 /// Sort groups because RootType deletion has to be done after others
208 std::string aType = (aDoc == aRootDoc)? aRootType : aSubType;
209 std::list<std::string> aGroups;
210 std::set<std::string>::const_iterator aSetIt;
211 for (aSetIt = aMsgGroups.begin(); aSetIt != aMsgGroups.end(); ++aSetIt) {
212 std::string aGroup = (*aSetIt);
214 aGroups.push_back(aGroup);
216 aGroups.push_front(aGroup);
219 std::list<std::string>::const_iterator aIt;
220 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
221 std::string aGroup = (*aIt);
222 if (aDoc == aRootDoc) { // If root objects
223 int aRow = aRootDoc->size(aGroup, true);
224 if ((aGroup == aRootType) || (aGroup == ModelAPI_Folder::group())) {
225 // Process root folder
226 removeRow(aRow + aNbFolders);
227 rebuildBranch(aNbFolders, aRow);
229 // Process root sub-folder
230 int aFolderId = myXMLReader->rootFolderId(aGroup);
231 if (aFolderId != -1) {
232 QModelIndex aFolderIndex = createIndex(aFolderId, 0, (void*)Q_NULLPTR);
233 removeRow(aRow, aFolderIndex);
234 //rebuildBranch(0, aRow);
237 // Check that some folders could erased
238 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
239 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
240 if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup, true) == 0)) {
241 // Appears first object in folder which can not be shown empty
242 removeRow(myXMLReader->rootFolderId(aGroup));
243 removeShownFolder(aRootDoc, aNotEmptyFolder);
244 //rebuildBranch(0, aNbFolders + aDoc->size(myXMLReader->rootType()));
249 // Remove row for sub-document
250 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get(), 0);
251 if (aDocRoot.isValid()) {
252 int aRow = aDoc->size(aGroup, true);
253 int aNbSubFolders = foldersCount(aDoc.get());
254 if ((aGroup == aSubType) || (aGroup == ModelAPI_Folder::group())) {
255 // List of objects under document root
256 removeRow(aRow + aNbSubFolders, aDocRoot);
257 rebuildBranch(aNbSubFolders, aRow, aDocRoot);
259 // List of objects under a folder
260 int aFolderId = folderId(aGroup, aDoc.get());
261 if (aFolderId != -1) {
262 QModelIndex aFolderRoot = createIndex(aFolderId, 0, aDoc.get());
263 removeRow(aRow, aFolderRoot);
264 //rebuildBranch(0, aRow, aFolderRoot);
267 // Check that some folders could disappear
268 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
269 int aSize = aDoc->size(aGroup, true);
270 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
271 if ((aNotEmptyFolder.toStdString() == aGroup) && (aSize == 0)) {
272 // Appears first object in folder which can not be shown empty
273 removeRow(myXMLReader->subFolderId(aGroup), aDocRoot);
274 removeShownFolder(aDoc, aNotEmptyFolder);
275 //rebuildBranch(0, aNbSubFolders + aDoc->size(myXMLReader->subType()), aDocRoot);
285 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
286 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
287 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
288 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
290 std::set<ObjectPtr>::const_iterator aIt;
291 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
292 ObjectPtr aObject = (*aIt);
293 if (aObject->data()->isValid()) {
294 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
295 if (aFeature.get() && aFeature->firstResult().get()
296 && (aFeature->firstResult()->groupName() == ModelAPI_ResultField::group())) {
297 ResultFieldPtr aResult =
298 std::dynamic_pointer_cast<ModelAPI_ResultField>(aFeature->firstResult());
299 QModelIndex aIndex = objectIndex(aResult, 0);
300 removeRows(0, aResult->stepsSize(), aIndex);
302 if (aObject->groupName() == ModelAPI_Folder::group()) {
305 QModelIndex aIndex = objectIndex(aObject, 0);
306 if (aIndex.isValid()) {
307 emit dataChanged(aIndex, aIndex);
316 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
317 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aUpdMsg =
318 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
319 if (aUpdMsg->reordered().get()) {
320 DocumentPtr aDoc = aUpdMsg->reordered()->document();
321 std::string aGroup = aUpdMsg->reordered()->group();
325 if (aDoc == aRootDoc) {
326 // Update a group under root
327 if (aGroup == myXMLReader->rootType()) // Update objects under root
328 aStartId = foldersCount();
329 else // Update objects in folder under root
330 aParent = createIndex(folderId(aGroup), 0, (void*)Q_NULLPTR);
332 // Update a sub-document
333 if (aGroup == myXMLReader->subType()) {
334 // Update sub-document root
335 aParent = findDocumentRootIndex(aDoc.get(), 0);
336 aStartId = foldersCount(aDoc.get());
338 // update folder in sub-document
339 aParent = createIndex(folderId(aGroup, aDoc.get()), 0, aDoc.get());
341 int aChildNb = rowCount(aParent);
342 rebuildBranch(aStartId, aChildNb - aStartId, aParent);
346 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
347 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
348 if (aDoc != aRootDoc) {
349 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get(), 0);
350 if (aDocRoot.isValid())
351 emit dataChanged(aDocRoot, aDocRoot);
353 // We have got a new document
359 //******************************************************
360 void XGUI_DataModel::clear()
366 //******************************************************
367 void XGUI_DataModel::rebuildDataTree()
374 //******************************************************
375 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
377 if (theIndex.internalId() == 0) // this is a folder
379 ModelAPI_Object* aObj = 0;
381 aObj = dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
386 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
389 return aObj->data()->owner();
392 //******************************************************
393 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject, int theColumn) const
395 std::string aType = theObject->groupName();
396 DocumentPtr aDoc = theObject->document();
397 int aRow = aDoc->index(theObject, true);
399 // it could be a part of complex object
400 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
401 if (aFeature.get()) {
402 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
403 if (aCompFea.get()) {
404 for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
405 if (aCompFea->subFeature(i, true) == theObject) {
412 FolderPtr aFolder = aDoc->findContainingFolder(aFeature, aFRow);
416 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
418 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
419 if (aCompRes.get()) {
420 aRow = ModelAPI_Tools::compSolidIndex(aResult);
425 return QModelIndex();
427 return createIndex(aRow, theColumn, theObject.get());
429 SessionPtr aSession = ModelAPI_Session::get();
430 DocumentPtr aRootDoc = aSession->moduleDocument();
431 if (aDoc == aRootDoc &&
432 ((myXMLReader->rootType() == aType) || (aType == ModelAPI_Folder::group()))) {
433 // The object from root document
434 aRow += foldersCount();
435 } else if ((myXMLReader->subType() == aType) || (aType == ModelAPI_Folder::group())) {
436 // The object from sub document
437 aRow += foldersCount(aDoc.get());
439 return createIndex(aRow, theColumn, theObject.get());
442 //******************************************************
443 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
445 SessionPtr aSession = ModelAPI_Session::get();
446 DocumentPtr aRootDoc = aSession->moduleDocument();
447 int aNbFolders = foldersCount();
448 int theIndexRow = theIndex.row();
450 if (theRole == Qt::DecorationRole) {
451 if (theIndex == lastHistoryIndex())
452 return QIcon(":pictures/arrow.png");
453 else if (theIndex.column() == 0) {
454 VisibilityState aState = getVisibilityState(theIndex);
459 return QIcon(":pictures/eyeopen.png");
461 return QIcon(":pictures/eyemiclosed.png");
463 return QIcon(":pictures/eyeclosed.png");
468 //if (theIndex.column() == 1)
469 if (theIndex.column() != 1)
472 quintptr aParentId = theIndex.internalId();
473 if (aParentId == 0) { // root folders
475 case Qt::DisplayRole:
476 return QString(myXMLReader->rootFolderName(theIndexRow).c_str()) +
477 QString(" (%1)").arg(rowCount(theIndex));
478 case Qt::DecorationRole:
479 return QIcon(myXMLReader->rootFolderIcon(theIndexRow).c_str());
480 case Qt::ForegroundRole:
482 Qt::ItemFlags aFlags = theIndex.flags();
483 if (aFlags == Qt::ItemFlags())
484 return QBrush(DISABLED_COLOR);
485 if (!aFlags.testFlag(Qt::ItemIsEditable))
486 return QBrush(SELECTABLE_COLOR);
490 } else { // an object or sub-document
491 if (theRole == Qt::ForegroundRole) {
492 Qt::ItemFlags aFlags = theIndex.flags();
493 if (aFlags == Qt::ItemFlags())
494 return QBrush(DISABLED_COLOR);
495 if (!aFlags.testFlag(Qt::ItemIsEditable))
496 return QBrush(SELECTABLE_COLOR);
500 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
501 if (aSubDoc) { // this is a folder of sub document
502 QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
503 int aRow = theIndexRow;
504 while (aMissedIdx.contains(aRow))
506 if (aRow < myXMLReader->subFoldersNumber()) {
508 case Qt::DisplayRole:
509 return QString(myXMLReader->subFolderName(aRow).c_str()) +
510 QString(" (%1)").arg(rowCount(theIndex));
511 case Qt::DecorationRole:
512 return QIcon(myXMLReader->subFolderIcon(aRow).c_str());
516 ObjectPtr aObj = object(theIndex);
519 case Qt::DisplayRole:
521 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
522 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObj);
523 AttributeDoublePtr aValueAttribute =
524 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
525 QString aVal = QString::number(aValueAttribute->value());
526 QString aTitle = QString(aObj->data()->name().c_str());
527 return aTitle + " = " + aVal;
530 if (aObj->groupName() == myXMLReader->subType()) {
531 ResultPartPtr aPartRes = getPartResult(aObj.get());
532 if (aPartRes.get()) {
533 if (aPartRes->partDoc().get() == NULL)
534 aSuffix = " (Not loaded)";
537 return aObj->data()->name().c_str() + aSuffix;
539 case Qt::DecorationRole:
541 if (aObj->groupName() == ModelAPI_Folder::group())
542 return QIcon(":pictures/features_folder.png");
544 return ModuleBase_IconFactory::get()->getIcon(aObj);
549 case Qt::DisplayRole:
551 ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
552 dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>
553 ((ModelAPI_Entity*)theIndex.internalPointer());
555 return "Step " + QString::number(aStep->id() + 1) + " " +
556 aStep->field()->textLine(aStep->id()).c_str();
567 //******************************************************
568 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
573 //******************************************************
574 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
576 SessionPtr aSession = ModelAPI_Session::get();
577 if (!aSession->hasModuleDocument())
579 DocumentPtr aRootDoc = aSession->moduleDocument();
581 if (!theParent.isValid()) {
582 // Return number of items in root
583 int aNbFolders = foldersCount();
585 std::string aType = myXMLReader->rootType();
587 aNbItems = aRootDoc->size(aType, true);
588 return aNbFolders + aNbItems;
591 quintptr aId = theParent.internalId();
593 // this is a folder under root
594 int aParentPos = theParent.row();
595 std::string aType = myXMLReader->rootFolderType(aParentPos);
596 return aRootDoc->size(aType);
598 // It is an object which could have children
599 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
601 // a folder of sub-document
602 QIntList aMissedIdx = missedFolderIndexes(aDoc);
603 int aRow = theParent.row();
604 while (aMissedIdx.contains(aRow))
606 if (aRow < myXMLReader->subFoldersNumber()) {
607 std::string aType = myXMLReader->subFolderType(aRow);
608 return aDoc->size(aType);
611 ModelAPI_Object* aObj =
612 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theParent.internalPointer());
613 // Check for Part feature
614 ResultPartPtr aPartRes = getPartResult(aObj);
615 if (aPartRes.get()) {
616 DocumentPtr aSubDoc = aPartRes->partDoc();
620 int aNbSubFolders = foldersCount(aSubDoc.get());
622 std::string aSubType = myXMLReader->subType();
623 if (!aSubType.empty())
624 aNbSubItems = aSubDoc->size(aSubType, true);
625 return aNbSubItems + aNbSubFolders;
627 // Check for composite object
628 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
630 return aCompFeature->numberOfSubs(true);
631 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
633 return aCompRes->numberOfSubs(true);
634 ModelAPI_ResultField* aFieldRes = dynamic_cast<ModelAPI_ResultField*>(aObj);
636 return aFieldRes->stepsSize();
637 ModelAPI_Folder* aFolder = dynamic_cast<ModelAPI_Folder*>(aObj);
639 return getNumberOfFolderItems(aFolder);
646 //******************************************************
647 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
652 //******************************************************
653 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
655 SessionPtr aSession = ModelAPI_Session::get();
656 DocumentPtr aRootDoc = aSession->moduleDocument();
657 int aNbFolders = foldersCount();
661 if (!theParent.isValid()) {
662 if (theRow < aNbFolders) // Return first level folder index
663 return createIndex(theRow, theColumn, (void*)Q_NULLPTR);
664 else { // return object under root index
665 std::string aType = myXMLReader->rootType();
666 int aObjId = theRow - aNbFolders;
667 if (aObjId < aRootDoc->size(aType, true)) {
668 ObjectPtr aObj = aRootDoc->object(aType, aObjId, true);
669 aIndex = objectIndex(aObj, theColumn);
673 quintptr aId = theParent.internalId();
674 int aParentPos = theParent.row();
675 if (aId == 0) { // return object index inside of first level of folders
676 std::string aType = myXMLReader->rootFolderType(aParentPos);
677 if (theRow < aRootDoc->size(aType)) {
678 ObjectPtr aObj = aRootDoc->object(aType, theRow, true);
679 aIndex = objectIndex(aObj, theColumn);
682 // It is an object which could have children
683 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
685 // It is a folder of sub-document
686 int aParentRow = aParentPos;
687 QIntList aMissedIdx = missedFolderIndexes(aDoc);
688 while (aMissedIdx.contains(aParentRow))
690 if (aParentRow < myXMLReader->subFoldersNumber()) {
691 std::string aType = myXMLReader->subFolderType(aParentRow);
692 if (theRow < aDoc->size(aType)) {
693 ObjectPtr aObj = aDoc->object(aType, theRow);
694 aIndex = objectIndex(aObj, theColumn);
698 ModelAPI_Object* aParentObj =
699 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theParent.internalPointer());
701 // Check for Part feature
702 ResultPartPtr aPartRes = getPartResult(aParentObj);
703 if (aPartRes.get()) {
704 DocumentPtr aSubDoc = aPartRes->partDoc();
705 int aNbSubFolders = foldersCount(aSubDoc.get());
706 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
707 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
709 // this is an object under sub document root
710 std::string aType = myXMLReader->subType();
711 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders, true);
712 aIndex = objectIndex(aObj, theColumn);
715 // Check for composite object
716 ModelAPI_CompositeFeature* aCompFeature =
717 dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
719 aIndex = objectIndex(aCompFeature->subFeature(theRow), theColumn);
721 ModelAPI_ResultCompSolid* aCompRes =
722 dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
724 aIndex = objectIndex(aCompRes->subResult(theRow), theColumn);
726 ModelAPI_ResultField* aFieldRes =
727 dynamic_cast<ModelAPI_ResultField*>(aParentObj);
729 aIndex = createIndex(theRow, theColumn, aFieldRes->step(theRow));
731 ModelAPI_Folder* aFolder = dynamic_cast<ModelAPI_Folder*>(aParentObj);
732 ObjectPtr aObj = getObjectInFolder(aFolder, theRow);
734 aIndex = objectIndex(aObj, theColumn);
745 //******************************************************
746 static QModelIndex MYLastDeleted;
747 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
749 if (!theIndex.isValid())
750 return QModelIndex();
751 // To avoid additional request about index which was already deleted
752 if (theIndex == MYLastDeleted)
753 return QModelIndex();
755 SessionPtr aSession = ModelAPI_Session::get();
756 quintptr aId = theIndex.internalId();
757 if (aId != 0) { // The object is not a root folder
758 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
760 // It is a folder of sub-document
761 return findDocumentRootIndex(aDoc);
763 ObjectPtr aObj = object(theIndex);
765 // It can be a step of a field
766 ModelAPI_ResultField::ModelAPI_FieldStep* aStep = 0;
768 aStep = dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>
769 ((ModelAPI_Entity*)theIndex.internalPointer());
773 ModelAPI_ResultField* aField = aStep->field();
774 DocumentPtr aDoc = aSession->activeDocument();
776 for(int i = 0; i < aDoc->size(ModelAPI_ResultField::group()); i++) {
777 aFld = aDoc->object(ModelAPI_ResultField::group(), i);
778 if (aFld.get() == aField)
779 return objectIndex(aFld);
782 // To avoid additional request about index which was already deleted
783 // If deleted it causes a crash on delete object from Part
784 MYLastDeleted = theIndex;
785 return QModelIndex();
787 // Check is it object a sub-object of a complex object
788 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
789 if (aFeature.get()) {
790 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
791 if (aCompFea.get()) {
792 return objectIndex(aCompFea);
794 DocumentPtr aDoc = aFeature->document();
796 FolderPtr aFolder = aDoc->findContainingFolder(aFeature, aRow);
798 return objectIndex(aFolder);
800 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
802 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
803 if (aCompRes.get()) {
804 return objectIndex(aCompRes);
807 // Use as ordinary object
808 std::string aType = aObj->groupName();
809 DocumentPtr aRootDoc = aSession->moduleDocument();
810 DocumentPtr aSubDoc = aObj->document();
811 if (aSubDoc == aRootDoc) {
812 if ((aType == myXMLReader->rootType()) || (aType == ModelAPI_Folder::group()))
813 return QModelIndex();
815 // return first level of folder index
816 int aFolderId = myXMLReader->rootFolderId(aType);
817 // Items in a one row must have the same parent
818 return createIndex(aFolderId, 1, (void*)Q_NULLPTR);
821 if ((aType == myXMLReader->subType()) || (aType == ModelAPI_Folder::group()))
822 return findDocumentRootIndex(aSubDoc.get());
824 // return first level of folder index
825 int aFolderId = folderId(aType, aSubDoc.get());
826 // Items in a one row must have the same parent
827 return createIndex(aFolderId, 1, aSubDoc.get());
831 return QModelIndex();
834 //******************************************************
835 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
837 return rowCount(theParent) > 0;
840 //******************************************************
841 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
843 beginInsertRows(theParent, theRow, theRow + theCount - 1);
849 //******************************************************
850 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
852 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
857 //******************************************************
858 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
860 quintptr aIt = theIndex.internalId();
861 ModelAPI_Object* aObj = 0;
862 ModelAPI_Document* aDoc = 0;
863 SessionPtr aSession = ModelAPI_Session::get();
864 DocumentPtr aActiveDoc = aSession->activeDocument();
866 Qt::ItemFlags aNullFlag;
867 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
868 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
872 // Folders under root
873 DocumentPtr aRootDoc = aSession->moduleDocument();
874 if (aRootDoc != aActiveDoc)
877 aDoc = getSubDocument(theIndex.internalPointer());
879 aObj = dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
884 if (aObj->isDisabled())
885 return theIndex.column() == 2? Qt::ItemIsSelectable : aNullFlag;
887 if (aSession->moduleDocument() != aObj->document())
888 if (aActiveDoc != aObj->document())
889 return theIndex.column() == 2? Qt::ItemIsSelectable : aNullFlag;
891 bool isCompositeSub = false;
892 // An object which is sub-object of a composite object can not be accessible in column 2
893 if (theIndex.column() == 2) {
894 ObjectPtr aObjPtr = aObj->data()->owner();
895 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
896 if (aFeature.get()) {
897 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
899 isCompositeSub = true;
901 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
903 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
905 isCompositeSub = true;
910 return Qt::ItemIsSelectable;
912 if (aObj->document() != aActiveDoc) {
913 // The object could be a root of sub-tree
914 ResultPartPtr aPartRes = getPartResult(aObj);
915 if (aPartRes.get()) {
916 if (aPartRes->partDoc() == aActiveDoc)
922 // A folder under sub-document
923 if (aActiveDoc.get() != aDoc)
929 //******************************************************
931 XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc, int aColumn) const
933 SessionPtr aSession = ModelAPI_Session::get();
934 DocumentPtr aRootDoc = aSession->moduleDocument();
935 if (myXMLReader->isAttachToResult()) { // If document is attached to result
936 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
938 ResultPartPtr aPartRes;
939 for (int i = 0; i < aNb; i++) {
940 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
941 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
942 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
944 if (myXMLReader->rootType() == ModelAPI_Feature::group()) {
945 aRow += foldersCount();
947 return createIndex(aRow, aColumn, aObj.get());
950 } else { // If document is attached to feature
951 int aNb = aRootDoc->size(ModelAPI_Feature::group(), true);
953 ResultPartPtr aPartRes;
954 for (int i = 0; i < aNb; i++) {
955 aObj = aRootDoc->object(ModelAPI_Feature::group(), i, true);
956 aPartRes = getPartResult(aObj.get());
957 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
959 if (myXMLReader->rootType() == ModelAPI_Feature::group())
960 aRow += foldersCount();
961 return createIndex(aRow, aColumn, aObj.get());
965 return QModelIndex();
968 //******************************************************
969 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc, int theColumn) const
971 SessionPtr aSession = ModelAPI_Session::get();
972 DocumentPtr aRootDoc = aSession->moduleDocument();
973 if (theDoc == aRootDoc)
974 return QModelIndex();
976 return findDocumentRootIndex(theDoc.get(), theColumn);
979 //******************************************************
980 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
983 SessionPtr aSession = ModelAPI_Session::get();
984 DocumentPtr aRootDoc = aSession->moduleDocument();
985 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
986 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
987 if (myXMLReader->rootShowEmpty(i))
990 if (aRootDoc->size(myXMLReader->rootFolderType(i)) > 0)
995 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
996 if (myXMLReader->subShowEmpty(i))
999 if (theDoc->size(myXMLReader->subFolderType(i)) > 0)
1008 //******************************************************
1009 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
1012 SessionPtr aSession = ModelAPI_Session::get();
1013 DocumentPtr aRootDoc = aSession->moduleDocument();
1014 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
1015 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
1016 if (!myXMLReader->rootShowEmpty(i)) {
1017 if (aRootDoc->size(myXMLReader->rootFolderType(i)) == 0)
1022 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
1023 if (!myXMLReader->subShowEmpty(i)) {
1024 if (theDoc->size(myXMLReader->subFolderType(i)) == 0)
1033 //******************************************************
1034 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
1036 QStringList aResult;
1038 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
1039 if (!myXMLReader->rootShowEmpty(i))
1040 aResult << myXMLReader->rootFolderType(i).c_str();
1043 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
1044 if (!myXMLReader->subShowEmpty(i))
1045 aResult << myXMLReader->subFolderType(i).c_str();
1051 //******************************************************
1052 QModelIndex XGUI_DataModel::lastHistoryIndex() const
1054 SessionPtr aSession = ModelAPI_Session::get();
1055 DocumentPtr aCurDoc = aSession->activeDocument();
1056 FeaturePtr aFeature = aCurDoc->currentFeature(true);
1057 if (aFeature.get()) {
1058 QModelIndex aInd = objectIndex(aFeature);
1059 return createIndex(aInd.row(), 2, aInd.internalPointer());
1061 if (aCurDoc == aSession->moduleDocument())
1062 return createIndex(foldersCount() - 1, 2, -1);
1064 return createIndex(foldersCount(aCurDoc.get()) - 1, 2, aCurDoc.get());
1068 //******************************************************
1069 bool XGUI_DataModel::hasHiddenState(const QModelIndex& theIndex)
1071 return getVisibilityState(theIndex) == Hidden;
1074 //******************************************************
1075 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc) const
1077 SessionPtr aSession = ModelAPI_Session::get();
1078 ModelAPI_Document* aDoc = theDoc;
1080 aDoc = aSession->moduleDocument().get();
1082 bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
1086 int aId = myXMLReader->subFolderId(theType);
1088 for (int i = 0; i < aId; i++) {
1089 if (!myXMLReader->subShowEmpty(i)) {
1090 if (aDoc->size(myXMLReader->subFolderType(i)) == 0)
1095 int aId = myXMLReader->rootFolderId(theType);
1097 for (int i = 0; i < aId; i++) {
1098 if (!myXMLReader->rootShowEmpty(i)) {
1099 if (aDoc->size(myXMLReader->rootFolderType(i)) == 0)
1107 //******************************************************
1108 void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex& theParent)
1111 removeRows(theRow, theCount, theParent);
1112 insertRows(theRow, theCount, theParent);
1116 //******************************************************
1117 //bool XGUI_DataModel::blockEventsProcessing(const bool theState)
1119 // bool aPreviousState = myIsEventsProcessingBlocked;
1120 // myIsEventsProcessingBlocked = theState;
1121 // return aPreviousState;
1124 //******************************************************
1125 XGUI_DataModel::VisibilityState
1126 XGUI_DataModel::getVisibilityState(const QModelIndex& theIndex) const
1128 Qt::ItemFlags aFlags = theIndex.flags();
1129 if (aFlags == Qt::ItemFlags())
1132 ObjectPtr aObj = object(theIndex);
1134 if (aObj->groupName() == ModelAPI_ResultParameter::group())
1136 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
1137 if (aResObj.get()) {
1138 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
1139 ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResObj);
1140 if (aCompRes.get()) {
1141 VisibilityState aState = aCompRes->numberOfSubs(true) == 0 ?
1142 (aDisplayer->isVisible(aCompRes)? Visible : Hidden) : NoneState;
1143 for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
1144 ResultPtr aSubRes = aCompRes->subResult(i, true);
1145 VisibilityState aS = aDisplayer->isVisible(aSubRes)? Visible : Hidden;
1146 if (aState == NoneState)
1148 else if (aState != aS) {
1149 aState = SemiVisible;
1155 if (aDisplayer->isVisible(aResObj))
1166 int XGUI_DataModel::getNumberOfFolderItems(const ModelAPI_Folder* theFolder) const
1168 DocumentPtr aDoc = theFolder->document();
1170 FeaturePtr aFirstFeatureInFolder;
1171 AttributeReferencePtr aFirstFeatAttr =
1172 theFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
1174 aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value());
1175 if (!aFirstFeatureInFolder.get())
1178 FeaturePtr aLastFeatureInFolder;
1179 AttributeReferencePtr aLastFeatAttr =
1180 theFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1182 aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
1183 if (!aLastFeatureInFolder.get())
1186 int aFirst = aDoc->index(aFirstFeatureInFolder);
1187 int aLast = aDoc->index(aLastFeatureInFolder);
1188 return aLast - aFirst + 1;
1191 ObjectPtr XGUI_DataModel::getObjectInFolder(const ModelAPI_Folder* theFolder, int theId) const
1193 DocumentPtr aDoc = theFolder->document();
1195 FeaturePtr aFirstFeatureInFolder;
1196 AttributeReferencePtr aFirstFeatAttr =
1197 theFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
1199 aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value());
1200 if (!aFirstFeatureInFolder.get())
1203 int aFirst = aDoc->index(aFirstFeatureInFolder);
1204 return aDoc->object(ModelAPI_Feature::group(), aFirst + theId);