Salome HOME
Issue #2388: Take into account attribute ID for remembering of a user choice
[modules/shaper.git] / src / ModuleBase / ModuleBase_PagedContainer.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_PagedContainer.h>
22 #include <ModuleBase_PageBase.h>
23 #include <ModuleBase_ModelWidget.h>
24 #include <ModuleBase_Tools.h>
25
26 #include <ModelAPI_AttributeString.h>
27
28 #include <QWidget>
29 #include <QList>
30 #include <QVBoxLayout>
31
32
33 static QMap<std::string, std::string> defaultValues;
34
35 ModuleBase_PagedContainer::ModuleBase_PagedContainer(QWidget* theParent,
36                                                      const Config_WidgetAPI* theData)
37 : ModuleBase_ModelWidget(theParent, theData),
38   myIsFocusOnCurrentPage(false), myIsFirst(true)
39 {
40   // it is not obligatory to be ignored when property panel tries to activate next active widget
41   // but if focus is moved to this control, it can accept it.
42   myIsObligatory = false;
43   if (defaultValues.contains(myFeatureId + attributeID()))
44     myDefValue = defaultValues[myFeatureId + attributeID()];
45 }
46
47 ModuleBase_PagedContainer::~ModuleBase_PagedContainer()
48 {
49 }
50
51 int ModuleBase_PagedContainer::addPage(ModuleBase_PageBase* thePage,
52                                       const QString& theName, const QString& theCaseId,
53                                       const QPixmap& theIcon )
54 {
55   if (!myPages.count()) {
56     setDefaultValue(theCaseId.toStdString());
57   }
58   myCaseIds << theCaseId;
59   myPages << thePage;
60
61   return myPages.count();
62 }
63
64 QList<QWidget*> ModuleBase_PagedContainer::getControls() const
65 {
66   QList<QWidget*> aResult;
67   int anIndex = currentPageIndex();
68   QList<ModuleBase_ModelWidget*> aModelWidgets = myPages[anIndex]->modelWidgets();
69   foreach(ModuleBase_ModelWidget* eachModelWidget, aModelWidgets) {
70     aResult << eachModelWidget->getControls();
71   }
72   return aResult;
73 }
74
75 bool ModuleBase_PagedContainer::focusTo()
76 {
77   int anIndex = currentPageIndex();
78   if (anIndex > myPages.count())
79     return false;
80   return myPages[anIndex]->takeFocus();
81 }
82
83 void ModuleBase_PagedContainer::setHighlighted(bool)
84 {
85   //page containers should not be highlighted, do nothing
86 }
87
88 void ModuleBase_PagedContainer::enableFocusProcessing()
89 {
90   myIsFocusOnCurrentPage = true;
91 }
92
93 bool ModuleBase_PagedContainer::restoreValueCustom()
94 {
95   // A rare case when plugin was not loaded.
96   if(!myFeature)
97     return false;
98   DataPtr aData = myFeature->data();
99   AttributeStringPtr aStringAttr = aData->string(attributeID());
100   QString aCaseId = QString::fromStdString(myDefValue.empty()?
101                                            aStringAttr->value() : myDefValue);
102   myIsFirst = false;
103   int idx = myCaseIds.indexOf(aCaseId);
104   if (idx == -1)
105     idx = currentPageIndex();
106   setCurrentPageIndex(idx);
107   return true;
108 }
109
110 void ModuleBase_PagedContainer::activateCustom()
111 {
112   // activate current page
113   focusTo();
114 }
115
116 bool ModuleBase_PagedContainer::storeValueCustom()
117 {
118   // A rare case when plugin was not loaded.
119   if(!myFeature)
120     return false;
121   DataPtr aData = myFeature->data();
122
123   AttributeStringPtr aStringAttr = aData->string(attributeID());
124   std::string aWidgetValue;
125   if (myIsFirst)
126     aWidgetValue = myDefValue.empty()?
127         myCaseIds.at(currentPageIndex()).toStdString() : myDefValue;
128   else
129     aWidgetValue = myCaseIds.at(currentPageIndex()).toStdString();
130   myDefValue = aWidgetValue;
131   aStringAttr->setValue(aWidgetValue);
132
133   myIsFirst = false;
134
135   updateObject(myFeature); // for preview
136   return true;
137 }
138
139
140 void ModuleBase_PagedContainer::onPageChanged()
141 {
142   if (!storeValue())
143     return;
144   // focus might be changed only if the value is correcly stored
145   // if it is not stored, reentrant manager will handle by this widget
146   // after it will restart operation, the widget might be removed
147   if (myIsFocusOnCurrentPage)
148     focusTo();
149 }
150
151 void ModuleBase_PagedContainer::onFeatureAccepted()
152 {
153   defaultValues[myFeatureId + attributeID()] = myDefValue;
154 }
155