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;
138 void ModuleBase_ModelWidget::initializeValueByActivate()
140 if (isComputedDefault()) {
141 if (myFeature->compute(myAttributeID)) {
150 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
152 QWidget* aControl = 0;
154 QList<QWidget*> aControls = getControls();
155 int aSize = aControls.size();
158 for (int i = 0; i < aSize && !aControl; i++) {
159 if (aControls[i]->focusPolicy() != Qt::NoFocus)
160 aControl = aControls[i];
164 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
165 if (aControls[i]->focusPolicy() != Qt::NoFocus)
166 aControl = aControls[i];
172 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
174 myDefaultValue = theValue;
177 bool ModuleBase_ModelWidget::storeValue()
179 setValueState(Stored);
181 emit beforeValuesChanged();
182 bool isDone = storeValueCustom();
183 emit afterValuesChanged();
188 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState(const ValueState& theState)
190 ValueState aState = myState;
191 if (myState != theState && !myIsValueStateBlocked) {
193 emit valueStateChanged();
198 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
200 bool isBlocked = myIsValueStateBlocked;
201 myIsValueStateBlocked = theBlocked;
205 bool ModuleBase_ModelWidget::restoreValue()
207 emit beforeValuesRestored();
208 bool isDone = restoreValueCustom();
209 emit afterValuesRestored();
214 void ModuleBase_ModelWidget::storeValueByApply()
216 // do not emit signal about update the currenty feature object
217 // in order to do not perform additional redisplay in the viewer.
218 // It should happens by finish operation of the apply action
222 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj)
224 blockUpdateViewer(true);
226 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
228 blockUpdateViewer(false);
231 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
233 //blockUpdateViewer(true);
235 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
236 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
237 Events_Loop::loop()->flush(anEvent);
239 //blockUpdateViewer(false);
242 bool ModuleBase_ModelWidget::processEnter()
247 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
249 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
250 if (theEvent->type() == QEvent::FocusIn) {
252 // The following two lines are for debugging purpose only
253 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
254 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
256 if (getControls().contains(aWidget)) {
257 emit focusInWidget(this);
260 // pass the event on to the parent class
262 return QObject::eventFilter(theObject, theEvent);
265 //**************************************************************
266 void ModuleBase_ModelWidget::onWidgetValuesChanged()
271 //**************************************************************
272 void ModuleBase_ModelWidget::onWidgetValuesModified()
274 setValueState(ModifiedInPP);
277 //**************************************************************
278 void ModuleBase_ModelWidget::blockUpdateViewer(const bool theValue)
280 // the viewer update should be blocked in order to avoid the temporary feature content
281 // when the solver processes the feature, the redisplay message can be flushed
282 // what caused the display in the viewer preliminary states of object
283 // e.g. fillet feature, angle value change
284 std::shared_ptr<Events_Message> aMsg;
286 aMsg = std::shared_ptr<Events_Message>(
287 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
290 // the viewer update should be unblocked
291 aMsg = std::shared_ptr<Events_Message>(
292 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
294 Events_Loop::loop()->send(aMsg);