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