Salome HOME
Construction elements are auxiliary entities:
[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
24 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
25                                                const Config_WidgetAPI* theData,
26                                                const std::string& theParentId)
27     : QWidget(theParent),
28       myParentId(theParentId),
29       myIsEditing(false)
30 {
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);
35
36   connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
37 }
38
39 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
40 {
41   return theObject->data()->attribute(attributeID())->isInitialized();
42 }
43
44 void ModuleBase_ModelWidget::enableFocusProcessing()
45 {
46   QList<QWidget*> aMyControls = getControls();
47   foreach(QWidget*  eachControl, aMyControls) {
48     if (myIsObligatory) {
49       eachControl->setFocusPolicy(Qt::StrongFocus);
50       eachControl->installEventFilter(this);
51     }
52     else {
53       eachControl->setFocusPolicy(Qt::NoFocus);
54     }
55   }
56 }
57
58 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
59 {
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
64     if(aLabel) continue;
65     if(isHighlighted) {
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);
74     } else {
75       QGraphicsEffect* anEffect = aWidget->graphicsEffect();
76       if(anEffect)
77         anEffect->deleteLater();
78       aWidget->setGraphicsEffect(NULL);
79     }
80   }
81 }
82
83 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
84 {
85   myFeature = theFeature;
86   if (theToStoreValue)
87     storeValue();
88 }
89
90 bool ModuleBase_ModelWidget::focusTo()
91 {
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) {
98       aWidget->setFocus();
99       isFocusAccepted = true;
100     }
101   }
102   return isFocusAccepted;
103 }
104
105 void ModuleBase_ModelWidget::activate()
106 {
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)) {
115           restoreValue();
116         }      
117       }
118       else {
119         storeValue();
120       }
121     }
122   }
123   activateCustom();
124 }
125
126 bool ModuleBase_ModelWidget::storeValue()
127 {
128   emit beforeValuesChanged();
129   bool isDone = storeValueCustom();
130   emit afterValuesChanged();
131
132   return isDone;
133 }
134
135 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj) const
136 {
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);
140 }
141
142 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj) const
143 {
144   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
145   ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
146   Events_Loop::loop()->flush(anEvent);
147 }
148
149 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
150 {
151   QWidget* aWidget = qobject_cast<QWidget*>(theObject);
152   if (theEvent->type() == QEvent::FocusIn) {
153     if (getControls().contains(aWidget)) {
154       emit focusInWidget(this);
155     }
156   } 
157   // pass the event on to the parent class
158
159   return QObject::eventFilter(theObject, theEvent);
160 }
161
162 //**************************************************************
163 void ModuleBase_ModelWidget::onWidgetValuesChanged()
164 {
165   storeValue();
166 }