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 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
30 const Config_WidgetAPI* theData)
34 myIsValueStateBlocked(false),
35 myFlushUpdateBlocked(false),
38 myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
40 myDefaultValue = theData->getProperty(ATTR_DEFAULT);
41 myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
42 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
43 myAttributeID = theData ? theData->widgetId() : "";
44 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
46 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
47 connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
50 bool ModuleBase_ModelWidget::reset()
52 bool aResult = resetCustom();
59 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
61 return theObject->data()->attribute(attributeID())->isInitialized();
64 void ModuleBase_ModelWidget::processValueState()
66 if (myState == ModifiedInPP || myState == ModifiedInViewer)
70 QString ModuleBase_ModelWidget::getValueStateError() const
74 ModuleBase_ModelWidget::ValueState aState = getValueState();
75 if (aState != ModuleBase_ModelWidget::Stored) {
76 AttributePtr anAttr = feature()->attribute(attributeID());
78 QString anAttributeName = anAttr->id().c_str();
80 case ModuleBase_ModelWidget::ModifiedInViewer:
81 anError = "Attribute \"" + anAttributeName +
82 "\" is locked by modification value in the viewer.";
84 case ModuleBase_ModelWidget::Reset:
85 anError = "Attribute \"" + anAttributeName + "\" is not initialized.";
87 case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
96 QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
100 if (!feature().get())
103 std::string anAttributeID = attributeID();
104 AttributePtr anAttribute = feature()->attribute(anAttributeID);
105 if (!anAttribute.get())
108 std::string aValidatorID;
109 std::string anErrorMsg;
111 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
112 if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
113 if (anErrorMsg.empty())
114 anErrorMsg = "unknown error.";
115 anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg;
118 anError = QString::fromStdString(anErrorMsg);
119 if (anError.isEmpty() && theValueStateChecked)
120 anError = getValueStateError();
125 void ModuleBase_ModelWidget::enableFocusProcessing()
127 QList<QWidget*> aMyControls = getControls();
128 foreach(QWidget* eachControl, aMyControls) {
129 eachControl->setFocusPolicy(Qt::StrongFocus);
130 eachControl->installEventFilter(this);
134 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
136 QList<QWidget*> aWidgetList = getControls();
137 foreach(QWidget* aWidget, aWidgetList) {
138 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
139 // We won't set the effect to QLabels - it looks ugly
141 // If effect is the installed on a different widget, setGraphicsEffect() will
142 // remove the effect from the widget and install it on this widget.
143 // That's why we create a new effect for each widget
144 ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
148 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue,
149 const bool isUpdateFlushed)
151 /// it is possible to give this flag as parameter in storeValue/storeCustomValue
152 /// after debug, it may be corrected
153 myFlushUpdateBlocked = !isUpdateFlushed;
154 myFeature = theFeature;
157 myFlushUpdateBlocked = false;
160 bool ModuleBase_ModelWidget::focusTo()
162 QList<QWidget*> aControls = getControls();
163 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
164 bool isFocusAccepted = false;
165 for (; anIt != aLast && !isFocusAccepted; anIt++) {
166 QWidget* aWidget = *anIt;
167 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
168 ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
169 isFocusAccepted = true;
172 return isFocusAccepted;
175 void ModuleBase_ModelWidget::activate()
177 // the control value is stored to the mode by the focus in on the widget
178 // we need the value is initialized in order to enable the apply button in the property panel.
179 // It should happens in the creation mode only because all fields are filled in the edition mode
180 if (!isEditingMode()) {
181 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
182 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
183 initializeValueByActivate();
186 if (myWidgetValidator)
187 myWidgetValidator->activateFilters(true);
192 void ModuleBase_ModelWidget::deactivate()
194 myIsValueStateBlocked = false;
196 if (myWidgetValidator)
197 myWidgetValidator->activateFilters(false);
200 void ModuleBase_ModelWidget::initializeValueByActivate()
202 if (isComputedDefault()) {
203 if (myFeature->compute(myAttributeID)) {
212 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
214 QWidget* aControl = 0;
216 QList<QWidget*> aControls = getControls();
217 int aSize = aControls.size();
220 for (int i = 0; i < aSize && !aControl; i++) {
221 if (aControls[i]->focusPolicy() != Qt::NoFocus)
222 aControl = aControls[i];
226 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
227 if (aControls[i]->focusPolicy() != Qt::NoFocus)
228 aControl = aControls[i];
234 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
236 myDefaultValue = theValue;
239 bool ModuleBase_ModelWidget::storeValue()
241 setValueState(Stored);
243 emit beforeValuesChanged();
244 bool isDone = storeValueCustom();
245 emit afterValuesChanged();
249 #ifdef DEBUG_VALUE_STATE
250 std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
254 case ModuleBase_ModelWidget::Stored: anInfo = "Stored "; break;
255 case ModuleBase_ModelWidget::ModifiedInPP: anInfo = "ModifiedInPP "; break;
256 case ModuleBase_ModelWidget::ModifiedInViewer: anInfo = "ModifiedInViewer"; break;
257 case ModuleBase_ModelWidget::Reset: anInfo = "Reset "; break;
264 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
265 (const ModuleBase_ModelWidget::ValueState& theState)
267 ValueState aState = myState;
269 if (myState != theState && !myIsValueStateBlocked) {
270 #ifdef DEBUG_VALUE_STATE
271 qDebug(QString("setValueState: previous state = %1,\t new state = %2")
272 .arg(getDebugInfo(myState).c_str())
273 .arg(getDebugInfo(theState).c_str()).toStdString().c_str());
276 emit valueStateChanged(aState);
281 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
283 bool isBlocked = myIsValueStateBlocked;
284 myIsValueStateBlocked = theBlocked;
288 bool ModuleBase_ModelWidget::restoreValue()
290 emit beforeValuesRestored();
291 bool isDone = restoreValueCustom();
292 emit afterValuesRestored();
297 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
299 if (!myFlushUpdateBlocked) {
300 ModuleBase_Tools::flushUpdated(theObject);
301 emit objectUpdated();
305 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
307 //blockUpdateViewer(true);
309 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
310 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
311 Events_Loop::loop()->flush(anEvent);
313 //blockUpdateViewer(false);
316 bool ModuleBase_ModelWidget::processEnter()
321 bool ModuleBase_ModelWidget::processDelete()
323 // we consider that model objects eats delete key in order to
324 // do nothing by for example symbol delete in line edit or spin box
328 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
330 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
331 if (theEvent->type() == QEvent::FocusIn) {
333 // The following two lines are for debugging purpose only
334 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
335 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
337 if (getControls().contains(aWidget)) {
338 emit focusInWidget(this);
341 else if (theEvent->type() == QEvent::FocusOut) {
342 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
344 Qt::FocusReason aReason = aFocusEvent->reason();
345 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
346 aReason == Qt::TabFocusReason ||
347 aReason == Qt::BacktabFocusReason ||
348 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
349 if (aMouseOrKey && getControls().contains(aWidget)) {
350 if (getValueState() == ModifiedInPP) {
355 // pass the event on to the parent class
357 return QObject::eventFilter(theObject, theEvent);
360 //**************************************************************
361 void ModuleBase_ModelWidget::onWidgetValuesChanged()
366 //**************************************************************
367 void ModuleBase_ModelWidget::onWidgetValuesModified()
369 setValueState(ModifiedInPP);