1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_ModelWidget.h
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 myDefaultValue = theData->getProperty(ATTR_DEFAULT);
33 myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
34 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
35 myAttributeID = theData ? theData->widgetId() : "";
36 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
38 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
41 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
43 return theObject->data()->attribute(attributeID())->isInitialized();
46 void ModuleBase_ModelWidget::enableFocusProcessing()
48 QList<QWidget*> aMyControls = getControls();
49 foreach(QWidget* eachControl, aMyControls) {
51 eachControl->setFocusPolicy(Qt::StrongFocus);
52 eachControl->installEventFilter(this);
55 eachControl->setFocusPolicy(Qt::NoFocus);
60 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
62 QList<QWidget*> aWidgetList = getControls();
63 foreach(QWidget* aWidget, aWidgetList) {
64 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
65 // We won't set the effect to QLabels - it looks ugly
68 // If effect is the installed on a different widget, setGraphicsEffect() will
69 // remove the effect from the widget and install it on this widget.
70 // That's why we create a new effect for each widget
71 QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
72 aGlowEffect->setOffset(.0);
73 aGlowEffect->setBlurRadius(10.0);
74 aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
75 aWidget->setGraphicsEffect(aGlowEffect);
77 QGraphicsEffect* anEffect = aWidget->graphicsEffect();
79 anEffect->deleteLater();
80 aWidget->setGraphicsEffect(NULL);
85 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
87 myFeature = theFeature;
92 bool ModuleBase_ModelWidget::focusTo()
94 QList<QWidget*> aControls = getControls();
95 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
96 bool isFocusAccepted = false;
97 for (; anIt != aLast && !isFocusAccepted; anIt++) {
98 QWidget* aWidget = *anIt;
99 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
101 isFocusAccepted = true;
104 return isFocusAccepted;
107 void ModuleBase_ModelWidget::activate()
109 // the control value is stored to the mode by the focus in on the widget
110 // we need the value is initialized in order to enable the apply button in the property panel.
111 // It should happens in the creation mode only because all fields are filled in the edition mode
112 if (!isEditingMode()) {
113 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
114 if (anAttribute.get() != NULL && !anAttribute->isInitialized()) {
115 if (isComputedDefault()) {
116 if (myFeature->compute(myAttributeID)) {
128 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
130 myDefaultValue = theValue;
133 bool ModuleBase_ModelWidget::storeValue()
135 emit beforeValuesChanged();
136 bool isDone = storeValueCustom();
137 emit afterValuesChanged();
142 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj) const
144 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
145 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
146 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
149 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj) const
151 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
152 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
153 Events_Loop::loop()->flush(anEvent);
156 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
158 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
159 if (theEvent->type() == QEvent::FocusIn) {
161 // The following two lines are for debugging purpose only
162 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
163 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
165 if (getControls().contains(aWidget)) {
166 emit focusInWidget(this);
169 // pass the event on to the parent class
171 return QObject::eventFilter(theObject, theEvent);
174 //**************************************************************
175 void ModuleBase_ModelWidget::onWidgetValuesChanged()