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),
33 myDefaultValue = theData->getProperty(ATTR_DEFAULT);
34 myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
35 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
36 myAttributeID = theData ? theData->widgetId() : "";
37 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
39 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
40 connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
43 bool ModuleBase_ModelWidget::reset()
45 bool aResult = resetCustom();
52 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
54 return theObject->data()->attribute(attributeID())->isInitialized();
57 void ModuleBase_ModelWidget::enableFocusProcessing()
59 QList<QWidget*> aMyControls = getControls();
60 foreach(QWidget* eachControl, aMyControls) {
62 eachControl->setFocusPolicy(Qt::StrongFocus);
63 eachControl->installEventFilter(this);
66 eachControl->setFocusPolicy(Qt::NoFocus);
71 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
73 QList<QWidget*> aWidgetList = getControls();
74 foreach(QWidget* aWidget, aWidgetList) {
75 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
76 // We won't set the effect to QLabels - it looks ugly
79 // If effect is the installed on a different widget, setGraphicsEffect() will
80 // remove the effect from the widget and install it on this widget.
81 // That's why we create a new effect for each widget
82 QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
83 aGlowEffect->setOffset(.0);
84 aGlowEffect->setBlurRadius(10.0);
85 aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
86 aWidget->setGraphicsEffect(aGlowEffect);
88 QGraphicsEffect* anEffect = aWidget->graphicsEffect();
90 anEffect->deleteLater();
91 aWidget->setGraphicsEffect(NULL);
96 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
98 myFeature = theFeature;
103 bool ModuleBase_ModelWidget::focusTo()
105 QList<QWidget*> aControls = getControls();
106 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
107 bool isFocusAccepted = false;
108 for (; anIt != aLast && !isFocusAccepted; anIt++) {
109 QWidget* aWidget = *anIt;
110 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
112 isFocusAccepted = true;
115 return isFocusAccepted;
118 void ModuleBase_ModelWidget::activate()
120 // the control value is stored to the mode by the focus in on the widget
121 // we need the value is initialized in order to enable the apply button in the property panel.
122 // It should happens in the creation mode only because all fields are filled in the edition mode
123 if (!isEditingMode()) {
124 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
125 if (anAttribute.get() != NULL && !anAttribute->isInitialized()) {
126 if (isComputedDefault()) {
127 if (myFeature->compute(myAttributeID)) {
139 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
141 QWidget* aControl = 0;
143 QList<QWidget*> aControls = getControls();
144 int aSize = aControls.size();
147 for (int i = 0; i < aSize && !aControl; i++) {
148 if (aControls[i]->focusPolicy() != Qt::NoFocus)
149 aControl = aControls[i];
153 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
154 if (aControls[i]->focusPolicy() != Qt::NoFocus)
155 aControl = aControls[i];
161 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
163 myDefaultValue = theValue;
166 bool ModuleBase_ModelWidget::storeValue()
168 setValueState(Stored);
170 emit beforeValuesChanged();
171 bool isDone = storeValueCustom();
172 emit afterValuesChanged();
177 void ModuleBase_ModelWidget::setValueState(const ValueState& theState)
179 if (myState == theState)
183 emit valueStateChanged();
186 bool ModuleBase_ModelWidget::restoreValue()
188 emit beforeValuesRestored();
189 bool isDone = restoreValueCustom();
190 emit afterValuesRestored();
195 void ModuleBase_ModelWidget::storeValueByApply()
197 // do not emit signal about update the currenty feature object
198 // in order to do not perform additional redisplay in the viewer.
199 // It should happens by finish operation of the apply action
203 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj)
205 blockUpdateViewer(true);
207 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
209 blockUpdateViewer(false);
212 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
214 //blockUpdateViewer(true);
216 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
217 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
218 Events_Loop::loop()->flush(anEvent);
220 //blockUpdateViewer(false);
223 bool ModuleBase_ModelWidget::processEnter()
228 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
230 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
231 if (theEvent->type() == QEvent::FocusIn) {
233 // The following two lines are for debugging purpose only
234 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
235 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
237 if (getControls().contains(aWidget)) {
238 emit focusInWidget(this);
241 // pass the event on to the parent class
243 return QObject::eventFilter(theObject, theEvent);
246 //**************************************************************
247 void ModuleBase_ModelWidget::onWidgetValuesChanged()
252 //**************************************************************
253 void ModuleBase_ModelWidget::onWidgetValuesModified()
255 setValueState(Modified);
258 //**************************************************************
259 void ModuleBase_ModelWidget::blockUpdateViewer(const bool theValue)
261 // the viewer update should be blocked in order to avoid the temporary feature content
262 // when the solver processes the feature, the redisplay message can be flushed
263 // what caused the display in the viewer preliminary states of object
264 // e.g. fillet feature, angle value change
265 std::shared_ptr<Events_Message> aMsg;
267 aMsg = std::shared_ptr<Events_Message>(
268 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
271 // the viewer update should be unblocked
272 aMsg = std::shared_ptr<Events_Message>(
273 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
275 Events_Loop::loop()->send(aMsg);