]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_ModelWidget.cpp
Salome HOME
Improvement for key "Delete" processing: should be done only on possible objects...
[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_ViewerPrs.h"
9 #include "ModuleBase_Tools.h"
10 #include "ModuleBase_WidgetValidator.h"
11
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_Attribute.h>
14 #include <ModelAPI_Events.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_Validator.h>
17
18 #include <Config_Keywords.h>
19 #include <Config_WidgetAPI.h>
20
21 #include <Events_Loop.h>
22
23 #include <QEvent>
24 #include <QLabel>
25 #include <QFocusEvent>
26
27 //#define DEBUG_VALUE_STATE
28
29 //#define DEBUG_WIDGET_INSTANCE
30
31 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
32                                                const Config_WidgetAPI* theData)
33     : QWidget(theParent),
34       myIsEditing(false),
35       myState(Stored),
36       myIsValueStateBlocked(false),
37       myFlushUpdateBlocked(false),
38       myWidgetValidator(0)
39 {
40 #ifdef DEBUG_WIDGET_INSTANCE
41   qDebug("ModuleBase_ModelWidget::ModuleBase_ModelWidget");
42 #endif
43
44   myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
45
46   myDefaultValue = theData->getProperty(ATTR_DEFAULT);
47   myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
48   myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
49   myAttributeID = theData ? theData->widgetId() : "";
50   myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
51
52   connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
53   connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
54 }
55
56 ModuleBase_ModelWidget::~ModuleBase_ModelWidget()
57 {
58 #ifdef DEBUG_WIDGET_INSTANCE
59   qDebug("ModuleBase_ModelWidget::~ModuleBase_ModelWidget");
60 #endif
61 }
62
63 bool ModuleBase_ModelWidget::reset()
64 {
65   bool aResult = resetCustom();
66   if (aResult)
67     setValueState(Reset);
68
69   return aResult;
70 }
71
72 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
73 {
74   return theObject->data()->attribute(attributeID())->isInitialized();
75 }
76
77 void ModuleBase_ModelWidget::processValueState()
78 {
79   if (myState == ModifiedInPP || myState == ModifiedInViewer)
80     storeValue();
81 }
82
83 QString ModuleBase_ModelWidget::getValueStateError() const
84 {
85   QString anError = "";
86
87   ModuleBase_ModelWidget::ValueState aState = getValueState();
88   if (aState != ModuleBase_ModelWidget::Stored) {
89     AttributePtr anAttr = feature()->attribute(attributeID());
90     if (anAttr.get()) {
91       QString anAttributeName = anAttr->id().c_str();
92       switch (aState) {
93         case ModuleBase_ModelWidget::ModifiedInViewer:
94           anError = "Attribute \"" + anAttributeName +
95                     "\" is locked by modification value in the viewer.";
96           break;
97         case ModuleBase_ModelWidget::Reset:
98           anError = "Attribute \"" + anAttributeName + "\" is not initialized.";
99           break;
100         case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
101         default:
102           break;
103       }
104     }
105   }
106   return anError;
107 }
108
109 QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
110 {
111   QString anError;
112
113   if (!feature().get())
114     return anError;
115
116   std::string anAttributeID = attributeID();
117   AttributePtr anAttribute = feature()->attribute(anAttributeID);
118   if (!anAttribute.get())
119     return anError;
120
121   std::string aValidatorID;
122   std::string anErrorMsg;
123
124   static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
125   if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
126     if (anErrorMsg.empty())
127       anErrorMsg = "unknown error.";
128     anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg;
129   }
130
131   anError = QString::fromStdString(anErrorMsg);
132   if (anError.isEmpty() && theValueStateChecked)
133     anError = getValueStateError();
134
135   return anError;
136 }
137
138 void ModuleBase_ModelWidget::enableFocusProcessing()
139 {
140   QList<QWidget*> aMyControls = getControls();
141   foreach(QWidget*  eachControl, aMyControls) {
142       eachControl->setFocusPolicy(Qt::StrongFocus);
143       eachControl->installEventFilter(this);
144   }
145 }
146
147 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
148 {
149   QList<QWidget*> aWidgetList = getControls();
150   foreach(QWidget* aWidget, aWidgetList) {
151     QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
152     // We won't set the effect to QLabels - it looks ugly
153     if(aLabel) continue;
154     // If effect is the installed on a different widget, setGraphicsEffect() will
155     // remove the effect from the widget and install it on this widget.
156     // That's why we create a new effect for each widget
157     ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
158   }
159 }
160
161 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue,
162                                         const bool isUpdateFlushed)
163 {
164   /// it is possible to give this flag as parameter in storeValue/storeCustomValue
165   /// after debug, it may be corrected
166   myFlushUpdateBlocked = !isUpdateFlushed;
167   myFeature = theFeature;
168   if (theToStoreValue)
169     storeValue();
170   myFlushUpdateBlocked = false;
171 }
172
173 bool ModuleBase_ModelWidget::focusTo()
174 {
175 #ifdef DEBUG_WIDGET_INSTANCE
176   qDebug("ModuleBase_ModelWidget::focusTo");
177 #endif
178   QList<QWidget*> aControls = getControls();
179   QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
180   bool isFocusAccepted = false;
181   for (; anIt != aLast && !isFocusAccepted; anIt++) {
182     QWidget* aWidget = *anIt;
183     if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
184       ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
185       isFocusAccepted = true;
186     }
187   }
188   return isFocusAccepted;
189 }
190
191 void ModuleBase_ModelWidget::activate()
192 {
193 #ifdef DEBUG_WIDGET_INSTANCE
194   qDebug("ModuleBase_ModelWidget::activate");
195 #endif
196   // the control value is stored to the mode by the focus in on the widget
197   // we need the value is initialized in order to enable the apply button in the property panel.
198   // It should happens in the creation mode only because all fields are filled in the edition mode
199   if (!isEditingMode()) {
200     AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
201     if (anAttribute.get() != NULL && !anAttribute->isInitialized())
202       initializeValueByActivate();
203   }
204
205   if (myWidgetValidator)
206     myWidgetValidator->activateFilters(true);
207
208   activateCustom();
209 }
210
211 void ModuleBase_ModelWidget::deactivate()
212 {
213 #ifdef DEBUG_WIDGET_INSTANCE
214   qDebug("ModuleBase_ModelWidget::deactivate");
215 #endif
216   myIsValueStateBlocked = false;
217   myState = Stored;
218   if (myWidgetValidator)
219     myWidgetValidator->activateFilters(false);
220 }
221
222 void ModuleBase_ModelWidget::initializeValueByActivate()
223 {
224   if (isComputedDefault()) {
225     if (myFeature->compute(myAttributeID)) {
226       restoreValue();
227     }
228   }
229   else {
230     storeValue();
231   }
232 }
233
234 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
235 {
236   QWidget* aControl = 0;
237
238   QList<QWidget*> aControls = getControls();
239   int aSize = aControls.size();
240
241   if (isFirst) {
242     for (int i = 0; i < aSize && !aControl; i++)  {
243       if (aControls[i]->focusPolicy() != Qt::NoFocus)
244         aControl = aControls[i];
245     }
246   }
247   else {
248     for (int i = aSize - 1; i >= 0 && !aControl; i--)  {
249       if (aControls[i]->focusPolicy() != Qt::NoFocus)
250         aControl = aControls[i];
251     }
252   }
253   return aControl;
254 }
255
256 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
257 {
258   myDefaultValue = theValue;
259 }
260
261 bool ModuleBase_ModelWidget::storeValue()
262 {
263   setValueState(Stored);
264
265   emit beforeValuesChanged();
266   bool isDone = storeValueCustom();
267   emit afterValuesChanged();
268
269   return isDone;
270 }
271 #ifdef DEBUG_VALUE_STATE
272 std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
273 {
274   std::string anInfo;
275   switch (theState) {
276     case ModuleBase_ModelWidget::Stored:           anInfo = "Stored          "; break;
277     case ModuleBase_ModelWidget::ModifiedInPP:     anInfo = "ModifiedInPP    "; break;
278     case ModuleBase_ModelWidget::ModifiedInViewer: anInfo = "ModifiedInViewer"; break;
279     case ModuleBase_ModelWidget::Reset:            anInfo = "Reset           "; break;
280     default: break;
281   }
282   return anInfo;
283 }
284
285 #endif
286 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
287                                          (const ModuleBase_ModelWidget::ValueState& theState)
288 {
289   ValueState aState = myState;
290
291   if (myState != theState && !myIsValueStateBlocked) {
292 #ifdef DEBUG_VALUE_STATE
293     qDebug(QString("setValueState: previous state = %1,\t new state = %2")
294            .arg(getDebugInfo(myState).c_str())
295            .arg(getDebugInfo(theState).c_str()).toStdString().c_str());
296 #endif
297     myState = theState;
298     emit valueStateChanged(aState);
299   }
300   return aState;
301 }
302
303 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
304 {
305   bool isBlocked = myIsValueStateBlocked;
306   myIsValueStateBlocked = theBlocked;
307   return isBlocked;
308 }
309
310 bool ModuleBase_ModelWidget::restoreValue()
311 {
312   emit beforeValuesRestored();
313   bool isDone = restoreValueCustom();
314   emit afterValuesRestored();
315
316   return isDone;
317 }
318
319 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
320 {
321   if (!myFlushUpdateBlocked) {
322 #ifdef DEBUG_WIDGET_INSTANCE
323     qDebug("ModuleBase_ModelWidget::updateObject");
324 #endif
325     ModuleBase_Tools::flushUpdated(theObject);
326     emit objectUpdated();
327   }
328 }
329
330 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
331 {
332   //blockUpdateViewer(true);
333 #ifdef DEBUG_WIDGET_INSTANCE
334   qDebug("ModuleBase_ModelWidget::moveObject");
335 #endif
336
337   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
338   ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
339   Events_Loop::loop()->flush(anEvent);
340
341   //blockUpdateViewer(false);
342 }
343
344 bool ModuleBase_ModelWidget::processEnter()
345 {
346   return false;
347 }
348
349 bool ModuleBase_ModelWidget::processDelete()
350 {
351   // we consider that model objects eats delete key in order to
352   // do nothing by for example symbol delete in line edit or spin box
353   return true;
354 }
355
356 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
357 {
358   QWidget* aWidget = qobject_cast<QWidget*>(theObject);
359   if (theEvent->type() == QEvent::FocusIn) {
360     #ifdef _DEBUG
361     // The following two lines are for debugging purpose only
362     QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
363     bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
364     #endif
365     if (getControls().contains(aWidget)) {
366       emit focusInWidget(this);
367     }
368   }
369   else if (theEvent->type() == QEvent::FocusOut) {
370     QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
371
372     Qt::FocusReason aReason = aFocusEvent->reason();
373     bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
374                         aReason == Qt::TabFocusReason ||
375                         aReason == Qt::BacktabFocusReason ||
376                         aReason == Qt::OtherFocusReason; // to process widget->setFocus()
377     if (aMouseOrKey && getControls().contains(aWidget)) {
378       if (getValueState() == ModifiedInPP) {
379         storeValue();
380       }
381     }
382   }
383   // pass the event on to the parent class
384
385   return QObject::eventFilter(theObject, theEvent);
386 }
387
388 //**************************************************************
389 void ModuleBase_ModelWidget::onWidgetValuesChanged()
390 {
391   storeValue();
392 }
393
394 //**************************************************************
395 void ModuleBase_ModelWidget::onWidgetValuesModified()
396 {
397   setValueState(ModifiedInPP);
398 }