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