Salome HOME
Merge with Dev_1.5.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   std::string aToolstr = theData->widgetTooltip();
35   if (!aToolstr.empty()) {
36     myLabel->setToolTip(QString::fromStdString(aToolstr));
37   }
38
39   myCombo = new QComboBox(this);
40   aLayout->addWidget(myCombo, 1);
41  
42   std::string aTypes = theData->getProperty("string_list");
43   QStringList aList = QString(aTypes.c_str()).split(' ');
44   myCombo->addItems(aList);
45
46   connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
47 }
48
49 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
50 {
51 }
52   
53 bool ModuleBase_WidgetChoice::storeValueCustom() const
54 {
55   DataPtr aData = myFeature->data();
56   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
57
58   aIntAttr->setValue(myCombo->currentIndex());
59   updateObject(myFeature);
60   return true;
61 }
62
63 bool ModuleBase_WidgetChoice::restoreValueCustom()
64 {
65   DataPtr aData = myFeature->data();
66   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
67
68   bool isBlocked = myCombo->blockSignals(true);
69   myCombo->setCurrentIndex(aIntAttr->value());
70   myCombo->blockSignals(isBlocked);
71   return true;
72 }
73
74 bool ModuleBase_WidgetChoice::focusTo()
75 {
76   myCombo->setFocus();
77   return true;
78 }
79
80 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
81 {
82   QList<QWidget*> aControls;
83   aControls.append(myCombo);
84   return aControls;
85 }
86
87 void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex)
88 {
89   emit valuesChanged();
90   // Don't transfer focus
91   // emit focusOutWidget(this);
92 }