]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetFeature.cpp
Salome HOME
e699a72de437ad568513e7e7fb2f0652fb641f07
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFeature.cpp
1 // File:        ModuleBase_WidgetFeature.cpp
2 // Created:     25 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <ModuleBase_WidgetFeature.h>
6
7 #include <ModuleBase_WidgetValueFeature.h>
8 #include <ModuleBase_WidgetValue.h>
9 #include <ModuleBase_ResultValidators.h>
10
11 #include <Config_Keywords.h>
12 #include <Config_WidgetAPI.h>
13
14 #include <Events_Loop.h>
15 #include <ModelAPI_Events.h>
16
17 #include <ModelAPI_Feature.h>
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_Object.h>
20 #include <ModelAPI_AttributeRefAttr.h>
21 #include <ModelAPI_Validator.h>
22
23 #include <QWidget>
24 #include <QLineEdit>
25 #include <QHBoxLayout>
26 #include <QLabel>
27
28 ModuleBase_WidgetFeature::ModuleBase_WidgetFeature(QWidget* theParent,
29                                                    const Config_WidgetAPI* theData, 
30                                                    const std::string& theParentId)
31 : ModuleBase_ModelWidget(theParent, theData, theParentId)
32 {
33   //QString aKinds = QString::fromStdString(theData->getProperty(FEATURE_KEYSEQUENCE));
34   //myObjectKinds = aKinds.split(" ");
35   //theData->
36
37   myContainer = new QWidget(theParent);
38   QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
39   aControlLay->setContentsMargins(0, 0, 0, 0);
40
41   QString aLabelText = QString::fromStdString(theData->widgetLabel());
42   myLabel = new QLabel(aLabelText, myContainer);
43   aControlLay->addWidget(myLabel);
44
45   myEditor = new QLineEdit(myContainer);
46   QString anObjName = QString::fromStdString(attributeID());
47   myEditor->setObjectName(anObjName);
48   myEditor->setReadOnly(true);
49   aControlLay->addWidget(myEditor);
50
51   QString aTTip = QString::fromStdString(theData->widgetTooltip());
52   myEditor->setToolTip(aTTip);
53
54   aControlLay->addWidget(myEditor);
55   aControlLay->setStretch(1, 1);
56 }
57
58 ModuleBase_WidgetFeature::~ModuleBase_WidgetFeature()
59 {
60 }
61
62 bool ModuleBase_WidgetFeature::setValue(ModuleBase_WidgetValue* theValue)
63 {
64   bool isDone = false;
65
66   if (theValue) {
67     ModuleBase_WidgetValueFeature* aFeatureValue = 
68                          dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
69     if (aFeatureValue)
70       isDone = setObject(aFeatureValue->object());
71   }
72   return isDone;
73 }
74
75 bool ModuleBase_WidgetFeature::setObject(const ObjectPtr& theObject)
76 {
77   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
78   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
79   std::list<ModelAPI_Validator*> aValidators;
80   std::list<std::list<std::string> > anArguments;
81   aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
82   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
83   bool isValid = true;
84   for(; aValidator != aValidators.end(); aValidator++) {
85     const ModuleBase_ResultValidator* aResValidator = 
86       dynamic_cast<const ModuleBase_ResultValidator*>(*aValidator);
87     if (aResValidator) {
88       isValid = false;
89       if (aResValidator->isValid(theObject)) {
90         isValid = true;
91         break;
92       }
93     }
94   }
95   if (!isValid)
96     return false;
97
98   myObject = theObject;
99   myEditor->setText(theObject ? theObject->data()->name().c_str() : "");
100   emit valuesChanged();
101   return true;
102 }
103
104 bool ModuleBase_WidgetFeature::storeValue(ObjectPtr theObject) const
105 {
106   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
107   if (!aFeature)
108     return false;
109   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
110   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
111           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
112
113   ModuleBase_WidgetFeature* that = (ModuleBase_WidgetFeature*) this;
114   aRef->setObject(myObject);
115   aFeature->execute();
116   updateObject(theObject);
117   return true;
118 }
119
120 bool ModuleBase_WidgetFeature::restoreValue(ObjectPtr theObject)
121 {
122   boost::shared_ptr<ModelAPI_Data> aData = theObject->data();
123   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
124           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
125
126   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aRef->object());
127   if (aFeature) {
128     myObject = aFeature;
129     myEditor->setText(myObject ? myObject->data()->name().c_str() : "");
130     return true;
131   }
132   return false;
133 }
134
135 QWidget* ModuleBase_WidgetFeature::getControl() const
136 {
137   return myContainer;
138 }
139
140 QList<QWidget*> ModuleBase_WidgetFeature::getControls() const
141 {
142   QList<QWidget*> aList;
143   aList.append(myLabel);
144   aList.append(myEditor);
145   return aList;
146 }