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