Salome HOME
Set default value for case id attribute of Paged Containers (like Toolbox)
[modules/shaper.git] / src / ModuleBase / ModuleBase_ModelWidget.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_ModelWidget.h
4 // Created:     25 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #include "ModuleBase_ModelWidget.h"
8
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Attribute.h>
11 #include <ModelAPI_Events.h>
12 #include <ModelAPI_Session.h>
13
14 #include <Config_Keywords.h>
15 #include <Config_WidgetAPI.h>
16
17 #include <Events_Loop.h>
18
19 #include <QEvent>
20 #include <QGraphicsDropShadowEffect>
21 #include <QColor>
22 #include <QLabel>
23 #include <QFocusEvent>
24
25 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
26                                                const Config_WidgetAPI* theData,
27                                                const std::string& theParentId)
28     : QWidget(theParent),
29       myParentId(theParentId),
30       myIsEditing(false)
31 {
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);
36
37   connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
38 }
39
40 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
41 {
42   return theObject->data()->attribute(attributeID())->isInitialized();
43 }
44
45 void ModuleBase_ModelWidget::enableFocusProcessing()
46 {
47   QList<QWidget*> aMyControls = getControls();
48   foreach(QWidget*  eachControl, aMyControls) {
49     if (myIsObligatory) {
50       eachControl->setFocusPolicy(Qt::StrongFocus);
51       eachControl->installEventFilter(this);
52     }
53     else {
54       eachControl->setFocusPolicy(Qt::NoFocus);
55     }
56   }
57 }
58
59 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
60 {
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
65     if(aLabel) continue;
66     if(isHighlighted) {
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);
75     } else {
76       QGraphicsEffect* anEffect = aWidget->graphicsEffect();
77       if(anEffect)
78         anEffect->deleteLater();
79       aWidget->setGraphicsEffect(NULL);
80     }
81   }
82 }
83
84 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
85 {
86   myFeature = theFeature;
87   if (theToStoreValue)
88     storeValue();
89 }
90
91 bool ModuleBase_ModelWidget::focusTo()
92 {
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) {
99       aWidget->setFocus();
100       isFocusAccepted = true;
101     }
102   }
103   return isFocusAccepted;
104 }
105
106 void ModuleBase_ModelWidget::activate()
107 {
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)) {
116           restoreValue();
117         }      
118       }
119       else {
120         storeValue();
121       }
122     }
123   }
124   activateCustom();
125 }
126
127 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
128 {
129   myDefaultValue = theValue;
130 }
131
132 bool ModuleBase_ModelWidget::storeValue()
133 {
134   emit beforeValuesChanged();
135   bool isDone = storeValueCustom();
136   emit afterValuesChanged();
137
138   return isDone;
139 }
140
141 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj) const
142 {
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);
146 }
147
148 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj) const
149 {
150   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
151   ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
152   Events_Loop::loop()->flush(anEvent);
153 }
154
155 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
156 {
157   QWidget* aWidget = qobject_cast<QWidget*>(theObject);
158   if (theEvent->type() == QEvent::FocusIn) {
159     #ifdef _DEBUG
160     // The following two lines are for debugging purpose only
161     QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
162     bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
163     #endif
164     if (getControls().contains(aWidget)) {
165       emit focusInWidget(this);
166     }
167   } 
168   // pass the event on to the parent class
169
170   return QObject::eventFilter(theObject, theEvent);
171 }
172
173 //**************************************************************
174 void ModuleBase_ModelWidget::onWidgetValuesChanged()
175 {
176   storeValue();
177 }