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 =
380 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
383 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
386 return aObj->data()->owner();
389 //******************************************************
390 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject, int theColumn) const
392 std::string aType = theObject->groupName();
393 DocumentPtr aDoc = theObject->document();
394 int aRow = aDoc->index(theObject, true);
396 // it could be a part of complex object
397 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
398 if (aFeature.get()) {
399 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
400 if (aCompFea.get()) {
401 for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
402 if (aCompFea->subFeature(i, true) == theObject) {
409 FolderPtr aFolder = aDoc->findContainingFolder(aFeature, aFRow);
413 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
415 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
416 if (aCompRes.get()) {
417 aRow = ModelAPI_Tools::compSolidIndex(aResult);
422 return QModelIndex();
424 return createIndex(aRow, theColumn, theObject.get());
426 SessionPtr aSession = ModelAPI_Session::get();
427 DocumentPtr aRootDoc = aSession->moduleDocument();
428 if (aDoc == aRootDoc &&
429 ((myXMLReader->rootType() == aType) || (aType == ModelAPI_Folder::group()))) {
430 // The object from root document
431 aRow += foldersCount();
432 } else if ((myXMLReader->subType() == aType) || (aType == ModelAPI_Folder::group())) {
433 // The object from sub document
434 aRow += foldersCount(aDoc.get());
436 return createIndex(aRow, theColumn, theObject.get());
439 //******************************************************
440 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
442 SessionPtr aSession = ModelAPI_Session::get();
443 DocumentPtr aRootDoc = aSession->moduleDocument();
444 int aNbFolders = foldersCount();
445 int theIndexRow = theIndex.row();
447 if (theRole == Qt::DecorationRole) {
448 if (theIndex == lastHistoryIndex())
449 return QIcon(":pictures/arrow.png");
450 else if (theIndex.column() == 0) {
451 VisibilityState aState = getVisibilityState(theIndex);
456 return QIcon(":pictures/eyeopen.png");
458 return QIcon(":pictures/eyemiclosed.png");
460 return QIcon(":pictures/eyeclosed.png");
465 //if (theIndex.column() == 1)
466 if (theIndex.column() != 1)
469 quintptr aParentId = theIndex.internalId();
470 if (aParentId == 0) { // root folders
472 case Qt::DisplayRole:
473 return QString(myXMLReader->rootFolderName(theIndexRow).c_str()) +
474 QString(" (%1)").arg(rowCount(theIndex));
475 case Qt::DecorationRole:
476 return QIcon(myXMLReader->rootFolderIcon(theIndexRow).c_str());
477 case Qt::ForegroundRole:
479 Qt::ItemFlags aFlags = theIndex.flags();
480 if (aFlags == Qt::ItemFlags())
481 return QBrush(DISABLED_COLOR);
482 if (!aFlags.testFlag(Qt::ItemIsEditable))
483 return QBrush(SELECTABLE_COLOR);
487 } else { // an object or sub-document
488 if (theRole == Qt::ForegroundRole) {
489 Qt::ItemFlags aFlags = theIndex.flags();
490 if (aFlags == Qt::ItemFlags())
491 return QBrush(DISABLED_COLOR);
492 if (!aFlags.testFlag(Qt::ItemIsEditable))
493 return QBrush(SELECTABLE_COLOR);
497 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
498 if (aSubDoc) { // this is a folder of sub document
499 QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
500 int aRow = theIndexRow;
501 while (aMissedIdx.contains(aRow))
503 if (aRow < myXMLReader->subFoldersNumber()) {
505 case Qt::DisplayRole:
506 return QString(myXMLReader->subFolderName(aRow).c_str()) +
507 QString(" (%1)").arg(rowCount(theIndex));
508 case Qt::DecorationRole:
509 return QIcon(myXMLReader->subFolderIcon(aRow).c_str());
513 ObjectPtr aObj = object(theIndex);
516 case Qt::DisplayRole:
518 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
519 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObj);
520 AttributeDoublePtr aValueAttribute =
521 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
522 QString aVal = QString::number(aValueAttribute->value());
523 QString aTitle = QString(aObj->data()->name().c_str());
524 return aTitle + " = " + aVal;
527 if (aObj->groupName() == myXMLReader->subType()) {
528 ResultPartPtr aPartRes = getPartResult(aObj.get());
529 if (aPartRes.get()) {
530 if (aPartRes->partDoc().get() == NULL)
531 aSuffix = " (Not loaded)";
534 return aObj->data()->name().c_str() + aSuffix;
536 case Qt::DecorationRole:
538 if (aObj->groupName() == ModelAPI_Folder::group())
539 return QIcon(":pictures/features_folder.png");
541 return ModuleBase_IconFactory::get()->getIcon(aObj);
546 case Qt::DisplayRole:
548 ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
549 dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>
550 ((ModelAPI_Entity*)theIndex.internalPointer());
552 return "Step " + QString::number(aStep->id() + 1) + " " +
553 aStep->field()->textLine(aStep->id()).c_str();
564 //******************************************************
565 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
570 //******************************************************
571 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
573 SessionPtr aSession = ModelAPI_Session::get();
574 if (!aSession->hasModuleDocument())
576 DocumentPtr aRootDoc = aSession->moduleDocument();
578 if (!theParent.isValid()) {
579 // Return number of items in root
580 int aNbFolders = foldersCount();
582 std::string aType = myXMLReader->rootType();
584 aNbItems = aRootDoc->size(aType, true);
585 return aNbFolders + aNbItems;
588 quintptr aId = theParent.internalId();
590 // this is a folder under root
591 int aParentPos = theParent.row();
592 std::string aType = myXMLReader->rootFolderType(aParentPos);
593 return aRootDoc->size(aType);
595 // It is an object which could have children
596 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
598 // a folder of sub-document
599 QIntList aMissedIdx = missedFolderIndexes(aDoc);
600 int aRow = theParent.row();
601 while (aMissedIdx.contains(aRow))
603 if (aRow < myXMLReader->subFoldersNumber()) {
604 std::string aType = myXMLReader->subFolderType(aRow);
605 return aDoc->size(aType);
608 ModelAPI_Object* aObj =
609 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theParent.internalPointer());
610 // Check for Part feature
611 ResultPartPtr aPartRes = getPartResult(aObj);
612 if (aPartRes.get()) {
613 DocumentPtr aSubDoc = aPartRes->partDoc();
617 int aNbSubFolders = foldersCount(aSubDoc.get());
619 std::string aSubType = myXMLReader->subType();
620 if (!aSubType.empty())
621 aNbSubItems = aSubDoc->size(aSubType, true);
622 return aNbSubItems + aNbSubFolders;
624 // Check for composite object
625 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
627 return aCompFeature->numberOfSubs(true);
628 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
630 return aCompRes->numberOfSubs(true);
631 ModelAPI_ResultField* aFieldRes = dynamic_cast<ModelAPI_ResultField*>(aObj);
633 return aFieldRes->stepsSize();
634 ModelAPI_Folder* aFolder = dynamic_cast<ModelAPI_Folder*>(aObj);
636 return getNumberOfFolderItems(aFolder);
643 //******************************************************
644 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
649 //******************************************************
650 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
652 SessionPtr aSession = ModelAPI_Session::get();
653 DocumentPtr aRootDoc = aSession->moduleDocument();
654 int aNbFolders = foldersCount();
658 if (!theParent.isValid()) {
659 if (theRow < aNbFolders) // Return first level folder index
660 return createIndex(theRow, theColumn, (void*)Q_NULLPTR);
661 else { // return object under root index
662 std::string aType = myXMLReader->rootType();
663 int aObjId = theRow - aNbFolders;
664 if (aObjId < aRootDoc->size(aType, true)) {
665 ObjectPtr aObj = aRootDoc->object(aType, aObjId, true);
666 aIndex = objectIndex(aObj, theColumn);
670 quintptr aId = theParent.internalId();
671 int aParentPos = theParent.row();
672 if (aId == 0) { // return object index inside of first level of folders
673 std::string aType = myXMLReader->rootFolderType(aParentPos);
674 if (theRow < aRootDoc->size(aType)) {
675 ObjectPtr aObj = aRootDoc->object(aType, theRow, true);
676 aIndex = objectIndex(aObj, theColumn);
679 // It is an object which could have children
680 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
682 // It is a folder of sub-document
683 int aParentRow = aParentPos;
684 QIntList aMissedIdx = missedFolderIndexes(aDoc);
685 while (aMissedIdx.contains(aParentRow))
687 if (aParentRow < myXMLReader->subFoldersNumber()) {
688 std::string aType = myXMLReader->subFolderType(aParentRow);
689 if (theRow < aDoc->size(aType)) {
690 ObjectPtr aObj = aDoc->object(aType, theRow);
691 aIndex = objectIndex(aObj, theColumn);
695 ModelAPI_Object* aParentObj =
696 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theParent.internalPointer());
698 // Check for Part feature
699 ResultPartPtr aPartRes = getPartResult(aParentObj);
700 if (aPartRes.get()) {
701 DocumentPtr aSubDoc = aPartRes->partDoc();
702 int aNbSubFolders = foldersCount(aSubDoc.get());
703 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
704 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
706 // this is an object under sub document root
707 std::string aType = myXMLReader->subType();
708 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders, true);
709 aIndex = objectIndex(aObj, theColumn);
712 // Check for composite object
713 ModelAPI_CompositeFeature* aCompFeature =
714 dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
716 aIndex = objectIndex(aCompFeature->subFeature(theRow), theColumn);
718 ModelAPI_ResultCompSolid* aCompRes =
719 dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
721 aIndex = objectIndex(aCompRes->subResult(theRow), theColumn);
723 ModelAPI_ResultField* aFieldRes =
724 dynamic_cast<ModelAPI_ResultField*>(aParentObj);
726 aIndex = createIndex(theRow, theColumn, aFieldRes->step(theRow));
728 ModelAPI_Folder* aFolder = dynamic_cast<ModelAPI_Folder*>(aParentObj);
729 ObjectPtr aObj = getObjectInFolder(aFolder, theRow);
731 aIndex = objectIndex(aObj, theColumn);
742 //******************************************************
743 static QModelIndex MYLastDeleted;
744 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
746 if (!theIndex.isValid())
747 return QModelIndex();
748 // To avoid additional request about index which was already deleted
749 if (theIndex == MYLastDeleted)
750 return QModelIndex();
752 SessionPtr aSession = ModelAPI_Session::get();
753 quintptr aId = theIndex.internalId();
754 if (aId != 0) { // The object is not a root folder
755 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
757 // It is a folder of sub-document
758 return findDocumentRootIndex(aDoc);
760 ObjectPtr aObj = object(theIndex);
762 // It can be a step of a field
763 ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
764 dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>
765 ((ModelAPI_Entity*)theIndex.internalPointer());
767 ModelAPI_ResultField* aField = aStep->field();
768 DocumentPtr aDoc = aSession->activeDocument();
770 for(int i = 0; i < aDoc->size(ModelAPI_ResultField::group()); i++) {
771 aFld = aDoc->object(ModelAPI_ResultField::group(), i);
772 if (aFld.get() == aField)
773 return objectIndex(aFld);
776 // To avoid additional request about index which was already deleted
777 // If deleted it causes a crash on delete object from Part
778 MYLastDeleted = theIndex;
779 return QModelIndex();
781 // Check is it object a sub-object of a complex object
782 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
783 if (aFeature.get()) {
784 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
785 if (aCompFea.get()) {
786 return objectIndex(aCompFea);
788 DocumentPtr aDoc = aFeature->document();
790 FolderPtr aFolder = aDoc->findContainingFolder(aFeature, aRow);
792 return objectIndex(aFolder);
794 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
796 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
797 if (aCompRes.get()) {
798 return objectIndex(aCompRes);
801 // Use as ordinary object
802 std::string aType = aObj->groupName();
803 DocumentPtr aRootDoc = aSession->moduleDocument();
804 DocumentPtr aSubDoc = aObj->document();
805 if (aSubDoc == aRootDoc) {
806 if ((aType == myXMLReader->rootType()) || (aType == ModelAPI_Folder::group()))
807 return QModelIndex();
809 // return first level of folder index
810 int aFolderId = myXMLReader->rootFolderId(aType);
811 // Items in a one row must have the same parent
812 return createIndex(aFolderId, 1, (void*)Q_NULLPTR);
815 if ((aType == myXMLReader->subType()) || (aType == ModelAPI_Folder::group()))
816 return findDocumentRootIndex(aSubDoc.get());
818 // return first level of folder index
819 int aFolderId = folderId(aType, aSubDoc.get());
820 // Items in a one row must have the same parent
821 return createIndex(aFolderId, 1, aSubDoc.get());
825 return QModelIndex();
828 //******************************************************
829 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
831 return rowCount(theParent) > 0;
834 //******************************************************
835 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
837 beginInsertRows(theParent, theRow, theRow + theCount - 1);
843 //******************************************************
844 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
846 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
851 //******************************************************
852 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
854 quintptr aIt = theIndex.internalId();
855 ModelAPI_Object* aObj = 0;
856 ModelAPI_Document* aDoc = 0;
857 SessionPtr aSession = ModelAPI_Session::get();
858 DocumentPtr aActiveDoc = aSession->activeDocument();
860 Qt::ItemFlags aNullFlag;
861 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
862 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
866 // Folders under root
867 DocumentPtr aRootDoc = aSession->moduleDocument();
868 if (aRootDoc != aActiveDoc)
871 aDoc = getSubDocument(theIndex.internalPointer());
873 aObj = dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
878 if (aObj->isDisabled())
879 return theIndex.column() == 2? Qt::ItemIsSelectable : aNullFlag;
881 if (aSession->moduleDocument() != aObj->document())
882 if (aActiveDoc != aObj->document())
883 return theIndex.column() == 2? Qt::ItemIsSelectable : aNullFlag;
885 bool isCompositeSub = false;
886 // An object which is sub-object of a composite object can not be accessible in column 2
887 if (theIndex.column() == 2) {
888 ObjectPtr aObjPtr = aObj->data()->owner();
889 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
890 if (aFeature.get()) {
891 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
893 isCompositeSub = true;
895 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
897 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
899 isCompositeSub = true;
904 return Qt::ItemIsSelectable;
906 if (aObj->document() != aActiveDoc) {
907 // The object could be a root of sub-tree
908 ResultPartPtr aPartRes = getPartResult(aObj);
909 if (aPartRes.get()) {
910 if (aPartRes->partDoc() == aActiveDoc)
916 // A folder under sub-document
917 if (aActiveDoc.get() != aDoc)
923 //******************************************************
925 XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc, int aColumn) const
927 SessionPtr aSession = ModelAPI_Session::get();
928 DocumentPtr aRootDoc = aSession->moduleDocument();
929 if (myXMLReader->isAttachToResult()) { // If document is attached to result
930 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
932 ResultPartPtr aPartRes;
933 for (int i = 0; i < aNb; i++) {
934 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
935 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
936 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
938 if (myXMLReader->rootType() == ModelAPI_Feature::group()) {
939 aRow += foldersCount();
941 return createIndex(aRow, aColumn, aObj.get());
944 } else { // If document is attached to feature
945 int aNb = aRootDoc->size(ModelAPI_Feature::group(), true);
947 ResultPartPtr aPartRes;
948 for (int i = 0; i < aNb; i++) {
949 aObj = aRootDoc->object(ModelAPI_Feature::group(), i, true);
950 aPartRes = getPartResult(aObj.get());
951 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
953 if (myXMLReader->rootType() == ModelAPI_Feature::group())
954 aRow += foldersCount();
955 return createIndex(aRow, aColumn, aObj.get());
959 return QModelIndex();
962 //******************************************************
963 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc, int theColumn) const
965 SessionPtr aSession = ModelAPI_Session::get();
966 DocumentPtr aRootDoc = aSession->moduleDocument();
967 if (theDoc == aRootDoc)
968 return QModelIndex();
970 return findDocumentRootIndex(theDoc.get(), theColumn);
973 //******************************************************
974 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
977 SessionPtr aSession = ModelAPI_Session::get();
978 DocumentPtr aRootDoc = aSession->moduleDocument();
979 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
980 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
981 if (myXMLReader->rootShowEmpty(i))
984 if (aRootDoc->size(myXMLReader->rootFolderType(i)) > 0)
989 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
990 if (myXMLReader->subShowEmpty(i))
993 if (theDoc->size(myXMLReader->subFolderType(i)) > 0)
1002 //******************************************************
1003 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
1006 SessionPtr aSession = ModelAPI_Session::get();
1007 DocumentPtr aRootDoc = aSession->moduleDocument();
1008 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
1009 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
1010 if (!myXMLReader->rootShowEmpty(i)) {
1011 if (aRootDoc->size(myXMLReader->rootFolderType(i)) == 0)
1016 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
1017 if (!myXMLReader->subShowEmpty(i)) {
1018 if (theDoc->size(myXMLReader->subFolderType(i)) == 0)
1027 //******************************************************
1028 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
1030 QStringList aResult;
1032 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
1033 if (!myXMLReader->rootShowEmpty(i))
1034 aResult << myXMLReader->rootFolderType(i).c_str();
1037 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
1038 if (!myXMLReader->subShowEmpty(i))
1039 aResult << myXMLReader->subFolderType(i).c_str();
1045 //******************************************************
1046 QModelIndex XGUI_DataModel::lastHistoryIndex() const
1048 SessionPtr aSession = ModelAPI_Session::get();
1049 DocumentPtr aCurDoc = aSession->activeDocument();
1050 FeaturePtr aFeature = aCurDoc->currentFeature(true);
1051 if (aFeature.get()) {
1052 QModelIndex aInd = objectIndex(aFeature);
1053 return createIndex(aInd.row(), 2, aInd.internalPointer());
1055 if (aCurDoc == aSession->moduleDocument())
1056 return createIndex(foldersCount() - 1, 2, -1);
1058 return createIndex(foldersCount(aCurDoc.get()) - 1, 2, aCurDoc.get());
1062 //******************************************************
1063 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc) const
1065 SessionPtr aSession = ModelAPI_Session::get();
1066 ModelAPI_Document* aDoc = theDoc;
1068 aDoc = aSession->moduleDocument().get();
1070 bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
1074 int aId = myXMLReader->subFolderId(theType);
1076 for (int i = 0; i < aId; i++) {
1077 if (!myXMLReader->subShowEmpty(i)) {
1078 if (aDoc->size(myXMLReader->subFolderType(i)) == 0)
1083 int aId = myXMLReader->rootFolderId(theType);
1085 for (int i = 0; i < aId; i++) {
1086 if (!myXMLReader->rootShowEmpty(i)) {
1087 if (aDoc->size(myXMLReader->rootFolderType(i)) == 0)
1095 //******************************************************
1096 void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex& theParent)
1099 removeRows(theRow, theCount, theParent);
1100 insertRows(theRow, theCount, theParent);
1104 //******************************************************
1105 //bool XGUI_DataModel::blockEventsProcessing(const bool theState)
1107 // bool aPreviousState = myIsEventsProcessingBlocked;
1108 // myIsEventsProcessingBlocked = theState;
1109 // return aPreviousState;
1112 //******************************************************
1113 XGUI_DataModel::VisibilityState
1114 XGUI_DataModel::getVisibilityState(const QModelIndex& theIndex) const
1116 Qt::ItemFlags aFlags = theIndex.flags();
1117 if (aFlags == Qt::ItemFlags())
1120 ObjectPtr aObj = object(theIndex);
1122 if (aObj->groupName() == ModelAPI_ResultParameter::group())
1124 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
1125 if (aResObj.get()) {
1126 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
1127 ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResObj);
1128 if (aCompRes.get()) {
1129 VisibilityState aState = aCompRes->numberOfSubs(true) == 0 ?
1130 (aDisplayer->isVisible(aCompRes)? Visible : Hidden) : NoneState;
1131 for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
1132 ResultPtr aSubRes = aCompRes->subResult(i, true);
1133 VisibilityState aS = aDisplayer->isVisible(aSubRes)? Visible : Hidden;
1134 if (aState == NoneState)
1136 else if (aState != aS) {
1137 aState = SemiVisible;
1143 if (aDisplayer->isVisible(aResObj))
1154 int XGUI_DataModel::getNumberOfFolderItems(const ModelAPI_Folder* theFolder) const
1156 DocumentPtr aDoc = theFolder->document();
1158 FeaturePtr aFirstFeatureInFolder;
1159 AttributeReferencePtr aFirstFeatAttr =
1160 theFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
1162 aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value());
1163 if (!aFirstFeatureInFolder.get())
1166 FeaturePtr aLastFeatureInFolder;
1167 AttributeReferencePtr aLastFeatAttr =
1168 theFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1170 aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
1171 if (!aLastFeatureInFolder.get())
1174 int aFirst = aDoc->index(aFirstFeatureInFolder);
1175 int aLast = aDoc->index(aLastFeatureInFolder);
1176 return aLast - aFirst + 1;
1179 ObjectPtr XGUI_DataModel::getObjectInFolder(const ModelAPI_Folder* theFolder, int theId) const
1181 DocumentPtr aDoc = theFolder->document();
1183 FeaturePtr aFirstFeatureInFolder;
1184 AttributeReferencePtr aFirstFeatAttr =
1185 theFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
1187 aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value());
1188 if (!aFirstFeatureInFolder.get())
1191 int aFirst = aDoc->index(aFirstFeatureInFolder);
1192 return aDoc->object(ModelAPI_Feature::group(), aFirst + theId);