Salome HOME
#1150 Tab buttons problems
[modules/shaper.git] / src / ModuleBase / ModuleBase_ModelWidget.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_ModelWidget.cpp
4 // Created:     25 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #include "ModuleBase_ModelWidget.h"
8 #include "ModuleBase_Tools.h"
9
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_Attribute.h>
12 #include <ModelAPI_Events.h>
13 #include <ModelAPI_Session.h>
14 #include <ModelAPI_Validator.h>
15
16 #include <Config_Keywords.h>
17 #include <Config_WidgetAPI.h>
18
19 #include <Events_Loop.h>
20
21 #include <QEvent>
22 #include <QLabel>
23 #include <QFocusEvent>
24
25 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
26                                                const Config_WidgetAPI* theData,
27                                                const std::string& theParentId)
28     : QWidget(theParent),
29       myParentId(theParentId),
30       myIsEditing(false),
31       myState(Stored),
32       myIsValueStateBlocked(false)
33 {
34   myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
35
36   myDefaultValue = theData->getProperty(ATTR_DEFAULT);
37   myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
38   myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
39   myAttributeID = theData ? theData->widgetId() : "";
40   myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
41
42   connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
43   connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
44 }
45
46 bool ModuleBase_ModelWidget::reset()
47 {
48   bool aResult = resetCustom();
49   if (aResult)
50     setValueState(Reset);
51
52   return aResult;
53 }
54
55 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
56 {
57   return theObject->data()->attribute(attributeID())->isInitialized();
58 }
59
60 QString ModuleBase_ModelWidget::getValueStateError() const
61 {
62   QString anError = "";
63
64   ModuleBase_ModelWidget::ValueState aState = getValueState();
65   if (aState != ModuleBase_ModelWidget::Stored) {
66     AttributePtr anAttr = feature()->attribute(attributeID());
67     if (anAttr.get()) {
68       QString anAttributeName = anAttr->id().c_str();
69       switch (aState) {
70         case ModuleBase_ModelWidget::ModifiedInViewer:
71           anError = "Attribute \"" + anAttributeName +
72                     "\" is locked by modification value in the viewer.";
73           break;
74         case ModuleBase_ModelWidget::Reset:
75           anError = "Attribute \"" + anAttributeName + "\" is not initialized.";
76           break;
77         case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
78         default:
79           break;
80       }
81     }
82   }
83   return anError;
84 }
85
86 QString ModuleBase_ModelWidget::getError() const
87 {
88   QString anError;
89
90   if (!feature().get())
91     return anError;
92
93   std::string anAttributeID = attributeID();
94   AttributePtr anAttribute = feature()->attribute(anAttributeID);
95   if (!anAttribute.get())
96     return anError;
97
98   std::string aValidatorID;
99   std::string anErrorMsg;
100
101   static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
102   if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
103     if (anErrorMsg.empty())
104       anErrorMsg = "unknown error.";
105     anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg;
106   }
107
108   anError = QString::fromStdString(anErrorMsg);
109   if (anError.isEmpty())
110     anError = getValueStateError();
111
112   return anError;
113 }
114
115 void ModuleBase_ModelWidget::enableFocusProcessing()
116 {
117   QList<QWidget*> aMyControls = getControls();
118   foreach(QWidget*  eachControl, aMyControls) {
119       eachControl->setFocusPolicy(Qt::StrongFocus);
120       eachControl->installEventFilter(this);
121   }
122 }
123
124 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
125 {
126   QList<QWidget*> aWidgetList = getControls();
127   foreach(QWidget* aWidget, aWidgetList) {
128     QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
129     // We won't set the effect to QLabels - it looks ugly
130     if(aLabel) continue;
131     // If effect is the installed on a different widget, setGraphicsEffect() will
132     // remove the effect from the widget and install it on this widget.
133     // That's why we create a new effect for each widget
134     ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
135   }
136 }
137
138 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
139 {
140   myFeature = theFeature;
141   if (theToStoreValue)
142     storeValue();
143 }
144
145 bool ModuleBase_ModelWidget::focusTo()
146 {
147   QList<QWidget*> aControls = getControls();
148   QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
149   bool isFocusAccepted = false;
150   for (; anIt != aLast && !isFocusAccepted; anIt++) {
151     QWidget* aWidget = *anIt;
152     if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
153       ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
154       isFocusAccepted = true;
155     }
156   }
157   return isFocusAccepted;
158 }
159
160 void ModuleBase_ModelWidget::activate()
161 {
162   // the control value is stored to the mode by the focus in on the widget
163   // we need the value is initialized in order to enable the apply button in the property panel.
164   // It should happens in the creation mode only because all fields are filled in the edition mode
165   if (!isEditingMode()) {
166     AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
167     if (anAttribute.get() != NULL && !anAttribute->isInitialized())
168       initializeValueByActivate();
169   }
170   activateCustom();
171 }
172
173 void ModuleBase_ModelWidget::deactivate()
174 {
175   myIsValueStateBlocked = false;
176   if (myState == ModifiedInPP || myState == ModifiedInViewer)
177     storeValue();
178   myState = Stored;
179 }
180
181 void ModuleBase_ModelWidget::initializeValueByActivate()
182 {
183   if (isComputedDefault()) {
184     if (myFeature->compute(myAttributeID)) {
185       restoreValue();
186     }
187   }
188   else {
189     storeValue();
190   }
191 }
192
193 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
194 {
195   QWidget* aControl = 0;
196
197   QList<QWidget*> aControls = getControls();
198   int aSize = aControls.size();
199
200   if (isFirst) {
201     for (int i = 0; i < aSize && !aControl; i++)  {
202       if (aControls[i]->focusPolicy() != Qt::NoFocus)
203         aControl = aControls[i];
204     }
205   }
206   else {
207     for (int i = aSize - 1; i >= 0 && !aControl; i--)  {
208       if (aControls[i]->focusPolicy() != Qt::NoFocus)
209         aControl = aControls[i];
210     }
211   }
212   return aControl;
213 }
214
215 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
216 {
217   myDefaultValue = theValue;
218 }
219
220 bool ModuleBase_ModelWidget::storeValue()
221 {
222   setValueState(Stored);
223
224   emit beforeValuesChanged();
225   bool isDone = storeValueCustom();
226   emit afterValuesChanged();
227
228   return isDone;
229 }
230
231 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState(const ModuleBase_ModelWidget::ValueState& theState)
232 {
233   ValueState aState = myState;
234   if (myState != theState && !myIsValueStateBlocked) {
235     myState = theState;
236     emit valueStateChanged(aState);
237   }
238   return aState;
239 }
240
241 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
242 {
243   bool isBlocked = myIsValueStateBlocked;
244   myIsValueStateBlocked = theBlocked;
245   return isBlocked;
246 }
247
248 bool ModuleBase_ModelWidget::restoreValue()
249 {
250   emit beforeValuesRestored();
251   bool isDone = restoreValueCustom();
252   emit afterValuesRestored();
253
254   return isDone;
255 }
256
257 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj)
258 {
259   blockUpdateViewer(true);
260
261   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
262
263   blockUpdateViewer(false);
264 }
265
266 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
267 {
268   //blockUpdateViewer(true);
269
270   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
271   ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
272   Events_Loop::loop()->flush(anEvent);
273
274   //blockUpdateViewer(false);
275 }
276
277 bool ModuleBase_ModelWidget::processEnter()
278 {
279   return false;
280 }
281
282 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
283 {
284   QWidget* aWidget = qobject_cast<QWidget*>(theObject);
285   if (theEvent->type() == QEvent::FocusIn) {
286     #ifdef _DEBUG
287     // The following two lines are for debugging purpose only
288     QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
289     bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
290     #endif
291     if (getControls().contains(aWidget)) {
292       emit focusInWidget(this);
293     }
294   }
295   else if (theEvent->type() == QEvent::FocusOut) {
296     QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
297
298     Qt::FocusReason aReason = aFocusEvent->reason();
299     bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
300                         aReason == Qt::TabFocusReason ||
301                         aReason == Qt::BacktabFocusReason ||
302                         aReason == Qt::OtherFocusReason; // to process widget->setFocus()
303     if (aMouseOrKey && getControls().contains(aWidget)) {
304       if (getValueState() == ModifiedInPP) {
305         storeValue();
306       }
307     }
308   }
309   // pass the event on to the parent class
310
311   return QObject::eventFilter(theObject, theEvent);
312 }
313
314 //**************************************************************
315 void ModuleBase_ModelWidget::onWidgetValuesChanged()
316 {
317   storeValue();
318 }
319
320 //**************************************************************
321 void ModuleBase_ModelWidget::onWidgetValuesModified()
322 {
323   setValueState(ModifiedInPP);
324 }
325
326 //**************************************************************
327 void ModuleBase_ModelWidget::blockUpdateViewer(const bool theValue)
328 {
329   // the viewer update should be blocked in order to avoid the temporary feature content
330   // when the solver processes the feature, the redisplay message can be flushed
331   // what caused the display in the viewer preliminary states of object
332   // e.g. fillet feature, angle value change
333   std::shared_ptr<Events_Message> aMsg;
334   if (theValue) {
335     aMsg = std::shared_ptr<Events_Message>(
336         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
337   }
338   else {
339     // the viewer update should be unblocked
340     aMsg = std::shared_ptr<Events_Message>(
341         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
342   }
343   Events_Loop::loop()->send(aMsg);
344 }