1 // Copyright (C) 2014-2020 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::fromStdWString(myObject->data()->name());
108 return aTitle + " = " + aVal;
110 return QString::fromStdWString(myObject->data()->name());
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 if (myObject->isDisabled())
137 return PartSet_TreeNode::data(theColumn, theRole);
138 std::vector<int> aColor =
139 Config_PropManager::color("Visualization", "feature_objectbrowser_color");
140 return QColor(aColor[0], aColor[1], aColor[2]);
144 return PartSet_TreeNode::data(theColumn, theRole);
147 Qt::ItemFlags PartSet_ObjectNode::flags(int theColumn) const
149 if (myObject->isDisabled()) {
150 return (theColumn == 2) ? Qt::ItemIsSelectable : aNullFlag;
152 DocumentPtr aDoc = myObject->document();
153 SessionPtr aSession = ModelAPI_Session::get();
154 if (aSession->activeDocument() == aDoc)
160 PartSet_ObjectNode::VisibilityState PartSet_ObjectNode::visibilityState() const
162 Qt::ItemFlags aFlags = flags(1);
163 if (aFlags == Qt::ItemFlags())
166 if (myObject->groupName() == ModelAPI_ResultParameter::group())
168 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
170 ModuleBase_IWorkshop* aWork = workshop();
171 ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResObj);
172 if (aCompRes.get()) {
173 std::list<ResultPtr> aResultsList;
174 ModelAPI_Tools::allSubs(aCompRes, aResultsList);
175 VisibilityState aState = aResultsList.size() == 0 ?
176 (aWork->isVisible(aCompRes) ? Visible : Hidden) : NoneState;
178 std::list<ResultPtr>::const_iterator aIt;
179 ResultBodyPtr aCompSub;
180 for (aIt = aResultsList.cbegin(); aIt != aResultsList.cend(); aIt++) {
181 ResultPtr aSubRes = (*aIt);
182 aCompSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aSubRes);
183 if (!(aCompSub.get() && aCompSub->numberOfSubs() > 0)) {
184 VisibilityState aS = aWork->isVisible(aSubRes) ? Visible : Hidden;
185 if (aState == NoneState)
187 else if (aState != aS) {
188 aState = SemiVisible;
195 if (aWork->isVisible(aResObj))
204 int PartSet_ObjectNode::numberOfSubs() const
206 ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
208 return aCompRes->numberOfSubs(true);
210 CompositeFeaturePtr aCompFeature =
211 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myObject);
212 if (aCompFeature.get() && aCompFeature->data()->isValid())
213 return aCompFeature->numberOfSubs(true);
215 ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
217 return aFieldRes->stepsSize();
224 ObjectPtr PartSet_ObjectNode::subObject(int theId) const
226 ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
228 return aCompRes->subResult(theId, true);
230 CompositeFeaturePtr aCompFeature =
231 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myObject);
232 if (aCompFeature.get())
233 return aCompFeature->subFeature(theId, true);
238 void PartSet_ObjectNode::update()
240 int aNb = numberOfSubs();
242 ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
244 // If the object is a field result then delete extra sub-objects
245 if (aFieldRes.get()) {
246 // Call shape in order to update content of Field.
247 // It is necessary to do for cases when field was created by script when module is inactive.
249 while (myChildren.size() > aNb) {
250 ModuleBase_ITreeNode* aNode = myChildren.last();
251 myChildren.removeAll(aNode);
257 ModuleBase_ITreeNode* aNode;
259 while (aId < myChildren.size()) {
260 aNode = myChildren.at(aId);
261 aObj = subObject(aId);
262 if (aNode->object() != aObj) {
263 myChildren.removeAll(aNode);
271 ModuleBase_ITreeNode* aNode;
274 for (i = 0; i < aNb; i++) {
275 aBody = subObject(i);
277 if (i < myChildren.size()) {
278 aNode = myChildren.at(i);
279 if (aNode->object() != aBody) {
280 ((PartSet_ObjectNode*)aNode)->setObject(aBody);
284 aNode = new PartSet_ObjectNode(aBody, this);
285 myChildren.append(aNode);
289 else if (aFieldRes.get()) {
290 FieldStepPtr aStep = aFieldRes->step(i);
292 if (i < myChildren.size()) {
293 PartSet_StepNode* aStepNode = static_cast<PartSet_StepNode*>(myChildren.at(i));
294 if (aStepNode->object() != aStep) {
295 aStepNode->setObject(aStep);
299 aNode = new PartSet_StepNode(aStep, this);
300 myChildren.append(aNode);
305 // Delete extra objects
306 while (myChildren.size() > aNb) {
307 aNode = myChildren.takeLast();
310 foreach(ModuleBase_ITreeNode* aChildNode, myChildren) {
311 aChildNode->update();
319 QTreeNodesList PartSet_ObjectNode::objectCreated(const QObjectPtrList& theObjects)
321 QTreeNodesList aResult;
322 int aNb = numberOfSubs();
324 ModuleBase_ITreeNode* aNode;
325 ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
328 // Call shape in order to update content of Field.
329 // It is necessary to do for cases when field was created by script when module is inactive.
332 for (i = 0; i < aNb; i++) {
333 aBody = subObject(i);
335 if (i < myChildren.size()) {
336 aNode = myChildren.at(i);
337 if (aNode->object() != aBody) {
338 ((PartSet_ObjectNode*)aNode)->setObject(aBody);
339 aResult.append(aNode);
343 aNode = new PartSet_ObjectNode(aBody, this);
344 myChildren.append(aNode);
345 aResult.append(aNode);
350 FieldStepPtr aStep = aFieldRes->step(i);
352 if (i < myChildren.size()) {
353 PartSet_StepNode* aStepNode = static_cast<PartSet_StepNode*>(myChildren.at(i));
354 if (aStepNode->object() != aStep) {
355 aStepNode->setObject(aStep);
359 aNode = new PartSet_StepNode(aStep, this);
360 myChildren.append(aNode);
365 foreach(ModuleBase_ITreeNode* aChildNode, myChildren) {
366 aResult.append(aChildNode->objectCreated(theObjects));
372 QTreeNodesList PartSet_ObjectNode::objectsDeleted(
373 const DocumentPtr& theDoc, const QString& theGroup)
375 QTreeNodesList aResult;
376 int aNb = numberOfSubs();
377 if (aNb != myChildren.size()) {
380 aResult.append(this);
383 // Delete extra objects
384 bool isDeleted = false;
386 ModuleBase_ITreeNode* aNode;
388 while (aId < myChildren.size()) {
389 aNode = myChildren.at(aId);
390 aObj = subObject(aId);
391 if (aNode->object() != aObj) {
392 myChildren.removeAll(aNode);
400 aResult.append(this);
403 foreach(ModuleBase_ITreeNode* aChildNode, myChildren) {
404 aBody = subObject(i);
405 ((PartSet_ObjectNode*)aChildNode)->setObject(aBody);
406 aResult.append(aChildNode->objectsDeleted(theDoc, theGroup));
413 //////////////////////////////////////////////////////////////////////////////////
414 PartSet_FolderNode::PartSet_FolderNode(ModuleBase_ITreeNode* theParent,
416 : PartSet_TreeNode(theParent), myType(theType)
420 QString PartSet_FolderNode::name() const
423 case ParametersFolder:
424 return QObject::tr("Parameters");
425 case ConstructionFolder:
426 return QObject::tr("Constructions");
428 return QObject::tr("Parts");
430 return QObject::tr("Results");
432 return QObject::tr("Fields");
434 return QObject::tr("Groups");
440 QVariant PartSet_FolderNode::data(int theColumn, int theRole) const
442 static QIcon aParamsIco(":pictures/params_folder.png");
443 static QIcon aConstrIco(":pictures/constr_folder.png");
445 if (theColumn == 1) {
447 case Qt::DisplayRole:
448 return name() + QString(" (%1)").arg(childrenCount());
449 case Qt::DecorationRole:
451 case ParametersFolder:
453 case ConstructionFolder:
466 if ((theColumn == 2) && (theRole == Qt::DecorationRole)) {
467 if (document().get()) {
468 SessionPtr aSession = ModelAPI_Session::get();
469 if (document() != aSession->activeDocument())
472 FeaturePtr aFeature = document()->currentFeature(true);
473 if (!aFeature.get()) { // There is no current feature
474 ModuleBase_ITreeNode* aLastFolder = 0;
475 foreach(ModuleBase_ITreeNode* aNode, parent()->children()) {
476 if (aNode->type() == PartSet_FolderNode::typeId())
481 if (aLastFolder == this)
482 return QIcon(":pictures/arrow.png");
488 return PartSet_TreeNode::data(theColumn, theRole);
491 Qt::ItemFlags PartSet_FolderNode::flags(int theColumn) const
493 SessionPtr aSession = ModelAPI_Session::get();
494 DocumentPtr aActiveDoc = aSession->activeDocument();
495 if (theColumn == 1) {
496 if (document() == aActiveDoc)
502 ModuleBase_ITreeNode* PartSet_FolderNode::createNode(const ObjectPtr& theObj)
504 //ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObj);
505 //if (aCompRes.get())
506 // return new PartSet_CompsolidNode(theObj, this);
507 ModuleBase_ITreeNode* aNode = new PartSet_ObjectNode(theObj, this);
512 void PartSet_FolderNode::update()
514 DocumentPtr aDoc = document();
518 // Remove extra sub-nodes
521 while (aId < myChildren.size()) {
522 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
523 aIndex = aDoc->index(aNode->object(), true);
524 if ((aIndex == -1) || (aId != aIndex)) {
525 myChildren.removeAll(aNode);
532 std::string aGroup = groupName();
533 int aSize = aDoc->size(aGroup, true);
534 for (int i = 0; i < aSize; i++) {
535 ObjectPtr aObj = aDoc->object(aGroup, i, true);
536 if (i < myChildren.size()) {
537 if (myChildren.at(i)->object() != aObj) {
538 ModuleBase_ITreeNode* aNode = createNode(aObj);
539 myChildren.insert(i, aNode);
542 ModuleBase_ITreeNode* aNode = createNode(aObj);
543 myChildren.append(aNode);
547 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
552 std::string PartSet_FolderNode::groupName() const
555 case ParametersFolder:
556 return ModelAPI_ResultParameter::group();
557 case ConstructionFolder:
558 return ModelAPI_ResultConstruction::group();
560 return ModelAPI_ResultPart::group();
562 return ModelAPI_ResultBody::group();
564 return ModelAPI_ResultField::group();
566 return ModelAPI_ResultGroup::group();
571 QTreeNodesList PartSet_FolderNode::objectCreated(const QObjectPtrList& theObjects)
573 QTreeNodesList aResult;
574 std::string aName = groupName();
575 DocumentPtr aDoc = document();
577 QMap<int, ModuleBase_ITreeNode*> aNewNodes;
578 foreach(ObjectPtr aObj, theObjects) {
579 if ((aObj->document() == aDoc) && (aObj->groupName() == aName)) {
580 aIdx = aDoc->index(aObj, true);
582 bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
584 ModuleBase_ITreeNode* aNode = createNode(aObj);
585 aNewNodes[aIdx] = aNode;
586 aResult.append(aNode);
592 // Add nodes in correct order
593 if (aNewNodes.size() > 0) {
595 for (i = 0; i < myChildren.size(); i++) {
596 if (aNewNodes.contains(i)) {
597 myChildren.insert(i, aNewNodes[i]);
601 while (aNewNodes.size()) {
602 i = myChildren.size();
603 myChildren.append(aNewNodes[i]);
607 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
608 aResult.append(aNode->objectCreated(theObjects));
613 QTreeNodesList PartSet_FolderNode::objectsDeleted(const DocumentPtr& theDoc,
614 const QString& theGroup)
616 DocumentPtr aDoc = document();
617 QTreeNodesList aResult;
618 if ((theGroup.toStdString() == groupName()) && (theDoc == aDoc)) {
619 QTreeNodesList aDelList;
622 bool aRemoved = false;
623 bool aToSort = false;
624 while (aId < myChildren.size()) {
625 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
626 aIndex = aDoc->index(aNode->object(), true);
627 aToSort |= ((aIndex != -1) && (aId != aIndex));
629 myChildren.removeAll(aNode);
637 aResult.append(this);
640 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
641 aResult.append(aNode->objectsDeleted(theDoc, theGroup));
647 //////////////////////////////////////////////////////////////////////////////////
648 QTreeNodesList PartSet_FeatureFolderNode::objectCreated(const QObjectPtrList& theObjects)
650 QTreeNodesList aResult;
651 // Process the root sub-objects
652 DocumentPtr aDoc = document();
654 int aNb = numberOfFolders();
655 QMap<int, ModuleBase_ITreeNode*> aNewNodes;
656 foreach(ObjectPtr aObj, theObjects) {
657 if (aDoc == aObj->document()) {
658 if ((aObj->groupName() == ModelAPI_Feature::group()) ||
659 (aObj->groupName() == ModelAPI_Folder::group())){
660 aIdx = aDoc->index(aObj, true);
662 ModuleBase_ITreeNode* aNode = createNode(aObj);
664 bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
666 aNewNodes[aIdx] = aNode;
667 aResult.append(aNode);
674 // To add in correct order
675 if (aNewNodes.size() > 0) {
677 for (i = 0; i < myChildren.size(); i++) {
678 if (aNewNodes.contains(i)) {
679 myChildren.insert(i, aNewNodes[i]);
683 while (aNewNodes.size()) {
684 i = myChildren.size();
685 if (aNewNodes.contains(i)) {
686 myChildren.append(aNewNodes[i]);
691 // Update sub-folders
692 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
693 aResult.append(aNode->objectCreated(theObjects));
698 QTreeNodesList PartSet_FeatureFolderNode::objectsDeleted(const DocumentPtr& theDoc,
699 const QString& theGroup)
701 QTreeNodesList aResult;
703 // Process sub-folders
704 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
705 if (aNode->childrenCount() > 0) { // aFolder node
706 QTreeNodesList aList = aNode->objectsDeleted(theDoc, theGroup);
707 if (aList.size() > 0)
708 aResult.append(aList);
713 DocumentPtr aDoc = document();
714 int aNb = numberOfFolders();
715 bool isGroup = ((theGroup.toStdString() == ModelAPI_Feature::group()) ||
716 (theGroup.toStdString() == ModelAPI_Folder::group()));
717 if ((theDoc == aDoc) && isGroup) {
720 bool aRemoved = false;
721 bool aToSort = false;
722 while (aId < myChildren.size()) {
723 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
724 if (aNode->object().get()) {
725 aIndex = aDoc->index(aNode->object(), true);
726 aToSort |= ((aIndex != -1) && (aId != (aIndex + aNb)));
728 myChildren.removeAll(aNode);
737 aResult.append(this);
744 ModuleBase_ITreeNode* PartSet_FeatureFolderNode::findParent(const DocumentPtr& theDoc,
747 ModuleBase_ITreeNode* aResult = 0;
748 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
749 aResult = aNode->findParent(theDoc, theGroup);
754 bool isGroup = ((theGroup.toStdString() == ModelAPI_Feature::group()) ||
755 (theGroup.toStdString() == ModelAPI_Folder::group()));
756 if ((theDoc == document()) && isGroup)
762 //////////////////////////////////////////////////////////////////////////////////
763 PartSet_RootNode::PartSet_RootNode() : PartSet_FeatureFolderNode(0), myWorkshop(0)
765 SessionPtr aSession = ModelAPI_Session::get();
766 DocumentPtr aDoc = aSession->moduleDocument();
768 myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
769 myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
770 myPartsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::PartsFolder);
772 myChildren.append(myParamsFolder);
773 myChildren.append(myConstrFolder);
774 myChildren.append(myPartsFolder);
780 void PartSet_RootNode::update()
782 myParamsFolder->update();
783 myConstrFolder->update();
784 myPartsFolder->update();
786 // Update features content
787 DocumentPtr aDoc = document();
788 int aNb = numberOfFolders();
790 // Remove extra sub-nodes
793 while (aId < myChildren.size()) {
794 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
795 if (aNode->object().get()) {
796 aIndex = aDoc->index(aNode->object(), true);
797 if ((aIndex == -1) || (aId != (aIndex + aNb))) {
798 myChildren.removeAll(aNode);
807 std::string aGroup = ModelAPI_Feature::group();
808 int aSize = aDoc->size(aGroup, true);
810 for (int i = 0; i < aSize; i++) {
811 ObjectPtr aObj = aDoc->object(aGroup, i, true);
812 aId = i + aNb; // Take into account existing folders
813 if (aId < myChildren.size()) {
814 if (myChildren.at(aId)->object() != aObj) {
815 ModuleBase_ITreeNode* aNode = createNode(aObj);
816 myChildren.insert(aId, aNode);
819 ModuleBase_ITreeNode* aNode = createNode(aObj);
820 myChildren.append(aNode);
823 // Update sub-folders
824 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
825 if ((aNode->type() == PartSet_ObjectFolderNode::typeId()) ||
826 (aNode->type() == PartSet_PartRootNode::typeId()))
831 DocumentPtr PartSet_RootNode::document() const
833 return ModelAPI_Session::get()->moduleDocument();
836 ModuleBase_ITreeNode* PartSet_RootNode::createNode(const ObjectPtr& theObj)
838 if (theObj->groupName() == ModelAPI_Folder::group())
839 return new PartSet_ObjectFolderNode(theObj, this);
841 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
842 if (aFeature->getKind() == PartSetPlugin_Part::ID())
843 return new PartSet_PartRootNode(theObj, this);
845 PartSet_ObjectNode* aNode = new PartSet_ObjectNode(theObj, this);
850 //////////////////////////////////////////////////////////////////////////////////
851 PartSet_PartRootNode::PartSet_PartRootNode(const ObjectPtr& theObj, ModuleBase_ITreeNode* theParent)
852 : PartSet_FeatureFolderNode(theParent), myObject(theObj)
854 myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
855 myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
856 myResultsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ResultsFolder);
857 myFieldsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::FieldsFolder);
858 myGroupsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::GroupsFolder);
860 myChildren.append(myParamsFolder);
861 myChildren.append(myConstrFolder);
862 myChildren.append(myResultsFolder);
867 void PartSet_PartRootNode::deleteChildren()
869 if (!myFieldsFolder->childrenCount()) {
870 delete myFieldsFolder;
872 if (!myGroupsFolder->childrenCount()) {
873 delete myGroupsFolder;
875 PartSet_FeatureFolderNode::deleteChildren();
879 void PartSet_PartRootNode::update()
881 DocumentPtr aDoc = document();
885 myParamsFolder->update();
886 myConstrFolder->update();
887 myResultsFolder->update();
888 myFieldsFolder->update();
889 myGroupsFolder->update();
891 bool aHasFields = myFieldsFolder->childrenCount() > 0;
892 bool aHasGroups = myGroupsFolder->childrenCount() > 0;
894 if (!myChildren.contains(myFieldsFolder)) {
895 myChildren.insert(3, myFieldsFolder);
897 } else if (myChildren.contains(myFieldsFolder)) {
898 myChildren.removeAll(myFieldsFolder);
901 if (!myChildren.contains(myGroupsFolder)) {
902 myChildren.insert(aHasFields ? 4 : 3, myGroupsFolder);
904 } else if (myChildren.contains(myGroupsFolder)) {
905 myChildren.removeAll(myGroupsFolder);
908 // Update features content
909 int aRows = numberOfFolders();
911 // Remove extra sub-nodes
914 QMap<int, ModuleBase_ITreeNode*> aExistingNodes;
915 while (aId < myChildren.size()) {
916 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
917 if (aNode->object().get()) {
918 aIndex = aDoc->index(aNode->object(), true);
919 if ((aIndex == -1) || (aId != (aIndex + aRows))) {
920 myChildren.removeAll(aNode);
924 aExistingNodes[aIndex + aRows] = aNode;
931 std::string aGroup = ModelAPI_Feature::group();
932 int aSize = aDoc->size(aGroup, true);
934 for (int i = 0; i < aSize; i++) {
935 ObjectPtr aObj = aDoc->object(aGroup, i, true);
936 aId = i + aRows; // Take into account existing folders
937 if (aId < myChildren.size()) {
938 if (myChildren.at(aId)->object() != aObj) {
939 if (aExistingNodes.contains(aId)) {
940 myChildren.insert(aId, aExistingNodes[aId]);
941 aExistingNodes.remove(aId);
944 myChildren.insert(aId, createNode(aObj));
948 if (aExistingNodes.contains(myChildren.size()))
949 myChildren.append(aExistingNodes[myChildren.size()]);
951 myChildren.append(createNode(aObj));
954 // Update sub-folders
955 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
956 if (aNode->type() == PartSet_ObjectFolderNode::typeId())
961 DocumentPtr PartSet_PartRootNode::document() const
963 ResultPartPtr aPartRes = getPartResult(myObject);
965 return aPartRes->partDoc();
966 return DocumentPtr();
969 QVariant PartSet_PartRootNode::data(int theColumn, int theRole) const
974 case Qt::DisplayRole:
976 ResultPartPtr aPartRes = getPartResult(myObject);
977 if (aPartRes.get()) {
978 if (aPartRes->partDoc().get() == NULL)
979 return QString::fromStdWString(myObject->data()->name()) + " (Not loaded)";
981 return QString::fromStdWString(myObject->data()->name());
983 case Qt::DecorationRole:
984 return ModuleBase_IconFactory::get()->getIcon(myObject);
987 if (theRole == Qt::DecorationRole) {
988 if (isCurrentFeature(myObject))
989 return QIcon(":pictures/arrow.png");
994 return PartSet_TreeNode::data(theColumn, theRole);
997 Qt::ItemFlags PartSet_PartRootNode::flags(int theColumn) const
999 if (myObject->isDisabled())
1000 return (theColumn == 2) ? Qt::ItemIsSelectable : aNullFlag;
1002 SessionPtr aSession = ModelAPI_Session::get();
1003 DocumentPtr aActiveDoc = aSession->activeDocument();
1004 if ((aActiveDoc == document()) || (myObject->document() == aActiveDoc))
1005 return aEditingFlag;
1006 return aDefaultFlag;
1009 ModuleBase_ITreeNode* PartSet_PartRootNode::createNode(const ObjectPtr& theObj)
1011 if (theObj->groupName() == ModelAPI_Folder::group())
1012 return new PartSet_ObjectFolderNode(theObj, this);
1013 PartSet_ObjectNode* aNode = new PartSet_ObjectNode(theObj, this);
1018 int PartSet_PartRootNode::numberOfFolders() const
1021 if (myFieldsFolder->childrenCount() > 0)
1023 if (myGroupsFolder->childrenCount() > 0)
1028 QTreeNodesList PartSet_PartRootNode::objectCreated(const QObjectPtrList& theObjects)
1030 QTreeNodesList aResult = PartSet_FeatureFolderNode::objectCreated(theObjects);
1031 if (!myFieldsFolder->childrenCount()) {
1032 QTreeNodesList aList = myFieldsFolder->objectCreated(theObjects);
1034 myChildren.insert(3, myFieldsFolder);
1035 aResult.append(myFieldsFolder);
1036 aResult.append(aList);
1039 if (!myGroupsFolder->childrenCount()) {
1040 QTreeNodesList aList = myGroupsFolder->objectCreated(theObjects);
1042 myChildren.insert(myFieldsFolder->childrenCount()? 4 : 3, myGroupsFolder);
1043 aResult.append(myGroupsFolder);
1044 aResult.append(aList);
1050 QTreeNodesList PartSet_PartRootNode::objectsDeleted(const DocumentPtr& theDoc,
1051 const QString& theGroup)
1053 QTreeNodesList aResult;
1054 if (myFieldsFolder->childrenCount()) {
1055 QTreeNodesList aList = myFieldsFolder->objectsDeleted(theDoc, theGroup);
1057 aResult.append(aList);
1058 if (!myFieldsFolder->childrenCount())
1059 myChildren.removeAll(myFieldsFolder);
1062 if (myGroupsFolder->childrenCount()) {
1063 QTreeNodesList aList = myGroupsFolder->objectsDeleted(theDoc, theGroup);
1065 aResult.append(aList);
1066 if (!myGroupsFolder->childrenCount())
1067 myChildren.removeAll(myGroupsFolder);
1070 aResult.append(PartSet_FeatureFolderNode::objectsDeleted(theDoc, theGroup));
1074 //////////////////////////////////////////////////////////////////////////////////
1075 void PartSet_ObjectFolderNode::update()
1077 int aFirst = -1, aLast = -1;
1078 PartSet_Tools::getFirstAndLastIndexInFolder(myObject, aFirst, aLast);
1079 if ((aFirst == -1) || (aLast == -1)) {
1084 int aNbItems = aLast - aFirst + 1;
1090 DocumentPtr aDoc = myObject->document();
1091 if (aNbItems < myChildren.size()) {
1092 // Delete obsolete nodes
1094 int aNbOfFeatures = aDoc->size(ModelAPI_Feature::group(), true);
1095 while (aId < myChildren.size()) {
1096 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
1097 if ((aId < aNbItems) && ((aFirst + aId) < aNbOfFeatures)) {
1098 if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
1099 myChildren.removeAll(aNode);
1105 myChildren.removeAll(aNode);
1112 if (aNbItems > myChildren.size()) {
1114 ModuleBase_ITreeNode* aNode;
1115 for (int i = 0; i < aNbItems; i++) {
1116 ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
1117 if (i < myChildren.size()) {
1118 if (aObj != myChildren.at(i)->object()) {
1119 aNode = new PartSet_ObjectNode(aObj, this);
1120 myChildren.insert(i, aNode);
1125 aNode = new PartSet_ObjectNode(aObj, this);
1126 myChildren.append(aNode);
1133 QTreeNodesList PartSet_ObjectFolderNode::objectCreated(const QObjectPtrList& /*theObjects*/)
1135 QTreeNodesList aResult;
1136 int aFirst = -1, aLast = -1;
1137 PartSet_Tools::getFirstAndLastIndexInFolder(myObject, aFirst, aLast);
1138 if ((aFirst == -1) || (aLast == -1)) {
1141 int aNbItems = aLast - aFirst + 1;
1145 DocumentPtr aDoc = myObject->document();
1147 ModuleBase_ITreeNode* aNode;
1148 for (int i = 0; i < aNbItems; i++) {
1149 ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
1150 if (i < myChildren.size()) {
1151 if (aObj != myChildren.at(i)->object()) {
1152 aNode = new PartSet_ObjectNode(aObj, this);
1153 myChildren.insert(i, aNode);
1154 aResult.append(aNode);
1158 aNode = new PartSet_ObjectNode(aObj, this);
1159 myChildren.append(aNode);
1160 aResult.append(aNode);
1167 QTreeNodesList PartSet_ObjectFolderNode::objectsDeleted(const DocumentPtr& /*theDoc*/,
1168 const QString& /*theGroup*/)
1170 QTreeNodesList aResult;
1171 int aFirst = -1, aLast = -1;
1172 PartSet_Tools::getFirstAndLastIndexInFolder(myObject, aFirst, aLast);
1173 if ((aFirst == -1) || (aLast == -1)) {
1176 int aNbItems = aLast - aFirst + 1;
1180 if (aNbItems >= myChildren.size()) // Nothing was deleted here
1183 DocumentPtr aDoc = myObject->document();
1184 // Delete obsolete nodes
1185 bool aRemoved = false;
1187 int aNbOfFeatures = aDoc->size(ModelAPI_Feature::group(), true);
1188 while (aId < myChildren.size()) {
1189 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
1190 if ((aFirst + aId) < aNbOfFeatures) {
1191 if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
1192 myChildren.removeAll(aNode);
1199 myChildren.removeAll(aNode);
1200 aResult.removeAll(aNode);
1208 aResult.append(this);
1213 QVariant PartSet_ObjectFolderNode::data(int theColumn, int theRole) const
1215 if (theRole == Qt::ForegroundRole) {
1216 if (!myObject->isDisabled()) {
1217 std::vector<int> aColor =
1218 Config_PropManager::color("Visualization", "feature_objectbrowser_color");
1219 return QColor(aColor[0], aColor[1], aColor[2]);
1222 return PartSet_ObjectNode::data(theColumn, theRole);
1226 //////////////////////////////////////////////////////////////////////////////////
1227 QVariant PartSet_StepNode::data(int theColumn, int theRole) const
1229 if ((theColumn == 1) && (theRole == Qt::DisplayRole)) {
1230 FieldStepPtr aStep =
1231 std::dynamic_pointer_cast<ModelAPI_ResultField::ModelAPI_FieldStep>(myObject);
1233 return "Step " + QString::number(aStep->id() + 1) + " " +
1234 aStep->field()->textLine(aStep->id()).c_str();
1236 return PartSet_ObjectNode::data(theColumn, theRole);
1239 ModuleBase_ITreeNode::VisibilityState PartSet_StepNode::visibilityState() const
1241 Qt::ItemFlags aFlags = flags(1);
1242 if (aFlags == Qt::ItemFlags())
1245 ModuleBase_IWorkshop* aWork = workshop();
1246 if (aWork->isVisible(myObject))