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