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