Salome HOME
b8ff4af2b9b7f2ad53b4d86398c21b0498b88c30
[modules/shaper.git] / src / SamplePanelPlugin / SamplePanelPlugin_Panel.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 <SamplePanelPlugin_Panel.h>
22 #include <SamplePanelPlugin_Feature.h>
23
24 #include <ModelAPI_AttributeInteger.h>
25 #include <ModelAPI_Events.h>
26
27 #include <Events_Loop.h>
28
29 #include <QGridLayout>
30 #include <QLabel>
31 #include <QComboBox>
32
33 SamplePanelPlugin_Panel::SamplePanelPlugin_Panel(QWidget* theParent)
34 : QWidget(theParent), myDefaultValue(0)
35 {
36   QGridLayout* aLayout = new QGridLayout(this);
37   aLayout->addWidget(new QLabel(tr("Values:")), 0, 0);
38
39   myComboBox = new QComboBox(this);
40   myComboBox->addItem("Value_1");
41   myComboBox->addItem("Value_2");
42   myComboBox->addItem("Value_3");
43
44   connect(myComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
45   aLayout->addWidget(myComboBox, 0, 1);
46 }
47
48 void SamplePanelPlugin_Panel::setFeature(const FeaturePtr& theFeature)
49 {
50   myFeature = theFeature;
51
52   AttributePtr anAttribute = myFeature->attribute(SamplePanelPlugin_Feature::VALUE_ID());
53   AttributeIntegerPtr aValueAttribute =
54                         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(anAttribute);
55   /// the attribute should be filled with some value, because Apply button of the Property
56   /// panel is enabled only if all attributes of the feature are initialized
57   /// It is possible to make it in the initializeAttribute method of the feature after
58   /// attribute creation
59   if (!aValueAttribute->isInitialized()) {
60     aValueAttribute->setValue(myDefaultValue);
61     /// to update error state of the feature: previous value was that this attribute is not
62     /// initialized yet. Apply of Property panel was disabled.
63     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
64   }
65
66   int aValueId = aValueAttribute->value();
67   myComboBox->setCurrentIndex(aValueId);
68 }
69
70 void SamplePanelPlugin_Panel::onCurrentIndexChanged(int theIndex)
71 {
72   if (!myFeature.get())
73     return;
74
75   AttributePtr anAttribute = myFeature->attribute(SamplePanelPlugin_Feature::VALUE_ID());
76   AttributeIntegerPtr aValueAttribute =
77                         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(anAttribute);
78   aValueAttribute->setValue(theIndex);
79   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
80 }