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 : ModuleBase_ModelWidget(theParent, theData), myCombo(0), myButtons(0)
28 QHBoxLayout* aLayout = new QHBoxLayout(this);
29 ModuleBase_Tools::adjustMargins(aLayout);
31 QString aLabelText = QString::fromStdString(theData->widgetLabel());
32 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
33 std::string aTypes = theData->getProperty("string_list");
34 QStringList aList = QString(aTypes.c_str()).split(' ');
36 if (theData->getBooleanAttribute("use_in_title", false))
37 myButtonTitles = QString(aTypes.c_str()).split(" ");
39 // Widget type can be combobox or radiobuttons
40 std::string aWgtType = theData->getProperty("widget_type");
41 if ((aWgtType.length() > 0) && (aWgtType == "radiobuttons")) {
42 myButtons = new QButtonGroup(this);
43 QGroupBox* aGroupBox = new QGroupBox(aLabelText, this);
44 aLayout->addWidget(aGroupBox);
47 QLayout* aBtnLayout = 0;
48 std::string aWgtDir = theData->getProperty("buttons_dir");
49 if (aWgtDir == "horizontal")
50 aBtnLayout = new QHBoxLayout(aGroupBox);
52 aBtnLayout = new QVBoxLayout(aGroupBox);
53 ModuleBase_Tools::adjustMargins(aBtnLayout);
55 std::string aIcons = theData->getProperty("icons_list");
56 QStringList aIconList = QString(aIcons.c_str()).split(' ');
57 if (aIconList.length() == aList.length()) {
59 foreach(QString aBtnTxt, aList) {
60 QToolButton* aBtn = new QToolButton(aGroupBox);
61 aBtn->setFocusPolicy(Qt::StrongFocus);
62 aBtn->setCheckable(true);
63 aBtn->setToolTip(aBtnTxt);
65 QPixmap aIcon(aIconList.at(aId));
67 aBtn->setIconSize(aIcon.size());
69 aBtnLayout->addWidget(aBtn);
70 myButtons->addButton(aBtn, aId++);
75 foreach(QString aBtnTxt, aList) {
76 QRadioButton* aBtn = new QRadioButton(aBtnTxt, aGroupBox);
77 aBtnLayout->addWidget(aBtn);
78 myButtons->addButton(aBtn, aId++);
81 myButtons->button(0)->setChecked(true);
82 connect(myButtons, SIGNAL(buttonClicked(int)), this, SLOT(onCurrentIndexChanged(int)));
84 myLabel = new QLabel(aLabelText, this);
85 if (!aLabelIcon.isEmpty())
86 myLabel->setPixmap(QPixmap(aLabelIcon));
87 aLayout->addWidget(myLabel);
89 std::string aToolstr = theData->widgetTooltip();
90 if (!aToolstr.empty()) {
91 myLabel->setToolTip(QString::fromStdString(aToolstr));
94 myCombo = new QComboBox(this);
95 aLayout->addWidget(myCombo, 1);
97 myCombo->addItems(aList);
99 connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
103 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
107 bool ModuleBase_WidgetChoice::storeValueCustom() const
109 DataPtr aData = myFeature->data();
110 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
113 aIntAttr->setValue(myCombo->currentIndex());
115 aIntAttr->setValue(myButtons->checkedId());
116 updateObject(myFeature);
120 bool ModuleBase_WidgetChoice::restoreValueCustom()
122 DataPtr aData = myFeature->data();
123 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
125 if (aIntAttr->value() != -1) {
127 bool isBlocked = myCombo->blockSignals(true);
128 myCombo->setCurrentIndex(aIntAttr->value());
129 myCombo->blockSignals(isBlocked);
131 bool isBlocked = myButtons->blockSignals(true);
132 myButtons->button(aIntAttr->value())->setChecked(true);
133 myButtons->blockSignals(isBlocked);
134 emit itemSelected(this, aIntAttr->value());
140 bool ModuleBase_WidgetChoice::focusTo()
143 ModuleBase_Tools::setFocus(myCombo, "ModuleBase_WidgetChoice::focusTo()");
149 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
151 QList<QWidget*> aControls;
153 aControls.append(myCombo);
155 // //foreach(QAbstractButton* aBtn, myButtons->buttons())
156 // //if (myButtons->checkedId() != -1)
157 // // aControls.append(myButtons->button(myButtons->checkedId()));
162 QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex)
165 if (myButtonTitles.length() > theIndex)
166 aTitle = tr(myButtonTitles[theIndex].toStdString().c_str());
170 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
172 emit valuesChanged();
173 // Don't transfer focus
174 // emit focusOutWidget(this);
176 emit itemSelected(this, theIndex);