Salome HOME
Apply warning icon to folder only in case if it is not expanded
[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 && (!myChildren.contains(myFieldsFolder))) {
863     myChildren.insert(3, myFieldsFolder);
864   }
865   if (aHasGroups && (!myChildren.contains(myGroupsFolder))) {
866     myChildren.insert(aHasFields ? 4 : 3, myGroupsFolder);
867   }
868
869   // Update features content
870   int aRows = numberOfFolders();
871
872   // Remove extra sub-nodes
873   int aIndex = -1;
874   int aId = aRows;
875   while (aId < myChildren.size()) {
876     ModuleBase_ITreeNode* aNode = myChildren.at(aId);
877     if (aNode->object().get()) {
878       aIndex = aDoc->index(aNode->object(), true);
879       if ((aIndex == -1) || (aId != (aIndex + aRows))) {
880         myChildren.removeAll(aNode);
881         delete aNode;
882         continue;
883       }
884     }
885     aId++;
886   }
887
888   std::string aGroup = ModelAPI_Feature::group();
889   int aSize = aDoc->size(aGroup, true);
890   FeaturePtr aFeature;
891   for (int i = 0; i < aSize; i++) {
892     ObjectPtr aObj = aDoc->object(aGroup, i, true);
893     aId = i + aRows; // Take into account existing folders
894     if (aId < myChildren.size()) {
895       if (myChildren.at(aId)->object() != aObj) {
896         ModuleBase_ITreeNode* aNode = createNode(aObj);
897         myChildren.insert(aId, aNode);
898       }
899     } else {
900       ModuleBase_ITreeNode* aNode = createNode(aObj);
901       myChildren.append(aNode);
902     }
903   }
904   // Update sub-folders
905   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
906     if (aNode->type() == PartSet_ObjectFolderNode::typeId())
907       aNode->update();
908   }
909 }
910
911 DocumentPtr PartSet_PartRootNode::document() const
912 {
913   ResultPartPtr aPartRes = getPartResult(myObject);
914   if (aPartRes.get())
915     return aPartRes->partDoc();
916   return DocumentPtr();
917 }
918
919 QVariant PartSet_PartRootNode::data(int theColumn, int theRole) const
920 {
921   switch (theColumn) {
922   case 1:
923     switch (theRole) {
924     case Qt::DisplayRole:
925     {
926       ResultPartPtr aPartRes = getPartResult(myObject);
927       if (aPartRes.get()) {
928         if (aPartRes->partDoc().get() == NULL)
929           return QString(myObject->data()->name().c_str()) + " (Not loaded)";
930       }
931       return QString(myObject->data()->name().c_str());
932     }
933     case Qt::DecorationRole:
934       return ModuleBase_IconFactory::get()->getIcon(myObject);
935     }
936   case 2:
937     if (theRole == Qt::DecorationRole) {
938       if (isCurrentFeature(myObject))
939         return QIcon(":pictures/arrow.png");
940       else
941         return QIcon();
942     }
943   }
944   return PartSet_TreeNode::data(theColumn, theRole);
945 }
946
947 Qt::ItemFlags PartSet_PartRootNode::flags(int theColumn) const
948 {
949   if (myObject->isDisabled())
950     return (theColumn == 2) ? Qt::ItemIsSelectable : aDefaultFlag;
951
952   SessionPtr aSession = ModelAPI_Session::get();
953   DocumentPtr aActiveDoc = aSession->activeDocument();
954   if ((aActiveDoc == document()) || (myObject->document() == aActiveDoc))
955     return aEditingFlag;
956   return aDefaultFlag;
957 }
958
959 ModuleBase_ITreeNode* PartSet_PartRootNode::createNode(const ObjectPtr& theObj)
960 {
961   if (theObj->groupName() == ModelAPI_Folder::group())
962     return new PartSet_ObjectFolderNode(theObj, this);
963   return new PartSet_ObjectNode(theObj, this);
964 }
965
966 int PartSet_PartRootNode::numberOfFolders() const
967 {
968   int aNb = 3;
969   if (myFieldsFolder->childrenCount() > 0)
970     aNb++;
971   if (myGroupsFolder->childrenCount() > 0)
972     aNb++;
973   return aNb;
974 }
975
976 QTreeNodesList PartSet_PartRootNode::objectCreated(const QObjectPtrList& theObjects)
977 {
978   QTreeNodesList aResult = PartSet_FeatureFolderNode::objectCreated(theObjects);
979   if (!myFieldsFolder->childrenCount()) {
980     QTreeNodesList aList = myFieldsFolder->objectCreated(theObjects);
981     if (aList.size()) {
982       myChildren.insert(3, myFieldsFolder);
983       aResult.append(myFieldsFolder);
984       aResult.append(aList);
985     }
986   }
987   if (!myGroupsFolder->childrenCount()) {
988     QTreeNodesList aList = myGroupsFolder->objectCreated(theObjects);
989     if (aList.size()) {
990       myChildren.insert(myFieldsFolder->childrenCount()? 4 : 3, myGroupsFolder);
991       aResult.append(myGroupsFolder);
992       aResult.append(aList);
993     }
994   }
995   return aResult;
996 }
997
998 QTreeNodesList PartSet_PartRootNode::objectsDeleted(const DocumentPtr& theDoc,
999   const QString& theGroup)
1000 {
1001   QTreeNodesList aResult;
1002   if (myFieldsFolder->childrenCount()) {
1003     QTreeNodesList aList = myFieldsFolder->objectsDeleted(theDoc, theGroup);
1004     if (aList.size()) {
1005       aResult.append(aList);
1006       if (!myFieldsFolder->childrenCount())
1007         myChildren.removeAll(myFieldsFolder);
1008     }
1009   }
1010   if (myGroupsFolder->childrenCount()) {
1011     QTreeNodesList aList = myGroupsFolder->objectsDeleted(theDoc, theGroup);
1012     if (aList.size()) {
1013       aResult.append(aList);
1014       if (!myGroupsFolder->childrenCount())
1015         myChildren.removeAll(myGroupsFolder);
1016     }
1017   }
1018   aResult.append(PartSet_FeatureFolderNode::objectsDeleted(theDoc, theGroup));
1019   return aResult;
1020 }
1021
1022 //////////////////////////////////////////////////////////////////////////////////
1023 void PartSet_ObjectFolderNode::update()
1024 {
1025   int aFirst = -1, aLast = -1;
1026   PartSet_Tools::getFirstAndLastIndexInFolder(myObject, aFirst, aLast);
1027   if ((aFirst == -1) || (aLast == -1)) {
1028     deleteChildren();
1029     return;
1030   }
1031
1032   int aNbItems = aLast - aFirst + 1;
1033   if (!aNbItems) {
1034     deleteChildren();
1035     return;
1036   }
1037
1038   DocumentPtr aDoc = myObject->document();
1039   if (aNbItems < myChildren.size()) {
1040     // Delete obsolete nodes
1041     int aId = 0;
1042     int aNbOfFeatures = aDoc->size(ModelAPI_Feature::group(), true);
1043     while (aId < myChildren.size()) {
1044       ModuleBase_ITreeNode* aNode = myChildren.at(aId);
1045       if ((aFirst + aId) < aNbOfFeatures) {
1046         if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
1047           myChildren.removeAll(aNode);
1048           delete aNode;
1049           continue;
1050         }
1051       }
1052       else {
1053         myChildren.removeAll(aNode);
1054         delete aNode;
1055         continue;
1056       }
1057       aId++;
1058     }
1059   }
1060   if (aNbItems > myChildren.size()) {
1061     // Add new nodes
1062     ModuleBase_ITreeNode* aNode;
1063     for (int i = 0; i < aNbItems; i++) {
1064       ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
1065       if (i < myChildren.size()) {
1066         if (aObj != myChildren.at(i)->object()) {
1067           aNode = new PartSet_ObjectNode(aObj, this);
1068           myChildren.insert(i, aNode);
1069         }
1070       }
1071       else {
1072         aNode = new PartSet_ObjectNode(aObj, this);
1073         myChildren.append(aNode);
1074       }
1075     }
1076   }
1077 }
1078
1079 QTreeNodesList PartSet_ObjectFolderNode::objectCreated(const QObjectPtrList& theObjects)
1080 {
1081   QTreeNodesList aResult;
1082   int aFirst = -1, aLast = -1;
1083   PartSet_Tools::getFirstAndLastIndexInFolder(myObject, aFirst, aLast);
1084   if ((aFirst == -1) || (aLast == -1)) {
1085     return aResult;
1086   }
1087   int aNbItems = aLast - aFirst + 1;
1088   if (!aNbItems) {
1089     return aResult;
1090   }
1091   DocumentPtr aDoc = myObject->document();
1092   // Add new nodes
1093   ModuleBase_ITreeNode* aNode;
1094   for (int i = 0; i < aNbItems; i++) {
1095     ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
1096     if (i < myChildren.size()) {
1097       if (aObj != myChildren.at(i)->object()) {
1098         aNode = new PartSet_ObjectNode(aObj, this);
1099         myChildren.insert(i, aNode);
1100         aResult.append(aNode);
1101       }
1102     } else {
1103       aNode = new PartSet_ObjectNode(aObj, this);
1104       myChildren.append(aNode);
1105       aResult.append(aNode);
1106     }
1107   }
1108   return aResult;
1109 }
1110
1111 QTreeNodesList PartSet_ObjectFolderNode::objectsDeleted(const DocumentPtr& theDoc,
1112   const QString& theGroup)
1113 {
1114   QTreeNodesList aResult;
1115   int aFirst = -1, aLast = -1;
1116   PartSet_Tools::getFirstAndLastIndexInFolder(myObject, aFirst, aLast);
1117   if ((aFirst == -1) || (aLast == -1)) {
1118     return aResult;
1119   }
1120   int aNbItems = aLast - aFirst + 1;
1121   if (!aNbItems) {
1122     return aResult;
1123   }
1124   if (aNbItems >= myChildren.size()) // Nothing was deleted here
1125     return aResult;
1126
1127   DocumentPtr aDoc = myObject->document();
1128   // Delete obsolete nodes
1129   bool aRemoved = false;
1130   int aId = 0;
1131   int aNbOfFeatures = aDoc->size(ModelAPI_Feature::group(), true);
1132   while (aId < myChildren.size()) {
1133     ModuleBase_ITreeNode* aNode = myChildren.at(aId);
1134     if ((aFirst + aId) < aNbOfFeatures) {
1135       if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
1136         myChildren.removeAll(aNode);
1137         delete aNode;
1138         aRemoved = true;
1139         continue;
1140       }
1141     }
1142     else {
1143       myChildren.removeAll(aNode);
1144       delete aNode;
1145       aRemoved = true;
1146       continue;
1147     }
1148     aId++;
1149   }
1150   if (aRemoved) {
1151     aResult.append(this);
1152   }
1153   return aResult;
1154 }
1155
1156
1157
1158 //////////////////////////////////////////////////////////////////////////////////
1159 QVariant PartSet_StepNode::data(int theColumn, int theRole) const
1160 {
1161   if ((theColumn == 1) && (theRole == Qt::DisplayRole)) {
1162     ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
1163       dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>(myEntity);
1164
1165     return "Step " + QString::number(aStep->id() + 1) + " " +
1166       aStep->field()->textLine(aStep->id()).c_str();
1167   }
1168   return PartSet_TreeNode::data(theColumn, theRole);
1169 }