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"
8 #include "ModuleBase_ViewerPrs.h"
9 #include "ModuleBase_Tools.h"
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Attribute.h>
13 #include <ModelAPI_Events.h>
14 #include <ModelAPI_Session.h>
15 #include <ModelAPI_Validator.h>
17 #include <Config_Keywords.h>
18 #include <Config_WidgetAPI.h>
20 #include <Events_Loop.h>
24 #include <QFocusEvent>
26 //#define DEBUG_VALUE_STATE
28 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
29 const Config_WidgetAPI* theData)
33 myIsValueStateBlocked(false)
35 myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
37 myDefaultValue = theData->getProperty(ATTR_DEFAULT);
38 myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
39 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
40 myAttributeID = theData ? theData->widgetId() : "";
41 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
43 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
44 connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
47 bool ModuleBase_ModelWidget::reset()
49 bool aResult = resetCustom();
56 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
58 return theObject->data()->attribute(attributeID())->isInitialized();
61 QString ModuleBase_ModelWidget::getValueStateError() const
65 ModuleBase_ModelWidget::ValueState aState = getValueState();
66 if (aState != ModuleBase_ModelWidget::Stored) {
67 AttributePtr anAttr = feature()->attribute(attributeID());
69 QString anAttributeName = anAttr->id().c_str();
71 case ModuleBase_ModelWidget::ModifiedInViewer:
72 anError = "Attribute \"" + anAttributeName +
73 "\" is locked by modification value in the viewer.";
75 case ModuleBase_ModelWidget::Reset:
76 anError = "Attribute \"" + anAttributeName + "\" is not initialized.";
78 case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
87 QString ModuleBase_ModelWidget::getError() const
94 std::string anAttributeID = attributeID();
95 AttributePtr anAttribute = feature()->attribute(anAttributeID);
96 if (!anAttribute.get())
99 std::string aValidatorID;
100 std::string anErrorMsg;
102 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
103 if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
104 if (anErrorMsg.empty())
105 anErrorMsg = "unknown error.";
106 anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg;
109 anError = QString::fromStdString(anErrorMsg);
110 if (anError.isEmpty())
111 anError = getValueStateError();
116 void ModuleBase_ModelWidget::enableFocusProcessing()
118 QList<QWidget*> aMyControls = getControls();
119 foreach(QWidget* eachControl, aMyControls) {
120 eachControl->setFocusPolicy(Qt::StrongFocus);
121 eachControl->installEventFilter(this);
125 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
127 QList<QWidget*> aWidgetList = getControls();
128 foreach(QWidget* aWidget, aWidgetList) {
129 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
130 // We won't set the effect to QLabels - it looks ugly
132 // If effect is the installed on a different widget, setGraphicsEffect() will
133 // remove the effect from the widget and install it on this widget.
134 // That's why we create a new effect for each widget
135 ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
139 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
141 myFeature = theFeature;
146 bool ModuleBase_ModelWidget::focusTo()
148 QList<QWidget*> aControls = getControls();
149 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
150 bool isFocusAccepted = false;
151 for (; anIt != aLast && !isFocusAccepted; anIt++) {
152 QWidget* aWidget = *anIt;
153 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
154 ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
155 isFocusAccepted = true;
158 return isFocusAccepted;
161 void ModuleBase_ModelWidget::activate()
163 // the control value is stored to the mode by the focus in on the widget
164 // we need the value is initialized in order to enable the apply button in the property panel.
165 // It should happens in the creation mode only because all fields are filled in the edition mode
166 if (!isEditingMode()) {
167 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
168 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
169 initializeValueByActivate();
174 void ModuleBase_ModelWidget::deactivate()
176 myIsValueStateBlocked = false;
177 if (myState == ModifiedInPP || myState == ModifiedInViewer)
182 void ModuleBase_ModelWidget::initializeValueByActivate()
184 if (isComputedDefault()) {
185 if (myFeature->compute(myAttributeID)) {
194 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
196 QWidget* aControl = 0;
198 QList<QWidget*> aControls = getControls();
199 int aSize = aControls.size();
202 for (int i = 0; i < aSize && !aControl; i++) {
203 if (aControls[i]->focusPolicy() != Qt::NoFocus)
204 aControl = aControls[i];
208 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
209 if (aControls[i]->focusPolicy() != Qt::NoFocus)
210 aControl = aControls[i];
216 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
218 myDefaultValue = theValue;
221 bool ModuleBase_ModelWidget::storeValue()
223 setValueState(Stored);
225 emit beforeValuesChanged();
226 bool isDone = storeValueCustom();
227 emit afterValuesChanged();
231 #ifdef DEBUG_VALUE_STATE
232 std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
236 case ModuleBase_ModelWidget::Stored: anInfo = "Stored "; break;
237 case ModuleBase_ModelWidget::ModifiedInPP: anInfo = "ModifiedInPP "; break;
238 case ModuleBase_ModelWidget::ModifiedInViewer: anInfo = "ModifiedInViewer"; break;
239 case ModuleBase_ModelWidget::Reset: anInfo = "Reset "; break;
246 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
247 (const ModuleBase_ModelWidget::ValueState& theState)
249 ValueState aState = myState;
251 if (myState != theState && !myIsValueStateBlocked) {
252 #ifdef DEBUG_VALUE_STATE
253 qDebug(QString("setValueState: previous state = %1,\t new state = %2")
254 .arg(getDebugInfo(myState).c_str())
255 .arg(getDebugInfo(theState).c_str()).toStdString().c_str());
258 emit valueStateChanged(aState);
263 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
265 bool isBlocked = myIsValueStateBlocked;
266 myIsValueStateBlocked = theBlocked;
270 bool ModuleBase_ModelWidget::restoreValue()
272 emit beforeValuesRestored();
273 bool isDone = restoreValueCustom();
274 emit afterValuesRestored();
279 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj)
281 blockUpdateViewer(true);
283 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
285 blockUpdateViewer(false);
288 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
290 //blockUpdateViewer(true);
292 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
293 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
294 Events_Loop::loop()->flush(anEvent);
296 //blockUpdateViewer(false);
299 bool ModuleBase_ModelWidget::processEnter()
304 bool ModuleBase_ModelWidget::processDelete()
306 // we consider that model objects eats delete key in order to
307 // do nothing by for example symbol delete in line edit or spin box
311 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
313 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
314 if (theEvent->type() == QEvent::FocusIn) {
316 // The following two lines are for debugging purpose only
317 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
318 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
320 if (getControls().contains(aWidget)) {
321 emit focusInWidget(this);
324 else if (theEvent->type() == QEvent::FocusOut) {
325 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
327 Qt::FocusReason aReason = aFocusEvent->reason();
328 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
329 aReason == Qt::TabFocusReason ||
330 aReason == Qt::BacktabFocusReason ||
331 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
332 if (aMouseOrKey && getControls().contains(aWidget)) {
333 if (getValueState() == ModifiedInPP) {
338 // pass the event on to the parent class
340 return QObject::eventFilter(theObject, theEvent);
343 //**************************************************************
344 void ModuleBase_ModelWidget::onWidgetValuesChanged()
349 //**************************************************************
350 void ModuleBase_ModelWidget::onWidgetValuesModified()
352 setValueState(ModifiedInPP);
355 //**************************************************************
356 void ModuleBase_ModelWidget::blockUpdateViewer(const bool theValue)
358 // the viewer update should be blocked in order to avoid the temporary feature content
359 // when the solver processes the feature, the redisplay message can be flushed
360 // what caused the display in the viewer preliminary states of object
361 // e.g. fillet feature, angle value change
362 std::shared_ptr<Events_Message> aMsg;
364 aMsg = std::shared_ptr<Events_Message>(
365 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
368 // the viewer update should be unblocked
369 aMsg = std::shared_ptr<Events_Message>(
370 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
372 Events_Loop::loop()->send(aMsg);