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"
9 #include "ModuleBase_IconFactory.h"
11 #include <ModelAPI_AttributeInteger.h>
12 #include <ModelAPI_Data.h>
13 #include <Config_WidgetAPI.h>
19 #include <QButtonGroup>
21 #include <QRadioButton>
22 #include <QToolButton>
25 ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
26 const Config_WidgetAPI* theData)
27 : ModuleBase_ModelWidget(theParent, theData), 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 if (theData->getBooleanAttribute("use_in_title", false))
38 myButtonTitles = QString(aTypes.c_str()).split(" ");
40 // Widget type can be combobox or radiobuttons
41 std::string aWgtType = theData->getProperty("widget_type");
42 if ((aWgtType.length() > 0) && (aWgtType == "radiobuttons")) {
43 myButtons = new QButtonGroup(this);
44 QGroupBox* aGroupBox = new QGroupBox(aLabelText, this);
45 aLayout->addWidget(aGroupBox);
48 QLayout* aBtnLayout = 0;
49 std::string aWgtDir = theData->getProperty("buttons_dir");
50 if (aWgtDir == "horizontal")
51 aBtnLayout = new QHBoxLayout(aGroupBox);
53 aBtnLayout = new QVBoxLayout(aGroupBox);
54 ModuleBase_Tools::adjustMargins(aBtnLayout);
56 std::string aIcons = theData->getProperty("icons_list");
57 QStringList aIconList = QString(aIcons.c_str()).split(' ');
58 if (aIconList.length() == aList.length()) {
60 foreach(QString aBtnTxt, aList) {
61 QToolButton* aBtn = new QToolButton(aGroupBox);
62 aBtn->setFocusPolicy(Qt::StrongFocus);
63 aBtn->setCheckable(true);
64 aBtn->setToolTip(aBtnTxt);
66 QPixmap aIcon = ModuleBase_IconFactory::loadPixmap(aIconList.at(aId));
68 aBtn->setIconSize(aIcon.size());
70 aBtnLayout->addWidget(aBtn);
71 myButtons->addButton(aBtn, aId++);
76 foreach(QString aBtnTxt, aList) {
77 QRadioButton* aBtn = new QRadioButton(aBtnTxt, aGroupBox);
78 aBtnLayout->addWidget(aBtn);
79 myButtons->addButton(aBtn, aId++);
82 myButtons->button(0)->setChecked(true);
83 connect(myButtons, SIGNAL(buttonClicked(int)), this, SLOT(onCurrentIndexChanged(int)));
85 myLabel = new QLabel(aLabelText, this);
86 if (!aLabelIcon.isEmpty())
87 myLabel->setPixmap(QPixmap(aLabelIcon));
88 aLayout->addWidget(myLabel);
90 std::string aToolstr = theData->widgetTooltip();
91 if (!aToolstr.empty()) {
92 myLabel->setToolTip(QString::fromStdString(aToolstr));
95 myCombo = new QComboBox(this);
96 aLayout->addWidget(myCombo, 1);
98 myCombo->addItems(aList);
100 connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
104 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
108 bool ModuleBase_WidgetChoice::storeValueCustom()
110 DataPtr aData = myFeature->data();
111 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
114 aIntAttr->setValue(myCombo->currentIndex());
116 aIntAttr->setValue(myButtons->checkedId());
117 updateObject(myFeature);
121 bool ModuleBase_WidgetChoice::restoreValueCustom()
123 DataPtr aData = myFeature->data();
124 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
126 if (aIntAttr->value() != -1) {
128 bool isBlocked = myCombo->blockSignals(true);
129 myCombo->setCurrentIndex(aIntAttr->value());
130 myCombo->blockSignals(isBlocked);
132 bool isBlocked = myButtons->blockSignals(true);
133 myButtons->button(aIntAttr->value())->setChecked(true);
134 myButtons->blockSignals(isBlocked);
135 emit itemSelected(this, aIntAttr->value());
141 bool ModuleBase_WidgetChoice::focusTo()
144 ModuleBase_Tools::setFocus(myCombo, "ModuleBase_WidgetChoice::focusTo()");
150 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
152 QList<QWidget*> aControls;
154 aControls.append(myCombo);
156 // //foreach(QAbstractButton* aBtn, myButtons->buttons())
157 // //if (myButtons->checkedId() != -1)
158 // // aControls.append(myButtons->button(myButtons->checkedId()));
163 QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex)
166 if (myButtonTitles.length() > theIndex)
167 aTitle = tr(myButtonTitles[theIndex].toStdString().c_str());
171 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
173 emit valuesChanged();
174 // Don't transfer focus
175 // emit focusOutWidget(this);
177 emit itemSelected(this, theIndex);