Salome HOME
Merge branch 'Dev_0.7.1' of newgeom:newgeom.git into Dev_0.7.1
[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 <QWidget>
21 #include <QGraphicsDropShadowEffect>
22 #include <QColor>
23 #include <QLabel>
24
25 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent, const Config_WidgetAPI* theData,
26                                                const std::string& theParentId)
27     : QObject(theParent),
28       myParentId(theParentId)
29 {
30   myIsValueDefault = !theData->getProperty(ATTR_DEFAULT).empty();
31   myIsComputedDefault = false;
32   myAttributeID = theData ? theData->widgetId() : "";
33 }
34
35 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
36 {
37   return theObject->data()->attribute(attributeID())->isInitialized();
38 }
39
40 void ModuleBase_ModelWidget::enableFocusProcessing()
41 {
42   QList<QWidget*> aMyControls = getControls();
43   foreach(QWidget*  eachControl, aMyControls) {
44     eachControl->setFocusPolicy(Qt::StrongFocus);
45     eachControl->installEventFilter(this);
46   }
47 }
48
49 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
50 {
51   QList<QWidget*> aWidgetList = getControls();
52   foreach(QWidget* aWidget, aWidgetList) {
53     QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
54     // We won't set the effect to QLabels - it looks ugly
55     if(aLabel) continue;
56     if(isHighlighted) {
57       // If effect is the installed on a different widget, setGraphicsEffect() will
58       // remove the effect from the widget and install it on this widget.
59       // That's why we create a new effect for each widget
60       QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
61       aGlowEffect->setOffset(.0);
62       aGlowEffect->setBlurRadius(10.0);
63       aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
64       aWidget->setGraphicsEffect(aGlowEffect);
65     } else {
66       QGraphicsEffect* anEffect = aWidget->graphicsEffect();
67       if(anEffect)
68         anEffect->deleteLater();
69       aWidget->setGraphicsEffect(NULL);
70     }
71   }
72 }
73
74 bool ModuleBase_ModelWidget::focusTo()
75 {
76   QList<QWidget*> aControls = getControls();
77   QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
78   for (; anIt != aLast; anIt++) {
79     QWidget* aWidget = *anIt;
80     if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
81       aWidget->setFocus();
82       break;
83     }
84   }
85   return true;
86 }
87
88 void ModuleBase_ModelWidget::activate()
89 {
90   if (!isEditingMode()) {
91     // the control value is stored to the mode by the focus in on the widget
92     // we need the value is initialized in order to enable the apply button in the property panel
93     // it should happens only in the creation mode because during edition all fields are filled
94     storeValue();
95   }
96   activateCustom();
97 }
98
99 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj) const
100 {
101   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
102   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
103   ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
104 }
105
106 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj) const
107 {
108   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
109   ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
110   Events_Loop::loop()->flush(anEvent);
111 }
112
113 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
114 {
115   QWidget* aWidget = qobject_cast<QWidget*>(theObject);
116   if (theEvent->type() == QEvent::FocusIn) {
117     if (getControls().contains(aWidget)) {
118       emit focusInWidget(this);
119     }
120   } 
121   // pass the event on to the parent class
122
123   return QObject::eventFilter(theObject, theEvent);
124 }