Salome HOME
Improve the correcting orientation in the Filling feature.
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetChoice.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "ModuleBase_WidgetChoice.h"
22 #include "ModuleBase_Tools.h"
23 #include "ModuleBase_IconFactory.h"
24
25 #include <ModelAPI_AttributeInteger.h>
26 #include <ModelAPI_AttributeStringArray.h>
27 #include <ModelAPI_Data.h>
28 #include <Config_WidgetAPI.h>
29 #include <Config_PropManager.h>
30
31 #include <QWidget>
32 #include <QLayout>
33 #include <QLabel>
34 #include <QComboBox>
35 #include <QButtonGroup>
36 #include <QGroupBox>
37 #include <QRadioButton>
38 #include <QToolButton>
39
40 ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
41                                                  const Config_WidgetAPI* theData)
42 : ModuleBase_ModelWidget(theParent, theData), myCombo(0), myButtons(0)
43 {
44   QHBoxLayout* aLayout = new QHBoxLayout(this);
45   ModuleBase_Tools::adjustMargins(aLayout);
46
47   QString aLabelText = translate(theData->widgetLabel());
48   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
49   std::string aTypes = theData->getProperty("string_list");
50   QStringList aList;
51
52   foreach(QString aType, QString(aTypes.c_str()).split(' ')) {
53     aList.append(translate(aType.toStdString()));
54   }
55   if (aTypes.empty()) {
56     myStringListAttribute = theData->getProperty("string_list_attribute");
57     if (!myStringListAttribute.empty())
58       aList.clear();
59   }
60   if (theData->getBooleanAttribute("use_in_title", false))
61     myButtonTitles = aList;
62
63   bool aHasDefaultValue;
64   int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
65   // Widget type can be combobox or radiobuttons
66   std::string aWgtType = theData->getProperty("widget_type");
67   if ((aWgtType.length() > 0) && (aWgtType == "radiobuttons")) {
68     myButtons = new QButtonGroup(this);
69     QGroupBox* aGroupBox = new QGroupBox(aLabelText, this);
70     aLayout->addWidget(aGroupBox);
71
72
73     QLayout* aBtnLayout = 0;
74     std::string aWgtDir = theData->getProperty("buttons_dir");
75     if (aWgtDir == "horizontal")
76       aBtnLayout = new QHBoxLayout(aGroupBox);
77     else
78       aBtnLayout = new QVBoxLayout(aGroupBox);
79     ModuleBase_Tools::adjustMargins(aBtnLayout);
80
81     std::string aIcons = theData->getProperty("icons_list");
82     QStringList aIconList = QString(aIcons.c_str()).split(' ');
83     if (aIconList.length() == aList.length()) {
84       int aId = 0;
85       foreach(QString aBtnTxt, aList) {
86         QToolButton* aBtn = new QToolButton(aGroupBox);
87         aBtn->setFocusPolicy(Qt::StrongFocus);
88         aBtn->setCheckable(true);
89         aBtn->setToolTip(aBtnTxt);
90
91         QPixmap aIcon = ModuleBase_IconFactory::loadPixmap(aIconList.at(aId));
92         aBtn->setIcon(aIcon);
93         aBtn->setIconSize(aIcon.size());
94
95         aBtnLayout->addWidget(aBtn);
96         myButtons->addButton(aBtn, aId++);
97       }
98
99     } else {
100       int aId = 0;
101       foreach(QString aBtnTxt, aList) {
102         QRadioButton* aBtn = new QRadioButton(aBtnTxt, aGroupBox);
103         aBtnLayout->addWidget(aBtn);
104         myButtons->addButton(aBtn, aId++);
105       }
106     }
107     int aCheckedId = aHasDefaultValue ? aDefaultVal : 0;
108     myButtons->button(aDefaultVal)->setChecked(true);
109     connect(myButtons, SIGNAL(buttonClicked(int)), this, SLOT(onCurrentIndexChanged(int)));
110   } else {
111     myLabel = new QLabel(aLabelText, this);
112     if (!aLabelIcon.isEmpty())
113       myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
114     aLayout->addWidget(myLabel);
115
116     std::string aToolstr = theData->widgetTooltip();
117     if (!aToolstr.empty()) {
118       myLabel->setToolTip(QString::fromStdString(aToolstr));
119     }
120
121     myCombo = new QComboBox(this);
122     aLayout->addWidget(myCombo, 1);
123
124     myCombo->addItems(aList);
125
126     if (aHasDefaultValue && aDefaultVal < aList.size())
127       myCombo->setCurrentIndex(aDefaultVal);
128
129     connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
130   }
131 }
132
133 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
134 {
135 }
136
137 bool ModuleBase_WidgetChoice::storeValueCustom()
138 {
139   DataPtr aData = myFeature->data();
140   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
141
142   if (myCombo)
143     aIntAttr->setValue(myCombo->currentIndex());
144   else
145     aIntAttr->setValue(myButtons->checkedId());
146   updateObject(myFeature);
147   return true;
148 }
149
150 bool ModuleBase_WidgetChoice::restoreValueCustom()
151 {
152   DataPtr aData = myFeature->data();
153   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
154
155   if (aIntAttr->value() != -1) {
156     if (myCombo) {
157       bool isBlocked = myCombo->blockSignals(true);
158       if (myCombo->count() == 0 && !myStringListAttribute.empty()) {
159         AttributeStringArrayPtr aStrAttr = aData->stringArray(myStringListAttribute);
160         if (aStrAttr) {
161           for (int i = 0; i < aStrAttr->size(); i++) {
162             myCombo->insertItem(i, aStrAttr->value(i).c_str());
163           }
164         }
165       }
166       myCombo->setCurrentIndex(aIntAttr->value());
167       myCombo->blockSignals(isBlocked);
168     } else {
169       bool isBlocked = myButtons->blockSignals(true);
170       if (aIntAttr->isInitialized())
171         myButtons->button(aIntAttr->value())->setChecked(true);
172       else {
173         bool aHasDefaultValue;
174         int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
175         myButtons->button(aHasDefaultValue ? aDefaultVal : 0)->setChecked(true);
176       }
177       myButtons->blockSignals(isBlocked);
178       emit itemSelected(this, aIntAttr->value());
179     }
180   }
181   return true;
182 }
183
184 bool ModuleBase_WidgetChoice::focusTo()
185 {
186   if (myCombo)
187     ModuleBase_Tools::setFocus(myCombo, "ModuleBase_WidgetChoice::focusTo()");
188   else
189     return false;
190   return true;
191 }
192
193 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
194 {
195   QList<QWidget*> aControls;
196   if (myCombo)
197     aControls.append(myCombo);
198   //else {
199   //  //foreach(QAbstractButton* aBtn, myButtons->buttons())
200   //  //if (myButtons->checkedId() != -1)
201   //  //  aControls.append(myButtons->button(myButtons->checkedId()));
202   //}
203   return aControls;
204 }
205
206 QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex)
207 {
208   QString aTitle;
209   if (myButtonTitles.length() > theIndex)
210     aTitle = myButtonTitles[theIndex];
211   return aTitle;
212 }
213
214 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
215 {
216   emit valuesChanged();
217   // Don't transfer focus
218   // emit focusOutWidget(this);
219
220   emit itemSelected(this, theIndex);
221 }