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"
10 #include "ModuleBase_WidgetValidator.h"
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_Attribute.h>
14 #include <ModelAPI_Events.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_Validator.h>
18 #include <Config_Keywords.h>
19 #include <Config_WidgetAPI.h>
20 #include <Config_Translator.h>
22 #include <Events_Loop.h>
26 #include <QFocusEvent>
29 //#define DEBUG_VALUE_STATE
31 //#define DEBUG_WIDGET_INSTANCE
33 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
34 const Config_WidgetAPI* theData)
38 myIsValueStateBlocked(false),
39 myFlushUpdateBlocked(false),
42 #ifdef DEBUG_WIDGET_INSTANCE
43 qDebug("ModuleBase_ModelWidget::ModuleBase_ModelWidget");
46 myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
48 myDefaultValue = theData->getProperty(ATTR_DEFAULT);
49 myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
50 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
51 myAttributeID = theData ? theData->widgetId() : "";
52 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
54 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
55 connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
58 ModuleBase_ModelWidget::~ModuleBase_ModelWidget()
60 #ifdef DEBUG_WIDGET_INSTANCE
61 qDebug("ModuleBase_ModelWidget::~ModuleBase_ModelWidget");
65 bool ModuleBase_ModelWidget::reset()
67 bool aResult = resetCustom();
74 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
76 return theObject->data()->attribute(attributeID())->isInitialized();
79 void ModuleBase_ModelWidget::processValueState()
81 if (myState == ModifiedInPP || myState == ModifiedInViewer)
85 QString ModuleBase_ModelWidget::getValueStateError() const
89 ModuleBase_ModelWidget::ValueState aState = getValueState();
90 if (aState != ModuleBase_ModelWidget::Stored) {
91 AttributePtr anAttr = feature()->attribute(attributeID());
93 QString anAttributeName = anAttr->id().c_str();
95 case ModuleBase_ModelWidget::ModifiedInViewer:
96 anError = "Attribute \"" + anAttributeName +
97 "\" is locked by modification value in the viewer.";
99 case ModuleBase_ModelWidget::Reset:
100 anError = "Attribute \"" + anAttributeName + "\" is not initialized.";
102 case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
111 QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
115 if (!feature().get())
118 std::string anAttributeID = attributeID();
119 AttributePtr anAttribute = feature()->attribute(anAttributeID);
120 if (!anAttribute.get())
123 std::string aValidatorID;
124 std::string anErrorMsg;
126 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
127 if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
128 if (anErrorMsg.empty())
129 anErrorMsg = "unknown error.";
130 anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg;
133 anError = QString::fromStdString(anErrorMsg);
134 if (anError.isEmpty() && theValueStateChecked)
135 anError = getValueStateError();
137 anError = translateString(anError);
142 QString ModuleBase_ModelWidget::translateString(const QString& theMsg) const
144 if (!theMsg.isEmpty()) {
145 std::string aContext = feature()->getKind();
146 std::string aStr = Config_Translator::translate(aContext, theMsg.toStdString().c_str());
147 std::string aCodec = Config_Translator::codec(aContext);
148 return QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
154 void ModuleBase_ModelWidget::enableFocusProcessing()
156 QList<QWidget*> aMyControls = getControls();
157 foreach(QWidget* eachControl, aMyControls) {
158 eachControl->setFocusPolicy(Qt::StrongFocus);
159 eachControl->installEventFilter(this);
163 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
165 QList<QWidget*> aWidgetList = getControls();
166 foreach(QWidget* aWidget, aWidgetList) {
167 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
168 // We won't set the effect to QLabels - it looks ugly
170 // If effect is the installed on a different widget, setGraphicsEffect() will
171 // remove the effect from the widget and install it on this widget.
172 // That's why we create a new effect for each widget
173 ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
177 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue,
178 const bool isUpdateFlushed)
180 /// it is possible to give this flag as parameter in storeValue/storeCustomValue
181 /// after debug, it may be corrected
182 myFlushUpdateBlocked = !isUpdateFlushed;
183 myFeature = theFeature;
186 myFlushUpdateBlocked = false;
189 bool ModuleBase_ModelWidget::focusTo()
191 #ifdef DEBUG_WIDGET_INSTANCE
192 qDebug("ModuleBase_ModelWidget::focusTo");
194 QList<QWidget*> aControls = getControls();
195 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
196 bool isFocusAccepted = false;
197 for (; anIt != aLast && !isFocusAccepted; anIt++) {
198 QWidget* aWidget = *anIt;
199 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
200 ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
201 isFocusAccepted = true;
204 return isFocusAccepted;
207 void ModuleBase_ModelWidget::activate()
209 #ifdef DEBUG_WIDGET_INSTANCE
210 qDebug("ModuleBase_ModelWidget::activate");
212 // the control value is stored to the mode by the focus in on the widget
213 // we need the value is initialized in order to enable the apply button in the property panel.
214 // It should happens in the creation mode only because all fields are filled in the edition mode
215 if (!isEditingMode()) {
216 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
217 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
218 initializeValueByActivate();
221 if (myWidgetValidator)
222 myWidgetValidator->activateFilters(true);
227 void ModuleBase_ModelWidget::deactivate()
229 #ifdef DEBUG_WIDGET_INSTANCE
230 qDebug("ModuleBase_ModelWidget::deactivate");
232 myIsValueStateBlocked = false;
234 if (myWidgetValidator)
235 myWidgetValidator->activateFilters(false);
238 void ModuleBase_ModelWidget::initializeValueByActivate()
240 if (isComputedDefault()) {
241 if (myFeature->compute(myAttributeID)) {
250 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
252 QWidget* aControl = 0;
254 QList<QWidget*> aControls = getControls();
255 int aSize = aControls.size();
258 for (int i = 0; i < aSize && !aControl; i++) {
259 if (aControls[i]->focusPolicy() != Qt::NoFocus)
260 aControl = aControls[i];
264 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
265 if (aControls[i]->focusPolicy() != Qt::NoFocus)
266 aControl = aControls[i];
272 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
274 myDefaultValue = theValue;
277 bool ModuleBase_ModelWidget::storeValue()
279 setValueState(Stored);
281 emit beforeValuesChanged();
282 bool isDone = storeValueCustom();
283 emit afterValuesChanged();
287 #ifdef DEBUG_VALUE_STATE
288 std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
292 case ModuleBase_ModelWidget::Stored: anInfo = "Stored "; break;
293 case ModuleBase_ModelWidget::ModifiedInPP: anInfo = "ModifiedInPP "; break;
294 case ModuleBase_ModelWidget::ModifiedInViewer: anInfo = "ModifiedInViewer"; break;
295 case ModuleBase_ModelWidget::Reset: anInfo = "Reset "; break;
302 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
303 (const ModuleBase_ModelWidget::ValueState& theState)
305 ValueState aState = myState;
307 if (myState != theState && !myIsValueStateBlocked) {
308 #ifdef DEBUG_VALUE_STATE
309 qDebug(QString("setValueState: previous state = %1,\t new state = %2")
310 .arg(getDebugInfo(myState).c_str())
311 .arg(getDebugInfo(theState).c_str()).toStdString().c_str());
314 emit valueStateChanged(aState);
319 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
321 bool isBlocked = myIsValueStateBlocked;
322 myIsValueStateBlocked = theBlocked;
326 bool ModuleBase_ModelWidget::restoreValue()
328 emit beforeValuesRestored();
329 bool isDone = restoreValueCustom();
330 emit afterValuesRestored();
335 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
337 if (!myFlushUpdateBlocked) {
338 #ifdef DEBUG_WIDGET_INSTANCE
339 qDebug("ModuleBase_ModelWidget::updateObject");
341 ModuleBase_Tools::flushUpdated(theObject);
342 emit objectUpdated();
346 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
348 //blockUpdateViewer(true);
349 #ifdef DEBUG_WIDGET_INSTANCE
350 qDebug("ModuleBase_ModelWidget::moveObject");
353 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
354 ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
355 Events_Loop::loop()->flush(anEvent);
357 //blockUpdateViewer(false);
360 bool ModuleBase_ModelWidget::processEnter()
365 bool ModuleBase_ModelWidget::processDelete()
367 // we consider that model objects eats delete key in order to
368 // do nothing by for example symbol delete in line edit or spin box
372 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
374 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
375 if (theEvent->type() == QEvent::FocusIn) {
377 // The following two lines are for debugging purpose only
378 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
379 bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
381 if (getControls().contains(aWidget)) {
382 emit focusInWidget(this);
385 else if (theEvent->type() == QEvent::FocusOut) {
386 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
388 Qt::FocusReason aReason = aFocusEvent->reason();
389 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
390 aReason == Qt::TabFocusReason ||
391 aReason == Qt::BacktabFocusReason ||
392 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
393 if (aMouseOrKey && getControls().contains(aWidget)) {
394 if (getValueState() == ModifiedInPP) {
399 // pass the event on to the parent class
401 return QObject::eventFilter(theObject, theEvent);
404 //**************************************************************
405 void ModuleBase_ModelWidget::onWidgetValuesChanged()
410 //**************************************************************
411 void ModuleBase_ModelWidget::onWidgetValuesModified()
413 setValueState(ModifiedInPP);