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_IPropertyPanel.h"
9 #include "ModuleBase_ViewerPrs.h"
10 #include "ModuleBase_Tools.h"
11 #include "ModuleBase_WidgetValidator.h"
13 #include <Events_InfoMessage.h>
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_Attribute.h>
17 #include <ModelAPI_Events.h>
18 #include <ModelAPI_Session.h>
19 #include <ModelAPI_Validator.h>
21 #include <Config_Keywords.h>
22 #include <Config_WidgetAPI.h>
23 #include <Config_Translator.h>
24 #include <Config_PropManager.h>
26 #include <Events_Loop.h>
30 #include <QFocusEvent>
33 //#define DEBUG_VALUE_STATE
35 //#define DEBUG_WIDGET_INSTANCE
36 //#define DEBUG_ENABLE_SKETCH_INPUT_FIELDS
38 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
39 const Config_WidgetAPI* theData)
43 myIsValueStateBlocked(false),
44 myFlushUpdateBlocked(false),
47 #ifdef DEBUG_WIDGET_INSTANCE
48 qDebug("ModuleBase_ModelWidget::ModuleBase_ModelWidget");
51 myFeatureId = theData->featureId();
53 myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
55 myIsModifiedInEdit = theData->getProperty(ATTR_MODIFIED_IN_EDIT);
57 myDefaultValue = theData->getProperty(ATTR_DEFAULT);
58 myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
59 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
60 myAttributeID = theData ? theData->widgetId() : "";
61 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
63 myIsValueEnabled = On; // not defined or "true"
64 std::string anEnableValue = theData->getProperty(DOUBLE_WDG_ENABLE_VALUE);
65 if (anEnableValue == "false")
66 myIsValueEnabled = Off;
67 if (anEnableValue == DOUBLE_WDG_ENABLE_VALUE_BY_PREFERENCES)
68 myIsValueEnabled = DefinedInPreferences;
70 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
71 connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
74 ModuleBase_ModelWidget::~ModuleBase_ModelWidget()
76 #ifdef DEBUG_WIDGET_INSTANCE
77 qDebug("ModuleBase_ModelWidget::~ModuleBase_ModelWidget");
81 bool ModuleBase_ModelWidget::reset()
83 bool aResult = resetCustom();
90 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
92 return theObject->data()->attribute(attributeID())->isInitialized();
95 bool ModuleBase_ModelWidget::isValueEnabled() const
97 bool anEnabled = true;
98 if (myIsValueEnabled == DefinedInPreferences) {
99 #ifdef DEBUG_ENABLE_SKETCH_INPUT_FIELDS
100 bool aCanDisable = false;
102 //Config_PropManager::boolean(SKETCH_TAB_NAME, "disable_input_fields", "true");
103 bool aCanDisable = true;
108 else if (myIsValueEnabled == Off)
113 void ModuleBase_ModelWidget::processValueState()
115 if (myState == ModifiedInPP || myState == ModifiedInViewer)
119 Events_InfoMessage ModuleBase_ModelWidget::getValueStateError() const
121 Events_InfoMessage aMessage;
123 ModuleBase_ModelWidget::ValueState aState = getValueState();
124 if (aState != ModuleBase_ModelWidget::Stored) {
125 AttributePtr anAttr = feature()->attribute(attributeID());
127 const std::string& anAttributeName = anAttr->id();
129 case ModuleBase_ModelWidget::ModifiedInViewer:
130 aMessage = "Attribute \"%1\" is locked by modification value in the viewer.";
131 aMessage.addParameter(anAttributeName);
133 case ModuleBase_ModelWidget::Reset:
134 aMessage = "Attribute \"%1\" is not initialized.";
135 aMessage.addParameter(anAttributeName);
137 case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
146 QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
150 if (!feature().get())
153 std::string aFeatureID = feature()->getKind();
154 std::string anAttributeID = attributeID();
155 AttributePtr anAttribute = feature()->attribute(anAttributeID);
156 if (!anAttribute.get())
159 std::string aValidatorID;
160 Events_InfoMessage anErrorMsg;
162 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
163 if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
164 if (anErrorMsg.empty())
165 anErrorMsg = "Unknown error.";
167 if (anErrorMsg.context().empty()) {
168 anErrorMsg.setContext(aFeatureID + ":" + anAttributeID + ":" + aValidatorID);
172 if (anErrorMsg.empty() && theValueStateChecked) {
173 anErrorMsg = getValueStateError();
176 if (!anErrorMsg.empty()) {
177 anError = ModuleBase_Tools::translate(anErrorMsg);
183 void ModuleBase_ModelWidget::enableFocusProcessing()
185 QList<QWidget*> aMyControls = getControls();
186 foreach(QWidget* eachControl, aMyControls) {
187 eachControl->setFocusPolicy(Qt::StrongFocus);
188 eachControl->installEventFilter(this);
192 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
194 QList<QWidget*> aWidgetList = getControls();
195 foreach(QWidget* aWidget, aWidgetList) {
196 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
197 // We won't set the effect to QLabels - it looks ugly
199 // If effect is the installed on a different widget, setGraphicsEffect() will
200 // remove the effect from the widget and install it on this widget.
201 // That's why we create a new effect for each widget
202 ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
206 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue,
207 const bool isUpdateFlushed)
209 /// it is possible to give this flag as parameter in storeValue/storeCustomValue
210 /// after debug, it may be corrected
211 myFlushUpdateBlocked = !isUpdateFlushed;
212 myFeature = theFeature;
213 if (theToStoreValue) {
214 /// it is possible that the attribute is filled before the operation is started,
215 /// e.g. by reentrant operation case some attributes are filled by values of
216 /// feature of previous operation, we should not lost them here
217 if (!theFeature->data()->attribute(attributeID())->isInitialized())
220 myFlushUpdateBlocked = false;
223 bool ModuleBase_ModelWidget::focusTo()
225 #ifdef DEBUG_WIDGET_INSTANCE
226 qDebug("ModuleBase_ModelWidget::focusTo");
228 QList<QWidget*> aControls = getControls();
229 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
230 bool isFocusAccepted = false;
231 for (; anIt != aLast && !isFocusAccepted; anIt++) {
232 QWidget* aWidget = *anIt;
233 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
234 ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
235 isFocusAccepted = true;
238 return isFocusAccepted;
241 void ModuleBase_ModelWidget::activate()
243 #ifdef DEBUG_WIDGET_INSTANCE
244 qDebug("ModuleBase_ModelWidget::activate");
246 // the control value is stored to the mode by the focus in on the widget
247 // we need the value is initialized in order to enable the apply button in the property panel.
248 // It should happens in the creation mode only because all fields are filled in the edition mode
249 if (!isEditingMode()) {
250 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
251 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
252 initializeValueByActivate();
255 if (myWidgetValidator)
256 myWidgetValidator->activateFilters(true);
261 void ModuleBase_ModelWidget::deactivate()
263 #ifdef DEBUG_WIDGET_INSTANCE
264 qDebug("ModuleBase_ModelWidget::deactivate");
266 myIsValueStateBlocked = false;
268 if (myWidgetValidator)
269 myWidgetValidator->activateFilters(false);
272 void ModuleBase_ModelWidget::initializeValueByActivate()
274 if (isComputedDefault()) {
275 if (myFeature->compute(myAttributeID)) {
284 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
286 QWidget* aControl = 0;
288 QList<QWidget*> aControls = getControls();
289 int aSize = aControls.size();
292 for (int i = 0; i < aSize && !aControl; i++) {
293 if (aControls[i]->focusPolicy() != Qt::NoFocus)
294 aControl = aControls[i];
298 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
299 if (aControls[i]->focusPolicy() != Qt::NoFocus)
300 aControl = aControls[i];
306 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
308 myDefaultValue = theValue;
311 bool ModuleBase_ModelWidget::storeValue()
313 setValueState(Stored);
315 emit beforeValuesChanged();
317 // value is stored only in creation mode and in edition if there is not
318 // XML flag prohibited modification in edit mode(macro feature circle/arc)
319 if (!isEditingMode() || isModifiedInEdit().empty())
320 isDone = storeValueCustom();
322 /// store value in an alternative attribute if possible(attribute has the same type)
323 std::string aWidgetAttribute = attributeID();
324 myAttributeID = isModifiedInEdit();
326 myAttributeID = aWidgetAttribute;
327 // operation will be restarted but if isDone == true, PagedContainer will try to set focus
328 // to the current widget, but will be already deleted
332 emit afterValuesChanged();
336 #ifdef DEBUG_VALUE_STATE
337 std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
341 case ModuleBase_ModelWidget::Stored: anInfo = "Stored "; break;
342 case ModuleBase_ModelWidget::ModifiedInPP: anInfo = "ModifiedInPP "; break;
343 case ModuleBase_ModelWidget::ModifiedInViewer: anInfo = "ModifiedInViewer"; break;
344 case ModuleBase_ModelWidget::Reset: anInfo = "Reset "; break;
351 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
352 (const ModuleBase_ModelWidget::ValueState& theState)
354 ValueState aState = myState;
356 if (myState != theState && !myIsValueStateBlocked) {
357 #ifdef DEBUG_VALUE_STATE
358 qDebug(QString("setValueState: previous state = %1,\t new state = %2")
359 .arg(getDebugInfo(myState).c_str())
360 .arg(getDebugInfo(theState).c_str()).toStdString().c_str());
363 emit valueStateChanged(aState);
368 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
370 bool isBlocked = myIsValueStateBlocked;
371 myIsValueStateBlocked = theBlocked;
375 bool ModuleBase_ModelWidget::restoreValue()
377 emit beforeValuesRestored();
378 bool isDone = restoreValueCustom();
379 emit afterValuesRestored();
384 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
386 if (!myFlushUpdateBlocked) {
387 #ifdef DEBUG_WIDGET_INSTANCE
388 qDebug("ModuleBase_ModelWidget::updateObject");
390 ModuleBase_Tools::flushUpdated(theObject);
391 emit objectUpdated();
395 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
397 //blockUpdateViewer(true);
398 #ifdef DEBUG_WIDGET_INSTANCE
399 qDebug("ModuleBase_ModelWidget::moveObject");
402 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
403 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
404 Events_Loop::loop()->flush(anEvent);
406 //blockUpdateViewer(false);
409 bool ModuleBase_ModelWidget::processEnter()
414 bool ModuleBase_ModelWidget::processDelete()
416 // we consider that model objects eats delete key in order to
417 // do nothing by for example symbol delete in line edit or spin box
421 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
423 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
424 if (theEvent->type() == QEvent::FocusIn) {
426 // The following two lines are for debugging purpose only
427 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
428 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
430 Qt::FocusReason aReason = aFocusEvent->reason();
431 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
432 /*aReason == Qt::TabFocusReason ||
433 //aReason == Qt::BacktabFocusReason ||*/
434 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
435 if (aMouseOrKey && getControls().contains(aWidget)) {
436 //if (getControls().contains(aWidget)) {
440 else if (theEvent->type() == QEvent::FocusOut) {
441 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
443 Qt::FocusReason aReason = aFocusEvent->reason();
444 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
445 aReason == Qt::TabFocusReason ||
446 aReason == Qt::BacktabFocusReason ||
447 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
448 if (aMouseOrKey && getControls().contains(aWidget)) {
449 if (getValueState() == ModifiedInPP) {
454 // pass the event on to the parent class
456 return QObject::eventFilter(theObject, theEvent);
459 //**************************************************************
460 void ModuleBase_ModelWidget::onWidgetValuesChanged()
465 //**************************************************************
466 void ModuleBase_ModelWidget::onWidgetValuesModified()
468 setValueState(ModifiedInPP);
471 //**************************************************************
472 QString ModuleBase_ModelWidget::translate(const std::string& theStr) const
474 return ModuleBase_Tools::translate(context(), theStr);
477 //**************************************************************
478 ModuleBase_ModelWidget* ModuleBase_ModelWidget::findModelWidget(ModuleBase_IPropertyPanel* theProp,
481 ModuleBase_ModelWidget* aModelWidget;
482 QObject* aParent = theWidget->parent();
484 aModelWidget = qobject_cast<ModuleBase_ModelWidget*>(aParent);
487 aParent = aParent->parent();