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 myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
52 myDefaultValue = theData->getProperty(ATTR_DEFAULT);
53 myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
54 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
55 myAttributeID = theData ? theData->widgetId() : "";
56 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
58 myIsValueEnabled = On; // not defined or "true"
59 std::string anEnableValue = theData->getProperty(DOUBLE_WDG_ENABLE_VALUE);
60 if (anEnableValue == "false")
61 myIsValueEnabled = Off;
62 if (anEnableValue == DOUBLE_WDG_ENABLE_VALUE_BY_PREFERENCES)
63 myIsValueEnabled = DefinedInPreferences;
65 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
66 connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
69 ModuleBase_ModelWidget::~ModuleBase_ModelWidget()
71 #ifdef DEBUG_WIDGET_INSTANCE
72 qDebug("ModuleBase_ModelWidget::~ModuleBase_ModelWidget");
76 bool ModuleBase_ModelWidget::reset()
78 bool aResult = resetCustom();
85 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
87 return theObject->data()->attribute(attributeID())->isInitialized();
90 bool ModuleBase_ModelWidget::isValueEnabled() const
92 bool anEnabled = true;
93 if (myIsValueEnabled == DefinedInPreferences) {
94 #ifdef DEBUG_ENABLE_SKETCH_INPUT_FIELDS
95 bool aCanDisable = false;
97 //Config_PropManager::boolean(SKETCH_TAB_NAME, "disable_input_fields", "true");
98 bool aCanDisable = true;
103 else if (myIsValueEnabled == Off)
108 void ModuleBase_ModelWidget::processValueState()
110 if (myState == ModifiedInPP || myState == ModifiedInViewer)
114 QString ModuleBase_ModelWidget::getValueStateError() const
116 QString anError = "";
118 ModuleBase_ModelWidget::ValueState aState = getValueState();
119 if (aState != ModuleBase_ModelWidget::Stored) {
120 AttributePtr anAttr = feature()->attribute(attributeID());
122 QString anAttributeName = anAttr->id().c_str();
124 case ModuleBase_ModelWidget::ModifiedInViewer:
125 anError = "Attribute \"" + anAttributeName +
126 "\" is locked by modification value in the viewer.";
128 case ModuleBase_ModelWidget::Reset:
129 anError = "Attribute \"" + anAttributeName + "\" is not initialized.";
131 case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
140 QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
144 if (!feature().get())
147 std::string anAttributeID = attributeID();
148 AttributePtr anAttribute = feature()->attribute(anAttributeID);
149 if (!anAttribute.get())
152 std::string aValidatorID;
153 Events_InfoMessage anErrorMsg;
155 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
156 if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
157 if (anErrorMsg.empty())
158 anErrorMsg = "unknown error.";
159 anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg.messageString();
162 if (!anErrorMsg.empty()) {
163 std::string aStr = Config_Translator::translate(anErrorMsg);
164 std::string aCodec = Config_Translator::codec(anErrorMsg.context());
165 anError = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
168 if (anError.isEmpty() && theValueStateChecked)
169 anError = getValueStateError();
171 anError = translateString(anError);
176 QString ModuleBase_ModelWidget::translateString(const QString& theMsg) const
178 if (!theMsg.isEmpty()) {
179 std::string aContext = feature()->getKind();
180 std::string aStr = Config_Translator::translate(aContext, theMsg.toStdString().c_str());
181 std::string aCodec = Config_Translator::codec(aContext);
182 return QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
188 void ModuleBase_ModelWidget::enableFocusProcessing()
190 QList<QWidget*> aMyControls = getControls();
191 foreach(QWidget* eachControl, aMyControls) {
192 eachControl->setFocusPolicy(Qt::StrongFocus);
193 eachControl->installEventFilter(this);
197 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
199 QList<QWidget*> aWidgetList = getControls();
200 foreach(QWidget* aWidget, aWidgetList) {
201 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
202 // We won't set the effect to QLabels - it looks ugly
204 // If effect is the installed on a different widget, setGraphicsEffect() will
205 // remove the effect from the widget and install it on this widget.
206 // That's why we create a new effect for each widget
207 ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
211 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue,
212 const bool isUpdateFlushed)
214 /// it is possible to give this flag as parameter in storeValue/storeCustomValue
215 /// after debug, it may be corrected
216 myFlushUpdateBlocked = !isUpdateFlushed;
217 myFeature = theFeature;
218 if (theToStoreValue) {
219 /// it is possible that the attribute is filled before the operation is started,
220 /// e.g. by reentrant operation case some attributes are filled by values of
221 /// feature of previous operation, we should not lost them here
222 if (!theFeature->data()->attribute(attributeID())->isInitialized())
225 myFlushUpdateBlocked = false;
228 bool ModuleBase_ModelWidget::focusTo()
230 #ifdef DEBUG_WIDGET_INSTANCE
231 qDebug("ModuleBase_ModelWidget::focusTo");
233 QList<QWidget*> aControls = getControls();
234 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
235 bool isFocusAccepted = false;
236 for (; anIt != aLast && !isFocusAccepted; anIt++) {
237 QWidget* aWidget = *anIt;
238 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
239 ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
240 isFocusAccepted = true;
243 return isFocusAccepted;
246 void ModuleBase_ModelWidget::activate()
248 #ifdef DEBUG_WIDGET_INSTANCE
249 qDebug("ModuleBase_ModelWidget::activate");
251 // the control value is stored to the mode by the focus in on the widget
252 // we need the value is initialized in order to enable the apply button in the property panel.
253 // It should happens in the creation mode only because all fields are filled in the edition mode
254 if (!isEditingMode()) {
255 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
256 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
257 initializeValueByActivate();
260 if (myWidgetValidator)
261 myWidgetValidator->activateFilters(true);
266 void ModuleBase_ModelWidget::deactivate()
268 #ifdef DEBUG_WIDGET_INSTANCE
269 qDebug("ModuleBase_ModelWidget::deactivate");
271 myIsValueStateBlocked = false;
273 if (myWidgetValidator)
274 myWidgetValidator->activateFilters(false);
277 void ModuleBase_ModelWidget::initializeValueByActivate()
279 if (isComputedDefault()) {
280 if (myFeature->compute(myAttributeID)) {
289 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
291 QWidget* aControl = 0;
293 QList<QWidget*> aControls = getControls();
294 int aSize = aControls.size();
297 for (int i = 0; i < aSize && !aControl; i++) {
298 if (aControls[i]->focusPolicy() != Qt::NoFocus)
299 aControl = aControls[i];
303 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
304 if (aControls[i]->focusPolicy() != Qt::NoFocus)
305 aControl = aControls[i];
311 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
313 myDefaultValue = theValue;
316 bool ModuleBase_ModelWidget::storeValue()
318 setValueState(Stored);
320 emit beforeValuesChanged();
321 bool isDone = storeValueCustom();
322 emit afterValuesChanged();
326 #ifdef DEBUG_VALUE_STATE
327 std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
331 case ModuleBase_ModelWidget::Stored: anInfo = "Stored "; break;
332 case ModuleBase_ModelWidget::ModifiedInPP: anInfo = "ModifiedInPP "; break;
333 case ModuleBase_ModelWidget::ModifiedInViewer: anInfo = "ModifiedInViewer"; break;
334 case ModuleBase_ModelWidget::Reset: anInfo = "Reset "; break;
341 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
342 (const ModuleBase_ModelWidget::ValueState& theState)
344 ValueState aState = myState;
346 if (myState != theState && !myIsValueStateBlocked) {
347 #ifdef DEBUG_VALUE_STATE
348 qDebug(QString("setValueState: previous state = %1,\t new state = %2")
349 .arg(getDebugInfo(myState).c_str())
350 .arg(getDebugInfo(theState).c_str()).toStdString().c_str());
353 emit valueStateChanged(aState);
358 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
360 bool isBlocked = myIsValueStateBlocked;
361 myIsValueStateBlocked = theBlocked;
365 bool ModuleBase_ModelWidget::restoreValue()
367 emit beforeValuesRestored();
368 bool isDone = restoreValueCustom();
369 emit afterValuesRestored();
374 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
376 if (!myFlushUpdateBlocked) {
377 #ifdef DEBUG_WIDGET_INSTANCE
378 qDebug("ModuleBase_ModelWidget::updateObject");
380 ModuleBase_Tools::flushUpdated(theObject);
381 emit objectUpdated();
385 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
387 //blockUpdateViewer(true);
388 #ifdef DEBUG_WIDGET_INSTANCE
389 qDebug("ModuleBase_ModelWidget::moveObject");
392 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
393 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
394 Events_Loop::loop()->flush(anEvent);
396 //blockUpdateViewer(false);
399 bool ModuleBase_ModelWidget::processEnter()
404 bool ModuleBase_ModelWidget::processDelete()
406 // we consider that model objects eats delete key in order to
407 // do nothing by for example symbol delete in line edit or spin box
411 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
413 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
414 if (theEvent->type() == QEvent::FocusIn) {
416 // The following two lines are for debugging purpose only
417 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
418 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
420 if (getControls().contains(aWidget)) {
421 emit focusInWidget(this);
424 else if (theEvent->type() == QEvent::FocusOut) {
425 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
427 Qt::FocusReason aReason = aFocusEvent->reason();
428 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
429 aReason == Qt::TabFocusReason ||
430 aReason == Qt::BacktabFocusReason ||
431 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
432 if (aMouseOrKey && getControls().contains(aWidget)) {
433 if (getValueState() == ModifiedInPP) {
438 // pass the event on to the parent class
440 return QObject::eventFilter(theObject, theEvent);
443 //**************************************************************
444 void ModuleBase_ModelWidget::onWidgetValuesChanged()
449 //**************************************************************
450 void ModuleBase_ModelWidget::onWidgetValuesModified()
452 setValueState(ModifiedInPP);