Salome HOME
a4368946be30ddbfe501965959559300eb6e3487
[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
35   connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
36 }
37
38 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
39 {
40   return theObject->data()->attribute(attributeID())->isInitialized();
41 }
42
43 void ModuleBase_ModelWidget::enableFocusProcessing()
44 {
45   QList<QWidget*> aMyControls = getControls();
46   foreach(QWidget*  eachControl, aMyControls) {
47     eachControl->setFocusPolicy(Qt::StrongFocus);
48     eachControl->installEventFilter(this);
49   }
50 }
51
52 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
53 {
54   QList<QWidget*> aWidgetList = getControls();
55   foreach(QWidget* aWidget, aWidgetList) {
56     QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
57     // We won't set the effect to QLabels - it looks ugly
58     if(aLabel) continue;
59     if(isHighlighted) {
60       // If effect is the installed on a different widget, setGraphicsEffect() will
61       // remove the effect from the widget and install it on this widget.
62       // That's why we create a new effect for each widget
63       QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
64       aGlowEffect->setOffset(.0);
65       aGlowEffect->setBlurRadius(10.0);
66       aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
67       aWidget->setGraphicsEffect(aGlowEffect);
68     } else {
69       QGraphicsEffect* anEffect = aWidget->graphicsEffect();
70       if(anEffect)
71         anEffect->deleteLater();
72       aWidget->setGraphicsEffect(NULL);
73     }
74   }
75 }
76
77 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
78 {
79   myFeature = theFeature;
80   if (theToStoreValue)
81     storeValue();
82 }
83
84 bool ModuleBase_ModelWidget::focusTo()
85 {
86   QList<QWidget*> aControls = getControls();
87   QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
88   for (; anIt != aLast; anIt++) {
89     QWidget* aWidget = *anIt;
90     if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
91       aWidget->setFocus();
92       break;
93     }
94   }
95   return true;
96 }
97
98 void ModuleBase_ModelWidget::activate()
99 {
100   // the control value is stored to the mode by the focus in on the widget
101   // we need the value is initialized in order to enable the apply button in the property panel.
102   // It should happens in the creation mode only because all fields are filled in the edition mode
103   if (!isEditingMode()) {
104     AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
105     if (anAttribute.get() != NULL && !anAttribute->isInitialized()) {
106       if (isComputedDefault()) {
107         if (myFeature->compute(myAttributeID)) {
108           restoreValue();
109         }      
110       }
111       else {
112         storeValue();
113       }
114     }
115   }
116   activateCustom();
117 }
118
119 bool ModuleBase_ModelWidget::storeValue()
120 {
121   emit beforeValuesChanged();
122   bool isDone = storeValueCustom();
123   emit afterValuesChanged();
124
125   return isDone;
126 }
127
128 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj) const
129 {
130   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
131   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
132   ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
133 }
134
135 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj) const
136 {
137   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
138   ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
139   Events_Loop::loop()->flush(anEvent);
140 }
141
142 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
143 {
144   QWidget* aWidget = qobject_cast<QWidget*>(theObject);
145   if (theEvent->type() == QEvent::FocusIn) {
146     if (getControls().contains(aWidget)) {
147       emit focusInWidget(this);
148     }
149   } 
150   // pass the event on to the parent class
151
152   return QObject::eventFilter(theObject, theEvent);
153 }
154
155 //**************************************************************
156 void ModuleBase_ModelWidget::onWidgetValuesChanged()
157 {
158   storeValue();
159 }