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