Salome HOME
Fix for the issue #2753 : error when dump/load script
[modules/shaper.git] / src / PartSet / PartSet_TreeNodes.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 "PartSet_TreeNodes.h"
22 #include "PartSet_Tools.h"
23
24 #include <ModuleBase_IconFactory.h>
25 #include <ModuleBase_IWorkshop.h>
26 #include <ModuleBase_Tools.h>
27
28 #include <PartSetPlugin_Part.h>
29
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_ResultParameter.h>
32 #include <ModelAPI_ResultField.h>
33 #include <ModelAPI_ResultGroup.h>
34 #include <ModelAPI_ResultConstruction.h>
35 #include <ModelAPI_ResultPart.h>
36 #include <ModelAPI_ResultBody.h>
37 #include <ModelAPI_Tools.h>
38 #include <ModelAPI_ResultBody.h>
39 #include <ModelAPI_CompositeFeature.h>
40 #include <ModelAPI_AttributeDouble.h>
41 #include <ModelAPI_Folder.h>
42 #include <ModelAPI_AttributeReference.h>
43
44 #include <QBrush>
45 #include <QMap>
46
47
48 #define ACTIVE_COLOR QColor(Qt::black)
49 #define SELECTABLE_COLOR QColor(100, 100, 100)
50 #define DISABLED_COLOR QColor(200, 200, 200)
51
52 Qt::ItemFlags aNullFlag;
53 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
54 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
55
56
57 ResultPartPtr getPartResult(const ObjectPtr& theObj)
58 {
59   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
60   if (aFeature) {
61     ResultPtr aRes = aFeature->firstResult();
62     if (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group())) {
63       ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aRes);
64       // Use only original parts, not a placement results
65       if (aPartRes == aPartRes->original())
66         return aPartRes;
67     }
68   }
69   return ResultPartPtr();
70 }
71
72 bool isCurrentFeature(const ObjectPtr& theObj)
73 {
74   SessionPtr aSession = ModelAPI_Session::get();
75   DocumentPtr aCurDoc = aSession->activeDocument();
76   FeaturePtr aFeature = aCurDoc->currentFeature(true);
77   return aFeature == theObj;
78 }
79
80 //////////////////////////////////////////////////////////////////////////////////
81 QVariant PartSet_TreeNode::data(int theColumn, int theRole) const
82 {
83   if ((theColumn == 1) && (theRole == Qt::ForegroundRole)) {
84     Qt::ItemFlags aFlags = flags(theColumn);
85     if (aFlags == Qt::ItemFlags())
86       return QBrush(DISABLED_COLOR);
87     if (!aFlags.testFlag(Qt::ItemIsEditable))
88       return QBrush(SELECTABLE_COLOR);
89     return ACTIVE_COLOR;
90   }
91   return ModuleBase_ITreeNode::data(theColumn, theRole);
92 }
93
94
95 //////////////////////////////////////////////////////////////////////////////////
96 QVariant PartSet_ObjectNode::data(int theColumn, int theRole) const
97 {
98   switch (theRole) {
99   case Qt::DisplayRole:
100     if (theColumn == 1) {
101       if (myObject->groupName() == ModelAPI_ResultParameter::group()) {
102         ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myObject);
103         AttributeDoublePtr aValueAttribute =
104           aParam->data()->real(ModelAPI_ResultParameter::VALUE());
105         QString aVal = QString::number(aValueAttribute->value());
106         QString aTitle = QString(myObject->data()->name().c_str());
107         return aTitle + " = " + aVal;
108       }
109       return myObject->data()->name().c_str();
110     }
111     break;
112   case Qt::DecorationRole:
113     switch (theColumn) {
114     case 0:
115       switch (visibilityState()) {
116       case NoneState:
117         return QIcon();
118       case Visible:
119         return QIcon(":pictures/eyeopen.png");
120       case SemiVisible:
121         return QIcon(":pictures/eyemiclosed.png");
122       case Hidden:
123         return QIcon(":pictures/eyeclosed.png");
124       }
125     case 1:
126       return ModuleBase_IconFactory::get()->getIcon(myObject);
127     case 2:
128       if (isCurrentFeature(myObject))
129         return QIcon(":pictures/arrow.png");
130       else
131         return QIcon();
132     }
133   }
134   return PartSet_TreeNode::data(theColumn, theRole);
135 }
136
137 Qt::ItemFlags PartSet_ObjectNode::flags(int theColumn) const
138 {
139   if (myObject->isDisabled()) {
140     return (theColumn == 2) ? Qt::ItemIsSelectable : aNullFlag;
141   } else {
142     DocumentPtr aDoc = myObject->document();
143     SessionPtr aSession = ModelAPI_Session::get();
144     if (aSession->activeDocument() == aDoc)
145       return aEditingFlag;
146   }
147   return aDefaultFlag;
148 }
149
150 PartSet_ObjectNode::VisibilityState PartSet_ObjectNode::visibilityState() const
151 {
152   Qt::ItemFlags aFlags = flags(1);
153   if (aFlags == Qt::ItemFlags())
154     return NoneState;
155
156   if (myObject->groupName() == ModelAPI_ResultParameter::group())
157     return NoneState;
158   ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
159   if (aResObj.get()) {
160     ModuleBase_IWorkshop* aWork = workshop();
161     ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResObj);
162     if (aCompRes.get()) {
163       std::list<ResultPtr> aResultsList;
164       ModelAPI_Tools::allSubs(aCompRes, aResultsList);
165       VisibilityState aState = aResultsList.size() == 0 ?
166         (aWork->isVisible(aCompRes) ? Visible : Hidden) : NoneState;
167
168       std::list<ResultPtr>::const_iterator aIt;
169       ResultBodyPtr aCompSub;
170       for (aIt = aResultsList.cbegin(); aIt != aResultsList.cend(); aIt++) {
171         ResultPtr aSubRes = (*aIt);
172         aCompSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aSubRes);
173         if (!(aCompSub.get() && aCompSub->numberOfSubs() > 0)) {
174           VisibilityState aS = aWork->isVisible(aSubRes) ? Visible : Hidden;
175           if (aState == NoneState)
176             aState = aS;
177           else if (aState != aS) {
178             aState = SemiVisible;
179             break;
180           }
181         }
182       }
183       return aState;
184     } else {
185       if (aWork->isVisible(aResObj))
186         return Visible;
187       else
188         return Hidden;
189     }
190   }
191   return NoneState;
192 }
193
194 int PartSet_ObjectNode::numberOfSubs() const
195 {
196   ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
197   if (aCompRes.get())
198    return aCompRes->numberOfSubs(true);
199   else {
200     CompositeFeaturePtr aCompFeature =
201       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myObject);
202     if (aCompFeature.get() && aCompFeature->data()->isValid())
203       return aCompFeature->numberOfSubs(true);
204     else {
205       ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
206       if (aFieldRes.get())
207         return aFieldRes->stepsSize();
208     }
209   }
210   return 0;
211 }
212
213
214 ObjectPtr PartSet_ObjectNode::subObject(int theId) const
215 {
216   ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
217   if (aCompRes.get())
218     return aCompRes->subResult(theId, true);
219   else {
220     CompositeFeaturePtr aCompFeature =
221       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myObject);
222     if (aCompFeature.get())
223       return aCompFeature->subFeature(theId, true);
224   }
225   return ObjectPtr();
226 }
227
228 void PartSet_ObjectNode::update()
229 {
230   int aNb = numberOfSubs();
231   if (aNb > 0) {
232     ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
233
234     // If the object is a field result then delete extra sub-objects
235     if (aFieldRes.get()) {
236       while (myChildren.size() > aNb) {
237         ModuleBase_ITreeNode* aNode = myChildren.last();
238         myChildren.removeAll(aNode);
239         delete aNode;
240       }
241     }
242     else {
243       ObjectPtr aObj;
244       ModuleBase_ITreeNode* aNode;
245       int aId = 0;
246       while (aId < myChildren.size()) {
247         aNode = myChildren.at(aId);
248         aObj = subObject(aId);
249         if (aNode->object() != aObj) {
250           myChildren.removeAll(aNode);
251           delete aNode;
252         }
253         else
254           aId++;
255       }
256     }
257
258     ModuleBase_ITreeNode* aNode;
259     ObjectPtr aBody;
260     int i;
261     for (i = 0; i < aNb; i++) {
262       aBody = subObject(i);
263       if (aBody.get()) {
264         if (i < myChildren.size()) {
265           aNode = myChildren.at(i);
266           if (aNode->object() != aBody) {
267             ((PartSet_ObjectNode*)aNode)->setObject(aBody);
268           }
269         }
270         else {
271           aNode = new PartSet_ObjectNode(aBody, this);
272           myChildren.append(aNode);
273         }
274       }
275       else if (aFieldRes.get()) {
276         ModelAPI_ResultField::ModelAPI_FieldStep* aStep = aFieldRes->step(i);
277         if (i < myChildren.size()) {
278           PartSet_StepNode* aStepNode = static_cast<PartSet_StepNode*>(myChildren.at(i));
279           if (aStepNode->entity() != aStep) {
280             aStepNode->setEntity(aStep);
281           }
282         }
283         else {
284           aNode = new PartSet_StepNode(aStep, this);
285           myChildren.append(aNode);
286         }
287       }
288     }
289     // Delete extra objects
290     while (myChildren.size() > aNb) {
291       aNode = myChildren.takeLast();
292       delete aNode;
293     }
294     foreach(ModuleBase_ITreeNode* aNode, myChildren) {
295       aNode->update();
296     }
297   }
298   else {
299     deleteChildren();
300   }
301 }
302
303 QTreeNodesList PartSet_ObjectNode::objectCreated(const QObjectPtrList& theObjects)
304 {
305   QTreeNodesList aResult;
306   int aNb = numberOfSubs();
307   if (aNb > 0) {
308     ModuleBase_ITreeNode* aNode;
309     ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
310     ObjectPtr aBody;
311     int i;
312     for (i = 0; i < aNb; i++) {
313       aBody = subObject(i);
314       if (aBody.get()) {
315         if (i < myChildren.size()) {
316           aNode = myChildren.at(i);
317           if (aNode->object() != aBody) {
318             ((PartSet_ObjectNode*)aNode)->setObject(aBody);
319             aResult.append(aNode);
320           }
321         }
322         else {
323           aNode = new PartSet_ObjectNode(aBody, this);
324           myChildren.append(aNode);
325           aResult.append(aNode);
326           aNode->update();
327         }
328       }
329       else {
330         ModelAPI_ResultField::ModelAPI_FieldStep* aStep = aFieldRes->step(i);
331         if (i < myChildren.size()) {
332           PartSet_StepNode* aStepNode = static_cast<PartSet_StepNode*>(myChildren.at(i));
333           if (aStepNode->entity() != aStep) {
334             aStepNode->setEntity(aStep);
335           }
336         }
337         else {
338           aNode = new PartSet_StepNode(aStep, this);
339           myChildren.append(aNode);
340         }
341       }
342     }
343     foreach(ModuleBase_ITreeNode* aNode, myChildren) {
344       aResult.append(aNode->objectCreated(theObjects));
345     }
346   }
347   return aResult;
348 }
349
350 QTreeNodesList PartSet_ObjectNode::objectsDeleted(
351   const DocumentPtr& theDoc, const QString& theGroup)
352 {
353   QTreeNodesList aResult;
354   int aNb = numberOfSubs();
355   if (aNb != myChildren.size()) {
356     if (aNb == 0) {
357       deleteChildren();
358       aResult.append(this);
359     }
360     else {
361       // Delete extra objects
362       bool isDeleted = false;
363       ObjectPtr aObj;
364       ModuleBase_ITreeNode* aNode;
365       int aId = 0;
366       while (aId < myChildren.size()) {
367         aNode = myChildren.at(aId);
368         aObj = subObject(aId);
369         if (aNode->object() != aObj) {
370           myChildren.removeAll(aNode);
371           delete aNode;
372           isDeleted = true;
373         }
374         else
375           aId++;
376       }
377       if (isDeleted)
378         aResult.append(this);
379       int i = 0;
380       ObjectPtr aBody;
381       foreach(ModuleBase_ITreeNode* aNode, myChildren) {
382         aBody = subObject(i);
383         ((PartSet_ObjectNode*)aNode)->setObject(aBody);
384         aResult.append(aNode->objectsDeleted(theDoc, theGroup));
385         i++;
386       }
387     }
388   }
389   return aResult;
390 }
391 //////////////////////////////////////////////////////////////////////////////////
392 PartSet_FolderNode::PartSet_FolderNode(ModuleBase_ITreeNode* theParent,
393   FolderType theType)
394   : PartSet_TreeNode(theParent), myType(theType)
395 {
396 }
397
398 QString PartSet_FolderNode::name() const
399 {
400   switch (myType) {
401   case ParametersFolder:
402     return QObject::tr("Parameters");
403   case ConstructionFolder:
404     return QObject::tr("Constructions");
405   case PartsFolder:
406     return QObject::tr("Parts");
407   case ResultsFolder:
408     return QObject::tr("Results");
409   case FieldsFolder:
410     return QObject::tr("Fields");
411   case GroupsFolder:
412     return QObject::tr("Groups");
413   }
414   return "NoName";
415 }
416
417
418 QVariant PartSet_FolderNode::data(int theColumn, int theRole) const
419 {
420   static QIcon aParamsIco(":pictures/params_folder.png");
421   static QIcon aConstrIco(":pictures/constr_folder.png");
422
423   if (theColumn == 1) {
424     switch (theRole) {
425     case Qt::DisplayRole:
426       return name() + QString(" (%1)").arg(childrenCount());
427     case Qt::DecorationRole:
428       switch (myType) {
429       case ParametersFolder:
430         return aParamsIco;
431       case ConstructionFolder:
432         return aConstrIco;
433       case PartsFolder:
434         return aConstrIco;
435       case ResultsFolder:
436         return aConstrIco;
437       case FieldsFolder:
438         return aConstrIco;
439       case GroupsFolder:
440         return aConstrIco;
441       }
442     }
443   }
444   if ((theColumn == 2) && (theRole == Qt::DecorationRole)) {
445     if (document().get()) {
446       SessionPtr aSession = ModelAPI_Session::get();
447       if (document() != aSession->activeDocument())
448           return QIcon();
449
450       FeaturePtr aFeature = document()->currentFeature(true);
451       if (!aFeature.get()) { // There is no current feature
452         ModuleBase_ITreeNode* aLastFolder = 0;
453         foreach(ModuleBase_ITreeNode* aNode, parent()->children()) {
454           if (aNode->type() == PartSet_FolderNode::typeId())
455             aLastFolder = aNode;
456           else
457             break;
458         }
459         if (aLastFolder == this)
460           return QIcon(":pictures/arrow.png");
461         else
462           return QIcon();
463       }
464     }
465   }
466   return PartSet_TreeNode::data(theColumn, theRole);
467 }
468
469 Qt::ItemFlags PartSet_FolderNode::flags(int theColumn) const
470 {
471   SessionPtr aSession = ModelAPI_Session::get();
472   DocumentPtr aActiveDoc = aSession->activeDocument();
473   if (theColumn == 1) {
474     if (document() == aActiveDoc)
475       return aEditingFlag;
476   }
477   return aDefaultFlag;
478 }
479
480 ModuleBase_ITreeNode* PartSet_FolderNode::createNode(const ObjectPtr& theObj)
481 {
482   //ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObj);
483   //if (aCompRes.get())
484   //  return new PartSet_CompsolidNode(theObj, this);
485   return new PartSet_ObjectNode(theObj, this);
486 }
487
488 void PartSet_FolderNode::update()
489 {
490   DocumentPtr aDoc = document();
491   if (!aDoc.get())
492     return;
493
494   // Remove extra sub-nodes
495   int aIndex;
496   int aId = 0;
497   while (aId < myChildren.size()) {
498     ModuleBase_ITreeNode* aNode = myChildren.at(aId);
499     aIndex = aDoc->index(aNode->object(), true);
500     if ((aIndex == -1) || (aId != aIndex)) {
501       myChildren.removeAll(aNode);
502       delete aNode;
503     } else
504       aId++;
505   }
506
507   // Add new nodes
508   std::string aGroup = groupName();
509   int aSize = aDoc->size(aGroup, true);
510   for (int i = 0; i < aSize; i++) {
511     ObjectPtr aObj = aDoc->object(aGroup, i, true);
512     if (i < myChildren.size()) {
513       if (myChildren.at(i)->object() != aObj) {
514         ModuleBase_ITreeNode* aNode = createNode(aObj);
515         myChildren.insert(i, aNode);
516       }
517     } else {
518       ModuleBase_ITreeNode* aNode = createNode(aObj);
519       myChildren.append(aNode);
520     }
521   }
522
523   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
524     aNode->update();
525   }
526 }
527
528 std::string PartSet_FolderNode::groupName() const
529 {
530   switch (myType) {
531   case ParametersFolder:
532     return ModelAPI_ResultParameter::group();
533   case ConstructionFolder:
534     return ModelAPI_ResultConstruction::group();
535   case PartsFolder:
536     return ModelAPI_ResultPart::group();
537   case ResultsFolder:
538     return ModelAPI_ResultBody::group();
539   case FieldsFolder:
540     return ModelAPI_ResultField::group();
541   case GroupsFolder:
542     return ModelAPI_ResultGroup::group();
543   }
544   return "";
545 }
546
547 QTreeNodesList PartSet_FolderNode::objectCreated(const QObjectPtrList& theObjects)
548 {
549   QTreeNodesList aResult;
550   std::string aName = groupName();
551   DocumentPtr aDoc = document();
552   int aIdx = -1;
553   QMap<int, ModuleBase_ITreeNode*> aNewNodes;
554   foreach(ObjectPtr aObj, theObjects) {
555     if ((aObj->document() == aDoc) && (aObj->groupName() == aName)) {
556       aIdx = aDoc->index(aObj, true);
557       if (aIdx != -1) {
558         bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
559         if (!aHasObject) {
560           ModuleBase_ITreeNode* aNode = createNode(aObj);
561           aNewNodes[aIdx] = aNode;
562           aResult.append(aNode);
563           aNode->update();
564         }
565       }
566     }
567   }
568   // Add nodes in correct order
569   int i;
570   for (i = 0; i < myChildren.size(); i++) {
571     if (aNewNodes.contains(i)) {
572       myChildren.insert(i, aNewNodes[i]);
573       aNewNodes.remove(i);
574     }
575   }
576   while (aNewNodes.size()) {
577     i = myChildren.size();
578     myChildren.append(aNewNodes[i]);
579     aNewNodes.remove(i);
580   }
581   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
582     aResult.append(aNode->objectCreated(theObjects));
583   }
584   return aResult;
585 }
586
587 QTreeNodesList PartSet_FolderNode::objectsDeleted(const DocumentPtr& theDoc,
588   const QString& theGroup)
589 {
590   DocumentPtr aDoc = document();
591   QTreeNodesList aResult;
592   if ((theGroup.toStdString() == groupName()) && (theDoc == aDoc)) {
593     QTreeNodesList aDelList;
594     int aIndex;
595     int aId = 0;
596     bool aRemoved = false;
597     bool aToSort = false;
598     while (aId < myChildren.size()) {
599       ModuleBase_ITreeNode* aNode = myChildren.at(aId);
600       aIndex = aDoc->index(aNode->object(), true);
601       aToSort |= ((aIndex != -1) && (aId != aIndex));
602       if (aIndex == -1) {
603         myChildren.removeAll(aNode);
604         delete aNode;
605         aRemoved = true;
606       }
607       else
608         aId++;
609     }
610     if (aRemoved)
611       aResult.append(this);
612     if (aToSort)
613       sortChildren();
614     foreach(ModuleBase_ITreeNode* aNode, myChildren) {
615       aResult.append(aNode->objectsDeleted(theDoc, theGroup));
616     }
617   }
618   return aResult;
619 }
620
621 //////////////////////////////////////////////////////////////////////////////////
622 QTreeNodesList PartSet_FeatureFolderNode::objectCreated(const QObjectPtrList& theObjects)
623 {
624   QTreeNodesList aResult;
625   // Process the root sub-objects
626   DocumentPtr aDoc = document();
627   int aIdx = -1;
628   int aNb = numberOfFolders();
629   QMap<int, ModuleBase_ITreeNode*> aNewNodes;
630   foreach(ObjectPtr aObj, theObjects) {
631     if (aDoc == aObj->document()) {
632       if ((aObj->groupName() == ModelAPI_Feature::group()) ||
633         (aObj->groupName() == ModelAPI_Folder::group())){
634         aIdx = aDoc->index(aObj, true);
635         if (aIdx != -1) {
636           ModuleBase_ITreeNode* aNode = createNode(aObj);
637           aIdx += aNb;
638           bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
639           if (!aHasObject) {
640             aNewNodes[aIdx] = aNode;
641             aResult.append(aNode);
642             aNode->update();
643           }
644         }
645       }
646     }
647   }
648   // To add in correct order
649   int i;
650   for (i = 0; i < myChildren.size(); i++) {
651     if (aNewNodes.contains(i)) {
652       myChildren.insert(i, aNewNodes[i]);
653       aNewNodes.remove(i);
654     }
655   }
656   while (aNewNodes.size()) {
657     i = myChildren.size();
658     myChildren.append(aNewNodes[i]);
659     aNewNodes.remove(i);
660   }
661
662   // Update sub-folders
663   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
664     aResult.append(aNode->objectCreated(theObjects));
665   }
666   return aResult;
667 }
668
669 QTreeNodesList PartSet_FeatureFolderNode::objectsDeleted(const DocumentPtr& theDoc,
670   const QString& theGroup)
671 {
672   QTreeNodesList aResult;
673
674   // Process sub-folders
675   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
676     if (aNode->childrenCount() > 0) { // aFolder node
677       QTreeNodesList aList = aNode->objectsDeleted(theDoc, theGroup);
678       if (aList.size() > 0)
679         aResult.append(aList);
680     }
681   }
682
683   // Process root
684   DocumentPtr aDoc = document();
685   int aNb = numberOfFolders();
686   bool isGroup = ((theGroup.toStdString() == ModelAPI_Feature::group()) ||
687     (theGroup.toStdString() == ModelAPI_Folder::group()));
688   if ((theDoc == aDoc) && isGroup) {
689     int aIndex;
690     int aId = 0;
691     bool aRemoved = false;
692     bool aToSort = false;
693     while (aId < myChildren.size()) {
694       ModuleBase_ITreeNode* aNode = myChildren.at(aId);
695       if (aNode->object().get()) {
696         aIndex = aDoc->index(aNode->object(), true);
697         aToSort |= ((aIndex != -1) && (aId != (aIndex + aNb)));
698         if (aIndex == -1) {
699           myChildren.removeAll(aNode);
700           delete aNode;
701           aRemoved = true;
702           continue;
703         }
704       }
705       aId++;
706     }
707     if (aRemoved)
708       aResult.append(this);
709     if (aToSort)
710       sortChildren();
711   }
712   return aResult;
713 }
714
715 ModuleBase_ITreeNode* PartSet_FeatureFolderNode::findParent(const DocumentPtr& theDoc,
716   QString theGroup)
717 {
718   ModuleBase_ITreeNode* aResult = 0;
719   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
720     aResult = aNode->findParent(theDoc, theGroup);
721     if (aResult) {
722       return aResult;
723     }
724   }
725   bool isGroup = ((theGroup.toStdString() == ModelAPI_Feature::group()) ||
726     (theGroup.toStdString() == ModelAPI_Folder::group()));
727   if ((theDoc == document()) && isGroup)
728     return this;
729   return 0;
730 }
731
732
733 //////////////////////////////////////////////////////////////////////////////////
734 PartSet_RootNode::PartSet_RootNode() : PartSet_FeatureFolderNode(0), myWorkshop(0)
735 {
736   SessionPtr aSession = ModelAPI_Session::get();
737   DocumentPtr aDoc = aSession->moduleDocument();
738
739   myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
740   myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
741   myPartsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::PartsFolder);
742
743   myChildren.append(myParamsFolder);
744   myChildren.append(myConstrFolder);
745   myChildren.append(myPartsFolder);
746
747   update();
748 }
749
750
751 void PartSet_RootNode::update()
752 {
753   myParamsFolder->update();
754   myConstrFolder->update();
755   myPartsFolder->update();
756
757   // Update features content
758   DocumentPtr aDoc = document();
759   int aNb = numberOfFolders();
760
761   // Remove extra sub-nodes
762   int aIndex;
763   int aId = 0;
764   while (aId < myChildren.size()) {
765     ModuleBase_ITreeNode* aNode = myChildren.at(aId);
766     if (aNode->object().get()) {
767       aIndex = aDoc->index(aNode->object(), true);
768       if ((aIndex == -1) || (aId != (aIndex + aNb))) {
769         myChildren.removeAll(aNode);
770         delete aNode;
771         continue;
772       }
773     }
774     aId++;
775   }
776
777   // Add new nodes
778   std::string aGroup = ModelAPI_Feature::group();
779   int aSize = aDoc->size(aGroup, true);
780   FeaturePtr aFeature;
781   for (int i = 0; i < aSize; i++) {
782     ObjectPtr aObj = aDoc->object(aGroup, i, true);
783     aId = i + aNb; // Take into account existing folders
784     if (aId < myChildren.size()) {
785       if (myChildren.at(aId)->object() != aObj) {
786         ModuleBase_ITreeNode* aNode = createNode(aObj);
787         myChildren.insert(aId, aNode);
788       }
789     } else {
790       ModuleBase_ITreeNode* aNode = createNode(aObj);
791       myChildren.append(aNode);
792     }
793   }
794   // Update sub-folders
795   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
796     if ((aNode->type() == PartSet_ObjectFolderNode::typeId()) ||
797       (aNode->type() == PartSet_PartRootNode::typeId()))
798       aNode->update();
799   }
800 }
801
802 DocumentPtr PartSet_RootNode::document() const
803 {
804   return ModelAPI_Session::get()->moduleDocument();
805 }
806
807 ModuleBase_ITreeNode* PartSet_RootNode::createNode(const ObjectPtr& theObj)
808 {
809   if (theObj->groupName() == ModelAPI_Folder::group())
810     return new PartSet_ObjectFolderNode(theObj, this);
811
812   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
813   if (aFeature->getKind() == PartSetPlugin_Part::ID())
814     return new PartSet_PartRootNode(theObj, this);
815
816   return new PartSet_ObjectNode(theObj, this);
817 }
818
819 //////////////////////////////////////////////////////////////////////////////////
820 PartSet_PartRootNode::PartSet_PartRootNode(const ObjectPtr& theObj, ModuleBase_ITreeNode* theParent)
821   : PartSet_FeatureFolderNode(theParent), myObject(theObj)
822 {
823   myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
824   myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
825   myResultsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ResultsFolder);
826   myFieldsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::FieldsFolder);
827   myGroupsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::GroupsFolder);
828
829   myChildren.append(myParamsFolder);
830   myChildren.append(myConstrFolder);
831   myChildren.append(myResultsFolder);
832
833   update();
834 }
835
836 void PartSet_PartRootNode::deleteChildren()
837 {
838   if (!myFieldsFolder->childrenCount()) {
839     delete myFieldsFolder;
840   }
841   if (!myGroupsFolder->childrenCount()) {
842     delete myGroupsFolder;
843   }
844   PartSet_FeatureFolderNode::deleteChildren();
845 }
846
847
848 void PartSet_PartRootNode::update()
849 {
850   DocumentPtr aDoc = document();
851   if (!aDoc.get())
852     return;
853
854   myParamsFolder->update();
855   myConstrFolder->update();
856   myResultsFolder->update();
857   myFieldsFolder->update();
858   myGroupsFolder->update();
859
860   bool aHasFields = myFieldsFolder->childrenCount() > 0;
861   bool aHasGroups = myGroupsFolder->childrenCount() > 0;
862   if (aHasFields) {
863     if (!myChildren.contains(myFieldsFolder)) {
864       myChildren.insert(3, myFieldsFolder);
865     }
866   } else if (myChildren.contains(myFieldsFolder)) {
867     myChildren.removeAll(myFieldsFolder);
868   }
869   if (aHasGroups) {
870     if (!myChildren.contains(myGroupsFolder)) {
871       myChildren.insert(aHasFields ? 4 : 3, myGroupsFolder);
872     }
873   } else if (myChildren.contains(myGroupsFolder)) {
874     myChildren.removeAll(myGroupsFolder);
875   }
876
877   // Update features content
878   int aRows = numberOfFolders();
879
880   // Remove extra sub-nodes
881   int aIndex = -1;
882   int aId = aRows;
883   while (aId < myChildren.size()) {
884     ModuleBase_ITreeNode* aNode = myChildren.at(aId);
885     if (aNode->object().get()) {
886       aIndex = aDoc->index(aNode->object(), true);
887       if ((aIndex == -1) || (aId != (aIndex + aRows))) {
888         myChildren.removeAll(aNode);
889         delete aNode;
890         continue;
891       }
892     }
893     aId++;
894   }
895
896   std::string aGroup = ModelAPI_Feature::group();
897   int aSize = aDoc->size(aGroup, true);
898   FeaturePtr aFeature;
899   for (int i = 0; i < aSize; i++) {
900     ObjectPtr aObj = aDoc->object(aGroup, i, true);
901     aId = i + aRows; // Take into account existing folders
902     if (aId < myChildren.size()) {
903       if (myChildren.at(aId)->object() != aObj) {
904         ModuleBase_ITreeNode* aNode = createNode(aObj);
905         myChildren.insert(aId, aNode);
906       }
907     } else {
908       ModuleBase_ITreeNode* aNode = createNode(aObj);
909       myChildren.append(aNode);
910     }
911   }
912   // Update sub-folders
913   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
914     if (aNode->type() == PartSet_ObjectFolderNode::typeId())
915       aNode->update();
916   }
917 }
918
919 DocumentPtr PartSet_PartRootNode::document() const
920 {
921   ResultPartPtr aPartRes = getPartResult(myObject);
922   if (aPartRes.get())
923     return aPartRes->partDoc();
924   return DocumentPtr();
925 }
926
927 QVariant PartSet_PartRootNode::data(int theColumn, int theRole) const
928 {
929   switch (theColumn) {
930   case 1:
931     switch (theRole) {
932     case Qt::DisplayRole:
933     {
934       ResultPartPtr aPartRes = getPartResult(myObject);
935       if (aPartRes.get()) {
936         if (aPartRes->partDoc().get() == NULL)
937           return QString(myObject->data()->name().c_str()) + " (Not loaded)";
938       }
939       return QString(myObject->data()->name().c_str());
940     }
941     case Qt::DecorationRole:
942       return ModuleBase_IconFactory::get()->getIcon(myObject);
943     }
944   case 2:
945     if (theRole == Qt::DecorationRole) {
946       if (isCurrentFeature(myObject))
947         return QIcon(":pictures/arrow.png");
948       else
949         return QIcon();
950     }
951   }
952   return PartSet_TreeNode::data(theColumn, theRole);
953 }
954
955 Qt::ItemFlags PartSet_PartRootNode::flags(int theColumn) const
956 {
957   if (myObject->isDisabled())
958     return (theColumn == 2) ? Qt::ItemIsSelectable : aNullFlag;
959
960   SessionPtr aSession = ModelAPI_Session::get();
961   DocumentPtr aActiveDoc = aSession->activeDocument();
962   if ((aActiveDoc == document()) || (myObject->document() == aActiveDoc))
963     return aEditingFlag;
964   return aDefaultFlag;
965 }
966
967 ModuleBase_ITreeNode* PartSet_PartRootNode::createNode(const ObjectPtr& theObj)
968 {
969   if (theObj->groupName() == ModelAPI_Folder::group())
970     return new PartSet_ObjectFolderNode(theObj, this);
971   return new PartSet_ObjectNode(theObj, this);
972 }
973
974 int PartSet_PartRootNode::numberOfFolders() const
975 {
976   int aNb = 3;
977   if (myFieldsFolder->childrenCount() > 0)
978     aNb++;
979   if (myGroupsFolder->childrenCount() > 0)
980     aNb++;
981   return aNb;
982 }
983
984 QTreeNodesList PartSet_PartRootNode::objectCreated(const QObjectPtrList& theObjects)
985 {
986   QTreeNodesList aResult = PartSet_FeatureFolderNode::objectCreated(theObjects);
987   if (!myFieldsFolder->childrenCount()) {
988     QTreeNodesList aList = myFieldsFolder->objectCreated(theObjects);
989     if (aList.size()) {
990       myChildren.insert(3, myFieldsFolder);
991       aResult.append(myFieldsFolder);
992       aResult.append(aList);
993     }
994   }
995   if (!myGroupsFolder->childrenCount()) {
996     QTreeNodesList aList = myGroupsFolder->objectCreated(theObjects);
997     if (aList.size()) {
998       myChildren.insert(myFieldsFolder->childrenCount()? 4 : 3, myGroupsFolder);
999       aResult.append(myGroupsFolder);
1000       aResult.append(aList);
1001     }
1002   }
1003   return aResult;
1004 }
1005
1006 QTreeNodesList PartSet_PartRootNode::objectsDeleted(const DocumentPtr& theDoc,
1007   const QString& theGroup)
1008 {
1009   QTreeNodesList aResult;
1010   if (myFieldsFolder->childrenCount()) {
1011     QTreeNodesList aList = myFieldsFolder->objectsDeleted(theDoc, theGroup);
1012     if (aList.size()) {
1013       aResult.append(aList);
1014       if (!myFieldsFolder->childrenCount())
1015         myChildren.removeAll(myFieldsFolder);
1016     }
1017   }
1018   if (myGroupsFolder->childrenCount()) {
1019     QTreeNodesList aList = myGroupsFolder->objectsDeleted(theDoc, theGroup);
1020     if (aList.size()) {
1021       aResult.append(aList);
1022       if (!myGroupsFolder->childrenCount())
1023         myChildren.removeAll(myGroupsFolder);
1024     }
1025   }
1026   aResult.append(PartSet_FeatureFolderNode::objectsDeleted(theDoc, theGroup));
1027   return aResult;
1028 }
1029
1030 //////////////////////////////////////////////////////////////////////////////////
1031 void PartSet_ObjectFolderNode::update()
1032 {
1033   int aFirst = -1, aLast = -1;
1034   PartSet_Tools::getFirstAndLastIndexInFolder(myObject, aFirst, aLast);
1035   if ((aFirst == -1) || (aLast == -1)) {
1036     deleteChildren();
1037     return;
1038   }
1039
1040   int aNbItems = aLast - aFirst + 1;
1041   if (!aNbItems) {
1042     deleteChildren();
1043     return;
1044   }
1045
1046   DocumentPtr aDoc = myObject->document();
1047   if (aNbItems < myChildren.size()) {
1048     // Delete obsolete nodes
1049     int aId = 0;
1050     int aNbOfFeatures = aDoc->size(ModelAPI_Feature::group(), true);
1051     while (aId < myChildren.size()) {
1052       ModuleBase_ITreeNode* aNode = myChildren.at(aId);
1053       if ((aFirst + aId) < aNbOfFeatures) {
1054         if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
1055           myChildren.removeAll(aNode);
1056           delete aNode;
1057           continue;
1058         }
1059       }
1060       else {
1061         myChildren.removeAll(aNode);
1062         delete aNode;
1063         continue;
1064       }
1065       aId++;
1066     }
1067   }
1068   if (aNbItems > myChildren.size()) {
1069     // Add new nodes
1070     ModuleBase_ITreeNode* aNode;
1071     for (int i = 0; i < aNbItems; i++) {
1072       ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
1073       if (i < myChildren.size()) {
1074         if (aObj != myChildren.at(i)->object()) {
1075           aNode = new PartSet_ObjectNode(aObj, this);
1076           myChildren.insert(i, aNode);
1077         }
1078       }
1079       else {
1080         aNode = new PartSet_ObjectNode(aObj, this);
1081         myChildren.append(aNode);
1082       }
1083     }
1084   }
1085 }
1086
1087 QTreeNodesList PartSet_ObjectFolderNode::objectCreated(const QObjectPtrList& theObjects)
1088 {
1089   QTreeNodesList aResult;
1090   int aFirst = -1, aLast = -1;
1091   PartSet_Tools::getFirstAndLastIndexInFolder(myObject, aFirst, aLast);
1092   if ((aFirst == -1) || (aLast == -1)) {
1093     return aResult;
1094   }
1095   int aNbItems = aLast - aFirst + 1;
1096   if (!aNbItems) {
1097     return aResult;
1098   }
1099   DocumentPtr aDoc = myObject->document();
1100   // Add new nodes
1101   ModuleBase_ITreeNode* aNode;
1102   for (int i = 0; i < aNbItems; i++) {
1103     ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
1104     if (i < myChildren.size()) {
1105       if (aObj != myChildren.at(i)->object()) {
1106         aNode = new PartSet_ObjectNode(aObj, this);
1107         myChildren.insert(i, aNode);
1108         aResult.append(aNode);
1109       }
1110     } else {
1111       aNode = new PartSet_ObjectNode(aObj, this);
1112       myChildren.append(aNode);
1113       aResult.append(aNode);
1114     }
1115   }
1116   return aResult;
1117 }
1118
1119 QTreeNodesList PartSet_ObjectFolderNode::objectsDeleted(const DocumentPtr& theDoc,
1120   const QString& theGroup)
1121 {
1122   QTreeNodesList aResult;
1123   int aFirst = -1, aLast = -1;
1124   PartSet_Tools::getFirstAndLastIndexInFolder(myObject, aFirst, aLast);
1125   if ((aFirst == -1) || (aLast == -1)) {
1126     return aResult;
1127   }
1128   int aNbItems = aLast - aFirst + 1;
1129   if (!aNbItems) {
1130     return aResult;
1131   }
1132   if (aNbItems >= myChildren.size()) // Nothing was deleted here
1133     return aResult;
1134
1135   DocumentPtr aDoc = myObject->document();
1136   // Delete obsolete nodes
1137   bool aRemoved = false;
1138   int aId = 0;
1139   int aNbOfFeatures = aDoc->size(ModelAPI_Feature::group(), true);
1140   while (aId < myChildren.size()) {
1141     ModuleBase_ITreeNode* aNode = myChildren.at(aId);
1142     if ((aFirst + aId) < aNbOfFeatures) {
1143       if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
1144         myChildren.removeAll(aNode);
1145         delete aNode;
1146         aRemoved = true;
1147         continue;
1148       }
1149     }
1150     else {
1151       myChildren.removeAll(aNode);
1152       aResult.removeAll(aNode);
1153       delete aNode;
1154       aRemoved = true;
1155       continue;
1156     }
1157     aId++;
1158   }
1159   if (aRemoved) {
1160     aResult.append(this);
1161   }
1162   return aResult;
1163 }
1164
1165
1166
1167 //////////////////////////////////////////////////////////////////////////////////
1168 QVariant PartSet_StepNode::data(int theColumn, int theRole) const
1169 {
1170   if ((theColumn == 1) && (theRole == Qt::DisplayRole)) {
1171     ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
1172       dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>(myEntity);
1173
1174     return "Step " + QString::number(aStep->id() + 1) + " " +
1175       aStep->field()->textLine(aStep->id()).c_str();
1176   }
1177   return PartSet_TreeNode::data(theColumn, theRole);
1178 }