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