1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "PartSet_TreeNodes.h"
22 #include "PartSet_Tools.h"
24 #include <ModuleBase_IconFactory.h>
25 #include <ModuleBase_IWorkshop.h>
26 #include <ModuleBase_Tools.h>
28 #include <PartSetPlugin_Part.h>
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_ResultParameter.h>
32 #include <ModelAPI_ResultField.h>
33 #include <ModelAPI_ResultGroup.h>
34 #include <ModelAPI_ResultConstruction.h>
35 #include <ModelAPI_ResultPart.h>
36 #include <ModelAPI_ResultBody.h>
37 #include <ModelAPI_Tools.h>
38 #include <ModelAPI_ResultBody.h>
39 #include <ModelAPI_CompositeFeature.h>
40 #include <ModelAPI_AttributeDouble.h>
41 #include <ModelAPI_Folder.h>
42 #include <ModelAPI_AttributeReference.h>
48 #define ACTIVE_COLOR QColor(Qt::black)
49 #define SELECTABLE_COLOR QColor(100, 100, 100)
50 #define DISABLED_COLOR QColor(200, 200, 200)
52 Qt::ItemFlags aNullFlag;
53 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
54 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
57 ResultPartPtr getPartResult(const ObjectPtr& theObj)
59 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
61 ResultPtr aRes = aFeature->firstResult();
62 if (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group())) {
63 ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aRes);
64 // Use only original parts, not a placement results
65 if (aPartRes == aPartRes->original())
69 return ResultPartPtr();
72 bool isCurrentFeature(const ObjectPtr& theObj)
74 SessionPtr aSession = ModelAPI_Session::get();
75 DocumentPtr aCurDoc = aSession->activeDocument();
76 FeaturePtr aFeature = aCurDoc->currentFeature(true);
77 return aFeature == theObj;
80 //////////////////////////////////////////////////////////////////////////////////
81 QVariant PartSet_TreeNode::data(int theColumn, int theRole) const
83 if ((theColumn == 1) && (theRole == Qt::ForegroundRole)) {
84 Qt::ItemFlags aFlags = flags(theColumn);
85 if (aFlags == Qt::ItemFlags())
86 return QBrush(DISABLED_COLOR);
87 if (!aFlags.testFlag(Qt::ItemIsEditable))
88 return QBrush(SELECTABLE_COLOR);
91 return ModuleBase_ITreeNode::data(theColumn, theRole);
95 //////////////////////////////////////////////////////////////////////////////////
96 QVariant PartSet_ObjectNode::data(int theColumn, int theRole) const
100 if (theColumn == 1) {
101 if (myObject->groupName() == ModelAPI_ResultParameter::group()) {
102 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myObject);
103 AttributeDoublePtr aValueAttribute =
104 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
105 QString aVal = QString::number(aValueAttribute->value());
106 QString aTitle = QString(myObject->data()->name().c_str());
107 return aTitle + " = " + aVal;
109 return myObject->data()->name().c_str();
112 case Qt::DecorationRole:
115 switch (visibilityState()) {
119 return QIcon(":pictures/eyeopen.png");
121 return QIcon(":pictures/eyemiclosed.png");
123 return QIcon(":pictures/eyeclosed.png");
126 return ModuleBase_IconFactory::get()->getIcon(myObject);
128 if (isCurrentFeature(myObject))
129 return QIcon(":pictures/arrow.png");
134 return PartSet_TreeNode::data(theColumn, theRole);
137 Qt::ItemFlags PartSet_ObjectNode::flags(int theColumn) const
139 if (myObject->isDisabled()) {
140 return (theColumn == 2) ? Qt::ItemIsSelectable : aNullFlag;
142 DocumentPtr aDoc = myObject->document();
143 SessionPtr aSession = ModelAPI_Session::get();
144 if (aSession->activeDocument() == aDoc)
150 PartSet_ObjectNode::VisibilityState PartSet_ObjectNode::visibilityState() const
152 Qt::ItemFlags aFlags = flags(1);
153 if (aFlags == Qt::ItemFlags())
156 if (myObject->groupName() == ModelAPI_ResultParameter::group())
158 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
160 ModuleBase_IWorkshop* aWork = workshop();
161 ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResObj);
162 if (aCompRes.get()) {
163 std::list<ResultPtr> aResultsList;
164 ModelAPI_Tools::allSubs(aCompRes, aResultsList);
165 VisibilityState aState = aResultsList.size() == 0 ?
166 (aWork->isVisible(aCompRes) ? Visible : Hidden) : NoneState;
168 std::list<ResultPtr>::const_iterator aIt;
169 ResultBodyPtr aCompSub;
170 for (aIt = aResultsList.cbegin(); aIt != aResultsList.cend(); aIt++) {
171 ResultPtr aSubRes = (*aIt);
172 aCompSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aSubRes);
173 if (!(aCompSub.get() && aCompSub->numberOfSubs() > 0)) {
174 VisibilityState aS = aWork->isVisible(aSubRes) ? Visible : Hidden;
175 if (aState == NoneState)
177 else if (aState != aS) {
178 aState = SemiVisible;
185 if (aWork->isVisible(aResObj))
194 int PartSet_ObjectNode::numberOfSubs() const
196 ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
198 return aCompRes->numberOfSubs(true);
200 CompositeFeaturePtr aCompFeature =
201 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myObject);
202 if (aCompFeature.get() && aCompFeature->data()->isValid())
203 return aCompFeature->numberOfSubs(true);
205 ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
207 return aFieldRes->stepsSize();
214 ObjectPtr PartSet_ObjectNode::subObject(int theId) const
216 ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
218 return aCompRes->subResult(theId, true);
220 CompositeFeaturePtr aCompFeature =
221 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myObject);
222 if (aCompFeature.get())
223 return aCompFeature->subFeature(theId, true);
228 void PartSet_ObjectNode::update()
230 int aNb = numberOfSubs();
232 ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
234 // If the object is a field result then delete extra sub-objects
235 if (aFieldRes.get()) {
236 while (myChildren.size() > aNb) {
237 ModuleBase_ITreeNode* aNode = myChildren.last();
238 myChildren.removeAll(aNode);
244 ModuleBase_ITreeNode* aNode;
246 while (aId < myChildren.size()) {
247 aNode = myChildren.at(aId);
248 aObj = subObject(aId);
249 if (aNode->object() != aObj) {
250 myChildren.removeAll(aNode);
258 ModuleBase_ITreeNode* aNode;
261 for (i = 0; i < aNb; i++) {
262 aBody = subObject(i);
264 if (i < myChildren.size()) {
265 aNode = myChildren.at(i);
266 if (aNode->object() != aBody) {
267 ((PartSet_ObjectNode*)aNode)->setObject(aBody);
271 aNode = new PartSet_ObjectNode(aBody, this);
272 myChildren.append(aNode);
276 else if (aFieldRes.get()) {
277 ModelAPI_ResultField::ModelAPI_FieldStep* aStep = aFieldRes->step(i);
278 if (i < myChildren.size()) {
279 PartSet_StepNode* aStepNode = static_cast<PartSet_StepNode*>(myChildren.at(i));
280 if (aStepNode->entity() != aStep) {
281 aStepNode->setEntity(aStep);
285 aNode = new PartSet_StepNode(aStep, this);
286 myChildren.append(aNode);
290 // Delete extra objects
291 while (myChildren.size() > aNb) {
292 aNode = myChildren.takeLast();
295 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
304 QTreeNodesList PartSet_ObjectNode::objectCreated(const QObjectPtrList& theObjects)
306 QTreeNodesList aResult;
307 int aNb = numberOfSubs();
309 ModuleBase_ITreeNode* aNode;
310 ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
313 for (i = 0; i < aNb; i++) {
314 aBody = subObject(i);
316 if (i < myChildren.size()) {
317 aNode = myChildren.at(i);
318 if (aNode->object() != aBody) {
319 ((PartSet_ObjectNode*)aNode)->setObject(aBody);
320 aResult.append(aNode);
324 aNode = new PartSet_ObjectNode(aBody, this);
325 myChildren.append(aNode);
326 aResult.append(aNode);
331 ModelAPI_ResultField::ModelAPI_FieldStep* aStep = aFieldRes->step(i);
332 if (i < myChildren.size()) {
333 PartSet_StepNode* aStepNode = static_cast<PartSet_StepNode*>(myChildren.at(i));
334 if (aStepNode->entity() != aStep) {
335 aStepNode->setEntity(aStep);
339 aNode = new PartSet_StepNode(aStep, this);
340 myChildren.append(aNode);
344 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
345 aResult.append(aNode->objectCreated(theObjects));
351 QTreeNodesList PartSet_ObjectNode::objectsDeleted(
352 const DocumentPtr& theDoc, const QString& theGroup)
354 QTreeNodesList aResult;
355 int aNb = numberOfSubs();
356 if (aNb != myChildren.size()) {
359 aResult.append(this);
362 // Delete extra objects
363 bool isDeleted = false;
365 ModuleBase_ITreeNode* aNode;
367 while (aId < myChildren.size()) {
368 aNode = myChildren.at(aId);
369 aObj = subObject(aId);
370 if (aNode->object() != aObj) {
371 myChildren.removeAll(aNode);
379 aResult.append(this);
382 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
383 aBody = subObject(i);
384 ((PartSet_ObjectNode*)aNode)->setObject(aBody);
385 aResult.append(aNode->objectsDeleted(theDoc, theGroup));
392 //////////////////////////////////////////////////////////////////////////////////
393 PartSet_FolderNode::PartSet_FolderNode(ModuleBase_ITreeNode* theParent,
395 : PartSet_TreeNode(theParent), myType(theType)
399 QString PartSet_FolderNode::name() const
402 case ParametersFolder:
403 return QObject::tr("Parameters");
404 case ConstructionFolder:
405 return QObject::tr("Constructions");
407 return QObject::tr("Parts");
409 return QObject::tr("Results");
411 return QObject::tr("Fields");
413 return QObject::tr("Groups");
419 QVariant PartSet_FolderNode::data(int theColumn, int theRole) const
421 static QIcon aParamsIco(":pictures/params_folder.png");
422 static QIcon aConstrIco(":pictures/constr_folder.png");
424 if (theColumn == 1) {
426 case Qt::DisplayRole:
427 return name() + QString(" (%1)").arg(childrenCount());
428 case Qt::DecorationRole:
430 case ParametersFolder:
432 case ConstructionFolder:
445 if ((theColumn == 2) && (theRole == Qt::DecorationRole)) {
446 if (document().get()) {
447 SessionPtr aSession = ModelAPI_Session::get();
448 if (document() != aSession->activeDocument())
451 FeaturePtr aFeature = document()->currentFeature(true);
452 if (!aFeature.get()) { // There is no current feature
453 ModuleBase_ITreeNode* aLastFolder = 0;
454 foreach(ModuleBase_ITreeNode* aNode, parent()->children()) {
455 if (aNode->type() == PartSet_FolderNode::typeId())
460 if (aLastFolder == this)
461 return QIcon(":pictures/arrow.png");
467 return PartSet_TreeNode::data(theColumn, theRole);
470 Qt::ItemFlags PartSet_FolderNode::flags(int theColumn) const
472 SessionPtr aSession = ModelAPI_Session::get();
473 DocumentPtr aActiveDoc = aSession->activeDocument();
474 if (theColumn == 1) {
475 if (document() == aActiveDoc)
481 ModuleBase_ITreeNode* PartSet_FolderNode::createNode(const ObjectPtr& theObj)
483 //ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObj);
484 //if (aCompRes.get())
485 // return new PartSet_CompsolidNode(theObj, this);
486 ModuleBase_ITreeNode* aNode = new PartSet_ObjectNode(theObj, this);
491 void PartSet_FolderNode::update()
493 DocumentPtr aDoc = document();
497 // Remove extra sub-nodes
500 while (aId < myChildren.size()) {
501 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
502 aIndex = aDoc->index(aNode->object(), true);
503 if ((aIndex == -1) || (aId != aIndex)) {
504 myChildren.removeAll(aNode);
511 std::string aGroup = groupName();
512 int aSize = aDoc->size(aGroup, true);
513 for (int i = 0; i < aSize; i++) {
514 ObjectPtr aObj = aDoc->object(aGroup, i, true);
515 if (i < myChildren.size()) {
516 if (myChildren.at(i)->object() != aObj) {
517 ModuleBase_ITreeNode* aNode = createNode(aObj);
518 myChildren.insert(i, aNode);
521 ModuleBase_ITreeNode* aNode = createNode(aObj);
522 myChildren.append(aNode);
526 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
531 std::string PartSet_FolderNode::groupName() const
534 case ParametersFolder:
535 return ModelAPI_ResultParameter::group();
536 case ConstructionFolder:
537 return ModelAPI_ResultConstruction::group();
539 return ModelAPI_ResultPart::group();
541 return ModelAPI_ResultBody::group();
543 return ModelAPI_ResultField::group();
545 return ModelAPI_ResultGroup::group();
550 QTreeNodesList PartSet_FolderNode::objectCreated(const QObjectPtrList& theObjects)
552 QTreeNodesList aResult;
553 std::string aName = groupName();
554 DocumentPtr aDoc = document();
556 QMap<int, ModuleBase_ITreeNode*> aNewNodes;
557 foreach(ObjectPtr aObj, theObjects) {
558 if ((aObj->document() == aDoc) && (aObj->groupName() == aName)) {
559 aIdx = aDoc->index(aObj, true);
561 bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
563 ModuleBase_ITreeNode* aNode = createNode(aObj);
564 aNewNodes[aIdx] = aNode;
565 aResult.append(aNode);
571 // Add nodes in correct order
573 for (i = 0; i < myChildren.size(); i++) {
574 if (aNewNodes.contains(i)) {
575 myChildren.insert(i, aNewNodes[i]);
579 while (aNewNodes.size()) {
580 i = myChildren.size();
581 myChildren.append(aNewNodes[i]);
584 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
585 aResult.append(aNode->objectCreated(theObjects));
590 QTreeNodesList PartSet_FolderNode::objectsDeleted(const DocumentPtr& theDoc,
591 const QString& theGroup)
593 DocumentPtr aDoc = document();
594 QTreeNodesList aResult;
595 if ((theGroup.toStdString() == groupName()) && (theDoc == aDoc)) {
596 QTreeNodesList aDelList;
599 bool aRemoved = false;
600 bool aToSort = false;
601 while (aId < myChildren.size()) {
602 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
603 aIndex = aDoc->index(aNode->object(), true);
604 aToSort |= ((aIndex != -1) && (aId != aIndex));
606 myChildren.removeAll(aNode);
614 aResult.append(this);
617 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
618 aResult.append(aNode->objectsDeleted(theDoc, theGroup));
624 //////////////////////////////////////////////////////////////////////////////////
625 QTreeNodesList PartSet_FeatureFolderNode::objectCreated(const QObjectPtrList& theObjects)
627 QTreeNodesList aResult;
628 // Process the root sub-objects
629 DocumentPtr aDoc = document();
631 int aNb = numberOfFolders();
632 QMap<int, ModuleBase_ITreeNode*> aNewNodes;
633 foreach(ObjectPtr aObj, theObjects) {
634 if (aDoc == aObj->document()) {
635 if ((aObj->groupName() == ModelAPI_Feature::group()) ||
636 (aObj->groupName() == ModelAPI_Folder::group())){
637 aIdx = aDoc->index(aObj, true);
639 ModuleBase_ITreeNode* aNode = createNode(aObj);
641 bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
643 aNewNodes[aIdx] = aNode;
644 aResult.append(aNode);
651 // To add in correct order
653 for (i = 0; i < myChildren.size(); i++) {
654 if (aNewNodes.contains(i)) {
655 myChildren.insert(i, aNewNodes[i]);
659 while (aNewNodes.size()) {
660 i = myChildren.size();
661 myChildren.append(aNewNodes[i]);
665 // Update sub-folders
666 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
667 aResult.append(aNode->objectCreated(theObjects));
672 QTreeNodesList PartSet_FeatureFolderNode::objectsDeleted(const DocumentPtr& theDoc,
673 const QString& theGroup)
675 QTreeNodesList aResult;
677 // Process sub-folders
678 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
679 if (aNode->childrenCount() > 0) { // aFolder node
680 QTreeNodesList aList = aNode->objectsDeleted(theDoc, theGroup);
681 if (aList.size() > 0)
682 aResult.append(aList);
687 DocumentPtr aDoc = document();
688 int aNb = numberOfFolders();
689 bool isGroup = ((theGroup.toStdString() == ModelAPI_Feature::group()) ||
690 (theGroup.toStdString() == ModelAPI_Folder::group()));
691 if ((theDoc == aDoc) && isGroup) {
694 bool aRemoved = false;
695 bool aToSort = false;
696 while (aId < myChildren.size()) {
697 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
698 if (aNode->object().get()) {
699 aIndex = aDoc->index(aNode->object(), true);
700 aToSort |= ((aIndex != -1) && (aId != (aIndex + aNb)));
702 myChildren.removeAll(aNode);
711 aResult.append(this);
718 ModuleBase_ITreeNode* PartSet_FeatureFolderNode::findParent(const DocumentPtr& theDoc,
721 ModuleBase_ITreeNode* aResult = 0;
722 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
723 aResult = aNode->findParent(theDoc, theGroup);
728 bool isGroup = ((theGroup.toStdString() == ModelAPI_Feature::group()) ||
729 (theGroup.toStdString() == ModelAPI_Folder::group()));
730 if ((theDoc == document()) && isGroup)
736 //////////////////////////////////////////////////////////////////////////////////
737 PartSet_RootNode::PartSet_RootNode() : PartSet_FeatureFolderNode(0), myWorkshop(0)
739 SessionPtr aSession = ModelAPI_Session::get();
740 DocumentPtr aDoc = aSession->moduleDocument();
742 myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
743 myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
744 myPartsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::PartsFolder);
746 myChildren.append(myParamsFolder);
747 myChildren.append(myConstrFolder);
748 myChildren.append(myPartsFolder);
754 void PartSet_RootNode::update()
756 myParamsFolder->update();
757 myConstrFolder->update();
758 myPartsFolder->update();
760 // Update features content
761 DocumentPtr aDoc = document();
762 int aNb = numberOfFolders();
764 // Remove extra sub-nodes
767 while (aId < myChildren.size()) {
768 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
769 if (aNode->object().get()) {
770 aIndex = aDoc->index(aNode->object(), true);
771 if ((aIndex == -1) || (aId != (aIndex + aNb))) {
772 myChildren.removeAll(aNode);
781 std::string aGroup = ModelAPI_Feature::group();
782 int aSize = aDoc->size(aGroup, true);
784 for (int i = 0; i < aSize; i++) {
785 ObjectPtr aObj = aDoc->object(aGroup, i, true);
786 aId = i + aNb; // Take into account existing folders
787 if (aId < myChildren.size()) {
788 if (myChildren.at(aId)->object() != aObj) {
789 ModuleBase_ITreeNode* aNode = createNode(aObj);
790 myChildren.insert(aId, aNode);
793 ModuleBase_ITreeNode* aNode = createNode(aObj);
794 myChildren.append(aNode);
797 // Update sub-folders
798 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
799 if ((aNode->type() == PartSet_ObjectFolderNode::typeId()) ||
800 (aNode->type() == PartSet_PartRootNode::typeId()))
805 DocumentPtr PartSet_RootNode::document() const
807 return ModelAPI_Session::get()->moduleDocument();
810 ModuleBase_ITreeNode* PartSet_RootNode::createNode(const ObjectPtr& theObj)
812 if (theObj->groupName() == ModelAPI_Folder::group())
813 return new PartSet_ObjectFolderNode(theObj, this);
815 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
816 if (aFeature->getKind() == PartSetPlugin_Part::ID())
817 return new PartSet_PartRootNode(theObj, this);
819 PartSet_ObjectNode* aNode = new PartSet_ObjectNode(theObj, this);
824 //////////////////////////////////////////////////////////////////////////////////
825 PartSet_PartRootNode::PartSet_PartRootNode(const ObjectPtr& theObj, ModuleBase_ITreeNode* theParent)
826 : PartSet_FeatureFolderNode(theParent), myObject(theObj)
828 myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
829 myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
830 myResultsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ResultsFolder);
831 myFieldsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::FieldsFolder);
832 myGroupsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::GroupsFolder);
834 myChildren.append(myParamsFolder);
835 myChildren.append(myConstrFolder);
836 myChildren.append(myResultsFolder);
841 void PartSet_PartRootNode::deleteChildren()
843 if (!myFieldsFolder->childrenCount()) {
844 delete myFieldsFolder;
846 if (!myGroupsFolder->childrenCount()) {
847 delete myGroupsFolder;
849 PartSet_FeatureFolderNode::deleteChildren();
853 void PartSet_PartRootNode::update()
855 DocumentPtr aDoc = document();
859 myParamsFolder->update();
860 myConstrFolder->update();
861 myResultsFolder->update();
862 myFieldsFolder->update();
863 myGroupsFolder->update();
865 bool aHasFields = myFieldsFolder->childrenCount() > 0;
866 bool aHasGroups = myGroupsFolder->childrenCount() > 0;
868 if (!myChildren.contains(myFieldsFolder)) {
869 myChildren.insert(3, myFieldsFolder);
871 } else if (myChildren.contains(myFieldsFolder)) {
872 myChildren.removeAll(myFieldsFolder);
875 if (!myChildren.contains(myGroupsFolder)) {
876 myChildren.insert(aHasFields ? 4 : 3, myGroupsFolder);
878 } else if (myChildren.contains(myGroupsFolder)) {
879 myChildren.removeAll(myGroupsFolder);
882 // Update features content
883 int aRows = numberOfFolders();
885 // Remove extra sub-nodes
888 while (aId < myChildren.size()) {
889 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
890 if (aNode->object().get()) {
891 aIndex = aDoc->index(aNode->object(), true);
892 if ((aIndex == -1) || (aId != (aIndex + aRows))) {
893 myChildren.removeAll(aNode);
901 std::string aGroup = ModelAPI_Feature::group();
902 int aSize = aDoc->size(aGroup, true);
904 for (int i = 0; i < aSize; i++) {
905 ObjectPtr aObj = aDoc->object(aGroup, i, true);
906 aId = i + aRows; // Take into account existing folders
907 if (aId < myChildren.size()) {
908 if (myChildren.at(aId)->object() != aObj) {
909 ModuleBase_ITreeNode* aNode = createNode(aObj);
910 myChildren.insert(aId, aNode);
913 ModuleBase_ITreeNode* aNode = createNode(aObj);
914 myChildren.append(aNode);
917 // Update sub-folders
918 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
919 if (aNode->type() == PartSet_ObjectFolderNode::typeId())
924 DocumentPtr PartSet_PartRootNode::document() const
926 ResultPartPtr aPartRes = getPartResult(myObject);
928 return aPartRes->partDoc();
929 return DocumentPtr();
932 QVariant PartSet_PartRootNode::data(int theColumn, int theRole) const
937 case Qt::DisplayRole:
939 ResultPartPtr aPartRes = getPartResult(myObject);
940 if (aPartRes.get()) {
941 if (aPartRes->partDoc().get() == NULL)
942 return QString(myObject->data()->name().c_str()) + " (Not loaded)";
944 return QString(myObject->data()->name().c_str());
946 case Qt::DecorationRole:
947 return ModuleBase_IconFactory::get()->getIcon(myObject);
950 if (theRole == Qt::DecorationRole) {
951 if (isCurrentFeature(myObject))
952 return QIcon(":pictures/arrow.png");
957 return PartSet_TreeNode::data(theColumn, theRole);
960 Qt::ItemFlags PartSet_PartRootNode::flags(int theColumn) const
962 if (myObject->isDisabled())
963 return (theColumn == 2) ? Qt::ItemIsSelectable : aNullFlag;
965 SessionPtr aSession = ModelAPI_Session::get();
966 DocumentPtr aActiveDoc = aSession->activeDocument();
967 if ((aActiveDoc == document()) || (myObject->document() == aActiveDoc))
972 ModuleBase_ITreeNode* PartSet_PartRootNode::createNode(const ObjectPtr& theObj)
974 if (theObj->groupName() == ModelAPI_Folder::group())
975 return new PartSet_ObjectFolderNode(theObj, this);
976 PartSet_ObjectNode* aNode = new PartSet_ObjectNode(theObj, this);
981 int PartSet_PartRootNode::numberOfFolders() const
984 if (myFieldsFolder->childrenCount() > 0)
986 if (myGroupsFolder->childrenCount() > 0)
991 QTreeNodesList PartSet_PartRootNode::objectCreated(const QObjectPtrList& theObjects)
993 QTreeNodesList aResult = PartSet_FeatureFolderNode::objectCreated(theObjects);
994 if (!myFieldsFolder->childrenCount()) {
995 QTreeNodesList aList = myFieldsFolder->objectCreated(theObjects);
997 myChildren.insert(3, myFieldsFolder);
998 aResult.append(myFieldsFolder);
999 aResult.append(aList);
1002 if (!myGroupsFolder->childrenCount()) {
1003 QTreeNodesList aList = myGroupsFolder->objectCreated(theObjects);
1005 myChildren.insert(myFieldsFolder->childrenCount()? 4 : 3, myGroupsFolder);
1006 aResult.append(myGroupsFolder);
1007 aResult.append(aList);
1013 QTreeNodesList PartSet_PartRootNode::objectsDeleted(const DocumentPtr& theDoc,
1014 const QString& theGroup)
1016 QTreeNodesList aResult;
1017 if (myFieldsFolder->childrenCount()) {
1018 QTreeNodesList aList = myFieldsFolder->objectsDeleted(theDoc, theGroup);
1020 aResult.append(aList);
1021 if (!myFieldsFolder->childrenCount())
1022 myChildren.removeAll(myFieldsFolder);
1025 if (myGroupsFolder->childrenCount()) {
1026 QTreeNodesList aList = myGroupsFolder->objectsDeleted(theDoc, theGroup);
1028 aResult.append(aList);
1029 if (!myGroupsFolder->childrenCount())
1030 myChildren.removeAll(myGroupsFolder);
1033 aResult.append(PartSet_FeatureFolderNode::objectsDeleted(theDoc, theGroup));
1037 //////////////////////////////////////////////////////////////////////////////////
1038 void PartSet_ObjectFolderNode::update()
1040 int aFirst = -1, aLast = -1;
1041 PartSet_Tools::getFirstAndLastIndexInFolder(myObject, aFirst, aLast);
1042 if ((aFirst == -1) || (aLast == -1)) {
1047 int aNbItems = aLast - aFirst + 1;
1053 DocumentPtr aDoc = myObject->document();
1054 if (aNbItems < myChildren.size()) {
1055 // Delete obsolete nodes
1057 int aNbOfFeatures = aDoc->size(ModelAPI_Feature::group(), true);
1058 while (aId < myChildren.size()) {
1059 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
1060 if ((aFirst + aId) < aNbOfFeatures) {
1061 if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
1062 myChildren.removeAll(aNode);
1068 myChildren.removeAll(aNode);
1075 if (aNbItems > myChildren.size()) {
1077 ModuleBase_ITreeNode* aNode;
1078 for (int i = 0; i < aNbItems; i++) {
1079 ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
1080 if (i < myChildren.size()) {
1081 if (aObj != myChildren.at(i)->object()) {
1082 aNode = new PartSet_ObjectNode(aObj, this);
1083 myChildren.insert(i, aNode);
1088 aNode = new PartSet_ObjectNode(aObj, this);
1089 myChildren.append(aNode);
1096 QTreeNodesList PartSet_ObjectFolderNode::objectCreated(const QObjectPtrList& theObjects)
1098 QTreeNodesList aResult;
1099 int aFirst = -1, aLast = -1;
1100 PartSet_Tools::getFirstAndLastIndexInFolder(myObject, aFirst, aLast);
1101 if ((aFirst == -1) || (aLast == -1)) {
1104 int aNbItems = aLast - aFirst + 1;
1108 DocumentPtr aDoc = myObject->document();
1110 ModuleBase_ITreeNode* aNode;
1111 for (int i = 0; i < aNbItems; i++) {
1112 ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
1113 if (i < myChildren.size()) {
1114 if (aObj != myChildren.at(i)->object()) {
1115 aNode = new PartSet_ObjectNode(aObj, this);
1116 myChildren.insert(i, aNode);
1117 aResult.append(aNode);
1121 aNode = new PartSet_ObjectNode(aObj, this);
1122 myChildren.append(aNode);
1123 aResult.append(aNode);
1130 QTreeNodesList PartSet_ObjectFolderNode::objectsDeleted(const DocumentPtr& theDoc,
1131 const QString& theGroup)
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 if (aNbItems >= myChildren.size()) // Nothing was deleted here
1146 DocumentPtr aDoc = myObject->document();
1147 // Delete obsolete nodes
1148 bool aRemoved = false;
1150 int aNbOfFeatures = aDoc->size(ModelAPI_Feature::group(), true);
1151 while (aId < myChildren.size()) {
1152 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
1153 if ((aFirst + aId) < aNbOfFeatures) {
1154 if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
1155 myChildren.removeAll(aNode);
1162 myChildren.removeAll(aNode);
1163 aResult.removeAll(aNode);
1171 aResult.append(this);
1178 //////////////////////////////////////////////////////////////////////////////////
1179 QVariant PartSet_StepNode::data(int theColumn, int theRole) const
1181 if ((theColumn == 1) && (theRole == Qt::DisplayRole)) {
1182 ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
1183 dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>(myEntity);
1185 return "Step " + QString::number(aStep->id() + 1) + " " +
1186 aStep->field()->textLine(aStep->id()).c_str();
1188 return PartSet_TreeNode::data(theColumn, theRole);