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