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