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 if (myIsObligatory) {
118 eachControl->setFocusPolicy(Qt::StrongFocus);
119 eachControl->installEventFilter(this);
122 eachControl->setFocusPolicy(Qt::NoFocus);
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();
176 void ModuleBase_ModelWidget::deactivate()
178 myIsValueStateBlocked = false;
179 if (myState == ModifiedInPP)
184 void ModuleBase_ModelWidget::initializeValueByActivate()
186 if (isComputedDefault()) {
187 if (myFeature->compute(myAttributeID)) {
196 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
198 QWidget* aControl = 0;
200 QList<QWidget*> aControls = getControls();
201 int aSize = aControls.size();
204 for (int i = 0; i < aSize && !aControl; i++) {
205 if (aControls[i]->focusPolicy() != Qt::NoFocus)
206 aControl = aControls[i];
210 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
211 if (aControls[i]->focusPolicy() != Qt::NoFocus)
212 aControl = aControls[i];
218 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
220 myDefaultValue = theValue;
223 bool ModuleBase_ModelWidget::storeValue()
225 setValueState(Stored);
227 emit beforeValuesChanged();
228 bool isDone = storeValueCustom();
229 emit afterValuesChanged();
234 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState(const ModuleBase_ModelWidget::ValueState& theState)
236 ValueState aState = myState;
237 if (myState != theState && !myIsValueStateBlocked) {
239 emit valueStateChanged(aState);
244 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
246 bool isBlocked = myIsValueStateBlocked;
247 myIsValueStateBlocked = theBlocked;
251 bool ModuleBase_ModelWidget::restoreValue()
253 emit beforeValuesRestored();
254 bool isDone = restoreValueCustom();
255 emit afterValuesRestored();
260 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj)
262 blockUpdateViewer(true);
264 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
266 blockUpdateViewer(false);
269 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
271 //blockUpdateViewer(true);
273 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
274 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
275 Events_Loop::loop()->flush(anEvent);
277 //blockUpdateViewer(false);
280 bool ModuleBase_ModelWidget::processEnter()
285 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
287 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
288 if (theEvent->type() == QEvent::FocusIn) {
290 // The following two lines are for debugging purpose only
291 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
292 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
294 if (getControls().contains(aWidget)) {
295 emit focusInWidget(this);
298 else if (theEvent->type() == QEvent::FocusOut) {
299 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
301 Qt::FocusReason aReason = aFocusEvent->reason();
302 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
303 aReason == Qt::TabFocusReason ||
304 aReason == Qt::BacktabFocusReason ||
305 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
306 if (aMouseOrKey && getControls().contains(aWidget) && getValueState() == ModifiedInPP)
309 // pass the event on to the parent class
311 return QObject::eventFilter(theObject, theEvent);
314 //**************************************************************
315 void ModuleBase_ModelWidget::onWidgetValuesChanged()
320 //**************************************************************
321 void ModuleBase_ModelWidget::onWidgetValuesModified()
323 setValueState(ModifiedInPP);
326 //**************************************************************
327 void ModuleBase_ModelWidget::blockUpdateViewer(const bool theValue)
329 // the viewer update should be blocked in order to avoid the temporary feature content
330 // when the solver processes the feature, the redisplay message can be flushed
331 // what caused the display in the viewer preliminary states of object
332 // e.g. fillet feature, angle value change
333 std::shared_ptr<Events_Message> aMsg;
335 aMsg = std::shared_ptr<Events_Message>(
336 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
339 // the viewer update should be unblocked
340 aMsg = std::shared_ptr<Events_Message>(
341 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
343 Events_Loop::loop()->send(aMsg);