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