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