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) {
225 // Process root folder
226 removeRow(aRow + aNbFolders);
227 rebuildBranch(aNbFolders, aRow);
228 } else if (aGroup == ModelAPI_Folder::group()) {
231 // Process root sub-folder
232 int aFolderId = myXMLReader->rootFolderId(aGroup);
233 if (aFolderId != -1) {
234 QModelIndex aFolderIndex = createIndex(aFolderId, 0, (void*)Q_NULLPTR);
235 removeRow(aRow, aFolderIndex);
236 //rebuildBranch(0, aRow);
239 // Check that some folders could erased
240 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
241 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
242 if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup, true) == 0)) {
243 // Appears first object in folder which can not be shown empty
244 removeRow(myXMLReader->rootFolderId(aGroup));
245 removeShownFolder(aRootDoc, aNotEmptyFolder);
246 //rebuildBranch(0, aNbFolders + aDoc->size(myXMLReader->rootType()));
251 // Remove row for sub-document
252 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get(), 0);
253 if (aDocRoot.isValid()) {
254 int aRow = aDoc->size(aGroup, true);
255 int aNbSubFolders = foldersCount(aDoc.get());
256 if (aGroup == aSubType) {
257 // List of objects under document root
258 removeRow(aRow + aNbSubFolders, aDocRoot);
259 rebuildBranch(aNbSubFolders, aRow, aDocRoot);
260 } if (aGroup == ModelAPI_Folder::group()) {
263 // List of objects under a folder
264 int aFolderId = folderId(aGroup, aDoc.get());
265 if (aFolderId != -1) {
266 QModelIndex aFolderRoot = createIndex(aFolderId, 0, aDoc.get());
267 removeRow(aRow, aFolderRoot);
268 //rebuildBranch(0, aRow, aFolderRoot);
271 // Check that some folders could disappear
272 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
273 int aSize = aDoc->size(aGroup, true);
274 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
275 if ((aNotEmptyFolder.toStdString() == aGroup) && (aSize == 0)) {
276 // Appears first object in folder which can not be shown empty
277 removeRow(myXMLReader->subFolderId(aGroup), aDocRoot);
278 removeShownFolder(aDoc, aNotEmptyFolder);
279 //rebuildBranch(0, aNbSubFolders + aDoc->size(myXMLReader->subType()), aDocRoot);
289 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
290 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
291 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
292 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
294 std::set<ObjectPtr>::const_iterator aIt;
295 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
296 ObjectPtr aObject = (*aIt);
297 if (aObject->data()->isValid()) {
298 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
299 if (aFeature.get() && aFeature->firstResult().get()
300 && (aFeature->firstResult()->groupName() == ModelAPI_ResultField::group())) {
301 ResultFieldPtr aResult =
302 std::dynamic_pointer_cast<ModelAPI_ResultField>(aFeature->firstResult());
303 QModelIndex aIndex = objectIndex(aResult, 0);
304 removeRows(0, aResult->stepsSize(), aIndex);
306 if (aObject->groupName() == ModelAPI_Folder::group()) {
309 QModelIndex aIndex = objectIndex(aObject, 0);
310 if (aIndex.isValid()) {
311 emit dataChanged(aIndex, aIndex);
320 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
321 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aUpdMsg =
322 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
323 if (aUpdMsg->reordered().get()) {
324 DocumentPtr aDoc = aUpdMsg->reordered()->document();
325 std::string aGroup = aUpdMsg->reordered()->group();
329 if (aDoc == aRootDoc) {
330 // Update a group under root
331 if (aGroup == myXMLReader->rootType()) // Update objects under root
332 aStartId = foldersCount();
333 else // Update objects in folder under root
334 aParent = createIndex(folderId(aGroup), 0, (void*)Q_NULLPTR);
336 // Update a sub-document
337 if (aGroup == myXMLReader->subType()) {
338 // Update sub-document root
339 aParent = findDocumentRootIndex(aDoc.get(), 0);
340 aStartId = foldersCount(aDoc.get());
342 // update folder in sub-document
343 aParent = createIndex(folderId(aGroup, aDoc.get()), 0, aDoc.get());
345 int aChildNb = rowCount(aParent);
346 rebuildBranch(aStartId, aChildNb - aStartId, aParent);
350 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
351 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
352 if (aDoc != aRootDoc) {
353 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get(), 0);
354 if (aDocRoot.isValid())
355 emit dataChanged(aDocRoot, aDocRoot);
357 // We have got a new document
363 //******************************************************
364 void XGUI_DataModel::clear()
370 //******************************************************
371 void XGUI_DataModel::rebuildDataTree()
378 //******************************************************
379 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
381 if (theIndex.internalId() == 0) // this is a folder
383 ModelAPI_Object* aObj = 0;
385 aObj = dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
390 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
393 return aObj->data()->owner();
396 //******************************************************
397 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject, int theColumn) const
399 std::string aType = theObject->groupName();
400 DocumentPtr aDoc = theObject->document();
401 int aRow = aDoc->index(theObject, true);
403 // it could be a part of complex object
404 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
405 if (aFeature.get()) {
406 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
407 if (aCompFea.get()) {
408 for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
409 if (aCompFea->subFeature(i, true) == theObject) {
416 FolderPtr aFolder = aDoc->findContainingFolder(aFeature, aFRow);
420 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
422 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
423 if (aCompRes.get()) {
424 aRow = ModelAPI_Tools::compSolidIndex(aResult);
429 return QModelIndex();
431 return createIndex(aRow, theColumn, theObject.get());
433 SessionPtr aSession = ModelAPI_Session::get();
434 DocumentPtr aRootDoc = aSession->moduleDocument();
435 if (aDoc == aRootDoc &&
436 ((myXMLReader->rootType() == aType) || (aType == ModelAPI_Folder::group()))) {
437 // The object from root document
438 aRow += foldersCount();
439 } else if ((myXMLReader->subType() == aType) || (aType == ModelAPI_Folder::group())) {
440 // The object from sub document
441 aRow += foldersCount(aDoc.get());
443 return createIndex(aRow, theColumn, theObject.get());
446 //******************************************************
447 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
449 SessionPtr aSession = ModelAPI_Session::get();
450 DocumentPtr aRootDoc = aSession->moduleDocument();
451 int aNbFolders = foldersCount();
452 int theIndexRow = theIndex.row();
454 if (theRole == Qt::DecorationRole) {
455 if (theIndex == lastHistoryIndex())
456 return QIcon(":pictures/arrow.png");
457 else if (theIndex.column() == 0) {
458 VisibilityState aState = getVisibilityState(theIndex);
463 return QIcon(":pictures/eyeopen.png");
465 return QIcon(":pictures/eyemiclosed.png");
467 return QIcon(":pictures/eyeclosed.png");
472 //if (theIndex.column() == 1)
473 if (theIndex.column() != 1)
476 quintptr aParentId = theIndex.internalId();
477 if (aParentId == 0) { // root folders
479 case Qt::DisplayRole:
480 return QString(myXMLReader->rootFolderName(theIndexRow).c_str()) +
481 QString(" (%1)").arg(rowCount(theIndex));
482 case Qt::DecorationRole:
483 return QIcon(myXMLReader->rootFolderIcon(theIndexRow).c_str());
484 case Qt::ForegroundRole:
486 Qt::ItemFlags aFlags = theIndex.flags();
487 if (aFlags == Qt::ItemFlags())
488 return QBrush(DISABLED_COLOR);
489 if (!aFlags.testFlag(Qt::ItemIsEditable))
490 return QBrush(SELECTABLE_COLOR);
494 } else { // an object or sub-document
495 if (theRole == Qt::ForegroundRole) {
496 Qt::ItemFlags aFlags = theIndex.flags();
497 if (aFlags == Qt::ItemFlags())
498 return QBrush(DISABLED_COLOR);
499 if (!aFlags.testFlag(Qt::ItemIsEditable))
500 return QBrush(SELECTABLE_COLOR);
504 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
505 if (aSubDoc) { // this is a folder of sub document
506 QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
507 int aRow = theIndexRow;
508 while (aMissedIdx.contains(aRow))
510 if (aRow < myXMLReader->subFoldersNumber()) {
512 case Qt::DisplayRole:
513 return QString(myXMLReader->subFolderName(aRow).c_str()) +
514 QString(" (%1)").arg(rowCount(theIndex));
515 case Qt::DecorationRole:
516 return QIcon(myXMLReader->subFolderIcon(aRow).c_str());
520 ObjectPtr aObj = object(theIndex);
523 case Qt::DisplayRole:
525 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
526 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObj);
527 AttributeDoublePtr aValueAttribute =
528 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
529 QString aVal = QString::number(aValueAttribute->value());
530 QString aTitle = QString(aObj->data()->name().c_str());
531 return aTitle + " = " + aVal;
534 if (aObj->groupName() == myXMLReader->subType()) {
535 ResultPartPtr aPartRes = getPartResult(aObj.get());
536 if (aPartRes.get()) {
537 if (aPartRes->partDoc().get() == NULL)
538 aSuffix = " (Not loaded)";
541 return aObj->data()->name().c_str() + aSuffix;
543 case Qt::DecorationRole:
545 if (aObj->groupName() == ModelAPI_Folder::group())
546 return QIcon(":pictures/features_folder.png");
548 return ModuleBase_IconFactory::get()->getIcon(aObj);
553 case Qt::DisplayRole:
555 ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
556 dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>
557 ((ModelAPI_Entity*)theIndex.internalPointer());
559 return "Step " + QString::number(aStep->id() + 1) + " " +
560 aStep->field()->textLine(aStep->id()).c_str();
571 //******************************************************
572 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
577 //******************************************************
578 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
580 SessionPtr aSession = ModelAPI_Session::get();
581 if (!aSession->hasModuleDocument())
583 DocumentPtr aRootDoc = aSession->moduleDocument();
585 if (!theParent.isValid()) {
586 // Return number of items in root
587 int aNbFolders = foldersCount();
589 std::string aType = myXMLReader->rootType();
591 aNbItems = aRootDoc->size(aType, true);
592 return aNbFolders + aNbItems;
595 quintptr aId = theParent.internalId();
597 // this is a folder under root
598 int aParentPos = theParent.row();
599 std::string aType = myXMLReader->rootFolderType(aParentPos);
600 return aRootDoc->size(aType);
602 // It is an object which could have children
603 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
605 // a folder of sub-document
606 QIntList aMissedIdx = missedFolderIndexes(aDoc);
607 int aRow = theParent.row();
608 while (aMissedIdx.contains(aRow))
610 if (aRow < myXMLReader->subFoldersNumber()) {
611 std::string aType = myXMLReader->subFolderType(aRow);
612 return aDoc->size(aType);
615 ModelAPI_Object* aObj =
616 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theParent.internalPointer());
617 // Check for Part feature
618 ResultPartPtr aPartRes = getPartResult(aObj);
619 if (aPartRes.get()) {
620 DocumentPtr aSubDoc = aPartRes->partDoc();
624 int aNbSubFolders = foldersCount(aSubDoc.get());
626 std::string aSubType = myXMLReader->subType();
627 if (!aSubType.empty())
628 aNbSubItems = aSubDoc->size(aSubType, true);
629 return aNbSubItems + aNbSubFolders;
631 // Check for composite object
632 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
634 return aCompFeature->numberOfSubs(true);
635 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
637 return aCompRes->numberOfSubs(true);
638 ModelAPI_ResultField* aFieldRes = dynamic_cast<ModelAPI_ResultField*>(aObj);
640 return aFieldRes->stepsSize();
641 ModelAPI_Folder* aFolder = dynamic_cast<ModelAPI_Folder*>(aObj);
643 return getNumberOfFolderItems(aFolder);
650 //******************************************************
651 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
656 //******************************************************
657 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
659 SessionPtr aSession = ModelAPI_Session::get();
660 DocumentPtr aRootDoc = aSession->moduleDocument();
661 int aNbFolders = foldersCount();
665 if (!theParent.isValid()) {
666 if (theRow < aNbFolders) // Return first level folder index
667 return createIndex(theRow, theColumn, (void*)Q_NULLPTR);
668 else { // return object under root index
669 std::string aType = myXMLReader->rootType();
670 int aObjId = theRow - aNbFolders;
671 if (aObjId < aRootDoc->size(aType, true)) {
672 ObjectPtr aObj = aRootDoc->object(aType, aObjId, true);
673 aIndex = objectIndex(aObj, theColumn);
677 quintptr aId = theParent.internalId();
678 int aParentPos = theParent.row();
679 if (aId == 0) { // return object index inside of first level of folders
680 std::string aType = myXMLReader->rootFolderType(aParentPos);
681 if (theRow < aRootDoc->size(aType)) {
682 ObjectPtr aObj = aRootDoc->object(aType, theRow, true);
683 aIndex = objectIndex(aObj, theColumn);
686 // It is an object which could have children
687 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
689 // It is a folder of sub-document
690 int aParentRow = aParentPos;
691 QIntList aMissedIdx = missedFolderIndexes(aDoc);
692 while (aMissedIdx.contains(aParentRow))
694 if (aParentRow < myXMLReader->subFoldersNumber()) {
695 std::string aType = myXMLReader->subFolderType(aParentRow);
696 if (theRow < aDoc->size(aType)) {
697 ObjectPtr aObj = aDoc->object(aType, theRow);
698 aIndex = objectIndex(aObj, theColumn);
702 ModelAPI_Object* aParentObj =
703 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theParent.internalPointer());
705 // Check for Part feature
706 ResultPartPtr aPartRes = getPartResult(aParentObj);
707 if (aPartRes.get()) {
708 DocumentPtr aSubDoc = aPartRes->partDoc();
709 int aNbSubFolders = foldersCount(aSubDoc.get());
710 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
711 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
713 // this is an object under sub document root
714 std::string aType = myXMLReader->subType();
715 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders, true);
716 aIndex = objectIndex(aObj, theColumn);
719 // Check for composite object
720 ModelAPI_CompositeFeature* aCompFeature =
721 dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
723 aIndex = objectIndex(aCompFeature->subFeature(theRow), theColumn);
725 ModelAPI_ResultCompSolid* aCompRes =
726 dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
728 aIndex = objectIndex(aCompRes->subResult(theRow), theColumn);
730 ModelAPI_ResultField* aFieldRes =
731 dynamic_cast<ModelAPI_ResultField*>(aParentObj);
733 aIndex = createIndex(theRow, theColumn, aFieldRes->step(theRow));
735 ModelAPI_Folder* aFolder = dynamic_cast<ModelAPI_Folder*>(aParentObj);
736 ObjectPtr aObj = getObjectInFolder(aFolder, theRow);
738 aIndex = objectIndex(aObj, theColumn);
749 //******************************************************
750 static QModelIndex MYLastDeleted;
751 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
753 if (!theIndex.isValid())
754 return QModelIndex();
755 // To avoid additional request about index which was already deleted
756 if (theIndex == MYLastDeleted)
757 return QModelIndex();
759 SessionPtr aSession = ModelAPI_Session::get();
760 quintptr aId = theIndex.internalId();
761 if (aId != 0) { // The object is not a root folder
762 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
764 // It is a folder of sub-document
765 return findDocumentRootIndex(aDoc);
767 ObjectPtr aObj = object(theIndex);
769 // It can be a step of a field
770 ModelAPI_ResultField::ModelAPI_FieldStep* aStep = 0;
772 aStep = dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>
773 ((ModelAPI_Entity*)theIndex.internalPointer());
777 ModelAPI_ResultField* aField = aStep->field();
778 DocumentPtr aDoc = aSession->activeDocument();
780 for(int i = 0; i < aDoc->size(ModelAPI_ResultField::group()); i++) {
781 aFld = aDoc->object(ModelAPI_ResultField::group(), i);
782 if (aFld.get() == aField)
783 return objectIndex(aFld);
786 // To avoid additional request about index which was already deleted
787 // If deleted it causes a crash on delete object from Part
788 MYLastDeleted = theIndex;
789 return QModelIndex();
791 // Check is it object a sub-object of a complex object
792 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
793 if (aFeature.get()) {
794 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
795 if (aCompFea.get()) {
796 return objectIndex(aCompFea);
798 DocumentPtr aDoc = aFeature->document();
800 FolderPtr aFolder = aDoc->findContainingFolder(aFeature, aRow);
802 return objectIndex(aFolder);
804 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
806 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
807 if (aCompRes.get()) {
808 return objectIndex(aCompRes);
811 // Use as ordinary object
812 std::string aType = aObj->groupName();
813 DocumentPtr aRootDoc = aSession->moduleDocument();
814 DocumentPtr aSubDoc = aObj->document();
815 if (aSubDoc == aRootDoc) {
816 if ((aType == myXMLReader->rootType()) || (aType == ModelAPI_Folder::group()))
817 return QModelIndex();
819 // return first level of folder index
820 int aFolderId = myXMLReader->rootFolderId(aType);
821 // Items in a one row must have the same parent
822 return createIndex(aFolderId, 1, (void*)Q_NULLPTR);
825 if ((aType == myXMLReader->subType()) || (aType == ModelAPI_Folder::group()))
826 return findDocumentRootIndex(aSubDoc.get());
828 // return first level of folder index
829 int aFolderId = folderId(aType, aSubDoc.get());
830 // Items in a one row must have the same parent
831 return createIndex(aFolderId, 1, aSubDoc.get());
835 return QModelIndex();
838 //******************************************************
839 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
841 return rowCount(theParent) > 0;
844 //******************************************************
845 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
847 beginInsertRows(theParent, theRow, theRow + theCount - 1);
853 //******************************************************
854 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
856 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
861 //******************************************************
862 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
864 quintptr aIt = theIndex.internalId();
865 ModelAPI_Object* aObj = 0;
866 ModelAPI_Document* aDoc = 0;
867 SessionPtr aSession = ModelAPI_Session::get();
868 DocumentPtr aActiveDoc = aSession->activeDocument();
870 Qt::ItemFlags aNullFlag;
871 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
872 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
876 // Folders under root
877 DocumentPtr aRootDoc = aSession->moduleDocument();
878 if (aRootDoc != aActiveDoc)
881 aDoc = getSubDocument(theIndex.internalPointer());
883 aObj = dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
888 if (aObj->isDisabled())
889 return theIndex.column() == 2? Qt::ItemIsSelectable : aNullFlag;
891 if (aSession->moduleDocument() != aObj->document())
892 if (aActiveDoc != aObj->document())
893 return theIndex.column() == 2? Qt::ItemIsSelectable : aNullFlag;
895 bool isCompositeSub = false;
896 // An object which is sub-object of a composite object can not be accessible in column 2
897 if (theIndex.column() == 2) {
898 ObjectPtr aObjPtr = aObj->data()->owner();
899 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
900 if (aFeature.get()) {
901 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
903 isCompositeSub = true;
905 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
907 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
909 isCompositeSub = true;
914 return Qt::ItemIsSelectable;
916 if (aObj->document() != aActiveDoc) {
917 // The object could be a root of sub-tree
918 ResultPartPtr aPartRes = getPartResult(aObj);
919 if (aPartRes.get()) {
920 if (aPartRes->partDoc() == aActiveDoc)
926 // A folder under sub-document
927 if (aActiveDoc.get() != aDoc)
933 //******************************************************
935 XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc, int aColumn) const
937 SessionPtr aSession = ModelAPI_Session::get();
938 DocumentPtr aRootDoc = aSession->moduleDocument();
939 if (myXMLReader->isAttachToResult()) { // If document is attached to result
940 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
942 ResultPartPtr aPartRes;
943 for (int i = 0; i < aNb; i++) {
944 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
945 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
946 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
948 if (myXMLReader->rootType() == ModelAPI_Feature::group()) {
949 aRow += foldersCount();
951 return createIndex(aRow, aColumn, aObj.get());
954 } else { // If document is attached to feature
955 int aNb = aRootDoc->size(ModelAPI_Feature::group(), true);
957 ResultPartPtr aPartRes;
958 for (int i = 0; i < aNb; i++) {
959 aObj = aRootDoc->object(ModelAPI_Feature::group(), i, true);
960 aPartRes = getPartResult(aObj.get());
961 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
963 if (myXMLReader->rootType() == ModelAPI_Feature::group())
964 aRow += foldersCount();
965 return createIndex(aRow, aColumn, aObj.get());
969 return QModelIndex();
972 //******************************************************
973 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc, int theColumn) const
975 SessionPtr aSession = ModelAPI_Session::get();
976 DocumentPtr aRootDoc = aSession->moduleDocument();
977 if (theDoc == aRootDoc)
978 return QModelIndex();
980 return findDocumentRootIndex(theDoc.get(), theColumn);
983 //******************************************************
984 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
987 SessionPtr aSession = ModelAPI_Session::get();
988 DocumentPtr aRootDoc = aSession->moduleDocument();
989 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
990 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
991 if (myXMLReader->rootShowEmpty(i))
994 if (aRootDoc->size(myXMLReader->rootFolderType(i)) > 0)
999 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
1000 if (myXMLReader->subShowEmpty(i))
1003 if (theDoc->size(myXMLReader->subFolderType(i)) > 0)
1012 //******************************************************
1013 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
1016 SessionPtr aSession = ModelAPI_Session::get();
1017 DocumentPtr aRootDoc = aSession->moduleDocument();
1018 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
1019 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
1020 if (!myXMLReader->rootShowEmpty(i)) {
1021 if (aRootDoc->size(myXMLReader->rootFolderType(i)) == 0)
1026 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
1027 if (!myXMLReader->subShowEmpty(i)) {
1028 if (theDoc->size(myXMLReader->subFolderType(i)) == 0)
1037 //******************************************************
1038 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
1040 QStringList aResult;
1042 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
1043 if (!myXMLReader->rootShowEmpty(i))
1044 aResult << myXMLReader->rootFolderType(i).c_str();
1047 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
1048 if (!myXMLReader->subShowEmpty(i))
1049 aResult << myXMLReader->subFolderType(i).c_str();
1055 //******************************************************
1056 QModelIndex XGUI_DataModel::lastHistoryIndex() const
1058 SessionPtr aSession = ModelAPI_Session::get();
1059 DocumentPtr aCurDoc = aSession->activeDocument();
1060 FeaturePtr aFeature = aCurDoc->currentFeature(true);
1061 if (aFeature.get()) {
1062 QModelIndex aInd = objectIndex(aFeature);
1063 return createIndex(aInd.row(), 2, aInd.internalPointer());
1065 if (aCurDoc == aSession->moduleDocument())
1066 return createIndex(foldersCount() - 1, 2, -1);
1068 return createIndex(foldersCount(aCurDoc.get()) - 1, 2, aCurDoc.get());
1072 //******************************************************
1073 bool XGUI_DataModel::hasHiddenState(const QModelIndex& theIndex)
1075 return getVisibilityState(theIndex) == Hidden;
1078 //******************************************************
1079 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc) const
1081 SessionPtr aSession = ModelAPI_Session::get();
1082 ModelAPI_Document* aDoc = theDoc;
1084 aDoc = aSession->moduleDocument().get();
1086 bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
1090 int aId = myXMLReader->subFolderId(theType);
1092 for (int i = 0; i < aId; i++) {
1093 if (!myXMLReader->subShowEmpty(i)) {
1094 if (aDoc->size(myXMLReader->subFolderType(i)) == 0)
1099 int aId = myXMLReader->rootFolderId(theType);
1101 for (int i = 0; i < aId; i++) {
1102 if (!myXMLReader->rootShowEmpty(i)) {
1103 if (aDoc->size(myXMLReader->rootFolderType(i)) == 0)
1111 //******************************************************
1112 void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex& theParent)
1115 removeRows(theRow, theCount, theParent);
1116 insertRows(theRow, theCount, theParent);
1120 //******************************************************
1121 //bool XGUI_DataModel::blockEventsProcessing(const bool theState)
1123 // bool aPreviousState = myIsEventsProcessingBlocked;
1124 // myIsEventsProcessingBlocked = theState;
1125 // return aPreviousState;
1128 //******************************************************
1129 XGUI_DataModel::VisibilityState
1130 XGUI_DataModel::getVisibilityState(const QModelIndex& theIndex) const
1132 Qt::ItemFlags aFlags = theIndex.flags();
1133 if (aFlags == Qt::ItemFlags())
1136 ObjectPtr aObj = object(theIndex);
1138 if (aObj->groupName() == ModelAPI_ResultParameter::group())
1140 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
1141 if (aResObj.get()) {
1142 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
1143 ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResObj);
1144 if (aCompRes.get()) {
1145 VisibilityState aState = aCompRes->numberOfSubs(true) == 0 ?
1146 (aDisplayer->isVisible(aCompRes)? Visible : Hidden) : NoneState;
1147 for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
1148 ResultPtr aSubRes = aCompRes->subResult(i, true);
1149 VisibilityState aS = aDisplayer->isVisible(aSubRes)? Visible : Hidden;
1150 if (aState == NoneState)
1152 else if (aState != aS) {
1153 aState = SemiVisible;
1159 if (aDisplayer->isVisible(aResObj))
1170 int XGUI_DataModel::getNumberOfFolderItems(const ModelAPI_Folder* theFolder) const
1172 DocumentPtr aDoc = theFolder->document();
1174 FeaturePtr aFirstFeatureInFolder;
1175 AttributeReferencePtr aFirstFeatAttr =
1176 theFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
1178 aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value());
1179 if (!aFirstFeatureInFolder.get())
1182 FeaturePtr aLastFeatureInFolder;
1183 AttributeReferencePtr aLastFeatAttr =
1184 theFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1186 aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
1187 if (!aLastFeatureInFolder.get())
1190 int aFirst = aDoc->index(aFirstFeatureInFolder);
1191 int aLast = aDoc->index(aLastFeatureInFolder);
1192 return aLast - aFirst + 1;
1195 ObjectPtr XGUI_DataModel::getObjectInFolder(const ModelAPI_Folder* theFolder, int theId) const
1197 DocumentPtr aDoc = theFolder->document();
1199 FeaturePtr aFirstFeatureInFolder;
1200 AttributeReferencePtr aFirstFeatAttr =
1201 theFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
1203 aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value());
1204 if (!aFirstFeatureInFolder.get())
1207 int aFirst = aDoc->index(aFirstFeatureInFolder);
1208 return aDoc->object(ModelAPI_Feature::group(), aFirst + theId);