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