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   myAttributeID = theData ? theData->widgetId() : "";
30 }
31
32 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
33 {
34   return theObject->data()->attribute(attributeID())->isInitialized();
35 }
36
37 void ModuleBase_ModelWidget::enableFocusProcessing()
38 {
39   QList<QWidget*> aMyControls = getControls();
40   foreach(QWidget*  eachControl, aMyControls) {
41     eachControl->setFocusPolicy(Qt::StrongFocus);
42     eachControl->installEventFilter(this);
43   }
44 }
45
46 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
47 {
48   QList<QWidget*> aWidgetList = getControls();
49   foreach(QWidget* aWidget, aWidgetList) {
50     QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
51     // We won't set the effect to QLabels - it looks ugly
52     if(aLabel) continue;
53     if(isHighlighted) {
54       // If effect is the installed on a different widget, setGraphicsEffect() will
55       // remove the effect from the widget and install it on this widget.
56       // That's why we create a new effect for each widget
57       QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
58       aGlowEffect->setOffset(.0);
59       aGlowEffect->setBlurRadius(10.0);
60       aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
61       aWidget->setGraphicsEffect(aGlowEffect);
62     } else {
63       QGraphicsEffect* anEffect = aWidget->graphicsEffect();
64       if(anEffect)
65         anEffect->deleteLater();
66       aWidget->setGraphicsEffect(NULL);
67     }
68   }
69 }
70
71 bool ModuleBase_ModelWidget::focusTo()
72 {
73   QList<QWidget*> aControls = getControls();
74   QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
75   for (; anIt != aLast; anIt++) {
76     QWidget* aWidget = *anIt;
77     if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
78       aWidget->setFocus();
79       break;
80     }
81   }
82   return true;
83 }
84
85
86 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj) const
87 {
88   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
89   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
90   ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
91 }
92
93 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
94 {
95   QWidget* aWidget = qobject_cast<QWidget*>(theObject);
96   if (theEvent->type() == QEvent::FocusIn) {
97     if (getControls().contains(aWidget)) {
98       emit focusInWidget(this);
99     }
100   } 
101   // pass the event on to the parent class
102
103   return QObject::eventFilter(theObject, theEvent);
104 }