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