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