Salome HOME
Issue #904 - Fatal error aftre delete sketch from dataset used in extrusion in part
[modules/shaper.git] / src / XGUI / XGUI_DataModel.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        ModuleBase_IDocumentDataModel.cpp
4 // Created:     28 Apr 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "XGUI_DataModel.h"
8
9 #include <ModuleBase_IconFactory.h>
10
11 #include <ModelAPI_Session.h>
12 #include <ModelAPI_Events.h>
13 #include <ModelAPI_ResultParameter.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_ResultPart.h>
16 #include <ModelAPI_Feature.h>
17 #include <ModelAPI_CompositeFeature.h>
18 #include <ModelAPI_ResultCompSolid.h>
19 #include <ModelAPI_Tools.h>
20
21 #include <Config_FeatureMessage.h>
22
23 #include <Events_Loop.h>
24 #include <Events_Error.h>
25
26 #include <QIcon>
27 #include <QBrush>
28
29 #define ACTIVE_COLOR QColor(0,72,140)
30 //#define PASSIVE_COLOR Qt::black
31
32 /// Returns ResultPart object if the given object is a Part feature
33 /// Otherwise returns NULL
34 ResultPartPtr getPartResult(ModelAPI_Object* theObj)
35 {
36   ModelAPI_Feature* aFeature = dynamic_cast<ModelAPI_Feature*>(theObj);
37   if (aFeature) {
38     ResultPtr aRes = aFeature->firstResult();
39     if (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group())) {
40       ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aRes);
41       // Use only original parts, not a placement results
42       if (aPartRes == aPartRes->original())
43       return aPartRes;
44     }
45   }
46   return ResultPartPtr();
47 }
48
49 /// Returns pointer on document if the given object is document object
50 ModelAPI_Document* getSubDocument(void* theObj)
51 {
52   ModelAPI_Document* aDoc = dynamic_cast<ModelAPI_Document*>((ModelAPI_Entity*)theObj);
53   return aDoc;
54 }
55
56
57
58
59 // Constructor *************************************************
60 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : ModuleBase_IDocumentDataModel(theParent)
61 {
62   myXMLReader.readAll();
63
64   Events_Loop* aLoop = Events_Loop::loop();
65   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
66   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
67   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_ORDER_UPDATED));
68   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
69 }
70
71 //******************************************************
72 void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
73 {
74   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
75   std::string aRootType = myXMLReader.rootType();
76   std::string aSubType = myXMLReader.subType();
77   int aNbFolders = foldersCount();
78
79   // Created object event *******************
80   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
81     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
82         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
83     std::set<ObjectPtr> aObjects = aUpdMsg->objects();
84
85     std::set<ObjectPtr>::const_iterator aIt;
86     std::string aObjType;
87     for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
88       ObjectPtr aObject = (*aIt);
89       // We do not show objects which not has to be shown in object browser
90       if (!aObject->isInHistory())
91         continue;
92
93       aObjType = aObject->groupName();
94       DocumentPtr aDoc = aObject->document();
95       if (aDoc == aRootDoc) {
96         // Check that new folders could appear
97         QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
98         foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
99           if ((aNotEmptyFolder.toStdString() == aObjType) && (aRootDoc->size(aObjType) == 1))
100             // Appears first object in folder which can not be shown empty
101             insertRow(myXMLReader.rootFolderId(aObjType));
102         }
103         // Insert new object
104         int aRow = aRootDoc->size(aObjType) - 1;
105         if (aRow != -1) {
106           if (aObjType == aRootType) {
107             insertRow(aRow + aNbFolders + 1);
108           } else {
109             int aFolderId = myXMLReader.rootFolderId(aObjType);
110             if (aFolderId != -1) {
111               insertRow(aRow, createIndex(aFolderId, 0, -1));
112             }
113           } 
114         }
115       } else {
116         // Object created in sub-document
117         QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
118         if (aDocRoot.isValid()) {
119           // Check that new folders could appear
120           QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
121           foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
122             if ((aNotEmptyFolder.toStdString() == aObjType) && (aDoc->size(aObjType) == 1))
123               // Appears first object in folder which can not be shown empty
124               insertRow(myXMLReader.subFolderId(aObjType), aDocRoot);
125           }
126           int aRow = aDoc->index(aObject);
127           if (aRow != -1) {
128             int aNbSubFolders = foldersCount(aDoc.get());
129             if (aObjType == aSubType) {
130               // List of objects under document root
131               insertRow(aRow + aNbSubFolders, aDocRoot);
132             } else {
133               // List of objects under a folder
134               if (aRow != -1) {
135                 int aFolderId = folderId(aObjType, aDoc.get());
136                 if (aFolderId != -1) {
137                   QModelIndex aParentFolder = createIndex(aFolderId, 0, aDoc.get());
138                   insertRow(aRow, aParentFolder);
139                   emit dataChanged(aParentFolder, aParentFolder);
140                 }
141               }
142             }
143           }
144         } 
145 #ifdef _DEBUG
146         else
147           Events_Error::send("Problem with Data Model definition of sub-document");
148 #endif
149       }
150     }
151     // Deleted object event ***********************
152   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
153     std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
154         std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
155     DocumentPtr aDoc = aUpdMsg->document();
156     std::set<std::string> aGroups = aUpdMsg->groups();
157     std::set<std::string>::const_iterator aIt;
158     for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
159       std::string aGroup = (*aIt);
160       if (aDoc == aRootDoc) {  // If root objects
161         int aRow = aRootDoc->size(aGroup);
162         if (aGroup == aRootType) {
163           removeRow(aRow + aNbFolders);
164         } else {
165           int aFolderId = myXMLReader.rootFolderId(aGroup);
166           if (aFolderId != -1) {
167             QModelIndex aFolderIndex = createIndex(aFolderId, 0, -1);
168             removeRow(aRow, aFolderIndex);
169           }
170         }
171         // Check that some folders could erased
172         QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
173         foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
174           if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup) == 0))
175             // Appears first object in folder which can not be shown empty
176             removeRow(myXMLReader.rootFolderId(aGroup));
177         }
178       } else {
179         // Remove row for sub-document
180         QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
181         if (aDocRoot.isValid()) {
182           int aRow = aDoc->size(aGroup);
183           int aNbSubFolders = foldersCount(aDoc.get());
184           if (aGroup == aSubType) {
185             // List of objects under document root
186             removeRow(aRow + aNbSubFolders, aDocRoot);
187           } else {
188             // List of objects under a folder
189             int aFolderId = folderId(aGroup, aDoc.get());
190             if (aFolderId != -1) {
191               removeRow(aRow, createIndex(aFolderId, 0, aDoc.get()));
192             }
193           }
194           // Check that some folders could disappear
195           QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
196           int aSize = aDoc->size(aGroup);
197           foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
198             if ((aNotEmptyFolder.toStdString() == aGroup) && (aSize == 0))
199               // Appears first object in folder which can not be shown empty
200               removeRow(myXMLReader.subFolderId(aGroup), aDocRoot);
201           }
202         } 
203 #ifdef _DEBUG
204         else
205           Events_Error::send("Problem with Data Model definition of sub-document");
206 #endif
207       }
208     }
209   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
210     std::shared_ptr<ModelAPI_OrderUpdatedMessage> aUpdMsg =
211         std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
212     DocumentPtr aDoc = aUpdMsg->document();
213     std::string aGroup = aUpdMsg->group();
214
215     QModelIndex aParent;
216     int aSartId = 0;
217     if (aDoc == aRootDoc) {
218       // Update a group under root
219       if (aGroup == myXMLReader.rootType()) // Update objects under root
220         aSartId = foldersCount();
221       else // Update objects in folder under root 
222         aParent = createIndex(folderId(aGroup), 0, -1);
223     } else {
224       // Update a sub-document
225       if (aGroup == myXMLReader.subType()) {
226         // Update sub-document root
227         aParent = findDocumentRootIndex(aDoc.get());
228         aSartId = foldersCount(aDoc.get());
229       } else 
230         // update folder in sub-document
231         aParent = createIndex(folderId(aGroup, aDoc.get()), 0, aDoc.get());
232     }
233     int aChildNb = rowCount(aParent);
234     // Rebuild all indexes
235     removeRows(aSartId, aChildNb - aSartId, aParent);
236     insertRows(aSartId, aChildNb - aSartId, aParent);
237   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
238     DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
239     if (aDoc != aRootDoc) {
240       QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
241       if (aDocRoot.isValid())
242         emit dataChanged(aDocRoot, aDocRoot);
243 #ifdef _DEBUG
244       else
245         Events_Error::send("Problem with Data Model definition of sub-document");
246 #endif
247     }
248   } 
249 }
250
251 //******************************************************
252 void XGUI_DataModel::clear()
253 {
254
255 }
256
257 //******************************************************
258 void XGUI_DataModel::rebuildDataTree()
259 {
260
261 }
262
263 //******************************************************
264 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
265 {
266   if (theIndex.internalId() < 0) // this is a folder
267     return ObjectPtr();
268   ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
269   if (getSubDocument(aObj)) // the selected index is a folder of sub-document
270     return ObjectPtr();
271
272   return aObj->data()->owner();
273 }
274
275 //******************************************************
276 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
277 {
278   std::string aType = theObject->groupName();
279   DocumentPtr aDoc = theObject->document();
280   int aRow = aDoc->index(theObject);
281   if (aRow == -1) {
282     // it could be a part of complex object
283     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
284     if (aFeature.get()) {
285       CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
286       if (aCompFea.get()) {
287         for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
288           if (aCompFea->subFeature(i, true) == theObject) {
289             aRow = i;
290             break;
291           }
292         }
293       }
294     } else {
295       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
296       if (aResult.get()) {
297         ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
298         if (aCompRes.get()) {
299           for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
300             if (aCompRes->subResult(i, true) == theObject) {
301               aRow = i;
302               break;
303             }
304           }
305         }
306       }
307     }
308     if (aRow == -1)
309       return QModelIndex();
310     else 
311       return createIndex(aRow, 0, theObject.get());
312   }
313   SessionPtr aSession = ModelAPI_Session::get();
314   DocumentPtr aRootDoc = aSession->moduleDocument();
315   if (aDoc == aRootDoc && myXMLReader.rootType() == aType) { 
316     // The object from root document
317     aRow += foldersCount();
318   } else if (myXMLReader.subType() == aType) { 
319     // The object from sub document
320     aRow += foldersCount(aDoc.get());
321   }
322   return createIndex(aRow, 0, theObject.get());
323 }
324
325 //******************************************************
326 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
327 {
328   SessionPtr aSession = ModelAPI_Session::get();
329   DocumentPtr aRootDoc = aSession->moduleDocument();
330   int aNbFolders = foldersCount();
331   int theIndexRow = theIndex.row();
332
333   if ((theRole == Qt::DecorationRole) && (theIndex == lastHistoryIndex()))
334     return QIcon(":pictures/arrow.png");
335
336   if (theIndex.column() == 1)
337     return QVariant();
338
339   int aParentId = theIndex.internalId();
340   if (aParentId == -1) { // root folders
341     switch (theRole) {
342       case Qt::DisplayRole:
343         return QString(myXMLReader.rootFolderName(theIndexRow).c_str()) + 
344           QString(" (%1)").arg(rowCount(theIndex));
345       case Qt::DecorationRole:
346         return QIcon(myXMLReader.rootFolderIcon(theIndexRow).c_str());
347       case Qt::ForegroundRole:
348         if ((flags(theIndex) & Qt::ItemIsEditable) == 0)
349           return QBrush(Qt::lightGray);
350         return ACTIVE_COLOR;
351     }
352   } else { // an object or sub-document
353     if (theRole == Qt::ForegroundRole) {
354       if ((flags(theIndex) & Qt::ItemIsEditable) == 0)
355         return QBrush(Qt::lightGray);
356       return ACTIVE_COLOR;
357     }
358
359     ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
360     if (aSubDoc) { // this is a folder of sub document
361       QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
362       int aRow = theIndexRow;
363       while (aMissedIdx.contains(aRow)) 
364         aRow++;
365
366       switch (theRole) {
367         case Qt::DisplayRole:
368           return QString(myXMLReader.subFolderName(aRow).c_str()) + 
369             QString(" (%1)").arg(rowCount(theIndex));
370         case Qt::DecorationRole:
371           return QIcon(myXMLReader.subFolderIcon(aRow).c_str());
372       }
373     } else {
374       ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
375       switch (theRole) {
376       case Qt::DisplayRole:
377         {
378           if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
379             ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
380             AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
381             QString aVal = QString::number(aValueAttribute->value());
382             QString aTitle = QString(aObj->data()->name().c_str());
383             return aTitle + " = " + aVal;
384           }
385           QString aPrefix;
386           if (aObj->groupName() == myXMLReader.subType()) {
387             ResultPartPtr aPartRes = getPartResult(aObj);
388             if (aPartRes.get()) {
389               if (aPartRes->partDoc().get() == NULL)
390                 aPrefix = "Not loaded ";
391             }
392           }
393           return aPrefix + aObj->data()->name().c_str();
394         }
395       case Qt::DecorationRole:
396         return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
397       }
398     }
399   }
400   return QVariant();
401 }
402
403 //******************************************************
404 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
405 {
406   return QVariant();
407 }
408
409 //******************************************************
410 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
411 {
412   SessionPtr aSession = ModelAPI_Session::get();
413   if (!aSession->hasModuleDocument())
414     return 0;
415   DocumentPtr aRootDoc = aSession->moduleDocument();
416
417   if (!theParent.isValid()) {
418     // Return number of items in root
419     int aNbFolders = foldersCount();
420     int aNbItems = 0;
421     std::string aType = myXMLReader.rootType();
422     if (!aType.empty())
423       aNbItems = aRootDoc->size(aType);
424     return aNbFolders + aNbItems;
425   }
426
427   int aId = theParent.internalId();
428   if (aId == -1) { 
429     // this is a folder under root
430     int aParentPos = theParent.row();
431     std::string aType = myXMLReader.rootFolderType(aParentPos);
432     return aRootDoc->size(aType);
433   } else {
434     // It is an object which could have children
435     ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
436     if (aDoc) { 
437       // a folder of sub-document
438       QIntList aMissedIdx = missedFolderIndexes(aDoc);
439       int aRow = theParent.row();
440       while (aMissedIdx.contains(aRow)) 
441         aRow++;
442       std::string aType = myXMLReader.subFolderType(aRow);
443       return aDoc->size(aType);
444     } else {
445       ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
446       // Check for Part feature
447       ResultPartPtr aPartRes = getPartResult(aObj);
448       if (aPartRes.get()) {
449         DocumentPtr aSubDoc = aPartRes->partDoc();
450         if (!aSubDoc.get())
451           return 0;
452
453         int aNbSubFolders = foldersCount(aSubDoc.get());
454         int aNbSubItems = 0;
455         std::string aSubType = myXMLReader.subType();
456         if (!aSubType.empty())
457           aNbSubItems = aSubDoc->size(aSubType);
458         return aNbSubItems + aNbSubFolders;
459       } else {
460         // Check for composite object
461         ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
462         if (aCompFeature) 
463           return aCompFeature->numberOfSubs(true);
464         ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
465         if (aCompRes) 
466           return aCompRes->numberOfSubs(true);
467       }
468     }
469   }
470   return 0;
471 }
472
473 //******************************************************
474 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
475 {
476   return 2;
477 }
478
479 //******************************************************
480 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
481 {
482   SessionPtr aSession = ModelAPI_Session::get();
483   DocumentPtr aRootDoc = aSession->moduleDocument();
484   int aNbFolders = foldersCount();
485
486   QModelIndex aIndex;
487
488   if (!theParent.isValid()) {
489     if (theRow < aNbFolders) // Return first level folder index
490       return createIndex(theRow, theColumn, -1);
491     else { // return object under root index
492       std::string aType = myXMLReader.rootType();
493       int aObjId = theRow - aNbFolders;
494       if (aObjId < aRootDoc->size(aType)) {
495         ObjectPtr aObj = aRootDoc->object(aType, aObjId);
496         aIndex = objectIndex(aObj);
497       }
498     }
499   } else {
500     int aId = theParent.internalId();
501     int aParentPos = theParent.row();
502     if (aId == -1) { // return object index inside of first level of folders
503       std::string aType = myXMLReader.rootFolderType(aParentPos);
504       if (theRow < aRootDoc->size(aType)) {
505         ObjectPtr aObj = aRootDoc->object(aType, theRow);
506         aIndex = objectIndex(aObj);
507       }
508     } else {
509       // It is an object which could have children
510       ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
511       if (aDoc) { 
512         // It is a folder of sub-document
513         int aParentRow = aParentPos;
514         QIntList aMissedIdx = missedFolderIndexes(aDoc);
515         while (aMissedIdx.contains(aParentRow))
516           aParentRow++;
517         std::string aType = myXMLReader.subFolderType(aParentRow);
518         if (theRow < aDoc->size(aType)) {
519           ObjectPtr aObj = aDoc->object(aType, theRow);
520           aIndex = objectIndex(aObj);
521         }
522       } else {
523         ModelAPI_Object* aParentObj = (ModelAPI_Object*)theParent.internalPointer();
524
525         // Check for Part feature
526         ResultPartPtr aPartRes = getPartResult(aParentObj);
527         if (aPartRes.get()) {
528           DocumentPtr aSubDoc = aPartRes->partDoc();
529           int aNbSubFolders = foldersCount(aSubDoc.get());
530           if (theRow < aNbSubFolders) { // Create a Folder of sub-document
531             aIndex = createIndex(theRow, theColumn, aSubDoc.get());
532           } else {
533             // this is an object under sub document root
534             std::string aType = myXMLReader.subType();
535             ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
536             aIndex = objectIndex(aObj);
537           }
538         } else {
539           // Check for composite object
540           ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
541           if (aCompFeature) {
542             aIndex = objectIndex(aCompFeature->subFeature(theRow));
543           } else {
544             ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
545             if (aCompRes) 
546               aIndex = objectIndex(aCompRes->subResult(theRow));
547           }
548         }
549       }
550     }
551   }
552   if (theColumn != 0)
553     return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
554   return aIndex;
555 }
556
557 //******************************************************
558 static QModelIndex MYLastDeleted;
559 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
560 {
561   // To avoid additional request about index which was already deleted
562   if (theIndex == MYLastDeleted)
563     return QModelIndex();
564
565   int aId = theIndex.internalId();
566   if (aId != -1) { // The object is not a root folder
567     ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
568     if (aDoc) { 
569       // It is a folder of sub-document
570       return findDocumentRootIndex(aDoc);
571     }
572     ObjectPtr aObj = object(theIndex);
573     if (!aObj.get()) {
574       // To avoid additional request about index which was already deleted
575       // If deleted it causes a crash on delete object from Part
576       MYLastDeleted = theIndex;
577       return QModelIndex();
578     }
579     // Check is it object a sub-object of a complex object
580     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
581     if (aFeature.get()) {
582       CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
583       if (aCompFea.get()) {
584         return objectIndex(aCompFea);
585       }
586     }
587     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
588     if (aResult.get()) {
589       ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
590       if (aCompRes.get()) {
591         return objectIndex(aCompRes);
592       }
593     }
594     // Use as ordinary object
595     std::string aType = aObj->groupName();
596     SessionPtr aSession = ModelAPI_Session::get();
597     DocumentPtr aRootDoc = aSession->moduleDocument();
598     DocumentPtr aSubDoc = aObj->document();
599     if (aSubDoc == aRootDoc) {
600       if (aType == myXMLReader.rootType())
601         return QModelIndex();
602       else {
603         // return first level of folder index
604         int aFolderId = myXMLReader.rootFolderId(aType);
605         // Items in a one row must have the same parent
606         return createIndex(aFolderId, 0, -1);
607       }
608     } else {
609       if (aType == myXMLReader.subType())
610         return findDocumentRootIndex(aSubDoc.get());
611       else {
612         // return first level of folder index
613         int aFolderId = myXMLReader.subFolderId(aType);
614         // Items in a one row must have the same parent
615         return createIndex(aFolderId, 0, aSubDoc.get());
616       }
617     }
618   } 
619   return QModelIndex();
620 }
621
622 //******************************************************
623 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
624 {
625   return rowCount(theParent) > 0;
626 }
627
628 //******************************************************
629 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
630 {
631   beginInsertRows(theParent, theRow, theRow + theCount - 1);
632   endInsertRows();
633
634   return true;
635 }
636
637 //******************************************************
638 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
639 {
640   beginRemoveRows(theParent, theRow, theRow + theCount - 1);
641   endRemoveRows();
642   return true;
643 }
644
645 //******************************************************
646 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
647 {
648   qint64 aIt = theIndex.internalId();
649   ModelAPI_Object* aObj = 0;
650   ModelAPI_Document* aDoc = 0;
651   SessionPtr aSession = ModelAPI_Session::get();
652   DocumentPtr aActiveDoc = aSession->activeDocument();
653
654   Qt::ItemFlags aNullFlag;
655   Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
656   Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
657
658
659   if (aIt == -1) {
660     // Folders under root
661     DocumentPtr aRootDoc = aSession->moduleDocument();
662     if (aRootDoc != aActiveDoc)
663       return aDefaultFlag;
664   } else {
665     aDoc = getSubDocument(theIndex.internalPointer());
666     if (!aDoc)
667       aObj = (ModelAPI_Object*) theIndex.internalPointer();
668   }
669
670   if (aObj) {
671     // An object
672     if (aObj->isDisabled()) 
673       return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
674     
675     bool isCompositeSub = false;
676     // An object which is sub-object of a composite object can not be accessible in column 1
677     if (theIndex.column() == 1) {
678       ObjectPtr aObjPtr = aObj->data()->owner();
679       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
680       if (aFeature.get()) {
681         CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
682         if (aCompFea.get()) 
683           isCompositeSub = true;
684       } else {
685         ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
686         if (aResult.get()) {
687           ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
688           if (aCompRes.get()) 
689             isCompositeSub = true;
690         }
691       }
692     }
693     if (isCompositeSub)
694       return Qt::ItemIsSelectable;
695
696     if (aObj->document() != aActiveDoc) {
697       // The object could be a root of sub-tree
698       ResultPartPtr aPartRes = getPartResult(aObj);
699       if (aPartRes.get()) {
700         if (aPartRes->partDoc() == aActiveDoc)
701           return aEditingFlag;
702       }
703       return aDefaultFlag;
704     }
705   } else if (aDoc) {
706     // A folder under sub-document
707     if (aActiveDoc.get() != aDoc)
708       return aDefaultFlag;
709   }
710   return aEditingFlag;
711 }
712
713 //******************************************************
714 QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
715 {
716   SessionPtr aSession = ModelAPI_Session::get();
717   DocumentPtr aRootDoc = aSession->moduleDocument();
718   if (myXMLReader.isAttachToResult()) { // If document is attached to result
719     int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
720     ObjectPtr aObj;
721     ResultPartPtr aPartRes;
722     for (int i = 0; i < aNb; i++) {
723       aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
724       aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
725       if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
726         int aRow = i;
727         if (myXMLReader.rootType() == ModelAPI_Feature::group()) {
728           aRow += foldersCount();
729         }
730         return createIndex(aRow, 0, aObj.get());
731       }
732     }
733   } else { // If document is attached to feature
734     int aNb = aRootDoc->size(ModelAPI_Feature::group());
735     ObjectPtr aObj;
736     ResultPartPtr aPartRes;
737     for (int i = 0; i < aNb; i++) {
738       aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
739       aPartRes = getPartResult(aObj.get());
740       if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
741         int aRow = i;
742         if (myXMLReader.rootType() == ModelAPI_Feature::group())
743           aRow += foldersCount();
744         return createIndex(aRow, 0, aObj.get());
745       }
746     }
747   }
748   return QModelIndex();
749 }
750
751 //******************************************************
752 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
753 {
754   SessionPtr aSession = ModelAPI_Session::get();
755   DocumentPtr aRootDoc = aSession->moduleDocument();
756   if (theDoc == aRootDoc)
757     return QModelIndex();
758   else 
759     return findDocumentRootIndex(theDoc.get());
760 }
761
762 //******************************************************
763 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
764 {
765   int aNb = 0;
766   SessionPtr aSession = ModelAPI_Session::get();
767   DocumentPtr aRootDoc = aSession->moduleDocument();
768   if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
769     for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
770       if (myXMLReader.rootShowEmpty(i))
771         aNb++;
772       else {
773         if (aRootDoc->size(myXMLReader.rootFolderType(i)) > 0)
774           aNb++;
775       }
776     }
777   } else {
778     for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
779       if (myXMLReader.subShowEmpty(i))
780         aNb++;
781       else {
782         if (theDoc->size(myXMLReader.subFolderType(i)) > 0)
783           aNb++;
784       }
785     }
786   }
787   return aNb;
788 }
789
790
791 //******************************************************
792 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
793 {
794   QIntList aList;
795   SessionPtr aSession = ModelAPI_Session::get();
796   DocumentPtr aRootDoc = aSession->moduleDocument();
797   if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
798     for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
799       if (!myXMLReader.rootShowEmpty(i)) {
800         if (aRootDoc->size(myXMLReader.rootFolderType(i)) == 0)
801           aList.append(i);
802       }
803     }
804   } else {
805     for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
806       if (!myXMLReader.subShowEmpty(i)) {
807         if (theDoc->size(myXMLReader.subFolderType(i)) == 0)
808           aList.append(i);
809       }
810     }
811   }
812   return aList;
813 }
814
815
816 //******************************************************
817 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
818 {
819   QStringList aResult;
820   if (fromRoot) {
821     for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
822       if (!myXMLReader.rootShowEmpty(i))
823         aResult << myXMLReader.rootFolderType(i).c_str();
824     }
825   } else {
826     for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
827       if (!myXMLReader.subShowEmpty(i))
828         aResult << myXMLReader.subFolderType(i).c_str();
829     }
830   }
831   return aResult;
832 }
833
834 //******************************************************
835 QModelIndex XGUI_DataModel::lastHistoryIndex() const
836 {
837   SessionPtr aSession = ModelAPI_Session::get();
838   DocumentPtr aCurDoc = aSession->activeDocument();
839   FeaturePtr aFeature = aCurDoc->currentFeature(true);
840   if (aFeature.get()) {
841     QModelIndex aInd = objectIndex(aFeature);
842     return createIndex(aInd.row(), 1, aInd.internalPointer());
843   } else {
844     if (aCurDoc == aSession->moduleDocument())
845       return createIndex(foldersCount() - 1, 1, -1);
846     else 
847       return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());
848   }
849 }
850
851 //******************************************************
852 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc)
853 {
854   SessionPtr aSession = ModelAPI_Session::get();
855   ModelAPI_Document* aDoc = theDoc;
856   if (aDoc == 0)
857     aDoc = aSession->moduleDocument().get();
858
859   bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
860
861   int aRes = -1;
862   if (aUseSubDoc) {
863     int aId = myXMLReader.subFolderId(theType);
864     aRes = aId;
865     for (int i = 0; i < aId; i++) {
866       if (!myXMLReader.subShowEmpty(i)) {
867         if (aDoc->size(myXMLReader.subFolderType(i)) == 0)
868           aRes--;
869       }
870     }
871   } else {
872     int aId = myXMLReader.rootFolderId(theType);
873     aRes = aId;
874     for (int i = 0; i < aId; i++) {
875       if (!myXMLReader.rootShowEmpty(i)) {
876         if (aDoc->size(myXMLReader.rootFolderType(i)) == 0)
877           aRes--;
878       }
879     }
880   }
881   return aRes;
882 }