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 = translate(theData->widgetLabel());
33 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
34 std::string aTypes = theData->getProperty("string_list");
37 foreach(QString aType, QString(aTypes.c_str()).split(' ')) {
38 aList.append(translate(aType.toStdString()));
41 if (theData->getBooleanAttribute("use_in_title", false))
42 myButtonTitles = aList;
44 // Widget type can be combobox or radiobuttons
45 std::string aWgtType = theData->getProperty("widget_type");
46 if ((aWgtType.length() > 0) && (aWgtType == "radiobuttons")) {
47 myButtons = new QButtonGroup(this);
48 QGroupBox* aGroupBox = new QGroupBox(aLabelText, this);
49 aLayout->addWidget(aGroupBox);
52 QLayout* aBtnLayout = 0;
53 std::string aWgtDir = theData->getProperty("buttons_dir");
54 if (aWgtDir == "horizontal")
55 aBtnLayout = new QHBoxLayout(aGroupBox);
57 aBtnLayout = new QVBoxLayout(aGroupBox);
58 ModuleBase_Tools::adjustMargins(aBtnLayout);
60 std::string aIcons = theData->getProperty("icons_list");
61 QStringList aIconList = QString(aIcons.c_str()).split(' ');
62 if (aIconList.length() == aList.length()) {
64 foreach(QString aBtnTxt, aList) {
65 QToolButton* aBtn = new QToolButton(aGroupBox);
66 aBtn->setFocusPolicy(Qt::StrongFocus);
67 aBtn->setCheckable(true);
68 aBtn->setToolTip(aBtnTxt);
70 QPixmap aIcon = ModuleBase_IconFactory::loadPixmap(aIconList.at(aId));
72 aBtn->setIconSize(aIcon.size());
74 aBtnLayout->addWidget(aBtn);
75 myButtons->addButton(aBtn, aId++);
80 foreach(QString aBtnTxt, aList) {
81 QRadioButton* aBtn = new QRadioButton(aBtnTxt, aGroupBox);
82 aBtnLayout->addWidget(aBtn);
83 myButtons->addButton(aBtn, aId++);
86 myButtons->button(0)->setChecked(true);
87 connect(myButtons, SIGNAL(buttonClicked(int)), this, SLOT(onCurrentIndexChanged(int)));
89 myLabel = new QLabel(aLabelText, this);
90 if (!aLabelIcon.isEmpty())
91 myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
92 aLayout->addWidget(myLabel);
94 std::string aToolstr = theData->widgetTooltip();
95 if (!aToolstr.empty()) {
96 myLabel->setToolTip(QString::fromStdString(aToolstr));
99 myCombo = new QComboBox(this);
100 aLayout->addWidget(myCombo, 1);
102 myCombo->addItems(aList);
104 connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
108 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
112 bool ModuleBase_WidgetChoice::storeValueCustom()
114 DataPtr aData = myFeature->data();
115 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
118 aIntAttr->setValue(myCombo->currentIndex());
120 aIntAttr->setValue(myButtons->checkedId());
121 updateObject(myFeature);
125 bool ModuleBase_WidgetChoice::restoreValueCustom()
127 DataPtr aData = myFeature->data();
128 std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
130 if (aIntAttr->value() != -1) {
132 bool isBlocked = myCombo->blockSignals(true);
133 myCombo->setCurrentIndex(aIntAttr->value());
134 myCombo->blockSignals(isBlocked);
136 bool isBlocked = myButtons->blockSignals(true);
137 myButtons->button(aIntAttr->value())->setChecked(true);
138 myButtons->blockSignals(isBlocked);
139 emit itemSelected(this, aIntAttr->value());
145 bool ModuleBase_WidgetChoice::focusTo()
148 ModuleBase_Tools::setFocus(myCombo, "ModuleBase_WidgetChoice::focusTo()");
154 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
156 QList<QWidget*> aControls;
158 aControls.append(myCombo);
160 // //foreach(QAbstractButton* aBtn, myButtons->buttons())
161 // //if (myButtons->checkedId() != -1)
162 // // aControls.append(myButtons->button(myButtons->checkedId()));
167 QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex)
170 if (myButtonTitles.length() > theIndex)
171 aTitle = myButtonTitles[theIndex];
175 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
177 emit valuesChanged();
178 // Don't transfer focus
179 // emit focusOutWidget(this);
181 emit itemSelected(this, theIndex);