1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ModuleBase_ModelWidget.h"
21 #include "ModuleBase_IPropertyPanel.h"
22 #include "ModuleBase_ViewerPrs.h"
23 #include "ModuleBase_Tools.h"
24 #include "ModuleBase_WidgetValidator.h"
26 #include <Events_InfoMessage.h>
28 #include <ModelAPI_Data.h>
29 #include <ModelAPI_Attribute.h>
30 #include <ModelAPI_Events.h>
31 #include <ModelAPI_Session.h>
32 #include <ModelAPI_Validator.h>
34 #include <Config_Keywords.h>
35 #include <Config_WidgetAPI.h>
36 #include <Config_Translator.h>
37 #include <Config_PropManager.h>
39 #include <Events_Loop.h>
43 #include <QFocusEvent>
46 //#define DEBUG_VALUE_STATE
48 //#define DEBUG_WIDGET_INSTANCE
49 //#define DEBUG_ENABLE_SKETCH_INPUT_FIELDS
51 //**************************************************************
52 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
53 const Config_WidgetAPI* theData)
57 myIsValueStateBlocked(false),
58 myFlushUpdateBlocked(false),
61 #ifdef DEBUG_WIDGET_INSTANCE
62 qDebug("ModuleBase_ModelWidget::ModuleBase_ModelWidget");
65 myFeatureId = theData->featureId();
67 myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
69 myIsModifiedInEdit = theData->getProperty(ATTR_MODIFIED_IN_EDIT);
71 myUpdateVisualAttributes = theData->getBooleanAttribute(ATTR_VISUAL_CHANGED, false);
73 myDefaultValue = theData->getProperty(ATTR_DEFAULT);
74 myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
75 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
76 myAttributeID = theData ? theData->widgetId() : "";
77 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
79 myIsValueEnabled = On; // not defined or "true"
80 std::string anEnableValue = theData->getProperty(DOUBLE_WDG_ENABLE_VALUE);
81 if (anEnableValue == "false")
82 myIsValueEnabled = Off;
83 if (anEnableValue == DOUBLE_WDG_ENABLE_VALUE_BY_PREFERENCES)
84 myIsValueEnabled = DefinedInPreferences;
86 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
87 connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
90 //**************************************************************
91 ModuleBase_ModelWidget::~ModuleBase_ModelWidget()
93 #ifdef DEBUG_WIDGET_INSTANCE
94 qDebug("ModuleBase_ModelWidget::~ModuleBase_ModelWidget");
98 //**************************************************************
99 bool ModuleBase_ModelWidget::reset()
101 bool aResult = resetCustom();
103 setValueState(Reset);
108 //**************************************************************
109 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
111 return theObject->data()->attribute(attributeID())->isInitialized();
114 //**************************************************************
115 void ModuleBase_ModelWidget::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
117 theModuleSelectionModes = -1;
118 if (myWidgetValidator)
119 myWidgetValidator->selectionModes(theModuleSelectionModes, theModes);
122 //**************************************************************
123 void ModuleBase_ModelWidget::selectionFilters(QIntList& theModuleSelectionFilters,
124 SelectMgr_ListOfFilter& theSelectionFilters)
126 if (myWidgetValidator)
127 myWidgetValidator->selectionFilters(theModuleSelectionFilters, theSelectionFilters);
130 //**************************************************************
131 bool ModuleBase_ModelWidget::isValueEnabled() const
133 bool anEnabled = true;
134 if (myIsValueEnabled == DefinedInPreferences) {
135 #ifdef DEBUG_ENABLE_SKETCH_INPUT_FIELDS
136 bool aCanDisable = false;
138 //Config_PropManager::boolean(SKETCH_TAB_NAME, "disable_input_fields", "true");
139 bool aCanDisable = true;
144 else if (myIsValueEnabled == Off)
149 //**************************************************************
150 void ModuleBase_ModelWidget::processValueState()
152 if (myState == ModifiedInPP || myState == ModifiedInViewer)
156 //**************************************************************
157 Events_InfoMessage ModuleBase_ModelWidget::getValueStateError() const
159 Events_InfoMessage aMessage;
160 aMessage.setContext(context());
162 ModuleBase_ModelWidget::ValueState aState = getValueState();
163 if (aState != ModuleBase_ModelWidget::Stored) {
164 AttributePtr anAttr = feature()->attribute(attributeID());
166 const std::string& anAttributeName = anAttr->id();
168 case ModuleBase_ModelWidget::ModifiedInViewer:
169 aMessage = "Attribute \"%1\" is locked by modification value in the viewer.";
170 aMessage.addParameter(anAttributeName);
172 case ModuleBase_ModelWidget::Reset:
173 aMessage = "Attribute \"%1\" is not initialized.";
174 aMessage.addParameter(anAttributeName);
176 case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
185 //**************************************************************
186 QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
190 if (!feature().get())
193 std::string aFeatureID = feature()->getKind();
194 std::string anAttributeID = attributeID();
195 AttributePtr anAttribute = feature()->attribute(anAttributeID);
196 if (!anAttribute.get())
199 std::string aValidatorID;
200 Events_InfoMessage anErrorMsg;
202 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
203 if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
204 if (anErrorMsg.empty())
205 anErrorMsg = "Unknown error.";
207 if (anErrorMsg.context().empty()) {
208 anErrorMsg.setContext(aFeatureID + ":" + anAttributeID + ":" + aValidatorID);
212 if (anErrorMsg.empty() && theValueStateChecked) {
213 anErrorMsg = getValueStateError();
216 if (!anErrorMsg.empty()) {
217 anError = ModuleBase_Tools::translate(anErrorMsg);
223 //**************************************************************
224 void ModuleBase_ModelWidget::enableFocusProcessing()
226 QList<QWidget*> aMyControls = getControls();
227 foreach(QWidget* eachControl, aMyControls) {
228 eachControl->setFocusPolicy(Qt::StrongFocus);
229 eachControl->installEventFilter(this);
233 //**************************************************************
234 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
236 QList<QWidget*> aWidgetList = getControls();
237 foreach(QWidget* aWidget, aWidgetList) {
238 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
239 // We won't set the effect to QLabels - it looks ugly
241 // If effect is the installed on a different widget, setGraphicsEffect() will
242 // remove the effect from the widget and install it on this widget.
243 // That's why we create a new effect for each widget
244 ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
248 //**************************************************************
249 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue,
250 const bool isUpdateFlushed)
252 /// it is possible to give this flag as parameter in storeValue/storeCustomValue
253 /// after debug, it may be corrected
254 myFlushUpdateBlocked = !isUpdateFlushed;
255 myFeature = theFeature;
256 if (theToStoreValue) {
257 /// it is possible that the attribute is filled before the operation is started,
258 /// e.g. by reentrant operation case some attributes are filled by values of
259 /// feature of previous operation, we should not lost them here
260 if (!theFeature->data()->attribute(attributeID())->isInitialized())
263 myFlushUpdateBlocked = false;
266 //**************************************************************
267 bool ModuleBase_ModelWidget::focusTo()
269 #ifdef DEBUG_WIDGET_INSTANCE
270 qDebug("ModuleBase_ModelWidget::focusTo");
272 QList<QWidget*> aControls = getControls();
273 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
274 bool isFocusAccepted = false;
275 for (; anIt != aLast && !isFocusAccepted; anIt++) {
276 QWidget* aWidget = *anIt;
277 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
278 ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
279 isFocusAccepted = true;
282 return isFocusAccepted;
285 //**************************************************************
286 void ModuleBase_ModelWidget::activate()
288 #ifdef DEBUG_WIDGET_INSTANCE
289 qDebug("ModuleBase_ModelWidget::activate");
291 // the control value is stored to the mode by the focus in on the widget
292 // we need the value is initialized in order to enable the apply button in the property panel.
293 // It should happens in the creation mode only because all fields are filled in the edition mode
294 if (!isEditingMode()) {
295 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
296 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
297 initializeValueByActivate();
302 //**************************************************************
303 void ModuleBase_ModelWidget::deactivate()
305 #ifdef DEBUG_WIDGET_INSTANCE
306 qDebug("ModuleBase_ModelWidget::deactivate");
308 myIsValueStateBlocked = false;
310 if (myWidgetValidator)
311 myWidgetValidator->clearValidatedCash();
314 //**************************************************************
315 void ModuleBase_ModelWidget::initializeValueByActivate()
317 if (isComputedDefault()) {
318 if (myFeature->compute(myAttributeID)) {
327 //**************************************************************
328 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
330 QWidget* aControl = 0;
332 QList<QWidget*> aControls = getControls();
333 int aSize = aControls.size();
336 for (int i = 0; i < aSize && !aControl; i++) {
337 if (aControls[i]->focusPolicy() != Qt::NoFocus)
338 aControl = aControls[i];
342 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
343 if (aControls[i]->focusPolicy() != Qt::NoFocus)
344 aControl = aControls[i];
350 //**************************************************************
351 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
353 myDefaultValue = theValue;
356 //**************************************************************
357 bool ModuleBase_ModelWidget::storeValue()
359 setValueState(Stored);
361 emit beforeValuesChanged();
363 // value is stored only in creation mode and in edition if there is not
364 // XML flag prohibited modification in edit mode(macro feature circle/arc)
365 if (!isEditingMode() || isModifiedInEdit().empty())
366 isDone = storeValueCustom();
368 /// store value in an alternative attribute if possible(attribute has the same type)
369 std::string aWidgetAttribute = attributeID();
370 myAttributeID = isModifiedInEdit();
372 myAttributeID = aWidgetAttribute;
373 // operation will be restarted but if isDone == true, PagedContainer will try to set focus
374 // to the current widget, but will be already deleted
378 emit afterValuesChanged();
382 #ifdef DEBUG_VALUE_STATE
384 //**************************************************************
385 std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
389 case ModuleBase_ModelWidget::Stored: anInfo = "Stored "; break;
390 case ModuleBase_ModelWidget::ModifiedInPP: anInfo = "ModifiedInPP "; break;
391 case ModuleBase_ModelWidget::ModifiedInViewer: anInfo = "ModifiedInViewer"; break;
392 case ModuleBase_ModelWidget::Reset: anInfo = "Reset "; break;
399 //**************************************************************
400 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
401 (const ModuleBase_ModelWidget::ValueState& theState)
403 ValueState aState = myState;
405 if (myState != theState && !myIsValueStateBlocked) {
406 #ifdef DEBUG_VALUE_STATE
407 qDebug(QString("setValueState: previous state = %1,\t new state = %2")
408 .arg(getDebugInfo(myState).c_str())
409 .arg(getDebugInfo(theState).c_str()).toStdString().c_str());
412 emit valueStateChanged(aState);
417 //**************************************************************
418 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
420 bool isBlocked = myIsValueStateBlocked;
421 myIsValueStateBlocked = theBlocked;
425 //**************************************************************
426 bool ModuleBase_ModelWidget::restoreValue()
429 // This code works in inspection panel
430 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
431 if (!aValidators->isCase(myFeature, attributeID()))
432 return false; // if it is not an active case for the widget
434 emit beforeValuesRestored();
435 bool isDone = restoreValueCustom();
436 emit afterValuesRestored();
441 //**************************************************************
442 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
444 if (!myFlushUpdateBlocked) {
445 #ifdef DEBUG_WIDGET_INSTANCE
446 qDebug("ModuleBase_ModelWidget::updateObject");
448 if (myFeature.get() && myUpdateVisualAttributes) {
449 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_VISUAL_ATTRIBUTES);
450 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
452 ModuleBase_Tools::flushUpdated(theObject);
453 emit objectUpdated();
457 //**************************************************************
458 bool ModuleBase_ModelWidget::canProcessAction(ModuleBase_ActionType theActionType,
459 bool& isActionEnabled)
461 isActionEnabled = false;
462 switch (theActionType) {
463 case ActionEnter: return false;
464 case ActionEscape: return false;
465 case ActionDelete: return true;
466 case ActionSelection: return true;
474 //**************************************************************
475 bool ModuleBase_ModelWidget::processAction(ModuleBase_ActionType theActionType,
476 const ActionParamPtr& theParam)
478 switch (theActionType) {
480 return processEnter();
482 return processEscape();
484 return processDelete();
485 case ActionSelection:
494 //**************************************************************
495 bool ModuleBase_ModelWidget::processEnter()
500 //**************************************************************
501 bool ModuleBase_ModelWidget::processEscape()
506 //**************************************************************
507 bool ModuleBase_ModelWidget::processDelete()
509 // we consider that model objects eats delete key in order to
510 // do nothing by for example symbol delete in line edit or spin box
514 //**************************************************************
515 bool ModuleBase_ModelWidget::processSelection()
520 //**************************************************************
521 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
523 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
524 if (theEvent->type() == QEvent::FocusIn) {
525 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
526 Qt::FocusReason aReason = aFocusEvent->reason();
527 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
528 /*aReason == Qt::TabFocusReason ||
529 //aReason == Qt::BacktabFocusReason ||*/
530 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
531 if (aMouseOrKey && getControls().contains(aWidget)) {
532 //if (getControls().contains(aWidget)) {
536 else if (theEvent->type() == QEvent::FocusOut) {
537 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
539 Qt::FocusReason aReason = aFocusEvent->reason();
540 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
541 aReason == Qt::TabFocusReason ||
542 aReason == Qt::BacktabFocusReason ||
543 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
544 if (aMouseOrKey && getControls().contains(aWidget)) {
545 if (getValueState() == ModifiedInPP) {
550 // pass the event on to the parent class
552 return QObject::eventFilter(theObject, theEvent);
555 //**************************************************************
556 void ModuleBase_ModelWidget::onWidgetValuesChanged()
561 //**************************************************************
562 void ModuleBase_ModelWidget::onWidgetValuesModified()
564 setValueState(ModifiedInPP);
567 //**************************************************************
568 QString ModuleBase_ModelWidget::translate(const std::string& theStr) const
570 return ModuleBase_Tools::translate(context(), theStr);
573 //**************************************************************
574 ModuleBase_ModelWidget* ModuleBase_ModelWidget::findModelWidget(ModuleBase_IPropertyPanel* theProp,
577 ModuleBase_ModelWidget* aModelWidget = 0;
581 QObject* aParent = theWidget->parent();
583 aModelWidget = qobject_cast<ModuleBase_ModelWidget*>(aParent);
586 aParent = aParent->parent();