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
36 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
37 const Config_WidgetAPI* theData)
41 myIsValueStateBlocked(false),
42 myFlushUpdateBlocked(false),
45 #ifdef DEBUG_WIDGET_INSTANCE
46 qDebug("ModuleBase_ModelWidget::ModuleBase_ModelWidget");
49 myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
51 myDefaultValue = theData->getProperty(ATTR_DEFAULT);
52 myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
53 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
54 myAttributeID = theData ? theData->widgetId() : "";
55 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
57 myIsValueEnabled = On; // not defined or "true"
58 std::string anEnableValue = theData->getProperty(DOUBLE_WDG_ENABLE_VALUE);
59 if (anEnableValue == "false")
60 myIsValueEnabled = Off;
61 if (anEnableValue == DOUBLE_WDG_ENABLE_VALUE_BY_PREFERENCES)
62 myIsValueEnabled = DefinedInPreferences;
64 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
65 connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
68 ModuleBase_ModelWidget::~ModuleBase_ModelWidget()
70 #ifdef DEBUG_WIDGET_INSTANCE
71 qDebug("ModuleBase_ModelWidget::~ModuleBase_ModelWidget");
75 bool ModuleBase_ModelWidget::reset()
77 bool aResult = resetCustom();
84 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
86 return theObject->data()->attribute(attributeID())->isInitialized();
89 bool ModuleBase_ModelWidget::isValueEnabled() const
91 bool anEnabled = true;
92 if (myIsValueEnabled == DefinedInPreferences) {
93 bool aCanDisable = Config_PropManager::boolean(SKETCH_TAB_NAME, "disable_input_fields", "true");
97 else if (myIsValueEnabled == Off)
102 void ModuleBase_ModelWidget::processValueState()
104 if (myState == ModifiedInPP || myState == ModifiedInViewer)
108 QString ModuleBase_ModelWidget::getValueStateError() const
110 QString anError = "";
112 ModuleBase_ModelWidget::ValueState aState = getValueState();
113 if (aState != ModuleBase_ModelWidget::Stored) {
114 AttributePtr anAttr = feature()->attribute(attributeID());
116 QString anAttributeName = anAttr->id().c_str();
118 case ModuleBase_ModelWidget::ModifiedInViewer:
119 anError = "Attribute \"" + anAttributeName +
120 "\" is locked by modification value in the viewer.";
122 case ModuleBase_ModelWidget::Reset:
123 anError = "Attribute \"" + anAttributeName + "\" is not initialized.";
125 case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
134 QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
138 if (!feature().get())
141 std::string anAttributeID = attributeID();
142 AttributePtr anAttribute = feature()->attribute(anAttributeID);
143 if (!anAttribute.get())
146 std::string aValidatorID;
147 Events_InfoMessage anErrorMsg;
149 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
150 if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
151 if (anErrorMsg.empty())
152 anErrorMsg = "unknown error.";
153 anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg.messageString();
156 if (!anErrorMsg.empty()) {
157 std::string aStr = Config_Translator::translate(anErrorMsg);
158 std::string aCodec = Config_Translator::codec(anErrorMsg.context());
159 anError = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
162 if (anError.isEmpty() && theValueStateChecked)
163 anError = getValueStateError();
165 anError = translateString(anError);
170 QString ModuleBase_ModelWidget::translateString(const QString& theMsg) const
172 if (!theMsg.isEmpty()) {
173 std::string aContext = feature()->getKind();
174 std::string aStr = Config_Translator::translate(aContext, theMsg.toStdString().c_str());
175 std::string aCodec = Config_Translator::codec(aContext);
176 return QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
182 void ModuleBase_ModelWidget::enableFocusProcessing()
184 QList<QWidget*> aMyControls = getControls();
185 foreach(QWidget* eachControl, aMyControls) {
186 eachControl->setFocusPolicy(Qt::StrongFocus);
187 eachControl->installEventFilter(this);
191 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
193 QList<QWidget*> aWidgetList = getControls();
194 foreach(QWidget* aWidget, aWidgetList) {
195 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
196 // We won't set the effect to QLabels - it looks ugly
198 // If effect is the installed on a different widget, setGraphicsEffect() will
199 // remove the effect from the widget and install it on this widget.
200 // That's why we create a new effect for each widget
201 ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
205 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue,
206 const bool isUpdateFlushed)
208 /// it is possible to give this flag as parameter in storeValue/storeCustomValue
209 /// after debug, it may be corrected
210 myFlushUpdateBlocked = !isUpdateFlushed;
211 myFeature = theFeature;
212 if (theToStoreValue) {
213 /// it is possible that the attribute is filled before the operation is started,
214 /// e.g. by reentrant operation case some attributes are filled by values of
215 /// feature of previous operation, we should not lost them here
216 if (!theFeature->data()->attribute(attributeID())->isInitialized())
219 myFlushUpdateBlocked = false;
222 bool ModuleBase_ModelWidget::focusTo()
224 #ifdef DEBUG_WIDGET_INSTANCE
225 qDebug("ModuleBase_ModelWidget::focusTo");
227 QList<QWidget*> aControls = getControls();
228 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
229 bool isFocusAccepted = false;
230 for (; anIt != aLast && !isFocusAccepted; anIt++) {
231 QWidget* aWidget = *anIt;
232 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
233 ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
234 isFocusAccepted = true;
237 return isFocusAccepted;
240 void ModuleBase_ModelWidget::activate()
242 #ifdef DEBUG_WIDGET_INSTANCE
243 qDebug("ModuleBase_ModelWidget::activate");
245 // the control value is stored to the mode by the focus in on the widget
246 // we need the value is initialized in order to enable the apply button in the property panel.
247 // It should happens in the creation mode only because all fields are filled in the edition mode
248 if (!isEditingMode()) {
249 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
250 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
251 initializeValueByActivate();
254 if (myWidgetValidator)
255 myWidgetValidator->activateFilters(true);
260 void ModuleBase_ModelWidget::deactivate()
262 #ifdef DEBUG_WIDGET_INSTANCE
263 qDebug("ModuleBase_ModelWidget::deactivate");
265 myIsValueStateBlocked = false;
267 if (myWidgetValidator)
268 myWidgetValidator->activateFilters(false);
271 void ModuleBase_ModelWidget::initializeValueByActivate()
273 if (isComputedDefault()) {
274 if (myFeature->compute(myAttributeID)) {
283 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
285 QWidget* aControl = 0;
287 QList<QWidget*> aControls = getControls();
288 int aSize = aControls.size();
291 for (int i = 0; i < aSize && !aControl; i++) {
292 if (aControls[i]->focusPolicy() != Qt::NoFocus)
293 aControl = aControls[i];
297 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
298 if (aControls[i]->focusPolicy() != Qt::NoFocus)
299 aControl = aControls[i];
305 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
307 myDefaultValue = theValue;
310 bool ModuleBase_ModelWidget::storeValue()
312 setValueState(Stored);
314 emit beforeValuesChanged();
315 bool isDone = storeValueCustom();
316 emit afterValuesChanged();
320 #ifdef DEBUG_VALUE_STATE
321 std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
325 case ModuleBase_ModelWidget::Stored: anInfo = "Stored "; break;
326 case ModuleBase_ModelWidget::ModifiedInPP: anInfo = "ModifiedInPP "; break;
327 case ModuleBase_ModelWidget::ModifiedInViewer: anInfo = "ModifiedInViewer"; break;
328 case ModuleBase_ModelWidget::Reset: anInfo = "Reset "; break;
335 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
336 (const ModuleBase_ModelWidget::ValueState& theState)
338 ValueState aState = myState;
340 if (myState != theState && !myIsValueStateBlocked) {
341 #ifdef DEBUG_VALUE_STATE
342 qDebug(QString("setValueState: previous state = %1,\t new state = %2")
343 .arg(getDebugInfo(myState).c_str())
344 .arg(getDebugInfo(theState).c_str()).toStdString().c_str());
347 emit valueStateChanged(aState);
352 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
354 bool isBlocked = myIsValueStateBlocked;
355 myIsValueStateBlocked = theBlocked;
359 bool ModuleBase_ModelWidget::restoreValue()
361 emit beforeValuesRestored();
362 bool isDone = restoreValueCustom();
363 emit afterValuesRestored();
368 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
370 if (!myFlushUpdateBlocked) {
371 #ifdef DEBUG_WIDGET_INSTANCE
372 qDebug("ModuleBase_ModelWidget::updateObject");
374 ModuleBase_Tools::flushUpdated(theObject);
375 emit objectUpdated();
379 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
381 //blockUpdateViewer(true);
382 #ifdef DEBUG_WIDGET_INSTANCE
383 qDebug("ModuleBase_ModelWidget::moveObject");
386 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
387 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
388 Events_Loop::loop()->flush(anEvent);
390 //blockUpdateViewer(false);
393 bool ModuleBase_ModelWidget::processEnter()
398 bool ModuleBase_ModelWidget::processDelete()
400 // we consider that model objects eats delete key in order to
401 // do nothing by for example symbol delete in line edit or spin box
405 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
407 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
408 if (theEvent->type() == QEvent::FocusIn) {
410 // The following two lines are for debugging purpose only
411 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
412 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
414 if (getControls().contains(aWidget)) {
415 emit focusInWidget(this);
418 else if (theEvent->type() == QEvent::FocusOut) {
419 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
421 Qt::FocusReason aReason = aFocusEvent->reason();
422 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
423 aReason == Qt::TabFocusReason ||
424 aReason == Qt::BacktabFocusReason ||
425 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
426 if (aMouseOrKey && getControls().contains(aWidget)) {
427 if (getValueState() == ModifiedInPP) {
432 // pass the event on to the parent class
434 return QObject::eventFilter(theObject, theEvent);
437 //**************************************************************
438 void ModuleBase_ModelWidget::onWidgetValuesChanged()
443 //**************************************************************
444 void ModuleBase_ModelWidget::onWidgetValuesModified()
446 setValueState(ModifiedInPP);