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