Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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   QHBoxLayout* aLayout = new QHBoxLayout(this);
25   ModuleBase_Tools::adjustMargins(aLayout);
26
27   QString aLabelText = QString::fromStdString(theData->widgetLabel());
28   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
29   myLabel = new QLabel(aLabelText, this);
30   if (!aLabelIcon.isEmpty())
31     myLabel->setPixmap(QPixmap(aLabelIcon));
32   aLayout->addWidget(myLabel);
33
34   myCombo = new QComboBox(this);
35   aLayout->addWidget(myCombo, 1);
36  
37   std::string aTypes = theData->getProperty("string_list");
38   QStringList aList = QString(aTypes.c_str()).split(' ');
39   myCombo->addItems(aList);
40
41   connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
42 }
43
44 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
45 {
46 }
47   
48 bool ModuleBase_WidgetChoice::storeValueCustom() const
49 {
50   DataPtr aData = myFeature->data();
51   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
52
53   aIntAttr->setValue(myCombo->currentIndex());
54   updateObject(myFeature);
55   return true;
56 }
57
58 bool ModuleBase_WidgetChoice::restoreValue()
59 {
60   DataPtr aData = myFeature->data();
61   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
62
63   bool isBlocked = myCombo->blockSignals(true);
64   myCombo->setCurrentIndex(aIntAttr->value());
65   myCombo->blockSignals(isBlocked);
66   return true;
67 }
68
69 bool ModuleBase_WidgetChoice::focusTo()
70 {
71   myCombo->setFocus();
72   return true;
73 }
74
75 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
76 {
77   QList<QWidget*> aControls;
78   aControls.append(myCombo);
79   return aControls;
80 }
81
82 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
83 {
84   emit valuesChanged();
85   // Don't transfer focus
86   // emit focusOutWidget(this);
87 }