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"
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Attribute.h>
11 #include <ModelAPI_Events.h>
12 #include <ModelAPI_Session.h>
14 #include <Config_Keywords.h>
15 #include <Config_WidgetAPI.h>
17 #include <Events_Loop.h>
20 #include <QGraphicsDropShadowEffect>
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 void ModuleBase_ModelWidget::enableFocusProcessing()
60 QList<QWidget*> aMyControls = getControls();
61 foreach(QWidget* eachControl, aMyControls) {
63 eachControl->setFocusPolicy(Qt::StrongFocus);
64 eachControl->installEventFilter(this);
67 eachControl->setFocusPolicy(Qt::NoFocus);
72 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
74 QList<QWidget*> aWidgetList = getControls();
75 foreach(QWidget* aWidget, aWidgetList) {
76 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
77 // We won't set the effect to QLabels - it looks ugly
80 // If effect is the installed on a different widget, setGraphicsEffect() will
81 // remove the effect from the widget and install it on this widget.
82 // That's why we create a new effect for each widget
83 QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
84 aGlowEffect->setOffset(.0);
85 aGlowEffect->setBlurRadius(10.0);
86 aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
87 aWidget->setGraphicsEffect(aGlowEffect);
89 QGraphicsEffect* anEffect = aWidget->graphicsEffect();
91 anEffect->deleteLater();
92 aWidget->setGraphicsEffect(NULL);
97 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
99 myFeature = theFeature;
104 bool ModuleBase_ModelWidget::focusTo()
106 QList<QWidget*> aControls = getControls();
107 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
108 bool isFocusAccepted = false;
109 for (; anIt != aLast && !isFocusAccepted; anIt++) {
110 QWidget* aWidget = *anIt;
111 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
113 isFocusAccepted = true;
116 return isFocusAccepted;
119 void ModuleBase_ModelWidget::activate()
121 // the control value is stored to the mode by the focus in on the widget
122 // we need the value is initialized in order to enable the apply button in the property panel.
123 // It should happens in the creation mode only because all fields are filled in the edition mode
124 if (!isEditingMode()) {
125 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
126 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
127 initializeValueByActivate();
132 void ModuleBase_ModelWidget::deactivate()
134 myIsValueStateBlocked = false;
135 if (myState == ModifiedInPP)
140 void ModuleBase_ModelWidget::initializeValueByActivate()
142 if (isComputedDefault()) {
143 if (myFeature->compute(myAttributeID)) {
152 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
154 QWidget* aControl = 0;
156 QList<QWidget*> aControls = getControls();
157 int aSize = aControls.size();
160 for (int i = 0; i < aSize && !aControl; i++) {
161 if (aControls[i]->focusPolicy() != Qt::NoFocus)
162 aControl = aControls[i];
166 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
167 if (aControls[i]->focusPolicy() != Qt::NoFocus)
168 aControl = aControls[i];
174 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
176 myDefaultValue = theValue;
179 bool ModuleBase_ModelWidget::storeValue()
181 setValueState(Stored);
183 emit beforeValuesChanged();
184 bool isDone = storeValueCustom();
185 emit afterValuesChanged();
190 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState(const ModuleBase_ModelWidget::ValueState& theState)
192 ValueState aState = myState;
193 if (myState != theState && !myIsValueStateBlocked) {
195 emit valueStateChanged(aState);
200 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
202 bool isBlocked = myIsValueStateBlocked;
203 myIsValueStateBlocked = theBlocked;
207 bool ModuleBase_ModelWidget::restoreValue()
209 emit beforeValuesRestored();
210 bool isDone = restoreValueCustom();
211 emit afterValuesRestored();
216 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj)
218 blockUpdateViewer(true);
220 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
222 blockUpdateViewer(false);
225 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
227 //blockUpdateViewer(true);
229 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
230 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
231 Events_Loop::loop()->flush(anEvent);
233 //blockUpdateViewer(false);
236 bool ModuleBase_ModelWidget::processEnter()
241 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
243 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
244 if (theEvent->type() == QEvent::FocusIn) {
246 // The following two lines are for debugging purpose only
247 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
248 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
250 if (getControls().contains(aWidget)) {
251 emit focusInWidget(this);
254 else if (theEvent->type() == QEvent::FocusOut) {
255 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
257 Qt::FocusReason aReason = aFocusEvent->reason();
258 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
259 aReason == Qt::TabFocusReason ||
260 aReason == Qt::BacktabFocusReason ||
261 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
262 if (aMouseOrKey && getControls().contains(aWidget) && getValueState() == ModifiedInPP)
265 // pass the event on to the parent class
267 return QObject::eventFilter(theObject, theEvent);
270 //**************************************************************
271 void ModuleBase_ModelWidget::onWidgetValuesChanged()
276 //**************************************************************
277 void ModuleBase_ModelWidget::onWidgetValuesModified()
279 setValueState(ModifiedInPP);
282 //**************************************************************
283 void ModuleBase_ModelWidget::blockUpdateViewer(const bool theValue)
285 // the viewer update should be blocked in order to avoid the temporary feature content
286 // when the solver processes the feature, the redisplay message can be flushed
287 // what caused the display in the viewer preliminary states of object
288 // e.g. fillet feature, angle value change
289 std::shared_ptr<Events_Message> aMsg;
291 aMsg = std::shared_ptr<Events_Message>(
292 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
295 // the viewer update should be unblocked
296 aMsg = std::shared_ptr<Events_Message>(
297 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
299 Events_Loop::loop()->send(aMsg);