1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "ModuleBase_ModelWidget.h"
22 #include "ModuleBase_IPropertyPanel.h"
23 #include "ModuleBase_ViewerPrs.h"
24 #include "ModuleBase_Tools.h"
25 #include "ModuleBase_WidgetValidator.h"
27 #include <Events_InfoMessage.h>
29 #include <ModelAPI_Data.h>
30 #include <ModelAPI_Attribute.h>
31 #include <ModelAPI_Events.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_Validator.h>
35 #include <Config_Keywords.h>
36 #include <Config_WidgetAPI.h>
37 #include <Config_Translator.h>
38 #include <Config_PropManager.h>
40 #include <Events_Loop.h>
44 #include <QFocusEvent>
47 //#define DEBUG_VALUE_STATE
49 //#define DEBUG_WIDGET_INSTANCE
50 //#define DEBUG_ENABLE_SKETCH_INPUT_FIELDS
52 //**************************************************************
53 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
54 const Config_WidgetAPI* theData)
58 myIsValueStateBlocked(false),
59 myFlushUpdateBlocked(false),
62 #ifdef DEBUG_WIDGET_INSTANCE
63 qDebug("ModuleBase_ModelWidget::ModuleBase_ModelWidget");
66 myFeatureId = theData->featureId();
68 myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
70 myIsModifiedInEdit = theData->getProperty(ATTR_MODIFIED_IN_EDIT);
72 myDefaultValue = theData->getProperty(ATTR_DEFAULT);
73 myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
74 myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
75 myAttributeID = theData ? theData->widgetId() : "";
76 myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
78 myIsValueEnabled = On; // not defined or "true"
79 std::string anEnableValue = theData->getProperty(DOUBLE_WDG_ENABLE_VALUE);
80 if (anEnableValue == "false")
81 myIsValueEnabled = Off;
82 if (anEnableValue == DOUBLE_WDG_ENABLE_VALUE_BY_PREFERENCES)
83 myIsValueEnabled = DefinedInPreferences;
85 connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
86 connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
89 //**************************************************************
90 ModuleBase_ModelWidget::~ModuleBase_ModelWidget()
92 #ifdef DEBUG_WIDGET_INSTANCE
93 qDebug("ModuleBase_ModelWidget::~ModuleBase_ModelWidget");
97 //**************************************************************
98 bool ModuleBase_ModelWidget::reset()
100 bool aResult = resetCustom();
102 setValueState(Reset);
107 //**************************************************************
108 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
110 return theObject->data()->attribute(attributeID())->isInitialized();
113 //**************************************************************
114 void ModuleBase_ModelWidget::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
116 theModuleSelectionModes = -1;
117 if (myWidgetValidator)
118 myWidgetValidator->selectionModes(theModuleSelectionModes, theModes);
121 //**************************************************************
122 void ModuleBase_ModelWidget::selectionFilters(QIntList& theModuleSelectionFilters,
123 SelectMgr_ListOfFilter& theSelectionFilters)
125 if (myWidgetValidator)
126 myWidgetValidator->selectionFilters(theModuleSelectionFilters, theSelectionFilters);
129 //**************************************************************
130 bool ModuleBase_ModelWidget::isValueEnabled() const
132 bool anEnabled = true;
133 if (myIsValueEnabled == DefinedInPreferences) {
134 #ifdef DEBUG_ENABLE_SKETCH_INPUT_FIELDS
135 bool aCanDisable = false;
137 //Config_PropManager::boolean(SKETCH_TAB_NAME, "disable_input_fields", "true");
138 bool aCanDisable = true;
143 else if (myIsValueEnabled == Off)
148 //**************************************************************
149 void ModuleBase_ModelWidget::processValueState()
151 if (myState == ModifiedInPP || myState == ModifiedInViewer)
155 //**************************************************************
156 Events_InfoMessage ModuleBase_ModelWidget::getValueStateError() const
158 Events_InfoMessage aMessage;
160 ModuleBase_ModelWidget::ValueState aState = getValueState();
161 if (aState != ModuleBase_ModelWidget::Stored) {
162 AttributePtr anAttr = feature()->attribute(attributeID());
164 const std::string& anAttributeName = anAttr->id();
166 case ModuleBase_ModelWidget::ModifiedInViewer:
167 aMessage = "Attribute \"%1\" is locked by modification value in the viewer.";
168 aMessage.addParameter(anAttributeName);
170 case ModuleBase_ModelWidget::Reset:
171 aMessage = "Attribute \"%1\" is not initialized.";
172 aMessage.addParameter(anAttributeName);
174 case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
183 //**************************************************************
184 QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
188 if (!feature().get())
191 std::string aFeatureID = feature()->getKind();
192 std::string anAttributeID = attributeID();
193 AttributePtr anAttribute = feature()->attribute(anAttributeID);
194 if (!anAttribute.get())
197 std::string aValidatorID;
198 Events_InfoMessage anErrorMsg;
200 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
201 if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
202 if (anErrorMsg.empty())
203 anErrorMsg = "Unknown error.";
205 if (anErrorMsg.context().empty()) {
206 anErrorMsg.setContext(aFeatureID + ":" + anAttributeID + ":" + aValidatorID);
210 if (anErrorMsg.empty() && theValueStateChecked) {
211 anErrorMsg = getValueStateError();
214 if (!anErrorMsg.empty()) {
215 anError = ModuleBase_Tools::translate(anErrorMsg);
221 //**************************************************************
222 void ModuleBase_ModelWidget::enableFocusProcessing()
224 QList<QWidget*> aMyControls = getControls();
225 foreach(QWidget* eachControl, aMyControls) {
226 eachControl->setFocusPolicy(Qt::StrongFocus);
227 eachControl->installEventFilter(this);
231 //**************************************************************
232 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
234 QList<QWidget*> aWidgetList = getControls();
235 foreach(QWidget* aWidget, aWidgetList) {
236 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
237 // We won't set the effect to QLabels - it looks ugly
239 // If effect is the installed on a different widget, setGraphicsEffect() will
240 // remove the effect from the widget and install it on this widget.
241 // That's why we create a new effect for each widget
242 ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
246 //**************************************************************
247 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue,
248 const bool isUpdateFlushed)
250 /// it is possible to give this flag as parameter in storeValue/storeCustomValue
251 /// after debug, it may be corrected
252 myFlushUpdateBlocked = !isUpdateFlushed;
253 myFeature = theFeature;
254 if (theToStoreValue) {
255 /// it is possible that the attribute is filled before the operation is started,
256 /// e.g. by reentrant operation case some attributes are filled by values of
257 /// feature of previous operation, we should not lost them here
258 if (!theFeature->data()->attribute(attributeID())->isInitialized())
261 myFlushUpdateBlocked = false;
264 //**************************************************************
265 bool ModuleBase_ModelWidget::focusTo()
267 #ifdef DEBUG_WIDGET_INSTANCE
268 qDebug("ModuleBase_ModelWidget::focusTo");
270 QList<QWidget*> aControls = getControls();
271 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
272 bool isFocusAccepted = false;
273 for (; anIt != aLast && !isFocusAccepted; anIt++) {
274 QWidget* aWidget = *anIt;
275 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
276 ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
277 isFocusAccepted = true;
280 return isFocusAccepted;
283 //**************************************************************
284 void ModuleBase_ModelWidget::activate()
286 #ifdef DEBUG_WIDGET_INSTANCE
287 qDebug("ModuleBase_ModelWidget::activate");
289 // the control value is stored to the mode by the focus in on the widget
290 // we need the value is initialized in order to enable the apply button in the property panel.
291 // It should happens in the creation mode only because all fields are filled in the edition mode
292 if (!isEditingMode()) {
293 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
294 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
295 initializeValueByActivate();
300 //**************************************************************
301 void ModuleBase_ModelWidget::deactivate()
303 #ifdef DEBUG_WIDGET_INSTANCE
304 qDebug("ModuleBase_ModelWidget::deactivate");
306 myIsValueStateBlocked = false;
308 if (myWidgetValidator)
309 myWidgetValidator->clearValidatedCash();
312 //**************************************************************
313 void ModuleBase_ModelWidget::initializeValueByActivate()
315 if (isComputedDefault()) {
316 if (myFeature->compute(myAttributeID)) {
325 //**************************************************************
326 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
328 QWidget* aControl = 0;
330 QList<QWidget*> aControls = getControls();
331 int aSize = aControls.size();
334 for (int i = 0; i < aSize && !aControl; i++) {
335 if (aControls[i]->focusPolicy() != Qt::NoFocus)
336 aControl = aControls[i];
340 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
341 if (aControls[i]->focusPolicy() != Qt::NoFocus)
342 aControl = aControls[i];
348 //**************************************************************
349 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
351 myDefaultValue = theValue;
354 //**************************************************************
355 bool ModuleBase_ModelWidget::storeValue()
357 setValueState(Stored);
359 emit beforeValuesChanged();
361 // value is stored only in creation mode and in edition if there is not
362 // XML flag prohibited modification in edit mode(macro feature circle/arc)
363 if (!isEditingMode() || isModifiedInEdit().empty())
364 isDone = storeValueCustom();
366 /// store value in an alternative attribute if possible(attribute has the same type)
367 std::string aWidgetAttribute = attributeID();
368 myAttributeID = isModifiedInEdit();
370 myAttributeID = aWidgetAttribute;
371 // operation will be restarted but if isDone == true, PagedContainer will try to set focus
372 // to the current widget, but will be already deleted
376 emit afterValuesChanged();
380 #ifdef DEBUG_VALUE_STATE
382 //**************************************************************
383 std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
387 case ModuleBase_ModelWidget::Stored: anInfo = "Stored "; break;
388 case ModuleBase_ModelWidget::ModifiedInPP: anInfo = "ModifiedInPP "; break;
389 case ModuleBase_ModelWidget::ModifiedInViewer: anInfo = "ModifiedInViewer"; break;
390 case ModuleBase_ModelWidget::Reset: anInfo = "Reset "; break;
397 //**************************************************************
398 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
399 (const ModuleBase_ModelWidget::ValueState& theState)
401 ValueState aState = myState;
403 if (myState != theState && !myIsValueStateBlocked) {
404 #ifdef DEBUG_VALUE_STATE
405 qDebug(QString("setValueState: previous state = %1,\t new state = %2")
406 .arg(getDebugInfo(myState).c_str())
407 .arg(getDebugInfo(theState).c_str()).toStdString().c_str());
410 emit valueStateChanged(aState);
415 //**************************************************************
416 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
418 bool isBlocked = myIsValueStateBlocked;
419 myIsValueStateBlocked = theBlocked;
423 //**************************************************************
424 bool ModuleBase_ModelWidget::restoreValue()
426 emit beforeValuesRestored();
427 bool isDone = restoreValueCustom();
428 emit afterValuesRestored();
433 //**************************************************************
434 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
436 if (!myFlushUpdateBlocked) {
437 #ifdef DEBUG_WIDGET_INSTANCE
438 qDebug("ModuleBase_ModelWidget::updateObject");
440 ModuleBase_Tools::flushUpdated(theObject);
441 emit objectUpdated();
445 //**************************************************************
446 bool ModuleBase_ModelWidget::canProcessAction(ModuleBase_ActionType theActionType,
447 bool& isActionEnabled)
449 isActionEnabled = false;
450 switch (theActionType) {
451 case ActionEnter: return false;
452 case ActionEscape: return false;
453 case ActionDelete: return true;
454 case ActionSelection: return true;
462 //**************************************************************
463 bool ModuleBase_ModelWidget::processAction(ModuleBase_ActionType theActionType,
464 const ActionParamPtr& theParam)
466 switch (theActionType) {
468 return processEnter();
470 return processEscape();
472 return processDelete();
473 case ActionSelection:
482 //**************************************************************
483 bool ModuleBase_ModelWidget::processEnter()
488 //**************************************************************
489 bool ModuleBase_ModelWidget::processEscape()
494 //**************************************************************
495 bool ModuleBase_ModelWidget::processDelete()
497 // we consider that model objects eats delete key in order to
498 // do nothing by for example symbol delete in line edit or spin box
502 //**************************************************************
503 bool ModuleBase_ModelWidget::processSelection()
508 //**************************************************************
509 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
511 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
512 if (theEvent->type() == QEvent::FocusIn) {
513 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
514 Qt::FocusReason aReason = aFocusEvent->reason();
515 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
516 /*aReason == Qt::TabFocusReason ||
517 //aReason == Qt::BacktabFocusReason ||*/
518 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
519 if (aMouseOrKey && getControls().contains(aWidget)) {
520 //if (getControls().contains(aWidget)) {
524 else if (theEvent->type() == QEvent::FocusOut) {
525 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
527 Qt::FocusReason aReason = aFocusEvent->reason();
528 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
529 aReason == Qt::TabFocusReason ||
530 aReason == Qt::BacktabFocusReason ||
531 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
532 if (aMouseOrKey && getControls().contains(aWidget)) {
533 if (getValueState() == ModifiedInPP) {
538 // pass the event on to the parent class
540 return QObject::eventFilter(theObject, theEvent);
543 //**************************************************************
544 void ModuleBase_ModelWidget::onWidgetValuesChanged()
549 //**************************************************************
550 void ModuleBase_ModelWidget::onWidgetValuesModified()
552 setValueState(ModifiedInPP);
555 //**************************************************************
556 QString ModuleBase_ModelWidget::translate(const std::string& theStr) const
558 return ModuleBase_Tools::translate(context(), theStr);
561 //**************************************************************
562 ModuleBase_ModelWidget* ModuleBase_ModelWidget::findModelWidget(ModuleBase_IPropertyPanel* theProp,
565 ModuleBase_ModelWidget* aModelWidget = 0;
569 QObject* aParent = theWidget->parent();
571 aModelWidget = qobject_cast<ModuleBase_ModelWidget*>(aParent);
574 aParent = aParent->parent();