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