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