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>
24 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
25 const Config_WidgetAPI* theData,
26 const std::string& theParentId)
28 myParentId(theParentId),
31 myDefaultValue = theData->getProperty(ATTR_DEFAULT);
32 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
33 myAttributeID = theData ? theData->widgetId() : "";
34 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
36 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
39 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
41 return theObject->data()->attribute(attributeID())->isInitialized();
44 void ModuleBase_ModelWidget::enableFocusProcessing()
46 QList<QWidget*> aMyControls = getControls();
47 foreach(QWidget* eachControl, aMyControls) {
49 eachControl->setFocusPolicy(Qt::StrongFocus);
50 eachControl->installEventFilter(this);
53 eachControl->setFocusPolicy(Qt::NoFocus);
58 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
60 QList<QWidget*> aWidgetList = getControls();
61 foreach(QWidget* aWidget, aWidgetList) {
62 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
63 // We won't set the effect to QLabels - it looks ugly
66 // If effect is the installed on a different widget, setGraphicsEffect() will
67 // remove the effect from the widget and install it on this widget.
68 // That's why we create a new effect for each widget
69 QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
70 aGlowEffect->setOffset(.0);
71 aGlowEffect->setBlurRadius(10.0);
72 aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
73 aWidget->setGraphicsEffect(aGlowEffect);
75 QGraphicsEffect* anEffect = aWidget->graphicsEffect();
77 anEffect->deleteLater();
78 aWidget->setGraphicsEffect(NULL);
83 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
85 myFeature = theFeature;
90 bool ModuleBase_ModelWidget::focusTo()
92 QList<QWidget*> aControls = getControls();
93 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
94 bool isFocusAccepted = false;
95 for (; anIt != aLast && !isFocusAccepted; anIt++) {
96 QWidget* aWidget = *anIt;
97 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
99 isFocusAccepted = true;
102 return isFocusAccepted;
105 void ModuleBase_ModelWidget::activate()
107 // the control value is stored to the mode by the focus in on the widget
108 // we need the value is initialized in order to enable the apply button in the property panel.
109 // It should happens in the creation mode only because all fields are filled in the edition mode
110 if (!isEditingMode()) {
111 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
112 if (anAttribute.get() != NULL && !anAttribute->isInitialized()) {
113 if (isComputedDefault()) {
114 if (myFeature->compute(myAttributeID)) {
126 bool ModuleBase_ModelWidget::storeValue()
128 emit beforeValuesChanged();
129 bool isDone = storeValueCustom();
130 emit afterValuesChanged();
135 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj) const
137 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
138 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
139 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
142 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj) const
144 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
145 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
146 Events_Loop::loop()->flush(anEvent);
149 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
151 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
152 if (theEvent->type() == QEvent::FocusIn) {
153 if (getControls().contains(aWidget)) {
154 emit focusInWidget(this);
157 // pass the event on to the parent class
159 return QObject::eventFilter(theObject, theEvent);
162 //**************************************************************
163 void ModuleBase_ModelWidget::onWidgetValuesChanged()