Salome HOME
Avoid crash on clearing of a document by a script
[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       else
132         return QIcon();
133     }
134   }
135   return PartSet_TreeNode::data(theColumn, theRole);
136 }
137
138 Qt::ItemFlags PartSet_ObjectNode::flags(int theColumn) const
139 {
140   if (myObject->isDisabled()) {
141     return (theColumn == 2) ? Qt::ItemIsSelectable : aNullFlag;
142   } else {
143     DocumentPtr aDoc = myObject->document();
144     SessionPtr aSession = ModelAPI_Session::get();
145     if (aSession->activeDocument() == aDoc)
146       return aEditingFlag;
147   }
148   return aDefaultFlag;
149 }
150
151 PartSet_ObjectNode::VisibilityState PartSet_ObjectNode::visibilityState() const
152 {
153   Qt::ItemFlags aFlags = flags(1);
154   if (aFlags == Qt::ItemFlags())
155     return NoneState;
156
157   if (myObject->groupName() == ModelAPI_ResultParameter::group())
158     return NoneState;
159   ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
160   if (aResObj.get()) {
161     ModuleBase_IWorkshop* aWork = workshop();
162     ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResObj);
163     if (aCompRes.get()) {
164       std::list<ResultPtr> aResultsList;
165       ModelAPI_Tools::allSubs(aCompRes, aResultsList);
166       VisibilityState aState = aResultsList.size() == 0 ?
167         (aWork->isVisible(aCompRes) ? Visible : Hidden) : NoneState;
168
169       std::list<ResultPtr>::const_iterator aIt;
170       ResultBodyPtr aCompSub;
171       for (aIt = aResultsList.cbegin(); aIt != aResultsList.cend(); aIt++) {
172         ResultPtr aSubRes = (*aIt);
173         aCompSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aSubRes);
174         if (!(aCompSub.get() && aCompSub->numberOfSubs() > 0)) {
175           VisibilityState aS = aWork->isVisible(aSubRes) ? Visible : Hidden;
176           if (aState == NoneState)
177             aState = aS;
178           else if (aState != aS) {
179             aState = SemiVisible;
180             break;
181           }
182         }
183       }
184       return aState;
185     } else {
186       if (aWork->isVisible(aResObj))
187         return Visible;
188       else
189         return Hidden;
190     }
191   }
192   return NoneState;
193 }
194
195 int PartSet_ObjectNode::numberOfSubs() const
196 {
197   ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
198   if (aCompRes.get())
199    return aCompRes->numberOfSubs(true);
200   else {
201     CompositeFeaturePtr aCompFeature =
202       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myObject);
203     if (aCompFeature.get() && aCompFeature->data()->isValid())
204       return aCompFeature->numberOfSubs(true);
205     else {
206       ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
207       if (aFieldRes.get())
208         return aFieldRes->stepsSize();
209     }
210   }
211   return 0;
212 }
213
214
215 ObjectPtr PartSet_ObjectNode::subObject(int theId) const
216 {
217   ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
218   if (aCompRes.get())
219     return aCompRes->subResult(theId, true);
220   else {
221     CompositeFeaturePtr aCompFeature =
222       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myObject);
223     if (aCompFeature.get())
224       return aCompFeature->subFeature(theId, true);
225   }
226   return ObjectPtr();
227 }
228
229 void PartSet_ObjectNode::update()
230 {
231   int aNb = numberOfSubs();
232   if (aNb > 0) {
233     ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
234
235     // If the object is a field result then delete extra sub-objects
236     if (aFieldRes.get()) {
237       while (myChildren.size() > aNb) {
238         ModuleBase_ITreeNode* aNode = myChildren.last();
239         myChildren.removeAll(aNode);
240         delete aNode;
241       }
242     }
243     else {
244       ObjectPtr aObj;
245       ModuleBase_ITreeNode* aNode;
246       int aId = 0;
247       while (aId < myChildren.size()) {
248         aNode = myChildren.at(aId);
249         aObj = subObject(aId);
250         if (aNode->object() != aObj) {
251           myChildren.removeAll(aNode);
252           delete aNode;
253         }
254         else
255           aId++;
256       }
257     }
258
259     ModuleBase_ITreeNode* aNode;
260     ObjectPtr aBody;
261     int i;
262     for (i = 0; i < aNb; i++) {
263       aBody = subObject(i);
264       if (aBody.get()) {
265         if (i < myChildren.size()) {
266           aNode = myChildren.at(i);
267           if (aNode->object() != aBody) {
268             ((PartSet_ObjectNode*)aNode)->setObject(aBody);
269           }
270         }
271         else {
272           aNode = new PartSet_ObjectNode(aBody, this);
273           myChildren.append(aNode);
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   return new PartSet_ObjectNode(theObj, this);
487 }
488
489 void PartSet_FolderNode::update()
490 {
491   DocumentPtr aDoc = document();
492   if (!aDoc.get())
493     return;
494
495   // Remove extra sub-nodes
496   int aIndex;
497   int aId = 0;
498   while (aId < myChildren.size()) {
499     ModuleBase_ITreeNode* aNode = myChildren.at(aId);
500     aIndex = aDoc->index(aNode->object(), true);
501     if ((aIndex == -1) || (aId != aIndex)) {
502       myChildren.removeAll(aNode);
503       delete aNode;
504     } else
505       aId++;
506   }
507
508   // Add new nodes
509   std::string aGroup = groupName();
510   int aSize = aDoc->size(aGroup, true);
511   for (int i = 0; i < aSize; i++) {
512     ObjectPtr aObj = aDoc->object(aGroup, i, true);
513     if (i < myChildren.size()) {
514       if (myChildren.at(i)->object() != aObj) {
515         ModuleBase_ITreeNode* aNode = createNode(aObj);
516         myChildren.insert(i, aNode);
517       }
518     } else {
519       ModuleBase_ITreeNode* aNode = createNode(aObj);
520       myChildren.append(aNode);
521     }
522   }
523
524   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
525     aNode->update();
526   }
527 }
528
529 std::string PartSet_FolderNode::groupName() const
530 {
531   switch (myType) {
532   case ParametersFolder:
533     return ModelAPI_ResultParameter::group();
534   case ConstructionFolder:
535     return ModelAPI_ResultConstruction::group();
536   case PartsFolder:
537     return ModelAPI_ResultPart::group();
538   case ResultsFolder:
539     return ModelAPI_ResultBody::group();
540   case FieldsFolder:
541     return ModelAPI_ResultField::group();
542   case GroupsFolder:
543     return ModelAPI_ResultGroup::group();
544   }
545   return "";
546 }
547
548 QTreeNodesList PartSet_FolderNode::objectCreated(const QObjectPtrList& theObjects)
549 {
550   QTreeNodesList aResult;
551   std::string aName = groupName();
552   DocumentPtr aDoc = document();
553   int aIdx = -1;
554   QMap<int, ModuleBase_ITreeNode*> aNewNodes;
555   foreach(ObjectPtr aObj, theObjects) {
556     if ((aObj->document() == aDoc) && (aObj->groupName() == aName)) {
557       aIdx = aDoc->index(aObj, true);
558       if (aIdx != -1) {
559         bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
560         if (!aHasObject) {
561           ModuleBase_ITreeNode* aNode = createNode(aObj);
562           aNewNodes[aIdx] = aNode;
563           aResult.append(aNode);
564           aNode->update();
565         }
566       }
567     }
568   }
569   // Add nodes in correct order
570   int i;
571   for (i = 0; i < myChildren.size(); i++) {
572     if (aNewNodes.contains(i)) {
573       myChildren.insert(i, aNewNodes[i]);
574       aNewNodes.remove(i);
575     }
576   }
577   while (aNewNodes.size()) {
578     i = myChildren.size();
579     myChildren.append(aNewNodes[i]);
580     aNewNodes.remove(i);
581   }
582   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
583     aResult.append(aNode->objectCreated(theObjects));
584   }
585   return aResult;
586 }
587
588 QTreeNodesList PartSet_FolderNode::objectsDeleted(const DocumentPtr& theDoc,
589   const QString& theGroup)
590 {
591   DocumentPtr aDoc = document();
592   QTreeNodesList aResult;
593   if ((theGroup.toStdString() == groupName()) && (theDoc == aDoc)) {
594     QTreeNodesList aDelList;
595     int aIndex;
596     int aId = 0;
597     bool aRemoved = false;
598     bool aToSort = false;
599     while (aId < myChildren.size()) {
600       ModuleBase_ITreeNode* aNode = myChildren.at(aId);
601       aIndex = aDoc->index(aNode->object(), true);
602       aToSort |= ((aIndex != -1) && (aId != aIndex));
603       if (aIndex == -1) {
604         myChildren.removeAll(aNode);
605         delete aNode;
606         aRemoved = true;
607       }
608       else
609         aId++;
610     }
611     if (aRemoved)
612       aResult.append(this);
613     if (aToSort)
614       sortChildren();
615     foreach(ModuleBase_ITreeNode* aNode, myChildren) {
616       aResult.append(aNode->objectsDeleted(theDoc, theGroup));
617     }
618   }
619   return aResult;
620 }
621
622 //////////////////////////////////////////////////////////////////////////////////
623 QTreeNodesList PartSet_FeatureFolderNode::objectCreated(const QObjectPtrList& theObjects)
624 {
625   QTreeNodesList aResult;
626   // Process the root sub-objects
627   DocumentPtr aDoc = document();
628   int aIdx = -1;
629   int aNb = numberOfFolders();
630   QMap<int, ModuleBase_ITreeNode*> aNewNodes;
631   foreach(ObjectPtr aObj, theObjects) {
632     if (aDoc == aObj->document()) {
633       if ((aObj->groupName() == ModelAPI_Feature::group()) ||
634         (aObj->groupName() == ModelAPI_Folder::group())){
635         aIdx = aDoc->index(aObj, true);
636         if (aIdx != -1) {
637           ModuleBase_ITreeNode* aNode = createNode(aObj);
638           aIdx += aNb;
639           bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
640           if (!aHasObject) {
641             aNewNodes[aIdx] = aNode;
642             aResult.append(aNode);
643             aNode->update();
644           }
645         }
646       }
647     }
648   }
649   // To add in correct order
650   int i;
651   for (i = 0; i < myChildren.size(); i++) {
652     if (aNewNodes.contains(i)) {
653       myChildren.insert(i, aNewNodes[i]);
654       aNewNodes.remove(i);
655     }
656   }
657   while (aNewNodes.size()) {
658     i = myChildren.size();
659     myChildren.append(aNewNodes[i]);
660     aNewNodes.remove(i);
661   }
662
663   // Update sub-folders
664   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
665     aResult.append(aNode->objectCreated(theObjects));
666   }
667   return aResult;
668 }
669
670 QTreeNodesList PartSet_FeatureFolderNode::objectsDeleted(const DocumentPtr& theDoc,
671   const QString& theGroup)
672 {
673   QTreeNodesList aResult;
674
675   // Process sub-folders
676   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
677     if (aNode->childrenCount() > 0) { // aFolder node
678       QTreeNodesList aList = aNode->objectsDeleted(theDoc, theGroup);
679       if (aList.size() > 0)
680         aResult.append(aList);
681     }
682   }
683
684   // Process root
685   DocumentPtr aDoc = document();
686   int aNb = numberOfFolders();
687   bool isGroup = ((theGroup.toStdString() == ModelAPI_Feature::group()) ||
688     (theGroup.toStdString() == ModelAPI_Folder::group()));
689   if ((theDoc == aDoc) && isGroup) {
690     int aIndex;
691     int aId = 0;
692     bool aRemoved = false;
693     bool aToSort = false;
694     while (aId < myChildren.size()) {
695       ModuleBase_ITreeNode* aNode = myChildren.at(aId);
696       if (aNode->object().get()) {
697         aIndex = aDoc->index(aNode->object(), true);
698         aToSort |= ((aIndex != -1) && (aId != (aIndex + aNb)));
699         if (aIndex == -1) {
700           myChildren.removeAll(aNode);
701           delete aNode;
702           aRemoved = true;
703           continue;
704         }
705       }
706       aId++;
707     }
708     if (aRemoved)
709       aResult.append(this);
710     if (aToSort)
711       sortChildren();
712   }
713   return aResult;
714 }
715
716 ModuleBase_ITreeNode* PartSet_FeatureFolderNode::findParent(const DocumentPtr& theDoc,
717   QString theGroup)
718 {
719   ModuleBase_ITreeNode* aResult = 0;
720   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
721     aResult = aNode->findParent(theDoc, theGroup);
722     if (aResult) {
723       return aResult;
724     }
725   }
726   bool isGroup = ((theGroup.toStdString() == ModelAPI_Feature::group()) ||
727     (theGroup.toStdString() == ModelAPI_Folder::group()));
728   if ((theDoc == document()) && isGroup)
729     return this;
730   return 0;
731 }
732
733
734 //////////////////////////////////////////////////////////////////////////////////
735 PartSet_RootNode::PartSet_RootNode() : PartSet_FeatureFolderNode(0), myWorkshop(0)
736 {
737   SessionPtr aSession = ModelAPI_Session::get();
738   DocumentPtr aDoc = aSession->moduleDocument();
739
740   myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
741   myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
742   myPartsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::PartsFolder);
743
744   myChildren.append(myParamsFolder);
745   myChildren.append(myConstrFolder);
746   myChildren.append(myPartsFolder);
747
748   update();
749 }
750
751
752 void PartSet_RootNode::update()
753 {
754   myParamsFolder->update();
755   myConstrFolder->update();
756   myPartsFolder->update();
757
758   // Update features content
759   DocumentPtr aDoc = document();
760   int aNb = numberOfFolders();
761
762   // Remove extra sub-nodes
763   int aIndex;
764   int aId = 0;
765   while (aId < myChildren.size()) {
766     ModuleBase_ITreeNode* aNode = myChildren.at(aId);
767     if (aNode->object().get()) {
768       aIndex = aDoc->index(aNode->object(), true);
769       if ((aIndex == -1) || (aId != (aIndex + aNb))) {
770         myChildren.removeAll(aNode);
771         delete aNode;
772         continue;
773       }
774     }
775     aId++;
776   }
777
778   // Add new nodes
779   std::string aGroup = ModelAPI_Feature::group();
780   int aSize = aDoc->size(aGroup, true);
781   FeaturePtr aFeature;
782   for (int i = 0; i < aSize; i++) {
783     ObjectPtr aObj = aDoc->object(aGroup, i, true);
784     aId = i + aNb; // Take into account existing folders
785     if (aId < myChildren.size()) {
786       if (myChildren.at(aId)->object() != aObj) {
787         ModuleBase_ITreeNode* aNode = createNode(aObj);
788         myChildren.insert(aId, aNode);
789       }
790     } else {
791       ModuleBase_ITreeNode* aNode = createNode(aObj);
792       myChildren.append(aNode);
793     }
794   }
795   // Update sub-folders
796   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
797     if ((aNode->type() == PartSet_ObjectFolderNode::typeId()) ||
798       (aNode->type() == PartSet_PartRootNode::typeId()))
799       aNode->update();
800   }
801 }
802
803 DocumentPtr PartSet_RootNode::document() const
804 {
805   return ModelAPI_Session::get()->moduleDocument();
806 }
807
808 ModuleBase_ITreeNode* PartSet_RootNode::createNode(const ObjectPtr& theObj)
809 {
810   if (theObj->groupName() == ModelAPI_Folder::group())
811     return new PartSet_ObjectFolderNode(theObj, this);
812
813   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
814   if (aFeature->getKind() == PartSetPlugin_Part::ID())
815     return new PartSet_PartRootNode(theObj, this);
816
817   return new PartSet_ObjectNode(theObj, this);
818 }
819
820 //////////////////////////////////////////////////////////////////////////////////
821 PartSet_PartRootNode::PartSet_PartRootNode(const ObjectPtr& theObj, ModuleBase_ITreeNode* theParent)
822   : PartSet_FeatureFolderNode(theParent), myObject(theObj)
823 {
824   myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
825   myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
826   myResultsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ResultsFolder);
827   myFieldsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::FieldsFolder);
828   myGroupsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::GroupsFolder);
829
830   myChildren.append(myParamsFolder);
831   myChildren.append(myConstrFolder);
832   myChildren.append(myResultsFolder);
833
834   update();
835 }
836
837 void PartSet_PartRootNode::deleteChildren()
838 {
839   if (!myFieldsFolder->childrenCount()) {
840     delete myFieldsFolder;
841   }
842   if (!myGroupsFolder->childrenCount()) {
843     delete myGroupsFolder;
844   }
845   PartSet_FeatureFolderNode::deleteChildren();
846 }
847
848
849 void PartSet_PartRootNode::update()
850 {
851   DocumentPtr aDoc = document();
852   if (!aDoc.get())
853     return;
854
855   myParamsFolder->update();
856   myConstrFolder->update();
857   myResultsFolder->update();
858   myFieldsFolder->update();
859   myGroupsFolder->update();
860
861   bool aHasFields = myFieldsFolder->childrenCount() > 0;
862   bool aHasGroups = myGroupsFolder->childrenCount() > 0;
863   if (aHasFields && (!myChildren.contains(myFieldsFolder))) {
864     myChildren.insert(3, myFieldsFolder);
865   }
866   if (aHasGroups && (!myChildren.contains(myGroupsFolder))) {
867     myChildren.insert(aHasFields ? 4 : 3, myGroupsFolder);
868   }
869
870   // Update features content
871   int aRows = numberOfFolders();
872
873   // Remove extra sub-nodes
874   int aIndex = -1;
875   int aId = aRows;
876   while (aId < myChildren.size()) {
877     ModuleBase_ITreeNode* aNode = myChildren.at(aId);
878     if (aNode->object().get()) {
879       aIndex = aDoc->index(aNode->object(), true);
880       if ((aIndex == -1) || (aId != (aIndex + aRows))) {
881         myChildren.removeAll(aNode);
882         delete aNode;
883         continue;
884       }
885     }
886     aId++;
887   }
888
889   std::string aGroup = ModelAPI_Feature::group();
890   int aSize = aDoc->size(aGroup, true);
891   FeaturePtr aFeature;
892   for (int i = 0; i < aSize; i++) {
893     ObjectPtr aObj = aDoc->object(aGroup, i, true);
894     aId = i + aRows; // Take into account existing folders
895     if (aId < myChildren.size()) {
896       if (myChildren.at(aId)->object() != aObj) {
897         ModuleBase_ITreeNode* aNode = createNode(aObj);
898         myChildren.insert(aId, aNode);
899       }
900     } else {
901       ModuleBase_ITreeNode* aNode = createNode(aObj);
902       myChildren.append(aNode);
903     }
904   }
905   // Update sub-folders
906   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
907     if (aNode->type() == PartSet_ObjectFolderNode::typeId())
908       aNode->update();
909   }
910 }
911
912 DocumentPtr PartSet_PartRootNode::document() const
913 {
914   ResultPartPtr aPartRes = getPartResult(myObject);
915   if (aPartRes.get())
916     return aPartRes->partDoc();
917   return DocumentPtr();
918 }
919
920 QVariant PartSet_PartRootNode::data(int theColumn, int theRole) const
921 {
922   switch (theColumn) {
923   case 1:
924     switch (theRole) {
925     case Qt::DisplayRole:
926     {
927       ResultPartPtr aPartRes = getPartResult(myObject);
928       if (aPartRes.get()) {
929         if (aPartRes->partDoc().get() == NULL)
930           return QString(myObject->data()->name().c_str()) + " (Not loaded)";
931       }
932       return QString(myObject->data()->name().c_str());
933     }
934     case Qt::DecorationRole:
935       return ModuleBase_IconFactory::get()->getIcon(myObject);
936     }
937   case 2:
938     if (theRole == Qt::DecorationRole) {
939       if (isCurrentFeature(myObject))
940         return QIcon(":pictures/arrow.png");
941       else
942         return QIcon();
943     }
944   }
945   return PartSet_TreeNode::data(theColumn, theRole);
946 }
947
948 Qt::ItemFlags PartSet_PartRootNode::flags(int theColumn) const
949 {
950   if (myObject->isDisabled())
951     return (theColumn == 2) ? Qt::ItemIsSelectable : aDefaultFlag;
952
953   SessionPtr aSession = ModelAPI_Session::get();
954   DocumentPtr aActiveDoc = aSession->activeDocument();
955   if ((aActiveDoc == document()) || (myObject->document() == aActiveDoc))
956     return aEditingFlag;
957   return aDefaultFlag;
958 }
959
960 ModuleBase_ITreeNode* PartSet_PartRootNode::createNode(const ObjectPtr& theObj)
961 {
962   if (theObj->groupName() == ModelAPI_Folder::group())
963     return new PartSet_ObjectFolderNode(theObj, this);
964   return new PartSet_ObjectNode(theObj, this);
965 }
966
967 int PartSet_PartRootNode::numberOfFolders() const
968 {
969   int aNb = 3;
970   if (myFieldsFolder->childrenCount() > 0)
971     aNb++;
972   if (myGroupsFolder->childrenCount() > 0)
973     aNb++;
974   return aNb;
975 }
976
977 QTreeNodesList PartSet_PartRootNode::objectCreated(const QObjectPtrList& theObjects)
978 {
979   QTreeNodesList aResult = PartSet_FeatureFolderNode::objectCreated(theObjects);
980   if (!myFieldsFolder->childrenCount()) {
981     QTreeNodesList aList = myFieldsFolder->objectCreated(theObjects);
982     if (aList.size()) {
983       myChildren.insert(3, myFieldsFolder);
984       aResult.append(myFieldsFolder);
985       aResult.append(aList);
986     }
987   }
988   if (!myGroupsFolder->childrenCount()) {
989     QTreeNodesList aList = myGroupsFolder->objectCreated(theObjects);
990     if (aList.size()) {
991       myChildren.insert(myFieldsFolder->childrenCount()? 4 : 3, myGroupsFolder);
992       aResult.append(myGroupsFolder);
993       aResult.append(aList);
994     }
995   }
996   return aResult;
997 }
998
999 QTreeNodesList PartSet_PartRootNode::objectsDeleted(const DocumentPtr& theDoc,
1000   const QString& theGroup)
1001 {
1002   QTreeNodesList aResult;
1003   if (myFieldsFolder->childrenCount()) {
1004     QTreeNodesList aList = myFieldsFolder->objectsDeleted(theDoc, theGroup);
1005     if (aList.size()) {
1006       aResult.append(aList);
1007       if (!myFieldsFolder->childrenCount())
1008         myChildren.removeAll(myFieldsFolder);
1009     }
1010   }
1011   if (myGroupsFolder->childrenCount()) {
1012     QTreeNodesList aList = myGroupsFolder->objectsDeleted(theDoc, theGroup);
1013     if (aList.size()) {
1014       aResult.append(aList);
1015       if (!myGroupsFolder->childrenCount())
1016         myChildren.removeAll(myGroupsFolder);
1017     }
1018   }
1019   aResult.append(PartSet_FeatureFolderNode::objectsDeleted(theDoc, theGroup));
1020   return aResult;
1021 }
1022
1023 //////////////////////////////////////////////////////////////////////////////////
1024 void PartSet_ObjectFolderNode::update()
1025 {
1026   int aFirst, aLast;
1027   getFirstAndLastIndex(aFirst, aLast);
1028   if ((aFirst == -1) || (aLast == -1)) {
1029     deleteChildren();
1030     return;
1031   }
1032
1033   int aNbItems = aLast - aFirst + 1;
1034   if (!aNbItems) {
1035     deleteChildren();
1036     return;
1037   }
1038
1039   DocumentPtr aDoc = myObject->document();
1040   if (aNbItems < myChildren.size()) {
1041     // Delete obsolete nodes
1042     int aId = 0;
1043     int aNbOfFeatures = aDoc->size(ModelAPI_Feature::group(), true);
1044     while (aId < myChildren.size()) {
1045       ModuleBase_ITreeNode* aNode = myChildren.at(aId);
1046       if ((aFirst + aId) < aNbOfFeatures) {
1047         if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
1048           myChildren.removeAll(aNode);
1049           delete aNode;
1050           continue;
1051         }
1052       }
1053       else {
1054         myChildren.removeAll(aNode);
1055         delete aNode;
1056         continue;
1057       }
1058       aId++;
1059     }
1060   }
1061   if (aNbItems > myChildren.size()) {
1062     // Add new nodes
1063     ModuleBase_ITreeNode* aNode;
1064     for (int i = 0; i < aNbItems; i++) {
1065       ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
1066       if (i < myChildren.size()) {
1067         if (aObj != myChildren.at(i)->object()) {
1068           aNode = new PartSet_ObjectNode(aObj, this);
1069           myChildren.insert(i, aNode);
1070         }
1071       }
1072       else {
1073         aNode = new PartSet_ObjectNode(aObj, this);
1074         myChildren.append(aNode);
1075       }
1076     }
1077   }
1078 }
1079
1080 QTreeNodesList PartSet_ObjectFolderNode::objectCreated(const QObjectPtrList& theObjects)
1081 {
1082   QTreeNodesList aResult;
1083   int aFirst, aLast;
1084   getFirstAndLastIndex(aFirst, aLast);
1085   if ((aFirst == -1) || (aLast == -1)) {
1086     return aResult;
1087   }
1088   int aNbItems = aLast - aFirst + 1;
1089   if (!aNbItems) {
1090     return aResult;
1091   }
1092   DocumentPtr aDoc = myObject->document();
1093   // Add new nodes
1094   ModuleBase_ITreeNode* aNode;
1095   for (int i = 0; i < aNbItems; i++) {
1096     ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
1097     if (i < myChildren.size()) {
1098       if (aObj != myChildren.at(i)->object()) {
1099         aNode = new PartSet_ObjectNode(aObj, this);
1100         myChildren.insert(i, aNode);
1101         aResult.append(aNode);
1102       }
1103     } else {
1104       aNode = new PartSet_ObjectNode(aObj, this);
1105       myChildren.append(aNode);
1106       aResult.append(aNode);
1107     }
1108   }
1109   return aResult;
1110 }
1111
1112 QTreeNodesList PartSet_ObjectFolderNode::objectsDeleted(const DocumentPtr& theDoc,
1113   const QString& theGroup)
1114 {
1115   QTreeNodesList aResult;
1116   int aFirst, aLast;
1117   getFirstAndLastIndex(aFirst, aLast);
1118   if ((aFirst == -1) || (aLast == -1)) {
1119     return aResult;
1120   }
1121   int aNbItems = aLast - aFirst + 1;
1122   if (!aNbItems) {
1123     return aResult;
1124   }
1125   if (aNbItems >= myChildren.size()) // Nothing was deleted here
1126     return aResult;
1127
1128   DocumentPtr aDoc = myObject->document();
1129   // Delete obsolete nodes
1130   bool aRemoved = false;
1131   int aId = 0;
1132   int aNbOfFeatures = aDoc->size(ModelAPI_Feature::group(), true);
1133   while (aId < myChildren.size()) {
1134     ModuleBase_ITreeNode* aNode = myChildren.at(aId);
1135     if ((aFirst + aId) < aNbOfFeatures) {
1136       if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
1137         myChildren.removeAll(aNode);
1138         delete aNode;
1139         aRemoved = true;
1140         continue;
1141       }
1142     }
1143     else {
1144       myChildren.removeAll(aNode);
1145       delete aNode;
1146       aRemoved = true;
1147       continue;
1148     }
1149     aId++;
1150   }
1151   if (aRemoved) {
1152     aResult.append(this);
1153   }
1154   return aResult;
1155 }
1156
1157 FeaturePtr PartSet_ObjectFolderNode::getFeature(const std::string& theId) const
1158 {
1159   FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(myObject);
1160   AttributeReferencePtr aFeatAttr = aFolder->data()->reference(theId);
1161   if (aFeatAttr)
1162     return ModelAPI_Feature::feature(aFeatAttr->value());
1163   return FeaturePtr();
1164 }
1165
1166 void PartSet_ObjectFolderNode::getFirstAndLastIndex(int& theFirst, int& theLast) const
1167 {
1168   DocumentPtr aDoc = myObject->document();
1169   FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(myObject);
1170
1171   FeaturePtr aFirstFeatureInFolder = getFeature(ModelAPI_Folder::FIRST_FEATURE_ID());
1172   if (!aFirstFeatureInFolder.get()) {
1173     theFirst = -1;
1174     return;
1175   }
1176   FeaturePtr aLastFeatureInFolder = getFeature(ModelAPI_Folder::LAST_FEATURE_ID());
1177   if (!aLastFeatureInFolder.get()) {
1178     theLast = -1;
1179     return;
1180   }
1181
1182   theFirst = aDoc->index(aFirstFeatureInFolder);
1183   theLast = aDoc->index(aLastFeatureInFolder);
1184 }
1185
1186
1187 //////////////////////////////////////////////////////////////////////////////////
1188 QVariant PartSet_StepNode::data(int theColumn, int theRole) const
1189 {
1190   if ((theColumn == 1) && (theRole == Qt::DisplayRole)) {
1191     ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
1192       dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>(myEntity);
1193
1194     return "Step " + QString::number(aStep->id() + 1) + " " +
1195       aStep->field()->textLine(aStep->id()).c_str();
1196   }
1197   return PartSet_TreeNode::data(theColumn, theRole);
1198 }