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 myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
36 myDefaultValue = theData->getProperty(ATTR_DEFAULT);
37 myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
38 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
39 myAttributeID = theData ? theData->widgetId() : "";
40 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
42 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
43 connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
46 bool ModuleBase_ModelWidget::reset()
48 bool aResult = resetCustom();
55 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
57 return theObject->data()->attribute(attributeID())->isInitialized();
60 QString ModuleBase_ModelWidget::getValueStateError() const
64 ModuleBase_ModelWidget::ValueState aState = getValueState();
65 if (aState != ModuleBase_ModelWidget::Stored) {
66 AttributePtr anAttr = feature()->attribute(attributeID());
68 QString anAttributeName = anAttr->id().c_str();
70 case ModuleBase_ModelWidget::ModifiedInViewer:
71 anError = "Attribute \"" + anAttributeName +
72 "\" is locked by modification value in the viewer.";
74 case ModuleBase_ModelWidget::Reset:
75 anError = "Attribute \"" + anAttributeName + "\" is not initialized.";
77 case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
86 QString ModuleBase_ModelWidget::getError() const
93 std::string anAttributeID = attributeID();
94 AttributePtr anAttribute = feature()->attribute(anAttributeID);
95 if (!anAttribute.get())
98 std::string aValidatorID;
99 std::string anErrorMsg;
101 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
102 if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
103 if (anErrorMsg.empty())
104 anErrorMsg = "unknown error.";
105 anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg;
108 anError = QString::fromStdString(anErrorMsg);
109 if (anError.isEmpty())
110 anError = getValueStateError();
115 void ModuleBase_ModelWidget::enableFocusProcessing()
117 QList<QWidget*> aMyControls = getControls();
118 foreach(QWidget* eachControl, aMyControls) {
119 eachControl->setFocusPolicy(Qt::StrongFocus);
120 eachControl->installEventFilter(this);
124 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
126 QList<QWidget*> aWidgetList = getControls();
127 foreach(QWidget* aWidget, aWidgetList) {
128 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
129 // We won't set the effect to QLabels - it looks ugly
131 // If effect is the installed on a different widget, setGraphicsEffect() will
132 // remove the effect from the widget and install it on this widget.
133 // That's why we create a new effect for each widget
134 ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
138 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
140 myFeature = theFeature;
145 bool ModuleBase_ModelWidget::focusTo()
147 QList<QWidget*> aControls = getControls();
148 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
149 bool isFocusAccepted = false;
150 for (; anIt != aLast && !isFocusAccepted; anIt++) {
151 QWidget* aWidget = *anIt;
152 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
153 ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
154 isFocusAccepted = true;
157 return isFocusAccepted;
160 void ModuleBase_ModelWidget::activate()
162 // the control value is stored to the mode by the focus in on the widget
163 // we need the value is initialized in order to enable the apply button in the property panel.
164 // It should happens in the creation mode only because all fields are filled in the edition mode
165 if (!isEditingMode()) {
166 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
167 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
168 initializeValueByActivate();
173 void ModuleBase_ModelWidget::deactivate()
175 myIsValueStateBlocked = false;
176 if (myState == ModifiedInPP || myState == ModifiedInViewer)
181 void ModuleBase_ModelWidget::initializeValueByActivate()
183 if (isComputedDefault()) {
184 if (myFeature->compute(myAttributeID)) {
193 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
195 QWidget* aControl = 0;
197 QList<QWidget*> aControls = getControls();
198 int aSize = aControls.size();
201 for (int i = 0; i < aSize && !aControl; i++) {
202 if (aControls[i]->focusPolicy() != Qt::NoFocus)
203 aControl = aControls[i];
207 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
208 if (aControls[i]->focusPolicy() != Qt::NoFocus)
209 aControl = aControls[i];
215 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
217 myDefaultValue = theValue;
220 bool ModuleBase_ModelWidget::storeValue()
222 setValueState(Stored);
224 emit beforeValuesChanged();
225 bool isDone = storeValueCustom();
226 emit afterValuesChanged();
231 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState(const ModuleBase_ModelWidget::ValueState& theState)
233 ValueState aState = myState;
234 if (myState != theState && !myIsValueStateBlocked) {
236 emit valueStateChanged(aState);
241 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
243 bool isBlocked = myIsValueStateBlocked;
244 myIsValueStateBlocked = theBlocked;
248 bool ModuleBase_ModelWidget::restoreValue()
250 emit beforeValuesRestored();
251 bool isDone = restoreValueCustom();
252 emit afterValuesRestored();
257 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj)
259 blockUpdateViewer(true);
261 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
263 blockUpdateViewer(false);
266 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
268 //blockUpdateViewer(true);
270 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
271 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
272 Events_Loop::loop()->flush(anEvent);
274 //blockUpdateViewer(false);
277 bool ModuleBase_ModelWidget::processEnter()
282 bool ModuleBase_ModelWidget::processDelete()
287 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
289 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
290 if (theEvent->type() == QEvent::FocusIn) {
292 // The following two lines are for debugging purpose only
293 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
294 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
296 if (getControls().contains(aWidget)) {
297 emit focusInWidget(this);
300 else if (theEvent->type() == QEvent::FocusOut) {
301 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
303 Qt::FocusReason aReason = aFocusEvent->reason();
304 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
305 aReason == Qt::TabFocusReason ||
306 aReason == Qt::BacktabFocusReason ||
307 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
308 if (aMouseOrKey && getControls().contains(aWidget)) {
309 if (getValueState() == ModifiedInPP) {
314 // pass the event on to the parent class
316 return QObject::eventFilter(theObject, theEvent);
319 //**************************************************************
320 void ModuleBase_ModelWidget::onWidgetValuesChanged()
325 //**************************************************************
326 void ModuleBase_ModelWidget::onWidgetValuesModified()
328 setValueState(ModifiedInPP);
331 //**************************************************************
332 void ModuleBase_ModelWidget::blockUpdateViewer(const bool theValue)
334 // the viewer update should be blocked in order to avoid the temporary feature content
335 // when the solver processes the feature, the redisplay message can be flushed
336 // what caused the display in the viewer preliminary states of object
337 // e.g. fillet feature, angle value change
338 std::shared_ptr<Events_Message> aMsg;
340 aMsg = std::shared_ptr<Events_Message>(
341 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
344 // the viewer update should be unblocked
345 aMsg = std::shared_ptr<Events_Message>(
346 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
348 Events_Loop::loop()->send(aMsg);