Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetChoice.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetChoice.cpp
4 // Created:     03 Sept 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "ModuleBase_WidgetChoice.h"
8 #include "ModuleBase_Tools.h"
9 #include "ModuleBase_IconFactory.h"
10
11 #include <ModelAPI_AttributeInteger.h>
12 #include <ModelAPI_Data.h>
13 #include <Config_WidgetAPI.h>
14
15 #include <QWidget>
16 #include <QLayout>
17 #include <QLabel>
18 #include <QComboBox>
19 #include <QButtonGroup>
20 #include <QGroupBox>
21 #include <QRadioButton>
22 #include <QToolButton>
23
24
25 ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
26                                                  const Config_WidgetAPI* theData)
27 : ModuleBase_ModelWidget(theParent, theData), myCombo(0), myButtons(0)
28 {
29   QHBoxLayout* aLayout = new QHBoxLayout(this);
30   ModuleBase_Tools::adjustMargins(aLayout);
31
32   QString aLabelText = translate(theData->widgetLabel());
33   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
34   std::string aTypes = theData->getProperty("string_list");
35   QStringList aList;
36
37   foreach(QString aType, QString(aTypes.c_str()).split(' ')) {
38     aList.append(translate(aType.toStdString()));
39   }
40
41   if (theData->getBooleanAttribute("use_in_title", false))
42     myButtonTitles = aList;
43
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);
50
51
52     QLayout* aBtnLayout = 0;
53     std::string aWgtDir = theData->getProperty("buttons_dir");
54     if (aWgtDir == "horizontal")
55       aBtnLayout = new QHBoxLayout(aGroupBox);
56     else
57       aBtnLayout = new QVBoxLayout(aGroupBox);
58     ModuleBase_Tools::adjustMargins(aBtnLayout);
59
60     std::string aIcons = theData->getProperty("icons_list");
61     QStringList aIconList = QString(aIcons.c_str()).split(' ');
62     if (aIconList.length() == aList.length()) {
63       int aId = 0;
64       foreach(QString aBtnTxt, aList) {
65         QToolButton* aBtn = new QToolButton(aGroupBox);
66         aBtn->setFocusPolicy(Qt::StrongFocus);
67         aBtn->setCheckable(true);
68         aBtn->setToolTip(aBtnTxt);
69
70         QPixmap aIcon = ModuleBase_IconFactory::loadPixmap(aIconList.at(aId));
71         aBtn->setIcon(aIcon);
72         aBtn->setIconSize(aIcon.size());
73
74         aBtnLayout->addWidget(aBtn);
75         myButtons->addButton(aBtn, aId++);
76       }
77
78     } else {
79       int aId = 0;
80       foreach(QString aBtnTxt, aList) {
81         QRadioButton* aBtn = new QRadioButton(aBtnTxt, aGroupBox);
82         aBtnLayout->addWidget(aBtn);
83         myButtons->addButton(aBtn, aId++);
84       }
85     }
86     myButtons->button(0)->setChecked(true);
87     connect(myButtons, SIGNAL(buttonClicked(int)), this, SLOT(onCurrentIndexChanged(int)));
88   } else {
89     myLabel = new QLabel(aLabelText, this);
90     if (!aLabelIcon.isEmpty())
91       myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
92     aLayout->addWidget(myLabel);
93
94     std::string aToolstr = theData->widgetTooltip();
95     if (!aToolstr.empty()) {
96       myLabel->setToolTip(QString::fromStdString(aToolstr));
97     }
98
99     myCombo = new QComboBox(this);
100     aLayout->addWidget(myCombo, 1);
101
102     myCombo->addItems(aList);
103
104     connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
105   }
106 }
107
108 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
109 {
110 }
111
112 bool ModuleBase_WidgetChoice::storeValueCustom()
113 {
114   DataPtr aData = myFeature->data();
115   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
116
117   if (myCombo)
118     aIntAttr->setValue(myCombo->currentIndex());
119   else
120     aIntAttr->setValue(myButtons->checkedId());
121   updateObject(myFeature);
122   return true;
123 }
124
125 bool ModuleBase_WidgetChoice::restoreValueCustom()
126 {
127   DataPtr aData = myFeature->data();
128   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
129
130   if (aIntAttr->value() != -1) {
131     if (myCombo) {
132       bool isBlocked = myCombo->blockSignals(true);
133       myCombo->setCurrentIndex(aIntAttr->value());
134       myCombo->blockSignals(isBlocked);
135     } else {
136       bool isBlocked = myButtons->blockSignals(true);
137       myButtons->button(aIntAttr->value())->setChecked(true);
138       myButtons->blockSignals(isBlocked);
139       emit itemSelected(this, aIntAttr->value());
140     }
141   }
142   return true;
143 }
144
145 bool ModuleBase_WidgetChoice::focusTo()
146 {
147   if (myCombo)
148     ModuleBase_Tools::setFocus(myCombo, "ModuleBase_WidgetChoice::focusTo()");
149   else
150     return false;
151   return true;
152 }
153
154 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
155 {
156   QList<QWidget*> aControls;
157   if (myCombo)
158     aControls.append(myCombo);
159   //else {
160   //  //foreach(QAbstractButton* aBtn, myButtons->buttons())
161   //  //if (myButtons->checkedId() != -1)
162   //  //  aControls.append(myButtons->button(myButtons->checkedId()));
163   //}
164   return aControls;
165 }
166
167 QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex)
168 {
169   QString aTitle;
170   if (myButtonTitles.length() > theIndex)
171     aTitle = myButtonTitles[theIndex];
172   return aTitle;
173 }
174
175 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
176 {
177   emit valuesChanged();
178   // Don't transfer focus
179   // emit focusOutWidget(this);
180
181   emit itemSelected(this, theIndex);
182 }