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