1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ModuleBase_WidgetChoice.h"
21 #include "ModuleBase_Tools.h"
22 #include "ModuleBase_IconFactory.h"
23 #include "ModuleBase_ChoiceCtrl.h"
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>
34 #include <QButtonGroup>
36 #include <QRadioButton>
37 #include <QToolButton>
39 static QMap<std::string, int> defaultValues;
41 ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
42 const Config_WidgetAPI* theData)
43 : ModuleBase_ModelWidget(theParent, theData), myIsFirst(true)
45 myHasValue = defaultValues.contains(myFeatureId + attributeID());
47 myDefValue = defaultValues[myFeatureId + attributeID()];
51 QString aLabelText = translate(theData->widgetLabel());
52 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
53 std::string aTypes = theData->getProperty("string_list");
56 foreach(QString aType, QString(aTypes.c_str()).split(' ')) {
57 aList.append(translate(aType.toStdString()));
60 myStringListAttribute = theData->getProperty("string_list_attribute");
61 if (!myStringListAttribute.empty())
64 if (theData->getBooleanAttribute("use_in_title", false))
65 myButtonTitles = aList;
67 bool aHasDefaultValue;
68 int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
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(' ');
75 std::string aWgtDir = theData->getProperty("buttons_dir");
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);
84 if (!aLabelIcon.isEmpty())
85 myChoiceCtrl->setLabelIcon(aLabelIcon);
87 connect(myChoiceCtrl, SIGNAL(valueChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
89 int aCheckedId = aHasDefaultValue ? aDefaultVal : 0;
90 myChoiceCtrl->setValue(aCheckedId);
91 aLayout->addWidget(myChoiceCtrl);
94 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
98 bool ModuleBase_WidgetChoice::storeValueCustom()
100 DataPtr aData = myFeature->data();
101 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
105 aCase = myHasValue? myDefValue : myChoiceCtrl->value();
107 aCase = myChoiceCtrl->value();
109 aIntAttr->setValue(aCase);
113 updateObject(myFeature);
117 bool ModuleBase_WidgetChoice::restoreValueCustom()
119 DataPtr aData = myFeature->data();
120 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
122 if (aIntAttr->value() != -1) {
123 bool isBlocked = myChoiceCtrl->blockSignals(true);
124 if (!myStringListAttribute.empty()) {
125 AttributeStringArrayPtr aStrAttr = aData->stringArray(myStringListAttribute);
126 QStringList aChoiceList;
128 for (int i = 0; i < aStrAttr->size(); i++) {
129 aChoiceList << aStrAttr->value(i).c_str();
131 myChoiceCtrl->setChoiceList(aChoiceList);
134 if (aIntAttr->isInitialized()) {
135 myChoiceCtrl->setValue(aIntAttr->value());
137 myChoiceCtrl->blockSignals(isBlocked);
138 emit itemSelected(this, aIntAttr->value());
139 myDefValue = aIntAttr->value();
142 bool aHasDefaultValue;
143 int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
144 int aVal = aHasDefaultValue ? aDefaultVal : 0;
145 myChoiceCtrl->setValue(aVal);
147 myChoiceCtrl->blockSignals(isBlocked);
148 emit itemSelected(this, aVal);
156 bool ModuleBase_WidgetChoice::focusTo()
158 return myChoiceCtrl->focusTo();
161 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
163 return myChoiceCtrl->getControls();
166 QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex)
169 if (myButtonTitles.length() > theIndex)
170 aTitle = myButtonTitles[theIndex];
174 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
176 emit valuesChanged();
177 // Don't transfer focus
178 // emit focusOutWidget(this);
180 emit itemSelected(this, theIndex);
183 void ModuleBase_WidgetChoice::onFeatureAccepted()
185 defaultValues[myFeatureId + attributeID()] = myDefValue;