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 theObj)
290 blockUpdateViewer(true);
292 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
294 blockUpdateViewer(false);
297 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
299 //blockUpdateViewer(true);
301 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
302 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
303 Events_Loop::loop()->flush(anEvent);
305 //blockUpdateViewer(false);
308 bool ModuleBase_ModelWidget::processEnter()
313 bool ModuleBase_ModelWidget::processDelete()
315 // we consider that model objects eats delete key in order to
316 // do nothing by for example symbol delete in line edit or spin box
320 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
322 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
323 if (theEvent->type() == QEvent::FocusIn) {
325 // The following two lines are for debugging purpose only
326 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
327 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
329 if (getControls().contains(aWidget)) {
330 emit focusInWidget(this);
333 else if (theEvent->type() == QEvent::FocusOut) {
334 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
336 Qt::FocusReason aReason = aFocusEvent->reason();
337 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
338 aReason == Qt::TabFocusReason ||
339 aReason == Qt::BacktabFocusReason ||
340 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
341 if (aMouseOrKey && getControls().contains(aWidget)) {
342 if (getValueState() == ModifiedInPP) {
347 // pass the event on to the parent class
349 return QObject::eventFilter(theObject, theEvent);
352 //**************************************************************
353 void ModuleBase_ModelWidget::onWidgetValuesChanged()
358 //**************************************************************
359 void ModuleBase_ModelWidget::onWidgetValuesModified()
361 setValueState(ModifiedInPP);
364 //**************************************************************
365 void ModuleBase_ModelWidget::blockUpdateViewer(const bool theValue)
367 // the viewer update should be blocked in order to avoid the temporary feature content
368 // when the solver processes the feature, the redisplay message can be flushed
369 // what caused the display in the viewer preliminary states of object
370 // e.g. fillet feature, angle value change
371 std::shared_ptr<Events_Message> aMsg;
373 aMsg = std::shared_ptr<Events_Message>(
374 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
377 // the viewer update should be unblocked
378 aMsg = std::shared_ptr<Events_Message>(
379 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
381 Events_Loop::loop()->send(aMsg);