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 <ModelAPI_Data.h>
13 #include <ModelAPI_Attribute.h>
14 #include <ModelAPI_Events.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_Validator.h>
18 #include <Config_Keywords.h>
19 #include <Config_WidgetAPI.h>
21 #include <Events_Loop.h>
25 #include <QFocusEvent>
27 //#define DEBUG_VALUE_STATE
29 //#define DEBUG_WIDGET_INSTANCE
31 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
32 const Config_WidgetAPI* theData)
36 myIsValueStateBlocked(false),
37 myFlushUpdateBlocked(false),
40 #ifdef DEBUG_WIDGET_INSTANCE
41 qDebug("ModuleBase_ModelWidget::ModuleBase_ModelWidget");
44 myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
46 myDefaultValue = theData->getProperty(ATTR_DEFAULT);
47 myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
48 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
49 myAttributeID = theData ? theData->widgetId() : "";
50 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
52 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
53 connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
56 ModuleBase_ModelWidget::~ModuleBase_ModelWidget()
58 #ifdef DEBUG_WIDGET_INSTANCE
59 qDebug("ModuleBase_ModelWidget::~ModuleBase_ModelWidget");
63 bool ModuleBase_ModelWidget::reset()
65 bool aResult = resetCustom();
72 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
74 return theObject->data()->attribute(attributeID())->isInitialized();
77 void ModuleBase_ModelWidget::processValueState()
79 if (myState == ModifiedInPP || myState == ModifiedInViewer)
83 QString ModuleBase_ModelWidget::getValueStateError() const
87 ModuleBase_ModelWidget::ValueState aState = getValueState();
88 if (aState != ModuleBase_ModelWidget::Stored) {
89 AttributePtr anAttr = feature()->attribute(attributeID());
91 QString anAttributeName = anAttr->id().c_str();
93 case ModuleBase_ModelWidget::ModifiedInViewer:
94 anError = "Attribute \"" + anAttributeName +
95 "\" is locked by modification value in the viewer.";
97 case ModuleBase_ModelWidget::Reset:
98 anError = "Attribute \"" + anAttributeName + "\" is not initialized.";
100 case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
109 QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
113 if (!feature().get())
116 std::string anAttributeID = attributeID();
117 AttributePtr anAttribute = feature()->attribute(anAttributeID);
118 if (!anAttribute.get())
121 std::string aValidatorID;
122 std::string anErrorMsg;
124 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
125 if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
126 if (anErrorMsg.empty())
127 anErrorMsg = "unknown error.";
128 anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg;
131 anError = QString::fromStdString(anErrorMsg);
132 if (anError.isEmpty() && theValueStateChecked)
133 anError = getValueStateError();
138 void ModuleBase_ModelWidget::enableFocusProcessing()
140 QList<QWidget*> aMyControls = getControls();
141 foreach(QWidget* eachControl, aMyControls) {
142 eachControl->setFocusPolicy(Qt::StrongFocus);
143 eachControl->installEventFilter(this);
147 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
149 QList<QWidget*> aWidgetList = getControls();
150 foreach(QWidget* aWidget, aWidgetList) {
151 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
152 // We won't set the effect to QLabels - it looks ugly
154 // If effect is the installed on a different widget, setGraphicsEffect() will
155 // remove the effect from the widget and install it on this widget.
156 // That's why we create a new effect for each widget
157 ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
161 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue,
162 const bool isUpdateFlushed)
164 /// it is possible to give this flag as parameter in storeValue/storeCustomValue
165 /// after debug, it may be corrected
166 myFlushUpdateBlocked = !isUpdateFlushed;
167 myFeature = theFeature;
170 myFlushUpdateBlocked = false;
173 bool ModuleBase_ModelWidget::focusTo()
175 #ifdef DEBUG_WIDGET_INSTANCE
176 qDebug("ModuleBase_ModelWidget::focusTo");
178 QList<QWidget*> aControls = getControls();
179 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
180 bool isFocusAccepted = false;
181 for (; anIt != aLast && !isFocusAccepted; anIt++) {
182 QWidget* aWidget = *anIt;
183 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
184 ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
185 isFocusAccepted = true;
188 return isFocusAccepted;
191 void ModuleBase_ModelWidget::activate()
193 #ifdef DEBUG_WIDGET_INSTANCE
194 qDebug("ModuleBase_ModelWidget::activate");
196 // the control value is stored to the mode by the focus in on the widget
197 // we need the value is initialized in order to enable the apply button in the property panel.
198 // It should happens in the creation mode only because all fields are filled in the edition mode
199 if (!isEditingMode()) {
200 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
201 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
202 initializeValueByActivate();
205 if (myWidgetValidator)
206 myWidgetValidator->activateFilters(true);
211 void ModuleBase_ModelWidget::deactivate()
213 #ifdef DEBUG_WIDGET_INSTANCE
214 qDebug("ModuleBase_ModelWidget::deactivate");
216 myIsValueStateBlocked = false;
218 if (myWidgetValidator)
219 myWidgetValidator->activateFilters(false);
222 void ModuleBase_ModelWidget::initializeValueByActivate()
224 if (isComputedDefault()) {
225 if (myFeature->compute(myAttributeID)) {
234 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
236 QWidget* aControl = 0;
238 QList<QWidget*> aControls = getControls();
239 int aSize = aControls.size();
242 for (int i = 0; i < aSize && !aControl; i++) {
243 if (aControls[i]->focusPolicy() != Qt::NoFocus)
244 aControl = aControls[i];
248 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
249 if (aControls[i]->focusPolicy() != Qt::NoFocus)
250 aControl = aControls[i];
256 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
258 myDefaultValue = theValue;
261 bool ModuleBase_ModelWidget::storeValue()
263 setValueState(Stored);
265 emit beforeValuesChanged();
266 bool isDone = storeValueCustom();
267 emit afterValuesChanged();
271 #ifdef DEBUG_VALUE_STATE
272 std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
276 case ModuleBase_ModelWidget::Stored: anInfo = "Stored "; break;
277 case ModuleBase_ModelWidget::ModifiedInPP: anInfo = "ModifiedInPP "; break;
278 case ModuleBase_ModelWidget::ModifiedInViewer: anInfo = "ModifiedInViewer"; break;
279 case ModuleBase_ModelWidget::Reset: anInfo = "Reset "; break;
286 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
287 (const ModuleBase_ModelWidget::ValueState& theState)
289 ValueState aState = myState;
291 if (myState != theState && !myIsValueStateBlocked) {
292 #ifdef DEBUG_VALUE_STATE
293 qDebug(QString("setValueState: previous state = %1,\t new state = %2")
294 .arg(getDebugInfo(myState).c_str())
295 .arg(getDebugInfo(theState).c_str()).toStdString().c_str());
298 emit valueStateChanged(aState);
303 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
305 bool isBlocked = myIsValueStateBlocked;
306 myIsValueStateBlocked = theBlocked;
310 bool ModuleBase_ModelWidget::restoreValue()
312 emit beforeValuesRestored();
313 bool isDone = restoreValueCustom();
314 emit afterValuesRestored();
319 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
321 if (!myFlushUpdateBlocked) {
322 #ifdef DEBUG_WIDGET_INSTANCE
323 qDebug("ModuleBase_ModelWidget::updateObject");
325 ModuleBase_Tools::flushUpdated(theObject);
326 emit objectUpdated();
330 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
332 //blockUpdateViewer(true);
333 #ifdef DEBUG_WIDGET_INSTANCE
334 qDebug("ModuleBase_ModelWidget::moveObject");
337 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
338 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
339 Events_Loop::loop()->flush(anEvent);
341 //blockUpdateViewer(false);
344 bool ModuleBase_ModelWidget::processEnter()
349 bool ModuleBase_ModelWidget::processDelete()
351 // we consider that model objects eats delete key in order to
352 // do nothing by for example symbol delete in line edit or spin box
356 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
358 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
359 if (theEvent->type() == QEvent::FocusIn) {
361 // The following two lines are for debugging purpose only
362 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
363 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
365 if (getControls().contains(aWidget)) {
366 emit focusInWidget(this);
369 else if (theEvent->type() == QEvent::FocusOut) {
370 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
372 Qt::FocusReason aReason = aFocusEvent->reason();
373 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
374 aReason == Qt::TabFocusReason ||
375 aReason == Qt::BacktabFocusReason ||
376 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
377 if (aMouseOrKey && getControls().contains(aWidget)) {
378 if (getValueState() == ModifiedInPP) {
383 // pass the event on to the parent class
385 return QObject::eventFilter(theObject, theEvent);
388 //**************************************************************
389 void ModuleBase_ModelWidget::onWidgetValuesChanged()
394 //**************************************************************
395 void ModuleBase_ModelWidget::onWidgetValuesModified()
397 setValueState(ModifiedInPP);