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