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