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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "ModuleBase_WidgetChoice.h"
21 #include "ModuleBase_Tools.h"
22 #include "ModuleBase_IconFactory.h"
24 #include <ModelAPI_AttributeInteger.h>
25 #include <ModelAPI_Data.h>
26 #include <Config_WidgetAPI.h>
32 #include <QButtonGroup>
34 #include <QRadioButton>
35 #include <QToolButton>
38 ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
39 const Config_WidgetAPI* theData)
40 : ModuleBase_ModelWidget(theParent, theData), myCombo(0), myButtons(0)
42 QHBoxLayout* aLayout = new QHBoxLayout(this);
43 ModuleBase_Tools::adjustMargins(aLayout);
45 QString aLabelText = translate(theData->widgetLabel());
46 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
47 std::string aTypes = theData->getProperty("string_list");
50 foreach(QString aType, QString(aTypes.c_str()).split(' ')) {
51 aList.append(translate(aType.toStdString()));
54 if (theData->getBooleanAttribute("use_in_title", false))
55 myButtonTitles = aList;
57 // Widget type can be combobox or radiobuttons
58 std::string aWgtType = theData->getProperty("widget_type");
59 if ((aWgtType.length() > 0) && (aWgtType == "radiobuttons")) {
60 myButtons = new QButtonGroup(this);
61 QGroupBox* aGroupBox = new QGroupBox(aLabelText, this);
62 aLayout->addWidget(aGroupBox);
65 QLayout* aBtnLayout = 0;
66 std::string aWgtDir = theData->getProperty("buttons_dir");
67 if (aWgtDir == "horizontal")
68 aBtnLayout = new QHBoxLayout(aGroupBox);
70 aBtnLayout = new QVBoxLayout(aGroupBox);
71 ModuleBase_Tools::adjustMargins(aBtnLayout);
73 std::string aIcons = theData->getProperty("icons_list");
74 QStringList aIconList = QString(aIcons.c_str()).split(' ');
75 if (aIconList.length() == aList.length()) {
77 foreach(QString aBtnTxt, aList) {
78 QToolButton* aBtn = new QToolButton(aGroupBox);
79 aBtn->setFocusPolicy(Qt::StrongFocus);
80 aBtn->setCheckable(true);
81 aBtn->setToolTip(aBtnTxt);
83 QPixmap aIcon = ModuleBase_IconFactory::loadPixmap(aIconList.at(aId));
85 aBtn->setIconSize(aIcon.size());
87 aBtnLayout->addWidget(aBtn);
88 myButtons->addButton(aBtn, aId++);
93 foreach(QString aBtnTxt, aList) {
94 QRadioButton* aBtn = new QRadioButton(aBtnTxt, aGroupBox);
95 aBtnLayout->addWidget(aBtn);
96 myButtons->addButton(aBtn, aId++);
99 myButtons->button(0)->setChecked(true);
100 connect(myButtons, SIGNAL(buttonClicked(int)), this, SLOT(onCurrentIndexChanged(int)));
102 myLabel = new QLabel(aLabelText, this);
103 if (!aLabelIcon.isEmpty())
104 myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
105 aLayout->addWidget(myLabel);
107 std::string aToolstr = theData->widgetTooltip();
108 if (!aToolstr.empty()) {
109 myLabel->setToolTip(QString::fromStdString(aToolstr));
112 myCombo = new QComboBox(this);
113 aLayout->addWidget(myCombo, 1);
115 myCombo->addItems(aList);
117 connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
121 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
125 bool ModuleBase_WidgetChoice::storeValueCustom()
127 DataPtr aData = myFeature->data();
128 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
131 aIntAttr->setValue(myCombo->currentIndex());
133 aIntAttr->setValue(myButtons->checkedId());
134 updateObject(myFeature);
138 bool ModuleBase_WidgetChoice::restoreValueCustom()
140 DataPtr aData = myFeature->data();
141 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
143 if (aIntAttr->value() != -1) {
145 bool isBlocked = myCombo->blockSignals(true);
146 myCombo->setCurrentIndex(aIntAttr->value());
147 myCombo->blockSignals(isBlocked);
149 bool isBlocked = myButtons->blockSignals(true);
150 myButtons->button(aIntAttr->value())->setChecked(true);
151 myButtons->blockSignals(isBlocked);
152 emit itemSelected(this, aIntAttr->value());
158 bool ModuleBase_WidgetChoice::focusTo()
161 ModuleBase_Tools::setFocus(myCombo, "ModuleBase_WidgetChoice::focusTo()");
167 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
169 QList<QWidget*> aControls;
171 aControls.append(myCombo);
173 // //foreach(QAbstractButton* aBtn, myButtons->buttons())
174 // //if (myButtons->checkedId() != -1)
175 // // aControls.append(myButtons->button(myButtons->checkedId()));
180 QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex)
183 if (myButtonTitles.length() > theIndex)
184 aTitle = myButtonTitles[theIndex];
188 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
190 emit valuesChanged();
191 // Don't transfer focus
192 // emit focusOutWidget(this);
194 emit itemSelected(this, theIndex);