1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "PartSet_TreeNodes.h"
21 #include "PartSet_Tools.h"
23 #include <ModuleBase_IconFactory.h>
24 #include <ModuleBase_IWorkshop.h>
25 #include <ModuleBase_Tools.h>
27 #include <PartSetPlugin_Part.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_ResultParameter.h>
31 #include <ModelAPI_ResultField.h>
32 #include <ModelAPI_ResultGroup.h>
33 #include <ModelAPI_ResultConstruction.h>
34 #include <ModelAPI_ResultPart.h>
35 #include <ModelAPI_ResultBody.h>
36 #include <ModelAPI_Tools.h>
37 #include <ModelAPI_ResultBody.h>
38 #include <ModelAPI_CompositeFeature.h>
39 #include <ModelAPI_AttributeDouble.h>
40 #include <ModelAPI_Folder.h>
41 #include <ModelAPI_AttributeReference.h>
43 #include <Config_PropManager.h>
49 #define ACTIVE_COLOR QColor(Qt::black)
50 #define SELECTABLE_COLOR QColor(100, 100, 100)
51 #define DISABLED_COLOR QColor(200, 200, 200)
53 Qt::ItemFlags aNullFlag;
54 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
55 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
58 ResultPartPtr getPartResult(const ObjectPtr& theObj)
60 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
62 ResultPtr aRes = aFeature->firstResult();
63 if (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group())) {
64 ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aRes);
65 // Use only original parts, not a placement results
66 if (aPartRes == aPartRes->original())
70 return ResultPartPtr();
73 bool isCurrentFeature(const ObjectPtr& theObj)
75 SessionPtr aSession = ModelAPI_Session::get();
76 DocumentPtr aCurDoc = aSession->activeDocument();
77 FeaturePtr aFeature = aCurDoc->currentFeature(true);
78 return aFeature == theObj;
81 //////////////////////////////////////////////////////////////////////////////////
82 QVariant PartSet_TreeNode::data(int theColumn, int theRole) const
84 if ((theColumn == 1) && (theRole == Qt::ForegroundRole)) {
85 Qt::ItemFlags aFlags = flags(theColumn);
86 if (aFlags == Qt::ItemFlags())
87 return QBrush(DISABLED_COLOR);
88 if (!aFlags.testFlag(Qt::ItemIsEditable))
89 return QBrush(SELECTABLE_COLOR);
92 return ModuleBase_ITreeNode::data(theColumn, theRole);
96 //////////////////////////////////////////////////////////////////////////////////
97 QVariant PartSet_ObjectNode::data(int theColumn, int theRole) const
100 case Qt::DisplayRole:
101 if (theColumn == 1) {
102 if (myObject->groupName() == ModelAPI_ResultParameter::group()) {
103 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myObject);
104 AttributeDoublePtr aValueAttribute =
105 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
106 QString aVal = QString::number(aValueAttribute->value());
107 QString aTitle = QString(myObject->data()->name().c_str());
108 return aTitle + " = " + aVal;
110 return myObject->data()->name().c_str();
113 case Qt::DecorationRole:
116 switch (visibilityState()) {
120 return QIcon(":pictures/eyeopen.png");
122 return QIcon(":pictures/eyemiclosed.png");
124 return QIcon(":pictures/eyeclosed.png");
127 return ModuleBase_IconFactory::get()->getIcon(myObject);
129 if (isCurrentFeature(myObject))
130 return QIcon(":pictures/arrow.png");
134 case Qt::ForegroundRole:
135 if (myObject->groupName() == ModelAPI_Feature::group()) {
136 std::vector<int> aColor =
137 Config_PropManager::color("Visualization", "feature_objectbrowser_color");
138 return QColor(aColor[0], aColor[1], aColor[2]);
142 return PartSet_TreeNode::data(theColumn, theRole);
145 Qt::ItemFlags PartSet_ObjectNode::flags(int theColumn) const
147 if (myObject->isDisabled()) {
148 return (theColumn == 2) ? Qt::ItemIsSelectable : aNullFlag;
150 DocumentPtr aDoc = myObject->document();
151 SessionPtr aSession = ModelAPI_Session::get();
152 if (aSession->activeDocument() == aDoc)
158 PartSet_ObjectNode::VisibilityState PartSet_ObjectNode::visibilityState() const
160 Qt::ItemFlags aFlags = flags(1);
161 if (aFlags == Qt::ItemFlags())
164 if (myObject->groupName() == ModelAPI_ResultParameter::group())
166 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
168 ModuleBase_IWorkshop* aWork = workshop();
169 ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResObj);
170 if (aCompRes.get()) {
171 std::list<ResultPtr> aResultsList;
172 ModelAPI_Tools::allSubs(aCompRes, aResultsList);
173 VisibilityState aState = aResultsList.size() == 0 ?
174 (aWork->isVisible(aCompRes) ? Visible : Hidden) : NoneState;
176 std::list<ResultPtr>::const_iterator aIt;
177 ResultBodyPtr aCompSub;
178 for (aIt = aResultsList.cbegin(); aIt != aResultsList.cend(); aIt++) {
179 ResultPtr aSubRes = (*aIt);
180 aCompSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aSubRes);
181 if (!(aCompSub.get() && aCompSub->numberOfSubs() > 0)) {
182 VisibilityState aS = aWork->isVisible(aSubRes) ? Visible : Hidden;
183 if (aState == NoneState)
185 else if (aState != aS) {
186 aState = SemiVisible;
193 if (aWork->isVisible(aResObj))
202 int PartSet_ObjectNode::numberOfSubs() const
204 ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
206 return aCompRes->numberOfSubs(true);
208 CompositeFeaturePtr aCompFeature =
209 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myObject);
210 if (aCompFeature.get() && aCompFeature->data()->isValid())
211 return aCompFeature->numberOfSubs(true);
213 ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
215 return aFieldRes->stepsSize();
222 ObjectPtr PartSet_ObjectNode::subObject(int theId) const
224 ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
226 return aCompRes->subResult(theId, true);
228 CompositeFeaturePtr aCompFeature =
229 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myObject);
230 if (aCompFeature.get())
231 return aCompFeature->subFeature(theId, true);
236 void PartSet_ObjectNode::update()
238 int aNb = numberOfSubs();
240 ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
242 // If the object is a field result then delete extra sub-objects
243 if (aFieldRes.get()) {
244 // Call shape in order to update content of Field.
245 // It is necessary to do for cases when field was created by script when module is inactive.
247 while (myChildren.size() > aNb) {
248 ModuleBase_ITreeNode* aNode = myChildren.last();
249 myChildren.removeAll(aNode);
255 ModuleBase_ITreeNode* aNode;
257 while (aId < myChildren.size()) {
258 aNode = myChildren.at(aId);
259 aObj = subObject(aId);
260 if (aNode->object() != aObj) {
261 myChildren.removeAll(aNode);
269 ModuleBase_ITreeNode* aNode;
272 for (i = 0; i < aNb; i++) {
273 aBody = subObject(i);
275 if (i < myChildren.size()) {
276 aNode = myChildren.at(i);
277 if (aNode->object() != aBody) {
278 ((PartSet_ObjectNode*)aNode)->setObject(aBody);
282 aNode = new PartSet_ObjectNode(aBody, this);
283 myChildren.append(aNode);
287 else if (aFieldRes.get()) {
288 FieldStepPtr aStep = aFieldRes->step(i);
290 if (i < myChildren.size()) {
291 PartSet_StepNode* aStepNode = static_cast<PartSet_StepNode*>(myChildren.at(i));
292 if (aStepNode->object() != aStep) {
293 aStepNode->setObject(aStep);
297 aNode = new PartSet_StepNode(aStep, this);
298 myChildren.append(aNode);
303 // Delete extra objects
304 while (myChildren.size() > aNb) {
305 aNode = myChildren.takeLast();
308 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
317 QTreeNodesList PartSet_ObjectNode::objectCreated(const QObjectPtrList& theObjects)
319 QTreeNodesList aResult;
320 int aNb = numberOfSubs();
322 ModuleBase_ITreeNode* aNode;
323 ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
326 // Call shape in order to update content of Field.
327 // It is necessary to do for cases when field was created by script when module is inactive.
330 for (i = 0; i < aNb; i++) {
331 aBody = subObject(i);
333 if (i < myChildren.size()) {
334 aNode = myChildren.at(i);
335 if (aNode->object() != aBody) {
336 ((PartSet_ObjectNode*)aNode)->setObject(aBody);
337 aResult.append(aNode);
341 aNode = new PartSet_ObjectNode(aBody, this);
342 myChildren.append(aNode);
343 aResult.append(aNode);
348 FieldStepPtr aStep = aFieldRes->step(i);
350 if (i < myChildren.size()) {
351 PartSet_StepNode* aStepNode = static_cast<PartSet_StepNode*>(myChildren.at(i));
352 if (aStepNode->object() != aStep) {
353 aStepNode->setObject(aStep);
357 aNode = new PartSet_StepNode(aStep, this);
358 myChildren.append(aNode);
363 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
364 aResult.append(aNode->objectCreated(theObjects));
370 QTreeNodesList PartSet_ObjectNode::objectsDeleted(
371 const DocumentPtr& theDoc, const QString& theGroup)
373 QTreeNodesList aResult;
374 int aNb = numberOfSubs();
375 if (aNb != myChildren.size()) {
378 aResult.append(this);
381 // Delete extra objects
382 bool isDeleted = false;
384 ModuleBase_ITreeNode* aNode;
386 while (aId < myChildren.size()) {
387 aNode = myChildren.at(aId);
388 aObj = subObject(aId);
389 if (aNode->object() != aObj) {
390 myChildren.removeAll(aNode);
398 aResult.append(this);
401 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
402 aBody = subObject(i);
403 ((PartSet_ObjectNode*)aNode)->setObject(aBody);
404 aResult.append(aNode->objectsDeleted(theDoc, theGroup));
411 //////////////////////////////////////////////////////////////////////////////////
412 PartSet_FolderNode::PartSet_FolderNode(ModuleBase_ITreeNode* theParent,
414 : PartSet_TreeNode(theParent), myType(theType)
418 QString PartSet_FolderNode::name() const
421 case ParametersFolder:
422 return QObject::tr("Parameters");
423 case ConstructionFolder:
424 return QObject::tr("Constructions");
426 return QObject::tr("Parts");
428 return QObject::tr("Results");
430 return QObject::tr("Fields");
432 return QObject::tr("Groups");
438 QVariant PartSet_FolderNode::data(int theColumn, int theRole) const
440 static QIcon aParamsIco(":pictures/params_folder.png");
441 static QIcon aConstrIco(":pictures/constr_folder.png");
443 if (theColumn == 1) {
445 case Qt::DisplayRole:
446 return name() + QString(" (%1)").arg(childrenCount());
447 case Qt::DecorationRole:
449 case ParametersFolder:
451 case ConstructionFolder:
464 if ((theColumn == 2) && (theRole == Qt::DecorationRole)) {
465 if (document().get()) {
466 SessionPtr aSession = ModelAPI_Session::get();
467 if (document() != aSession->activeDocument())
470 FeaturePtr aFeature = document()->currentFeature(true);
471 if (!aFeature.get()) { // There is no current feature
472 ModuleBase_ITreeNode* aLastFolder = 0;
473 foreach(ModuleBase_ITreeNode* aNode, parent()->children()) {
474 if (aNode->type() == PartSet_FolderNode::typeId())
479 if (aLastFolder == this)
480 return QIcon(":pictures/arrow.png");
486 return PartSet_TreeNode::data(theColumn, theRole);
489 Qt::ItemFlags PartSet_FolderNode::flags(int theColumn) const
491 SessionPtr aSession = ModelAPI_Session::get();
492 DocumentPtr aActiveDoc = aSession->activeDocument();
493 if (theColumn == 1) {
494 if (document() == aActiveDoc)
500 ModuleBase_ITreeNode* PartSet_FolderNode::createNode(const ObjectPtr& theObj)
502 //ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObj);
503 //if (aCompRes.get())
504 // return new PartSet_CompsolidNode(theObj, this);
505 ModuleBase_ITreeNode* aNode = new PartSet_ObjectNode(theObj, this);
510 void PartSet_FolderNode::update()
512 DocumentPtr aDoc = document();
516 // Remove extra sub-nodes
519 while (aId < myChildren.size()) {
520 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
521 aIndex = aDoc->index(aNode->object(), true);
522 if ((aIndex == -1) || (aId != aIndex)) {
523 myChildren.removeAll(aNode);
530 std::string aGroup = groupName();
531 int aSize = aDoc->size(aGroup, true);
532 for (int i = 0; i < aSize; i++) {
533 ObjectPtr aObj = aDoc->object(aGroup, i, true);
534 if (i < myChildren.size()) {
535 if (myChildren.at(i)->object() != aObj) {
536 ModuleBase_ITreeNode* aNode = createNode(aObj);
537 myChildren.insert(i, aNode);
540 ModuleBase_ITreeNode* aNode = createNode(aObj);
541 myChildren.append(aNode);
545 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
550 std::string PartSet_FolderNode::groupName() const
553 case ParametersFolder:
554 return ModelAPI_ResultParameter::group();
555 case ConstructionFolder:
556 return ModelAPI_ResultConstruction::group();
558 return ModelAPI_ResultPart::group();
560 return ModelAPI_ResultBody::group();
562 return ModelAPI_ResultField::group();
564 return ModelAPI_ResultGroup::group();
569 QTreeNodesList PartSet_FolderNode::objectCreated(const QObjectPtrList& theObjects)
571 QTreeNodesList aResult;
572 std::string aName = groupName();
573 DocumentPtr aDoc = document();
575 QMap<int, ModuleBase_ITreeNode*> aNewNodes;
576 foreach(ObjectPtr aObj, theObjects) {
577 if ((aObj->document() == aDoc) && (aObj->groupName() == aName)) {
578 aIdx = aDoc->index(aObj, true);
580 bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
582 ModuleBase_ITreeNode* aNode = createNode(aObj);
583 aNewNodes[aIdx] = aNode;
584 aResult.append(aNode);
590 // Add nodes in correct order
591 if (aNewNodes.size() > 0) {
593 for (i = 0; i < myChildren.size(); i++) {
594 if (aNewNodes.contains(i)) {
595 myChildren.insert(i, aNewNodes[i]);
599 while (aNewNodes.size()) {
600 i = myChildren.size();
601 myChildren.append(aNewNodes[i]);
605 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
606 aResult.append(aNode->objectCreated(theObjects));
611 QTreeNodesList PartSet_FolderNode::objectsDeleted(const DocumentPtr& theDoc,
612 const QString& theGroup)
614 DocumentPtr aDoc = document();
615 QTreeNodesList aResult;
616 if ((theGroup.toStdString() == groupName()) && (theDoc == aDoc)) {
617 QTreeNodesList aDelList;
620 bool aRemoved = false;
621 bool aToSort = false;
622 while (aId < myChildren.size()) {
623 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
624 aIndex = aDoc->index(aNode->object(), true);
625 aToSort |= ((aIndex != -1) && (aId != aIndex));
627 myChildren.removeAll(aNode);
635 aResult.append(this);
638 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
639 aResult.append(aNode->objectsDeleted(theDoc, theGroup));
645 //////////////////////////////////////////////////////////////////////////////////
646 QTreeNodesList PartSet_FeatureFolderNode::objectCreated(const QObjectPtrList& theObjects)
648 QTreeNodesList aResult;
649 // Process the root sub-objects
650 DocumentPtr aDoc = document();
652 int aNb = numberOfFolders();
653 QMap<int, ModuleBase_ITreeNode*> aNewNodes;
654 foreach(ObjectPtr aObj, theObjects) {
655 if (aDoc == aObj->document()) {
656 if ((aObj->groupName() == ModelAPI_Feature::group()) ||
657 (aObj->groupName() == ModelAPI_Folder::group())){
658 aIdx = aDoc->index(aObj, true);
660 ModuleBase_ITreeNode* aNode = createNode(aObj);
662 bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
664 aNewNodes[aIdx] = aNode;
665 aResult.append(aNode);
672 // To add in correct order
673 if (aNewNodes.size() > 0) {
675 for (i = 0; i < myChildren.size(); i++) {
676 if (aNewNodes.contains(i)) {
677 myChildren.insert(i, aNewNodes[i]);
681 while (aNewNodes.size()) {
682 i = myChildren.size();
683 if (aNewNodes.contains(i)) {
684 myChildren.append(aNewNodes[i]);
689 // Update sub-folders
690 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
691 aResult.append(aNode->objectCreated(theObjects));
696 QTreeNodesList PartSet_FeatureFolderNode::objectsDeleted(const DocumentPtr& theDoc,
697 const QString& theGroup)
699 QTreeNodesList aResult;
701 // Process sub-folders
702 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
703 if (aNode->childrenCount() > 0) { // aFolder node
704 QTreeNodesList aList = aNode->objectsDeleted(theDoc, theGroup);
705 if (aList.size() > 0)
706 aResult.append(aList);
711 DocumentPtr aDoc = document();
712 int aNb = numberOfFolders();
713 bool isGroup = ((theGroup.toStdString() == ModelAPI_Feature::group()) ||
714 (theGroup.toStdString() == ModelAPI_Folder::group()));
715 if ((theDoc == aDoc) && isGroup) {
718 bool aRemoved = false;
719 bool aToSort = false;
720 while (aId < myChildren.size()) {
721 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
722 if (aNode->object().get()) {
723 aIndex = aDoc->index(aNode->object(), true);
724 aToSort |= ((aIndex != -1) && (aId != (aIndex + aNb)));
726 myChildren.removeAll(aNode);
735 aResult.append(this);
742 ModuleBase_ITreeNode* PartSet_FeatureFolderNode::findParent(const DocumentPtr& theDoc,
745 ModuleBase_ITreeNode* aResult = 0;
746 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
747 aResult = aNode->findParent(theDoc, theGroup);
752 bool isGroup = ((theGroup.toStdString() == ModelAPI_Feature::group()) ||
753 (theGroup.toStdString() == ModelAPI_Folder::group()));
754 if ((theDoc == document()) && isGroup)
760 //////////////////////////////////////////////////////////////////////////////////
761 PartSet_RootNode::PartSet_RootNode() : PartSet_FeatureFolderNode(0), myWorkshop(0)
763 SessionPtr aSession = ModelAPI_Session::get();
764 DocumentPtr aDoc = aSession->moduleDocument();
766 myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
767 myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
768 myPartsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::PartsFolder);
770 myChildren.append(myParamsFolder);
771 myChildren.append(myConstrFolder);
772 myChildren.append(myPartsFolder);
778 void PartSet_RootNode::update()
780 myParamsFolder->update();
781 myConstrFolder->update();
782 myPartsFolder->update();
784 // Update features content
785 DocumentPtr aDoc = document();
786 int aNb = numberOfFolders();
788 // Remove extra sub-nodes
791 while (aId < myChildren.size()) {
792 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
793 if (aNode->object().get()) {
794 aIndex = aDoc->index(aNode->object(), true);
795 if ((aIndex == -1) || (aId != (aIndex + aNb))) {
796 myChildren.removeAll(aNode);
805 std::string aGroup = ModelAPI_Feature::group();
806 int aSize = aDoc->size(aGroup, true);
808 for (int i = 0; i < aSize; i++) {
809 ObjectPtr aObj = aDoc->object(aGroup, i, true);
810 aId = i + aNb; // Take into account existing folders
811 if (aId < myChildren.size()) {
812 if (myChildren.at(aId)->object() != aObj) {
813 ModuleBase_ITreeNode* aNode = createNode(aObj);
814 myChildren.insert(aId, aNode);
817 ModuleBase_ITreeNode* aNode = createNode(aObj);
818 myChildren.append(aNode);
821 // Update sub-folders
822 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
823 if ((aNode->type() == PartSet_ObjectFolderNode::typeId()) ||
824 (aNode->type() == PartSet_PartRootNode::typeId()))
829 DocumentPtr PartSet_RootNode::document() const
831 return ModelAPI_Session::get()->moduleDocument();
834 ModuleBase_ITreeNode* PartSet_RootNode::createNode(const ObjectPtr& theObj)
836 if (theObj->groupName() == ModelAPI_Folder::group())
837 return new PartSet_ObjectFolderNode(theObj, this);
839 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
840 if (aFeature->getKind() == PartSetPlugin_Part::ID())
841 return new PartSet_PartRootNode(theObj, this);
843 PartSet_ObjectNode* aNode = new PartSet_ObjectNode(theObj, this);
848 //////////////////////////////////////////////////////////////////////////////////
849 PartSet_PartRootNode::PartSet_PartRootNode(const ObjectPtr& theObj, ModuleBase_ITreeNode* theParent)
850 : PartSet_FeatureFolderNode(theParent), myObject(theObj)
852 myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
853 myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
854 myResultsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ResultsFolder);
855 myFieldsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::FieldsFolder);
856 myGroupsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::GroupsFolder);
858 myChildren.append(myParamsFolder);
859 myChildren.append(myConstrFolder);
860 myChildren.append(myResultsFolder);
865 void PartSet_PartRootNode::deleteChildren()
867 if (!myFieldsFolder->childrenCount()) {
868 delete myFieldsFolder;
870 if (!myGroupsFolder->childrenCount()) {
871 delete myGroupsFolder;
873 PartSet_FeatureFolderNode::deleteChildren();
877 void PartSet_PartRootNode::update()
879 DocumentPtr aDoc = document();
883 myParamsFolder->update();
884 myConstrFolder->update();
885 myResultsFolder->update();
886 myFieldsFolder->update();
887 myGroupsFolder->update();
889 bool aHasFields = myFieldsFolder->childrenCount() > 0;
890 bool aHasGroups = myGroupsFolder->childrenCount() > 0;
892 if (!myChildren.contains(myFieldsFolder)) {
893 myChildren.insert(3, myFieldsFolder);
895 } else if (myChildren.contains(myFieldsFolder)) {
896 myChildren.removeAll(myFieldsFolder);
899 if (!myChildren.contains(myGroupsFolder)) {
900 myChildren.insert(aHasFields ? 4 : 3, myGroupsFolder);
902 } else if (myChildren.contains(myGroupsFolder)) {
903 myChildren.removeAll(myGroupsFolder);
906 // Update features content
907 int aRows = numberOfFolders();
909 // Remove extra sub-nodes
912 QMap<int, ModuleBase_ITreeNode*> aExistingNodes;
913 while (aId < myChildren.size()) {
914 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
915 if (aNode->object().get()) {
916 aIndex = aDoc->index(aNode->object(), true);
917 if ((aIndex == -1) || (aId != (aIndex + aRows))) {
918 myChildren.removeAll(aNode);
922 aExistingNodes[aIndex + aRows] = aNode;
929 std::string aGroup = ModelAPI_Feature::group();
930 int aSize = aDoc->size(aGroup, true);
932 for (int i = 0; i < aSize; i++) {
933 ObjectPtr aObj = aDoc->object(aGroup, i, true);
934 aId = i + aRows; // Take into account existing folders
935 if (aId < myChildren.size()) {
936 if (myChildren.at(aId)->object() != aObj) {
937 if (aExistingNodes.contains(aId)) {
938 myChildren.insert(aId, aExistingNodes[aId]);
939 aExistingNodes.remove(aId);
942 myChildren.insert(aId, createNode(aObj));
946 if (aExistingNodes.contains(myChildren.size()))
947 myChildren.append(aExistingNodes[myChildren.size()]);
949 myChildren.append(createNode(aObj));
952 // Update sub-folders
953 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
954 if (aNode->type() == PartSet_ObjectFolderNode::typeId())
959 DocumentPtr PartSet_PartRootNode::document() const
961 ResultPartPtr aPartRes = getPartResult(myObject);
963 return aPartRes->partDoc();
964 return DocumentPtr();
967 QVariant PartSet_PartRootNode::data(int theColumn, int theRole) const
972 case Qt::DisplayRole:
974 ResultPartPtr aPartRes = getPartResult(myObject);
975 if (aPartRes.get()) {
976 if (aPartRes->partDoc().get() == NULL)
977 return QString(myObject->data()->name().c_str()) + " (Not loaded)";
979 return QString(myObject->data()->name().c_str());
981 case Qt::DecorationRole:
982 return ModuleBase_IconFactory::get()->getIcon(myObject);
985 if (theRole == Qt::DecorationRole) {
986 if (isCurrentFeature(myObject))
987 return QIcon(":pictures/arrow.png");
992 return PartSet_TreeNode::data(theColumn, theRole);
995 Qt::ItemFlags PartSet_PartRootNode::flags(int theColumn) const
997 if (myObject->isDisabled())
998 return (theColumn == 2) ? Qt::ItemIsSelectable : aNullFlag;
1000 SessionPtr aSession = ModelAPI_Session::get();
1001 DocumentPtr aActiveDoc = aSession->activeDocument();
1002 if ((aActiveDoc == document()) || (myObject->document() == aActiveDoc))
1003 return aEditingFlag;
1004 return aDefaultFlag;
1007 ModuleBase_ITreeNode* PartSet_PartRootNode::createNode(const ObjectPtr& theObj)
1009 if (theObj->groupName() == ModelAPI_Folder::group())
1010 return new PartSet_ObjectFolderNode(theObj, this);
1011 PartSet_ObjectNode* aNode = new PartSet_ObjectNode(theObj, this);
1016 int PartSet_PartRootNode::numberOfFolders() const
1019 if (myFieldsFolder->childrenCount() > 0)
1021 if (myGroupsFolder->childrenCount() > 0)
1026 QTreeNodesList PartSet_PartRootNode::objectCreated(const QObjectPtrList& theObjects)
1028 QTreeNodesList aResult = PartSet_FeatureFolderNode::objectCreated(theObjects);
1029 if (!myFieldsFolder->childrenCount()) {
1030 QTreeNodesList aList = myFieldsFolder->objectCreated(theObjects);
1032 myChildren.insert(3, myFieldsFolder);
1033 aResult.append(myFieldsFolder);
1034 aResult.append(aList);
1037 if (!myGroupsFolder->childrenCount()) {
1038 QTreeNodesList aList = myGroupsFolder->objectCreated(theObjects);
1040 myChildren.insert(myFieldsFolder->childrenCount()? 4 : 3, myGroupsFolder);
1041 aResult.append(myGroupsFolder);
1042 aResult.append(aList);
1048 QTreeNodesList PartSet_PartRootNode::objectsDeleted(const DocumentPtr& theDoc,
1049 const QString& theGroup)
1051 QTreeNodesList aResult;
1052 if (myFieldsFolder->childrenCount()) {
1053 QTreeNodesList aList = myFieldsFolder->objectsDeleted(theDoc, theGroup);
1055 aResult.append(aList);
1056 if (!myFieldsFolder->childrenCount())
1057 myChildren.removeAll(myFieldsFolder);
1060 if (myGroupsFolder->childrenCount()) {
1061 QTreeNodesList aList = myGroupsFolder->objectsDeleted(theDoc, theGroup);
1063 aResult.append(aList);
1064 if (!myGroupsFolder->childrenCount())
1065 myChildren.removeAll(myGroupsFolder);
1068 aResult.append(PartSet_FeatureFolderNode::objectsDeleted(theDoc, theGroup));
1072 //////////////////////////////////////////////////////////////////////////////////
1073 void PartSet_ObjectFolderNode::update()
1075 int aFirst = -1, aLast = -1;
1076 PartSet_Tools::getFirstAndLastIndexInFolder(myObject, aFirst, aLast);
1077 if ((aFirst == -1) || (aLast == -1)) {
1082 int aNbItems = aLast - aFirst + 1;
1088 DocumentPtr aDoc = myObject->document();
1089 if (aNbItems < myChildren.size()) {
1090 // Delete obsolete nodes
1092 int aNbOfFeatures = aDoc->size(ModelAPI_Feature::group(), true);
1093 while (aId < myChildren.size()) {
1094 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
1095 if ((aId < aNbItems) && ((aFirst + aId) < aNbOfFeatures)) {
1096 if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
1097 myChildren.removeAll(aNode);
1103 myChildren.removeAll(aNode);
1110 if (aNbItems > myChildren.size()) {
1112 ModuleBase_ITreeNode* aNode;
1113 for (int i = 0; i < aNbItems; i++) {
1114 ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
1115 if (i < myChildren.size()) {
1116 if (aObj != myChildren.at(i)->object()) {
1117 aNode = new PartSet_ObjectNode(aObj, this);
1118 myChildren.insert(i, aNode);
1123 aNode = new PartSet_ObjectNode(aObj, this);
1124 myChildren.append(aNode);
1131 QTreeNodesList PartSet_ObjectFolderNode::objectCreated(const QObjectPtrList& theObjects)
1133 QTreeNodesList aResult;
1134 int aFirst = -1, aLast = -1;
1135 PartSet_Tools::getFirstAndLastIndexInFolder(myObject, aFirst, aLast);
1136 if ((aFirst == -1) || (aLast == -1)) {
1139 int aNbItems = aLast - aFirst + 1;
1143 DocumentPtr aDoc = myObject->document();
1145 ModuleBase_ITreeNode* aNode;
1146 for (int i = 0; i < aNbItems; i++) {
1147 ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
1148 if (i < myChildren.size()) {
1149 if (aObj != myChildren.at(i)->object()) {
1150 aNode = new PartSet_ObjectNode(aObj, this);
1151 myChildren.insert(i, aNode);
1152 aResult.append(aNode);
1156 aNode = new PartSet_ObjectNode(aObj, this);
1157 myChildren.append(aNode);
1158 aResult.append(aNode);
1165 QTreeNodesList PartSet_ObjectFolderNode::objectsDeleted(const DocumentPtr& theDoc,
1166 const QString& theGroup)
1168 QTreeNodesList aResult;
1169 int aFirst = -1, aLast = -1;
1170 PartSet_Tools::getFirstAndLastIndexInFolder(myObject, aFirst, aLast);
1171 if ((aFirst == -1) || (aLast == -1)) {
1174 int aNbItems = aLast - aFirst + 1;
1178 if (aNbItems >= myChildren.size()) // Nothing was deleted here
1181 DocumentPtr aDoc = myObject->document();
1182 // Delete obsolete nodes
1183 bool aRemoved = false;
1185 int aNbOfFeatures = aDoc->size(ModelAPI_Feature::group(), true);
1186 while (aId < myChildren.size()) {
1187 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
1188 if ((aFirst + aId) < aNbOfFeatures) {
1189 if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
1190 myChildren.removeAll(aNode);
1197 myChildren.removeAll(aNode);
1198 aResult.removeAll(aNode);
1206 aResult.append(this);
1211 QVariant PartSet_ObjectFolderNode::data(int theColumn, int theRole) const
1213 if (theRole == Qt::ForegroundRole) {
1214 std::vector<int> aColor =
1215 Config_PropManager::color("Visualization", "feature_objectbrowser_color");
1216 return QColor(aColor[0], aColor[1], aColor[2]);
1218 return PartSet_ObjectNode::data(theColumn, theRole);
1222 //////////////////////////////////////////////////////////////////////////////////
1223 QVariant PartSet_StepNode::data(int theColumn, int theRole) const
1225 if ((theColumn == 1) && (theRole == Qt::DisplayRole)) {
1226 FieldStepPtr aStep =
1227 std::dynamic_pointer_cast<ModelAPI_ResultField::ModelAPI_FieldStep>(myObject);
1229 return "Step " + QString::number(aStep->id() + 1) + " " +
1230 aStep->field()->textLine(aStep->id()).c_str();
1232 return PartSet_ObjectNode::data(theColumn, theRole);
1235 ModuleBase_ITreeNode::VisibilityState PartSet_StepNode::visibilityState() const
1237 Qt::ItemFlags aFlags = flags(1);
1238 if (aFlags == Qt::ItemFlags())
1241 ModuleBase_IWorkshop* aWork = workshop();
1242 if (aWork->isVisible(myObject))