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 bool aCanDisable = true;//Config_PropManager::boolean(SKETCH_TAB_NAME, "disable_input_fields", "true");
102 else if (myIsValueEnabled == Off)
107 void ModuleBase_ModelWidget::processValueState()
109 if (myState == ModifiedInPP || myState == ModifiedInViewer)
113 QString ModuleBase_ModelWidget::getValueStateError() const
115 QString anError = "";
117 ModuleBase_ModelWidget::ValueState aState = getValueState();
118 if (aState != ModuleBase_ModelWidget::Stored) {
119 AttributePtr anAttr = feature()->attribute(attributeID());
121 QString anAttributeName = anAttr->id().c_str();
123 case ModuleBase_ModelWidget::ModifiedInViewer:
124 anError = "Attribute \"" + anAttributeName +
125 "\" is locked by modification value in the viewer.";
127 case ModuleBase_ModelWidget::Reset:
128 anError = "Attribute \"" + anAttributeName + "\" is not initialized.";
130 case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
139 QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
143 if (!feature().get())
146 std::string anAttributeID = attributeID();
147 AttributePtr anAttribute = feature()->attribute(anAttributeID);
148 if (!anAttribute.get())
151 std::string aValidatorID;
152 Events_InfoMessage anErrorMsg;
154 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
155 if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
156 if (anErrorMsg.empty())
157 anErrorMsg = "unknown error.";
158 anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg.messageString();
161 if (!anErrorMsg.empty()) {
162 std::string aStr = Config_Translator::translate(anErrorMsg);
163 std::string aCodec = Config_Translator::codec(anErrorMsg.context());
164 anError = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
167 if (anError.isEmpty() && theValueStateChecked)
168 anError = getValueStateError();
170 anError = translateString(anError);
175 QString ModuleBase_ModelWidget::translateString(const QString& theMsg) const
177 if (!theMsg.isEmpty()) {
178 std::string aContext = feature()->getKind();
179 std::string aStr = Config_Translator::translate(aContext, theMsg.toStdString().c_str());
180 std::string aCodec = Config_Translator::codec(aContext);
181 return QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
187 void ModuleBase_ModelWidget::enableFocusProcessing()
189 QList<QWidget*> aMyControls = getControls();
190 foreach(QWidget* eachControl, aMyControls) {
191 eachControl->setFocusPolicy(Qt::StrongFocus);
192 eachControl->installEventFilter(this);
196 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
198 QList<QWidget*> aWidgetList = getControls();
199 foreach(QWidget* aWidget, aWidgetList) {
200 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
201 // We won't set the effect to QLabels - it looks ugly
203 // If effect is the installed on a different widget, setGraphicsEffect() will
204 // remove the effect from the widget and install it on this widget.
205 // That's why we create a new effect for each widget
206 ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
210 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue,
211 const bool isUpdateFlushed)
213 /// it is possible to give this flag as parameter in storeValue/storeCustomValue
214 /// after debug, it may be corrected
215 myFlushUpdateBlocked = !isUpdateFlushed;
216 myFeature = theFeature;
217 if (theToStoreValue) {
218 /// it is possible that the attribute is filled before the operation is started,
219 /// e.g. by reentrant operation case some attributes are filled by values of
220 /// feature of previous operation, we should not lost them here
221 if (!theFeature->data()->attribute(attributeID())->isInitialized())
224 myFlushUpdateBlocked = false;
227 bool ModuleBase_ModelWidget::focusTo()
229 #ifdef DEBUG_WIDGET_INSTANCE
230 qDebug("ModuleBase_ModelWidget::focusTo");
232 QList<QWidget*> aControls = getControls();
233 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
234 bool isFocusAccepted = false;
235 for (; anIt != aLast && !isFocusAccepted; anIt++) {
236 QWidget* aWidget = *anIt;
237 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
238 ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
239 isFocusAccepted = true;
242 return isFocusAccepted;
245 void ModuleBase_ModelWidget::activate()
247 #ifdef DEBUG_WIDGET_INSTANCE
248 qDebug("ModuleBase_ModelWidget::activate");
250 // the control value is stored to the mode by the focus in on the widget
251 // we need the value is initialized in order to enable the apply button in the property panel.
252 // It should happens in the creation mode only because all fields are filled in the edition mode
253 if (!isEditingMode()) {
254 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
255 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
256 initializeValueByActivate();
259 if (myWidgetValidator)
260 myWidgetValidator->activateFilters(true);
265 void ModuleBase_ModelWidget::deactivate()
267 #ifdef DEBUG_WIDGET_INSTANCE
268 qDebug("ModuleBase_ModelWidget::deactivate");
270 myIsValueStateBlocked = false;
272 if (myWidgetValidator)
273 myWidgetValidator->activateFilters(false);
276 void ModuleBase_ModelWidget::initializeValueByActivate()
278 if (isComputedDefault()) {
279 if (myFeature->compute(myAttributeID)) {
288 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
290 QWidget* aControl = 0;
292 QList<QWidget*> aControls = getControls();
293 int aSize = aControls.size();
296 for (int i = 0; i < aSize && !aControl; i++) {
297 if (aControls[i]->focusPolicy() != Qt::NoFocus)
298 aControl = aControls[i];
302 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
303 if (aControls[i]->focusPolicy() != Qt::NoFocus)
304 aControl = aControls[i];
310 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
312 myDefaultValue = theValue;
315 bool ModuleBase_ModelWidget::storeValue()
317 setValueState(Stored);
319 emit beforeValuesChanged();
320 bool isDone = storeValueCustom();
321 emit afterValuesChanged();
325 #ifdef DEBUG_VALUE_STATE
326 std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
330 case ModuleBase_ModelWidget::Stored: anInfo = "Stored "; break;
331 case ModuleBase_ModelWidget::ModifiedInPP: anInfo = "ModifiedInPP "; break;
332 case ModuleBase_ModelWidget::ModifiedInViewer: anInfo = "ModifiedInViewer"; break;
333 case ModuleBase_ModelWidget::Reset: anInfo = "Reset "; break;
340 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
341 (const ModuleBase_ModelWidget::ValueState& theState)
343 ValueState aState = myState;
345 if (myState != theState && !myIsValueStateBlocked) {
346 #ifdef DEBUG_VALUE_STATE
347 qDebug(QString("setValueState: previous state = %1,\t new state = %2")
348 .arg(getDebugInfo(myState).c_str())
349 .arg(getDebugInfo(theState).c_str()).toStdString().c_str());
352 emit valueStateChanged(aState);
357 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
359 bool isBlocked = myIsValueStateBlocked;
360 myIsValueStateBlocked = theBlocked;
364 bool ModuleBase_ModelWidget::restoreValue()
366 emit beforeValuesRestored();
367 bool isDone = restoreValueCustom();
368 emit afterValuesRestored();
373 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
375 if (!myFlushUpdateBlocked) {
376 #ifdef DEBUG_WIDGET_INSTANCE
377 qDebug("ModuleBase_ModelWidget::updateObject");
379 ModuleBase_Tools::flushUpdated(theObject);
380 emit objectUpdated();
384 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
386 //blockUpdateViewer(true);
387 #ifdef DEBUG_WIDGET_INSTANCE
388 qDebug("ModuleBase_ModelWidget::moveObject");
391 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
392 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
393 Events_Loop::loop()->flush(anEvent);
395 //blockUpdateViewer(false);
398 bool ModuleBase_ModelWidget::processEnter()
403 bool ModuleBase_ModelWidget::processDelete()
405 // we consider that model objects eats delete key in order to
406 // do nothing by for example symbol delete in line edit or spin box
410 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
412 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
413 if (theEvent->type() == QEvent::FocusIn) {
415 // The following two lines are for debugging purpose only
416 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
417 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
419 if (getControls().contains(aWidget)) {
420 emit focusInWidget(this);
423 else if (theEvent->type() == QEvent::FocusOut) {
424 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
426 Qt::FocusReason aReason = aFocusEvent->reason();
427 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
428 aReason == Qt::TabFocusReason ||
429 aReason == Qt::BacktabFocusReason ||
430 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
431 if (aMouseOrKey && getControls().contains(aWidget)) {
432 if (getValueState() == ModifiedInPP) {
437 // pass the event on to the parent class
439 return QObject::eventFilter(theObject, theEvent);
442 //**************************************************************
443 void ModuleBase_ModelWidget::onWidgetValuesChanged()
448 //**************************************************************
449 void ModuleBase_ModelWidget::onWidgetValuesModified()
451 setValueState(ModifiedInPP);