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