Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / ModuleBase / ModuleBase_ModelWidget.cpp
1 // File:        ModuleBase_ModelWidget.h
2 // Created:     25 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include "ModuleBase_ModelWidget.h"
6
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_Attribute.h>
9 #include <ModelAPI_Events.h>
10 #include <ModelAPI_Session.h>
11
12 #include <Config_Keywords.h>
13 #include <Config_WidgetAPI.h>
14
15 #include <Events_Loop.h>
16
17 #include <QEvent>
18 #include <QWidget>
19 #include <QGraphicsDropShadowEffect>
20 #include <QColor>
21 #include <QLabel>
22
23 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent, const Config_WidgetAPI* theData,
24                                                const std::string& theParentId)
25     : QObject(theParent),
26       myParentId(theParentId)
27 {
28   myIsComputedDefault = false;
29   myIsObligatory = theData ? theData->getBooleanAttribute(FEATURE_OBLIGATORY, true) : true;
30   myAttributeID = theData ? theData->widgetId() : "";
31 }
32
33 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
34 {
35   return theObject->data()->attribute(attributeID())->isInitialized();
36 }
37
38 void ModuleBase_ModelWidget::enableFocusProcessing()
39 {
40   QList<QWidget*> aMyControls = getControls();
41   foreach(QWidget*  eachControl, aMyControls) {
42     if(!myFocusInWidgets.contains(eachControl)) {
43       enableFocusProcessing(eachControl);
44     }
45   }
46 }
47
48 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
49 {
50   QList<QWidget*> aWidgetList = getControls();
51   foreach(QWidget* aWidget, aWidgetList) {
52     QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
53     // We won't set the effect to QLabels - it looks ugly
54     if(aLabel) continue;
55     if(isHighlighted) {
56       // If effect is the installed on a different widget, setGraphicsEffect() will
57       // remove the effect from the widget and install it on this widget.
58       // That's why we create a new effect for each widget
59       QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
60       aGlowEffect->setOffset(.0);
61       aGlowEffect->setBlurRadius(10.0);
62       aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
63       aWidget->setGraphicsEffect(aGlowEffect);
64     } else {
65       QGraphicsEffect* anEffect = aWidget->graphicsEffect();
66       if(anEffect)
67         anEffect->deleteLater();
68       aWidget->setGraphicsEffect(NULL);
69     }
70   }
71 }
72
73 bool ModuleBase_ModelWidget::focusTo()
74 {
75   QList<QWidget*> aControls = getControls();
76   QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
77   for (; anIt != aLast; anIt++) {
78     QWidget* aWidget = *anIt;
79     if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
80       aWidget->setFocus();
81       break;
82     }
83   }
84   return true;
85 }
86
87
88 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj) const
89 {
90   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
91   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
92   ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
93 }
94
95 void ModuleBase_ModelWidget::enableFocusProcessing(QWidget* theWidget)
96 {
97   theWidget->setFocusPolicy(Qt::StrongFocus);
98   theWidget->installEventFilter(this);
99   myFocusInWidgets.append(theWidget);
100 }
101
102 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
103 {
104   QWidget* aWidget = qobject_cast<QWidget*>(theObject);
105   if (theEvent->type() == QEvent::MouseButtonRelease && 
106       myFocusInWidgets.contains(aWidget)) {
107     emit focusInWidget(this);
108   } 
109   // pass the event on to the parent class
110   return QObject::eventFilter(theObject, theEvent);
111 }