1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_ModelWidget.cpp
4 // Created: 25 Apr 2014
5 // Author: Natalia ERMOLAEVA
7 #include "ModuleBase_ModelWidget.h"
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Attribute.h>
11 #include <ModelAPI_Events.h>
12 #include <ModelAPI_Session.h>
14 #include <Config_Keywords.h>
15 #include <Config_WidgetAPI.h>
17 #include <Events_Loop.h>
20 #include <QGraphicsDropShadowEffect>
23 #include <QFocusEvent>
25 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
26 const Config_WidgetAPI* theData,
27 const std::string& theParentId)
29 myParentId(theParentId),
32 myIsValueStateBlocked(false)
34 myDefaultValue = theData->getProperty(ATTR_DEFAULT);
35 myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
36 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
37 myAttributeID = theData ? theData->widgetId() : "";
38 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
40 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
41 connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
44 bool ModuleBase_ModelWidget::reset()
46 bool aResult = resetCustom();
53 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
55 return theObject->data()->attribute(attributeID())->isInitialized();
58 QString ModuleBase_ModelWidget::getValueStateError() const
62 ModuleBase_ModelWidget::ValueState aState = getValueState();
63 if (aState != ModuleBase_ModelWidget::Stored) {
64 AttributePtr anAttr = feature()->attribute(attributeID());
66 QString anAttributeName = anAttr->id().c_str();
68 case ModuleBase_ModelWidget::ModifiedInPP:
69 anError = "Attribute \"" + anAttributeName +
70 "\" modification is not applyed. Please click \"Enter\" or \"Tab\".";
72 case ModuleBase_ModelWidget::ModifiedInViewer:
73 anError = "Attribute \"" + anAttributeName +
74 "\" is locked by modification value in the viewer.";
76 case ModuleBase_ModelWidget::Reset:
77 anError = "Attribute \"" + anAttributeName + "\" is not initialized.";
85 void ModuleBase_ModelWidget::enableFocusProcessing()
87 QList<QWidget*> aMyControls = getControls();
88 foreach(QWidget* eachControl, aMyControls) {
90 eachControl->setFocusPolicy(Qt::StrongFocus);
91 eachControl->installEventFilter(this);
94 eachControl->setFocusPolicy(Qt::NoFocus);
99 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
101 QList<QWidget*> aWidgetList = getControls();
102 foreach(QWidget* aWidget, aWidgetList) {
103 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
104 // We won't set the effect to QLabels - it looks ugly
107 // If effect is the installed on a different widget, setGraphicsEffect() will
108 // remove the effect from the widget and install it on this widget.
109 // That's why we create a new effect for each widget
110 QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
111 aGlowEffect->setOffset(.0);
112 aGlowEffect->setBlurRadius(10.0);
113 aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
114 aWidget->setGraphicsEffect(aGlowEffect);
116 QGraphicsEffect* anEffect = aWidget->graphicsEffect();
118 anEffect->deleteLater();
119 aWidget->setGraphicsEffect(NULL);
124 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
126 myFeature = theFeature;
131 bool ModuleBase_ModelWidget::focusTo()
133 QList<QWidget*> aControls = getControls();
134 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
135 bool isFocusAccepted = false;
136 for (; anIt != aLast && !isFocusAccepted; anIt++) {
137 QWidget* aWidget = *anIt;
138 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
140 isFocusAccepted = true;
143 return isFocusAccepted;
146 void ModuleBase_ModelWidget::activate()
148 // the control value is stored to the mode by the focus in on the widget
149 // we need the value is initialized in order to enable the apply button in the property panel.
150 // It should happens in the creation mode only because all fields are filled in the edition mode
151 if (!isEditingMode()) {
152 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
153 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
154 initializeValueByActivate();
159 void ModuleBase_ModelWidget::deactivate()
161 myIsValueStateBlocked = false;
162 if (myState == ModifiedInPP)
167 void ModuleBase_ModelWidget::initializeValueByActivate()
169 if (isComputedDefault()) {
170 if (myFeature->compute(myAttributeID)) {
179 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
181 QWidget* aControl = 0;
183 QList<QWidget*> aControls = getControls();
184 int aSize = aControls.size();
187 for (int i = 0; i < aSize && !aControl; i++) {
188 if (aControls[i]->focusPolicy() != Qt::NoFocus)
189 aControl = aControls[i];
193 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
194 if (aControls[i]->focusPolicy() != Qt::NoFocus)
195 aControl = aControls[i];
201 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
203 myDefaultValue = theValue;
206 bool ModuleBase_ModelWidget::storeValue()
208 setValueState(Stored);
210 emit beforeValuesChanged();
211 bool isDone = storeValueCustom();
212 emit afterValuesChanged();
217 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState(const ModuleBase_ModelWidget::ValueState& theState)
219 ValueState aState = myState;
220 if (myState != theState && !myIsValueStateBlocked) {
222 emit valueStateChanged(aState);
227 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
229 bool isBlocked = myIsValueStateBlocked;
230 myIsValueStateBlocked = theBlocked;
234 bool ModuleBase_ModelWidget::restoreValue()
236 emit beforeValuesRestored();
237 bool isDone = restoreValueCustom();
238 emit afterValuesRestored();
243 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj)
245 blockUpdateViewer(true);
247 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
249 blockUpdateViewer(false);
252 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
254 //blockUpdateViewer(true);
256 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
257 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
258 Events_Loop::loop()->flush(anEvent);
260 //blockUpdateViewer(false);
263 bool ModuleBase_ModelWidget::processEnter()
268 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
270 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
271 if (theEvent->type() == QEvent::FocusIn) {
273 // The following two lines are for debugging purpose only
274 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
275 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
277 if (getControls().contains(aWidget)) {
278 emit focusInWidget(this);
281 else if (theEvent->type() == QEvent::FocusOut) {
282 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
284 Qt::FocusReason aReason = aFocusEvent->reason();
285 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
286 aReason == Qt::TabFocusReason ||
287 aReason == Qt::BacktabFocusReason ||
288 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
289 if (aMouseOrKey && getControls().contains(aWidget) && getValueState() == ModifiedInPP)
292 // pass the event on to the parent class
294 return QObject::eventFilter(theObject, theEvent);
297 //**************************************************************
298 void ModuleBase_ModelWidget::onWidgetValuesChanged()
303 //**************************************************************
304 void ModuleBase_ModelWidget::onWidgetValuesModified()
306 setValueState(ModifiedInPP);
309 //**************************************************************
310 void ModuleBase_ModelWidget::blockUpdateViewer(const bool theValue)
312 // the viewer update should be blocked in order to avoid the temporary feature content
313 // when the solver processes the feature, the redisplay message can be flushed
314 // what caused the display in the viewer preliminary states of object
315 // e.g. fillet feature, angle value change
316 std::shared_ptr<Events_Message> aMsg;
318 aMsg = std::shared_ptr<Events_Message>(
319 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
322 // the viewer update should be unblocked
323 aMsg = std::shared_ptr<Events_Message>(
324 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
326 Events_Loop::loop()->send(aMsg);