Salome HOME
Issue #2371: Fatal error when undo/redo delete folder
[modules/shaper.git] / src / XGUI / XGUI_DataModel.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "XGUI_DataModel.h"
22 #include "XGUI_Workshop.h"
23 #include "XGUI_ObjectsBrowser.h"
24 #include "XGUI_Displayer.h"
25
26 #include <ModuleBase_IconFactory.h>
27
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>
40
41 #include <Config_FeatureMessage.h>
42 #include <Config_DataModelReader.h>
43
44 #include <Events_Loop.h>
45
46 #include <QIcon>
47 #include <QBrush>
48
49 #define ACTIVE_COLOR QColor(Qt::black)
50 //#define ACTIVE_COLOR QColor(0,72,140)
51 //#define PASSIVE_COLOR Qt::black
52
53 /// Returns ResultPart object if the given object is a Part feature
54 /// Otherwise returns NULL
55
56 #define SELECTABLE_COLOR QColor(80, 80, 80)
57 #define DISABLED_COLOR QColor(200, 200, 200)
58
59
60 ResultPartPtr getPartResult(ModelAPI_Object* theObj)
61 {
62   ModelAPI_Feature* aFeature = dynamic_cast<ModelAPI_Feature*>(theObj);
63   if (aFeature) {
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())
69       return aPartRes;
70     }
71   }
72   return ResultPartPtr();
73 }
74
75 /// Returns pointer on document if the given object is document object
76 ModelAPI_Document* getSubDocument(void* theObj)
77 {
78   ModelAPI_Document* aDoc = 0;
79   try {
80     aDoc = dynamic_cast<ModelAPI_Document*>((ModelAPI_Entity*)theObj);
81   } catch(...) {}
82   return aDoc;
83 }
84
85
86
87
88 // Constructor *************************************************
89 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : QAbstractItemModel(theParent)//,
90   //myIsEventsProcessingBlocked(false)
91 {
92   XGUI_ObjectsBrowser* aOB = qobject_cast<XGUI_ObjectsBrowser*>(theParent);
93   myWorkshop = aOB->workshop();
94
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));
101 }
102
103 XGUI_DataModel::~XGUI_DataModel()
104 {
105   clear();
106 }
107
108 //******************************************************
109 void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
110 {
111   //if (myIsEventsProcessingBlocked)
112   //  return;
113   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
114   std::string aRootType = myXMLReader->rootType();
115   std::string aSubType = myXMLReader->subType();
116   int aNbFolders = foldersCount();
117
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();
123
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())
130         continue;
131
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);
143             }
144           }
145         }
146         // Insert new object
147         int aRow = aRootDoc->size(aObjType) - 1;
148         if (aRow != -1) {
149           if ((aObjType == aRootType) || (aObjType == ModelAPI_Folder::group())) {
150             insertRow(aRow + aNbFolders + 1);
151           } else {
152             int aFolderId = myXMLReader->rootFolderId(aObjType);
153             if (aFolderId != -1) {
154               insertRow(aRow, createIndex(aFolderId, 0, (void*)Q_NULLPTR));
155             }
156           }
157         }
158       } else {
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);
170               }
171             }
172          }
173           int aRow = aDoc->index(aObject, true);
174           if (aRow != -1) {
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);
179             } else {
180               // List of objects under a folder
181               if (aRow != -1) {
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);
187                 }
188               }
189             }
190           } else {
191             rebuildDataTree();
192             break;
193           }
194         } else {
195           rebuildDataTree();
196           break;
197         }
198       }
199     }
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();
206
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);
213       if (aGroup == aType)
214         aGroups.push_back(aGroup);
215       else
216         aGroups.push_front(aGroup);
217     }
218
219     std::list<std::string>::const_iterator aIt;
220     for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
221       std::string aGroup = (*aIt);
222       if (aDoc == aRootDoc) {  // If root objects
223         int aRow = aRootDoc->size(aGroup, true);
224         if ((aGroup == aRootType) || (aGroup == ModelAPI_Folder::group())) {
225           // Process root folder
226           removeRow(aRow + aNbFolders);
227           rebuildBranch(aNbFolders, aRow);
228         } else {
229           // Process root sub-folder
230           int aFolderId = myXMLReader->rootFolderId(aGroup);
231           if (aFolderId != -1) {
232             QModelIndex aFolderIndex = createIndex(aFolderId, 0, (void*)Q_NULLPTR);
233             removeRow(aRow, aFolderIndex);
234             //rebuildBranch(0, aRow);
235           }
236         }
237         // Check that some folders could erased
238         QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
239         foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
240           if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup, true) == 0)) {
241             // Appears first object in folder which can not be shown empty
242             removeRow(myXMLReader->rootFolderId(aGroup));
243             removeShownFolder(aRootDoc, aNotEmptyFolder);
244             //rebuildBranch(0, aNbFolders + aDoc->size(myXMLReader->rootType()));
245             break;
246           }
247         }
248       } else {
249         // Remove row for sub-document
250         QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get(), 0);
251         if (aDocRoot.isValid()) {
252           int aRow = aDoc->size(aGroup, true);
253           int aNbSubFolders = foldersCount(aDoc.get());
254           if ((aGroup == aSubType) || (aGroup == ModelAPI_Folder::group())) {
255             // List of objects under document root
256             removeRow(aRow + aNbSubFolders, aDocRoot);
257             rebuildBranch(aNbSubFolders, aRow, aDocRoot);
258           } else {
259             // List of objects under a folder
260             int aFolderId = folderId(aGroup, aDoc.get());
261             if (aFolderId != -1) {
262               QModelIndex aFolderRoot = createIndex(aFolderId, 0, aDoc.get());
263               removeRow(aRow, aFolderRoot);
264               //rebuildBranch(0, aRow, aFolderRoot);
265             }
266           }
267           // Check that some folders could disappear
268           QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
269           int aSize = aDoc->size(aGroup, true);
270           foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
271             if ((aNotEmptyFolder.toStdString() == aGroup) && (aSize == 0)) {
272               // Appears first object in folder which can not be shown empty
273               removeRow(myXMLReader->subFolderId(aGroup), aDocRoot);
274               removeShownFolder(aDoc, aNotEmptyFolder);
275               //rebuildBranch(0, aNbSubFolders + aDoc->size(myXMLReader->subType()), aDocRoot);
276               break;
277             }
278           }
279         } else {
280           rebuildDataTree();
281           break;
282         }
283       }
284     }
285   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
286     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
287         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
288     std::set<ObjectPtr> aObjects = aUpdMsg->objects();
289
290     std::set<ObjectPtr>::const_iterator aIt;
291     for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
292       ObjectPtr aObject = (*aIt);
293       if (aObject->data()->isValid()) {
294         FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
295         if (aFeature.get() && aFeature->firstResult().get()
296           && (aFeature->firstResult()->groupName() == ModelAPI_ResultField::group())) {
297             ResultFieldPtr aResult =
298               std::dynamic_pointer_cast<ModelAPI_ResultField>(aFeature->firstResult());
299             QModelIndex aIndex = objectIndex(aResult, 0);
300             removeRows(0, aResult->stepsSize(), aIndex);
301         } else {
302           if (aObject->groupName() == ModelAPI_Folder::group()) {
303             rebuildDataTree();
304           } else {
305             QModelIndex aIndex = objectIndex(aObject, 0);
306             if (aIndex.isValid()) {
307               emit dataChanged(aIndex, aIndex);
308             }
309           }
310         }
311       } else {
312         rebuildDataTree();
313         break;
314       }
315     }
316   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
317     std::shared_ptr<ModelAPI_OrderUpdatedMessage> aUpdMsg =
318         std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
319     if (aUpdMsg->reordered().get()) {
320       DocumentPtr aDoc = aUpdMsg->reordered()->document();
321       std::string aGroup = aUpdMsg->reordered()->group();
322
323       QModelIndex aParent;
324       int aStartId = 0;
325       if (aDoc == aRootDoc) {
326         // Update a group under root
327         if (aGroup == myXMLReader->rootType()) // Update objects under root
328           aStartId = foldersCount();
329         else // Update objects in folder under root
330           aParent = createIndex(folderId(aGroup), 0, (void*)Q_NULLPTR);
331       } else {
332         // Update a sub-document
333         if (aGroup == myXMLReader->subType()) {
334           // Update sub-document root
335           aParent = findDocumentRootIndex(aDoc.get(), 0);
336           aStartId = foldersCount(aDoc.get());
337         } else
338           // update folder in sub-document
339           aParent = createIndex(folderId(aGroup, aDoc.get()), 0, aDoc.get());
340       }
341       int aChildNb = rowCount(aParent);
342       rebuildBranch(aStartId, aChildNb - aStartId, aParent);
343     } else {
344       rebuildDataTree();
345     }
346   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
347     DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
348     if (aDoc != aRootDoc) {
349       QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get(), 0);
350       if (aDocRoot.isValid())
351         emit dataChanged(aDocRoot, aDocRoot);
352       else
353         // We have got a new document
354         rebuildDataTree();
355     }
356   }
357 }
358
359 //******************************************************
360 void XGUI_DataModel::clear()
361 {
362   beginResetModel();
363   endResetModel();
364 }
365
366 //******************************************************
367 void XGUI_DataModel::rebuildDataTree()
368 {
369   beginResetModel();
370   endResetModel();
371   emit treeRebuilt();
372 }
373
374 //******************************************************
375 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
376 {
377   if (theIndex.internalId() == 0) // this is a folder
378     return ObjectPtr();
379   ModelAPI_Object* aObj = 0;
380   try {
381     aObj = dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
382   } catch(...) {}
383
384   if (!aObj)
385     return ObjectPtr();
386   if (getSubDocument(aObj)) // the selected index is a folder of sub-document
387     return ObjectPtr();
388
389   return aObj->data()->owner();
390 }
391
392 //******************************************************
393 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject, int theColumn) const
394 {
395   std::string aType = theObject->groupName();
396   DocumentPtr aDoc = theObject->document();
397   int aRow = aDoc->index(theObject, true);
398   if (aRow == -1) {
399     // it could be a part of complex object
400     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
401     if (aFeature.get()) {
402       CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
403       if (aCompFea.get()) {
404         for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
405           if (aCompFea->subFeature(i, true) == theObject) {
406             aRow = i;
407             break;
408           }
409         }
410       }
411       int aFRow = -1;
412       FolderPtr aFolder = aDoc->findContainingFolder(aFeature, aFRow);
413       if (aFolder.get())
414         aRow = aFRow;
415     } else {
416       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
417       if (aResult.get()) {
418         ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
419         if (aCompRes.get()) {
420           aRow = ModelAPI_Tools::compSolidIndex(aResult);
421         }
422       }
423     }
424     if (aRow == -1)
425       return QModelIndex();
426     else
427       return createIndex(aRow, theColumn, theObject.get());
428   }
429   SessionPtr aSession = ModelAPI_Session::get();
430   DocumentPtr aRootDoc = aSession->moduleDocument();
431   if (aDoc == aRootDoc &&
432     ((myXMLReader->rootType() == aType) || (aType == ModelAPI_Folder::group()))) {
433     // The object from root document
434     aRow += foldersCount();
435   } else if ((myXMLReader->subType() == aType) || (aType == ModelAPI_Folder::group())) {
436     // The object from sub document
437     aRow += foldersCount(aDoc.get());
438   }
439   return createIndex(aRow, theColumn, theObject.get());
440 }
441
442 //******************************************************
443 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
444 {
445   SessionPtr aSession = ModelAPI_Session::get();
446   DocumentPtr aRootDoc = aSession->moduleDocument();
447   int aNbFolders = foldersCount();
448   int theIndexRow = theIndex.row();
449
450   if (theRole == Qt::DecorationRole) {
451     if (theIndex == lastHistoryIndex())
452       return QIcon(":pictures/arrow.png");
453     else if (theIndex.column() == 0) {
454       VisibilityState aState = getVisibilityState(theIndex);
455       switch (aState) {
456       case NoneState:
457         return QIcon();
458       case Visible:
459         return QIcon(":pictures/eyeopen.png");
460       case SemiVisible:
461         return QIcon(":pictures/eyemiclosed.png");
462       case Hidden:
463         return QIcon(":pictures/eyeclosed.png");
464       }
465     }
466   }
467
468   //if (theIndex.column() == 1)
469   if (theIndex.column() != 1)
470     return QVariant();
471
472   quintptr aParentId = theIndex.internalId();
473   if (aParentId == 0) { // root folders
474     switch (theRole) {
475       case Qt::DisplayRole:
476         return QString(myXMLReader->rootFolderName(theIndexRow).c_str()) +
477           QString(" (%1)").arg(rowCount(theIndex));
478       case Qt::DecorationRole:
479         return QIcon(myXMLReader->rootFolderIcon(theIndexRow).c_str());
480       case Qt::ForegroundRole:
481         {
482           Qt::ItemFlags aFlags = theIndex.flags();
483           if (aFlags == Qt::ItemFlags())
484             return QBrush(DISABLED_COLOR);
485           if (!aFlags.testFlag(Qt::ItemIsEditable))
486             return QBrush(SELECTABLE_COLOR);
487         }
488         return ACTIVE_COLOR;
489     }
490   } else { // an object or sub-document
491     if (theRole == Qt::ForegroundRole) {
492       Qt::ItemFlags aFlags = theIndex.flags();
493       if (aFlags == Qt::ItemFlags())
494         return QBrush(DISABLED_COLOR);
495       if (!aFlags.testFlag(Qt::ItemIsEditable))
496         return QBrush(SELECTABLE_COLOR);
497       return ACTIVE_COLOR;
498     }
499
500     ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
501     if (aSubDoc) { // this is a folder of sub document
502       QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
503       int aRow = theIndexRow;
504       while (aMissedIdx.contains(aRow))
505         aRow++;
506       if (aRow < myXMLReader->subFoldersNumber()) {
507         switch (theRole) {
508           case Qt::DisplayRole:
509             return QString(myXMLReader->subFolderName(aRow).c_str()) +
510               QString(" (%1)").arg(rowCount(theIndex));
511           case Qt::DecorationRole:
512             return QIcon(myXMLReader->subFolderIcon(aRow).c_str());
513         }
514       }
515     } else {
516       ObjectPtr aObj = object(theIndex);
517       if (aObj) {
518         switch (theRole) {
519         case Qt::DisplayRole:
520           {
521             if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
522               ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObj);
523               AttributeDoublePtr aValueAttribute =
524                 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
525               QString aVal = QString::number(aValueAttribute->value());
526               QString aTitle = QString(aObj->data()->name().c_str());
527               return aTitle + " = " + aVal;
528             }
529             QString aSuffix;
530             if (aObj->groupName() == myXMLReader->subType()) {
531               ResultPartPtr aPartRes = getPartResult(aObj.get());
532               if (aPartRes.get()) {
533                 if (aPartRes->partDoc().get() == NULL)
534                   aSuffix = " (Not loaded)";
535               }
536             }
537             return aObj->data()->name().c_str() + aSuffix;
538           }
539         case Qt::DecorationRole:
540           {
541             if (aObj->groupName() == ModelAPI_Folder::group())
542               return QIcon(":pictures/features_folder.png");
543             else
544               return ModuleBase_IconFactory::get()->getIcon(aObj);
545           }
546         }
547       } else {
548         switch (theRole) {
549         case Qt::DisplayRole:
550           {
551             ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
552               dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>
553               ((ModelAPI_Entity*)theIndex.internalPointer());
554             if (aStep) {
555               return "Step " + QString::number(aStep->id() + 1) + " " +
556                 aStep->field()->textLine(aStep->id()).c_str();
557             }
558           }
559           break;
560         }
561       }
562     }
563   }
564   return QVariant();
565 }
566
567 //******************************************************
568 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
569 {
570   return QVariant();
571 }
572
573 //******************************************************
574 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
575 {
576   SessionPtr aSession = ModelAPI_Session::get();
577   if (!aSession->hasModuleDocument())
578     return 0;
579   DocumentPtr aRootDoc = aSession->moduleDocument();
580
581   if (!theParent.isValid()) {
582     // Return number of items in root
583     int aNbFolders = foldersCount();
584     int aNbItems = 0;
585     std::string aType = myXMLReader->rootType();
586     if (!aType.empty())
587       aNbItems = aRootDoc->size(aType, true);
588     return aNbFolders + aNbItems;
589   }
590
591   quintptr aId = theParent.internalId();
592   if (aId == 0) {
593     // this is a folder under root
594     int aParentPos = theParent.row();
595     std::string aType = myXMLReader->rootFolderType(aParentPos);
596     return aRootDoc->size(aType);
597   } else {
598     // It is an object which could have children
599     ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
600     if (aDoc) {
601       // a folder of sub-document
602       QIntList aMissedIdx = missedFolderIndexes(aDoc);
603       int aRow = theParent.row();
604       while (aMissedIdx.contains(aRow))
605         aRow++;
606       if (aRow < myXMLReader->subFoldersNumber()) {
607         std::string aType = myXMLReader->subFolderType(aRow);
608         return aDoc->size(aType);
609       }
610     } else {
611       ModelAPI_Object* aObj =
612         dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theParent.internalPointer());
613       // Check for Part feature
614       ResultPartPtr aPartRes = getPartResult(aObj);
615       if (aPartRes.get()) {
616         DocumentPtr aSubDoc = aPartRes->partDoc();
617         if (!aSubDoc.get())
618           return 0;
619
620         int aNbSubFolders = foldersCount(aSubDoc.get());
621         int aNbSubItems = 0;
622         std::string aSubType = myXMLReader->subType();
623         if (!aSubType.empty())
624           aNbSubItems = aSubDoc->size(aSubType, true);
625         return aNbSubItems + aNbSubFolders;
626       } else {
627         // Check for composite object
628         ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
629         if (aCompFeature)
630           return aCompFeature->numberOfSubs(true);
631         ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
632         if (aCompRes)
633           return aCompRes->numberOfSubs(true);
634         ModelAPI_ResultField* aFieldRes = dynamic_cast<ModelAPI_ResultField*>(aObj);
635         if (aFieldRes)
636           return aFieldRes->stepsSize();
637         ModelAPI_Folder* aFolder = dynamic_cast<ModelAPI_Folder*>(aObj);
638         if (aFolder)
639           return getNumberOfFolderItems(aFolder);
640       }
641     }
642   }
643   return 0;
644 }
645
646 //******************************************************
647 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
648 {
649   return 3;
650 }
651
652 //******************************************************
653 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
654 {
655   SessionPtr aSession = ModelAPI_Session::get();
656   DocumentPtr aRootDoc = aSession->moduleDocument();
657   int aNbFolders = foldersCount();
658
659   QModelIndex aIndex;
660
661   if (!theParent.isValid()) {
662     if (theRow < aNbFolders) // Return first level folder index
663       return createIndex(theRow, theColumn, (void*)Q_NULLPTR);
664     else { // return object under root index
665       std::string aType = myXMLReader->rootType();
666       int aObjId = theRow - aNbFolders;
667       if (aObjId < aRootDoc->size(aType, true)) {
668         ObjectPtr aObj = aRootDoc->object(aType, aObjId, true);
669         aIndex = objectIndex(aObj, theColumn);
670       }
671     }
672   } else {
673     quintptr aId = theParent.internalId();
674     int aParentPos = theParent.row();
675     if (aId == 0) { // return object index inside of first level of folders
676       std::string aType = myXMLReader->rootFolderType(aParentPos);
677       if (theRow < aRootDoc->size(aType)) {
678         ObjectPtr aObj = aRootDoc->object(aType, theRow, true);
679         aIndex = objectIndex(aObj, theColumn);
680       }
681     } else {
682       // It is an object which could have children
683       ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
684       if (aDoc) {
685         // It is a folder of sub-document
686         int aParentRow = aParentPos;
687         QIntList aMissedIdx = missedFolderIndexes(aDoc);
688         while (aMissedIdx.contains(aParentRow))
689           aParentRow++;
690         if (aParentRow < myXMLReader->subFoldersNumber()) {
691           std::string aType = myXMLReader->subFolderType(aParentRow);
692           if (theRow < aDoc->size(aType)) {
693             ObjectPtr aObj = aDoc->object(aType, theRow);
694             aIndex = objectIndex(aObj, theColumn);
695           }
696         }
697       } else {
698         ModelAPI_Object* aParentObj =
699           dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theParent.internalPointer());
700
701         // Check for Part feature
702         ResultPartPtr aPartRes = getPartResult(aParentObj);
703         if (aPartRes.get()) {
704           DocumentPtr aSubDoc = aPartRes->partDoc();
705           int aNbSubFolders = foldersCount(aSubDoc.get());
706           if (theRow < aNbSubFolders) { // Create a Folder of sub-document
707             aIndex = createIndex(theRow, theColumn, aSubDoc.get());
708           } else {
709             // this is an object under sub document root
710             std::string aType = myXMLReader->subType();
711             ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders, true);
712             aIndex = objectIndex(aObj, theColumn);
713           }
714         } else {
715           // Check for composite object
716           ModelAPI_CompositeFeature* aCompFeature =
717             dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
718           if (aCompFeature) {
719             aIndex = objectIndex(aCompFeature->subFeature(theRow), theColumn);
720           } else {
721             ModelAPI_ResultCompSolid* aCompRes =
722               dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
723             if (aCompRes)
724               aIndex = objectIndex(aCompRes->subResult(theRow), theColumn);
725             else {
726               ModelAPI_ResultField* aFieldRes =
727                 dynamic_cast<ModelAPI_ResultField*>(aParentObj);
728               if (aFieldRes) {
729                 aIndex = createIndex(theRow, theColumn, aFieldRes->step(theRow));
730               } else {
731                 ModelAPI_Folder* aFolder = dynamic_cast<ModelAPI_Folder*>(aParentObj);
732                 ObjectPtr aObj = getObjectInFolder(aFolder, theRow);
733                 if (aObj.get())
734                   aIndex = objectIndex(aObj, theColumn);
735               }
736             }
737           }
738         }
739       }
740     }
741   }
742   return aIndex;
743 }
744
745 //******************************************************
746 static QModelIndex MYLastDeleted;
747 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
748 {
749   if (!theIndex.isValid())
750     return QModelIndex();
751   // To avoid additional request about index which was already deleted
752   if (theIndex == MYLastDeleted)
753     return QModelIndex();
754
755   SessionPtr aSession = ModelAPI_Session::get();
756   quintptr aId = theIndex.internalId();
757   if (aId != 0) { // The object is not a root folder
758     ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
759     if (aDoc) {
760       // It is a folder of sub-document
761       return findDocumentRootIndex(aDoc);
762     }
763     ObjectPtr aObj = object(theIndex);
764     if (!aObj.get()) {
765       // It can be a step of a field
766       ModelAPI_ResultField::ModelAPI_FieldStep* aStep = 0;
767       try {
768         aStep = dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>
769                 ((ModelAPI_Entity*)theIndex.internalPointer());
770       } catch(...) {}
771
772       if (aStep) {
773         ModelAPI_ResultField* aField = aStep->field();
774         DocumentPtr aDoc = aSession->activeDocument();
775         ObjectPtr aFld;
776         for(int i = 0; i < aDoc->size(ModelAPI_ResultField::group()); i++) {
777           aFld = aDoc->object(ModelAPI_ResultField::group(), i);
778           if (aFld.get() == aField)
779             return objectIndex(aFld);
780         }
781       }
782       // To avoid additional request about index which was already deleted
783       // If deleted it causes a crash on delete object from Part
784       MYLastDeleted = theIndex;
785       return QModelIndex();
786     }
787     // Check is it object a sub-object of a complex object
788     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
789     if (aFeature.get()) {
790       CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
791       if (aCompFea.get()) {
792         return objectIndex(aCompFea);
793       }
794       DocumentPtr aDoc = aFeature->document();
795       int aRow;
796       FolderPtr aFolder = aDoc->findContainingFolder(aFeature, aRow);
797       if (aFolder.get())
798         return objectIndex(aFolder);
799     }
800     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
801     if (aResult.get()) {
802       ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
803       if (aCompRes.get()) {
804         return objectIndex(aCompRes);
805       }
806     }
807     // Use as ordinary object
808     std::string aType = aObj->groupName();
809     DocumentPtr aRootDoc = aSession->moduleDocument();
810     DocumentPtr aSubDoc = aObj->document();
811     if (aSubDoc == aRootDoc) {
812       if ((aType == myXMLReader->rootType()) || (aType == ModelAPI_Folder::group()))
813         return QModelIndex();
814       else {
815         // return first level of folder index
816         int aFolderId = myXMLReader->rootFolderId(aType);
817         // Items in a one row must have the same parent
818         return createIndex(aFolderId, 1, (void*)Q_NULLPTR);
819       }
820     } else {
821       if ((aType == myXMLReader->subType()) || (aType == ModelAPI_Folder::group()))
822         return findDocumentRootIndex(aSubDoc.get());
823       else {
824         // return first level of folder index
825         int aFolderId = folderId(aType, aSubDoc.get());
826         // Items in a one row must have the same parent
827         return createIndex(aFolderId, 1, aSubDoc.get());
828       }
829     }
830   }
831   return QModelIndex();
832 }
833
834 //******************************************************
835 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
836 {
837   return rowCount(theParent) > 0;
838 }
839
840 //******************************************************
841 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
842 {
843   beginInsertRows(theParent, theRow, theRow + theCount - 1);
844   endInsertRows();
845
846   return true;
847 }
848
849 //******************************************************
850 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
851 {
852   beginRemoveRows(theParent, theRow, theRow + theCount - 1);
853   endRemoveRows();
854   return true;
855 }
856
857 //******************************************************
858 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
859 {
860   quintptr aIt = theIndex.internalId();
861   ModelAPI_Object* aObj = 0;
862   ModelAPI_Document* aDoc = 0;
863   SessionPtr aSession = ModelAPI_Session::get();
864   DocumentPtr aActiveDoc = aSession->activeDocument();
865
866   Qt::ItemFlags aNullFlag;
867   Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
868   Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
869
870
871   if (aIt == 0) {
872     // Folders under root
873     DocumentPtr aRootDoc = aSession->moduleDocument();
874     if (aRootDoc != aActiveDoc)
875       return aDefaultFlag;
876   } else {
877     aDoc = getSubDocument(theIndex.internalPointer());
878     if (!aDoc)
879       aObj = dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
880   }
881
882   if (aObj) {
883     // An object
884     if (aObj->isDisabled())
885       return theIndex.column() == 2? Qt::ItemIsSelectable : aNullFlag;
886
887     if (aSession->moduleDocument() != aObj->document())
888       if (aActiveDoc != aObj->document())
889         return theIndex.column() == 2? Qt::ItemIsSelectable : aNullFlag;
890
891     bool isCompositeSub = false;
892     // An object which is sub-object of a composite object can not be accessible in column 2
893     if (theIndex.column() == 2) {
894       ObjectPtr aObjPtr = aObj->data()->owner();
895       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
896       if (aFeature.get()) {
897         CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
898         if (aCompFea.get())
899           isCompositeSub = true;
900       } else {
901         ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
902         if (aResult.get()) {
903           ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
904           if (aCompRes.get())
905             isCompositeSub = true;
906         }
907       }
908     }
909     if (isCompositeSub)
910       return Qt::ItemIsSelectable;
911
912     if (aObj->document() != aActiveDoc) {
913       // The object could be a root of sub-tree
914       ResultPartPtr aPartRes = getPartResult(aObj);
915       if (aPartRes.get()) {
916         if (aPartRes->partDoc() == aActiveDoc)
917           return aEditingFlag;
918       }
919       return aDefaultFlag;
920     }
921   } else if (aDoc) {
922     // A folder under sub-document
923     if (aActiveDoc.get() != aDoc)
924       return aNullFlag;
925   }
926   return aEditingFlag;
927 }
928
929 //******************************************************
930 QModelIndex
931   XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc, int aColumn) const
932 {
933   SessionPtr aSession = ModelAPI_Session::get();
934   DocumentPtr aRootDoc = aSession->moduleDocument();
935   if (myXMLReader->isAttachToResult()) { // If document is attached to result
936     int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
937     ObjectPtr aObj;
938     ResultPartPtr aPartRes;
939     for (int i = 0; i < aNb; i++) {
940       aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
941       aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
942       if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
943         int aRow = i;
944         if (myXMLReader->rootType() == ModelAPI_Feature::group()) {
945           aRow += foldersCount();
946         }
947         return createIndex(aRow, aColumn, aObj.get());
948       }
949     }
950   } else { // If document is attached to feature
951     int aNb = aRootDoc->size(ModelAPI_Feature::group(), true);
952     ObjectPtr aObj;
953     ResultPartPtr aPartRes;
954     for (int i = 0; i < aNb; i++) {
955       aObj = aRootDoc->object(ModelAPI_Feature::group(), i, true);
956       aPartRes = getPartResult(aObj.get());
957       if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
958         int aRow = i;
959         if (myXMLReader->rootType() == ModelAPI_Feature::group())
960           aRow += foldersCount();
961         return createIndex(aRow, aColumn, aObj.get());
962       }
963     }
964   }
965   return QModelIndex();
966 }
967
968 //******************************************************
969 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc, int theColumn) const
970 {
971   SessionPtr aSession = ModelAPI_Session::get();
972   DocumentPtr aRootDoc = aSession->moduleDocument();
973   if (theDoc == aRootDoc)
974     return QModelIndex();
975   else
976     return findDocumentRootIndex(theDoc.get(), theColumn);
977 }
978
979 //******************************************************
980 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
981 {
982   int aNb = 0;
983   SessionPtr aSession = ModelAPI_Session::get();
984   DocumentPtr aRootDoc = aSession->moduleDocument();
985   if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
986     for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
987       if (myXMLReader->rootShowEmpty(i))
988         aNb++;
989       else {
990         if (aRootDoc->size(myXMLReader->rootFolderType(i)) > 0)
991           aNb++;
992       }
993     }
994   } else {
995     for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
996       if (myXMLReader->subShowEmpty(i))
997         aNb++;
998       else {
999         if (theDoc->size(myXMLReader->subFolderType(i)) > 0)
1000           aNb++;
1001       }
1002     }
1003   }
1004   return aNb;
1005 }
1006
1007
1008 //******************************************************
1009 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
1010 {
1011   QIntList aList;
1012   SessionPtr aSession = ModelAPI_Session::get();
1013   DocumentPtr aRootDoc = aSession->moduleDocument();
1014   if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
1015     for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
1016       if (!myXMLReader->rootShowEmpty(i)) {
1017         if (aRootDoc->size(myXMLReader->rootFolderType(i)) == 0)
1018           aList.append(i);
1019       }
1020     }
1021   } else {
1022     for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
1023       if (!myXMLReader->subShowEmpty(i)) {
1024         if (theDoc->size(myXMLReader->subFolderType(i)) == 0)
1025           aList.append(i);
1026       }
1027     }
1028   }
1029   return aList;
1030 }
1031
1032
1033 //******************************************************
1034 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
1035 {
1036   QStringList aResult;
1037   if (fromRoot) {
1038     for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
1039       if (!myXMLReader->rootShowEmpty(i))
1040         aResult << myXMLReader->rootFolderType(i).c_str();
1041     }
1042   } else {
1043     for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
1044       if (!myXMLReader->subShowEmpty(i))
1045         aResult << myXMLReader->subFolderType(i).c_str();
1046     }
1047   }
1048   return aResult;
1049 }
1050
1051 //******************************************************
1052 QModelIndex XGUI_DataModel::lastHistoryIndex() const
1053 {
1054   SessionPtr aSession = ModelAPI_Session::get();
1055   DocumentPtr aCurDoc = aSession->activeDocument();
1056   FeaturePtr aFeature = aCurDoc->currentFeature(true);
1057   if (aFeature.get()) {
1058     QModelIndex aInd = objectIndex(aFeature);
1059     return createIndex(aInd.row(), 2, aInd.internalPointer());
1060   } else {
1061     if (aCurDoc == aSession->moduleDocument())
1062       return createIndex(foldersCount() - 1, 2, -1);
1063     else
1064       return createIndex(foldersCount(aCurDoc.get()) - 1, 2, aCurDoc.get());
1065   }
1066 }
1067
1068 //******************************************************
1069 bool XGUI_DataModel::hasHiddenState(const QModelIndex& theIndex)
1070 {
1071   return getVisibilityState(theIndex) == Hidden;
1072 }
1073
1074 //******************************************************
1075 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc) const
1076 {
1077   SessionPtr aSession = ModelAPI_Session::get();
1078   ModelAPI_Document* aDoc = theDoc;
1079   if (aDoc == 0)
1080     aDoc = aSession->moduleDocument().get();
1081
1082   bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
1083
1084   int aRes = -1;
1085   if (aUseSubDoc) {
1086     int aId = myXMLReader->subFolderId(theType);
1087     aRes = aId;
1088     for (int i = 0; i < aId; i++) {
1089       if (!myXMLReader->subShowEmpty(i)) {
1090         if (aDoc->size(myXMLReader->subFolderType(i)) == 0)
1091           aRes--;
1092       }
1093     }
1094   } else {
1095     int aId = myXMLReader->rootFolderId(theType);
1096     aRes = aId;
1097     for (int i = 0; i < aId; i++) {
1098       if (!myXMLReader->rootShowEmpty(i)) {
1099         if (aDoc->size(myXMLReader->rootFolderType(i)) == 0)
1100           aRes--;
1101       }
1102     }
1103   }
1104   return aRes;
1105 }
1106
1107 //******************************************************
1108 void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex& theParent)
1109 {
1110   if (theCount > 0) {
1111     removeRows(theRow, theCount, theParent);
1112     insertRows(theRow, theCount, theParent);
1113   }
1114 }
1115
1116 //******************************************************
1117 //bool XGUI_DataModel::blockEventsProcessing(const bool theState)
1118 //{
1119 //  bool aPreviousState = myIsEventsProcessingBlocked;
1120 //  myIsEventsProcessingBlocked = theState;
1121 //  return aPreviousState;
1122 //}
1123
1124 //******************************************************
1125 XGUI_DataModel::VisibilityState
1126   XGUI_DataModel::getVisibilityState(const QModelIndex& theIndex) const
1127 {
1128   Qt::ItemFlags aFlags = theIndex.flags();
1129   if (aFlags == Qt::ItemFlags())
1130     return NoneState;
1131
1132   ObjectPtr aObj = object(theIndex);
1133   if (aObj.get()) {
1134     if (aObj->groupName() == ModelAPI_ResultParameter::group())
1135       return NoneState;
1136     ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
1137     if (aResObj.get()) {
1138       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
1139       ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResObj);
1140       if (aCompRes.get()) {
1141         VisibilityState aState = aCompRes->numberOfSubs(true) == 0 ?
1142           (aDisplayer->isVisible(aCompRes)? Visible : Hidden) : NoneState;
1143         for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
1144           ResultPtr aSubRes = aCompRes->subResult(i, true);
1145           VisibilityState aS = aDisplayer->isVisible(aSubRes)? Visible : Hidden;
1146           if (aState == NoneState)
1147             aState = aS;
1148           else if (aState != aS) {
1149             aState = SemiVisible;
1150             break;
1151           }
1152         }
1153         return aState;
1154       } else {
1155         if (aDisplayer->isVisible(aResObj))
1156           return Visible;
1157         else
1158           return Hidden;
1159       }
1160     }
1161   }
1162   return NoneState;
1163 }
1164
1165
1166 int XGUI_DataModel::getNumberOfFolderItems(const ModelAPI_Folder* theFolder) const
1167 {
1168   DocumentPtr aDoc = theFolder->document();
1169
1170   FeaturePtr aFirstFeatureInFolder;
1171   AttributeReferencePtr aFirstFeatAttr =
1172       theFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
1173   if (aFirstFeatAttr)
1174     aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value());
1175   if (!aFirstFeatureInFolder.get())
1176     return 0;
1177
1178   FeaturePtr aLastFeatureInFolder;
1179   AttributeReferencePtr aLastFeatAttr =
1180       theFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1181   if (aLastFeatAttr)
1182     aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
1183   if (!aLastFeatureInFolder.get())
1184     return 0;
1185
1186   int aFirst = aDoc->index(aFirstFeatureInFolder);
1187   int aLast = aDoc->index(aLastFeatureInFolder);
1188   return aLast - aFirst + 1;
1189 }
1190
1191 ObjectPtr XGUI_DataModel::getObjectInFolder(const ModelAPI_Folder* theFolder, int theId) const
1192 {
1193   DocumentPtr aDoc = theFolder->document();
1194
1195   FeaturePtr aFirstFeatureInFolder;
1196   AttributeReferencePtr aFirstFeatAttr =
1197       theFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
1198   if (aFirstFeatAttr)
1199     aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value());
1200   if (!aFirstFeatureInFolder.get())
1201     return ObjectPtr();
1202
1203   int aFirst = aDoc->index(aFirstFeatureInFolder);
1204   return aDoc->object(ModelAPI_Feature::group(), aFirst + theId);
1205 }