Salome HOME
Issue #144 If an operation has valid feature after initialization by preselection...
[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
10 #include <Config_Keywords.h>
11 #include <Config_WidgetAPI.h>
12
13 #include <Events_Loop.h>
14
15 #include <ModelAPI_Events.h>
16 #include <ModelAPI_Feature.h>
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Object.h>
19 #include <ModelAPI_AttributeRefAttr.h>
20 #include <ModelAPI_Validator.h>
21 #include <ModelAPI_ResultValidator.h>
22 #include <ModelAPI_RefAttrValidator.h>
23
24 #include <QWidget>
25 #include <QLineEdit>
26 #include <QHBoxLayout>
27 #include <QLabel>
28
29 ModuleBase_WidgetFeature::ModuleBase_WidgetFeature(QWidget* theParent,
30                                                    const Config_WidgetAPI* theData,
31                                                    const std::string& theParentId)
32     : ModuleBase_ModelWidget(theParent, theData, theParentId)
33 {
34   myContainer = new QWidget(theParent);
35   QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
36   aControlLay->setContentsMargins(0, 0, 0, 0);
37
38   QString aLabelText = QString::fromStdString(theData->widgetLabel());
39   myLabel = new QLabel(aLabelText, myContainer);
40   aControlLay->addWidget(myLabel);
41
42   myEditor = new QLineEdit(myContainer);
43   QString anObjName = QString::fromStdString(attributeID());
44   myEditor->setObjectName(anObjName);
45   myEditor->setReadOnly(true);
46   aControlLay->addWidget(myEditor);
47
48   QString aTTip = QString::fromStdString(theData->widgetTooltip());
49   myEditor->setToolTip(aTTip);
50   aControlLay->addWidget(myEditor);
51   aControlLay->setStretch(1, 1);
52 }
53
54 ModuleBase_WidgetFeature::~ModuleBase_WidgetFeature()
55 {
56 }
57
58 bool ModuleBase_WidgetFeature::setValue(ModuleBase_WidgetValue* theValue)
59 {
60   bool isDone = false;
61
62   if (theValue) {
63     ModuleBase_WidgetValueFeature* aFeatureValue =
64         dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
65     if (aFeatureValue)
66       isDone = setObject(aFeatureValue->object());
67   }
68   return isDone;
69 }
70
71 bool ModuleBase_WidgetFeature::setObject(const ObjectPtr& theObject, bool theSendEvent)
72 {
73   SessionPtr aMgr = ModelAPI_Session::get();
74   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
75   std::list<ModelAPI_Validator*> aValidators;
76   std::list<std::list<std::string> > anArguments;
77   aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
78
79   // Check the type of selected object
80   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
81   bool isValid = true;
82   for (; aValidator != aValidators.end(); aValidator++) {
83     const ModelAPI_ResultValidator* aResValidator =
84         dynamic_cast<const ModelAPI_ResultValidator*>(*aValidator);
85     if (aResValidator) {
86       isValid = false;
87       if (aResValidator->isValid(theObject)) {
88         isValid = true;
89         break;
90       }
91     }
92   }
93   if (!isValid)
94     return false;
95
96   // Check the acceptability of the object as attribute
97   aValidator = aValidators.begin();
98   std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
99   for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
100     const ModelAPI_RefAttrValidator* aAttrValidator =
101         dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
102     if (aAttrValidator) {
103       if (!aAttrValidator->isValid(myFeature, *aArgs, theObject)) {
104         return false;
105       }
106     }
107   }
108
109   myObject = theObject;
110   myEditor->setText(theObject ? theObject->data()->name().c_str() : "");
111   if (theSendEvent)
112     emit valuesChanged();
113   return true;
114 }
115
116 bool ModuleBase_WidgetFeature::storeValue() const
117 {
118   //FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
119   if (!myObject)
120     return false;
121
122   boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
123   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::dynamic_pointer_cast<
124       ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
125
126   ModuleBase_WidgetFeature* that = (ModuleBase_WidgetFeature*) this;
127   aRef->setObject(myObject);
128   myFeature->execute();
129   updateObject(myFeature);
130   return true;
131 }
132
133 bool ModuleBase_WidgetFeature::restoreValue()
134 {
135   boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
136   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::dynamic_pointer_cast<
137       ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
138
139   ObjectPtr anObjPtr = aRef->object();
140   if (anObjPtr) {
141     myObject = anObjPtr;
142     myEditor->setText(myObject ? myObject->data()->name().c_str() : "");
143     return true;
144   }
145   return false;
146 }
147
148 QWidget* ModuleBase_WidgetFeature::getControl() const
149 {
150   return myContainer;
151 }
152
153 QList<QWidget*> ModuleBase_WidgetFeature::getControls() const
154 {
155   QList<QWidget*> aList;
156   aList.append(myLabel);
157   aList.append(myEditor);
158   return aList;
159 }