]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_TreeNodes.cpp
Salome HOME
Show Complex nodes
[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 void PartSet_ObjectNode::update()
186 {
187   ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
188   if (aCompRes.get()) {
189     int aNb = aCompRes->numberOfSubs(true);
190     ModuleBase_ITreeNode* aNode;
191     ResultBodyPtr aBody;
192     int i;
193     for (i = 0; i < aNb; i++) {
194       aBody = aCompRes->subResult(i, true);
195       if (i < myChildren.size()) {
196         aNode = myChildren.at(i);
197         if (aNode->object() != aBody) {
198           ((PartSet_ObjectNode*)aNode)->setObject(aBody);
199         }
200       } else {
201         aNode = new PartSet_ObjectNode(aBody, this);
202         myChildren.append(aNode);
203       }
204     }
205     // Delete extra objects
206     while (myChildren.size() > aNb) {
207       aNode = myChildren.takeLast();
208       delete aNode;
209     }
210   }
211 }
212
213 QTreeNodesList PartSet_ObjectNode::objectCreated(const QObjectPtrList& theObjects)
214 {
215   QTreeNodesList aResult;
216
217   ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
218   if (aCompRes.get()) {
219     int aNb = aCompRes->numberOfSubs(true);
220     ModuleBase_ITreeNode* aNode;
221     ResultBodyPtr aBody;
222     int i;
223     for (i = 0; i < aNb; i++) {
224       aBody = aCompRes->subResult(i, true);
225       if (i < myChildren.size()) {
226         aNode = myChildren.at(i);
227         if (aNode->object() != aBody) {
228           ((PartSet_ObjectNode*)aNode)->setObject(aBody);
229           aResult.append(aNode);
230         }
231       } else {
232         aNode = new PartSet_ObjectNode(aBody, this);
233         myChildren.append(aNode);
234         aResult.append(aNode);
235       }
236     }
237   }
238   return aResult;
239 }
240
241 QTreeNodesList PartSet_ObjectNode::objectsDeleted(const DocumentPtr& theDoc, const QString& theGroup)
242 {
243   QTreeNodesList aResult;
244   ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
245   if (aCompRes.get()) {
246     int aNb = aCompRes->numberOfSubs(true);
247     ModuleBase_ITreeNode* aNode;
248     // Delete extra objects
249     bool isDeleted = false;
250     while (myChildren.size() > aNb) {
251       aNode = myChildren.takeLast();
252       delete aNode;
253       isDeleted = true;
254     }
255     if (isDeleted)
256       aResult.append(this);
257   }
258   return aResult;
259 }
260 //////////////////////////////////////////////////////////////////////////////////
261 PartSet_FolderNode::PartSet_FolderNode(ModuleBase_ITreeNode* theParent,
262   FolderType theType)
263   : PartSet_TreeNode(theParent), myType(theType)
264 {
265 }
266
267 QString PartSet_FolderNode::name() const
268 {
269   switch (myType) {
270   case ParametersFolder:
271     return QObject::tr("Parameters");
272   case ConstructionFolder:
273     return QObject::tr("Constructions");
274   case PartsFolder:
275     return QObject::tr("Parts");
276   case ResultsFolder:
277     return QObject::tr("Results");
278   case FieldsFolder:
279     return QObject::tr("Fields");
280   case GroupsFolder:
281     return QObject::tr("Groups");
282   }
283   return "NoName";
284 }
285
286
287 QVariant PartSet_FolderNode::data(int theColumn, int theRole) const
288 {
289   static QIcon aParamsIco(":pictures/params_folder.png");
290   static QIcon aConstrIco(":pictures/constr_folder.png");
291
292   if (theColumn == 1) {
293     switch (theRole) {
294     case Qt::DisplayRole:
295       return name() + QString(" (%1)").arg(childrenCount());
296     case Qt::DecorationRole:
297       switch (myType) {
298       case ParametersFolder:
299         return aParamsIco;
300       case ConstructionFolder:
301         return aConstrIco;
302       case PartsFolder:
303         return aConstrIco;
304       case ResultsFolder:
305         return aConstrIco;
306       case FieldsFolder:
307         return aConstrIco;
308       case GroupsFolder:
309         return aConstrIco;
310       }
311     }
312   }
313   return PartSet_TreeNode::data(theColumn, theRole);
314 }
315
316 Qt::ItemFlags PartSet_FolderNode::flags(int theColumn) const
317 {
318   SessionPtr aSession = ModelAPI_Session::get();
319   DocumentPtr aActiveDoc = aSession->activeDocument();
320   if (theColumn == 1) {
321     if (document() == aActiveDoc)
322       return aEditingFlag;
323   }
324   return aDefaultFlag;
325 }
326
327 ModuleBase_ITreeNode* PartSet_FolderNode::createNode(const ObjectPtr& theObj)
328 {
329   //ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObj);
330   //if (aCompRes.get())
331   //  return new PartSet_CompsolidNode(theObj, this);
332   return new PartSet_ObjectNode(theObj, this);
333 }
334
335 void PartSet_FolderNode::update()
336 {
337   DocumentPtr aDoc = document();
338   if (!aDoc.get())
339     return;
340
341   // Remove extra sub-nodes
342   int aIndex;
343   int aId = 0;
344   while (aId < myChildren.size()) {
345     ModuleBase_ITreeNode* aNode = myChildren.at(aId);
346     aIndex = aDoc->index(aNode->object(), true);
347     if ((aIndex == -1) || (aId != aIndex)) {
348       myChildren.removeAll(aNode);
349       delete aNode;
350     } else
351       aId++;
352   }
353
354   // Add new nodes
355   std::string aGroup = groupName();
356   int aSize = aDoc->size(aGroup, true);
357   for (int i = 0; i < aSize; i++) {
358     ObjectPtr aObj = aDoc->object(aGroup, i, true);
359     if (i < myChildren.size()) {
360       if (myChildren.at(i)->object() != aObj) {
361         ModuleBase_ITreeNode* aNode = createNode(aObj);
362         myChildren.insert(i, aNode);
363       }
364     } else {
365       ModuleBase_ITreeNode* aNode = createNode(aObj);
366       myChildren.append(aNode);
367     }
368   }
369
370   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
371     aNode->update();
372   }
373 }
374
375 std::string PartSet_FolderNode::groupName() const
376 {
377   switch (myType) {
378   case ParametersFolder:
379     return ModelAPI_ResultParameter::group();
380   case ConstructionFolder:
381     return ModelAPI_ResultConstruction::group();
382   case PartsFolder:
383     return ModelAPI_ResultPart::group();
384   case ResultsFolder:
385     return ModelAPI_ResultBody::group();
386   case FieldsFolder:
387     return ModelAPI_ResultField::group();
388   case GroupsFolder:
389     return ModelAPI_ResultGroup::group();
390   }
391   return "";
392 }
393
394 QTreeNodesList PartSet_FolderNode::objectCreated(const QObjectPtrList& theObjects)
395 {
396   QTreeNodesList aResult;
397   std::string aName = groupName();
398   DocumentPtr aDoc = document();
399   int aIdx = -1;
400   QMap<int, ModuleBase_ITreeNode*> aNewNodes;
401   foreach(ObjectPtr aObj, theObjects) {
402     if ((aObj->document() == aDoc) && (aObj->groupName() == aName)) {
403       aIdx = aDoc->index(aObj, true);
404       if (aIdx != -1) {
405         bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
406         if (!aHasObject) {
407           ModuleBase_ITreeNode* aNode = createNode(aObj);
408           aNewNodes[aIdx] = aNode;
409           aResult.append(aNode);
410         }
411       }
412     }
413   }
414   // Add nodes in correct order
415   int i;
416   for (i = 0; i < myChildren.size(); i++) {
417     if (aNewNodes.contains(i)) {
418       myChildren.insert(i, aNewNodes[i]);
419       aNewNodes.remove(i);
420     }
421   }
422   while (aNewNodes.size()) {
423     i = myChildren.size();
424     myChildren.append(aNewNodes[i]);
425     aNewNodes.remove(i);
426   }
427   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
428     aResult.append(aNode->objectCreated(theObjects));
429   }
430   return aResult;
431 }
432
433 QTreeNodesList PartSet_FolderNode::objectsDeleted(const DocumentPtr& theDoc,
434   const QString& theGroup)
435 {
436   DocumentPtr aDoc = document();
437   QTreeNodesList aResult;
438   if ((theGroup.toStdString() == groupName()) && (theDoc == aDoc)) {
439     QTreeNodesList aDelList;
440     int aIndex;
441     int aId = 0;
442     bool aRemoved = false;
443     while (aId < myChildren.size()) {
444       ModuleBase_ITreeNode* aNode = myChildren.at(aId);
445       aIndex = aDoc->index(aNode->object(), true);
446       if ((aIndex == -1) || (aId != aIndex)) {
447         myChildren.removeAll(aNode);
448         delete aNode;
449         aRemoved = true;
450       }  else
451         aId++;
452     }
453     if (aRemoved)
454       aResult.append(this);
455
456     foreach(ModuleBase_ITreeNode* aNode, myChildren) {
457       aResult.append(aNode->objectsDeleted(theDoc, theGroup));
458     }
459   }
460   return aResult;
461 }
462
463 //////////////////////////////////////////////////////////////////////////////////
464 QTreeNodesList PartSet_FeatureFolderNode::objectCreated(const QObjectPtrList& theObjects)
465 {
466   QTreeNodesList aResult;
467   // Process all folders
468   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
469     if (aNode->type() == PartSet_FolderNode::typeId()) { // aFolder node
470       aResult.append(aNode->objectCreated(theObjects));
471     }
472   }
473   // Process the root sub-objects
474   DocumentPtr aDoc = document();
475   int aIdx = -1;
476   int aNb = numberOfFolders();
477   QMap<int, ModuleBase_ITreeNode*> aNewNodes;
478   foreach(ObjectPtr aObj, theObjects) {
479     if (aDoc == aObj->document()) {
480       if ((aObj->groupName() == ModelAPI_Feature::group()) ||
481         (aObj->groupName() == ModelAPI_Folder::group())){
482         ModuleBase_ITreeNode* aNode = createNode(aObj);
483         aIdx = aDoc->index(aObj, true);
484         if (aIdx != -1) {
485           aIdx += aNb;
486           bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
487           if (!aHasObject) {
488             aNewNodes[aIdx] = aNode;
489             aResult.append(aNode);
490           }
491         }
492       }
493     }
494   }
495   // To add in correct order
496   int i;
497   for (i = 0; i < myChildren.size(); i++) {
498     if (aNewNodes.contains(i)) {
499       myChildren.insert(i, aNewNodes[i]);
500       aNewNodes.remove(i);
501     }
502   }
503   while (aNewNodes.size()) {
504     i = myChildren.size();
505     myChildren.append(aNewNodes[i]);
506     aNewNodes.remove(i);
507   }
508
509   // Update sub-folders
510   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
511     if ((aNode->type() == PartSet_ObjectFolderNode::typeId()) ||
512       (aNode->type() == PartSet_PartRootNode::typeId()))
513       aResult.append(aNode->objectCreated(theObjects));
514   }
515   return aResult;
516 }
517
518 QTreeNodesList PartSet_FeatureFolderNode::objectsDeleted(const DocumentPtr& theDoc,
519   const QString& theGroup)
520 {
521   QTreeNodesList aResult;
522
523   // Process sub-folders
524   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
525     if (aNode->childrenCount() > 0) { // aFolder node
526       QTreeNodesList aList = aNode->objectsDeleted(theDoc, theGroup);
527       if (aList.size() > 0)
528         aResult.append(aList);
529     }
530   }
531
532   // Process root
533   DocumentPtr aDoc = document();
534   int aNb = numberOfFolders();
535   bool isGroup = ((theGroup.toStdString() == ModelAPI_Feature::group()) ||
536     (theGroup.toStdString() == ModelAPI_Folder::group()));
537   if ((theDoc == aDoc) && isGroup) {
538     int aIndex;
539     int aId = 0;
540     bool aRemoved = false;
541     while (aId < myChildren.size()) {
542       ModuleBase_ITreeNode* aNode = myChildren.at(aId);
543       if (aNode->object().get()) {
544         aIndex = aDoc->index(aNode->object(), true);
545         if ((aIndex == -1) || (aId != (aIndex + aNb))) {
546           myChildren.removeAll(aNode);
547           delete aNode;
548           aRemoved = true;
549           continue;
550         }
551       }
552       aId++;
553     }
554     if (aRemoved)
555       aResult.append(this);
556   }
557   return aResult;
558 }
559
560 ModuleBase_ITreeNode* PartSet_FeatureFolderNode::findParent(const DocumentPtr& theDoc,
561   QString theGroup)
562 {
563   ModuleBase_ITreeNode* aResult = 0;
564   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
565     aResult = aNode->findParent(theDoc, theGroup);
566     if (aResult) {
567       return aResult;
568     }
569   }
570   bool isGroup = ((theGroup.toStdString() == ModelAPI_Feature::group()) ||
571     (theGroup.toStdString() == ModelAPI_Folder::group()));
572   if ((theDoc == document()) && isGroup)
573     return this;
574   return 0;
575 }
576
577
578 //////////////////////////////////////////////////////////////////////////////////
579 PartSet_RootNode::PartSet_RootNode() : PartSet_FeatureFolderNode(0), myWorkshop(0)
580 {
581   SessionPtr aSession = ModelAPI_Session::get();
582   DocumentPtr aDoc = aSession->moduleDocument();
583
584   myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
585   myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
586   myPartsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::PartsFolder);
587
588   myChildren.append(myParamsFolder);
589   myChildren.append(myConstrFolder);
590   myChildren.append(myPartsFolder);
591
592   update();
593 }
594
595
596 void PartSet_RootNode::update()
597 {
598   myParamsFolder->update();
599   myConstrFolder->update();
600   myPartsFolder->update();
601
602   // Update features content
603   DocumentPtr aDoc = document();
604   int aNb = numberOfFolders();
605
606   // Remove extra sub-nodes
607   int aIndex;
608   int aId = 0;
609   while (aId < myChildren.size()) {
610     ModuleBase_ITreeNode* aNode = myChildren.at(aId);
611     if (aNode->object().get()) {
612       aIndex = aDoc->index(aNode->object(), true);
613       if ((aIndex == -1) || (aId != (aIndex + aNb))) {
614         myChildren.removeAll(aNode);
615         delete aNode;
616         continue;
617       }
618     }
619     aId++;
620   }
621
622   // Add new nodes
623   std::string aGroup = ModelAPI_Feature::group();
624   int aSize = aDoc->size(aGroup, true);
625   FeaturePtr aFeature;
626   for (int i = 0; i < aSize; i++) {
627     ObjectPtr aObj = aDoc->object(aGroup, i, true);
628     aId = i + aNb; // Take into account existing folders
629     if (aId < myChildren.size()) {
630       if (myChildren.at(aId)->object() != aObj) {
631         ModuleBase_ITreeNode* aNode = createNode(aObj);
632         myChildren.insert(aId, aNode);
633       }
634     } else {
635       ModuleBase_ITreeNode* aNode = createNode(aObj);
636       myChildren.append(aNode);
637     }
638   }
639   // Update sub-folders
640   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
641     if ((aNode->type() == PartSet_ObjectFolderNode::typeId()) ||
642       (aNode->type() == PartSet_PartRootNode::typeId()))
643       aNode->update();
644   }
645 }
646
647 DocumentPtr PartSet_RootNode::document() const
648 {
649   return ModelAPI_Session::get()->moduleDocument();
650 }
651
652 ModuleBase_ITreeNode* PartSet_RootNode::createNode(const ObjectPtr& theObj)
653 {
654   if (theObj->groupName() == ModelAPI_Folder::group())
655     return new PartSet_ObjectFolderNode(theObj, this);
656
657   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
658   if (aFeature->getKind() == PartSetPlugin_Part::ID())
659     return new PartSet_PartRootNode(theObj, this);
660
661   return new PartSet_ObjectNode(theObj, this);
662 }
663
664 //////////////////////////////////////////////////////////////////////////////////
665 PartSet_PartRootNode::PartSet_PartRootNode(const ObjectPtr& theObj, ModuleBase_ITreeNode* theParent)
666   : PartSet_FeatureFolderNode(theParent), myObject(theObj)
667 {
668   myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
669   myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
670   myResultsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ResultsFolder);
671   myFieldsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::FieldsFolder);
672   myGroupsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::GroupsFolder);
673
674   myChildren.append(myParamsFolder);
675   myChildren.append(myConstrFolder);
676   myChildren.append(myResultsFolder);
677
678   update();
679 }
680
681 void PartSet_PartRootNode::deleteChildren()
682 {
683   if (!myFieldsFolder->childrenCount()) {
684     delete myFieldsFolder;
685   }
686   if (!myGroupsFolder->childrenCount()) {
687     delete myGroupsFolder;
688   }
689   PartSet_FeatureFolderNode::deleteChildren();
690 }
691
692
693 void PartSet_PartRootNode::update()
694 {
695   DocumentPtr aDoc = document();
696   if (!aDoc.get())
697     return;
698
699   myParamsFolder->update();
700   myConstrFolder->update();
701   myResultsFolder->update();
702   myFieldsFolder->update();
703   myGroupsFolder->update();
704
705   bool aHasFields = myFieldsFolder->childrenCount() > 0;
706   bool aHasGroups = myGroupsFolder->childrenCount() > 0;
707   if (aHasFields && (!myChildren.contains(myFieldsFolder))) {
708     myChildren.insert(3, myFieldsFolder);
709   }
710   if (aHasGroups && (!myChildren.contains(myGroupsFolder))) {
711     myChildren.insert(aHasFields ? 4 : 3, myGroupsFolder);
712   }
713
714   // Update features content
715   int aRows = numberOfFolders();
716
717   // Remove extra sub-nodes
718   int aIndex = -1;
719   int aId = 0;
720   while (aId < myChildren.size()) {
721     ModuleBase_ITreeNode* aNode = myChildren.at(aId);
722     if (aNode->object().get()) {
723       aIndex = aDoc->index(aNode->object(), true);
724       if ((aIndex == -1) || (aId != (aIndex + aRows))) {
725         myChildren.removeAll(aNode);
726         delete aNode;
727         continue;
728       }
729     }
730     aId++;
731   }
732
733   std::string aGroup = ModelAPI_Feature::group();
734   int aSize = aDoc->size(aGroup, true);
735   FeaturePtr aFeature;
736   for (int i = 0; i < aSize; i++) {
737     ObjectPtr aObj = aDoc->object(aGroup, i, true);
738     aId = i + aRows; // Take into account existing folders
739     if (aId < myChildren.size()) {
740       if (myChildren.at(aId)->object() != aObj) {
741         ModuleBase_ITreeNode* aNode = createNode(aObj);
742         myChildren.insert(aId, aNode);
743       }
744     } else {
745       ModuleBase_ITreeNode* aNode = createNode(aObj);
746       myChildren.append(aNode);
747     }
748   }
749   // Update sub-folders
750   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
751     if (aNode->type() == PartSet_ObjectFolderNode::typeId())
752       aNode->update();
753   }
754 }
755
756 DocumentPtr PartSet_PartRootNode::document() const
757 {
758   ResultPartPtr aPartRes = getPartResult(myObject);
759   if (aPartRes.get())
760     return aPartRes->partDoc();
761   return DocumentPtr();
762 }
763
764 QVariant PartSet_PartRootNode::data(int theColumn, int theRole) const
765 {
766   switch (theColumn) {
767   case 1:
768     switch (theRole) {
769     case Qt::DisplayRole:
770       return QString(myObject->data()->name().c_str());
771     case Qt::DecorationRole:
772       return ModuleBase_IconFactory::get()->getIcon(myObject);
773     }
774   case 2:
775     if (theRole == Qt::DecorationRole)
776       if (isCurrentFeature(myObject))
777         return QIcon(":pictures/arrow.png");
778   }
779   return PartSet_TreeNode::data(theColumn, theRole);
780 }
781
782 Qt::ItemFlags PartSet_PartRootNode::flags(int theColumn) const
783 {
784   SessionPtr aSession = ModelAPI_Session::get();
785   DocumentPtr aActiveDoc = aSession->activeDocument();
786   if ((aActiveDoc == document()) || (myObject->document() == aActiveDoc))
787     return aEditingFlag;
788   return aDefaultFlag;
789 }
790
791 ModuleBase_ITreeNode* PartSet_PartRootNode::createNode(const ObjectPtr& theObj)
792 {
793   if (theObj->groupName() == ModelAPI_Folder::group())
794     return new PartSet_ObjectFolderNode(theObj, this);
795   return new PartSet_ObjectNode(theObj, this);
796 }
797
798 int PartSet_PartRootNode::numberOfFolders() const
799 {
800   int aNb = 3;
801   if (myFieldsFolder->childrenCount() > 0)
802     aNb++;
803   if (myGroupsFolder->childrenCount() > 0)
804     aNb++;
805   return aNb;
806 }
807
808 QTreeNodesList PartSet_PartRootNode::objectCreated(const QObjectPtrList& theObjects)
809 {
810   QTreeNodesList aResult = PartSet_FeatureFolderNode::objectCreated(theObjects);
811   if (!myFieldsFolder->childrenCount()) {
812     QTreeNodesList aList = myFieldsFolder->objectCreated(theObjects);
813     if (aList.size()) {
814       myChildren.insert(3, myFieldsFolder);
815       aResult.append(myFieldsFolder);
816       aResult.append(aList);
817     }
818   }
819   if (!myGroupsFolder->childrenCount()) {
820     QTreeNodesList aList = myGroupsFolder->objectCreated(theObjects);
821     if (aList.size()) {
822       myChildren.insert(myFieldsFolder->childrenCount()? 4 : 3, myGroupsFolder);
823       aResult.append(myGroupsFolder);
824       aResult.append(aList);
825     }
826   }
827   return aResult;
828 }
829
830 QTreeNodesList PartSet_PartRootNode::objectsDeleted(const DocumentPtr& theDoc,
831   const QString& theGroup)
832 {
833   QTreeNodesList aResult;
834   if (myFieldsFolder->childrenCount()) {
835     QTreeNodesList aList = myFieldsFolder->objectsDeleted(theDoc, theGroup);
836     if (aList.size()) {
837       aResult.append(aList);
838       if (!myFieldsFolder->childrenCount())
839         myChildren.removeAll(myFieldsFolder);
840     }
841   }
842   if (myGroupsFolder->childrenCount()) {
843     QTreeNodesList aList = myGroupsFolder->objectsDeleted(theDoc, theGroup);
844     if (aList.size()) {
845       aResult.append(aList);
846       if (!myGroupsFolder->childrenCount())
847         myChildren.removeAll(myGroupsFolder);
848     }
849   }
850   aResult.append(PartSet_FeatureFolderNode::objectsDeleted(theDoc, theGroup));
851   return aResult;
852 }
853
854 //////////////////////////////////////////////////////////////////////////////////
855 void PartSet_ObjectFolderNode::update()
856 {
857   int aFirst, aLast;
858   getFirstAndLastIndex(aFirst, aLast);
859   if ((aFirst == -1) || (aLast == -1)) {
860     deleteChildren();
861     return;
862   }
863
864   int aNbItems = aLast - aFirst + 1;
865   if (!aNbItems) {
866     deleteChildren();
867     return;
868   }
869
870   DocumentPtr aDoc = myObject->document();
871   // Delete obsolete nodes
872   int aId = 0;
873   while (aId < myChildren.size()) {
874     ModuleBase_ITreeNode* aNode = myChildren.at(aId);
875     if ((aFirst + aId) < aDoc->size(ModelAPI_Feature::group(), true)) {
876       if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
877         myChildren.removeAll(aNode);
878         delete aNode;
879         continue;
880       }
881     }
882     aId++;
883   }
884
885   // Add new nodes
886   ModuleBase_ITreeNode* aNode;
887   for (int i = 0; i < aNbItems; i++) {
888     ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
889     if (i < myChildren.size()) {
890       if (aObj != myChildren.at(i)->object()) {
891         aNode = new PartSet_ObjectNode(aObj, this);
892         myChildren.insert(i, aNode);
893       }
894     } else {
895       aNode = new PartSet_ObjectNode(aObj, this);
896       myChildren.append(aNode);
897     }
898   }
899 }
900
901 QTreeNodesList PartSet_ObjectFolderNode::objectCreated(const QObjectPtrList& theObjects)
902 {
903   QTreeNodesList aResult;
904   int aFirst, aLast;
905   getFirstAndLastIndex(aFirst, aLast);
906   if ((aFirst == -1) || (aLast == -1)) {
907     return aResult;
908   }
909   int aNbItems = aLast - aFirst + 1;
910   if (!aNbItems) {
911     return aResult;
912   }
913   DocumentPtr aDoc = myObject->document();
914   // Add new nodes
915   ModuleBase_ITreeNode* aNode;
916   for (int i = 0; i < aNbItems; i++) {
917     ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
918     if (i < myChildren.size()) {
919       if (aObj != myChildren.at(i)->object()) {
920         aNode = new PartSet_ObjectNode(aObj, this);
921         myChildren.insert(i, aNode);
922         aResult.append(aNode);
923       }
924     } else {
925       aNode = new PartSet_ObjectNode(aObj, this);
926       myChildren.append(aNode);
927       aResult.append(aNode);
928     }
929   }
930   return aResult;
931 }
932
933 QTreeNodesList PartSet_ObjectFolderNode::objectsDeleted(const DocumentPtr& theDoc,
934   const QString& theGroup)
935 {
936   QTreeNodesList aResult;
937   int aFirst, aLast;
938   getFirstAndLastIndex(aFirst, aLast);
939   if ((aFirst == -1) || (aLast == -1)) {
940     return aResult;
941   }
942   int aNbItems = aLast - aFirst + 1;
943   if (!aNbItems) {
944     return aResult;
945   }
946   DocumentPtr aDoc = myObject->document();
947   // Delete obsolete nodes
948   bool aRemoved = false;
949   int aId = 0;
950   while (aId < myChildren.size()) {
951     ModuleBase_ITreeNode* aNode = myChildren.at(aId);
952     if ((aFirst + aId) < aDoc->size(ModelAPI_Feature::group(), true)) {
953       if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
954         myChildren.removeAll(aNode);
955         delete aNode;
956         aRemoved = true;
957         continue;
958       }
959     }
960     aId++;
961   }
962   if (aRemoved) {
963     aResult.append(this);
964   }
965   return aResult;
966 }
967
968 FeaturePtr PartSet_ObjectFolderNode::getFeature(const std::string& theId) const
969 {
970   FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(myObject);
971   AttributeReferencePtr aFeatAttr = aFolder->data()->reference(theId);
972   if (aFeatAttr)
973     return ModelAPI_Feature::feature(aFeatAttr->value());
974   return FeaturePtr();
975 }
976
977 void PartSet_ObjectFolderNode::getFirstAndLastIndex(int& theFirst, int& theLast) const
978 {
979   DocumentPtr aDoc = myObject->document();
980   FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(myObject);
981
982   FeaturePtr aFirstFeatureInFolder = getFeature(ModelAPI_Folder::FIRST_FEATURE_ID());
983   if (!aFirstFeatureInFolder.get()) {
984     theFirst = -1;
985     return;
986   }
987   FeaturePtr aLastFeatureInFolder = getFeature(ModelAPI_Folder::LAST_FEATURE_ID());
988   if (!aLastFeatureInFolder.get()) {
989     theLast = -1;
990     return;
991   }
992
993   theFirst = aDoc->index(aFirstFeatureInFolder);
994   theLast = aDoc->index(aLastFeatureInFolder);
995 }
996
997
998 //////////////////////////////////////////////////////////////////////////////////
999 //PartSet_CompsolidNode::PartSet_CompsolidNode(const ObjectPtr& theObj,
1000 //  ModuleBase_ITreeNode* theParent) : PartSet_ObjectNode(theObj, theParent)
1001 //{
1002 //  update();
1003 //}
1004
1005 //void PartSet_CompsolidNode::update()
1006 //{
1007 //  ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(myObject);
1008 //  int aNb = aCompRes->numberOfSubs(true);
1009 //  ModuleBase_ITreeNode* aNode;
1010 //  ResultBodyPtr aBody;
1011 //  int i;
1012 //  for (i = 0; i < aNb; i++) {
1013 //    aBody = aCompRes->subResult(i, true);
1014 //    if (i < myChildren.size()) {
1015 //      aNode = myChildren.at(i);
1016 //      if (aNode->object() != aBody) {
1017 //        ((PartSet_ObjectNode*)aNode)->setObject(aBody);
1018 //      }
1019 //    } else {
1020 //      aNode = new PartSet_ObjectNode(aBody, this);
1021 //      myChildren.append(aNode);
1022 //    }
1023 //  }
1024 //  // Delete extra objects
1025 //  while (myChildren.size() > aNb) {
1026 //    aNode = myChildren.takeLast();
1027 //    delete aNode;
1028 //  }
1029 //}
1030 //
1031 //QTreeNodesList PartSet_CompsolidNode::objectCreated(const QObjectPtrList& theObjects)
1032 //{
1033 //  QTreeNodesList aResult;
1034 //
1035 //  ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(myObject);
1036 //  int aNb = aCompRes->numberOfSubs(true);
1037 //  ModuleBase_ITreeNode* aNode;
1038 //  ResultBodyPtr aBody;
1039 //  int i;
1040 //  for (i = 0; i < aNb; i++) {
1041 //    aBody = aCompRes->subResult(i, true);
1042 //    if (i < myChildren.size()) {
1043 //      aNode = myChildren.at(i);
1044 //      if (aNode->object() != aBody) {
1045 //        ((PartSet_ObjectNode*)aNode)->setObject(aBody);
1046 //        aResult.append(aNode);
1047 //      }
1048 //    } else {
1049 //      aNode = new PartSet_ObjectNode(aBody, this);
1050 //      myChildren.append(aNode);
1051 //      aResult.append(aNode);
1052 //    }
1053 //  }
1054 //  return aResult;
1055 //}
1056 //
1057 //QTreeNodesList PartSet_CompsolidNode::objectsDeleted(const DocumentPtr& theDoc, const QString& theGroup)
1058 //{
1059 //  QTreeNodesList aResult;
1060 //  ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(myObject);
1061 //  int aNb = aCompRes->numberOfSubs(true);
1062 //  ModuleBase_ITreeNode* aNode;
1063 //  // Delete extra objects
1064 //  bool isDeleted = false;
1065 //  while (myChildren.size() > aNb) {
1066 //    aNode = myChildren.takeLast();
1067 //    delete aNode;
1068 //    isDeleted = true;
1069 //  }
1070 //  if (isDeleted)
1071 //    aResult.append(this);
1072 //  return aResult;
1073 //}