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_Data.h>
27 #include <Config_WidgetAPI.h>
33 #include <QButtonGroup>
35 #include <QRadioButton>
36 #include <QToolButton>
39 ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
40 const Config_WidgetAPI* theData)
41 : ModuleBase_ModelWidget(theParent, theData), myCombo(0), myButtons(0)
43 QHBoxLayout* aLayout = new QHBoxLayout(this);
44 ModuleBase_Tools::adjustMargins(aLayout);
46 QString aLabelText = translate(theData->widgetLabel());
47 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
48 std::string aTypes = theData->getProperty("string_list");
51 foreach(QString aType, QString(aTypes.c_str()).split(' ')) {
52 aList.append(translate(aType.toStdString()));
55 if (theData->getBooleanAttribute("use_in_title", false))
56 myButtonTitles = aList;
58 bool aHasDefaultValue;
59 int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
60 // Widget type can be combobox or radiobuttons
61 std::string aWgtType = theData->getProperty("widget_type");
62 if ((aWgtType.length() > 0) && (aWgtType == "radiobuttons")) {
63 myButtons = new QButtonGroup(this);
64 QGroupBox* aGroupBox = new QGroupBox(aLabelText, this);
65 aLayout->addWidget(aGroupBox);
68 QLayout* aBtnLayout = 0;
69 std::string aWgtDir = theData->getProperty("buttons_dir");
70 if (aWgtDir == "horizontal")
71 aBtnLayout = new QHBoxLayout(aGroupBox);
73 aBtnLayout = new QVBoxLayout(aGroupBox);
74 ModuleBase_Tools::adjustMargins(aBtnLayout);
76 std::string aIcons = theData->getProperty("icons_list");
77 QStringList aIconList = QString(aIcons.c_str()).split(' ');
78 if (aIconList.length() == aList.length()) {
80 foreach(QString aBtnTxt, aList) {
81 QToolButton* aBtn = new QToolButton(aGroupBox);
82 aBtn->setFocusPolicy(Qt::StrongFocus);
83 aBtn->setCheckable(true);
84 aBtn->setToolTip(aBtnTxt);
86 QPixmap aIcon = ModuleBase_IconFactory::loadPixmap(aIconList.at(aId));
88 aBtn->setIconSize(aIcon.size());
90 aBtnLayout->addWidget(aBtn);
91 myButtons->addButton(aBtn, aId++);
96 foreach(QString aBtnTxt, aList) {
97 QRadioButton* aBtn = new QRadioButton(aBtnTxt, aGroupBox);
98 aBtnLayout->addWidget(aBtn);
99 myButtons->addButton(aBtn, aId++);
102 int aCheckedId = aHasDefaultValue ? aDefaultVal : 0;
103 myButtons->button(aDefaultVal)->setChecked(true);
104 connect(myButtons, SIGNAL(buttonClicked(int)), this, SLOT(onCurrentIndexChanged(int)));
106 myLabel = new QLabel(aLabelText, this);
107 if (!aLabelIcon.isEmpty())
108 myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
109 aLayout->addWidget(myLabel);
111 std::string aToolstr = theData->widgetTooltip();
112 if (!aToolstr.empty()) {
113 myLabel->setToolTip(QString::fromStdString(aToolstr));
116 myCombo = new QComboBox(this);
117 aLayout->addWidget(myCombo, 1);
119 myCombo->addItems(aList);
121 if (aHasDefaultValue && aDefaultVal < aList.size())
122 myCombo->setCurrentIndex(aDefaultVal);
124 connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
128 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
132 bool ModuleBase_WidgetChoice::storeValueCustom()
134 DataPtr aData = myFeature->data();
135 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
138 aIntAttr->setValue(myCombo->currentIndex());
140 aIntAttr->setValue(myButtons->checkedId());
141 updateObject(myFeature);
145 bool ModuleBase_WidgetChoice::restoreValueCustom()
147 DataPtr aData = myFeature->data();
148 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
150 if (aIntAttr->value() != -1) {
152 bool isBlocked = myCombo->blockSignals(true);
153 myCombo->setCurrentIndex(aIntAttr->value());
154 myCombo->blockSignals(isBlocked);
156 bool isBlocked = myButtons->blockSignals(true);
157 if (aIntAttr->isInitialized())
158 myButtons->button(aIntAttr->value())->setChecked(true);
160 bool aHasDefaultValue;
161 int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
162 myButtons->button(aHasDefaultValue ? aDefaultVal : 0)->setChecked(true);
164 myButtons->blockSignals(isBlocked);
165 emit itemSelected(this, aIntAttr->value());
171 bool ModuleBase_WidgetChoice::focusTo()
174 ModuleBase_Tools::setFocus(myCombo, "ModuleBase_WidgetChoice::focusTo()");
180 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
182 QList<QWidget*> aControls;
184 aControls.append(myCombo);
186 // //foreach(QAbstractButton* aBtn, myButtons->buttons())
187 // //if (myButtons->checkedId() != -1)
188 // // aControls.append(myButtons->button(myButtons->checkedId()));
193 QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex)
196 if (myButtonTitles.length() > theIndex)
197 aTitle = myButtonTitles[theIndex];
201 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
203 emit valuesChanged();
204 // Don't transfer focus
205 // emit focusOutWidget(this);
207 emit itemSelected(this, theIndex);