Salome HOME
Merge branch 'BR_PYTHON_PLUGIN' of newgeom:newgeom.git into Dev_0.6.1
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetChoice.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetChoice.cpp
4 // Created:     03 Sept 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "ModuleBase_WidgetChoice.h"
8 #include <ModuleBase_Tools.h>
9
10 #include <ModelAPI_AttributeInteger.h>
11 #include <ModelAPI_Data.h>
12 #include <Config_WidgetAPI.h>
13
14 #include <QWidget>
15 #include <QLayout>
16 #include <QLabel>
17 #include <QComboBox>
18
19 ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent, 
20                                                  const Config_WidgetAPI* theData, 
21                                                  const std::string& theParentId)
22     : ModuleBase_ModelWidget(theParent, theData, theParentId)
23 {
24   myContainer = new QWidget(theParent);
25   QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
26   ModuleBase_Tools::adjustMargins(aLayout);
27
28   QString aLabelText = QString::fromStdString(theData->widgetLabel());
29   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
30   myLabel = new QLabel(aLabelText, myContainer);
31   if (!aLabelIcon.isEmpty())
32     myLabel->setPixmap(QPixmap(aLabelIcon));
33   aLayout->addWidget(myLabel);
34
35   myCombo = new QComboBox(myContainer);
36   aLayout->addWidget(myCombo, 1);
37  
38   std::string aTypes = theData->getProperty("string_list");
39   QStringList aList = QString(aTypes.c_str()).split(' ');
40   myCombo->addItems(aList);
41
42   connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
43 }
44
45 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
46 {
47 }
48   
49 bool ModuleBase_WidgetChoice::storeValue() const
50 {
51   DataPtr aData = myFeature->data();
52   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
53
54   aIntAttr->setValue(myCombo->currentIndex());
55   updateObject(myFeature);
56   return true;
57 }
58
59 bool ModuleBase_WidgetChoice::restoreValue()
60 {
61   DataPtr aData = myFeature->data();
62   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
63
64   bool isBlocked = myCombo->blockSignals(true);
65   myCombo->setCurrentIndex(aIntAttr->value());
66   myCombo->blockSignals(isBlocked);
67   return true;
68 }
69
70 bool ModuleBase_WidgetChoice::focusTo()
71 {
72   myCombo->setFocus();
73   return true;
74 }
75
76 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
77 {
78   QList<QWidget*> aControls;
79   aControls.append(myCombo);
80   return aControls;
81 }
82
83 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
84 {
85   emit valuesChanged();
86   // Don't transfer focus
87   // emit focusOutWidget(this);
88 }