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 // Widget type can be combobox or radiobuttons
59 std::string aWgtType = theData->getProperty("widget_type");
60 if ((aWgtType.length() > 0) && (aWgtType == "radiobuttons")) {
61 myButtons = new QButtonGroup(this);
62 QGroupBox* aGroupBox = new QGroupBox(aLabelText, this);
63 aLayout->addWidget(aGroupBox);
66 QLayout* aBtnLayout = 0;
67 std::string aWgtDir = theData->getProperty("buttons_dir");
68 if (aWgtDir == "horizontal")
69 aBtnLayout = new QHBoxLayout(aGroupBox);
71 aBtnLayout = new QVBoxLayout(aGroupBox);
72 ModuleBase_Tools::adjustMargins(aBtnLayout);
74 std::string aIcons = theData->getProperty("icons_list");
75 QStringList aIconList = QString(aIcons.c_str()).split(' ');
76 if (aIconList.length() == aList.length()) {
78 foreach(QString aBtnTxt, aList) {
79 QToolButton* aBtn = new QToolButton(aGroupBox);
80 aBtn->setFocusPolicy(Qt::StrongFocus);
81 aBtn->setCheckable(true);
82 aBtn->setToolTip(aBtnTxt);
84 QPixmap aIcon = ModuleBase_IconFactory::loadPixmap(aIconList.at(aId));
86 aBtn->setIconSize(aIcon.size());
88 aBtnLayout->addWidget(aBtn);
89 myButtons->addButton(aBtn, aId++);
94 foreach(QString aBtnTxt, aList) {
95 QRadioButton* aBtn = new QRadioButton(aBtnTxt, aGroupBox);
96 aBtnLayout->addWidget(aBtn);
97 myButtons->addButton(aBtn, aId++);
100 myButtons->button(0)->setChecked(true);
101 connect(myButtons, SIGNAL(buttonClicked(int)), this, SLOT(onCurrentIndexChanged(int)));
103 myLabel = new QLabel(aLabelText, this);
104 if (!aLabelIcon.isEmpty())
105 myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
106 aLayout->addWidget(myLabel);
108 std::string aToolstr = theData->widgetTooltip();
109 if (!aToolstr.empty()) {
110 myLabel->setToolTip(QString::fromStdString(aToolstr));
113 myCombo = new QComboBox(this);
114 aLayout->addWidget(myCombo, 1);
116 myCombo->addItems(aList);
118 connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
122 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
126 bool ModuleBase_WidgetChoice::storeValueCustom()
128 DataPtr aData = myFeature->data();
129 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
132 aIntAttr->setValue(myCombo->currentIndex());
134 aIntAttr->setValue(myButtons->checkedId());
135 updateObject(myFeature);
139 bool ModuleBase_WidgetChoice::restoreValueCustom()
141 DataPtr aData = myFeature->data();
142 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
144 if (aIntAttr->value() != -1) {
146 bool isBlocked = myCombo->blockSignals(true);
147 myCombo->setCurrentIndex(aIntAttr->value());
148 myCombo->blockSignals(isBlocked);
150 bool isBlocked = myButtons->blockSignals(true);
151 myButtons->button(aIntAttr->value())->setChecked(true);
152 myButtons->blockSignals(isBlocked);
153 emit itemSelected(this, aIntAttr->value());
159 bool ModuleBase_WidgetChoice::focusTo()
162 ModuleBase_Tools::setFocus(myCombo, "ModuleBase_WidgetChoice::focusTo()");
168 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
170 QList<QWidget*> aControls;
172 aControls.append(myCombo);
174 // //foreach(QAbstractButton* aBtn, myButtons->buttons())
175 // //if (myButtons->checkedId() != -1)
176 // // aControls.append(myButtons->button(myButtons->checkedId()));
181 QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex)
184 if (myButtonTitles.length() > theIndex)
185 aTitle = myButtonTitles[theIndex];
189 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
191 emit valuesChanged();
192 // Don't transfer focus
193 // emit focusOutWidget(this);
195 emit itemSelected(this, theIndex);