Salome HOME
Update copyrights
[modules/shaper.git] / src / ModuleBase / ModuleBase_PagedContainer.cpp
1 // Copyright (C) 2014-2019  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   myIsFocusOnCurrentPage(false), myIsFirst(true), myRemeberChoice(true)
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, const QString& theCaseId,
52                                       const QPixmap& theIcon,
53                                       const QString& theTooltip)
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
99   std::string aDefVal = myRemeberChoice? myDefValue : "";
100
101   DataPtr aData = myFeature->data();
102   AttributeStringPtr aStringAttr = aData->string(attributeID());
103   QString aCaseId;
104   if (aStringAttr->isInitialized()) {
105     if (myIsEditing)
106       aCaseId = QString::fromStdString(aStringAttr->value());
107     else
108       aCaseId = QString::fromStdString(aDefVal.empty() ? aStringAttr->value() : aDefVal);
109     myIsFirst = false;
110     int idx = myCaseIds.indexOf(aCaseId);
111     if (idx == -1)
112       idx = currentPageIndex();
113     setCurrentPageIndex(idx);
114   }
115   else {
116     // It is added because if user edits the feature created from Python
117     // and switches his choice
118     // it will not be stored in the attribute while apply not pressed.
119     // But this button will be disabled because of not initialized attribute
120     aStringAttr->setValue(myCaseIds.at(0).toStdString());
121     setCurrentPageIndex(0);
122   }
123   return true;
124 }
125
126 void ModuleBase_PagedContainer::activateCustom()
127 {
128   // activate current page
129   focusTo();
130 }
131
132 bool ModuleBase_PagedContainer::storeValueCustom()
133 {
134   // A rare case when plugin was not loaded.
135   if(!myFeature)
136     return false;
137   DataPtr aData = myFeature->data();
138
139   AttributeStringPtr aStringAttr = aData->string(attributeID());
140   std::string aWidgetValue;
141   if (myIsFirst)
142     aWidgetValue = myDefValue.empty()?
143         myCaseIds.at(currentPageIndex()).toStdString() : myDefValue;
144   else
145     aWidgetValue = myCaseIds.at(currentPageIndex()).toStdString();
146   myDefValue = aWidgetValue;
147   aStringAttr->setValue(aWidgetValue);
148
149   myIsFirst = false;
150
151   updateObject(myFeature); // for preview
152   return true;
153 }
154
155
156 void ModuleBase_PagedContainer::onPageChanged()
157 {
158   if (!storeValue())
159     return;
160   // focus might be changed only if the value is correcly stored
161   // if it is not stored, reentrant manager will handle by this widget
162   // after it will restart operation, the widget might be removed
163   if (myIsFocusOnCurrentPage)
164     focusTo();
165 }
166
167 void ModuleBase_PagedContainer::onFeatureAccepted()
168 {
169   if (myRemeberChoice)
170     defaultValues[myFeatureId + attributeID()] = myDefValue;
171 }
172