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>
15 #include <Config_Keywords.h>
16 #include <Config_WidgetAPI.h>
18 #include <Events_Loop.h>
21 #include <QGraphicsDropShadowEffect>
24 #include <QFocusEvent>
26 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
27 const Config_WidgetAPI* theData,
28 const std::string& theParentId)
30 myParentId(theParentId),
33 myIsValueStateBlocked(false)
35 myDefaultValue = theData->getProperty(ATTR_DEFAULT);
36 myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
37 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
38 myAttributeID = theData ? theData->widgetId() : "";
39 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
41 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
42 connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
45 bool ModuleBase_ModelWidget::reset()
47 bool aResult = resetCustom();
54 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
56 return theObject->data()->attribute(attributeID())->isInitialized();
59 QString ModuleBase_ModelWidget::getValueStateError() const
63 ModuleBase_ModelWidget::ValueState aState = getValueState();
64 if (aState != ModuleBase_ModelWidget::Stored) {
65 AttributePtr anAttr = feature()->attribute(attributeID());
67 QString anAttributeName = anAttr->id().c_str();
69 case ModuleBase_ModelWidget::ModifiedInPP:
70 anError = "Attribute \"" + anAttributeName +
71 "\" modification is not applyed. Please click \"Enter\" or \"Tab\".";
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.";
86 void ModuleBase_ModelWidget::enableFocusProcessing()
88 QList<QWidget*> aMyControls = getControls();
89 foreach(QWidget* eachControl, aMyControls) {
91 eachControl->setFocusPolicy(Qt::StrongFocus);
92 eachControl->installEventFilter(this);
95 eachControl->setFocusPolicy(Qt::NoFocus);
100 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
102 QList<QWidget*> aWidgetList = getControls();
103 foreach(QWidget* aWidget, aWidgetList) {
104 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
105 // We won't set the effect to QLabels - it looks ugly
108 // If effect is the installed on a different widget, setGraphicsEffect() will
109 // remove the effect from the widget and install it on this widget.
110 // That's why we create a new effect for each widget
111 QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
112 aGlowEffect->setOffset(.0);
113 aGlowEffect->setBlurRadius(10.0);
114 aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
115 aWidget->setGraphicsEffect(aGlowEffect);
117 QGraphicsEffect* anEffect = aWidget->graphicsEffect();
119 anEffect->deleteLater();
120 aWidget->setGraphicsEffect(NULL);
125 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
127 myFeature = theFeature;
132 bool ModuleBase_ModelWidget::focusTo()
134 QList<QWidget*> aControls = getControls();
135 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
136 bool isFocusAccepted = false;
137 for (; anIt != aLast && !isFocusAccepted; anIt++) {
138 QWidget* aWidget = *anIt;
139 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
140 ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
141 isFocusAccepted = true;
144 return isFocusAccepted;
147 void ModuleBase_ModelWidget::activate()
149 // the control value is stored to the mode by the focus in on the widget
150 // we need the value is initialized in order to enable the apply button in the property panel.
151 // It should happens in the creation mode only because all fields are filled in the edition mode
152 if (!isEditingMode()) {
153 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
154 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
155 initializeValueByActivate();
160 void ModuleBase_ModelWidget::deactivate()
162 myIsValueStateBlocked = false;
163 if (myState == ModifiedInPP)
168 void ModuleBase_ModelWidget::initializeValueByActivate()
170 if (isComputedDefault()) {
171 if (myFeature->compute(myAttributeID)) {
180 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
182 QWidget* aControl = 0;
184 QList<QWidget*> aControls = getControls();
185 int aSize = aControls.size();
188 for (int i = 0; i < aSize && !aControl; i++) {
189 if (aControls[i]->focusPolicy() != Qt::NoFocus)
190 aControl = aControls[i];
194 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
195 if (aControls[i]->focusPolicy() != Qt::NoFocus)
196 aControl = aControls[i];
202 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
204 myDefaultValue = theValue;
207 bool ModuleBase_ModelWidget::storeValue()
209 setValueState(Stored);
211 emit beforeValuesChanged();
212 bool isDone = storeValueCustom();
213 emit afterValuesChanged();
218 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState(const ModuleBase_ModelWidget::ValueState& theState)
220 ValueState aState = myState;
221 if (myState != theState && !myIsValueStateBlocked) {
223 emit valueStateChanged(aState);
228 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
230 bool isBlocked = myIsValueStateBlocked;
231 myIsValueStateBlocked = theBlocked;
235 bool ModuleBase_ModelWidget::restoreValue()
237 emit beforeValuesRestored();
238 bool isDone = restoreValueCustom();
239 emit afterValuesRestored();
244 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj)
246 blockUpdateViewer(true);
248 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
250 blockUpdateViewer(false);
253 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
255 //blockUpdateViewer(true);
257 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
258 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
259 Events_Loop::loop()->flush(anEvent);
261 //blockUpdateViewer(false);
264 bool ModuleBase_ModelWidget::processEnter()
269 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
271 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
272 if (theEvent->type() == QEvent::FocusIn) {
274 // The following two lines are for debugging purpose only
275 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
276 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
278 if (getControls().contains(aWidget)) {
279 emit focusInWidget(this);
282 else if (theEvent->type() == QEvent::FocusOut) {
283 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
285 Qt::FocusReason aReason = aFocusEvent->reason();
286 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
287 aReason == Qt::TabFocusReason ||
288 aReason == Qt::BacktabFocusReason ||
289 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
290 if (aMouseOrKey && getControls().contains(aWidget) && getValueState() == ModifiedInPP)
293 // pass the event on to the parent class
295 return QObject::eventFilter(theObject, theEvent);
298 //**************************************************************
299 void ModuleBase_ModelWidget::onWidgetValuesChanged()
304 //**************************************************************
305 void ModuleBase_ModelWidget::onWidgetValuesModified()
307 setValueState(ModifiedInPP);
310 //**************************************************************
311 void ModuleBase_ModelWidget::blockUpdateViewer(const bool theValue)
313 // the viewer update should be blocked in order to avoid the temporary feature content
314 // when the solver processes the feature, the redisplay message can be flushed
315 // what caused the display in the viewer preliminary states of object
316 // e.g. fillet feature, angle value change
317 std::shared_ptr<Events_Message> aMsg;
319 aMsg = std::shared_ptr<Events_Message>(
320 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
323 // the viewer update should be unblocked
324 aMsg = std::shared_ptr<Events_Message>(
325 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
327 Events_Loop::loop()->send(aMsg);