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