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