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"
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 if (myObject->groupName() == ModelAPI_Folder::group())
126 return QIcon(":pictures/features_folder.png");
128 return ModuleBase_IconFactory::get()->getIcon(myObject);
130 if (isCurrentFeature(myObject))
131 return QIcon(":pictures/arrow.png");
136 return PartSet_TreeNode::data(theColumn, theRole);
139 Qt::ItemFlags PartSet_ObjectNode::flags(int theColumn) const
141 if (myObject->isDisabled()) {
142 return (theColumn == 2) ? Qt::ItemIsSelectable : aNullFlag;
144 DocumentPtr aDoc = myObject->document();
145 SessionPtr aSession = ModelAPI_Session::get();
146 if (aSession->activeDocument() == aDoc)
152 PartSet_ObjectNode::VisibilityState PartSet_ObjectNode::visibilityState() const
154 Qt::ItemFlags aFlags = flags(1);
155 if (aFlags == Qt::ItemFlags())
158 if (myObject->groupName() == ModelAPI_ResultParameter::group())
160 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
162 ModuleBase_IWorkshop* aWork = workshop();
163 ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResObj);
164 if (aCompRes.get()) {
165 std::list<ResultPtr> aResultsList;
166 ModelAPI_Tools::allSubs(aCompRes, aResultsList);
167 VisibilityState aState = aResultsList.size() == 0 ?
168 (aWork->isVisible(aCompRes) ? Visible : Hidden) : NoneState;
170 std::list<ResultPtr>::const_iterator aIt;
171 ResultBodyPtr aCompSub;
172 for (aIt = aResultsList.cbegin(); aIt != aResultsList.cend(); aIt++) {
173 ResultPtr aSubRes = (*aIt);
174 aCompSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aSubRes);
175 if (!(aCompSub.get() && aCompSub->numberOfSubs() > 0)) {
176 VisibilityState aS = aWork->isVisible(aSubRes) ? Visible : Hidden;
177 if (aState == NoneState)
179 else if (aState != aS) {
180 aState = SemiVisible;
187 if (aWork->isVisible(aResObj))
196 int PartSet_ObjectNode::numberOfSubs() const
198 ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
200 return aCompRes->numberOfSubs(true);
202 CompositeFeaturePtr aCompFeature =
203 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myObject);
204 if (aCompFeature.get() && aCompFeature->data()->isValid())
205 return aCompFeature->numberOfSubs(true);
207 ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
209 return aFieldRes->stepsSize();
216 ObjectPtr PartSet_ObjectNode::subObject(int theId) const
218 ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
220 return aCompRes->subResult(theId, true);
222 CompositeFeaturePtr aCompFeature =
223 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myObject);
224 if (aCompFeature.get())
225 return aCompFeature->subFeature(theId, true);
230 void PartSet_ObjectNode::update()
232 int aNb = numberOfSubs();
234 ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
236 // If the object is a field result then delete extra sub-objects
237 if (aFieldRes.get()) {
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);
277 else if (aFieldRes.get()) {
278 ModelAPI_ResultField::ModelAPI_FieldStep* aStep = aFieldRes->step(i);
279 if (i < myChildren.size()) {
280 PartSet_StepNode* aStepNode = static_cast<PartSet_StepNode*>(myChildren.at(i));
281 if (aStepNode->entity() != aStep) {
282 aStepNode->setEntity(aStep);
286 aNode = new PartSet_StepNode(aStep, this);
287 myChildren.append(aNode);
291 // Delete extra objects
292 while (myChildren.size() > aNb) {
293 aNode = myChildren.takeLast();
296 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
305 QTreeNodesList PartSet_ObjectNode::objectCreated(const QObjectPtrList& theObjects)
307 QTreeNodesList aResult;
308 int aNb = numberOfSubs();
310 ModuleBase_ITreeNode* aNode;
311 ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
314 for (i = 0; i < aNb; i++) {
315 aBody = subObject(i);
317 if (i < myChildren.size()) {
318 aNode = myChildren.at(i);
319 if (aNode->object() != aBody) {
320 ((PartSet_ObjectNode*)aNode)->setObject(aBody);
321 aResult.append(aNode);
325 aNode = new PartSet_ObjectNode(aBody, this);
326 myChildren.append(aNode);
327 aResult.append(aNode);
332 ModelAPI_ResultField::ModelAPI_FieldStep* aStep = aFieldRes->step(i);
333 if (i < myChildren.size()) {
334 PartSet_StepNode* aStepNode = static_cast<PartSet_StepNode*>(myChildren.at(i));
335 if (aStepNode->entity() != aStep) {
336 aStepNode->setEntity(aStep);
340 aNode = new PartSet_StepNode(aStep, this);
341 myChildren.append(aNode);
345 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
346 aResult.append(aNode->objectCreated(theObjects));
352 QTreeNodesList PartSet_ObjectNode::objectsDeleted(
353 const DocumentPtr& theDoc, const QString& theGroup)
355 QTreeNodesList aResult;
356 int aNb = numberOfSubs();
357 if (aNb != myChildren.size()) {
360 aResult.append(this);
363 // Delete extra objects
364 bool isDeleted = false;
366 ModuleBase_ITreeNode* aNode;
368 while (aId < myChildren.size()) {
369 aNode = myChildren.at(aId);
370 aObj = subObject(aId);
371 if (aNode->object() != aObj) {
372 myChildren.removeAll(aNode);
380 aResult.append(this);
383 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
384 aBody = subObject(i);
385 ((PartSet_ObjectNode*)aNode)->setObject(aBody);
386 aResult.append(aNode->objectsDeleted(theDoc, theGroup));
393 //////////////////////////////////////////////////////////////////////////////////
394 PartSet_FolderNode::PartSet_FolderNode(ModuleBase_ITreeNode* theParent,
396 : PartSet_TreeNode(theParent), myType(theType)
400 QString PartSet_FolderNode::name() const
403 case ParametersFolder:
404 return QObject::tr("Parameters");
405 case ConstructionFolder:
406 return QObject::tr("Constructions");
408 return QObject::tr("Parts");
410 return QObject::tr("Results");
412 return QObject::tr("Fields");
414 return QObject::tr("Groups");
420 QVariant PartSet_FolderNode::data(int theColumn, int theRole) const
422 static QIcon aParamsIco(":pictures/params_folder.png");
423 static QIcon aConstrIco(":pictures/constr_folder.png");
425 if (theColumn == 1) {
427 case Qt::DisplayRole:
428 return name() + QString(" (%1)").arg(childrenCount());
429 case Qt::DecorationRole:
431 case ParametersFolder:
433 case ConstructionFolder:
446 if ((theColumn == 2) && (theRole == Qt::DecorationRole)) {
447 if (document().get()) {
448 SessionPtr aSession = ModelAPI_Session::get();
449 if (document() != aSession->activeDocument())
452 FeaturePtr aFeature = document()->currentFeature(true);
453 if (!aFeature.get()) { // There is no current feature
454 ModuleBase_ITreeNode* aLastFolder = 0;
455 foreach(ModuleBase_ITreeNode* aNode, parent()->children()) {
456 if (aNode->type() == PartSet_FolderNode::typeId())
461 if (aLastFolder == this)
462 return QIcon(":pictures/arrow.png");
468 return PartSet_TreeNode::data(theColumn, theRole);
471 Qt::ItemFlags PartSet_FolderNode::flags(int theColumn) const
473 SessionPtr aSession = ModelAPI_Session::get();
474 DocumentPtr aActiveDoc = aSession->activeDocument();
475 if (theColumn == 1) {
476 if (document() == aActiveDoc)
482 ModuleBase_ITreeNode* PartSet_FolderNode::createNode(const ObjectPtr& theObj)
484 //ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObj);
485 //if (aCompRes.get())
486 // return new PartSet_CompsolidNode(theObj, this);
487 return new PartSet_ObjectNode(theObj, this);
490 void PartSet_FolderNode::update()
492 DocumentPtr aDoc = document();
496 // Remove extra sub-nodes
499 while (aId < myChildren.size()) {
500 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
501 aIndex = aDoc->index(aNode->object(), true);
502 if ((aIndex == -1) || (aId != aIndex)) {
503 myChildren.removeAll(aNode);
510 std::string aGroup = groupName();
511 int aSize = aDoc->size(aGroup, true);
512 for (int i = 0; i < aSize; i++) {
513 ObjectPtr aObj = aDoc->object(aGroup, i, true);
514 if (i < myChildren.size()) {
515 if (myChildren.at(i)->object() != aObj) {
516 ModuleBase_ITreeNode* aNode = createNode(aObj);
517 myChildren.insert(i, aNode);
520 ModuleBase_ITreeNode* aNode = createNode(aObj);
521 myChildren.append(aNode);
525 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
530 std::string PartSet_FolderNode::groupName() const
533 case ParametersFolder:
534 return ModelAPI_ResultParameter::group();
535 case ConstructionFolder:
536 return ModelAPI_ResultConstruction::group();
538 return ModelAPI_ResultPart::group();
540 return ModelAPI_ResultBody::group();
542 return ModelAPI_ResultField::group();
544 return ModelAPI_ResultGroup::group();
549 QTreeNodesList PartSet_FolderNode::objectCreated(const QObjectPtrList& theObjects)
551 QTreeNodesList aResult;
552 std::string aName = groupName();
553 DocumentPtr aDoc = document();
555 QMap<int, ModuleBase_ITreeNode*> aNewNodes;
556 foreach(ObjectPtr aObj, theObjects) {
557 if ((aObj->document() == aDoc) && (aObj->groupName() == aName)) {
558 aIdx = aDoc->index(aObj, true);
560 bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
562 ModuleBase_ITreeNode* aNode = createNode(aObj);
563 aNewNodes[aIdx] = aNode;
564 aResult.append(aNode);
570 // Add nodes in correct order
572 for (i = 0; i < myChildren.size(); i++) {
573 if (aNewNodes.contains(i)) {
574 myChildren.insert(i, aNewNodes[i]);
578 while (aNewNodes.size()) {
579 i = myChildren.size();
580 myChildren.append(aNewNodes[i]);
583 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
584 aResult.append(aNode->objectCreated(theObjects));
589 QTreeNodesList PartSet_FolderNode::objectsDeleted(const DocumentPtr& theDoc,
590 const QString& theGroup)
592 DocumentPtr aDoc = document();
593 QTreeNodesList aResult;
594 if ((theGroup.toStdString() == groupName()) && (theDoc == aDoc)) {
595 QTreeNodesList aDelList;
598 bool aRemoved = false;
599 bool aToSort = false;
600 while (aId < myChildren.size()) {
601 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
602 aIndex = aDoc->index(aNode->object(), true);
603 aToSort |= ((aIndex != -1) && (aId != aIndex));
605 myChildren.removeAll(aNode);
613 aResult.append(this);
616 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
617 aResult.append(aNode->objectsDeleted(theDoc, theGroup));
623 //////////////////////////////////////////////////////////////////////////////////
624 QTreeNodesList PartSet_FeatureFolderNode::objectCreated(const QObjectPtrList& theObjects)
626 QTreeNodesList aResult;
627 // Process the root sub-objects
628 DocumentPtr aDoc = document();
630 int aNb = numberOfFolders();
631 QMap<int, ModuleBase_ITreeNode*> aNewNodes;
632 foreach(ObjectPtr aObj, theObjects) {
633 if (aDoc == aObj->document()) {
634 if ((aObj->groupName() == ModelAPI_Feature::group()) ||
635 (aObj->groupName() == ModelAPI_Folder::group())){
636 aIdx = aDoc->index(aObj, true);
638 ModuleBase_ITreeNode* aNode = createNode(aObj);
640 bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
642 aNewNodes[aIdx] = aNode;
643 aResult.append(aNode);
650 // To add in correct order
652 for (i = 0; i < myChildren.size(); i++) {
653 if (aNewNodes.contains(i)) {
654 myChildren.insert(i, aNewNodes[i]);
658 while (aNewNodes.size()) {
659 i = myChildren.size();
660 myChildren.append(aNewNodes[i]);
664 // Update sub-folders
665 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
666 aResult.append(aNode->objectCreated(theObjects));
671 QTreeNodesList PartSet_FeatureFolderNode::objectsDeleted(const DocumentPtr& theDoc,
672 const QString& theGroup)
674 QTreeNodesList aResult;
676 // Process sub-folders
677 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
678 if (aNode->childrenCount() > 0) { // aFolder node
679 QTreeNodesList aList = aNode->objectsDeleted(theDoc, theGroup);
680 if (aList.size() > 0)
681 aResult.append(aList);
686 DocumentPtr aDoc = document();
687 int aNb = numberOfFolders();
688 bool isGroup = ((theGroup.toStdString() == ModelAPI_Feature::group()) ||
689 (theGroup.toStdString() == ModelAPI_Folder::group()));
690 if ((theDoc == aDoc) && isGroup) {
693 bool aRemoved = false;
694 bool aToSort = false;
695 while (aId < myChildren.size()) {
696 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
697 if (aNode->object().get()) {
698 aIndex = aDoc->index(aNode->object(), true);
699 aToSort |= ((aIndex != -1) && (aId != (aIndex + aNb)));
701 myChildren.removeAll(aNode);
710 aResult.append(this);
717 ModuleBase_ITreeNode* PartSet_FeatureFolderNode::findParent(const DocumentPtr& theDoc,
720 ModuleBase_ITreeNode* aResult = 0;
721 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
722 aResult = aNode->findParent(theDoc, theGroup);
727 bool isGroup = ((theGroup.toStdString() == ModelAPI_Feature::group()) ||
728 (theGroup.toStdString() == ModelAPI_Folder::group()));
729 if ((theDoc == document()) && isGroup)
735 //////////////////////////////////////////////////////////////////////////////////
736 PartSet_RootNode::PartSet_RootNode() : PartSet_FeatureFolderNode(0), myWorkshop(0)
738 SessionPtr aSession = ModelAPI_Session::get();
739 DocumentPtr aDoc = aSession->moduleDocument();
741 myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
742 myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
743 myPartsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::PartsFolder);
745 myChildren.append(myParamsFolder);
746 myChildren.append(myConstrFolder);
747 myChildren.append(myPartsFolder);
753 void PartSet_RootNode::update()
755 myParamsFolder->update();
756 myConstrFolder->update();
757 myPartsFolder->update();
759 // Update features content
760 DocumentPtr aDoc = document();
761 int aNb = numberOfFolders();
763 // Remove extra sub-nodes
766 while (aId < myChildren.size()) {
767 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
768 if (aNode->object().get()) {
769 aIndex = aDoc->index(aNode->object(), true);
770 if ((aIndex == -1) || (aId != (aIndex + aNb))) {
771 myChildren.removeAll(aNode);
780 std::string aGroup = ModelAPI_Feature::group();
781 int aSize = aDoc->size(aGroup, true);
783 for (int i = 0; i < aSize; i++) {
784 ObjectPtr aObj = aDoc->object(aGroup, i, true);
785 aId = i + aNb; // Take into account existing folders
786 if (aId < myChildren.size()) {
787 if (myChildren.at(aId)->object() != aObj) {
788 ModuleBase_ITreeNode* aNode = createNode(aObj);
789 myChildren.insert(aId, aNode);
792 ModuleBase_ITreeNode* aNode = createNode(aObj);
793 myChildren.append(aNode);
796 // Update sub-folders
797 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
798 if ((aNode->type() == PartSet_ObjectFolderNode::typeId()) ||
799 (aNode->type() == PartSet_PartRootNode::typeId()))
804 DocumentPtr PartSet_RootNode::document() const
806 return ModelAPI_Session::get()->moduleDocument();
809 ModuleBase_ITreeNode* PartSet_RootNode::createNode(const ObjectPtr& theObj)
811 if (theObj->groupName() == ModelAPI_Folder::group())
812 return new PartSet_ObjectFolderNode(theObj, this);
814 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
815 if (aFeature->getKind() == PartSetPlugin_Part::ID())
816 return new PartSet_PartRootNode(theObj, this);
818 return new PartSet_ObjectNode(theObj, this);
821 //////////////////////////////////////////////////////////////////////////////////
822 PartSet_PartRootNode::PartSet_PartRootNode(const ObjectPtr& theObj, ModuleBase_ITreeNode* theParent)
823 : PartSet_FeatureFolderNode(theParent), myObject(theObj)
825 myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
826 myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
827 myResultsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ResultsFolder);
828 myFieldsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::FieldsFolder);
829 myGroupsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::GroupsFolder);
831 myChildren.append(myParamsFolder);
832 myChildren.append(myConstrFolder);
833 myChildren.append(myResultsFolder);
838 void PartSet_PartRootNode::deleteChildren()
840 if (!myFieldsFolder->childrenCount()) {
841 delete myFieldsFolder;
843 if (!myGroupsFolder->childrenCount()) {
844 delete myGroupsFolder;
846 PartSet_FeatureFolderNode::deleteChildren();
850 void PartSet_PartRootNode::update()
852 DocumentPtr aDoc = document();
856 myParamsFolder->update();
857 myConstrFolder->update();
858 myResultsFolder->update();
859 myFieldsFolder->update();
860 myGroupsFolder->update();
862 bool aHasFields = myFieldsFolder->childrenCount() > 0;
863 bool aHasGroups = myGroupsFolder->childrenCount() > 0;
864 if (aHasFields && (!myChildren.contains(myFieldsFolder))) {
865 myChildren.insert(3, myFieldsFolder);
867 if (aHasGroups && (!myChildren.contains(myGroupsFolder))) {
868 myChildren.insert(aHasFields ? 4 : 3, myGroupsFolder);
871 // Update features content
872 int aRows = numberOfFolders();
874 // Remove extra sub-nodes
877 while (aId < myChildren.size()) {
878 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
879 if (aNode->object().get()) {
880 aIndex = aDoc->index(aNode->object(), true);
881 if ((aIndex == -1) || (aId != (aIndex + aRows))) {
882 myChildren.removeAll(aNode);
890 std::string aGroup = ModelAPI_Feature::group();
891 int aSize = aDoc->size(aGroup, true);
893 for (int i = 0; i < aSize; i++) {
894 ObjectPtr aObj = aDoc->object(aGroup, i, true);
895 aId = i + aRows; // Take into account existing folders
896 if (aId < myChildren.size()) {
897 if (myChildren.at(aId)->object() != aObj) {
898 ModuleBase_ITreeNode* aNode = createNode(aObj);
899 myChildren.insert(aId, aNode);
902 ModuleBase_ITreeNode* aNode = createNode(aObj);
903 myChildren.append(aNode);
906 // Update sub-folders
907 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
908 if (aNode->type() == PartSet_ObjectFolderNode::typeId())
913 DocumentPtr PartSet_PartRootNode::document() const
915 ResultPartPtr aPartRes = getPartResult(myObject);
917 return aPartRes->partDoc();
918 return DocumentPtr();
921 QVariant PartSet_PartRootNode::data(int theColumn, int theRole) const
926 case Qt::DisplayRole:
928 ResultPartPtr aPartRes = getPartResult(myObject);
929 if (aPartRes.get()) {
930 if (aPartRes->partDoc().get() == NULL)
931 return QString(myObject->data()->name().c_str()) + " (Not loaded)";
933 return QString(myObject->data()->name().c_str());
935 case Qt::DecorationRole:
936 return ModuleBase_IconFactory::get()->getIcon(myObject);
939 if (theRole == Qt::DecorationRole) {
940 if (isCurrentFeature(myObject))
941 return QIcon(":pictures/arrow.png");
946 return PartSet_TreeNode::data(theColumn, theRole);
949 Qt::ItemFlags PartSet_PartRootNode::flags(int theColumn) const
951 if (myObject->isDisabled())
952 return (theColumn == 2) ? Qt::ItemIsSelectable : aDefaultFlag;
954 SessionPtr aSession = ModelAPI_Session::get();
955 DocumentPtr aActiveDoc = aSession->activeDocument();
956 if ((aActiveDoc == document()) || (myObject->document() == aActiveDoc))
961 ModuleBase_ITreeNode* PartSet_PartRootNode::createNode(const ObjectPtr& theObj)
963 if (theObj->groupName() == ModelAPI_Folder::group())
964 return new PartSet_ObjectFolderNode(theObj, this);
965 return new PartSet_ObjectNode(theObj, this);
968 int PartSet_PartRootNode::numberOfFolders() const
971 if (myFieldsFolder->childrenCount() > 0)
973 if (myGroupsFolder->childrenCount() > 0)
978 QTreeNodesList PartSet_PartRootNode::objectCreated(const QObjectPtrList& theObjects)
980 QTreeNodesList aResult = PartSet_FeatureFolderNode::objectCreated(theObjects);
981 if (!myFieldsFolder->childrenCount()) {
982 QTreeNodesList aList = myFieldsFolder->objectCreated(theObjects);
984 myChildren.insert(3, myFieldsFolder);
985 aResult.append(myFieldsFolder);
986 aResult.append(aList);
989 if (!myGroupsFolder->childrenCount()) {
990 QTreeNodesList aList = myGroupsFolder->objectCreated(theObjects);
992 myChildren.insert(myFieldsFolder->childrenCount()? 4 : 3, myGroupsFolder);
993 aResult.append(myGroupsFolder);
994 aResult.append(aList);
1000 QTreeNodesList PartSet_PartRootNode::objectsDeleted(const DocumentPtr& theDoc,
1001 const QString& theGroup)
1003 QTreeNodesList aResult;
1004 if (myFieldsFolder->childrenCount()) {
1005 QTreeNodesList aList = myFieldsFolder->objectsDeleted(theDoc, theGroup);
1007 aResult.append(aList);
1008 if (!myFieldsFolder->childrenCount())
1009 myChildren.removeAll(myFieldsFolder);
1012 if (myGroupsFolder->childrenCount()) {
1013 QTreeNodesList aList = myGroupsFolder->objectsDeleted(theDoc, theGroup);
1015 aResult.append(aList);
1016 if (!myGroupsFolder->childrenCount())
1017 myChildren.removeAll(myGroupsFolder);
1020 aResult.append(PartSet_FeatureFolderNode::objectsDeleted(theDoc, theGroup));
1024 //////////////////////////////////////////////////////////////////////////////////
1025 void PartSet_ObjectFolderNode::update()
1028 getFirstAndLastIndex(aFirst, aLast);
1029 if ((aFirst == -1) || (aLast == -1)) {
1034 int aNbItems = aLast - aFirst + 1;
1040 DocumentPtr aDoc = myObject->document();
1041 if (aNbItems < myChildren.size()) {
1042 // Delete obsolete nodes
1044 int aNbOfFeatures = aDoc->size(ModelAPI_Feature::group(), true);
1045 while (aId < myChildren.size()) {
1046 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
1047 if ((aFirst + aId) < aNbOfFeatures) {
1048 if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
1049 myChildren.removeAll(aNode);
1055 myChildren.removeAll(aNode);
1062 if (aNbItems > myChildren.size()) {
1064 ModuleBase_ITreeNode* aNode;
1065 for (int i = 0; i < aNbItems; i++) {
1066 ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
1067 if (i < myChildren.size()) {
1068 if (aObj != myChildren.at(i)->object()) {
1069 aNode = new PartSet_ObjectNode(aObj, this);
1070 myChildren.insert(i, aNode);
1074 aNode = new PartSet_ObjectNode(aObj, this);
1075 myChildren.append(aNode);
1081 QTreeNodesList PartSet_ObjectFolderNode::objectCreated(const QObjectPtrList& theObjects)
1083 QTreeNodesList aResult;
1085 getFirstAndLastIndex(aFirst, aLast);
1086 if ((aFirst == -1) || (aLast == -1)) {
1089 int aNbItems = aLast - aFirst + 1;
1093 DocumentPtr aDoc = myObject->document();
1095 ModuleBase_ITreeNode* aNode;
1096 for (int i = 0; i < aNbItems; i++) {
1097 ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
1098 if (i < myChildren.size()) {
1099 if (aObj != myChildren.at(i)->object()) {
1100 aNode = new PartSet_ObjectNode(aObj, this);
1101 myChildren.insert(i, aNode);
1102 aResult.append(aNode);
1105 aNode = new PartSet_ObjectNode(aObj, this);
1106 myChildren.append(aNode);
1107 aResult.append(aNode);
1113 QTreeNodesList PartSet_ObjectFolderNode::objectsDeleted(const DocumentPtr& theDoc,
1114 const QString& theGroup)
1116 QTreeNodesList aResult;
1118 getFirstAndLastIndex(aFirst, aLast);
1119 if ((aFirst == -1) || (aLast == -1)) {
1122 int aNbItems = aLast - aFirst + 1;
1126 if (aNbItems >= myChildren.size()) // Nothing was deleted here
1129 DocumentPtr aDoc = myObject->document();
1130 // Delete obsolete nodes
1131 bool aRemoved = false;
1133 int aNbOfFeatures = aDoc->size(ModelAPI_Feature::group(), true);
1134 while (aId < myChildren.size()) {
1135 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
1136 if ((aFirst + aId) < aNbOfFeatures) {
1137 if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
1138 myChildren.removeAll(aNode);
1145 myChildren.removeAll(aNode);
1153 aResult.append(this);
1158 FeaturePtr PartSet_ObjectFolderNode::getFeature(const std::string& theId) const
1160 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(myObject);
1161 AttributeReferencePtr aFeatAttr = aFolder->data()->reference(theId);
1163 return ModelAPI_Feature::feature(aFeatAttr->value());
1164 return FeaturePtr();
1167 void PartSet_ObjectFolderNode::getFirstAndLastIndex(int& theFirst, int& theLast) const
1169 DocumentPtr aDoc = myObject->document();
1170 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(myObject);
1172 FeaturePtr aFirstFeatureInFolder = getFeature(ModelAPI_Folder::FIRST_FEATURE_ID());
1173 if (!aFirstFeatureInFolder.get()) {
1177 FeaturePtr aLastFeatureInFolder = getFeature(ModelAPI_Folder::LAST_FEATURE_ID());
1178 if (!aLastFeatureInFolder.get()) {
1183 theFirst = aDoc->index(aFirstFeatureInFolder);
1184 theLast = aDoc->index(aLastFeatureInFolder);
1188 QVariant PartSet_ObjectFolderNode::data(int theColumn, int theRole) const
1190 const QImage anAditional(":icons/hasWarning.png");
1192 if ((theRole == Qt::DecorationRole) && (theColumn == 1)) {
1194 bool aHasWarning = false;
1195 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
1196 aObject = aNode->object();
1197 if (aObject.get()) {
1198 ModelAPI_ExecState aState = aObject->data()->execState();
1199 if ((aState == ModelAPI_StateExecFailed) || (aState == ModelAPI_StateMustBeUpdated)) {
1206 return QIcon(ModuleBase_Tools::composite(":icons/hasWarning.png",
1207 ":pictures/features_folder.png"));
1210 return PartSet_ObjectNode::data(theColumn, theRole);
1214 //////////////////////////////////////////////////////////////////////////////////
1215 QVariant PartSet_StepNode::data(int theColumn, int theRole) const
1217 if ((theColumn == 1) && (theRole == Qt::DisplayRole)) {
1218 ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
1219 dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>(myEntity);
1221 return "Step " + QString::number(aStep->id() + 1) + " " +
1222 aStep->field()->textLine(aStep->id()).c_str();
1224 return PartSet_TreeNode::data(theColumn, theRole);