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