1 // Copyright (C) 2014-2019 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>
35 #include <QButtonGroup>
37 #include <QRadioButton>
38 #include <QToolButton>
40 static QMap<std::string, int> defaultValues;
42 ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
43 const Config_WidgetAPI* theData)
44 : ModuleBase_ModelWidget(theParent, theData), myIsFirst(true)
46 myHasValue = defaultValues.contains(myFeatureId + attributeID());
48 myDefValue = defaultValues[myFeatureId + attributeID()];
52 QString aLabelText = translate(theData->widgetLabel());
53 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
54 std::string aTypes = theData->getProperty("string_list");
57 foreach(QString aType, QString(aTypes.c_str()).split(' ')) {
58 aList.append(translate(aType.toStdString()));
61 myStringListAttribute = theData->getProperty("string_list_attribute");
62 if (!myStringListAttribute.empty())
65 if (theData->getBooleanAttribute("use_in_title", false))
66 myButtonTitles = aList;
68 bool aHasDefaultValue;
69 int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
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(' ');
76 std::string aWgtDir = theData->getProperty("buttons_dir");
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);
85 if (!aLabelIcon.isEmpty())
86 myChoiceCtrl->setLabelIcon(aLabelIcon);
88 connect(myChoiceCtrl, SIGNAL(valueChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
90 int aCheckedId = aHasDefaultValue ? aDefaultVal : 0;
91 myChoiceCtrl->setValue(aCheckedId);
92 aLayout->addWidget(myChoiceCtrl);
95 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
99 bool ModuleBase_WidgetChoice::storeValueCustom()
101 DataPtr aData = myFeature->data();
102 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
106 aCase = myHasValue? myDefValue : myChoiceCtrl->value();
108 aCase = myChoiceCtrl->value();
110 aIntAttr->setValue(aCase);
114 updateObject(myFeature);
118 bool ModuleBase_WidgetChoice::restoreValueCustom()
120 DataPtr aData = myFeature->data();
121 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
123 if (aIntAttr->value() != -1) {
124 bool isBlocked = myChoiceCtrl->blockSignals(true);
125 if (!myStringListAttribute.empty()) {
126 AttributeStringArrayPtr aStrAttr = aData->stringArray(myStringListAttribute);
127 QStringList aChoiceList;
129 for (int i = 0; i < aStrAttr->size(); i++) {
130 aChoiceList << aStrAttr->value(i).c_str();
132 myChoiceCtrl->setChoiceList(aChoiceList);
135 if (aIntAttr->isInitialized()) {
136 myChoiceCtrl->setValue(aIntAttr->value());
138 myChoiceCtrl->blockSignals(isBlocked);
139 emit itemSelected(this, aIntAttr->value());
140 myDefValue = aIntAttr->value();
143 bool aHasDefaultValue;
144 int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
145 int aVal = aHasDefaultValue ? aDefaultVal : 0;
146 myChoiceCtrl->setValue(aVal);
148 myChoiceCtrl->blockSignals(isBlocked);
149 emit itemSelected(this, aVal);
157 bool ModuleBase_WidgetChoice::focusTo()
159 return myChoiceCtrl->focusTo();
162 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
164 return myChoiceCtrl->getControls();
167 QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex)
170 if (myButtonTitles.length() > theIndex)
171 aTitle = myButtonTitles[theIndex];
175 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
177 emit valuesChanged();
178 // Don't transfer focus
179 // emit focusOutWidget(this);
181 emit itemSelected(this, theIndex);
184 void ModuleBase_WidgetChoice::onFeatureAccepted()
186 defaultValues[myFeatureId + attributeID()] = myDefValue;