Salome HOME
2bf5195ca425248bdd51eb0aeead072a96662fb7
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetSelectorStore.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetSelectorStore.cpp
4 // Created:     2 June 2014
5 // Author:      Vitaly Smetannikov
6
7 #include <ModuleBase_WidgetSelectorStore.h>
8 #include <ModuleBase_Tools.h>
9 #include <ModuleBase_IWorkshop.h>
10
11 #include <ModelAPI_AttributeRefAttr.h>
12 #include <ModelAPI_AttributeSelectionList.h>
13 #include <ModelAPI_AttributeRefList.h>
14 #include <ModelAPI_AttributeRefAttrList.h>
15
16 ModuleBase_WidgetSelectorStore::ModuleBase_WidgetSelectorStore()
17 : myIsObject(false), mySelectionType(""), mySelectionCount(0)
18 {
19 }
20
21 void ModuleBase_WidgetSelectorStore::storeAttributeValue(const AttributePtr& theAttribute,
22                                                          ModuleBase_IWorkshop* theWorkshop)
23 {
24   if (!theAttribute.get())
25     return;
26
27   std::string aType = theAttribute->attributeType();
28   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
29     AttributeSelectionListPtr aSelectionListAttr =
30                          std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
31     mySelectionType = aSelectionListAttr->selectionType();
32     mySelectionCount = aSelectionListAttr->size();
33   }
34   else if (aType == ModelAPI_AttributeRefList::typeId()) {
35     AttributeRefListPtr aRefListAttr =
36                          std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
37     mySelectionCount = aRefListAttr->size();
38   }
39   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
40     AttributeRefAttrListPtr aRefAttrListAttr =
41                          std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
42     mySelectionCount = aRefAttrListAttr->size();
43   }
44   else {
45     myObject = ModuleBase_Tools::getObject(theAttribute);
46     myShape = ModuleBase_Tools::getShape(theAttribute, theWorkshop);
47     myRefAttribute = AttributePtr();
48     myIsObject = false;
49     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
50     if (aRefAttr) {
51       myIsObject = aRefAttr->isObject();
52       myRefAttribute = aRefAttr->attr();
53     }
54   }
55 }
56
57 //********************************************************************
58 void ModuleBase_WidgetSelectorStore::restoreAttributeValue(const AttributePtr& theAttribute,
59                                                            ModuleBase_IWorkshop* theWorkshop)
60 {
61   std::string aType = theAttribute->attributeType();
62   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
63     AttributeSelectionListPtr aSelectionListAttr = 
64                    std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
65     aSelectionListAttr->setSelectionType(mySelectionType);
66     // restore selection in the attribute. Indeed there is only one stored object
67     int aCountAppened = aSelectionListAttr->size() - mySelectionCount;
68     for (int i = 0; i < aCountAppened; i++)
69       aSelectionListAttr->removeLast();
70   }
71   else if (aType == ModelAPI_AttributeRefList::typeId()) {
72     AttributeRefListPtr aRefListAttr = 
73                          std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
74     // restore objects in the attribute. Indeed there is only one stored object
75     int aCountAppened = aRefListAttr->size() - mySelectionCount;
76     for (int i = 0; i < aCountAppened; i++)
77       aRefListAttr->removeLast();
78   }
79   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
80     AttributeRefAttrListPtr aRefAttrListAttr =
81                      std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
82     // restore objects in the attribute. Indeed there is only one stored object
83     int aCountAppened = aRefAttrListAttr->size() - mySelectionCount;
84     for (int i = 0; i < aCountAppened; i++)
85       aRefAttrListAttr->removeLast();
86   }
87   else {
88     ModuleBase_Tools::setObject(theAttribute, myObject, myShape, theWorkshop, true);
89     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
90     if (aRefAttr) {
91       if (!myIsObject)
92         aRefAttr->setAttr(myRefAttribute);
93     }
94   }
95 }