]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetChoice.cpp
Salome HOME
Fix coding standard issues
[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 #include "ModuleBase_ChoiceCtrl.h"
25
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_AttributeStringArray.h>
28 #include <ModelAPI_Data.h>
29 #include <Config_WidgetAPI.h>
30 #include <Config_PropManager.h>
31
32 #include <QWidget>
33 #include <QLayout>
34 #include <QLabel>
35 #include <QComboBox>
36 #include <QButtonGroup>
37 #include <QGroupBox>
38 #include <QRadioButton>
39 #include <QToolButton>
40
41 ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
42                                                  const Config_WidgetAPI* theData)
43 : ModuleBase_ModelWidget(theParent, theData)//, myCombo(0), myButtons(0)
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   if (aTypes.empty()) {
54     myStringListAttribute = theData->getProperty("string_list_attribute");
55     if (!myStringListAttribute.empty())
56       aList.clear();
57   }
58   if (theData->getBooleanAttribute("use_in_title", false))
59     myButtonTitles = aList;
60
61   bool aHasDefaultValue;
62   int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
63
64   // Widget type can be combobox or radiobuttons
65   std::string aWgtType = theData->getProperty("widget_type");
66   std::string aIcons = theData->getProperty("icons_list");
67   QStringList aIconList = QString(aIcons.c_str()).split(' ');
68
69   std::string aWgtDir = theData->getProperty("buttons_dir");
70
71   QHBoxLayout* aLayout = new QHBoxLayout(this);
72   myChoiceCtrl =  new ModuleBase_ChoiceCtrl(this, aList, aIconList,
73     (aWgtType == "radiobuttons")? ModuleBase_ChoiceCtrl::RadioButtons :
74                                   ModuleBase_ChoiceCtrl::ComboBox,
75     (aWgtDir == "horizontal")? Qt::Horizontal : Qt::Vertical);
76   myChoiceCtrl->setLabel(aLabelText);
77
78   if (!aLabelIcon.isEmpty())
79     myChoiceCtrl->setLabelIcon(aLabelIcon);
80
81   connect(myChoiceCtrl, SIGNAL(valueChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
82
83   int aCheckedId = aHasDefaultValue ? aDefaultVal : 0;
84   myChoiceCtrl->setValue(aCheckedId);
85   aLayout->addWidget(myChoiceCtrl);
86 }
87
88 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
89 {
90 }
91
92 bool ModuleBase_WidgetChoice::storeValueCustom()
93 {
94   DataPtr aData = myFeature->data();
95   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
96
97   aIntAttr->setValue(myChoiceCtrl->value());
98   updateObject(myFeature);
99   return true;
100 }
101
102 bool ModuleBase_WidgetChoice::restoreValueCustom()
103 {
104   DataPtr aData = myFeature->data();
105   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
106
107   if (aIntAttr->value() != -1) {
108     bool isBlocked = myChoiceCtrl->blockSignals(true);
109     if (!myStringListAttribute.empty()) {
110       AttributeStringArrayPtr aStrAttr = aData->stringArray(myStringListAttribute);
111       QStringList aChoiceList;
112       if (aStrAttr) {
113         for (int i = 0; i < aStrAttr->size(); i++) {
114           aChoiceList << aStrAttr->value(i).c_str();
115         }
116         myChoiceCtrl->setChoiceList(aChoiceList);
117       }
118     }
119     if (aIntAttr->isInitialized())
120       myChoiceCtrl->setValue(aIntAttr->value());
121     else {
122       bool aHasDefaultValue;
123       int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
124       myChoiceCtrl->setValue(aHasDefaultValue ? aDefaultVal : 0);
125     }
126     myChoiceCtrl->blockSignals(isBlocked);
127     emit itemSelected(this, aIntAttr->value());
128   }
129   return true;
130 }
131
132 bool ModuleBase_WidgetChoice::focusTo()
133 {
134   return myChoiceCtrl->focusTo();
135 }
136
137 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
138 {
139   return myChoiceCtrl->getControls();
140 }
141
142 QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex)
143 {
144   QString aTitle;
145   if (myButtonTitles.length() > theIndex)
146     aTitle = myButtonTitles[theIndex];
147   return aTitle;
148 }
149
150 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
151 {
152   emit valuesChanged();
153   // Don't transfer focus
154   // emit focusOutWidget(this);
155
156   emit itemSelected(this, theIndex);
157 }