1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_WidgetChoice.cpp
4 // Created: 03 Sept 2014
5 // Author: Vitaly Smetannikov
7 #include "ModuleBase_WidgetChoice.h"
8 #include <ModuleBase_Tools.h>
10 #include <ModelAPI_AttributeInteger.h>
11 #include <ModelAPI_Data.h>
12 #include <Config_WidgetAPI.h>
18 #include <QButtonGroup>
20 #include <QRadioButton>
21 #include <QToolButton>
24 ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
25 const Config_WidgetAPI* theData,
26 const std::string& theParentId)
27 : ModuleBase_ModelWidget(theParent, theData, theParentId), myCombo(0), myButtons(0)
29 QHBoxLayout* aLayout = new QHBoxLayout(this);
30 ModuleBase_Tools::adjustMargins(aLayout);
32 QString aLabelText = QString::fromStdString(theData->widgetLabel());
33 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
34 std::string aTypes = theData->getProperty("string_list");
35 QStringList aList = QString(aTypes.c_str()).split(' ');
37 // Widget type can be combobox or radiobuttons
38 std::string aWgtType = theData->getProperty("widget_type");
39 if ((aWgtType.length() > 0) && (aWgtType == "radiobuttons")) {
40 myButtons = new QButtonGroup(this);
41 QGroupBox* aGroupBox = new QGroupBox(aLabelText, this);
42 aLayout->addWidget(aGroupBox);
45 QLayout* aBtnLayout = 0;
46 std::string aWgtDir = theData->getProperty("buttons_dir");
47 if (aWgtDir == "horizontal")
48 aBtnLayout = new QHBoxLayout(aGroupBox);
50 aBtnLayout = new QVBoxLayout(aGroupBox);
51 ModuleBase_Tools::adjustMargins(aBtnLayout);
53 std::string aIcons = theData->getProperty("icons_list");
54 QStringList aIconList = QString(aIcons.c_str()).split(' ');
55 if (aIconList.length() == aList.length()) {
57 foreach(QString aBtnTxt, aList) {
58 QToolButton* aBtn = new QToolButton(aGroupBox);
59 aBtn->setCheckable(true);
60 aBtn->setToolTip(aBtnTxt);
62 QPixmap aIcon(aIconList.at(aId));
64 aBtn->setIconSize(aIcon.size());
66 aBtnLayout->addWidget(aBtn);
67 myButtons->addButton(aBtn, aId++);
72 foreach(QString aBtnTxt, aList) {
73 QRadioButton* aBtn = new QRadioButton(aBtnTxt, aGroupBox);
74 aBtnLayout->addWidget(aBtn);
75 myButtons->addButton(aBtn, aId++);
78 myButtons->button(0)->setChecked(true);
79 connect(myButtons, SIGNAL(buttonClicked(int)), this, SLOT(onCurrentIndexChanged(int)));
81 myLabel = new QLabel(aLabelText, this);
82 if (!aLabelIcon.isEmpty())
83 myLabel->setPixmap(QPixmap(aLabelIcon));
84 aLayout->addWidget(myLabel);
86 std::string aToolstr = theData->widgetTooltip();
87 if (!aToolstr.empty()) {
88 myLabel->setToolTip(QString::fromStdString(aToolstr));
91 myCombo = new QComboBox(this);
92 aLayout->addWidget(myCombo, 1);
94 myCombo->addItems(aList);
96 connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
100 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
104 bool ModuleBase_WidgetChoice::storeValueCustom() const
106 DataPtr aData = myFeature->data();
107 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
110 aIntAttr->setValue(myCombo->currentIndex());
112 aIntAttr->setValue(myButtons->checkedId());
113 updateObject(myFeature);
117 bool ModuleBase_WidgetChoice::restoreValueCustom()
119 DataPtr aData = myFeature->data();
120 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
122 if (aIntAttr->value() != -1) {
124 bool isBlocked = myCombo->blockSignals(true);
125 myCombo->setCurrentIndex(aIntAttr->value());
126 myCombo->blockSignals(isBlocked);
128 bool isBlocked = myButtons->blockSignals(true);
129 myButtons->button(aIntAttr->value())->setChecked(true);
130 myButtons->blockSignals(isBlocked);
136 bool ModuleBase_WidgetChoice::focusTo()
141 myButtons->button(0)->setFocus();
145 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
147 QList<QWidget*> aControls;
149 aControls.append(myCombo);
151 // //foreach(QAbstractButton* aBtn, myButtons->buttons())
152 // //if (myButtons->checkedId() != -1)
153 // // aControls.append(myButtons->button(myButtons->checkedId()));
158 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
160 emit valuesChanged();
161 // Don't transfer focus
162 // emit focusOutWidget(this);