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 = theData->getBooleanAttribute(DOUBLE_WDG_ENABLE_VALUE, true);
59 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
60 connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
63 ModuleBase_ModelWidget::~ModuleBase_ModelWidget()
65 #ifdef DEBUG_WIDGET_INSTANCE
66 qDebug("ModuleBase_ModelWidget::~ModuleBase_ModelWidget");
70 bool ModuleBase_ModelWidget::reset()
72 bool aResult = resetCustom();
79 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
81 return theObject->data()->attribute(attributeID())->isInitialized();
84 bool ModuleBase_ModelWidget::isValueEnabled() const
86 bool anEnabled = true;
87 bool aCanDisable = Config_PropManager::boolean("Sketch planes", "disable_input_fields", "true");
89 anEnabled = myIsValueEnabled;
93 void ModuleBase_ModelWidget::processValueState()
95 if (myState == ModifiedInPP || myState == ModifiedInViewer)
99 QString ModuleBase_ModelWidget::getValueStateError() const
101 QString anError = "";
103 ModuleBase_ModelWidget::ValueState aState = getValueState();
104 if (aState != ModuleBase_ModelWidget::Stored) {
105 AttributePtr anAttr = feature()->attribute(attributeID());
107 QString anAttributeName = anAttr->id().c_str();
109 case ModuleBase_ModelWidget::ModifiedInViewer:
110 anError = "Attribute \"" + anAttributeName +
111 "\" is locked by modification value in the viewer.";
113 case ModuleBase_ModelWidget::Reset:
114 anError = "Attribute \"" + anAttributeName + "\" is not initialized.";
116 case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
125 QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
129 if (!feature().get())
132 std::string anAttributeID = attributeID();
133 AttributePtr anAttribute = feature()->attribute(anAttributeID);
134 if (!anAttribute.get())
137 std::string aValidatorID;
138 Events_InfoMessage anErrorMsg;
140 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
141 if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
142 if (anErrorMsg.empty())
143 anErrorMsg = "unknown error.";
144 anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg.messageString();
147 if (!anErrorMsg.empty()) {
148 std::string aStr = Config_Translator::translate(anErrorMsg);
149 std::string aCodec = Config_Translator::codec(anErrorMsg.context());
150 anError = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
153 if (anError.isEmpty() && theValueStateChecked)
154 anError = getValueStateError();
156 anError = translateString(anError);
161 QString ModuleBase_ModelWidget::translateString(const QString& theMsg) const
163 if (!theMsg.isEmpty()) {
164 std::string aContext = feature()->getKind();
165 std::string aStr = Config_Translator::translate(aContext, theMsg.toStdString().c_str());
166 std::string aCodec = Config_Translator::codec(aContext);
167 return QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
173 void ModuleBase_ModelWidget::enableFocusProcessing()
175 QList<QWidget*> aMyControls = getControls();
176 foreach(QWidget* eachControl, aMyControls) {
177 eachControl->setFocusPolicy(Qt::StrongFocus);
178 eachControl->installEventFilter(this);
182 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
184 QList<QWidget*> aWidgetList = getControls();
185 foreach(QWidget* aWidget, aWidgetList) {
186 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
187 // We won't set the effect to QLabels - it looks ugly
189 // If effect is the installed on a different widget, setGraphicsEffect() will
190 // remove the effect from the widget and install it on this widget.
191 // That's why we create a new effect for each widget
192 ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
196 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue,
197 const bool isUpdateFlushed)
199 /// it is possible to give this flag as parameter in storeValue/storeCustomValue
200 /// after debug, it may be corrected
201 myFlushUpdateBlocked = !isUpdateFlushed;
202 myFeature = theFeature;
205 myFlushUpdateBlocked = false;
208 bool ModuleBase_ModelWidget::focusTo()
210 #ifdef DEBUG_WIDGET_INSTANCE
211 qDebug("ModuleBase_ModelWidget::focusTo");
213 QList<QWidget*> aControls = getControls();
214 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
215 bool isFocusAccepted = false;
216 for (; anIt != aLast && !isFocusAccepted; anIt++) {
217 QWidget* aWidget = *anIt;
218 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
219 ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
220 isFocusAccepted = true;
223 return isFocusAccepted;
226 void ModuleBase_ModelWidget::activate()
228 #ifdef DEBUG_WIDGET_INSTANCE
229 qDebug("ModuleBase_ModelWidget::activate");
231 // the control value is stored to the mode by the focus in on the widget
232 // we need the value is initialized in order to enable the apply button in the property panel.
233 // It should happens in the creation mode only because all fields are filled in the edition mode
234 if (!isEditingMode()) {
235 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
236 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
237 initializeValueByActivate();
240 if (myWidgetValidator)
241 myWidgetValidator->activateFilters(true);
246 void ModuleBase_ModelWidget::deactivate()
248 #ifdef DEBUG_WIDGET_INSTANCE
249 qDebug("ModuleBase_ModelWidget::deactivate");
251 myIsValueStateBlocked = false;
253 if (myWidgetValidator)
254 myWidgetValidator->activateFilters(false);
257 void ModuleBase_ModelWidget::initializeValueByActivate()
259 if (isComputedDefault()) {
260 if (myFeature->compute(myAttributeID)) {
269 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
271 QWidget* aControl = 0;
273 QList<QWidget*> aControls = getControls();
274 int aSize = aControls.size();
277 for (int i = 0; i < aSize && !aControl; i++) {
278 if (aControls[i]->focusPolicy() != Qt::NoFocus)
279 aControl = aControls[i];
283 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
284 if (aControls[i]->focusPolicy() != Qt::NoFocus)
285 aControl = aControls[i];
291 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
293 myDefaultValue = theValue;
296 bool ModuleBase_ModelWidget::storeValue()
298 setValueState(Stored);
300 emit beforeValuesChanged();
301 bool isDone = storeValueCustom();
302 emit afterValuesChanged();
306 #ifdef DEBUG_VALUE_STATE
307 std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
311 case ModuleBase_ModelWidget::Stored: anInfo = "Stored "; break;
312 case ModuleBase_ModelWidget::ModifiedInPP: anInfo = "ModifiedInPP "; break;
313 case ModuleBase_ModelWidget::ModifiedInViewer: anInfo = "ModifiedInViewer"; break;
314 case ModuleBase_ModelWidget::Reset: anInfo = "Reset "; break;
321 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
322 (const ModuleBase_ModelWidget::ValueState& theState)
324 ValueState aState = myState;
326 if (myState != theState && !myIsValueStateBlocked) {
327 #ifdef DEBUG_VALUE_STATE
328 qDebug(QString("setValueState: previous state = %1,\t new state = %2")
329 .arg(getDebugInfo(myState).c_str())
330 .arg(getDebugInfo(theState).c_str()).toStdString().c_str());
333 emit valueStateChanged(aState);
338 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
340 bool isBlocked = myIsValueStateBlocked;
341 myIsValueStateBlocked = theBlocked;
345 bool ModuleBase_ModelWidget::restoreValue()
347 emit beforeValuesRestored();
348 bool isDone = restoreValueCustom();
349 emit afterValuesRestored();
354 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
356 if (!myFlushUpdateBlocked) {
357 #ifdef DEBUG_WIDGET_INSTANCE
358 qDebug("ModuleBase_ModelWidget::updateObject");
360 ModuleBase_Tools::flushUpdated(theObject);
361 emit objectUpdated();
365 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
367 //blockUpdateViewer(true);
368 #ifdef DEBUG_WIDGET_INSTANCE
369 qDebug("ModuleBase_ModelWidget::moveObject");
372 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
373 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
374 Events_Loop::loop()->flush(anEvent);
376 //blockUpdateViewer(false);
379 bool ModuleBase_ModelWidget::processEnter()
384 bool ModuleBase_ModelWidget::processDelete()
386 // we consider that model objects eats delete key in order to
387 // do nothing by for example symbol delete in line edit or spin box
391 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
393 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
394 if (theEvent->type() == QEvent::FocusIn) {
396 // The following two lines are for debugging purpose only
397 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
398 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
400 if (getControls().contains(aWidget)) {
401 emit focusInWidget(this);
404 else if (theEvent->type() == QEvent::FocusOut) {
405 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
407 Qt::FocusReason aReason = aFocusEvent->reason();
408 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
409 aReason == Qt::TabFocusReason ||
410 aReason == Qt::BacktabFocusReason ||
411 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
412 if (aMouseOrKey && getControls().contains(aWidget)) {
413 if (getValueState() == ModifiedInPP) {
418 // pass the event on to the parent class
420 return QObject::eventFilter(theObject, theEvent);
423 //**************************************************************
424 void ModuleBase_ModelWidget::onWidgetValuesChanged()
429 //**************************************************************
430 void ModuleBase_ModelWidget::onWidgetValuesModified()
432 setValueState(ModifiedInPP);