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(110, 110, 110)
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) {
225 // Process root folder
226 // remove optimization due to the issue #2456
227 //removeRow(aRow + aNbFolders);
228 //rebuildBranch(aNbFolders, aRow);
230 } else if (aGroup == ModelAPI_Folder::group()) {
233 // Process root sub-folder
234 int aFolderId = myXMLReader->rootFolderId(aGroup);
235 if (aFolderId != -1) {
236 QModelIndex aFolderIndex = createIndex(aFolderId, 0, (void*)Q_NULLPTR);
237 removeRow(aRow, aFolderIndex);
238 //rebuildBranch(0, aRow);
241 // Check that some folders could erased
242 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
243 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
244 if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup, true) == 0)) {
245 // Appears first object in folder which can not be shown empty
246 removeRow(myXMLReader->rootFolderId(aGroup));
247 removeShownFolder(aRootDoc, aNotEmptyFolder);
248 //rebuildBranch(0, aNbFolders + aDoc->size(myXMLReader->rootType()));
253 // Remove row for sub-document
254 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get(), 0);
255 if (aDocRoot.isValid()) {
256 int aRow = aDoc->size(aGroup, true);
257 int aNbSubFolders = foldersCount(aDoc.get());
258 if (aGroup == aSubType) {
259 // List of objects under document root
260 removeRow(aRow + aNbSubFolders, aDocRoot);
261 rebuildBranch(aNbSubFolders, aRow, aDocRoot);
262 } if (aGroup == ModelAPI_Folder::group()) {
265 // List of objects under a folder
266 int aFolderId = folderId(aGroup, aDoc.get());
267 if (aFolderId != -1) {
268 QModelIndex aFolderRoot = createIndex(aFolderId, 0, aDoc.get());
269 removeRow(aRow, aFolderRoot);
270 //rebuildBranch(0, aRow, aFolderRoot);
273 // Check that some folders could disappear
274 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
275 int aSize = aDoc->size(aGroup, true);
276 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
277 if ((aNotEmptyFolder.toStdString() == aGroup) && (aSize == 0)) {
278 // Appears first object in folder which can not be shown empty
279 removeRow(myXMLReader->subFolderId(aGroup), aDocRoot);
280 removeShownFolder(aDoc, aNotEmptyFolder);
281 //rebuildBranch(0, aNbSubFolders + aDoc->size(myXMLReader->subType()), aDocRoot);
291 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
292 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
293 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
294 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
296 std::set<ObjectPtr>::const_iterator aIt;
297 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
298 ObjectPtr aObject = (*aIt);
299 if (aObject->data()->isValid()) {
300 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
301 if (aFeature.get() && aFeature->firstResult().get()
302 && (aFeature->firstResult()->groupName() == ModelAPI_ResultField::group())) {
303 ResultFieldPtr aResult =
304 std::dynamic_pointer_cast<ModelAPI_ResultField>(aFeature->firstResult());
305 QModelIndex aIndex = objectIndex(aResult, 0);
306 removeRows(0, aResult->stepsSize(), aIndex);
308 if (aObject->groupName() == ModelAPI_Folder::group()) {
311 QModelIndex aIndex = objectIndex(aObject, 0);
312 if (aIndex.isValid()) {
313 emit dataChanged(aIndex, aIndex);
322 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
323 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aUpdMsg =
324 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
325 if (aUpdMsg->reordered().get()) {
326 DocumentPtr aDoc = aUpdMsg->reordered()->document();
327 std::string aGroup = aUpdMsg->reordered()->group();
331 if (aDoc == aRootDoc) {
332 // Update a group under root
333 if (aGroup == myXMLReader->rootType()) // Update objects under root
334 aStartId = foldersCount();
335 else // Update objects in folder under root
336 aParent = createIndex(folderId(aGroup), 0, (void*)Q_NULLPTR);
338 // Update a sub-document
339 if (aGroup == myXMLReader->subType()) {
340 // Update sub-document root
341 aParent = findDocumentRootIndex(aDoc.get(), 0);
342 aStartId = foldersCount(aDoc.get());
344 // update folder in sub-document
345 aParent = createIndex(folderId(aGroup, aDoc.get()), 0, aDoc.get());
347 int aChildNb = rowCount(aParent);
348 rebuildBranch(aStartId, aChildNb - aStartId, aParent);
352 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
353 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
354 if (aDoc != aRootDoc) {
355 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get(), 0);
356 if (aDocRoot.isValid())
357 emit dataChanged(aDocRoot, aDocRoot);
359 // We have got a new document
365 //******************************************************
366 void XGUI_DataModel::clear()
372 //******************************************************
373 void XGUI_DataModel::rebuildDataTree()
380 //******************************************************
381 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
383 if (theIndex.internalId() == 0) // this is a folder
385 ModelAPI_Object* aObj = 0;
387 aObj = dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
392 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
395 return aObj->data()->owner();
398 //******************************************************
399 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject, int theColumn) const
401 std::string aType = theObject->groupName();
402 DocumentPtr aDoc = theObject->document();
403 int aRow = aDoc->index(theObject, true);
405 // it could be a part of complex object
406 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
407 if (aFeature.get()) {
408 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
409 if (aCompFea.get()) {
410 for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
411 if (aCompFea->subFeature(i, true) == theObject) {
418 FolderPtr aFolder = aDoc->findContainingFolder(aFeature, aFRow);
422 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
424 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
425 if (aCompRes.get()) {
426 aRow = ModelAPI_Tools::compSolidIndex(aResult);
431 return QModelIndex();
433 return createIndex(aRow, theColumn, theObject.get());
435 SessionPtr aSession = ModelAPI_Session::get();
436 DocumentPtr aRootDoc = aSession->moduleDocument();
437 if (aDoc == aRootDoc &&
438 ((myXMLReader->rootType() == aType) || (aType == ModelAPI_Folder::group()))) {
439 // The object from root document
440 aRow += foldersCount();
441 } else if ((myXMLReader->subType() == aType) || (aType == ModelAPI_Folder::group())) {
442 // The object from sub document
443 aRow += foldersCount(aDoc.get());
445 return createIndex(aRow, theColumn, theObject.get());
448 //******************************************************
449 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
451 SessionPtr aSession = ModelAPI_Session::get();
452 DocumentPtr aRootDoc = aSession->moduleDocument();
453 int aNbFolders = foldersCount();
454 int theIndexRow = theIndex.row();
456 if (theRole == Qt::DecorationRole) {
457 if (theIndex == lastHistoryIndex())
458 return QIcon(":pictures/arrow.png");
459 else if (theIndex.column() == 0) {
460 VisibilityState aState = getVisibilityState(theIndex);
465 return QIcon(":pictures/eyeopen.png");
467 return QIcon(":pictures/eyemiclosed.png");
469 return QIcon(":pictures/eyeclosed.png");
474 //if (theIndex.column() == 1)
475 if (theIndex.column() != 1)
478 quintptr aParentId = theIndex.internalId();
479 if (aParentId == 0) { // root folders
481 case Qt::DisplayRole:
482 return QString(myXMLReader->rootFolderName(theIndexRow).c_str()) +
483 QString(" (%1)").arg(rowCount(theIndex));
484 case Qt::DecorationRole:
485 return QIcon(myXMLReader->rootFolderIcon(theIndexRow).c_str());
486 case Qt::ForegroundRole:
488 Qt::ItemFlags aFlags = theIndex.flags();
489 if (aFlags == Qt::ItemFlags())
490 return QBrush(DISABLED_COLOR);
491 if (!aFlags.testFlag(Qt::ItemIsEditable))
492 return QBrush(SELECTABLE_COLOR);
496 } else { // an object or sub-document
497 if (theRole == Qt::ForegroundRole) {
498 Qt::ItemFlags aFlags = theIndex.flags();
499 if (aFlags == Qt::ItemFlags())
500 return QBrush(DISABLED_COLOR);
501 if (!aFlags.testFlag(Qt::ItemIsEditable))
502 return QBrush(SELECTABLE_COLOR);
506 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
507 if (aSubDoc) { // this is a folder of sub document
508 QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
509 int aRow = theIndexRow;
510 while (aMissedIdx.contains(aRow))
512 if (aRow < myXMLReader->subFoldersNumber()) {
514 case Qt::DisplayRole:
515 return QString(myXMLReader->subFolderName(aRow).c_str()) +
516 QString(" (%1)").arg(rowCount(theIndex));
517 case Qt::DecorationRole:
518 return QIcon(myXMLReader->subFolderIcon(aRow).c_str());
522 ObjectPtr aObj = object(theIndex);
525 case Qt::DisplayRole:
527 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
528 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObj);
529 AttributeDoublePtr aValueAttribute =
530 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
531 QString aVal = QString::number(aValueAttribute->value());
532 QString aTitle = QString(aObj->data()->name().c_str());
533 return aTitle + " = " + aVal;
536 if (aObj->groupName() == myXMLReader->subType()) {
537 ResultPartPtr aPartRes = getPartResult(aObj.get());
538 if (aPartRes.get()) {
539 if (aPartRes->partDoc().get() == NULL)
540 aSuffix = " (Not loaded)";
543 return aObj->data()->name().c_str() + aSuffix;
545 case Qt::DecorationRole:
547 if (aObj->groupName() == ModelAPI_Folder::group())
548 return QIcon(":pictures/features_folder.png");
550 return ModuleBase_IconFactory::get()->getIcon(aObj);
555 case Qt::DisplayRole:
557 ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
558 dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>
559 ((ModelAPI_Entity*)theIndex.internalPointer());
561 return "Step " + QString::number(aStep->id() + 1) + " " +
562 aStep->field()->textLine(aStep->id()).c_str();
573 //******************************************************
574 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
579 //******************************************************
580 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
582 SessionPtr aSession = ModelAPI_Session::get();
583 if (!aSession->hasModuleDocument())
585 DocumentPtr aRootDoc = aSession->moduleDocument();
587 if (!theParent.isValid()) {
588 // Return number of items in root
589 int aNbFolders = foldersCount();
591 std::string aType = myXMLReader->rootType();
593 aNbItems = aRootDoc->size(aType, true);
594 return aNbFolders + aNbItems;
597 quintptr aId = theParent.internalId();
599 // this is a folder under root
600 int aParentPos = theParent.row();
601 std::string aType = myXMLReader->rootFolderType(aParentPos);
602 return aRootDoc->size(aType);
604 // It is an object which could have children
605 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
607 // a folder of sub-document
608 QIntList aMissedIdx = missedFolderIndexes(aDoc);
609 int aRow = theParent.row();
610 while (aMissedIdx.contains(aRow))
612 if (aRow < myXMLReader->subFoldersNumber()) {
613 std::string aType = myXMLReader->subFolderType(aRow);
614 return aDoc->size(aType);
617 ModelAPI_Object* aObj =
618 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theParent.internalPointer());
619 // Check for Part feature
620 ResultPartPtr aPartRes = getPartResult(aObj);
621 if (aPartRes.get()) {
622 DocumentPtr aSubDoc = aPartRes->partDoc();
626 int aNbSubFolders = foldersCount(aSubDoc.get());
628 std::string aSubType = myXMLReader->subType();
629 if (!aSubType.empty())
630 aNbSubItems = aSubDoc->size(aSubType, true);
631 return aNbSubItems + aNbSubFolders;
633 // Check for composite object
634 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
636 return aCompFeature->numberOfSubs(true);
637 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
639 return aCompRes->numberOfSubs(true);
640 ModelAPI_ResultField* aFieldRes = dynamic_cast<ModelAPI_ResultField*>(aObj);
642 return aFieldRes->stepsSize();
643 ModelAPI_Folder* aFolder = dynamic_cast<ModelAPI_Folder*>(aObj);
645 return getNumberOfFolderItems(aFolder);
652 //******************************************************
653 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
658 //******************************************************
659 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
661 SessionPtr aSession = ModelAPI_Session::get();
662 DocumentPtr aRootDoc = aSession->moduleDocument();
663 int aNbFolders = foldersCount();
667 if (!theParent.isValid()) {
668 if (theRow < aNbFolders) // Return first level folder index
669 return createIndex(theRow, theColumn, (void*)Q_NULLPTR);
670 else { // return object under root index
671 std::string aType = myXMLReader->rootType();
672 int aObjId = theRow - aNbFolders;
673 if (aObjId < aRootDoc->size(aType, true)) {
674 ObjectPtr aObj = aRootDoc->object(aType, aObjId, true);
675 aIndex = objectIndex(aObj, theColumn);
679 quintptr aId = theParent.internalId();
680 int aParentPos = theParent.row();
681 if (aId == 0) { // return object index inside of first level of folders
682 std::string aType = myXMLReader->rootFolderType(aParentPos);
683 if (theRow < aRootDoc->size(aType)) {
684 ObjectPtr aObj = aRootDoc->object(aType, theRow, true);
685 aIndex = objectIndex(aObj, theColumn);
688 // It is an object which could have children
689 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
691 // It is a folder of sub-document
692 int aParentRow = aParentPos;
693 QIntList aMissedIdx = missedFolderIndexes(aDoc);
694 while (aMissedIdx.contains(aParentRow))
696 if (aParentRow < myXMLReader->subFoldersNumber()) {
697 std::string aType = myXMLReader->subFolderType(aParentRow);
698 if (theRow < aDoc->size(aType)) {
699 ObjectPtr aObj = aDoc->object(aType, theRow);
700 aIndex = objectIndex(aObj, theColumn);
704 ModelAPI_Object* aParentObj =
705 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theParent.internalPointer());
707 // Check for Part feature
708 ResultPartPtr aPartRes = getPartResult(aParentObj);
709 if (aPartRes.get()) {
710 DocumentPtr aSubDoc = aPartRes->partDoc();
711 int aNbSubFolders = foldersCount(aSubDoc.get());
712 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
713 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
715 // this is an object under sub document root
716 std::string aType = myXMLReader->subType();
717 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders, true);
718 aIndex = objectIndex(aObj, theColumn);
721 // Check for composite object
722 ModelAPI_CompositeFeature* aCompFeature =
723 dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
725 aIndex = objectIndex(aCompFeature->subFeature(theRow), theColumn);
727 ModelAPI_ResultCompSolid* aCompRes =
728 dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
730 aIndex = objectIndex(aCompRes->subResult(theRow), theColumn);
732 ModelAPI_ResultField* aFieldRes =
733 dynamic_cast<ModelAPI_ResultField*>(aParentObj);
735 aIndex = createIndex(theRow, theColumn, aFieldRes->step(theRow));
737 ModelAPI_Folder* aFolder = dynamic_cast<ModelAPI_Folder*>(aParentObj);
738 ObjectPtr aObj = getObjectInFolder(aFolder, theRow);
740 aIndex = objectIndex(aObj, theColumn);
751 //******************************************************
752 static QModelIndex MYLastDeleted;
753 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
755 if (!theIndex.isValid())
756 return QModelIndex();
757 // To avoid additional request about index which was already deleted
758 if (theIndex == MYLastDeleted)
759 return QModelIndex();
761 SessionPtr aSession = ModelAPI_Session::get();
762 quintptr aId = theIndex.internalId();
763 if (aId != 0) { // The object is not a root folder
764 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
766 // It is a folder of sub-document
767 return findDocumentRootIndex(aDoc);
769 ObjectPtr aObj = object(theIndex);
771 // It can be a step of a field
772 ModelAPI_ResultField::ModelAPI_FieldStep* aStep = 0;
774 aStep = dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>
775 ((ModelAPI_Entity*)theIndex.internalPointer());
779 ModelAPI_ResultField* aField = aStep->field();
780 DocumentPtr aDoc = aSession->activeDocument();
782 for(int i = 0; i < aDoc->size(ModelAPI_ResultField::group()); i++) {
783 aFld = aDoc->object(ModelAPI_ResultField::group(), i);
784 if (aFld.get() == aField)
785 return objectIndex(aFld);
788 // To avoid additional request about index which was already deleted
789 // If deleted it causes a crash on delete object from Part
790 MYLastDeleted = theIndex;
791 return QModelIndex();
793 // Check is it object a sub-object of a complex object
794 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
795 if (aFeature.get()) {
796 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
797 if (aCompFea.get()) {
798 return objectIndex(aCompFea);
800 DocumentPtr aDoc = aFeature->document();
802 FolderPtr aFolder = aDoc->findContainingFolder(aFeature, aRow);
804 return objectIndex(aFolder);
806 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
808 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
809 if (aCompRes.get()) {
810 return objectIndex(aCompRes);
813 // Use as ordinary object
814 std::string aType = aObj->groupName();
815 DocumentPtr aRootDoc = aSession->moduleDocument();
816 DocumentPtr aSubDoc = aObj->document();
817 if (aSubDoc == aRootDoc) {
818 if ((aType == myXMLReader->rootType()) || (aType == ModelAPI_Folder::group()))
819 return QModelIndex();
821 // return first level of folder index
822 int aFolderId = myXMLReader->rootFolderId(aType);
823 // Items in a one row must have the same parent
824 return createIndex(aFolderId, 1, (void*)Q_NULLPTR);
827 if ((aType == myXMLReader->subType()) || (aType == ModelAPI_Folder::group()))
828 return findDocumentRootIndex(aSubDoc.get());
830 // return first level of folder index
831 int aFolderId = folderId(aType, aSubDoc.get());
832 // Items in a one row must have the same parent
833 return createIndex(aFolderId, 1, aSubDoc.get());
837 return QModelIndex();
840 //******************************************************
841 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
843 return rowCount(theParent) > 0;
846 //******************************************************
847 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
849 beginInsertRows(theParent, theRow, theRow + theCount - 1);
855 //******************************************************
856 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
858 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
863 //******************************************************
864 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
866 quintptr aIt = theIndex.internalId();
867 ModelAPI_Object* aObj = 0;
868 ModelAPI_Document* aDoc = 0;
869 SessionPtr aSession = ModelAPI_Session::get();
870 DocumentPtr aActiveDoc = aSession->activeDocument();
872 Qt::ItemFlags aNullFlag;
873 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
874 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
878 // Folders under root
879 DocumentPtr aRootDoc = aSession->moduleDocument();
880 if (aRootDoc != aActiveDoc)
883 aDoc = getSubDocument(theIndex.internalPointer());
885 aObj = dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
890 if (aObj->isDisabled())
891 return theIndex.column() == 2? Qt::ItemIsSelectable : aNullFlag;
893 if (aSession->moduleDocument() != aObj->document())
894 if (aActiveDoc != aObj->document())
895 return theIndex.column() == 2? Qt::ItemIsSelectable : aNullFlag;
897 bool isCompositeSub = false;
898 // An object which is sub-object of a composite object can not be accessible in column 2
899 if (theIndex.column() == 2) {
900 ObjectPtr aObjPtr = aObj->data()->owner();
901 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
902 if (aFeature.get()) {
903 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
905 isCompositeSub = true;
907 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
909 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
911 isCompositeSub = true;
916 return Qt::ItemIsSelectable;
918 if (aObj->document() != aActiveDoc) {
919 // The object could be a root of sub-tree
920 ResultPartPtr aPartRes = getPartResult(aObj);
921 if (aPartRes.get()) {
922 if (aPartRes->partDoc() == aActiveDoc)
928 // A folder under sub-document
929 if (aActiveDoc.get() != aDoc)
935 //******************************************************
937 XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc, int aColumn) const
939 SessionPtr aSession = ModelAPI_Session::get();
940 DocumentPtr aRootDoc = aSession->moduleDocument();
941 if (myXMLReader->isAttachToResult()) { // If document is attached to result
942 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
944 ResultPartPtr aPartRes;
945 for (int i = 0; i < aNb; i++) {
946 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
947 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
948 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
950 if (myXMLReader->rootType() == ModelAPI_Feature::group()) {
951 aRow += foldersCount();
953 return createIndex(aRow, aColumn, aObj.get());
956 } else { // If document is attached to feature
957 int aNb = aRootDoc->size(ModelAPI_Feature::group(), true);
959 ResultPartPtr aPartRes;
960 for (int i = 0; i < aNb; i++) {
961 aObj = aRootDoc->object(ModelAPI_Feature::group(), i, true);
962 aPartRes = getPartResult(aObj.get());
963 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
965 if (myXMLReader->rootType() == ModelAPI_Feature::group())
966 aRow += foldersCount();
967 return createIndex(aRow, aColumn, aObj.get());
971 return QModelIndex();
974 //******************************************************
975 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc, int theColumn) const
977 SessionPtr aSession = ModelAPI_Session::get();
978 DocumentPtr aRootDoc = aSession->moduleDocument();
979 if (theDoc == aRootDoc)
980 return QModelIndex();
982 return findDocumentRootIndex(theDoc.get(), theColumn);
985 //******************************************************
986 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
989 SessionPtr aSession = ModelAPI_Session::get();
990 DocumentPtr aRootDoc = aSession->moduleDocument();
991 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
992 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
993 if (myXMLReader->rootShowEmpty(i))
996 if (aRootDoc->size(myXMLReader->rootFolderType(i)) > 0)
1001 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
1002 if (myXMLReader->subShowEmpty(i))
1005 if (theDoc->size(myXMLReader->subFolderType(i)) > 0)
1014 //******************************************************
1015 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
1018 SessionPtr aSession = ModelAPI_Session::get();
1019 DocumentPtr aRootDoc = aSession->moduleDocument();
1020 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
1021 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
1022 if (!myXMLReader->rootShowEmpty(i)) {
1023 if (aRootDoc->size(myXMLReader->rootFolderType(i)) == 0)
1028 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
1029 if (!myXMLReader->subShowEmpty(i)) {
1030 if (theDoc->size(myXMLReader->subFolderType(i)) == 0)
1039 //******************************************************
1040 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
1042 QStringList aResult;
1044 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
1045 if (!myXMLReader->rootShowEmpty(i))
1046 aResult << myXMLReader->rootFolderType(i).c_str();
1049 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
1050 if (!myXMLReader->subShowEmpty(i))
1051 aResult << myXMLReader->subFolderType(i).c_str();
1057 //******************************************************
1058 QModelIndex XGUI_DataModel::lastHistoryIndex() const
1060 SessionPtr aSession = ModelAPI_Session::get();
1061 DocumentPtr aCurDoc = aSession->activeDocument();
1062 FeaturePtr aFeature = aCurDoc->currentFeature(true);
1063 if (aFeature.get()) {
1064 QModelIndex aInd = objectIndex(aFeature);
1065 return createIndex(aInd.row(), 2, aInd.internalPointer());
1067 if (aCurDoc == aSession->moduleDocument())
1068 return createIndex(foldersCount() - 1, 2, -1);
1070 return createIndex(foldersCount(aCurDoc.get()) - 1, 2, aCurDoc.get());
1074 //******************************************************
1075 bool XGUI_DataModel::hasHiddenState(const QModelIndex& theIndex)
1077 return getVisibilityState(theIndex) == Hidden;
1080 //******************************************************
1081 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc) const
1083 SessionPtr aSession = ModelAPI_Session::get();
1084 ModelAPI_Document* aDoc = theDoc;
1086 aDoc = aSession->moduleDocument().get();
1088 bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
1092 int aId = myXMLReader->subFolderId(theType);
1094 for (int i = 0; i < aId; i++) {
1095 if (!myXMLReader->subShowEmpty(i)) {
1096 if (aDoc->size(myXMLReader->subFolderType(i)) == 0)
1101 int aId = myXMLReader->rootFolderId(theType);
1103 for (int i = 0; i < aId; i++) {
1104 if (!myXMLReader->rootShowEmpty(i)) {
1105 if (aDoc->size(myXMLReader->rootFolderType(i)) == 0)
1113 //******************************************************
1114 void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex& theParent)
1117 removeRows(theRow, theCount, theParent);
1118 insertRows(theRow, theCount, theParent);
1122 //******************************************************
1123 //bool XGUI_DataModel::blockEventsProcessing(const bool theState)
1125 // bool aPreviousState = myIsEventsProcessingBlocked;
1126 // myIsEventsProcessingBlocked = theState;
1127 // return aPreviousState;
1130 //******************************************************
1131 XGUI_DataModel::VisibilityState
1132 XGUI_DataModel::getVisibilityState(const QModelIndex& theIndex) const
1134 Qt::ItemFlags aFlags = theIndex.flags();
1135 if (aFlags == Qt::ItemFlags())
1138 ObjectPtr aObj = object(theIndex);
1140 if (aObj->groupName() == ModelAPI_ResultParameter::group())
1142 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
1143 if (aResObj.get()) {
1144 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
1145 ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResObj);
1146 if (aCompRes.get()) {
1147 VisibilityState aState = aCompRes->numberOfSubs(true) == 0 ?
1148 (aDisplayer->isVisible(aCompRes)? Visible : Hidden) : NoneState;
1149 for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
1150 ResultPtr aSubRes = aCompRes->subResult(i, true);
1151 VisibilityState aS = aDisplayer->isVisible(aSubRes)? Visible : Hidden;
1152 if (aState == NoneState)
1154 else if (aState != aS) {
1155 aState = SemiVisible;
1161 if (aDisplayer->isVisible(aResObj))
1172 int XGUI_DataModel::getNumberOfFolderItems(const ModelAPI_Folder* theFolder) const
1174 DocumentPtr aDoc = theFolder->document();
1176 FeaturePtr aFirstFeatureInFolder;
1177 AttributeReferencePtr aFirstFeatAttr =
1178 theFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
1180 aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value());
1181 if (!aFirstFeatureInFolder.get())
1184 FeaturePtr aLastFeatureInFolder;
1185 AttributeReferencePtr aLastFeatAttr =
1186 theFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1188 aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
1189 if (!aLastFeatureInFolder.get())
1192 int aFirst = aDoc->index(aFirstFeatureInFolder);
1193 int aLast = aDoc->index(aLastFeatureInFolder);
1194 return aLast - aFirst + 1;
1197 ObjectPtr XGUI_DataModel::getObjectInFolder(const ModelAPI_Folder* theFolder, int theId) const
1199 DocumentPtr aDoc = theFolder->document();
1201 FeaturePtr aFirstFeatureInFolder;
1202 AttributeReferencePtr aFirstFeatAttr =
1203 theFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
1205 aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value());
1206 if (!aFirstFeatureInFolder.get())
1209 int aFirst = aDoc->index(aFirstFeatureInFolder);
1210 return aDoc->object(ModelAPI_Feature::group(), aFirst + theId);