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