Salome HOME
f079e63d34da5ed60759458650a06d138a61a90b
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetSelectorStore.cpp
1 // Copyright (C) 2014-2023  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <ModuleBase_WidgetSelectorStore.h>
21 #include <ModuleBase_Tools.h>
22 #include <ModuleBase_IWorkshop.h>
23
24 #include <ModelAPI_AttributeRefAttr.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeRefList.h>
27 #include <ModelAPI_AttributeRefAttrList.h>
28
29 ModuleBase_WidgetSelectorStore::ModuleBase_WidgetSelectorStore()
30 : myIsObject(false), mySelectionType(""), mySelectionCount(0)
31 {
32 }
33
34 void ModuleBase_WidgetSelectorStore::storeAttributeValue(const AttributePtr& theAttribute,
35                                                          ModuleBase_IWorkshop* theWorkshop)
36 {
37   if (!theAttribute.get())
38     return;
39
40   std::string aType = theAttribute->attributeType();
41   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
42     AttributeSelectionListPtr aSelectionListAttr =
43                          std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
44     mySelectionType = aSelectionListAttr->selectionType();
45     mySelectionCount = aSelectionListAttr->size();
46   }
47   else if (aType == ModelAPI_AttributeRefList::typeId()) {
48     AttributeRefListPtr aRefListAttr =
49                          std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
50     mySelectionCount = aRefListAttr->size();
51   }
52   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
53     AttributeRefAttrListPtr aRefAttrListAttr =
54                          std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
55     mySelectionCount = aRefAttrListAttr->size();
56   }
57   else {
58     myObject = ModuleBase_Tools::getObject(theAttribute);
59     myShape = ModuleBase_Tools::getShape(theAttribute, theWorkshop);
60     myRefAttribute = AttributePtr();
61     myIsObject = false;
62     AttributeRefAttrPtr aRefAttr =
63       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
64     if (aRefAttr) {
65       myIsObject = aRefAttr->isObject();
66       myRefAttribute = aRefAttr->attr();
67     }
68   }
69 }
70
71 //********************************************************************
72 void ModuleBase_WidgetSelectorStore::restoreAttributeValue(const AttributePtr& theAttribute,
73                                                            ModuleBase_IWorkshop* theWorkshop)
74 {
75   std::string aType = theAttribute->attributeType();
76   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
77     AttributeSelectionListPtr aSelectionListAttr =
78                    std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
79     aSelectionListAttr->setSelectionType(mySelectionType);
80     // restore selection in the attribute. Indeed there is only one stored object
81     int aCountAppened = aSelectionListAttr->size() - mySelectionCount;
82     for (int i = 0; i < aCountAppened; i++)
83       aSelectionListAttr->removeLast();
84   }
85   else if (aType == ModelAPI_AttributeRefList::typeId()) {
86     AttributeRefListPtr aRefListAttr =
87                          std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
88     // restore objects in the attribute. Indeed there is only one stored object
89     int aCountAppened = aRefListAttr->size() - mySelectionCount;
90     for (int i = 0; i < aCountAppened; i++)
91       aRefListAttr->removeLast();
92   }
93   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
94     AttributeRefAttrListPtr aRefAttrListAttr =
95                      std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
96     // restore objects in the attribute. Indeed there is only one stored object
97     int aCountAppened = aRefAttrListAttr->size() - mySelectionCount;
98     for (int i = 0; i < aCountAppened; i++)
99       aRefAttrListAttr->removeLast();
100   }
101   else {
102     ModuleBase_Tools::setObject(theAttribute, myObject, myShape, theWorkshop, true, true);
103     AttributeRefAttrPtr aRefAttr =
104       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
105     if (aRefAttr) {
106       if (!myIsObject)
107         aRefAttr->setAttr(myRefAttribute);
108     }
109   }
110 }