Salome HOME
Correction of Boolean crash
[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 = 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(' ');
36
37   if (theData->getBooleanAttribute("use_in_title", false))
38     myButtonTitles = QString(aTypes.c_str()).split(" ");
39
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);
46
47
48     QLayout* aBtnLayout = 0;
49     std::string aWgtDir = theData->getProperty("buttons_dir");
50     if (aWgtDir == "horizontal")
51       aBtnLayout = new QHBoxLayout(aGroupBox);
52     else 
53       aBtnLayout = new QVBoxLayout(aGroupBox);
54     ModuleBase_Tools::adjustMargins(aBtnLayout);
55
56     std::string aIcons = theData->getProperty("icons_list");
57     QStringList aIconList = QString(aIcons.c_str()).split(' ');
58     if (aIconList.length() == aList.length()) {
59       int aId = 0;
60       foreach(QString aBtnTxt, aList) {
61         QToolButton* aBtn = new QToolButton(aGroupBox);
62         aBtn->setFocusPolicy(Qt::StrongFocus);
63         aBtn->setCheckable(true);
64         aBtn->setToolTip(aBtnTxt);
65
66         QPixmap aIcon = ModuleBase_IconFactory::loadPixmap(aIconList.at(aId));
67         aBtn->setIcon(aIcon);
68         aBtn->setIconSize(aIcon.size());
69         
70         aBtnLayout->addWidget(aBtn);
71         myButtons->addButton(aBtn, aId++);
72       }
73
74     } else {
75       int aId = 0;
76       foreach(QString aBtnTxt, aList) {
77         QRadioButton* aBtn = new QRadioButton(aBtnTxt, aGroupBox);
78         aBtnLayout->addWidget(aBtn);
79         myButtons->addButton(aBtn, aId++);
80       }
81     }
82     myButtons->button(0)->setChecked(true);
83     connect(myButtons, SIGNAL(buttonClicked(int)), this, SLOT(onCurrentIndexChanged(int)));
84   } else {
85     myLabel = new QLabel(aLabelText, this);
86     if (!aLabelIcon.isEmpty())
87       myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
88     aLayout->addWidget(myLabel);
89
90     std::string aToolstr = theData->widgetTooltip();
91     if (!aToolstr.empty()) {
92       myLabel->setToolTip(QString::fromStdString(aToolstr));
93     }
94
95     myCombo = new QComboBox(this);
96     aLayout->addWidget(myCombo, 1);
97  
98     myCombo->addItems(aList);
99
100     connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
101   }
102 }
103
104 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
105 {
106 }
107   
108 bool ModuleBase_WidgetChoice::storeValueCustom()
109 {
110   DataPtr aData = myFeature->data();
111   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
112
113   if (myCombo)
114     aIntAttr->setValue(myCombo->currentIndex());
115   else
116     aIntAttr->setValue(myButtons->checkedId());
117   updateObject(myFeature);
118   return true;
119 }
120
121 bool ModuleBase_WidgetChoice::restoreValueCustom()
122 {
123   DataPtr aData = myFeature->data();
124   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
125
126   if (aIntAttr->value() != -1) {
127     if (myCombo) {
128       bool isBlocked = myCombo->blockSignals(true);
129       myCombo->setCurrentIndex(aIntAttr->value());
130       myCombo->blockSignals(isBlocked);
131     } else {
132       bool isBlocked = myButtons->blockSignals(true);
133       myButtons->button(aIntAttr->value())->setChecked(true);
134       myButtons->blockSignals(isBlocked);
135       emit itemSelected(this, aIntAttr->value());
136     }
137   }
138   return true;
139 }
140
141 bool ModuleBase_WidgetChoice::focusTo()
142 {
143   if (myCombo)
144     ModuleBase_Tools::setFocus(myCombo, "ModuleBase_WidgetChoice::focusTo()");
145   else
146     return false;
147   return true;
148 }
149
150 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
151 {
152   QList<QWidget*> aControls;
153   if (myCombo)
154     aControls.append(myCombo);
155   //else {
156   //  //foreach(QAbstractButton* aBtn, myButtons->buttons())
157   //  //if (myButtons->checkedId() != -1)
158   //  //  aControls.append(myButtons->button(myButtons->checkedId()));
159   //}
160   return aControls;
161 }
162
163 QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex)
164 {
165   QString aTitle;
166   if (myButtonTitles.length() > theIndex)
167     aTitle = tr(myButtonTitles[theIndex].toStdString().c_str());
168   return aTitle;
169 }
170
171 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
172 {
173   emit valuesChanged();
174   // Don't transfer focus
175   // emit focusOutWidget(this);
176
177   emit itemSelected(this, theIndex);
178 }