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),
37 myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
39 myDefaultValue = theData->getProperty(ATTR_DEFAULT);
40 myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
41 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
42 myAttributeID = theData ? theData->widgetId() : "";
43 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
45 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
46 connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
49 bool ModuleBase_ModelWidget::reset()
51 bool aResult = resetCustom();
58 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
60 return theObject->data()->attribute(attributeID())->isInitialized();
63 QString ModuleBase_ModelWidget::getValueStateError() const
67 ModuleBase_ModelWidget::ValueState aState = getValueState();
68 if (aState != ModuleBase_ModelWidget::Stored) {
69 AttributePtr anAttr = feature()->attribute(attributeID());
71 QString anAttributeName = anAttr->id().c_str();
73 case ModuleBase_ModelWidget::ModifiedInViewer:
74 anError = "Attribute \"" + anAttributeName +
75 "\" is locked by modification value in the viewer.";
77 case ModuleBase_ModelWidget::Reset:
78 anError = "Attribute \"" + anAttributeName + "\" is not initialized.";
80 case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
89 QString ModuleBase_ModelWidget::getError() const
96 std::string anAttributeID = attributeID();
97 AttributePtr anAttribute = feature()->attribute(anAttributeID);
98 if (!anAttribute.get())
101 std::string aValidatorID;
102 std::string anErrorMsg;
104 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
105 if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
106 if (anErrorMsg.empty())
107 anErrorMsg = "unknown error.";
108 anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg;
111 anError = QString::fromStdString(anErrorMsg);
112 if (anError.isEmpty())
113 anError = getValueStateError();
118 void ModuleBase_ModelWidget::enableFocusProcessing()
120 QList<QWidget*> aMyControls = getControls();
121 foreach(QWidget* eachControl, aMyControls) {
122 eachControl->setFocusPolicy(Qt::StrongFocus);
123 eachControl->installEventFilter(this);
127 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
129 QList<QWidget*> aWidgetList = getControls();
130 foreach(QWidget* aWidget, aWidgetList) {
131 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
132 // We won't set the effect to QLabels - it looks ugly
134 // If effect is the installed on a different widget, setGraphicsEffect() will
135 // remove the effect from the widget and install it on this widget.
136 // That's why we create a new effect for each widget
137 ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
141 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
143 myFeature = theFeature;
148 bool ModuleBase_ModelWidget::focusTo()
150 QList<QWidget*> aControls = getControls();
151 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
152 bool isFocusAccepted = false;
153 for (; anIt != aLast && !isFocusAccepted; anIt++) {
154 QWidget* aWidget = *anIt;
155 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
156 ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
157 isFocusAccepted = true;
160 return isFocusAccepted;
163 void ModuleBase_ModelWidget::activate()
165 // the control value is stored to the mode by the focus in on the widget
166 // we need the value is initialized in order to enable the apply button in the property panel.
167 // It should happens in the creation mode only because all fields are filled in the edition mode
168 if (!isEditingMode()) {
169 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
170 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
171 initializeValueByActivate();
174 if (myWidgetValidator)
175 myWidgetValidator->activateFilters(true);
180 void ModuleBase_ModelWidget::deactivate()
182 myIsValueStateBlocked = false;
183 if (myState == ModifiedInPP || myState == ModifiedInViewer)
187 if (myWidgetValidator)
188 myWidgetValidator->activateFilters(false);
191 void ModuleBase_ModelWidget::initializeValueByActivate()
193 if (isComputedDefault()) {
194 if (myFeature->compute(myAttributeID)) {
203 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
205 QWidget* aControl = 0;
207 QList<QWidget*> aControls = getControls();
208 int aSize = aControls.size();
211 for (int i = 0; i < aSize && !aControl; i++) {
212 if (aControls[i]->focusPolicy() != Qt::NoFocus)
213 aControl = aControls[i];
217 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
218 if (aControls[i]->focusPolicy() != Qt::NoFocus)
219 aControl = aControls[i];
225 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
227 myDefaultValue = theValue;
230 bool ModuleBase_ModelWidget::storeValue()
232 setValueState(Stored);
234 emit beforeValuesChanged();
235 bool isDone = storeValueCustom();
236 emit afterValuesChanged();
240 #ifdef DEBUG_VALUE_STATE
241 std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
245 case ModuleBase_ModelWidget::Stored: anInfo = "Stored "; break;
246 case ModuleBase_ModelWidget::ModifiedInPP: anInfo = "ModifiedInPP "; break;
247 case ModuleBase_ModelWidget::ModifiedInViewer: anInfo = "ModifiedInViewer"; break;
248 case ModuleBase_ModelWidget::Reset: anInfo = "Reset "; break;
255 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
256 (const ModuleBase_ModelWidget::ValueState& theState)
258 ValueState aState = myState;
260 if (myState != theState && !myIsValueStateBlocked) {
261 #ifdef DEBUG_VALUE_STATE
262 qDebug(QString("setValueState: previous state = %1,\t new state = %2")
263 .arg(getDebugInfo(myState).c_str())
264 .arg(getDebugInfo(theState).c_str()).toStdString().c_str());
267 emit valueStateChanged(aState);
272 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
274 bool isBlocked = myIsValueStateBlocked;
275 myIsValueStateBlocked = theBlocked;
279 bool ModuleBase_ModelWidget::restoreValue()
281 emit beforeValuesRestored();
282 bool isDone = restoreValueCustom();
283 emit afterValuesRestored();
288 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
290 ModuleBase_Tools::flushUpdated(theObject);
291 emit objectUpdated();
294 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
296 //blockUpdateViewer(true);
298 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
299 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
300 Events_Loop::loop()->flush(anEvent);
302 //blockUpdateViewer(false);
305 bool ModuleBase_ModelWidget::processEnter()
310 bool ModuleBase_ModelWidget::processDelete()
312 // we consider that model objects eats delete key in order to
313 // do nothing by for example symbol delete in line edit or spin box
317 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
319 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
320 if (theEvent->type() == QEvent::FocusIn) {
322 // The following two lines are for debugging purpose only
323 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
324 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
326 if (getControls().contains(aWidget)) {
327 emit focusInWidget(this);
330 else if (theEvent->type() == QEvent::FocusOut) {
331 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
333 Qt::FocusReason aReason = aFocusEvent->reason();
334 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
335 aReason == Qt::TabFocusReason ||
336 aReason == Qt::BacktabFocusReason ||
337 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
338 if (aMouseOrKey && getControls().contains(aWidget)) {
339 if (getValueState() == ModifiedInPP) {
344 // pass the event on to the parent class
346 return QObject::eventFilter(theObject, theEvent);
349 //**************************************************************
350 void ModuleBase_ModelWidget::onWidgetValuesChanged()
355 //**************************************************************
356 void ModuleBase_ModelWidget::onWidgetValuesModified()
358 setValueState(ModifiedInPP);