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