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