1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_ModelWidget.cpp
4 // Created: 25 Apr 2014
5 // Author: Natalia ERMOLAEVA
7 #include "ModuleBase_ModelWidget.h"
8 #include "ModuleBase_ViewerPrs.h"
9 #include "ModuleBase_Tools.h"
10 #include "ModuleBase_WidgetValidator.h"
12 #include <Events_InfoMessage.h>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_Attribute.h>
16 #include <ModelAPI_Events.h>
17 #include <ModelAPI_Session.h>
18 #include <ModelAPI_Validator.h>
20 #include <Config_Keywords.h>
21 #include <Config_WidgetAPI.h>
22 #include <Config_Translator.h>
23 #include <Config_PropManager.h>
25 #include <Events_Loop.h>
29 #include <QFocusEvent>
32 //#define DEBUG_VALUE_STATE
34 //#define DEBUG_WIDGET_INSTANCE
35 //#define DEBUG_ENABLE_SKETCH_INPUT_FIELDS
37 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
38 const Config_WidgetAPI* theData)
42 myIsValueStateBlocked(false),
43 myFlushUpdateBlocked(false),
46 #ifdef DEBUG_WIDGET_INSTANCE
47 qDebug("ModuleBase_ModelWidget::ModuleBase_ModelWidget");
50 myFeatureId = theData->featureId();
52 myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
54 myDefaultValue = theData->getProperty(ATTR_DEFAULT);
55 myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
56 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
57 myAttributeID = theData ? theData->widgetId() : "";
58 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
60 myIsValueEnabled = On; // not defined or "true"
61 std::string anEnableValue = theData->getProperty(DOUBLE_WDG_ENABLE_VALUE);
62 if (anEnableValue == "false")
63 myIsValueEnabled = Off;
64 if (anEnableValue == DOUBLE_WDG_ENABLE_VALUE_BY_PREFERENCES)
65 myIsValueEnabled = DefinedInPreferences;
67 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
68 connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
71 ModuleBase_ModelWidget::~ModuleBase_ModelWidget()
73 #ifdef DEBUG_WIDGET_INSTANCE
74 qDebug("ModuleBase_ModelWidget::~ModuleBase_ModelWidget");
78 bool ModuleBase_ModelWidget::reset()
80 bool aResult = resetCustom();
87 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
89 return theObject->data()->attribute(attributeID())->isInitialized();
92 bool ModuleBase_ModelWidget::isValueEnabled() const
94 bool anEnabled = true;
95 if (myIsValueEnabled == DefinedInPreferences) {
96 #ifdef DEBUG_ENABLE_SKETCH_INPUT_FIELDS
97 bool aCanDisable = false;
99 //Config_PropManager::boolean(SKETCH_TAB_NAME, "disable_input_fields", "true");
100 bool aCanDisable = true;
105 else if (myIsValueEnabled == Off)
110 void ModuleBase_ModelWidget::processValueState()
112 if (myState == ModifiedInPP || myState == ModifiedInViewer)
116 Events_InfoMessage ModuleBase_ModelWidget::getValueStateError() const
118 Events_InfoMessage aMessage;
120 ModuleBase_ModelWidget::ValueState aState = getValueState();
121 if (aState != ModuleBase_ModelWidget::Stored) {
122 AttributePtr anAttr = feature()->attribute(attributeID());
124 const std::string& anAttributeName = anAttr->id();
126 case ModuleBase_ModelWidget::ModifiedInViewer:
127 aMessage = "Attribute \"%1\" is locked by modification value in the viewer.";
128 aMessage.addParameter(anAttributeName);
130 case ModuleBase_ModelWidget::Reset:
131 aMessage = "Attribute \"%1\" is not initialized.";
132 aMessage.addParameter(anAttributeName);
134 case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
143 QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
147 if (!feature().get())
150 std::string aFeatureID = feature()->getKind();
151 std::string anAttributeID = attributeID();
152 AttributePtr anAttribute = feature()->attribute(anAttributeID);
153 if (!anAttribute.get())
156 std::string aValidatorID;
157 Events_InfoMessage anErrorMsg;
159 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
160 if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
161 if (anErrorMsg.empty())
162 anErrorMsg = "Unknown error.";
164 if (anErrorMsg.context().empty()) {
165 anErrorMsg.setContext(aFeatureID + ":" + anAttributeID + ":" + aValidatorID);
169 if (anErrorMsg.empty() && theValueStateChecked) {
170 anErrorMsg = getValueStateError();
173 if (!anErrorMsg.empty()) {
174 anError = ModuleBase_Tools::translate(anErrorMsg);
180 void ModuleBase_ModelWidget::enableFocusProcessing()
182 QList<QWidget*> aMyControls = getControls();
183 foreach(QWidget* eachControl, aMyControls) {
184 eachControl->setFocusPolicy(Qt::StrongFocus);
185 eachControl->installEventFilter(this);
189 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
191 QList<QWidget*> aWidgetList = getControls();
192 foreach(QWidget* aWidget, aWidgetList) {
193 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
194 // We won't set the effect to QLabels - it looks ugly
196 // If effect is the installed on a different widget, setGraphicsEffect() will
197 // remove the effect from the widget and install it on this widget.
198 // That's why we create a new effect for each widget
199 ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
203 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue,
204 const bool isUpdateFlushed)
206 /// it is possible to give this flag as parameter in storeValue/storeCustomValue
207 /// after debug, it may be corrected
208 myFlushUpdateBlocked = !isUpdateFlushed;
209 myFeature = theFeature;
210 if (theToStoreValue) {
211 /// it is possible that the attribute is filled before the operation is started,
212 /// e.g. by reentrant operation case some attributes are filled by values of
213 /// feature of previous operation, we should not lost them here
214 if (!theFeature->data()->attribute(attributeID())->isInitialized())
217 myFlushUpdateBlocked = false;
220 bool ModuleBase_ModelWidget::focusTo()
222 #ifdef DEBUG_WIDGET_INSTANCE
223 qDebug("ModuleBase_ModelWidget::focusTo");
225 QList<QWidget*> aControls = getControls();
226 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
227 bool isFocusAccepted = false;
228 for (; anIt != aLast && !isFocusAccepted; anIt++) {
229 QWidget* aWidget = *anIt;
230 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
231 ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
232 isFocusAccepted = true;
235 return isFocusAccepted;
238 void ModuleBase_ModelWidget::activate()
240 #ifdef DEBUG_WIDGET_INSTANCE
241 qDebug("ModuleBase_ModelWidget::activate");
243 // the control value is stored to the mode by the focus in on the widget
244 // we need the value is initialized in order to enable the apply button in the property panel.
245 // It should happens in the creation mode only because all fields are filled in the edition mode
246 if (!isEditingMode()) {
247 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
248 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
249 initializeValueByActivate();
252 if (myWidgetValidator)
253 myWidgetValidator->activateFilters(true);
258 void ModuleBase_ModelWidget::deactivate()
260 #ifdef DEBUG_WIDGET_INSTANCE
261 qDebug("ModuleBase_ModelWidget::deactivate");
263 myIsValueStateBlocked = false;
265 if (myWidgetValidator)
266 myWidgetValidator->activateFilters(false);
269 void ModuleBase_ModelWidget::initializeValueByActivate()
271 if (isComputedDefault()) {
272 if (myFeature->compute(myAttributeID)) {
281 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
283 QWidget* aControl = 0;
285 QList<QWidget*> aControls = getControls();
286 int aSize = aControls.size();
289 for (int i = 0; i < aSize && !aControl; i++) {
290 if (aControls[i]->focusPolicy() != Qt::NoFocus)
291 aControl = aControls[i];
295 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
296 if (aControls[i]->focusPolicy() != Qt::NoFocus)
297 aControl = aControls[i];
303 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
305 myDefaultValue = theValue;
308 bool ModuleBase_ModelWidget::storeValue()
310 setValueState(Stored);
312 emit beforeValuesChanged();
313 bool isDone = storeValueCustom();
314 emit afterValuesChanged();
318 #ifdef DEBUG_VALUE_STATE
319 std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
323 case ModuleBase_ModelWidget::Stored: anInfo = "Stored "; break;
324 case ModuleBase_ModelWidget::ModifiedInPP: anInfo = "ModifiedInPP "; break;
325 case ModuleBase_ModelWidget::ModifiedInViewer: anInfo = "ModifiedInViewer"; break;
326 case ModuleBase_ModelWidget::Reset: anInfo = "Reset "; break;
333 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
334 (const ModuleBase_ModelWidget::ValueState& theState)
336 ValueState aState = myState;
338 if (myState != theState && !myIsValueStateBlocked) {
339 #ifdef DEBUG_VALUE_STATE
340 qDebug(QString("setValueState: previous state = %1,\t new state = %2")
341 .arg(getDebugInfo(myState).c_str())
342 .arg(getDebugInfo(theState).c_str()).toStdString().c_str());
345 emit valueStateChanged(aState);
350 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
352 bool isBlocked = myIsValueStateBlocked;
353 myIsValueStateBlocked = theBlocked;
357 bool ModuleBase_ModelWidget::restoreValue()
359 emit beforeValuesRestored();
360 bool isDone = restoreValueCustom();
361 emit afterValuesRestored();
366 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
368 if (!myFlushUpdateBlocked) {
369 #ifdef DEBUG_WIDGET_INSTANCE
370 qDebug("ModuleBase_ModelWidget::updateObject");
372 ModuleBase_Tools::flushUpdated(theObject);
373 emit objectUpdated();
377 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
379 //blockUpdateViewer(true);
380 #ifdef DEBUG_WIDGET_INSTANCE
381 qDebug("ModuleBase_ModelWidget::moveObject");
384 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
385 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
386 Events_Loop::loop()->flush(anEvent);
388 //blockUpdateViewer(false);
391 bool ModuleBase_ModelWidget::processEnter()
396 bool ModuleBase_ModelWidget::processDelete()
398 // we consider that model objects eats delete key in order to
399 // do nothing by for example symbol delete in line edit or spin box
403 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
405 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
406 if (theEvent->type() == QEvent::FocusIn) {
408 // The following two lines are for debugging purpose only
409 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
410 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
412 if (getControls().contains(aWidget)) {
413 emit focusInWidget(this);
416 else if (theEvent->type() == QEvent::FocusOut) {
417 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
419 Qt::FocusReason aReason = aFocusEvent->reason();
420 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
421 aReason == Qt::TabFocusReason ||
422 aReason == Qt::BacktabFocusReason ||
423 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
424 if (aMouseOrKey && getControls().contains(aWidget)) {
425 if (getValueState() == ModifiedInPP) {
430 // pass the event on to the parent class
432 return QObject::eventFilter(theObject, theEvent);
435 //**************************************************************
436 void ModuleBase_ModelWidget::onWidgetValuesChanged()
441 //**************************************************************
442 void ModuleBase_ModelWidget::onWidgetValuesModified()
444 setValueState(ModifiedInPP);
447 QString ModuleBase_ModelWidget::translate(const std::string& theStr) const
449 return ModuleBase_Tools::translate(context(), theStr);