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>
39 #include <Config_FeatureMessage.h>
40 #include <Config_DataModelReader.h>
42 #include <Events_Loop.h>
47 #define ACTIVE_COLOR QColor(Qt::black)
48 //#define ACTIVE_COLOR QColor(0,72,140)
49 //#define PASSIVE_COLOR Qt::black
51 /// Returns ResultPart object if the given object is a Part feature
52 /// Otherwise returns NULL
54 #define SELECTABLE_COLOR QColor(80, 80, 80)
55 #define DISABLED_COLOR QColor(200, 200, 200)
58 ResultPartPtr getPartResult(ModelAPI_Object* theObj)
60 ModelAPI_Feature* aFeature = dynamic_cast<ModelAPI_Feature*>(theObj);
62 ResultPtr aRes = aFeature->firstResult();
63 if (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group())) {
64 ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aRes);
65 // Use only original parts, not a placement results
66 if (aPartRes == aPartRes->original())
70 return ResultPartPtr();
73 /// Returns pointer on document if the given object is document object
74 ModelAPI_Document* getSubDocument(void* theObj)
76 ModelAPI_Document* aDoc = dynamic_cast<ModelAPI_Document*>((ModelAPI_Entity*)theObj);
83 // Constructor *************************************************
84 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : QAbstractItemModel(theParent)//,
85 //myIsEventsProcessingBlocked(false)
87 XGUI_ObjectsBrowser* aOB = qobject_cast<XGUI_ObjectsBrowser*>(theParent);
88 myWorkshop = aOB->workshop();
90 Events_Loop* aLoop = Events_Loop::loop();
91 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
92 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
93 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
94 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_ORDER_UPDATED));
95 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
98 XGUI_DataModel::~XGUI_DataModel()
103 //******************************************************
104 void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
106 //if (myIsEventsProcessingBlocked)
108 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
109 std::string aRootType = myXMLReader->rootType();
110 std::string aSubType = myXMLReader->subType();
111 int aNbFolders = foldersCount();
113 // Created object event *******************
114 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
115 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
116 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
117 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
119 std::set<ObjectPtr>::const_iterator aIt;
120 std::string aObjType;
121 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
122 ObjectPtr aObject = (*aIt);
123 // We do not show objects which does not need to be shown in object browser
124 if (!aObject->isInHistory())
127 aObjType = aObject->groupName();
128 DocumentPtr aDoc = aObject->document();
129 if (aDoc == aRootDoc) {
130 // Check that new folders could appear
131 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
132 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
133 if ((aNotEmptyFolder.toStdString() == aObjType) && (aRootDoc->size(aObjType) > 0)) {
134 // Appears first object in folder which can not be shown empty
135 if (!hasShownFolder(aRootDoc, aNotEmptyFolder)) {
136 insertRow(myXMLReader->rootFolderId(aObjType));
137 addShownFolder(aRootDoc, aNotEmptyFolder);
142 int aRow = aRootDoc->size(aObjType) - 1;
144 if (aObjType == aRootType) {
145 insertRow(aRow + aNbFolders + 1);
147 int aFolderId = myXMLReader->rootFolderId(aObjType);
148 if (aFolderId != -1) {
149 insertRow(aRow, createIndex(aFolderId, 0, (void*)Q_NULLPTR));
154 // Object created in sub-document
155 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get(), 0);
156 if (aDocRoot.isValid()) {
157 // Check that new folders could appear
158 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
159 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
160 if ((aNotEmptyFolder.toStdString() == aObjType) && (aDoc->size(aObjType) > 0)) {
161 // Appears first object in folder which can not be shown empty
162 if (!hasShownFolder(aDoc, aNotEmptyFolder)) {
163 insertRow(myXMLReader->subFolderId(aObjType), aDocRoot);
164 addShownFolder(aDoc, aNotEmptyFolder);
168 int aRow = aDoc->index(aObject);
170 int aNbSubFolders = foldersCount(aDoc.get());
171 if (aObjType == aSubType) {
172 // List of objects under document root
173 insertRow(aRow + aNbSubFolders, aDocRoot);
175 // List of objects under a folder
177 int aFolderId = folderId(aObjType, aDoc.get());
178 if (aFolderId != -1) {
179 QModelIndex aParentFolder = createIndex(aFolderId, 0, aDoc.get());
180 insertRow(aRow, aParentFolder);
181 emit dataChanged(aParentFolder, aParentFolder);
195 // Deleted object event ***********************
196 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
197 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
198 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
199 DocumentPtr aDoc = aUpdMsg->document();
200 std::set<std::string> aMsgGroups = aUpdMsg->groups();
202 /// Sort groups because RootType deletion has to be done after others
203 std::string aType = (aDoc == aRootDoc)? aRootType : aSubType;
204 std::list<std::string> aGroups;
205 std::set<std::string>::const_iterator aSetIt;
206 for (aSetIt = aMsgGroups.begin(); aSetIt != aMsgGroups.end(); ++aSetIt) {
207 std::string aGroup = (*aSetIt);
209 aGroups.push_back(aGroup);
211 aGroups.push_front(aGroup);
214 std::list<std::string>::const_iterator aIt;
215 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
216 std::string aGroup = (*aIt);
217 if (aDoc == aRootDoc) { // If root objects
218 int aRow = aRootDoc->size(aGroup);
219 if (aGroup == aRootType) {
220 // Process root folder
221 removeRow(aRow + aNbFolders);
222 rebuildBranch(aNbFolders, aRow);
224 // Process root sub-folder
225 int aFolderId = myXMLReader->rootFolderId(aGroup);
226 if (aFolderId != -1) {
227 QModelIndex aFolderIndex = createIndex(aFolderId, 0, (void*)Q_NULLPTR);
228 removeRow(aRow, aFolderIndex);
229 //rebuildBranch(0, aRow);
232 // Check that some folders could erased
233 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
234 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
235 if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup) == 0)) {
236 // Appears first object in folder which can not be shown empty
237 removeRow(myXMLReader->rootFolderId(aGroup));
238 removeShownFolder(aRootDoc, aNotEmptyFolder);
239 //rebuildBranch(0, aNbFolders + aDoc->size(myXMLReader->rootType()));
244 // Remove row for sub-document
245 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get(), 0);
246 if (aDocRoot.isValid()) {
247 int aRow = aDoc->size(aGroup);
248 int aNbSubFolders = foldersCount(aDoc.get());
249 if (aGroup == aSubType) {
250 // List of objects under document root
251 removeRow(aRow + aNbSubFolders, aDocRoot);
252 rebuildBranch(aNbSubFolders, aRow, aDocRoot);
254 // List of objects under a folder
255 int aFolderId = folderId(aGroup, aDoc.get());
256 if (aFolderId != -1) {
257 QModelIndex aFolderRoot = createIndex(aFolderId, 0, aDoc.get());
258 removeRow(aRow, aFolderRoot);
259 //rebuildBranch(0, aRow, aFolderRoot);
262 // Check that some folders could disappear
263 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
264 int aSize = aDoc->size(aGroup);
265 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
266 if ((aNotEmptyFolder.toStdString() == aGroup) && (aSize == 0)) {
267 // Appears first object in folder which can not be shown empty
268 removeRow(myXMLReader->subFolderId(aGroup), aDocRoot);
269 removeShownFolder(aDoc, aNotEmptyFolder);
270 //rebuildBranch(0, aNbSubFolders + aDoc->size(myXMLReader->subType()), aDocRoot);
280 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
281 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
282 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
283 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
285 std::set<ObjectPtr>::const_iterator aIt;
286 std::string aObjType;
287 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
288 ObjectPtr aObject = (*aIt);
289 if (aObject->data()->isValid()) {
290 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
291 if (aFeature.get() && aFeature->firstResult().get()
292 && (aFeature->firstResult()->groupName() == ModelAPI_ResultField::group())) {
293 ResultFieldPtr aResult =
294 std::dynamic_pointer_cast<ModelAPI_ResultField>(aFeature->firstResult());
295 QModelIndex aIndex = objectIndex(aResult, 0);
296 removeRows(0, aResult->stepsSize(), aIndex);
298 QModelIndex aIndex = objectIndex(aObject, 0);
299 if (aIndex.isValid()) {
300 emit dataChanged(aIndex, aIndex);
308 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
309 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aUpdMsg =
310 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
311 if (aUpdMsg->reordered().get()) {
312 DocumentPtr aDoc = aUpdMsg->reordered()->document();
313 std::string aGroup = aUpdMsg->reordered()->group();
317 if (aDoc == aRootDoc) {
318 // Update a group under root
319 if (aGroup == myXMLReader->rootType()) // Update objects under root
320 aStartId = foldersCount();
321 else // Update objects in folder under root
322 aParent = createIndex(folderId(aGroup), 0, (void*)Q_NULLPTR);
324 // Update a sub-document
325 if (aGroup == myXMLReader->subType()) {
326 // Update sub-document root
327 aParent = findDocumentRootIndex(aDoc.get(), 0);
328 aStartId = foldersCount(aDoc.get());
330 // update folder in sub-document
331 aParent = createIndex(folderId(aGroup, aDoc.get()), 0, aDoc.get());
333 int aChildNb = rowCount(aParent);
334 rebuildBranch(aStartId, aChildNb - aStartId, aParent);
338 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
339 DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
340 if (aDoc != aRootDoc) {
341 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get(), 0);
342 if (aDocRoot.isValid())
343 emit dataChanged(aDocRoot, aDocRoot);
345 // We have got a new document
351 //******************************************************
352 void XGUI_DataModel::clear()
358 //******************************************************
359 void XGUI_DataModel::rebuildDataTree()
366 //******************************************************
367 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
369 if (theIndex.internalId() == 0) // this is a folder
371 ModelAPI_Object* aObj =
372 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
375 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
378 return aObj->data()->owner();
381 //******************************************************
382 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject, int theColumn) const
384 std::string aType = theObject->groupName();
385 DocumentPtr aDoc = theObject->document();
386 int aRow = aDoc->index(theObject);
388 // it could be a part of complex object
389 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
390 if (aFeature.get()) {
391 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
392 if (aCompFea.get()) {
393 for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
394 if (aCompFea->subFeature(i, true) == theObject) {
401 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
403 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
404 if (aCompRes.get()) {
405 aRow = ModelAPI_Tools::compSolidIndex(aResult);
410 return QModelIndex();
412 return createIndex(aRow, theColumn, theObject.get());
414 SessionPtr aSession = ModelAPI_Session::get();
415 DocumentPtr aRootDoc = aSession->moduleDocument();
416 if (aDoc == aRootDoc && myXMLReader->rootType() == aType) {
417 // The object from root document
418 aRow += foldersCount();
419 } else if (myXMLReader->subType() == aType) {
420 // The object from sub document
421 aRow += foldersCount(aDoc.get());
423 return createIndex(aRow, theColumn, theObject.get());
426 //******************************************************
427 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
429 SessionPtr aSession = ModelAPI_Session::get();
430 DocumentPtr aRootDoc = aSession->moduleDocument();
431 int aNbFolders = foldersCount();
432 int theIndexRow = theIndex.row();
434 if (theRole == Qt::DecorationRole) {
435 if (theIndex == lastHistoryIndex())
436 return QIcon(":pictures/arrow.png");
437 else if (theIndex.column() == 0) {
438 VisibilityState aState = getVisibilityState(theIndex);
443 return QIcon(":pictures/eyeopen.png");
445 return QIcon(":pictures/eyemiclosed.png");
447 return QIcon(":pictures/eyeclosed.png");
452 //if (theIndex.column() == 1)
453 if (theIndex.column() != 1)
456 quintptr aParentId = theIndex.internalId();
457 if (aParentId == 0) { // root folders
459 case Qt::DisplayRole:
460 return QString(myXMLReader->rootFolderName(theIndexRow).c_str()) +
461 QString(" (%1)").arg(rowCount(theIndex));
462 case Qt::DecorationRole:
463 return QIcon(myXMLReader->rootFolderIcon(theIndexRow).c_str());
464 case Qt::ForegroundRole:
466 Qt::ItemFlags aFlags = theIndex.flags();
467 if (aFlags == Qt::ItemFlags())
468 return QBrush(DISABLED_COLOR);
469 if (!aFlags.testFlag(Qt::ItemIsEditable))
470 return QBrush(SELECTABLE_COLOR);
474 } else { // an object or sub-document
475 if (theRole == Qt::ForegroundRole) {
476 Qt::ItemFlags aFlags = theIndex.flags();
477 if (aFlags == Qt::ItemFlags())
478 return QBrush(DISABLED_COLOR);
479 if (!aFlags.testFlag(Qt::ItemIsEditable))
480 return QBrush(SELECTABLE_COLOR);
484 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
485 if (aSubDoc) { // this is a folder of sub document
486 QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
487 int aRow = theIndexRow;
488 while (aMissedIdx.contains(aRow))
490 if (aRow < myXMLReader->subFoldersNumber()) {
492 case Qt::DisplayRole:
493 return QString(myXMLReader->subFolderName(aRow).c_str()) +
494 QString(" (%1)").arg(rowCount(theIndex));
495 case Qt::DecorationRole:
496 return QIcon(myXMLReader->subFolderIcon(aRow).c_str());
500 ModelAPI_Object* aObj =
501 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
504 case Qt::DisplayRole:
506 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
507 ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
508 AttributeDoublePtr aValueAttribute =
509 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
510 QString aVal = QString::number(aValueAttribute->value());
511 QString aTitle = QString(aObj->data()->name().c_str());
512 return aTitle + " = " + aVal;
515 if (aObj->groupName() == myXMLReader->subType()) {
516 ResultPartPtr aPartRes = getPartResult(aObj);
517 if (aPartRes.get()) {
518 if (aPartRes->partDoc().get() == NULL)
519 aSuffix = " (Not loaded)";
522 return aObj->data()->name().c_str() + aSuffix;
524 case Qt::DecorationRole:
525 return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
529 case Qt::DisplayRole:
531 ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
532 dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>
533 ((ModelAPI_Entity*)theIndex.internalPointer());
535 return "Step " + QString::number(aStep->id() + 1) + " " +
536 aStep->field()->textLine(aStep->id()).c_str();
547 //******************************************************
548 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
553 //******************************************************
554 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
556 SessionPtr aSession = ModelAPI_Session::get();
557 if (!aSession->hasModuleDocument())
559 DocumentPtr aRootDoc = aSession->moduleDocument();
561 if (!theParent.isValid()) {
562 // Return number of items in root
563 int aNbFolders = foldersCount();
565 std::string aType = myXMLReader->rootType();
567 aNbItems = aRootDoc->size(aType);
568 return aNbFolders + aNbItems;
571 quintptr aId = theParent.internalId();
573 // this is a folder under root
574 int aParentPos = theParent.row();
575 std::string aType = myXMLReader->rootFolderType(aParentPos);
576 return aRootDoc->size(aType);
578 // It is an object which could have children
579 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
581 // a folder of sub-document
582 QIntList aMissedIdx = missedFolderIndexes(aDoc);
583 int aRow = theParent.row();
584 while (aMissedIdx.contains(aRow))
586 if (aRow < myXMLReader->subFoldersNumber()) {
587 std::string aType = myXMLReader->subFolderType(aRow);
588 return aDoc->size(aType);
591 ModelAPI_Object* aObj =
592 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theParent.internalPointer());
593 // Check for Part feature
594 ResultPartPtr aPartRes = getPartResult(aObj);
595 if (aPartRes.get()) {
596 DocumentPtr aSubDoc = aPartRes->partDoc();
600 int aNbSubFolders = foldersCount(aSubDoc.get());
602 std::string aSubType = myXMLReader->subType();
603 if (!aSubType.empty())
604 aNbSubItems = aSubDoc->size(aSubType);
605 return aNbSubItems + aNbSubFolders;
607 // Check for composite object
608 ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
610 return aCompFeature->numberOfSubs(true);
611 ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
613 return aCompRes->numberOfSubs(true);
614 ModelAPI_ResultField* aFieldRes = dynamic_cast<ModelAPI_ResultField*>(aObj);
616 return aFieldRes->stepsSize();
623 //******************************************************
624 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
629 //******************************************************
630 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
632 SessionPtr aSession = ModelAPI_Session::get();
633 DocumentPtr aRootDoc = aSession->moduleDocument();
634 int aNbFolders = foldersCount();
638 if (!theParent.isValid()) {
639 if (theRow < aNbFolders) // Return first level folder index
640 return createIndex(theRow, theColumn, (void*)Q_NULLPTR);
641 else { // return object under root index
642 std::string aType = myXMLReader->rootType();
643 int aObjId = theRow - aNbFolders;
644 if (aObjId < aRootDoc->size(aType)) {
645 ObjectPtr aObj = aRootDoc->object(aType, aObjId);
646 aIndex = objectIndex(aObj, theColumn);
650 quintptr aId = theParent.internalId();
651 int aParentPos = theParent.row();
652 if (aId == 0) { // return object index inside of first level of folders
653 std::string aType = myXMLReader->rootFolderType(aParentPos);
654 if (theRow < aRootDoc->size(aType)) {
655 ObjectPtr aObj = aRootDoc->object(aType, theRow);
656 aIndex = objectIndex(aObj, theColumn);
659 // It is an object which could have children
660 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
662 // It is a folder of sub-document
663 int aParentRow = aParentPos;
664 QIntList aMissedIdx = missedFolderIndexes(aDoc);
665 while (aMissedIdx.contains(aParentRow))
667 if (aParentRow < myXMLReader->subFoldersNumber()) {
668 std::string aType = myXMLReader->subFolderType(aParentRow);
669 if (theRow < aDoc->size(aType)) {
670 ObjectPtr aObj = aDoc->object(aType, theRow);
671 aIndex = objectIndex(aObj, theColumn);
675 ModelAPI_Object* aParentObj =
676 dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theParent.internalPointer());
678 // Check for Part feature
679 ResultPartPtr aPartRes = getPartResult(aParentObj);
680 if (aPartRes.get()) {
681 DocumentPtr aSubDoc = aPartRes->partDoc();
682 int aNbSubFolders = foldersCount(aSubDoc.get());
683 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
684 aIndex = createIndex(theRow, theColumn, aSubDoc.get());
686 // this is an object under sub document root
687 std::string aType = myXMLReader->subType();
688 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
689 aIndex = objectIndex(aObj, theColumn);
692 // Check for composite object
693 ModelAPI_CompositeFeature* aCompFeature =
694 dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
696 aIndex = objectIndex(aCompFeature->subFeature(theRow), theColumn);
698 ModelAPI_ResultCompSolid* aCompRes =
699 dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
701 aIndex = objectIndex(aCompRes->subResult(theRow), theColumn);
703 ModelAPI_ResultField* aFieldRes =
704 dynamic_cast<ModelAPI_ResultField*>(aParentObj);
706 aIndex = createIndex(theRow, theColumn, aFieldRes->step(theRow));
717 //******************************************************
718 static QModelIndex MYLastDeleted;
719 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
721 if (!theIndex.isValid())
722 return QModelIndex();
723 // To avoid additional request about index which was already deleted
724 if (theIndex == MYLastDeleted)
725 return QModelIndex();
727 SessionPtr aSession = ModelAPI_Session::get();
728 quintptr aId = theIndex.internalId();
729 if (aId != 0) { // The object is not a root folder
730 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
732 // It is a folder of sub-document
733 return findDocumentRootIndex(aDoc);
735 ObjectPtr aObj = object(theIndex);
737 // It can b e a step of a field
738 ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
739 dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>
740 ((ModelAPI_Entity*)theIndex.internalPointer());
742 ModelAPI_ResultField* aField = aStep->field();
743 DocumentPtr aDoc = aSession->activeDocument();
745 for(int i = 0; i < aDoc->size(ModelAPI_ResultField::group()); i++) {
746 aFld = aDoc->object(ModelAPI_ResultField::group(), i);
747 if (aFld.get() == aField)
748 return objectIndex(aFld);
751 // To avoid additional request about index which was already deleted
752 // If deleted it causes a crash on delete object from Part
753 MYLastDeleted = theIndex;
754 return QModelIndex();
756 // Check is it object a sub-object of a complex object
757 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
758 if (aFeature.get()) {
759 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
760 if (aCompFea.get()) {
761 return objectIndex(aCompFea);
764 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
766 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
767 if (aCompRes.get()) {
768 return objectIndex(aCompRes);
771 // Use as ordinary object
772 std::string aType = aObj->groupName();
773 DocumentPtr aRootDoc = aSession->moduleDocument();
774 DocumentPtr aSubDoc = aObj->document();
775 if (aSubDoc == aRootDoc) {
776 if (aType == myXMLReader->rootType())
777 return QModelIndex();
779 // return first level of folder index
780 int aFolderId = myXMLReader->rootFolderId(aType);
781 // Items in a one row must have the same parent
782 return createIndex(aFolderId, 1, (void*)Q_NULLPTR);
785 if (aType == myXMLReader->subType())
786 return findDocumentRootIndex(aSubDoc.get());
788 // return first level of folder index
789 int aFolderId = folderId(aType, aSubDoc.get());
790 // Items in a one row must have the same parent
791 return createIndex(aFolderId, 1, aSubDoc.get());
795 return QModelIndex();
798 //******************************************************
799 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
801 return rowCount(theParent) > 0;
804 //******************************************************
805 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
807 beginInsertRows(theParent, theRow, theRow + theCount - 1);
813 //******************************************************
814 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
816 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
821 //******************************************************
822 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
824 quintptr aIt = theIndex.internalId();
825 ModelAPI_Object* aObj = 0;
826 ModelAPI_Document* aDoc = 0;
827 SessionPtr aSession = ModelAPI_Session::get();
828 DocumentPtr aActiveDoc = aSession->activeDocument();
830 Qt::ItemFlags aNullFlag;
831 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
832 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
836 // Folders under root
837 DocumentPtr aRootDoc = aSession->moduleDocument();
838 if (aRootDoc != aActiveDoc)
841 aDoc = getSubDocument(theIndex.internalPointer());
843 aObj = dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
848 if (aObj->isDisabled())
849 return theIndex.column() == 2? Qt::ItemIsSelectable : aNullFlag;
851 if (aSession->moduleDocument() != aObj->document())
852 if (aActiveDoc != aObj->document())
853 return theIndex.column() == 2? Qt::ItemIsSelectable : aNullFlag;
855 bool isCompositeSub = false;
856 // An object which is sub-object of a composite object can not be accessible in column 2
857 if (theIndex.column() == 2) {
858 ObjectPtr aObjPtr = aObj->data()->owner();
859 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
860 if (aFeature.get()) {
861 CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
863 isCompositeSub = true;
865 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
867 ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
869 isCompositeSub = true;
874 return Qt::ItemIsSelectable;
876 if (aObj->document() != aActiveDoc) {
877 // The object could be a root of sub-tree
878 ResultPartPtr aPartRes = getPartResult(aObj);
879 if (aPartRes.get()) {
880 if (aPartRes->partDoc() == aActiveDoc)
886 // A folder under sub-document
887 if (aActiveDoc.get() != aDoc)
893 //******************************************************
895 XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc, int aColumn) const
897 SessionPtr aSession = ModelAPI_Session::get();
898 DocumentPtr aRootDoc = aSession->moduleDocument();
899 if (myXMLReader->isAttachToResult()) { // If document is attached to result
900 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
902 ResultPartPtr aPartRes;
903 for (int i = 0; i < aNb; i++) {
904 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
905 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
906 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
908 if (myXMLReader->rootType() == ModelAPI_Feature::group()) {
909 aRow += foldersCount();
911 return createIndex(aRow, aColumn, aObj.get());
914 } else { // If document is attached to feature
915 int aNb = aRootDoc->size(ModelAPI_Feature::group());
917 ResultPartPtr aPartRes;
918 for (int i = 0; i < aNb; i++) {
919 aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
920 aPartRes = getPartResult(aObj.get());
921 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
923 if (myXMLReader->rootType() == ModelAPI_Feature::group())
924 aRow += foldersCount();
925 return createIndex(aRow, aColumn, aObj.get());
929 return QModelIndex();
932 //******************************************************
933 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc, int theColumn) const
935 SessionPtr aSession = ModelAPI_Session::get();
936 DocumentPtr aRootDoc = aSession->moduleDocument();
937 if (theDoc == aRootDoc)
938 return QModelIndex();
940 return findDocumentRootIndex(theDoc.get(), theColumn);
943 //******************************************************
944 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
947 SessionPtr aSession = ModelAPI_Session::get();
948 DocumentPtr aRootDoc = aSession->moduleDocument();
949 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
950 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
951 if (myXMLReader->rootShowEmpty(i))
954 if (aRootDoc->size(myXMLReader->rootFolderType(i)) > 0)
959 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
960 if (myXMLReader->subShowEmpty(i))
963 if (theDoc->size(myXMLReader->subFolderType(i)) > 0)
972 //******************************************************
973 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
976 SessionPtr aSession = ModelAPI_Session::get();
977 DocumentPtr aRootDoc = aSession->moduleDocument();
978 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
979 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
980 if (!myXMLReader->rootShowEmpty(i)) {
981 if (aRootDoc->size(myXMLReader->rootFolderType(i)) == 0)
986 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
987 if (!myXMLReader->subShowEmpty(i)) {
988 if (theDoc->size(myXMLReader->subFolderType(i)) == 0)
997 //******************************************************
998 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
1000 QStringList aResult;
1002 for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
1003 if (!myXMLReader->rootShowEmpty(i))
1004 aResult << myXMLReader->rootFolderType(i).c_str();
1007 for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
1008 if (!myXMLReader->subShowEmpty(i))
1009 aResult << myXMLReader->subFolderType(i).c_str();
1015 //******************************************************
1016 QModelIndex XGUI_DataModel::lastHistoryIndex() const
1018 SessionPtr aSession = ModelAPI_Session::get();
1019 DocumentPtr aCurDoc = aSession->activeDocument();
1020 FeaturePtr aFeature = aCurDoc->currentFeature(true);
1021 if (aFeature.get()) {
1022 QModelIndex aInd = objectIndex(aFeature);
1023 return createIndex(aInd.row(), 2, aInd.internalPointer());
1025 if (aCurDoc == aSession->moduleDocument())
1026 return createIndex(foldersCount() - 1, 2, -1);
1028 return createIndex(foldersCount(aCurDoc.get()) - 1, 2, aCurDoc.get());
1032 //******************************************************
1033 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc) const
1035 SessionPtr aSession = ModelAPI_Session::get();
1036 ModelAPI_Document* aDoc = theDoc;
1038 aDoc = aSession->moduleDocument().get();
1040 bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
1044 int aId = myXMLReader->subFolderId(theType);
1046 for (int i = 0; i < aId; i++) {
1047 if (!myXMLReader->subShowEmpty(i)) {
1048 if (aDoc->size(myXMLReader->subFolderType(i)) == 0)
1053 int aId = myXMLReader->rootFolderId(theType);
1055 for (int i = 0; i < aId; i++) {
1056 if (!myXMLReader->rootShowEmpty(i)) {
1057 if (aDoc->size(myXMLReader->rootFolderType(i)) == 0)
1065 //******************************************************
1066 void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex& theParent)
1069 removeRows(theRow, theCount, theParent);
1070 insertRows(theRow, theCount, theParent);
1074 //******************************************************
1075 //bool XGUI_DataModel::blockEventsProcessing(const bool theState)
1077 // bool aPreviousState = myIsEventsProcessingBlocked;
1078 // myIsEventsProcessingBlocked = theState;
1079 // return aPreviousState;
1082 //******************************************************
1083 XGUI_DataModel::VisibilityState
1084 XGUI_DataModel::getVisibilityState(const QModelIndex& theIndex) const
1086 Qt::ItemFlags aFlags = theIndex.flags();
1087 if (aFlags == Qt::ItemFlags())
1090 ObjectPtr aObj = object(theIndex);
1092 if (aObj->groupName() == ModelAPI_ResultParameter::group())
1094 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
1095 if (aResObj.get()) {
1096 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
1097 ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResObj);
1098 if (aCompRes.get()) {
1099 VisibilityState aState = aCompRes->numberOfSubs(true) == 0 ?
1100 (aDisplayer->isVisible(aCompRes)? Visible : Hidden) : NoneState;
1101 for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
1102 ResultPtr aSubRes = aCompRes->subResult(i, true);
1103 VisibilityState aS = aDisplayer->isVisible(aSubRes)? Visible : Hidden;
1104 if (aState == NoneState)
1106 else if (aState != aS) {
1107 aState = SemiVisible;
1113 if (aDisplayer->isVisible(aResObj))