Salome HOME
#2205 Ability to customize the arrows and texts of dimensions: GUI correction to...
[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_Data.h>
27 #include <Config_WidgetAPI.h>
28
29 #include <QWidget>
30 #include <QLayout>
31 #include <QLabel>
32 #include <QComboBox>
33 #include <QButtonGroup>
34 #include <QGroupBox>
35 #include <QRadioButton>
36 #include <QToolButton>
37
38
39 ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
40                                                  const Config_WidgetAPI* theData)
41 : ModuleBase_ModelWidget(theParent, theData), myCombo(0), myButtons(0)
42 {
43   QHBoxLayout* aLayout = new QHBoxLayout(this);
44   ModuleBase_Tools::adjustMargins(aLayout);
45
46   QString aLabelText = translate(theData->widgetLabel());
47   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
48   std::string aTypes = theData->getProperty("string_list");
49   QStringList aList;
50
51   foreach(QString aType, QString(aTypes.c_str()).split(' ')) {
52     aList.append(translate(aType.toStdString()));
53   }
54
55   if (theData->getBooleanAttribute("use_in_title", false))
56     myButtonTitles = aList;
57
58   bool aHasDefaultValue;
59   int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
60   // Widget type can be combobox or radiobuttons
61   std::string aWgtType = theData->getProperty("widget_type");
62   if ((aWgtType.length() > 0) && (aWgtType == "radiobuttons")) {
63     myButtons = new QButtonGroup(this);
64     QGroupBox* aGroupBox = new QGroupBox(aLabelText, this);
65     aLayout->addWidget(aGroupBox);
66
67
68     QLayout* aBtnLayout = 0;
69     std::string aWgtDir = theData->getProperty("buttons_dir");
70     if (aWgtDir == "horizontal")
71       aBtnLayout = new QHBoxLayout(aGroupBox);
72     else
73       aBtnLayout = new QVBoxLayout(aGroupBox);
74     ModuleBase_Tools::adjustMargins(aBtnLayout);
75
76     std::string aIcons = theData->getProperty("icons_list");
77     QStringList aIconList = QString(aIcons.c_str()).split(' ');
78     if (aIconList.length() == aList.length()) {
79       int aId = 0;
80       foreach(QString aBtnTxt, aList) {
81         QToolButton* aBtn = new QToolButton(aGroupBox);
82         aBtn->setFocusPolicy(Qt::StrongFocus);
83         aBtn->setCheckable(true);
84         aBtn->setToolTip(aBtnTxt);
85
86         QPixmap aIcon = ModuleBase_IconFactory::loadPixmap(aIconList.at(aId));
87         aBtn->setIcon(aIcon);
88         aBtn->setIconSize(aIcon.size());
89
90         aBtnLayout->addWidget(aBtn);
91         myButtons->addButton(aBtn, aId++);
92       }
93
94     } else {
95       int aId = 0;
96       foreach(QString aBtnTxt, aList) {
97         QRadioButton* aBtn = new QRadioButton(aBtnTxt, aGroupBox);
98         aBtnLayout->addWidget(aBtn);
99         myButtons->addButton(aBtn, aId++);
100       }
101     }
102     int aCheckedId = aHasDefaultValue ? aDefaultVal : 0;
103     myButtons->button(aDefaultVal)->setChecked(true);
104     connect(myButtons, SIGNAL(buttonClicked(int)), this, SLOT(onCurrentIndexChanged(int)));
105   } else {
106     myLabel = new QLabel(aLabelText, this);
107     if (!aLabelIcon.isEmpty())
108       myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
109     aLayout->addWidget(myLabel);
110
111     std::string aToolstr = theData->widgetTooltip();
112     if (!aToolstr.empty()) {
113       myLabel->setToolTip(QString::fromStdString(aToolstr));
114     }
115
116     myCombo = new QComboBox(this);
117     aLayout->addWidget(myCombo, 1);
118
119     myCombo->addItems(aList);
120
121     if (aHasDefaultValue && aDefaultVal < aList.size())
122       myCombo->setCurrentIndex(aDefaultVal);
123
124     connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
125   }
126 }
127
128 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
129 {
130 }
131
132 bool ModuleBase_WidgetChoice::storeValueCustom()
133 {
134   DataPtr aData = myFeature->data();
135   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
136
137   if (myCombo)
138     aIntAttr->setValue(myCombo->currentIndex());
139   else
140     aIntAttr->setValue(myButtons->checkedId());
141   updateObject(myFeature);
142   return true;
143 }
144
145 bool ModuleBase_WidgetChoice::restoreValueCustom()
146 {
147   DataPtr aData = myFeature->data();
148   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
149
150   if (aIntAttr->value() != -1) {
151     if (myCombo) {
152       bool isBlocked = myCombo->blockSignals(true);
153       myCombo->setCurrentIndex(aIntAttr->value());
154       myCombo->blockSignals(isBlocked);
155     } else {
156       bool isBlocked = myButtons->blockSignals(true);
157       if (aIntAttr->isInitialized())
158         myButtons->button(aIntAttr->value())->setChecked(true);
159       else {
160         bool aHasDefaultValue;
161         int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
162         myButtons->button(aHasDefaultValue ? aDefaultVal : 0)->setChecked(true);
163       }
164       myButtons->blockSignals(isBlocked);
165       emit itemSelected(this, aIntAttr->value());
166     }
167   }
168   return true;
169 }
170
171 bool ModuleBase_WidgetChoice::focusTo()
172 {
173   if (myCombo)
174     ModuleBase_Tools::setFocus(myCombo, "ModuleBase_WidgetChoice::focusTo()");
175   else
176     return false;
177   return true;
178 }
179
180 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
181 {
182   QList<QWidget*> aControls;
183   if (myCombo)
184     aControls.append(myCombo);
185   //else {
186   //  //foreach(QAbstractButton* aBtn, myButtons->buttons())
187   //  //if (myButtons->checkedId() != -1)
188   //  //  aControls.append(myButtons->button(myButtons->checkedId()));
189   //}
190   return aControls;
191 }
192
193 QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex)
194 {
195   QString aTitle;
196   if (myButtonTitles.length() > theIndex)
197     aTitle = myButtonTitles[theIndex];
198   return aTitle;
199 }
200
201 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
202 {
203   emit valuesChanged();
204   // Don't transfer focus
205   // emit focusOutWidget(this);
206
207   emit itemSelected(this, theIndex);
208 }