Salome HOME
Issue #2388: Take into account attribute ID for remembering of a user choice
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetChoice.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_WidgetChoice.h"
22 #include "ModuleBase_Tools.h"
23 #include "ModuleBase_IconFactory.h"
24 #include "ModuleBase_ChoiceCtrl.h"
25
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_AttributeStringArray.h>
28 #include <ModelAPI_Data.h>
29 #include <Config_WidgetAPI.h>
30 #include <Config_PropManager.h>
31
32 #include <QWidget>
33 #include <QLayout>
34 #include <QLabel>
35 #include <QComboBox>
36 #include <QButtonGroup>
37 #include <QGroupBox>
38 #include <QRadioButton>
39 #include <QToolButton>
40
41 static QMap<std::string, int> defaultValues;
42
43 ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
44                                                  const Config_WidgetAPI* theData)
45 : ModuleBase_ModelWidget(theParent, theData), myIsFirst(true)
46 {
47   myHasValue = defaultValues.contains(myFeatureId + attributeID());
48   if (myHasValue)
49     myDefValue = defaultValues[myFeatureId + attributeID()];
50   else
51     myDefValue = 0;
52
53   QString aLabelText = translate(theData->widgetLabel());
54   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
55   std::string aTypes = theData->getProperty("string_list");
56   QStringList aList;
57
58   foreach(QString aType, QString(aTypes.c_str()).split(' ')) {
59     aList.append(translate(aType.toStdString()));
60   }
61   if (aTypes.empty()) {
62     myStringListAttribute = theData->getProperty("string_list_attribute");
63     if (!myStringListAttribute.empty())
64       aList.clear();
65   }
66   if (theData->getBooleanAttribute("use_in_title", false))
67     myButtonTitles = aList;
68
69   bool aHasDefaultValue;
70   int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
71
72   // Widget type can be combobox or radiobuttons
73   std::string aWgtType = theData->getProperty("widget_type");
74   std::string aIcons = theData->getProperty("icons_list");
75   QStringList aIconList = QString(aIcons.c_str()).split(' ');
76
77   std::string aWgtDir = theData->getProperty("buttons_dir");
78
79   QHBoxLayout* aLayout = new QHBoxLayout(this);
80   myChoiceCtrl =  new ModuleBase_ChoiceCtrl(this, aList, aIconList,
81     (aWgtType == "radiobuttons")? ModuleBase_ChoiceCtrl::RadioButtons :
82                                   ModuleBase_ChoiceCtrl::ComboBox,
83     (aWgtDir == "horizontal")? Qt::Horizontal : Qt::Vertical);
84   myChoiceCtrl->setLabel(aLabelText);
85
86   if (!aLabelIcon.isEmpty())
87     myChoiceCtrl->setLabelIcon(aLabelIcon);
88
89   connect(myChoiceCtrl, SIGNAL(valueChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
90
91   int aCheckedId = aHasDefaultValue ? aDefaultVal : 0;
92   myChoiceCtrl->setValue(aCheckedId);
93   aLayout->addWidget(myChoiceCtrl);
94 }
95
96 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
97 {
98 }
99
100 bool ModuleBase_WidgetChoice::storeValueCustom()
101 {
102   DataPtr aData = myFeature->data();
103   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
104
105   int aCase = 0;
106   if (myIsFirst)
107     aCase = myHasValue? myDefValue : myChoiceCtrl->value();
108   else
109     aCase = myChoiceCtrl->value();
110
111   aIntAttr->setValue(aCase);
112   myDefValue = aCase;
113   myIsFirst = false;
114
115   updateObject(myFeature);
116   return true;
117 }
118
119 bool ModuleBase_WidgetChoice::restoreValueCustom()
120 {
121   DataPtr aData = myFeature->data();
122   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
123
124   if (aIntAttr->value() != -1) {
125     bool isBlocked = myChoiceCtrl->blockSignals(true);
126     if (!myStringListAttribute.empty()) {
127       AttributeStringArrayPtr aStrAttr = aData->stringArray(myStringListAttribute);
128       QStringList aChoiceList;
129       if (aStrAttr) {
130         for (int i = 0; i < aStrAttr->size(); i++) {
131           aChoiceList << aStrAttr->value(i).c_str();
132         }
133         myChoiceCtrl->setChoiceList(aChoiceList);
134       }
135     }
136     if (aIntAttr->isInitialized())
137       myChoiceCtrl->setValue(aIntAttr->value());
138     else {
139       bool aHasDefaultValue;
140       int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
141       myChoiceCtrl->setValue(aHasDefaultValue ? aDefaultVal : 0);
142     }
143     myChoiceCtrl->blockSignals(isBlocked);
144     emit itemSelected(this, aIntAttr->value());
145     myDefValue = aIntAttr->value();
146     myIsFirst = false;
147   }
148   return true;
149 }
150
151 bool ModuleBase_WidgetChoice::focusTo()
152 {
153   return myChoiceCtrl->focusTo();
154 }
155
156 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
157 {
158   return myChoiceCtrl->getControls();
159 }
160
161 QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex)
162 {
163   QString aTitle;
164   if (myButtonTitles.length() > theIndex)
165     aTitle = myButtonTitles[theIndex];
166   return aTitle;
167 }
168
169 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
170 {
171   emit valuesChanged();
172   // Don't transfer focus
173   // emit focusOutWidget(this);
174
175   emit itemSelected(this, theIndex);
176 }
177
178 void ModuleBase_WidgetChoice::onFeatureAccepted()
179 {
180   defaultValues[myFeatureId + attributeID()] = myDefValue;
181 }