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>
28 #include <Config_PropManager.h>
36 #include <QButtonGroup>
38 #include <QRadioButton>
39 #include <QTextStream>
40 #include <QToolButton>
42 void getValues(const std::string& thePath, const std::string& theFileName,
43 QStringList& theValues)
45 QString aFileName = thePath.c_str();
46 aFileName += QDir::separator();
47 aFileName += theFileName.c_str();
49 QFile aFile(aFileName);
50 if (!aFile.open(QIODevice::ReadOnly | QIODevice::Text))
53 QTextStream aStream(&aFile);
54 while (!aStream.atEnd()) {
55 QString aLine = aStream.readLine();
57 theValues.append(aLine);
61 ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
62 const Config_WidgetAPI* theData)
63 : ModuleBase_ModelWidget(theParent, theData), myCombo(0), myButtons(0)
65 QHBoxLayout* aLayout = new QHBoxLayout(this);
66 ModuleBase_Tools::adjustMargins(aLayout);
68 QString aLabelText = translate(theData->widgetLabel());
69 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
70 std::string aTypes = theData->getProperty("string_list");
73 foreach(QString aType, QString(aTypes.c_str()).split(' ')) {
74 aList.append(translate(aType.toStdString()));
78 std::string aFileName = theData->getProperty("file_name");
79 if (!aFileName.empty()) {
80 std::string aPath = Config_PropManager::string("Plugins", "combo_box_elements_path");
81 getValues(aPath, aFileName, aList);
85 if (theData->getBooleanAttribute("use_in_title", false))
86 myButtonTitles = aList;
88 bool aHasDefaultValue;
89 int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
90 // Widget type can be combobox or radiobuttons
91 std::string aWgtType = theData->getProperty("widget_type");
92 if ((aWgtType.length() > 0) && (aWgtType == "radiobuttons")) {
93 myButtons = new QButtonGroup(this);
94 QGroupBox* aGroupBox = new QGroupBox(aLabelText, this);
95 aLayout->addWidget(aGroupBox);
98 QLayout* aBtnLayout = 0;
99 std::string aWgtDir = theData->getProperty("buttons_dir");
100 if (aWgtDir == "horizontal")
101 aBtnLayout = new QHBoxLayout(aGroupBox);
103 aBtnLayout = new QVBoxLayout(aGroupBox);
104 ModuleBase_Tools::adjustMargins(aBtnLayout);
106 std::string aIcons = theData->getProperty("icons_list");
107 QStringList aIconList = QString(aIcons.c_str()).split(' ');
108 if (aIconList.length() == aList.length()) {
110 foreach(QString aBtnTxt, aList) {
111 QToolButton* aBtn = new QToolButton(aGroupBox);
112 aBtn->setFocusPolicy(Qt::StrongFocus);
113 aBtn->setCheckable(true);
114 aBtn->setToolTip(aBtnTxt);
116 QPixmap aIcon = ModuleBase_IconFactory::loadPixmap(aIconList.at(aId));
117 aBtn->setIcon(aIcon);
118 aBtn->setIconSize(aIcon.size());
120 aBtnLayout->addWidget(aBtn);
121 myButtons->addButton(aBtn, aId++);
126 foreach(QString aBtnTxt, aList) {
127 QRadioButton* aBtn = new QRadioButton(aBtnTxt, aGroupBox);
128 aBtnLayout->addWidget(aBtn);
129 myButtons->addButton(aBtn, aId++);
132 int aCheckedId = aHasDefaultValue ? aDefaultVal : 0;
133 myButtons->button(aDefaultVal)->setChecked(true);
134 connect(myButtons, SIGNAL(buttonClicked(int)), this, SLOT(onCurrentIndexChanged(int)));
136 myLabel = new QLabel(aLabelText, this);
137 if (!aLabelIcon.isEmpty())
138 myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
139 aLayout->addWidget(myLabel);
141 std::string aToolstr = theData->widgetTooltip();
142 if (!aToolstr.empty()) {
143 myLabel->setToolTip(QString::fromStdString(aToolstr));
146 myCombo = new QComboBox(this);
147 aLayout->addWidget(myCombo, 1);
149 myCombo->addItems(aList);
151 if (aHasDefaultValue && aDefaultVal < aList.size())
152 myCombo->setCurrentIndex(aDefaultVal);
154 connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
158 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
162 bool ModuleBase_WidgetChoice::storeValueCustom()
164 DataPtr aData = myFeature->data();
165 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
168 aIntAttr->setValue(myCombo->currentIndex());
170 aIntAttr->setValue(myButtons->checkedId());
171 updateObject(myFeature);
175 bool ModuleBase_WidgetChoice::restoreValueCustom()
177 DataPtr aData = myFeature->data();
178 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
180 if (aIntAttr->value() != -1) {
182 bool isBlocked = myCombo->blockSignals(true);
183 myCombo->setCurrentIndex(aIntAttr->value());
184 myCombo->blockSignals(isBlocked);
186 bool isBlocked = myButtons->blockSignals(true);
187 if (aIntAttr->isInitialized())
188 myButtons->button(aIntAttr->value())->setChecked(true);
190 bool aHasDefaultValue;
191 int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
192 myButtons->button(aHasDefaultValue ? aDefaultVal : 0)->setChecked(true);
194 myButtons->blockSignals(isBlocked);
195 emit itemSelected(this, aIntAttr->value());
201 bool ModuleBase_WidgetChoice::focusTo()
204 ModuleBase_Tools::setFocus(myCombo, "ModuleBase_WidgetChoice::focusTo()");
210 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
212 QList<QWidget*> aControls;
214 aControls.append(myCombo);
216 // //foreach(QAbstractButton* aBtn, myButtons->buttons())
217 // //if (myButtons->checkedId() != -1)
218 // // aControls.append(myButtons->button(myButtons->checkedId()));
223 QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex)
226 if (myButtonTitles.length() > theIndex)
227 aTitle = myButtonTitles[theIndex];
231 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
233 emit valuesChanged();
234 // Don't transfer focus
235 // emit focusOutWidget(this);
237 emit itemSelected(this, theIndex);