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 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
34 myAttributeID = theData ? theData->widgetId() : "";
35 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
37 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
40 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
42 return theObject->data()->attribute(attributeID())->isInitialized();
45 void ModuleBase_ModelWidget::enableFocusProcessing()
47 QList<QWidget*> aMyControls = getControls();
48 foreach(QWidget* eachControl, aMyControls) {
50 eachControl->setFocusPolicy(Qt::StrongFocus);
51 eachControl->installEventFilter(this);
54 eachControl->setFocusPolicy(Qt::NoFocus);
59 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
61 QList<QWidget*> aWidgetList = getControls();
62 foreach(QWidget* aWidget, aWidgetList) {
63 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
64 // We won't set the effect to QLabels - it looks ugly
67 // If effect is the installed on a different widget, setGraphicsEffect() will
68 // remove the effect from the widget and install it on this widget.
69 // That's why we create a new effect for each widget
70 QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
71 aGlowEffect->setOffset(.0);
72 aGlowEffect->setBlurRadius(10.0);
73 aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
74 aWidget->setGraphicsEffect(aGlowEffect);
76 QGraphicsEffect* anEffect = aWidget->graphicsEffect();
78 anEffect->deleteLater();
79 aWidget->setGraphicsEffect(NULL);
84 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
86 myFeature = theFeature;
91 bool ModuleBase_ModelWidget::focusTo()
93 QList<QWidget*> aControls = getControls();
94 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
95 bool isFocusAccepted = false;
96 for (; anIt != aLast && !isFocusAccepted; anIt++) {
97 QWidget* aWidget = *anIt;
98 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
100 isFocusAccepted = true;
103 return isFocusAccepted;
106 void ModuleBase_ModelWidget::activate()
108 // the control value is stored to the mode by the focus in on the widget
109 // we need the value is initialized in order to enable the apply button in the property panel.
110 // It should happens in the creation mode only because all fields are filled in the edition mode
111 if (!isEditingMode()) {
112 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
113 if (anAttribute.get() != NULL && !anAttribute->isInitialized()) {
114 if (isComputedDefault()) {
115 if (myFeature->compute(myAttributeID)) {
127 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
129 myDefaultValue = theValue;
132 bool ModuleBase_ModelWidget::storeValue()
134 emit beforeValuesChanged();
135 bool isDone = storeValueCustom();
136 emit afterValuesChanged();
141 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj) const
143 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
144 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
145 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
148 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj) const
150 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
151 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
152 Events_Loop::loop()->flush(anEvent);
155 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
157 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
158 if (theEvent->type() == QEvent::FocusIn) {
160 // The following two lines are for debugging purpose only
161 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
162 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
164 if (getControls().contains(aWidget)) {
165 emit focusInWidget(this);
168 // pass the event on to the parent class
170 return QObject::eventFilter(theObject, theEvent);
173 //**************************************************************
174 void ModuleBase_ModelWidget::onWidgetValuesChanged()