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>
26 #include <PartSetPlugin_Part.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_ResultParameter.h>
30 #include <ModelAPI_ResultField.h>
31 #include <ModelAPI_ResultGroup.h>
32 #include <ModelAPI_ResultConstruction.h>
33 #include <ModelAPI_ResultPart.h>
34 #include <ModelAPI_ResultBody.h>
35 #include <ModelAPI_Tools.h>
36 #include <ModelAPI_ResultBody.h>
37 #include <ModelAPI_CompositeFeature.h>
38 #include <ModelAPI_AttributeDouble.h>
39 #include <ModelAPI_Folder.h>
40 #include <ModelAPI_AttributeReference.h>
46 #define ACTIVE_COLOR QColor(Qt::black)
47 #define SELECTABLE_COLOR QColor(100, 100, 100)
48 #define DISABLED_COLOR QColor(200, 200, 200)
50 Qt::ItemFlags aNullFlag;
51 Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
52 Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
55 ResultPartPtr getPartResult(const ObjectPtr& theObj)
57 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
59 ResultPtr aRes = aFeature->firstResult();
60 if (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group())) {
61 ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aRes);
62 // Use only original parts, not a placement results
63 if (aPartRes == aPartRes->original())
67 return ResultPartPtr();
70 bool isCurrentFeature(const ObjectPtr& theObj)
72 SessionPtr aSession = ModelAPI_Session::get();
73 DocumentPtr aCurDoc = aSession->activeDocument();
74 FeaturePtr aFeature = aCurDoc->currentFeature(true);
75 return aFeature == theObj;
78 //////////////////////////////////////////////////////////////////////////////////
79 QVariant PartSet_TreeNode::data(int theColumn, int theRole) const
81 if ((theColumn == 1) && (theRole == Qt::ForegroundRole)) {
82 Qt::ItemFlags aFlags = flags(theColumn);
83 if (aFlags == Qt::ItemFlags())
84 return QBrush(DISABLED_COLOR);
85 if (!aFlags.testFlag(Qt::ItemIsEditable))
86 return QBrush(SELECTABLE_COLOR);
89 return ModuleBase_ITreeNode::data(theColumn, theRole);
93 //////////////////////////////////////////////////////////////////////////////////
94 QVariant PartSet_ObjectNode::data(int theColumn, int theRole) const
99 if (myObject->groupName() == ModelAPI_ResultParameter::group()) {
100 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myObject);
101 AttributeDoublePtr aValueAttribute =
102 aParam->data()->real(ModelAPI_ResultParameter::VALUE());
103 QString aVal = QString::number(aValueAttribute->value());
104 QString aTitle = QString(myObject->data()->name().c_str());
105 return aTitle + " = " + aVal;
107 return myObject->data()->name().c_str();
110 case Qt::DecorationRole:
113 switch (visibilityState()) {
117 return QIcon(":pictures/eyeopen.png");
119 return QIcon(":pictures/eyemiclosed.png");
121 return QIcon(":pictures/eyeclosed.png");
124 if (myObject->groupName() == ModelAPI_Folder::group())
125 return QIcon(":pictures/features_folder.png");
127 return ModuleBase_IconFactory::get()->getIcon(myObject);
129 if (isCurrentFeature(myObject))
130 return QIcon(":pictures/arrow.png");
135 return PartSet_TreeNode::data(theColumn, theRole);
138 Qt::ItemFlags PartSet_ObjectNode::flags(int theColumn) const
140 if (myObject->isDisabled()) {
141 return (theColumn == 2) ? Qt::ItemIsSelectable : aNullFlag;
143 DocumentPtr aDoc = myObject->document();
144 SessionPtr aSession = ModelAPI_Session::get();
145 if (aSession->activeDocument() == aDoc)
151 PartSet_ObjectNode::VisibilityState PartSet_ObjectNode::visibilityState() const
153 Qt::ItemFlags aFlags = flags(1);
154 if (aFlags == Qt::ItemFlags())
157 if (myObject->groupName() == ModelAPI_ResultParameter::group())
159 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
161 ModuleBase_IWorkshop* aWork = workshop();
162 ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResObj);
163 if (aCompRes.get()) {
164 std::list<ResultPtr> aResultsList;
165 ModelAPI_Tools::allSubs(aCompRes, aResultsList);
166 VisibilityState aState = aResultsList.size() == 0 ?
167 (aWork->isVisible(aCompRes) ? Visible : Hidden) : NoneState;
169 std::list<ResultPtr>::const_iterator aIt;
170 ResultBodyPtr aCompSub;
171 for (aIt = aResultsList.cbegin(); aIt != aResultsList.cend(); aIt++) {
172 ResultPtr aSubRes = (*aIt);
173 aCompSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aSubRes);
174 if (!(aCompSub.get() && aCompSub->numberOfSubs() > 0)) {
175 VisibilityState aS = aWork->isVisible(aSubRes) ? Visible : Hidden;
176 if (aState == NoneState)
178 else if (aState != aS) {
179 aState = SemiVisible;
186 if (aWork->isVisible(aResObj))
195 int PartSet_ObjectNode::numberOfSubs() const
197 ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
199 return aCompRes->numberOfSubs(true);
201 CompositeFeaturePtr aCompFeature =
202 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myObject);
203 if (aCompFeature.get() && aCompFeature->data()->isValid())
204 return aCompFeature->numberOfSubs(true);
206 ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
208 return aFieldRes->stepsSize();
215 ObjectPtr PartSet_ObjectNode::subObject(int theId) const
217 ResultBodyPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myObject);
219 return aCompRes->subResult(theId, true);
221 CompositeFeaturePtr aCompFeature =
222 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myObject);
223 if (aCompFeature.get())
224 return aCompFeature->subFeature(theId, true);
229 void PartSet_ObjectNode::update()
231 int aNb = numberOfSubs();
233 ResultFieldPtr aFieldRes = std::dynamic_pointer_cast<ModelAPI_ResultField>(myObject);
235 // If the object is a field result then delete extra sub-objects
236 if (aFieldRes.get()) {
237 while (myChildren.size() > aNb) {
238 ModuleBase_ITreeNode* aNode = myChildren.last();
239 myChildren.removeAll(aNode);
245 ModuleBase_ITreeNode* aNode;
247 while (aId < myChildren.size()) {
248 aNode = myChildren.at(aId);
249 aObj = subObject(aId);
250 if (aNode->object() != aObj) {
251 myChildren.removeAll(aNode);
259 ModuleBase_ITreeNode* aNode;
262 for (i = 0; i < aNb; i++) {
263 aBody = subObject(i);
265 if (i < myChildren.size()) {
266 aNode = myChildren.at(i);
267 if (aNode->object() != aBody) {
268 ((PartSet_ObjectNode*)aNode)->setObject(aBody);
272 aNode = new PartSet_ObjectNode(aBody, this);
273 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 return new PartSet_ObjectNode(theObj, this);
489 void PartSet_FolderNode::update()
491 DocumentPtr aDoc = document();
495 // Remove extra sub-nodes
498 while (aId < myChildren.size()) {
499 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
500 aIndex = aDoc->index(aNode->object(), true);
501 if ((aIndex == -1) || (aId != aIndex)) {
502 myChildren.removeAll(aNode);
509 std::string aGroup = groupName();
510 int aSize = aDoc->size(aGroup, true);
511 for (int i = 0; i < aSize; i++) {
512 ObjectPtr aObj = aDoc->object(aGroup, i, true);
513 if (i < myChildren.size()) {
514 if (myChildren.at(i)->object() != aObj) {
515 ModuleBase_ITreeNode* aNode = createNode(aObj);
516 myChildren.insert(i, aNode);
519 ModuleBase_ITreeNode* aNode = createNode(aObj);
520 myChildren.append(aNode);
524 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
529 std::string PartSet_FolderNode::groupName() const
532 case ParametersFolder:
533 return ModelAPI_ResultParameter::group();
534 case ConstructionFolder:
535 return ModelAPI_ResultConstruction::group();
537 return ModelAPI_ResultPart::group();
539 return ModelAPI_ResultBody::group();
541 return ModelAPI_ResultField::group();
543 return ModelAPI_ResultGroup::group();
548 QTreeNodesList PartSet_FolderNode::objectCreated(const QObjectPtrList& theObjects)
550 QTreeNodesList aResult;
551 std::string aName = groupName();
552 DocumentPtr aDoc = document();
554 QMap<int, ModuleBase_ITreeNode*> aNewNodes;
555 foreach(ObjectPtr aObj, theObjects) {
556 if ((aObj->document() == aDoc) && (aObj->groupName() == aName)) {
557 aIdx = aDoc->index(aObj, true);
559 bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
561 ModuleBase_ITreeNode* aNode = createNode(aObj);
562 aNewNodes[aIdx] = aNode;
563 aResult.append(aNode);
569 // Add nodes in correct order
571 for (i = 0; i < myChildren.size(); i++) {
572 if (aNewNodes.contains(i)) {
573 myChildren.insert(i, aNewNodes[i]);
577 while (aNewNodes.size()) {
578 i = myChildren.size();
579 myChildren.append(aNewNodes[i]);
582 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
583 aResult.append(aNode->objectCreated(theObjects));
588 QTreeNodesList PartSet_FolderNode::objectsDeleted(const DocumentPtr& theDoc,
589 const QString& theGroup)
591 DocumentPtr aDoc = document();
592 QTreeNodesList aResult;
593 if ((theGroup.toStdString() == groupName()) && (theDoc == aDoc)) {
594 QTreeNodesList aDelList;
597 bool aRemoved = false;
598 bool aToSort = false;
599 while (aId < myChildren.size()) {
600 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
601 aIndex = aDoc->index(aNode->object(), true);
602 aToSort |= ((aIndex != -1) && (aId != aIndex));
604 myChildren.removeAll(aNode);
612 aResult.append(this);
615 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
616 aResult.append(aNode->objectsDeleted(theDoc, theGroup));
622 //////////////////////////////////////////////////////////////////////////////////
623 QTreeNodesList PartSet_FeatureFolderNode::objectCreated(const QObjectPtrList& theObjects)
625 QTreeNodesList aResult;
626 // Process the root sub-objects
627 DocumentPtr aDoc = document();
629 int aNb = numberOfFolders();
630 QMap<int, ModuleBase_ITreeNode*> aNewNodes;
631 foreach(ObjectPtr aObj, theObjects) {
632 if (aDoc == aObj->document()) {
633 if ((aObj->groupName() == ModelAPI_Feature::group()) ||
634 (aObj->groupName() == ModelAPI_Folder::group())){
635 aIdx = aDoc->index(aObj, true);
637 ModuleBase_ITreeNode* aNode = createNode(aObj);
639 bool aHasObject = (aIdx < myChildren.size()) && (myChildren.at(aIdx)->object() == aObj);
641 aNewNodes[aIdx] = aNode;
642 aResult.append(aNode);
649 // To add in correct order
651 for (i = 0; i < myChildren.size(); i++) {
652 if (aNewNodes.contains(i)) {
653 myChildren.insert(i, aNewNodes[i]);
657 while (aNewNodes.size()) {
658 i = myChildren.size();
659 myChildren.append(aNewNodes[i]);
663 // Update sub-folders
664 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
665 aResult.append(aNode->objectCreated(theObjects));
670 QTreeNodesList PartSet_FeatureFolderNode::objectsDeleted(const DocumentPtr& theDoc,
671 const QString& theGroup)
673 QTreeNodesList aResult;
675 // Process sub-folders
676 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
677 if (aNode->childrenCount() > 0) { // aFolder node
678 QTreeNodesList aList = aNode->objectsDeleted(theDoc, theGroup);
679 if (aList.size() > 0)
680 aResult.append(aList);
685 DocumentPtr aDoc = document();
686 int aNb = numberOfFolders();
687 bool isGroup = ((theGroup.toStdString() == ModelAPI_Feature::group()) ||
688 (theGroup.toStdString() == ModelAPI_Folder::group()));
689 if ((theDoc == aDoc) && isGroup) {
692 bool aRemoved = false;
693 bool aToSort = false;
694 while (aId < myChildren.size()) {
695 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
696 if (aNode->object().get()) {
697 aIndex = aDoc->index(aNode->object(), true);
698 aToSort |= ((aIndex != -1) && (aId != (aIndex + aNb)));
700 myChildren.removeAll(aNode);
709 aResult.append(this);
716 ModuleBase_ITreeNode* PartSet_FeatureFolderNode::findParent(const DocumentPtr& theDoc,
719 ModuleBase_ITreeNode* aResult = 0;
720 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
721 aResult = aNode->findParent(theDoc, theGroup);
726 bool isGroup = ((theGroup.toStdString() == ModelAPI_Feature::group()) ||
727 (theGroup.toStdString() == ModelAPI_Folder::group()));
728 if ((theDoc == document()) && isGroup)
734 //////////////////////////////////////////////////////////////////////////////////
735 PartSet_RootNode::PartSet_RootNode() : PartSet_FeatureFolderNode(0), myWorkshop(0)
737 SessionPtr aSession = ModelAPI_Session::get();
738 DocumentPtr aDoc = aSession->moduleDocument();
740 myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
741 myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
742 myPartsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::PartsFolder);
744 myChildren.append(myParamsFolder);
745 myChildren.append(myConstrFolder);
746 myChildren.append(myPartsFolder);
752 void PartSet_RootNode::update()
754 myParamsFolder->update();
755 myConstrFolder->update();
756 myPartsFolder->update();
758 // Update features content
759 DocumentPtr aDoc = document();
760 int aNb = numberOfFolders();
762 // Remove extra sub-nodes
765 while (aId < myChildren.size()) {
766 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
767 if (aNode->object().get()) {
768 aIndex = aDoc->index(aNode->object(), true);
769 if ((aIndex == -1) || (aId != (aIndex + aNb))) {
770 myChildren.removeAll(aNode);
779 std::string aGroup = ModelAPI_Feature::group();
780 int aSize = aDoc->size(aGroup, true);
782 for (int i = 0; i < aSize; i++) {
783 ObjectPtr aObj = aDoc->object(aGroup, i, true);
784 aId = i + aNb; // Take into account existing folders
785 if (aId < myChildren.size()) {
786 if (myChildren.at(aId)->object() != aObj) {
787 ModuleBase_ITreeNode* aNode = createNode(aObj);
788 myChildren.insert(aId, aNode);
791 ModuleBase_ITreeNode* aNode = createNode(aObj);
792 myChildren.append(aNode);
795 // Update sub-folders
796 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
797 if ((aNode->type() == PartSet_ObjectFolderNode::typeId()) ||
798 (aNode->type() == PartSet_PartRootNode::typeId()))
803 DocumentPtr PartSet_RootNode::document() const
805 return ModelAPI_Session::get()->moduleDocument();
808 ModuleBase_ITreeNode* PartSet_RootNode::createNode(const ObjectPtr& theObj)
810 if (theObj->groupName() == ModelAPI_Folder::group())
811 return new PartSet_ObjectFolderNode(theObj, this);
813 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
814 if (aFeature->getKind() == PartSetPlugin_Part::ID())
815 return new PartSet_PartRootNode(theObj, this);
817 return new PartSet_ObjectNode(theObj, this);
820 //////////////////////////////////////////////////////////////////////////////////
821 PartSet_PartRootNode::PartSet_PartRootNode(const ObjectPtr& theObj, ModuleBase_ITreeNode* theParent)
822 : PartSet_FeatureFolderNode(theParent), myObject(theObj)
824 myParamsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ParametersFolder);
825 myConstrFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ConstructionFolder);
826 myResultsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::ResultsFolder);
827 myFieldsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::FieldsFolder);
828 myGroupsFolder = new PartSet_FolderNode(this, PartSet_FolderNode::GroupsFolder);
830 myChildren.append(myParamsFolder);
831 myChildren.append(myConstrFolder);
832 myChildren.append(myResultsFolder);
837 void PartSet_PartRootNode::deleteChildren()
839 if (!myFieldsFolder->childrenCount()) {
840 delete myFieldsFolder;
842 if (!myGroupsFolder->childrenCount()) {
843 delete myGroupsFolder;
845 PartSet_FeatureFolderNode::deleteChildren();
849 void PartSet_PartRootNode::update()
851 DocumentPtr aDoc = document();
855 myParamsFolder->update();
856 myConstrFolder->update();
857 myResultsFolder->update();
858 myFieldsFolder->update();
859 myGroupsFolder->update();
861 bool aHasFields = myFieldsFolder->childrenCount() > 0;
862 bool aHasGroups = myGroupsFolder->childrenCount() > 0;
863 if (aHasFields && (!myChildren.contains(myFieldsFolder))) {
864 myChildren.insert(3, myFieldsFolder);
866 if (aHasGroups && (!myChildren.contains(myGroupsFolder))) {
867 myChildren.insert(aHasFields ? 4 : 3, myGroupsFolder);
870 // Update features content
871 int aRows = numberOfFolders();
873 // Remove extra sub-nodes
876 while (aId < myChildren.size()) {
877 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
878 if (aNode->object().get()) {
879 aIndex = aDoc->index(aNode->object(), true);
880 if ((aIndex == -1) || (aId != (aIndex + aRows))) {
881 myChildren.removeAll(aNode);
889 std::string aGroup = ModelAPI_Feature::group();
890 int aSize = aDoc->size(aGroup, true);
892 for (int i = 0; i < aSize; i++) {
893 ObjectPtr aObj = aDoc->object(aGroup, i, true);
894 aId = i + aRows; // Take into account existing folders
895 if (aId < myChildren.size()) {
896 if (myChildren.at(aId)->object() != aObj) {
897 ModuleBase_ITreeNode* aNode = createNode(aObj);
898 myChildren.insert(aId, aNode);
901 ModuleBase_ITreeNode* aNode = createNode(aObj);
902 myChildren.append(aNode);
905 // Update sub-folders
906 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
907 if (aNode->type() == PartSet_ObjectFolderNode::typeId())
912 DocumentPtr PartSet_PartRootNode::document() const
914 ResultPartPtr aPartRes = getPartResult(myObject);
916 return aPartRes->partDoc();
917 return DocumentPtr();
920 QVariant PartSet_PartRootNode::data(int theColumn, int theRole) const
925 case Qt::DisplayRole:
927 ResultPartPtr aPartRes = getPartResult(myObject);
928 if (aPartRes.get()) {
929 if (aPartRes->partDoc().get() == NULL)
930 return QString(myObject->data()->name().c_str()) + " (Not loaded)";
932 return QString(myObject->data()->name().c_str());
934 case Qt::DecorationRole:
935 return ModuleBase_IconFactory::get()->getIcon(myObject);
938 if (theRole == Qt::DecorationRole) {
939 if (isCurrentFeature(myObject))
940 return QIcon(":pictures/arrow.png");
945 return PartSet_TreeNode::data(theColumn, theRole);
948 Qt::ItemFlags PartSet_PartRootNode::flags(int theColumn) const
950 if (myObject->isDisabled())
951 return (theColumn == 2) ? Qt::ItemIsSelectable : aDefaultFlag;
953 SessionPtr aSession = ModelAPI_Session::get();
954 DocumentPtr aActiveDoc = aSession->activeDocument();
955 if ((aActiveDoc == document()) || (myObject->document() == aActiveDoc))
960 ModuleBase_ITreeNode* PartSet_PartRootNode::createNode(const ObjectPtr& theObj)
962 if (theObj->groupName() == ModelAPI_Folder::group())
963 return new PartSet_ObjectFolderNode(theObj, this);
964 return new PartSet_ObjectNode(theObj, this);
967 int PartSet_PartRootNode::numberOfFolders() const
970 if (myFieldsFolder->childrenCount() > 0)
972 if (myGroupsFolder->childrenCount() > 0)
977 QTreeNodesList PartSet_PartRootNode::objectCreated(const QObjectPtrList& theObjects)
979 QTreeNodesList aResult = PartSet_FeatureFolderNode::objectCreated(theObjects);
980 if (!myFieldsFolder->childrenCount()) {
981 QTreeNodesList aList = myFieldsFolder->objectCreated(theObjects);
983 myChildren.insert(3, myFieldsFolder);
984 aResult.append(myFieldsFolder);
985 aResult.append(aList);
988 if (!myGroupsFolder->childrenCount()) {
989 QTreeNodesList aList = myGroupsFolder->objectCreated(theObjects);
991 myChildren.insert(myFieldsFolder->childrenCount()? 4 : 3, myGroupsFolder);
992 aResult.append(myGroupsFolder);
993 aResult.append(aList);
999 QTreeNodesList PartSet_PartRootNode::objectsDeleted(const DocumentPtr& theDoc,
1000 const QString& theGroup)
1002 QTreeNodesList aResult;
1003 if (myFieldsFolder->childrenCount()) {
1004 QTreeNodesList aList = myFieldsFolder->objectsDeleted(theDoc, theGroup);
1006 aResult.append(aList);
1007 if (!myFieldsFolder->childrenCount())
1008 myChildren.removeAll(myFieldsFolder);
1011 if (myGroupsFolder->childrenCount()) {
1012 QTreeNodesList aList = myGroupsFolder->objectsDeleted(theDoc, theGroup);
1014 aResult.append(aList);
1015 if (!myGroupsFolder->childrenCount())
1016 myChildren.removeAll(myGroupsFolder);
1019 aResult.append(PartSet_FeatureFolderNode::objectsDeleted(theDoc, theGroup));
1023 //////////////////////////////////////////////////////////////////////////////////
1024 void PartSet_ObjectFolderNode::update()
1027 getFirstAndLastIndex(aFirst, aLast);
1028 if ((aFirst == -1) || (aLast == -1)) {
1033 int aNbItems = aLast - aFirst + 1;
1039 DocumentPtr aDoc = myObject->document();
1040 if (aNbItems < myChildren.size()) {
1041 // Delete obsolete nodes
1043 int aNbOfFeatures = aDoc->size(ModelAPI_Feature::group(), true);
1044 while (aId < myChildren.size()) {
1045 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
1046 if ((aFirst + aId) < aNbOfFeatures) {
1047 if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
1048 myChildren.removeAll(aNode);
1054 myChildren.removeAll(aNode);
1061 if (aNbItems > myChildren.size()) {
1063 ModuleBase_ITreeNode* aNode;
1064 for (int i = 0; i < aNbItems; i++) {
1065 ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
1066 if (i < myChildren.size()) {
1067 if (aObj != myChildren.at(i)->object()) {
1068 aNode = new PartSet_ObjectNode(aObj, this);
1069 myChildren.insert(i, aNode);
1073 aNode = new PartSet_ObjectNode(aObj, this);
1074 myChildren.append(aNode);
1080 QTreeNodesList PartSet_ObjectFolderNode::objectCreated(const QObjectPtrList& theObjects)
1082 QTreeNodesList aResult;
1084 getFirstAndLastIndex(aFirst, aLast);
1085 if ((aFirst == -1) || (aLast == -1)) {
1088 int aNbItems = aLast - aFirst + 1;
1092 DocumentPtr aDoc = myObject->document();
1094 ModuleBase_ITreeNode* aNode;
1095 for (int i = 0; i < aNbItems; i++) {
1096 ObjectPtr aObj = aDoc->object(ModelAPI_Feature::group(), aFirst + i);
1097 if (i < myChildren.size()) {
1098 if (aObj != myChildren.at(i)->object()) {
1099 aNode = new PartSet_ObjectNode(aObj, this);
1100 myChildren.insert(i, aNode);
1101 aResult.append(aNode);
1104 aNode = new PartSet_ObjectNode(aObj, this);
1105 myChildren.append(aNode);
1106 aResult.append(aNode);
1112 QTreeNodesList PartSet_ObjectFolderNode::objectsDeleted(const DocumentPtr& theDoc,
1113 const QString& theGroup)
1115 QTreeNodesList aResult;
1117 getFirstAndLastIndex(aFirst, aLast);
1118 if ((aFirst == -1) || (aLast == -1)) {
1121 int aNbItems = aLast - aFirst + 1;
1125 if (aNbItems >= myChildren.size()) // Nothing was deleted here
1128 DocumentPtr aDoc = myObject->document();
1129 // Delete obsolete nodes
1130 bool aRemoved = false;
1132 int aNbOfFeatures = aDoc->size(ModelAPI_Feature::group(), true);
1133 while (aId < myChildren.size()) {
1134 ModuleBase_ITreeNode* aNode = myChildren.at(aId);
1135 if ((aFirst + aId) < aNbOfFeatures) {
1136 if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
1137 myChildren.removeAll(aNode);
1144 myChildren.removeAll(aNode);
1152 aResult.append(this);
1157 FeaturePtr PartSet_ObjectFolderNode::getFeature(const std::string& theId) const
1159 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(myObject);
1160 AttributeReferencePtr aFeatAttr = aFolder->data()->reference(theId);
1162 return ModelAPI_Feature::feature(aFeatAttr->value());
1163 return FeaturePtr();
1166 void PartSet_ObjectFolderNode::getFirstAndLastIndex(int& theFirst, int& theLast) const
1168 DocumentPtr aDoc = myObject->document();
1169 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(myObject);
1171 FeaturePtr aFirstFeatureInFolder = getFeature(ModelAPI_Folder::FIRST_FEATURE_ID());
1172 if (!aFirstFeatureInFolder.get()) {
1176 FeaturePtr aLastFeatureInFolder = getFeature(ModelAPI_Folder::LAST_FEATURE_ID());
1177 if (!aLastFeatureInFolder.get()) {
1182 theFirst = aDoc->index(aFirstFeatureInFolder);
1183 theLast = aDoc->index(aLastFeatureInFolder);
1187 //////////////////////////////////////////////////////////////////////////////////
1188 QVariant PartSet_StepNode::data(int theColumn, int theRole) const
1190 if ((theColumn == 1) && (theRole == Qt::DisplayRole)) {
1191 ModelAPI_ResultField::ModelAPI_FieldStep* aStep =
1192 dynamic_cast<ModelAPI_ResultField::ModelAPI_FieldStep*>(myEntity);
1194 return "Step " + QString::number(aStep->id() + 1) + " " +
1195 aStep->field()->textLine(aStep->id()).c_str();
1197 return PartSet_TreeNode::data(theColumn, theRole);