Salome HOME
Issue #1368: Creation of a Qt panel. Persistent mechanism.
[modules/shaper.git] / src / SamplePanelPlugin / SamplePanelPlugin_Panel.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        SamplePanelPlugin_PageGroupBox.h
4 // Created:     29 Mar 2015
5 // Author:      Natalia ERMOLAEVA
6
7 #include <SamplePanelPlugin_Panel.h>
8 #include <SamplePanelPlugin_Feature.h>
9
10 #include <ModelAPI_AttributeInteger.h>
11 #include <ModelAPI_Events.h>
12
13 #include <Events_Loop.h>
14
15 #include <QGridLayout>
16 #include <QLabel>
17 #include <QComboBox>
18
19 SamplePanelPlugin_Panel::SamplePanelPlugin_Panel(QWidget* theParent)
20 : QWidget(theParent), myDefaultValue(0)
21 {
22   QGridLayout* aLayout = new QGridLayout(this);
23   aLayout->addWidget(new QLabel("Values:"), 0, 0);
24
25   myComboBox = new QComboBox(this);
26   myComboBox->addItem("Value_1");
27   myComboBox->addItem("Value_2");
28   myComboBox->addItem("Value_3");
29
30   connect(myComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
31   aLayout->addWidget(myComboBox, 0, 1);
32 }
33
34 void SamplePanelPlugin_Panel::setFeature(const FeaturePtr& theFeature)
35 {
36   myFeature = theFeature;
37
38   AttributePtr anAttribute = myFeature->attribute(SamplePanelPlugin_Feature::VALUE_ID());
39   AttributeIntegerPtr aValueAttribute =
40                         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(anAttribute);
41   /// the attribute should be filled with some value, because Apply button of the Property
42   /// panel is enabled only if all attributes of the feature are initialized
43   /// It is possible to make it in the initializeAttribute method of the feature after
44   /// attribute creation
45   if (!aValueAttribute->isInitialized()) {
46     aValueAttribute->setValue(myDefaultValue);
47     /// to update error state of the feature: previous value was that this attribute is not
48     /// initialized yet. Apply of Property panel was disabled.
49     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
50   }
51
52   int aValueId = aValueAttribute->value();
53   myComboBox->setCurrentIndex(aValueId);
54 }
55
56 void SamplePanelPlugin_Panel::onCurrentIndexChanged(int theIndex)
57 {
58   if (!myFeature.get())
59     return;
60
61   AttributePtr anAttribute = myFeature->attribute(SamplePanelPlugin_Feature::VALUE_ID());
62   AttributeIntegerPtr aValueAttribute =
63                         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(anAttribute);
64   aValueAttribute->setValue(theIndex);
65   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
66 }