]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_DataModel.cpp
Salome HOME
5aec6885cc0d3e5a886470598179a5d076cad40c
[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_ObjectUpdatedMessage> aUpdMsg =
211         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
212     std::set<ObjectPtr> aObjects = aUpdMsg->objects();
213
214     std::set<ObjectPtr>::const_iterator aIt;
215     std::string aObjType;
216     for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
217       ObjectPtr aObject = (*aIt);
218       // We do not show objects which not has to be shown in object browser
219       if (!aObject->isInHistory())
220         continue;
221       QModelIndex aIndex = objectIndex(aObject);
222       QModelIndex aParent = parent(aIndex);
223     }
224   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
225     DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
226     if (aDoc != aRootDoc) {
227       QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
228       if (aDocRoot.isValid())
229         emit dataChanged(aDocRoot, aDocRoot);
230 #ifdef _DEBUG
231       else
232         Events_Error::send("Problem with Data Model definition of sub-document");
233 #endif
234     }
235   } 
236 }
237
238 //******************************************************
239 void XGUI_DataModel::clear()
240 {
241
242 }
243
244 //******************************************************
245 void XGUI_DataModel::rebuildDataTree()
246 {
247
248 }
249
250 //******************************************************
251 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
252 {
253   if (theIndex.internalId() < 0) // this is a folder
254     return ObjectPtr();
255   ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
256   if (getSubDocument(aObj)) // the selected index is a folder of sub-document
257     return ObjectPtr();
258
259   return aObj->data()->owner();
260 }
261
262 //******************************************************
263 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
264 {
265   std::string aType = theObject->groupName();
266   DocumentPtr aDoc = theObject->document();
267   int aRow = aDoc->index(theObject);
268   if (aRow == -1) {
269     // it could be a part of complex object
270     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
271     if (aFeature.get()) {
272       CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
273       if (aCompFea.get()) {
274         for (int i = 0; i < aCompFea->numberOfSubs(true); i++) {
275           if (aCompFea->subFeature(i, true) == theObject) {
276             aRow = i;
277             break;
278           }
279         }
280       }
281     } else {
282       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
283       if (aResult.get()) {
284         ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
285         if (aCompRes.get()) {
286           for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
287             if (aCompRes->subResult(i, true) == theObject) {
288               aRow = i;
289               break;
290             }
291           }
292         }
293       }
294     }
295     if (aRow == -1)
296       return QModelIndex();
297     else 
298       return createIndex(aRow, 0, theObject.get());
299   }
300   SessionPtr aSession = ModelAPI_Session::get();
301   DocumentPtr aRootDoc = aSession->moduleDocument();
302   if (aDoc == aRootDoc && myXMLReader.rootType() == aType) { 
303     // The object from root document
304     aRow += foldersCount();
305   } else if (myXMLReader.subType() == aType) { 
306     // The object from sub document
307     aRow += foldersCount(aDoc.get());
308   }
309   return createIndex(aRow, 0, theObject.get());
310 }
311
312 //******************************************************
313 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
314 {
315   SessionPtr aSession = ModelAPI_Session::get();
316   DocumentPtr aRootDoc = aSession->moduleDocument();
317   int aNbFolders = foldersCount();
318   int theIndexRow = theIndex.row();
319
320   if ((theRole == Qt::DecorationRole) && (theIndex == lastHistoryIndex()))
321     return QIcon(":pictures/arrow.png");
322
323   if (theIndex.column() == 1)
324     return QVariant();
325
326   int aParentId = theIndex.internalId();
327   if (aParentId == -1) { // root folders
328     switch (theRole) {
329       case Qt::DisplayRole:
330         return QString(myXMLReader.rootFolderName(theIndexRow).c_str()) + 
331           QString(" (%1)").arg(rowCount(theIndex));
332       case Qt::DecorationRole:
333         return QIcon(myXMLReader.rootFolderIcon(theIndexRow).c_str());
334       case Qt::ForegroundRole:
335         if ((flags(theIndex) & Qt::ItemIsEditable) == 0)
336           return QBrush(Qt::lightGray);
337         return ACTIVE_COLOR;
338     }
339   } else { // an object or sub-document
340     if (theRole == Qt::ForegroundRole) {
341       if ((flags(theIndex) & Qt::ItemIsEditable) == 0)
342         return QBrush(Qt::lightGray);
343       return ACTIVE_COLOR;
344     }
345
346     ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
347     if (aSubDoc) { // this is a folder of sub document
348       QIntList aMissedIdx = missedFolderIndexes(aSubDoc);
349       int aRow = theIndexRow;
350       while (aMissedIdx.contains(aRow)) 
351         aRow++;
352
353       switch (theRole) {
354         case Qt::DisplayRole:
355           return QString(myXMLReader.subFolderName(aRow).c_str()) + 
356             QString(" (%1)").arg(rowCount(theIndex));
357         case Qt::DecorationRole:
358           return QIcon(myXMLReader.subFolderIcon(aRow).c_str());
359       }
360     } else {
361       ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
362       switch (theRole) {
363       case Qt::DisplayRole:
364         {
365           if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
366             ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
367             AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
368             QString aVal = QString::number(aValueAttribute->value());
369             QString aTitle = QString(aObj->data()->name().c_str());
370             return aTitle + " = " + aVal;
371           }
372           QString aPrefix;
373           if (aObj->groupName() == myXMLReader.subType()) {
374             ResultPartPtr aPartRes = getPartResult(aObj);
375             if (aPartRes.get()) {
376               if (aPartRes->partDoc().get() == NULL)
377                 aPrefix = "Not loaded ";
378             }
379           }
380           return aPrefix + aObj->data()->name().c_str();
381         }
382       case Qt::DecorationRole:
383         return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
384       }
385     }
386   }
387   return QVariant();
388 }
389
390 //******************************************************
391 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
392 {
393   return QVariant();
394 }
395
396 //******************************************************
397 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
398 {
399   SessionPtr aSession = ModelAPI_Session::get();
400   if (!aSession->hasModuleDocument())
401     return 0;
402   DocumentPtr aRootDoc = aSession->moduleDocument();
403
404   if (!theParent.isValid()) {
405     // Return number of items in root
406     int aNbFolders = foldersCount();
407     int aNbItems = 0;
408     std::string aType = myXMLReader.rootType();
409     if (!aType.empty())
410       aNbItems = aRootDoc->size(aType);
411     return aNbFolders + aNbItems;
412   }
413
414   int aId = theParent.internalId();
415   if (aId == -1) { 
416     // this is a folder under root
417     int aParentPos = theParent.row();
418     std::string aType = myXMLReader.rootFolderType(aParentPos);
419     return aRootDoc->size(aType);
420   } else {
421     // It is an object which could have children
422     ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
423     if (aDoc) { 
424       // a folder of sub-document
425       QIntList aMissedIdx = missedFolderIndexes(aDoc);
426       int aRow = theParent.row();
427       while (aMissedIdx.contains(aRow)) 
428         aRow++;
429       std::string aType = myXMLReader.subFolderType(aRow);
430       return aDoc->size(aType);
431     } else {
432       ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
433       // Check for Part feature
434       ResultPartPtr aPartRes = getPartResult(aObj);
435       if (aPartRes.get()) {
436         DocumentPtr aSubDoc = aPartRes->partDoc();
437         if (!aSubDoc.get())
438           return 0;
439
440         int aNbSubFolders = foldersCount(aSubDoc.get());
441         int aNbSubItems = 0;
442         std::string aSubType = myXMLReader.subType();
443         if (!aSubType.empty())
444           aNbSubItems = aSubDoc->size(aSubType);
445         return aNbSubItems + aNbSubFolders;
446       } else {
447         // Check for composite object
448         ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aObj);
449         if (aCompFeature) 
450           return aCompFeature->numberOfSubs(true);
451         ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aObj);
452         if (aCompRes) 
453           return aCompRes->numberOfSubs(true);
454       }
455     }
456   }
457   return 0;
458 }
459
460 //******************************************************
461 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
462 {
463   return 2;
464 }
465
466 //******************************************************
467 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
468 {
469   SessionPtr aSession = ModelAPI_Session::get();
470   DocumentPtr aRootDoc = aSession->moduleDocument();
471   int aNbFolders = foldersCount();
472
473   QModelIndex aIndex;
474
475   if (!theParent.isValid()) {
476     if (theRow < aNbFolders) // Return first level folder index
477       return createIndex(theRow, theColumn, -1);
478     else { // return object under root index
479       std::string aType = myXMLReader.rootType();
480       int aObjId = theRow - aNbFolders;
481       if (aObjId < aRootDoc->size(aType)) {
482         ObjectPtr aObj = aRootDoc->object(aType, aObjId);
483         aIndex = objectIndex(aObj);
484       }
485     }
486   } else {
487     int aId = theParent.internalId();
488     int aParentPos = theParent.row();
489     if (aId == -1) { // return object index inside of first level of folders
490       std::string aType = myXMLReader.rootFolderType(aParentPos);
491       if (theRow < aRootDoc->size(aType)) {
492         ObjectPtr aObj = aRootDoc->object(aType, theRow);
493         aIndex = objectIndex(aObj);
494       }
495     } else {
496       // It is an object which could have children
497       ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
498       if (aDoc) { 
499         // It is a folder of sub-document
500         int aParentRow = aParentPos;
501         QIntList aMissedIdx = missedFolderIndexes(aDoc);
502         while (aMissedIdx.contains(aParentRow))
503           aParentRow++;
504         std::string aType = myXMLReader.subFolderType(aParentRow);
505         if (theRow < aDoc->size(aType)) {
506           ObjectPtr aObj = aDoc->object(aType, theRow);
507           aIndex = objectIndex(aObj);
508         }
509       } else {
510         ModelAPI_Object* aParentObj = (ModelAPI_Object*)theParent.internalPointer();
511
512         // Check for Part feature
513         ResultPartPtr aPartRes = getPartResult(aParentObj);
514         if (aPartRes.get()) {
515           DocumentPtr aSubDoc = aPartRes->partDoc();
516           int aNbSubFolders = foldersCount(aSubDoc.get());
517           if (theRow < aNbSubFolders) { // Create a Folder of sub-document
518             aIndex = createIndex(theRow, theColumn, aSubDoc.get());
519           } else {
520             // this is an object under sub document root
521             std::string aType = myXMLReader.subType();
522             ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
523             aIndex = objectIndex(aObj);
524           }
525         } else {
526           // Check for composite object
527           ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
528           if (aCompFeature) {
529             aIndex = objectIndex(aCompFeature->subFeature(theRow));
530           } else {
531             ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
532             if (aCompRes) 
533               aIndex = objectIndex(aCompRes->subResult(theRow));
534           }
535         }
536       }
537     }
538   }
539   if (theColumn != 0)
540     return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
541   return aIndex;
542 }
543
544 //******************************************************
545 static QModelIndex MYLastDeleted;
546 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
547 {
548   // To avoid additional request about index which was already deleted
549   if (theIndex == MYLastDeleted)
550     return QModelIndex();
551
552   int aId = theIndex.internalId();
553   if (aId != -1) { // The object is not a root folder
554     ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
555     if (aDoc) { 
556       // It is a folder of sub-document
557       return findDocumentRootIndex(aDoc);
558     }
559     ObjectPtr aObj = object(theIndex);
560     if (!aObj.get()) {
561       // To avoid additional request about index which was already deleted
562       // If deleted it causes a crash on delete object from Part
563       MYLastDeleted = theIndex;
564       return QModelIndex();
565     }
566     // Check is it object a sub-object of a complex object
567     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
568     if (aFeature.get()) {
569       CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
570       if (aCompFea.get()) {
571         return objectIndex(aCompFea);
572       }
573     }
574     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
575     if (aResult.get()) {
576       ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
577       if (aCompRes.get()) {
578         return objectIndex(aCompRes);
579       }
580     }
581     // Use as ordinary object
582     std::string aType = aObj->groupName();
583     SessionPtr aSession = ModelAPI_Session::get();
584     DocumentPtr aRootDoc = aSession->moduleDocument();
585     DocumentPtr aSubDoc = aObj->document();
586     if (aSubDoc == aRootDoc) {
587       if (aType == myXMLReader.rootType())
588         return QModelIndex();
589       else {
590         // return first level of folder index
591         int aFolderId = myXMLReader.rootFolderId(aType);
592         // Items in a one row must have the same parent
593         return createIndex(aFolderId, 0, -1);
594       }
595     } else {
596       if (aType == myXMLReader.subType())
597         return findDocumentRootIndex(aSubDoc.get());
598       else {
599         // return first level of folder index
600         int aFolderId = myXMLReader.subFolderId(aType);
601         // Items in a one row must have the same parent
602         return createIndex(aFolderId, 0, aSubDoc.get());
603       }
604     }
605   } 
606   return QModelIndex();
607 }
608
609 //******************************************************
610 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
611 {
612   return rowCount(theParent) > 0;
613 }
614
615 //******************************************************
616 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
617 {
618   beginInsertRows(theParent, theRow, theRow + theCount - 1);
619   endInsertRows();
620
621   return true;
622 }
623
624 //******************************************************
625 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
626 {
627   beginRemoveRows(theParent, theRow, theRow + theCount - 1);
628   endRemoveRows();
629   return true;
630 }
631
632 //******************************************************
633 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
634 {
635   qint64 aIt = theIndex.internalId();
636   ModelAPI_Object* aObj = 0;
637   ModelAPI_Document* aDoc = 0;
638   SessionPtr aSession = ModelAPI_Session::get();
639   DocumentPtr aActiveDoc = aSession->activeDocument();
640
641   Qt::ItemFlags aNullFlag;
642   Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
643   Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
644
645
646   if (aIt == -1) {
647     // Folders under root
648     DocumentPtr aRootDoc = aSession->moduleDocument();
649     if (aRootDoc != aActiveDoc)
650       return aDefaultFlag;
651   } else {
652     aDoc = getSubDocument(theIndex.internalPointer());
653     if (!aDoc)
654       aObj = (ModelAPI_Object*) theIndex.internalPointer();
655   }
656
657   if (aObj) {
658     // An object
659     if (aObj->isDisabled()) 
660       return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
661     
662     bool isCompositeSub = false;
663     // An object which is sub-object of a composite object can not be accessible in column 1
664     if (theIndex.column() == 1) {
665       ObjectPtr aObjPtr = aObj->data()->owner();
666       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
667       if (aFeature.get()) {
668         CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
669         if (aCompFea.get()) 
670           isCompositeSub = true;
671       } else {
672         ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
673         if (aResult.get()) {
674           ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
675           if (aCompRes.get()) 
676             isCompositeSub = true;
677         }
678       }
679     }
680     if (isCompositeSub)
681       return Qt::ItemIsSelectable;
682
683     if (aObj->document() != aActiveDoc) {
684       // The object could be a root of sub-tree
685       ResultPartPtr aPartRes = getPartResult(aObj);
686       if (aPartRes.get()) {
687         if (aPartRes->partDoc() == aActiveDoc)
688           return aEditingFlag;
689       }
690       return aDefaultFlag;
691     }
692   } else if (aDoc) {
693     // A folder under sub-document
694     if (aActiveDoc.get() != aDoc)
695       return aDefaultFlag;
696   }
697   return aEditingFlag;
698 }
699
700 //******************************************************
701 QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
702 {
703   SessionPtr aSession = ModelAPI_Session::get();
704   DocumentPtr aRootDoc = aSession->moduleDocument();
705   if (myXMLReader.isAttachToResult()) { // If document is attached to result
706     int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
707     ObjectPtr aObj;
708     ResultPartPtr aPartRes;
709     for (int i = 0; i < aNb; i++) {
710       aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
711       aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
712       if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
713         int aRow = i;
714         if (myXMLReader.rootType() == ModelAPI_Feature::group()) {
715           aRow += foldersCount();
716         }
717         return createIndex(aRow, 0, aObj.get());
718       }
719     }
720   } else { // If document is attached to feature
721     int aNb = aRootDoc->size(ModelAPI_Feature::group());
722     ObjectPtr aObj;
723     ResultPartPtr aPartRes;
724     for (int i = 0; i < aNb; i++) {
725       aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
726       aPartRes = getPartResult(aObj.get());
727       if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
728         int aRow = i;
729         if (myXMLReader.rootType() == ModelAPI_Feature::group())
730           aRow += foldersCount();
731         return createIndex(aRow, 0, aObj.get());
732       }
733     }
734   }
735   return QModelIndex();
736 }
737
738 //******************************************************
739 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
740 {
741   SessionPtr aSession = ModelAPI_Session::get();
742   DocumentPtr aRootDoc = aSession->moduleDocument();
743   if (theDoc == aRootDoc)
744     return QModelIndex();
745   else 
746     return findDocumentRootIndex(theDoc.get());
747 }
748
749 //******************************************************
750 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
751 {
752   int aNb = 0;
753   SessionPtr aSession = ModelAPI_Session::get();
754   DocumentPtr aRootDoc = aSession->moduleDocument();
755   if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
756     for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
757       if (myXMLReader.rootShowEmpty(i))
758         aNb++;
759       else {
760         if (aRootDoc->size(myXMLReader.rootFolderType(i)) > 0)
761           aNb++;
762       }
763     }
764   } else {
765     for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
766       if (myXMLReader.subShowEmpty(i))
767         aNb++;
768       else {
769         if (theDoc->size(myXMLReader.subFolderType(i)) > 0)
770           aNb++;
771       }
772     }
773   }
774   return aNb;
775 }
776
777
778 //******************************************************
779 QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
780 {
781   QIntList aList;
782   SessionPtr aSession = ModelAPI_Session::get();
783   DocumentPtr aRootDoc = aSession->moduleDocument();
784   if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
785     for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
786       if (!myXMLReader.rootShowEmpty(i)) {
787         if (aRootDoc->size(myXMLReader.rootFolderType(i)) == 0)
788           aList.append(i);
789       }
790     }
791   } else {
792     for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
793       if (!myXMLReader.subShowEmpty(i)) {
794         if (theDoc->size(myXMLReader.subFolderType(i)) == 0)
795           aList.append(i);
796       }
797     }
798   }
799   return aList;
800 }
801
802
803 //******************************************************
804 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
805 {
806   QStringList aResult;
807   if (fromRoot) {
808     for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
809       if (!myXMLReader.rootShowEmpty(i))
810         aResult << myXMLReader.rootFolderType(i).c_str();
811     }
812   } else {
813     for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
814       if (!myXMLReader.subShowEmpty(i))
815         aResult << myXMLReader.subFolderType(i).c_str();
816     }
817   }
818   return aResult;
819 }
820
821 //******************************************************
822 QModelIndex XGUI_DataModel::lastHistoryIndex() const
823 {
824   SessionPtr aSession = ModelAPI_Session::get();
825   DocumentPtr aCurDoc = aSession->activeDocument();
826   FeaturePtr aFeature = aCurDoc->currentFeature(true);
827   if (aFeature.get()) {
828     QModelIndex aInd = objectIndex(aFeature);
829     return createIndex(aInd.row(), 1, aInd.internalPointer());
830   } else {
831     if (aCurDoc == aSession->moduleDocument())
832       return createIndex(foldersCount() - 1, 1, -1);
833     else 
834       return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());
835   }
836 }
837
838 //******************************************************
839 int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc)
840 {
841   SessionPtr aSession = ModelAPI_Session::get();
842   ModelAPI_Document* aDoc = theDoc;
843   if (aDoc == 0)
844     aDoc = aSession->moduleDocument().get();
845
846   bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
847
848   int aRes = -1;
849   if (aUseSubDoc) {
850     int aId = myXMLReader.subFolderId(theType);
851     aRes = aId;
852     for (int i = 0; i < aId; i++) {
853       if (!myXMLReader.subShowEmpty(i)) {
854         if (aDoc->size(myXMLReader.subFolderType(i)) == 0)
855           aRes--;
856       }
857     }
858   } else {
859     int aId = myXMLReader.rootFolderId(theType);
860     aRes = aId;
861     for (int i = 0; i < aId; i++) {
862       if (!myXMLReader.rootShowEmpty(i)) {
863         if (aDoc->size(myXMLReader.rootFolderType(i)) == 0)
864           aRes--;
865       }
866     }
867   }
868   return aRes;
869 }