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