1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "ModuleBase_WidgetChoice.h"
22 #include "ModuleBase_Tools.h"
23 #include "ModuleBase_IconFactory.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 ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
41 const Config_WidgetAPI* theData)
42 : ModuleBase_ModelWidget(theParent, theData), myCombo(0), myButtons(0)
44 QHBoxLayout* aLayout = new QHBoxLayout(this);
45 ModuleBase_Tools::adjustMargins(aLayout);
47 QString aLabelText = translate(theData->widgetLabel());
48 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
49 std::string aTypes = theData->getProperty("string_list");
52 foreach(QString aType, QString(aTypes.c_str()).split(' ')) {
53 aList.append(translate(aType.toStdString()));
56 myStringListAttribute = theData->getProperty("string_list_attribute");
57 if (!myStringListAttribute.empty())
60 if (theData->getBooleanAttribute("use_in_title", false))
61 myButtonTitles = aList;
63 bool aHasDefaultValue;
64 int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
65 // Widget type can be combobox or radiobuttons
66 std::string aWgtType = theData->getProperty("widget_type");
67 if ((aWgtType.length() > 0) && (aWgtType == "radiobuttons")) {
68 myButtons = new QButtonGroup(this);
69 QGroupBox* aGroupBox = new QGroupBox(aLabelText, this);
70 aLayout->addWidget(aGroupBox);
73 QLayout* aBtnLayout = 0;
74 std::string aWgtDir = theData->getProperty("buttons_dir");
75 if (aWgtDir == "horizontal")
76 aBtnLayout = new QHBoxLayout(aGroupBox);
78 aBtnLayout = new QVBoxLayout(aGroupBox);
79 ModuleBase_Tools::adjustMargins(aBtnLayout);
81 std::string aIcons = theData->getProperty("icons_list");
82 QStringList aIconList = QString(aIcons.c_str()).split(' ');
83 if (aIconList.length() == aList.length()) {
85 foreach(QString aBtnTxt, aList) {
86 QToolButton* aBtn = new QToolButton(aGroupBox);
87 aBtn->setFocusPolicy(Qt::StrongFocus);
88 aBtn->setCheckable(true);
89 aBtn->setToolTip(aBtnTxt);
91 QPixmap aIcon = ModuleBase_IconFactory::loadPixmap(aIconList.at(aId));
93 aBtn->setIconSize(aIcon.size());
95 aBtnLayout->addWidget(aBtn);
96 myButtons->addButton(aBtn, aId++);
101 foreach(QString aBtnTxt, aList) {
102 QRadioButton* aBtn = new QRadioButton(aBtnTxt, aGroupBox);
103 aBtnLayout->addWidget(aBtn);
104 myButtons->addButton(aBtn, aId++);
107 int aCheckedId = aHasDefaultValue ? aDefaultVal : 0;
108 myButtons->button(aDefaultVal)->setChecked(true);
109 connect(myButtons, SIGNAL(buttonClicked(int)), this, SLOT(onCurrentIndexChanged(int)));
111 myLabel = new QLabel(aLabelText, this);
112 if (!aLabelIcon.isEmpty())
113 myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
114 aLayout->addWidget(myLabel);
116 std::string aToolstr = theData->widgetTooltip();
117 if (!aToolstr.empty()) {
118 myLabel->setToolTip(QString::fromStdString(aToolstr));
121 myCombo = new QComboBox(this);
122 aLayout->addWidget(myCombo, 1);
124 myCombo->addItems(aList);
126 if (aHasDefaultValue && aDefaultVal < aList.size())
127 myCombo->setCurrentIndex(aDefaultVal);
129 connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
133 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
137 bool ModuleBase_WidgetChoice::storeValueCustom()
139 DataPtr aData = myFeature->data();
140 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
143 aIntAttr->setValue(myCombo->currentIndex());
145 aIntAttr->setValue(myButtons->checkedId());
146 updateObject(myFeature);
150 bool ModuleBase_WidgetChoice::restoreValueCustom()
152 DataPtr aData = myFeature->data();
153 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
155 if (aIntAttr->value() != -1) {
157 bool isBlocked = myCombo->blockSignals(true);
158 if (myCombo->count() == 0 && !myStringListAttribute.empty()) {
159 AttributeStringArrayPtr aStrAttr = aData->stringArray(myStringListAttribute);
161 for (int i = 0; i < aStrAttr->size(); i++) {
162 myCombo->insertItem(i, aStrAttr->value(i).c_str());
166 myCombo->setCurrentIndex(aIntAttr->value());
167 myCombo->blockSignals(isBlocked);
169 bool isBlocked = myButtons->blockSignals(true);
170 if (aIntAttr->isInitialized())
171 myButtons->button(aIntAttr->value())->setChecked(true);
173 bool aHasDefaultValue;
174 int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
175 myButtons->button(aHasDefaultValue ? aDefaultVal : 0)->setChecked(true);
177 myButtons->blockSignals(isBlocked);
178 emit itemSelected(this, aIntAttr->value());
184 bool ModuleBase_WidgetChoice::focusTo()
187 ModuleBase_Tools::setFocus(myCombo, "ModuleBase_WidgetChoice::focusTo()");
193 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
195 QList<QWidget*> aControls;
197 aControls.append(myCombo);
199 // //foreach(QAbstractButton* aBtn, myButtons->buttons())
200 // //if (myButtons->checkedId() != -1)
201 // // aControls.append(myButtons->button(myButtons->checkedId()));
206 QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex)
209 if (myButtonTitles.length() > theIndex)
210 aTitle = myButtonTitles[theIndex];
214 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
216 emit valuesChanged();
217 // Don't transfer focus
218 // emit focusOutWidget(this);
220 emit itemSelected(this, theIndex);