Salome HOME
Merge branch 'Dev_FolderFeature' of salome:modules/shaper into Dev_FolderFeature
[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 =
380     dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
381   if (!aObj)
382     return ObjectPtr();
383   if (getSubDocument(aObj)) // the selected index is a folder of sub-document
384     return ObjectPtr();
385
386   return aObj->data()->owner();
387 }
388
389 //******************************************************
390 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject, int theColumn) const
391 {
392   std::string aType = theObject->groupName();
393   DocumentPtr aDoc = theObject->document();
394   int aRow = aDoc->index(theObject, true);
395   if (aRow == -1) {
396     // it could be a part of complex object
397     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
398     if (aFeature.get()) {
399       CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
400       if (aCompFea.get()) {
401         for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
402           if (aCompFea->subFeature(i, true) == theObject) {
403             aRow = i;
404             break;
405           }
406         }
407       }
408       int aFRow = -1;
409       FolderPtr aFolder = aDoc->findContainingFolder(aFeature, aFRow);
410       if (aFolder.get())
411         aRow = aFRow;
412     } else {
413       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
414       if (aResult.get()) {
415         ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
416         if (aCompRes.get()) {
417           aRow = ModelAPI_Tools::compSolidIndex(aResult);
418         }
419       }
420     }
421     if (aRow == -1)
422       return QModelIndex();
423     else
424       return createIndex(aRow, theColumn, theObject.get());
425   }
426   SessionPtr aSession = ModelAPI_Session::get();
427   DocumentPtr aRootDoc = aSession->moduleDocument();
428   if (aDoc == aRootDoc &&
429     ((myXMLReader->rootType() == aType) || (aType == ModelAPI_Folder::group()))) {
430     // The object from root document
431     aRow += foldersCount();
432   } else if ((myXMLReader->subType() == aType) || (aType == ModelAPI_Folder::group())) {
433     // The object from sub document
434     aRow += foldersCount(aDoc.get());
435   }
436   return createIndex(aRow, theColumn, theObject.get());
437 }
438
439 //******************************************************
440 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
441 {
442   SessionPtr aSession = ModelAPI_Session::get();
443   DocumentPtr aRootDoc = aSession->moduleDocument();
444   int aNbFolders = foldersCount();
445   int theIndexRow = theIndex.row();
446
447   if (theRole == Qt::DecorationRole) {
448     if (theIndex == lastHistoryIndex())
449       return QIcon(":pictures/arrow.png");
450     else if (theIndex.column() == 0) {
451       VisibilityState aState = getVisibilityState(theIndex);
452       switch (aState) {
453       case NoneState:
454         return QIcon();
455       case Visible:
456         return QIcon(":pictures/eyeopen.png");
457       case SemiVisible:
458         return QIcon(":pictures/eyemiclosed.png");
459       case Hidden:
460         return QIcon(":pictures/eyeclosed.png");
461       }
462     }
463   }
464
465   //if (theIndex.column() == 1)
466   if (theIndex.column() != 1)
467     return QVariant();
468
469   quintptr aParentId = theIndex.internalId();
470   if (aParentId == 0) { // root folders
471     switch (theRole) {
472       case Qt::DisplayRole:
473         return QString(myXMLReader->rootFolderName(theIndexRow).c_str()) +
474           QString(" (%1)").arg(rowCount(theIndex));
475       case Qt::DecorationRole:
476         return QIcon(myXMLReader->rootFolderIcon(theIndexRow).c_str());
477       case Qt::ForegroundRole:
478         {
479           Qt::ItemFlags aFlags = theIndex.flags();
480           if (aFlags == Qt::ItemFlags())
481             return QBrush(DISABLED_COLOR);
482           if (!aFlags.testFlag(Qt::ItemIsEditable))
483             return QBrush(SELECTABLE_COLOR);
484         }
485         return ACTIVE_COLOR;
486     }
487   } else { // an object or sub-document
488     if (theRole == Qt::ForegroundRole) {
489       Qt::ItemFlags aFlags = theIndex.flags();
490       if (aFlags == Qt::ItemFlags())
491         return QBrush(DISABLED_COLOR);
492       if (!aFlags.testFlag(Qt::ItemIsEditable))
493         return QBrush(SELECTABLE_COLOR);
494       return ACTIVE_COLOR;
495     }
496
497     ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
498     if (aSubDoc) { // this is a folder of sub document
499       QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
500       int aRow = theIndexRow;
501       while (aMissedIdx.contains(aRow))
502         aRow++;
503       if (aRow < myXMLReader->subFoldersNumber()) {
504         switch (theRole) {
505           case Qt::DisplayRole:
506             return QString(myXMLReader->subFolderName(aRow).c_str()) +
507               QString(" (%1)").arg(rowCount(theIndex));
508           case Qt::DecorationRole:
509             return QIcon(myXMLReader->subFolderIcon(aRow).c_str());
510         }
511       }
512     } else {
513       ObjectPtr aObj = object(theIndex);
514       if (aObj) {
515         switch (theRole) {
516         case Qt::DisplayRole:
517           {
518             if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
519               ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObj);
520               AttributeDoublePtr aValueAttribute =
521                 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
522               QString aVal = QString::number(aValueAttribute->value());
523               QString aTitle = QString(aObj->data()->name().c_str());
524               return aTitle + " = " + aVal;
525             }
526             QString aSuffix;
527             if (aObj->groupName() == myXMLReader->subType()) {
528               ResultPartPtr aPartRes = getPartResult(aObj.get());
529               if (aPartRes.get()) {
530                 if (aPartRes->partDoc().get() == NULL)
531                   aSuffix = " (Not loaded)";
532               }
533             }
534             return aObj->data()->name().c_str() + aSuffix;
535           }
536         case Qt::DecorationRole:
537           {
538             if (aObj->groupName() == ModelAPI_Folder::group())
539               return QIcon(":pictures/features_folder.png");
540             else
541               return ModuleBase_IconFactory::get()->getIcon(aObj);
542           }
543         }
544       } else {
545         switch (theRole) {
546         case Qt::DisplayRole:
547           {
548             ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
549               dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>
550               ((ModelAPI_Entity*)theIndex.internalPointer());
551             if (aStep) {
552               return "Step " + QString::number(aStep->id() + 1) + " " +
553                 aStep->field()->textLine(aStep->id()).c_str();
554             }
555           }
556           break;
557         }
558       }
559     }
560   }
561   return QVariant();
562 }
563
564 //******************************************************
565 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
566 {
567   return QVariant();
568 }
569
570 //******************************************************
571 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
572 {
573   SessionPtr aSession = ModelAPI_Session::get();
574   if (!aSession->hasModuleDocument())
575     return 0;
576   DocumentPtr aRootDoc = aSession->moduleDocument();
577
578   if (!theParent.isValid()) {
579     // Return number of items in root
580     int aNbFolders = foldersCount();
581     int aNbItems = 0;
582     std::string aType = myXMLReader->rootType();
583     if (!aType.empty())
584       aNbItems = aRootDoc->size(aType, true);
585     return aNbFolders + aNbItems;
586   }
587
588   quintptr aId = theParent.internalId();
589   if (aId == 0) {
590     // this is a folder under root
591     int aParentPos = theParent.row();
592     std::string aType = myXMLReader->rootFolderType(aParentPos);
593     return aRootDoc->size(aType);
594   } else {
595     // It is an object which could have children
596     ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
597     if (aDoc) {
598       // a folder of sub-document
599       QIntList aMissedIdx = missedFolderIndexes(aDoc);
600       int aRow = theParent.row();
601       while (aMissedIdx.contains(aRow))
602         aRow++;
603       if (aRow < myXMLReader->subFoldersNumber()) {
604         std::string aType = myXMLReader->subFolderType(aRow);
605         return aDoc->size(aType);
606       }
607     } else {
608       ModelAPI_Object* aObj =
609         dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theParent.internalPointer());
610       // Check for Part feature
611       ResultPartPtr aPartRes = getPartResult(aObj);
612       if (aPartRes.get()) {
613         DocumentPtr aSubDoc = aPartRes->partDoc();
614         if (!aSubDoc.get())
615           return 0;
616
617         int aNbSubFolders = foldersCount(aSubDoc.get());
618         int aNbSubItems = 0;
619         std::string aSubType = myXMLReader->subType();
620         if (!aSubType.empty())
621           aNbSubItems = aSubDoc->size(aSubType, true);
622         return aNbSubItems + aNbSubFolders;
623       } else {
624         // Check for composite object
625         ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
626         if (aCompFeature)
627           return aCompFeature->numberOfSubs(true);
628         ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
629         if (aCompRes)
630           return aCompRes->numberOfSubs(true);
631         ModelAPI_ResultField* aFieldRes = dynamic_cast<ModelAPI_ResultField*>(aObj);
632         if (aFieldRes)
633           return aFieldRes->stepsSize();
634         ModelAPI_Folder* aFolder = dynamic_cast<ModelAPI_Folder*>(aObj);
635         if (aFolder)
636           return getNumberOfFolderItems(aFolder);
637       }
638     }
639   }
640   return 0;
641 }
642
643 //******************************************************
644 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
645 {
646   return 3;
647 }
648
649 //******************************************************
650 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
651 {
652   SessionPtr aSession = ModelAPI_Session::get();
653   DocumentPtr aRootDoc = aSession->moduleDocument();
654   int aNbFolders = foldersCount();
655
656   QModelIndex aIndex;
657
658   if (!theParent.isValid()) {
659     if (theRow < aNbFolders) // Return first level folder index
660       return createIndex(theRow, theColumn, (void*)Q_NULLPTR);
661     else { // return object under root index
662       std::string aType = myXMLReader->rootType();
663       int aObjId = theRow - aNbFolders;
664       if (aObjId < aRootDoc->size(aType, true)) {
665         ObjectPtr aObj = aRootDoc->object(aType, aObjId, true);
666         aIndex = objectIndex(aObj, theColumn);
667       }
668     }
669   } else {
670     quintptr aId = theParent.internalId();
671     int aParentPos = theParent.row();
672     if (aId == 0) { // return object index inside of first level of folders
673       std::string aType = myXMLReader->rootFolderType(aParentPos);
674       if (theRow < aRootDoc->size(aType)) {
675         ObjectPtr aObj = aRootDoc->object(aType, theRow, true);
676         aIndex = objectIndex(aObj, theColumn);
677       }
678     } else {
679       // It is an object which could have children
680       ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
681       if (aDoc) {
682         // It is a folder of sub-document
683         int aParentRow = aParentPos;
684         QIntList aMissedIdx = missedFolderIndexes(aDoc);
685         while (aMissedIdx.contains(aParentRow))
686           aParentRow++;
687         if (aParentRow < myXMLReader->subFoldersNumber()) {
688           std::string aType = myXMLReader->subFolderType(aParentRow);
689           if (theRow < aDoc->size(aType)) {
690             ObjectPtr aObj = aDoc->object(aType, theRow);
691             aIndex = objectIndex(aObj, theColumn);
692           }
693         }
694       } else {
695         ModelAPI_Object* aParentObj =
696           dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theParent.internalPointer());
697
698         // Check for Part feature
699         ResultPartPtr aPartRes = getPartResult(aParentObj);
700         if (aPartRes.get()) {
701           DocumentPtr aSubDoc = aPartRes->partDoc();
702           int aNbSubFolders = foldersCount(aSubDoc.get());
703           if (theRow < aNbSubFolders) { // Create a Folder of sub-document
704             aIndex = createIndex(theRow, theColumn, aSubDoc.get());
705           } else {
706             // this is an object under sub document root
707             std::string aType = myXMLReader->subType();
708             ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders, true);
709             aIndex = objectIndex(aObj, theColumn);
710           }
711         } else {
712           // Check for composite object
713           ModelAPI_CompositeFeature* aCompFeature =
714             dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
715           if (aCompFeature) {
716             aIndex = objectIndex(aCompFeature->subFeature(theRow), theColumn);
717           } else {
718             ModelAPI_ResultCompSolid* aCompRes =
719               dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
720             if (aCompRes)
721               aIndex = objectIndex(aCompRes->subResult(theRow), theColumn);
722             else {
723               ModelAPI_ResultField* aFieldRes =
724                 dynamic_cast<ModelAPI_ResultField*>(aParentObj);
725               if (aFieldRes) {
726                 aIndex = createIndex(theRow, theColumn, aFieldRes->step(theRow));
727               } else {
728                 ModelAPI_Folder* aFolder = dynamic_cast<ModelAPI_Folder*>(aParentObj);
729                 ObjectPtr aObj = getObjectInFolder(aFolder, theRow);
730                 if (aObj.get())
731                   aIndex = objectIndex(aObj, theColumn);
732               }
733             }
734           }
735         }
736       }
737     }
738   }
739   return aIndex;
740 }
741
742 //******************************************************
743 static QModelIndex MYLastDeleted;
744 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
745 {
746   if (!theIndex.isValid())
747     return QModelIndex();
748   // To avoid additional request about index which was already deleted
749   if (theIndex == MYLastDeleted)
750     return QModelIndex();
751
752   SessionPtr aSession = ModelAPI_Session::get();
753   quintptr aId = theIndex.internalId();
754   if (aId != 0) { // The object is not a root folder
755     ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
756     if (aDoc) {
757       // It is a folder of sub-document
758       return findDocumentRootIndex(aDoc);
759     }
760     ObjectPtr aObj = object(theIndex);
761     if (!aObj.get()) {
762       // It can be a step of a field
763       ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
764         dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>
765         ((ModelAPI_Entity*)theIndex.internalPointer());
766       if (aStep) {
767         ModelAPI_ResultField* aField = aStep->field();
768         DocumentPtr aDoc = aSession->activeDocument();
769         ObjectPtr aFld;
770         for(int i = 0; i < aDoc->size(ModelAPI_ResultField::group()); i++) {
771           aFld = aDoc->object(ModelAPI_ResultField::group(), i);
772           if (aFld.get() == aField)
773             return objectIndex(aFld);
774         }
775       }
776       // To avoid additional request about index which was already deleted
777       // If deleted it causes a crash on delete object from Part
778       MYLastDeleted = theIndex;
779       return QModelIndex();
780     }
781     // Check is it object a sub-object of a complex object
782     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
783     if (aFeature.get()) {
784       CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
785       if (aCompFea.get()) {
786         return objectIndex(aCompFea);
787       }
788       DocumentPtr aDoc = aFeature->document();
789       int aRow;
790       FolderPtr aFolder = aDoc->findContainingFolder(aFeature, aRow);
791       if (aFolder.get())
792         return objectIndex(aFolder);
793     }
794     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
795     if (aResult.get()) {
796       ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
797       if (aCompRes.get()) {
798         return objectIndex(aCompRes);
799       }
800     }
801     // Use as ordinary object
802     std::string aType = aObj->groupName();
803     DocumentPtr aRootDoc = aSession->moduleDocument();
804     DocumentPtr aSubDoc = aObj->document();
805     if (aSubDoc == aRootDoc) {
806       if ((aType == myXMLReader->rootType()) || (aType == ModelAPI_Folder::group()))
807         return QModelIndex();
808       else {
809         // return first level of folder index
810         int aFolderId = myXMLReader->rootFolderId(aType);
811         // Items in a one row must have the same parent
812         return createIndex(aFolderId, 1, (void*)Q_NULLPTR);
813       }
814     } else {
815       if ((aType == myXMLReader->subType()) || (aType == ModelAPI_Folder::group()))
816         return findDocumentRootIndex(aSubDoc.get());
817       else {
818         // return first level of folder index
819         int aFolderId = folderId(aType, aSubDoc.get());
820         // Items in a one row must have the same parent
821         return createIndex(aFolderId, 1, aSubDoc.get());
822       }
823     }
824   }
825   return QModelIndex();
826 }
827
828 //******************************************************
829 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
830 {
831   return rowCount(theParent) > 0;
832 }
833
834 //******************************************************
835 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
836 {
837   beginInsertRows(theParent, theRow, theRow + theCount - 1);
838   endInsertRows();
839
840   return true;
841 }
842
843 //******************************************************
844 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
845 {
846   beginRemoveRows(theParent, theRow, theRow + theCount - 1);
847   endRemoveRows();
848   return true;
849 }
850
851 //******************************************************
852 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
853 {
854   quintptr aIt = theIndex.internalId();
855   ModelAPI_Object* aObj = 0;
856   ModelAPI_Document* aDoc = 0;
857   SessionPtr aSession = ModelAPI_Session::get();
858   DocumentPtr aActiveDoc = aSession->activeDocument();
859
860   Qt::ItemFlags aNullFlag;
861   Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
862   Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
863
864
865   if (aIt == 0) {
866     // Folders under root
867     DocumentPtr aRootDoc = aSession->moduleDocument();
868     if (aRootDoc != aActiveDoc)
869       return aDefaultFlag;
870   } else {
871     aDoc = getSubDocument(theIndex.internalPointer());
872     if (!aDoc)
873       aObj = dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
874   }
875
876   if (aObj) {
877     // An object
878     if (aObj->isDisabled())
879       return theIndex.column() == 2? Qt::ItemIsSelectable : aNullFlag;
880
881     if (aSession->moduleDocument() != aObj->document())
882       if (aActiveDoc != aObj->document())
883         return theIndex.column() == 2? Qt::ItemIsSelectable : aNullFlag;
884
885     bool isCompositeSub = false;
886     // An object which is sub-object of a composite object can not be accessible in column 2
887     if (theIndex.column() == 2) {
888       ObjectPtr aObjPtr = aObj->data()->owner();
889       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
890       if (aFeature.get()) {
891         CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
892         if (aCompFea.get())
893           isCompositeSub = true;
894       } else {
895         ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
896         if (aResult.get()) {
897           ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
898           if (aCompRes.get())
899             isCompositeSub = true;
900         }
901       }
902     }
903     if (isCompositeSub)
904       return Qt::ItemIsSelectable;
905
906     if (aObj->document() != aActiveDoc) {
907       // The object could be a root of sub-tree
908       ResultPartPtr aPartRes = getPartResult(aObj);
909       if (aPartRes.get()) {
910         if (aPartRes->partDoc() == aActiveDoc)
911           return aEditingFlag;
912       }
913       return aDefaultFlag;
914     }
915   } else if (aDoc) {
916     // A folder under sub-document
917     if (aActiveDoc.get() != aDoc)
918       return aNullFlag;
919   }
920   return aEditingFlag;
921 }
922
923 //******************************************************
924 QModelIndex
925   XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc, int aColumn) const
926 {
927   SessionPtr aSession = ModelAPI_Session::get();
928   DocumentPtr aRootDoc = aSession->moduleDocument();
929   if (myXMLReader->isAttachToResult()) { // If document is attached to result
930     int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
931     ObjectPtr aObj;
932     ResultPartPtr aPartRes;
933     for (int i = 0; i < aNb; i++) {
934       aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
935       aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
936       if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
937         int aRow = i;
938         if (myXMLReader->rootType() == ModelAPI_Feature::group()) {
939           aRow += foldersCount();
940         }
941         return createIndex(aRow, aColumn, aObj.get());
942       }
943     }
944   } else { // If document is attached to feature
945     int aNb = aRootDoc->size(ModelAPI_Feature::group(), true);
946     ObjectPtr aObj;
947     ResultPartPtr aPartRes;
948     for (int i = 0; i < aNb; i++) {
949       aObj = aRootDoc->object(ModelAPI_Feature::group(), i, true);
950       aPartRes = getPartResult(aObj.get());
951       if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
952         int aRow = i;
953         if (myXMLReader->rootType() == ModelAPI_Feature::group())
954           aRow += foldersCount();
955         return createIndex(aRow, aColumn, aObj.get());
956       }
957     }
958   }
959   return QModelIndex();
960 }
961
962 //******************************************************
963 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc, int theColumn) const
964 {
965   SessionPtr aSession = ModelAPI_Session::get();
966   DocumentPtr aRootDoc = aSession->moduleDocument();
967   if (theDoc == aRootDoc)
968     return QModelIndex();
969   else
970     return findDocumentRootIndex(theDoc.get(), theColumn);
971 }
972
973 //******************************************************
974 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
975 {
976   int aNb = 0;
977   SessionPtr aSession = ModelAPI_Session::get();
978   DocumentPtr aRootDoc = aSession->moduleDocument();
979   if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
980     for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
981       if (myXMLReader->rootShowEmpty(i))
982         aNb++;
983       else {
984         if (aRootDoc->size(myXMLReader->rootFolderType(i)) > 0)
985           aNb++;
986       }
987     }
988   } else {
989     for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
990       if (myXMLReader->subShowEmpty(i))
991         aNb++;
992       else {
993         if (theDoc->size(myXMLReader->subFolderType(i)) > 0)
994           aNb++;
995       }
996     }
997   }
998   return aNb;
999 }
1000
1001
1002 //******************************************************
1003 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
1004 {
1005   QIntList aList;
1006   SessionPtr aSession = ModelAPI_Session::get();
1007   DocumentPtr aRootDoc = aSession->moduleDocument();
1008   if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
1009     for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
1010       if (!myXMLReader->rootShowEmpty(i)) {
1011         if (aRootDoc->size(myXMLReader->rootFolderType(i)) == 0)
1012           aList.append(i);
1013       }
1014     }
1015   } else {
1016     for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
1017       if (!myXMLReader->subShowEmpty(i)) {
1018         if (theDoc->size(myXMLReader->subFolderType(i)) == 0)
1019           aList.append(i);
1020       }
1021     }
1022   }
1023   return aList;
1024 }
1025
1026
1027 //******************************************************
1028 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
1029 {
1030   QStringList aResult;
1031   if (fromRoot) {
1032     for (int i = 0; i < myXMLReader->rootFoldersNumber(); i++) {
1033       if (!myXMLReader->rootShowEmpty(i))
1034         aResult << myXMLReader->rootFolderType(i).c_str();
1035     }
1036   } else {
1037     for (int i = 0; i < myXMLReader->subFoldersNumber(); i++) {
1038       if (!myXMLReader->subShowEmpty(i))
1039         aResult << myXMLReader->subFolderType(i).c_str();
1040     }
1041   }
1042   return aResult;
1043 }
1044
1045 //******************************************************
1046 QModelIndex XGUI_DataModel::lastHistoryIndex() const
1047 {
1048   SessionPtr aSession = ModelAPI_Session::get();
1049   DocumentPtr aCurDoc = aSession->activeDocument();
1050   FeaturePtr aFeature = aCurDoc->currentFeature(true);
1051   if (aFeature.get()) {
1052     QModelIndex aInd = objectIndex(aFeature);
1053     return createIndex(aInd.row(), 2, aInd.internalPointer());
1054   } else {
1055     if (aCurDoc == aSession->moduleDocument())
1056       return createIndex(foldersCount() - 1, 2, -1);
1057     else
1058       return createIndex(foldersCount(aCurDoc.get()) - 1, 2, aCurDoc.get());
1059   }
1060 }
1061
1062 //******************************************************
1063 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc) const
1064 {
1065   SessionPtr aSession = ModelAPI_Session::get();
1066   ModelAPI_Document* aDoc = theDoc;
1067   if (aDoc == 0)
1068     aDoc = aSession->moduleDocument().get();
1069
1070   bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
1071
1072   int aRes = -1;
1073   if (aUseSubDoc) {
1074     int aId = myXMLReader->subFolderId(theType);
1075     aRes = aId;
1076     for (int i = 0; i < aId; i++) {
1077       if (!myXMLReader->subShowEmpty(i)) {
1078         if (aDoc->size(myXMLReader->subFolderType(i)) == 0)
1079           aRes--;
1080       }
1081     }
1082   } else {
1083     int aId = myXMLReader->rootFolderId(theType);
1084     aRes = aId;
1085     for (int i = 0; i < aId; i++) {
1086       if (!myXMLReader->rootShowEmpty(i)) {
1087         if (aDoc->size(myXMLReader->rootFolderType(i)) == 0)
1088           aRes--;
1089       }
1090     }
1091   }
1092   return aRes;
1093 }
1094
1095 //******************************************************
1096 void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex& theParent)
1097 {
1098   if (theCount > 0) {
1099     removeRows(theRow, theCount, theParent);
1100     insertRows(theRow, theCount, theParent);
1101   }
1102 }
1103
1104 //******************************************************
1105 //bool XGUI_DataModel::blockEventsProcessing(const bool theState)
1106 //{
1107 //  bool aPreviousState = myIsEventsProcessingBlocked;
1108 //  myIsEventsProcessingBlocked = theState;
1109 //  return aPreviousState;
1110 //}
1111
1112 //******************************************************
1113 XGUI_DataModel::VisibilityState
1114   XGUI_DataModel::getVisibilityState(const QModelIndex& theIndex) const
1115 {
1116   Qt::ItemFlags aFlags = theIndex.flags();
1117   if (aFlags == Qt::ItemFlags())
1118     return NoneState;
1119
1120   ObjectPtr aObj = object(theIndex);
1121   if (aObj.get()) {
1122     ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
1123     if (aResObj.get()) {
1124       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
1125       ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResObj);
1126       if (aCompRes.get()) {
1127         VisibilityState aState = aCompRes->numberOfSubs(true) == 0 ?
1128           (aDisplayer->isVisible(aCompRes)? Visible : Hidden) : NoneState;
1129         for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
1130           ResultPtr aSubRes = aCompRes->subResult(i, true);
1131           VisibilityState aS = aDisplayer->isVisible(aSubRes)? Visible : Hidden;
1132           if (aState == NoneState)
1133             aState = aS;
1134           else if (aState != aS) {
1135             aState = SemiVisible;
1136             break;
1137           }
1138         }
1139         return aState;
1140       } else {
1141         if (aDisplayer->isVisible(aResObj))
1142           return Visible;
1143         else
1144           return Hidden;
1145       }
1146     }
1147   }
1148   return NoneState;
1149 }
1150
1151
1152 int XGUI_DataModel::getNumberOfFolderItems(const ModelAPI_Folder* theFolder) const
1153 {
1154   DocumentPtr aDoc = theFolder->document();
1155
1156   FeaturePtr aFirstFeatureInFolder;
1157   AttributeReferencePtr aFirstFeatAttr =
1158       theFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
1159   if (aFirstFeatAttr)
1160     aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value());
1161   if (!aFirstFeatureInFolder.get())
1162     return 0;
1163
1164   FeaturePtr aLastFeatureInFolder;
1165   AttributeReferencePtr aLastFeatAttr =
1166       theFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
1167   if (aLastFeatAttr)
1168     aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
1169   if (!aLastFeatureInFolder.get())
1170     return 0;
1171
1172   int aFirst = aDoc->index(aFirstFeatureInFolder);
1173   int aLast = aDoc->index(aLastFeatureInFolder);
1174   return aLast - aFirst + 1;
1175 }
1176
1177 ObjectPtr XGUI_DataModel::getObjectInFolder(const ModelAPI_Folder* theFolder, int theId) const
1178 {
1179   DocumentPtr aDoc = theFolder->document();
1180
1181   FeaturePtr aFirstFeatureInFolder;
1182   AttributeReferencePtr aFirstFeatAttr =
1183       theFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
1184   if (aFirstFeatAttr)
1185     aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value());
1186   if (!aFirstFeatureInFolder.get())
1187     return ObjectPtr();
1188
1189   int aFirst = aDoc->index(aFirstFeatureInFolder);
1190   return aDoc->object(ModelAPI_Feature::group(), aFirst + theId);
1191 }