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