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_Tools.h"
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_Attribute.h>
12 #include <ModelAPI_Events.h>
13 #include <ModelAPI_Session.h>
14 #include <ModelAPI_Validator.h>
16 #include <Config_Keywords.h>
17 #include <Config_WidgetAPI.h>
19 #include <Events_Loop.h>
23 #include <QFocusEvent>
25 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
26 const Config_WidgetAPI* theData,
27 const std::string& theParentId)
29 myParentId(theParentId),
32 myIsValueStateBlocked(false)
34 myDefaultValue = theData->getProperty(ATTR_DEFAULT);
35 myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
36 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
37 myAttributeID = theData ? theData->widgetId() : "";
38 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
40 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
41 connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
44 bool ModuleBase_ModelWidget::reset()
46 bool aResult = resetCustom();
53 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
55 return theObject->data()->attribute(attributeID())->isInitialized();
58 QString ModuleBase_ModelWidget::getValueStateError() const
62 ModuleBase_ModelWidget::ValueState aState = getValueState();
63 if (aState != ModuleBase_ModelWidget::Stored) {
64 AttributePtr anAttr = feature()->attribute(attributeID());
66 QString anAttributeName = anAttr->id().c_str();
68 case ModuleBase_ModelWidget::ModifiedInViewer:
69 anError = "Attribute \"" + anAttributeName +
70 "\" is locked by modification value in the viewer.";
72 case ModuleBase_ModelWidget::Reset:
73 anError = "Attribute \"" + anAttributeName + "\" is not initialized.";
75 case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
84 QString ModuleBase_ModelWidget::getError() const
91 std::string anAttributeID = attributeID();
92 AttributePtr anAttribute = feature()->attribute(anAttributeID);
93 if (!anAttribute.get())
96 std::string aValidatorID;
97 std::string anErrorMsg;
99 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
100 if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
101 if (anErrorMsg.empty())
102 anErrorMsg = "unknown error.";
103 anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg;
106 anError = QString::fromStdString(anErrorMsg);
107 if (anError.isEmpty())
108 anError = getValueStateError();
113 void ModuleBase_ModelWidget::enableFocusProcessing()
115 QList<QWidget*> aMyControls = getControls();
116 foreach(QWidget* eachControl, aMyControls) {
117 eachControl->setFocusPolicy(Qt::StrongFocus);
118 eachControl->installEventFilter(this);
122 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
124 QList<QWidget*> aWidgetList = getControls();
125 foreach(QWidget* aWidget, aWidgetList) {
126 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
127 // We won't set the effect to QLabels - it looks ugly
129 // If effect is the installed on a different widget, setGraphicsEffect() will
130 // remove the effect from the widget and install it on this widget.
131 // That's why we create a new effect for each widget
132 ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
136 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
138 myFeature = theFeature;
143 bool ModuleBase_ModelWidget::focusTo()
145 QList<QWidget*> aControls = getControls();
146 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
147 bool isFocusAccepted = false;
148 for (; anIt != aLast && !isFocusAccepted; anIt++) {
149 QWidget* aWidget = *anIt;
150 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
151 ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
152 isFocusAccepted = true;
155 return isFocusAccepted;
158 void ModuleBase_ModelWidget::activate()
160 // the control value is stored to the mode by the focus in on the widget
161 // we need the value is initialized in order to enable the apply button in the property panel.
162 // It should happens in the creation mode only because all fields are filled in the edition mode
163 if (!isEditingMode()) {
164 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
165 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
166 initializeValueByActivate();
171 void ModuleBase_ModelWidget::deactivate()
173 myIsValueStateBlocked = false;
174 if (myState == ModifiedInPP)
179 void ModuleBase_ModelWidget::initializeValueByActivate()
181 if (isComputedDefault()) {
182 if (myFeature->compute(myAttributeID)) {
191 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
193 QWidget* aControl = 0;
195 QList<QWidget*> aControls = getControls();
196 int aSize = aControls.size();
199 for (int i = 0; i < aSize && !aControl; i++) {
200 if (aControls[i]->focusPolicy() != Qt::NoFocus)
201 aControl = aControls[i];
205 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
206 if (aControls[i]->focusPolicy() != Qt::NoFocus)
207 aControl = aControls[i];
213 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
215 myDefaultValue = theValue;
218 bool ModuleBase_ModelWidget::storeValue()
220 setValueState(Stored);
222 emit beforeValuesChanged();
223 bool isDone = storeValueCustom();
224 emit afterValuesChanged();
229 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState(const ModuleBase_ModelWidget::ValueState& theState)
231 ValueState aState = myState;
232 if (myState != theState && !myIsValueStateBlocked) {
234 emit valueStateChanged(aState);
239 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
241 bool isBlocked = myIsValueStateBlocked;
242 myIsValueStateBlocked = theBlocked;
246 bool ModuleBase_ModelWidget::restoreValue()
248 emit beforeValuesRestored();
249 bool isDone = restoreValueCustom();
250 emit afterValuesRestored();
255 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj)
257 blockUpdateViewer(true);
259 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
261 blockUpdateViewer(false);
264 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
266 //blockUpdateViewer(true);
268 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
269 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
270 Events_Loop::loop()->flush(anEvent);
272 //blockUpdateViewer(false);
275 bool ModuleBase_ModelWidget::processEnter()
280 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
282 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
283 if (theEvent->type() == QEvent::FocusIn) {
285 // The following two lines are for debugging purpose only
286 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
287 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
289 if (getControls().contains(aWidget)) {
290 emit focusInWidget(this);
293 else if (theEvent->type() == QEvent::FocusOut) {
294 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
296 Qt::FocusReason aReason = aFocusEvent->reason();
297 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
298 aReason == Qt::TabFocusReason ||
299 aReason == Qt::BacktabFocusReason ||
300 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
301 if (aMouseOrKey && getControls().contains(aWidget) && getValueState() == ModifiedInPP)
304 // pass the event on to the parent class
306 return QObject::eventFilter(theObject, theEvent);
309 //**************************************************************
310 void ModuleBase_ModelWidget::onWidgetValuesChanged()
315 //**************************************************************
316 void ModuleBase_ModelWidget::onWidgetValuesModified()
318 setValueState(ModifiedInPP);
321 //**************************************************************
322 void ModuleBase_ModelWidget::blockUpdateViewer(const bool theValue)
324 // the viewer update should be blocked in order to avoid the temporary feature content
325 // when the solver processes the feature, the redisplay message can be flushed
326 // what caused the display in the viewer preliminary states of object
327 // e.g. fillet feature, angle value change
328 std::shared_ptr<Events_Message> aMsg;
330 aMsg = std::shared_ptr<Events_Message>(
331 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
334 // the viewer update should be unblocked
335 aMsg = std::shared_ptr<Events_Message>(
336 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
338 Events_Loop::loop()->send(aMsg);