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>
47 #define ACTIVE_COLOR QColor(Qt::black)
48 #define SELECTABLE_COLOR QColor(100, 100, 100)
49 #define DISABLED_COLOR QColor(200, 200, 200)
51 Qt::ItemFlags aNullFlag;
52 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
53 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
56 ResultPartPtr getPartResult(const ObjectPtr& theObj)
58 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
60 ResultPtr aRes = aFeature->firstResult();
61 if (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group())) {
62 ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aRes);
63 // Use only original parts, not a placement results
64 if (aPartRes == aPartRes->original())
68 return ResultPartPtr();
71 bool isCurrentFeature(const ObjectPtr& theObj)
73 SessionPtr aSession = ModelAPI_Session::get();
74 DocumentPtr aCurDoc = aSession->activeDocument();
75 FeaturePtr aFeature = aCurDoc->currentFeature(true);
76 return aFeature == theObj;
79 //////////////////////////////////////////////////////////////////////////////////
80 QVariant PartSet_TreeNode::data(int theColumn, int theRole) const
82 if ((theColumn == 1) && (theRole == Qt::ForegroundRole)) {
83 Qt::ItemFlags aFlags = flags(theColumn);
84 if (aFlags == Qt::ItemFlags())
85 return QBrush(DISABLED_COLOR);
86 if (!aFlags.testFlag(Qt::ItemIsEditable))
87 return QBrush(SELECTABLE_COLOR);
90 return ModuleBase_ITreeNode::data(theColumn, theRole);
94 //////////////////////////////////////////////////////////////////////////////////
95 QVariant PartSet_ObjectNode::data(int theColumn, int theRole) const
100 if (myObject->groupName() == ModelAPI_ResultParameter::group()) {
101 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myObject);
102 AttributeDoublePtr aValueAttribute =
103 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
104 QString aVal = QString::number(aValueAttribute->value());
105 QString aTitle = QString(myObject->data()->name().c_str());
106 return aTitle + " = " + aVal;
108 return myObject->data()->name().c_str();
111 case Qt::DecorationRole:
114 switch (visibilityState()) {
118 return QIcon(":pictures/eyeopen.png");
120 return QIcon(":pictures/eyemiclosed.png");
122 return QIcon(":pictures/eyeclosed.png");
125 return ModuleBase_IconFactory::get()->getIcon(myObject);
127 if (isCurrentFeature(myObject))
128 return QIcon(":pictures/arrow.png");
133 return PartSet_TreeNode::data(theColumn, theRole);
136 Qt::ItemFlags PartSet_ObjectNode::flags(int theColumn) const
138 if (myObject->isDisabled()) {
139 return (theColumn == 2) ? Qt::ItemIsSelectable : aNullFlag;
141 DocumentPtr aDoc = myObject->document();
142 SessionPtr aSession = ModelAPI_Session::get();
143 if (aSession->activeDocument() == aDoc)
149 PartSet_ObjectNode::VisibilityState PartSet_ObjectNode::visibilityState() const
151 Qt::ItemFlags aFlags = flags(1);
152 if (aFlags == Qt::ItemFlags())
155 if (myObject->groupName() == ModelAPI_ResultParameter::group())
157 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
159 ModuleBase_IWorkshop* aWork = workshop();
160 ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResObj);
161 if (aCompRes.get()) {
162 std::list<ResultPtr> aResultsList;
163 ModelAPI_Tools::allSubs(aCompRes, aResultsList);
164 VisibilityState aState = aResultsList.size() == 0 ?
165 (aWork->isVisible(aCompRes) ? Visible : Hidden) : NoneState;
167 std::list<ResultPtr>::const_iterator aIt;
168 ResultBodyPtr aCompSub;
169 for (aIt = aResultsList.cbegin(); aIt != aResultsList.cend(); aIt++) {
170 ResultPtr aSubRes = (*aIt);
171 aCompSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aSubRes);
172 if (!(aCompSub.get() && aCompSub->numberOfSubs() > 0)) {
173 VisibilityState aS = aWork->isVisible(aSubRes) ? Visible : Hidden;
174 if (aState == NoneState)
176 else if (aState != aS) {
177 aState = SemiVisible;
184 if (aWork->isVisible(aResObj))
193 int PartSet_ObjectNode::numberOfSubs() const
195 ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
197 return aCompRes->numberOfSubs(true);
199 CompositeFeaturePtr aCompFeature =
200 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myObject);
201 if (aCompFeature.get() && aCompFeature->data()->isValid())
202 return aCompFeature->numberOfSubs(true);
204 ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
206 return aFieldRes->stepsSize();
213 ObjectPtr PartSet_ObjectNode::subObject(int theId) const
215 ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
217 return aCompRes->subResult(theId, true);
219 CompositeFeaturePtr aCompFeature =
220 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myObject);
221 if (aCompFeature.get())
222 return aCompFeature->subFeature(theId, true);
227 void PartSet_ObjectNode::update()
229 int aNb = numberOfSubs();
231 ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
233 // If the object is a field result then delete extra sub-objects
234 if (aFieldRes.get()) {
235 // Call shape in order to update content of Field.
236 // It is necessary to do for cases when field was created by script when module is inactive.
238 while (myChildren.size() > aNb) {
239 ModuleBase_ITreeNode* aNode = myChildren.last();
240 myChildren.removeAll(aNode);
246 ModuleBase_ITreeNode* aNode;
248 while (aId < myChildren.size()) {
249 aNode = myChildren.at(aId);
250 aObj = subObject(aId);
251 if (aNode->object() != aObj) {
252 myChildren.removeAll(aNode);
260 ModuleBase_ITreeNode* aNode;
263 for (i = 0; i < aNb; i++) {
264 aBody = subObject(i);
266 if (i < myChildren.size()) {
267 aNode = myChildren.at(i);
268 if (aNode->object() != aBody) {
269 ((PartSet_ObjectNode*)aNode)->setObject(aBody);
273 aNode = new PartSet_ObjectNode(aBody, this);
274 myChildren.append(aNode);
278 else if (aFieldRes.get()) {
279 FieldStepPtr aStep = aFieldRes->step(i);
281 if (i < myChildren.size()) {
282 PartSet_StepNode* aStepNode = static_cast<PartSet_StepNode*>(myChildren.at(i));
283 if (aStepNode->object() != aStep) {
284 aStepNode->setObject(aStep);
288 aNode = new PartSet_StepNode(aStep, this);
289 myChildren.append(aNode);
294 // Delete extra objects
295 while (myChildren.size() > aNb) {
296 aNode = myChildren.takeLast();
299 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
308 QTreeNodesList PartSet_ObjectNode::objectCreated(const QObjectPtrList& theObjects)
310 QTreeNodesList aResult;
311 int aNb = numberOfSubs();
313 ModuleBase_ITreeNode* aNode;
314 ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
317 // Call shape in order to update content of Field.
318 // It is necessary to do for cases when field was created by script when module is inactive.
321 for (i = 0; i < aNb; i++) {
322 aBody = subObject(i);
324 if (i < myChildren.size()) {
325 aNode = myChildren.at(i);
326 if (aNode->object() != aBody) {
327 ((PartSet_ObjectNode*)aNode)->setObject(aBody);
328 aResult.append(aNode);
332 aNode = new PartSet_ObjectNode(aBody, this);
333 myChildren.append(aNode);
334 aResult.append(aNode);
339 FieldStepPtr aStep = aFieldRes->step(i);
341 if (i < myChildren.size()) {
342 PartSet_StepNode* aStepNode = static_cast<PartSet_StepNode*>(myChildren.at(i));
343 if (aStepNode->object() != aStep) {
344 aStepNode->setObject(aStep);
348 aNode = new PartSet_StepNode(aStep, this);
349 myChildren.append(aNode);
354 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
355 aResult.append(aNode->objectCreated(theObjects));
361 QTreeNodesList PartSet_ObjectNode::objectsDeleted(
362 const DocumentPtr& theDoc, const QString& theGroup)
364 QTreeNodesList aResult;
365 int aNb = numberOfSubs();
366 if (aNb != myChildren.size()) {
369 aResult.append(this);
372 // Delete extra objects
373 bool isDeleted = false;
375 ModuleBase_ITreeNode* aNode;
377 while (aId < myChildren.size()) {
378 aNode = myChildren.at(aId);
379 aObj = subObject(aId);
380 if (aNode->object() != aObj) {
381 myChildren.removeAll(aNode);
389 aResult.append(this);
392 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
393 aBody = subObject(i);
394 ((PartSet_ObjectNode*)aNode)->setObject(aBody);
395 aResult.append(aNode->objectsDeleted(theDoc, theGroup));
402 //////////////////////////////////////////////////////////////////////////////////
403 PartSet_FolderNode::PartSet_FolderNode(ModuleBase_ITreeNode* theParent,
405 : PartSet_TreeNode(theParent), myType(theType)
409 QString PartSet_FolderNode::name() const
412 case ParametersFolder:
413 return QObject::tr("Parameters");
414 case ConstructionFolder:
415 return QObject::tr("Constructions");
417 return QObject::tr("Parts");
419 return QObject::tr("Results");
421 return QObject::tr("Fields");
423 return QObject::tr("Groups");
429 QVariant PartSet_FolderNode::data(int theColumn, int theRole) const
431 static QIcon aParamsIco(":pictures/params_folder.png");
432 static QIcon aConstrIco(":pictures/constr_folder.png");
434 if (theColumn == 1) {
436 case Qt::DisplayRole:
437 return name() + QString(" (%1)").arg(childrenCount());
438 case Qt::DecorationRole:
440 case ParametersFolder:
442 case ConstructionFolder:
455 if ((theColumn == 2) && (theRole == Qt::DecorationRole)) {
456 if (document().get()) {
457 SessionPtr aSession = ModelAPI_Session::get();
458 if (document() != aSession->activeDocument())
461 FeaturePtr aFeature = document()->currentFeature(true);
462 if (!aFeature.get()) { // There is no current feature
463 ModuleBase_ITreeNode* aLastFolder = 0;
464 foreach(ModuleBase_ITreeNode* aNode, parent()->children()) {
465 if (aNode->type() == PartSet_FolderNode::typeId())
470 if (aLastFolder == this)
471 return QIcon(":pictures/arrow.png");
477 return PartSet_TreeNode::data(theColumn, theRole);
480 Qt::ItemFlags PartSet_FolderNode::flags(int theColumn) const
482 SessionPtr aSession = ModelAPI_Session::get();
483 DocumentPtr aActiveDoc = aSession->activeDocument();
484 if (theColumn == 1) {
485 if (document() == aActiveDoc)
491 ModuleBase_ITreeNode* PartSet_FolderNode::createNode(const ObjectPtr& theObj)
493 //ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObj);
494 //if (aCompRes.get())
495 // return new PartSet_CompsolidNode(theObj, this);
496 ModuleBase_ITreeNode* aNode = new PartSet_ObjectNode(theObj, this);
501 void PartSet_FolderNode::update()
503 DocumentPtr aDoc = document();
507 // Remove extra sub-nodes
510 while (aId < myChildren.size()) {
511 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
512 aIndex = aDoc->index(aNode->object(), true);
513 if ((aIndex == -1) || (aId != aIndex)) {
514 myChildren.removeAll(aNode);
521 std::string aGroup = groupName();
522 int aSize = aDoc->size(aGroup, true);
523 for (int i = 0; i < aSize; i++) {
524 ObjectPtr aObj = aDoc->object(aGroup, i, true);
525 if (i < myChildren.size()) {
526 if (myChildren.at(i)->object() != aObj) {
527 ModuleBase_ITreeNode* aNode = createNode(aObj);
528 myChildren.insert(i, aNode);
531 ModuleBase_ITreeNode* aNode = createNode(aObj);
532 myChildren.append(aNode);
536 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
541 std::string PartSet_FolderNode::groupName() const
544 case ParametersFolder:
545 return ModelAPI_ResultParameter::group();
546 case ConstructionFolder:
547 return ModelAPI_ResultConstruction::group();
549 return ModelAPI_ResultPart::group();
551 return ModelAPI_ResultBody::group();
553 return ModelAPI_ResultField::group();
555 return ModelAPI_ResultGroup::group();
560 QTreeNodesList PartSet_FolderNode::objectCreated(const QObjectPtrList& theObjects)
562 QTreeNodesList aResult;
563 std::string aName = groupName();
564 DocumentPtr aDoc = document();
566 QMap<int, ModuleBase_ITreeNode*> aNewNodes;
567 foreach(ObjectPtr aObj, theObjects) {
568 if ((aObj->document() == aDoc) && (aObj->groupName() == aName)) {
569 aIdx = aDoc->index(aObj, true);
571 bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
573 ModuleBase_ITreeNode* aNode = createNode(aObj);
574 aNewNodes[aIdx] = aNode;
575 aResult.append(aNode);
581 // Add nodes in correct order
582 if (aNewNodes.size() > 0) {
584 for (i = 0; i < myChildren.size(); i++) {
585 if (aNewNodes.contains(i)) {
586 myChildren.insert(i, aNewNodes[i]);
590 while (aNewNodes.size()) {
591 i = myChildren.size();
592 myChildren.append(aNewNodes[i]);
596 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
597 aResult.append(aNode->objectCreated(theObjects));
602 QTreeNodesList PartSet_FolderNode::objectsDeleted(const DocumentPtr& theDoc,
603 const QString& theGroup)
605 DocumentPtr aDoc = document();
606 QTreeNodesList aResult;
607 if ((theGroup.toStdString() == groupName()) && (theDoc == aDoc)) {
608 QTreeNodesList aDelList;
611 bool aRemoved = false;
612 bool aToSort = false;
613 while (aId < myChildren.size()) {
614 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
615 aIndex = aDoc->index(aNode->object(), true);
616 aToSort |= ((aIndex != -1) && (aId != aIndex));
618 myChildren.removeAll(aNode);
626 aResult.append(this);
629 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
630 aResult.append(aNode->objectsDeleted(theDoc, theGroup));
636 //////////////////////////////////////////////////////////////////////////////////
637 QTreeNodesList PartSet_FeatureFolderNode::objectCreated(const QObjectPtrList& theObjects)
639 QTreeNodesList aResult;
640 // Process the root sub-objects
641 DocumentPtr aDoc = document();
643 int aNb = numberOfFolders();
644 QMap<int, ModuleBase_ITreeNode*> aNewNodes;
645 foreach(ObjectPtr aObj, theObjects) {
646 if (aDoc == aObj->document()) {
647 if ((aObj->groupName() == ModelAPI_Feature::group()) ||
648 (aObj->groupName() == ModelAPI_Folder::group())){
649 aIdx = aDoc->index(aObj, true);
651 ModuleBase_ITreeNode* aNode = createNode(aObj);
653 bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
655 aNewNodes[aIdx] = aNode;
656 aResult.append(aNode);
663 // To add in correct order
664 if (aNewNodes.size() > 0) {
666 for (i = 0; i < myChildren.size(); i++) {
667 if (aNewNodes.contains(i)) {
668 myChildren.insert(i, aNewNodes[i]);
672 while (aNewNodes.size()) {
673 i = myChildren.size();
674 if (aNewNodes.contains(i)) {
675 myChildren.append(aNewNodes[i]);
680 // Update sub-folders
681 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
682 aResult.append(aNode->objectCreated(theObjects));
687 QTreeNodesList PartSet_FeatureFolderNode::objectsDeleted(const DocumentPtr& theDoc,
688 const QString& theGroup)
690 QTreeNodesList aResult;
692 // Process sub-folders
693 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
694 if (aNode->childrenCount() > 0) { // aFolder node
695 QTreeNodesList aList = aNode->objectsDeleted(theDoc, theGroup);
696 if (aList.size() > 0)
697 aResult.append(aList);
702 DocumentPtr aDoc = document();
703 int aNb = numberOfFolders();
704 bool isGroup = ((theGroup.toStdString() == ModelAPI_Feature::group()) ||
705 (theGroup.toStdString() == ModelAPI_Folder::group()));
706 if ((theDoc == aDoc) && isGroup) {
709 bool aRemoved = false;
710 bool aToSort = false;
711 while (aId < myChildren.size()) {
712 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
713 if (aNode->object().get()) {
714 aIndex = aDoc->index(aNode->object(), true);
715 aToSort |= ((aIndex != -1) && (aId != (aIndex + aNb)));
717 myChildren.removeAll(aNode);
726 aResult.append(this);
733 ModuleBase_ITreeNode* PartSet_FeatureFolderNode::findParent(const DocumentPtr& theDoc,
736 ModuleBase_ITreeNode* aResult = 0;
737 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
738 aResult = aNode->findParent(theDoc, theGroup);
743 bool isGroup = ((theGroup.toStdString() == ModelAPI_Feature::group()) ||
744 (theGroup.toStdString() == ModelAPI_Folder::group()));
745 if ((theDoc == document()) && isGroup)
751 //////////////////////////////////////////////////////////////////////////////////
752 PartSet_RootNode::PartSet_RootNode() : PartSet_FeatureFolderNode(0), myWorkshop(0)
754 SessionPtr aSession = ModelAPI_Session::get();
755 DocumentPtr aDoc = aSession->moduleDocument();
757 myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
758 myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
759 myPartsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::PartsFolder);
761 myChildren.append(myParamsFolder);
762 myChildren.append(myConstrFolder);
763 myChildren.append(myPartsFolder);
769 void PartSet_RootNode::update()
771 myParamsFolder->update();
772 myConstrFolder->update();
773 myPartsFolder->update();
775 // Update features content
776 DocumentPtr aDoc = document();
777 int aNb = numberOfFolders();
779 // Remove extra sub-nodes
782 while (aId < myChildren.size()) {
783 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
784 if (aNode->object().get()) {
785 aIndex = aDoc->index(aNode->object(), true);
786 if ((aIndex == -1) || (aId != (aIndex + aNb))) {
787 myChildren.removeAll(aNode);
796 std::string aGroup = ModelAPI_Feature::group();
797 int aSize = aDoc->size(aGroup, true);
799 for (int i = 0; i < aSize; i++) {
800 ObjectPtr aObj = aDoc->object(aGroup, i, true);
801 aId = i + aNb; // Take into account existing folders
802 if (aId < myChildren.size()) {
803 if (myChildren.at(aId)->object() != aObj) {
804 ModuleBase_ITreeNode* aNode = createNode(aObj);
805 myChildren.insert(aId, aNode);
808 ModuleBase_ITreeNode* aNode = createNode(aObj);
809 myChildren.append(aNode);
812 // Update sub-folders
813 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
814 if ((aNode->type() == PartSet_ObjectFolderNode::typeId()) ||
815 (aNode->type() == PartSet_PartRootNode::typeId()))
820 DocumentPtr PartSet_RootNode::document() const
822 return ModelAPI_Session::get()->moduleDocument();
825 ModuleBase_ITreeNode* PartSet_RootNode::createNode(const ObjectPtr& theObj)
827 if (theObj->groupName() == ModelAPI_Folder::group())
828 return new PartSet_ObjectFolderNode(theObj, this);
830 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
831 if (aFeature->getKind() == PartSetPlugin_Part::ID())
832 return new PartSet_PartRootNode(theObj, this);
834 PartSet_ObjectNode* aNode = new PartSet_ObjectNode(theObj, this);
839 //////////////////////////////////////////////////////////////////////////////////
840 PartSet_PartRootNode::PartSet_PartRootNode(const ObjectPtr& theObj, ModuleBase_ITreeNode* theParent)
841 : PartSet_FeatureFolderNode(theParent), myObject(theObj)
843 myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
844 myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
845 myResultsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ResultsFolder);
846 myFieldsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::FieldsFolder);
847 myGroupsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::GroupsFolder);
849 myChildren.append(myParamsFolder);
850 myChildren.append(myConstrFolder);
851 myChildren.append(myResultsFolder);
856 void PartSet_PartRootNode::deleteChildren()
858 if (!myFieldsFolder->childrenCount()) {
859 delete myFieldsFolder;
861 if (!myGroupsFolder->childrenCount()) {
862 delete myGroupsFolder;
864 PartSet_FeatureFolderNode::deleteChildren();
868 void PartSet_PartRootNode::update()
870 DocumentPtr aDoc = document();
874 myParamsFolder->update();
875 myConstrFolder->update();
876 myResultsFolder->update();
877 myFieldsFolder->update();
878 myGroupsFolder->update();
880 bool aHasFields = myFieldsFolder->childrenCount() > 0;
881 bool aHasGroups = myGroupsFolder->childrenCount() > 0;
883 if (!myChildren.contains(myFieldsFolder)) {
884 myChildren.insert(3, myFieldsFolder);
886 } else if (myChildren.contains(myFieldsFolder)) {
887 myChildren.removeAll(myFieldsFolder);
890 if (!myChildren.contains(myGroupsFolder)) {
891 myChildren.insert(aHasFields ? 4 : 3, myGroupsFolder);
893 } else if (myChildren.contains(myGroupsFolder)) {
894 myChildren.removeAll(myGroupsFolder);
897 // Update features content
898 int aRows = numberOfFolders();
900 // Remove extra sub-nodes
903 QMap<int, ModuleBase_ITreeNode*> aExistingNodes;
904 while (aId < myChildren.size()) {
905 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
906 if (aNode->object().get()) {
907 aIndex = aDoc->index(aNode->object(), true);
908 if ((aIndex == -1) || (aId != (aIndex + aRows))) {
909 myChildren.removeAll(aNode);
913 aExistingNodes[aIndex + aRows] = aNode;
920 std::string aGroup = ModelAPI_Feature::group();
921 int aSize = aDoc->size(aGroup, true);
923 for (int i = 0; i < aSize; i++) {
924 ObjectPtr aObj = aDoc->object(aGroup, i, true);
925 aId = i + aRows; // Take into account existing folders
926 if (aId < myChildren.size()) {
927 if (myChildren.at(aId)->object() != aObj) {
928 if (aExistingNodes.contains(aId)) {
929 myChildren.insert(aId, aExistingNodes[aId]);
930 aExistingNodes.remove(aId);
933 myChildren.insert(aId, createNode(aObj));
937 if (aExistingNodes.contains(myChildren.size()))
938 myChildren.append(aExistingNodes[myChildren.size()]);
940 myChildren.append(createNode(aObj));
943 // Update sub-folders
944 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
945 if (aNode->type() == PartSet_ObjectFolderNode::typeId())
950 DocumentPtr PartSet_PartRootNode::document() const
952 ResultPartPtr aPartRes = getPartResult(myObject);
954 return aPartRes->partDoc();
955 return DocumentPtr();
958 QVariant PartSet_PartRootNode::data(int theColumn, int theRole) const
963 case Qt::DisplayRole:
965 ResultPartPtr aPartRes = getPartResult(myObject);
966 if (aPartRes.get()) {
967 if (aPartRes->partDoc().get() == NULL)
968 return QString(myObject->data()->name().c_str()) + " (Not loaded)";
970 return QString(myObject->data()->name().c_str());
972 case Qt::DecorationRole:
973 return ModuleBase_IconFactory::get()->getIcon(myObject);
976 if (theRole == Qt::DecorationRole) {
977 if (isCurrentFeature(myObject))
978 return QIcon(":pictures/arrow.png");
983 return PartSet_TreeNode::data(theColumn, theRole);
986 Qt::ItemFlags PartSet_PartRootNode::flags(int theColumn) const
988 if (myObject->isDisabled())
989 return (theColumn == 2) ? Qt::ItemIsSelectable : aNullFlag;
991 SessionPtr aSession = ModelAPI_Session::get();
992 DocumentPtr aActiveDoc = aSession->activeDocument();
993 if ((aActiveDoc == document()) || (myObject->document() == aActiveDoc))
998 ModuleBase_ITreeNode* PartSet_PartRootNode::createNode(const ObjectPtr& theObj)
1000 if (theObj->groupName() == ModelAPI_Folder::group())
1001 return new PartSet_ObjectFolderNode(theObj, this);
1002 PartSet_ObjectNode* aNode = new PartSet_ObjectNode(theObj, this);
1007 int PartSet_PartRootNode::numberOfFolders() const
1010 if (myFieldsFolder->childrenCount() > 0)
1012 if (myGroupsFolder->childrenCount() > 0)
1017 QTreeNodesList PartSet_PartRootNode::objectCreated(const QObjectPtrList& theObjects)
1019 QTreeNodesList aResult = PartSet_FeatureFolderNode::objectCreated(theObjects);
1020 if (!myFieldsFolder->childrenCount()) {
1021 QTreeNodesList aList = myFieldsFolder->objectCreated(theObjects);
1023 myChildren.insert(3, myFieldsFolder);
1024 aResult.append(myFieldsFolder);
1025 aResult.append(aList);
1028 if (!myGroupsFolder->childrenCount()) {
1029 QTreeNodesList aList = myGroupsFolder->objectCreated(theObjects);
1031 myChildren.insert(myFieldsFolder->childrenCount()? 4 : 3, myGroupsFolder);
1032 aResult.append(myGroupsFolder);
1033 aResult.append(aList);
1039 QTreeNodesList PartSet_PartRootNode::objectsDeleted(const DocumentPtr& theDoc,
1040 const QString& theGroup)
1042 QTreeNodesList aResult;
1043 if (myFieldsFolder->childrenCount()) {
1044 QTreeNodesList aList = myFieldsFolder->objectsDeleted(theDoc, theGroup);
1046 aResult.append(aList);
1047 if (!myFieldsFolder->childrenCount())
1048 myChildren.removeAll(myFieldsFolder);
1051 if (myGroupsFolder->childrenCount()) {
1052 QTreeNodesList aList = myGroupsFolder->objectsDeleted(theDoc, theGroup);
1054 aResult.append(aList);
1055 if (!myGroupsFolder->childrenCount())
1056 myChildren.removeAll(myGroupsFolder);
1059 aResult.append(PartSet_FeatureFolderNode::objectsDeleted(theDoc, theGroup));
1063 //////////////////////////////////////////////////////////////////////////////////
1064 void PartSet_ObjectFolderNode::update()
1066 int aFirst = -1, aLast = -1;
1067 PartSet_Tools::getFirstAndLastIndexInFolder(myObject, aFirst, aLast);
1068 if ((aFirst == -1) || (aLast == -1)) {
1073 int aNbItems = aLast - aFirst + 1;
1079 DocumentPtr aDoc = myObject->document();
1080 if (aNbItems < myChildren.size()) {
1081 // Delete obsolete nodes
1083 int aNbOfFeatures = aDoc->size(ModelAPI_Feature::group(), true);
1084 while (aId < myChildren.size()) {
1085 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
1086 if ((aFirst + aId) < aNbOfFeatures) {
1087 if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
1088 myChildren.removeAll(aNode);
1094 myChildren.removeAll(aNode);
1101 if (aNbItems > myChildren.size()) {
1103 ModuleBase_ITreeNode* aNode;
1104 for (int i = 0; i < aNbItems; i++) {
1105 ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
1106 if (i < myChildren.size()) {
1107 if (aObj != myChildren.at(i)->object()) {
1108 aNode = new PartSet_ObjectNode(aObj, this);
1109 myChildren.insert(i, aNode);
1114 aNode = new PartSet_ObjectNode(aObj, this);
1115 myChildren.append(aNode);
1122 QTreeNodesList PartSet_ObjectFolderNode::objectCreated(const QObjectPtrList& theObjects)
1124 QTreeNodesList aResult;
1125 int aFirst = -1, aLast = -1;
1126 PartSet_Tools::getFirstAndLastIndexInFolder(myObject, aFirst, aLast);
1127 if ((aFirst == -1) || (aLast == -1)) {
1130 int aNbItems = aLast - aFirst + 1;
1134 DocumentPtr aDoc = myObject->document();
1136 ModuleBase_ITreeNode* aNode;
1137 for (int i = 0; i < aNbItems; i++) {
1138 ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
1139 if (i < myChildren.size()) {
1140 if (aObj != myChildren.at(i)->object()) {
1141 aNode = new PartSet_ObjectNode(aObj, this);
1142 myChildren.insert(i, aNode);
1143 aResult.append(aNode);
1147 aNode = new PartSet_ObjectNode(aObj, this);
1148 myChildren.append(aNode);
1149 aResult.append(aNode);
1156 QTreeNodesList PartSet_ObjectFolderNode::objectsDeleted(const DocumentPtr& theDoc,
1157 const QString& theGroup)
1159 QTreeNodesList aResult;
1160 int aFirst = -1, aLast = -1;
1161 PartSet_Tools::getFirstAndLastIndexInFolder(myObject, aFirst, aLast);
1162 if ((aFirst == -1) || (aLast == -1)) {
1165 int aNbItems = aLast - aFirst + 1;
1169 if (aNbItems >= myChildren.size()) // Nothing was deleted here
1172 DocumentPtr aDoc = myObject->document();
1173 // Delete obsolete nodes
1174 bool aRemoved = false;
1176 int aNbOfFeatures = aDoc->size(ModelAPI_Feature::group(), true);
1177 while (aId < myChildren.size()) {
1178 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
1179 if ((aFirst + aId) < aNbOfFeatures) {
1180 if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
1181 myChildren.removeAll(aNode);
1188 myChildren.removeAll(aNode);
1189 aResult.removeAll(aNode);
1197 aResult.append(this);
1204 //////////////////////////////////////////////////////////////////////////////////
1205 QVariant PartSet_StepNode::data(int theColumn, int theRole) const
1207 if ((theColumn == 1) && (theRole == Qt::DisplayRole)) {
1208 FieldStepPtr aStep =
1209 std::dynamic_pointer_cast<ModelAPI_ResultField::ModelAPI_FieldStep>(myObject);
1211 return "Step " + QString::number(aStep->id() + 1) + " " +
1212 aStep->field()->textLine(aStep->id()).c_str();
1214 return PartSet_ObjectNode::data(theColumn, theRole);
1217 ModuleBase_ITreeNode::VisibilityState PartSet_StepNode::visibilityState() const
1219 Qt::ItemFlags aFlags = flags(1);
1220 if (aFlags == Qt::ItemFlags())
1223 ModuleBase_IWorkshop* aWork = workshop();
1224 if (aWork->isVisible(myObject))