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(int& theModuleSelectionFilters,
123 SelectMgr_ListOfFilter& theSelectionFilters)
125 theModuleSelectionFilters = -1;
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;
161 ModuleBase_ModelWidget::ValueState aState = getValueState();
162 if (aState != ModuleBase_ModelWidget::Stored) {
163 AttributePtr anAttr = feature()->attribute(attributeID());
165 const std::string& anAttributeName = anAttr->id();
167 case ModuleBase_ModelWidget::ModifiedInViewer:
168 aMessage = "Attribute \"%1\" is locked by modification value in the viewer.";
169 aMessage.addParameter(anAttributeName);
171 case ModuleBase_ModelWidget::Reset:
172 aMessage = "Attribute \"%1\" is not initialized.";
173 aMessage.addParameter(anAttributeName);
175 case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
184 //**************************************************************
185 QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
189 if (!feature().get())
192 std::string aFeatureID = feature()->getKind();
193 std::string anAttributeID = attributeID();
194 AttributePtr anAttribute = feature()->attribute(anAttributeID);
195 if (!anAttribute.get())
198 std::string aValidatorID;
199 Events_InfoMessage anErrorMsg;
201 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
202 if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
203 if (anErrorMsg.empty())
204 anErrorMsg = "Unknown error.";
206 if (anErrorMsg.context().empty()) {
207 anErrorMsg.setContext(aFeatureID + ":" + anAttributeID + ":" + aValidatorID);
211 if (anErrorMsg.empty() && theValueStateChecked) {
212 anErrorMsg = getValueStateError();
215 if (!anErrorMsg.empty()) {
216 anError = ModuleBase_Tools::translate(anErrorMsg);
222 //**************************************************************
223 void ModuleBase_ModelWidget::enableFocusProcessing()
225 QList<QWidget*> aMyControls = getControls();
226 foreach(QWidget* eachControl, aMyControls) {
227 eachControl->setFocusPolicy(Qt::StrongFocus);
228 eachControl->installEventFilter(this);
232 //**************************************************************
233 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
235 QList<QWidget*> aWidgetList = getControls();
236 foreach(QWidget* aWidget, aWidgetList) {
237 QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
238 // We won't set the effect to QLabels - it looks ugly
240 // If effect is the installed on a different widget, setGraphicsEffect() will
241 // remove the effect from the widget and install it on this widget.
242 // That's why we create a new effect for each widget
243 ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
247 //**************************************************************
248 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue,
249 const bool isUpdateFlushed)
251 /// it is possible to give this flag as parameter in storeValue/storeCustomValue
252 /// after debug, it may be corrected
253 myFlushUpdateBlocked = !isUpdateFlushed;
254 myFeature = theFeature;
255 if (theToStoreValue) {
256 /// it is possible that the attribute is filled before the operation is started,
257 /// e.g. by reentrant operation case some attributes are filled by values of
258 /// feature of previous operation, we should not lost them here
259 if (!theFeature->data()->attribute(attributeID())->isInitialized())
262 myFlushUpdateBlocked = false;
265 //**************************************************************
266 bool ModuleBase_ModelWidget::focusTo()
268 #ifdef DEBUG_WIDGET_INSTANCE
269 qDebug("ModuleBase_ModelWidget::focusTo");
271 QList<QWidget*> aControls = getControls();
272 QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
273 bool isFocusAccepted = false;
274 for (; anIt != aLast && !isFocusAccepted; anIt++) {
275 QWidget* aWidget = *anIt;
276 if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
277 ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
278 isFocusAccepted = true;
281 return isFocusAccepted;
284 //**************************************************************
285 void ModuleBase_ModelWidget::activate()
287 #ifdef DEBUG_WIDGET_INSTANCE
288 qDebug("ModuleBase_ModelWidget::activate");
290 // the control value is stored to the mode by the focus in on the widget
291 // we need the value is initialized in order to enable the apply button in the property panel.
292 // It should happens in the creation mode only because all fields are filled in the edition mode
293 if (!isEditingMode()) {
294 AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
295 if (anAttribute.get() != NULL && !anAttribute->isInitialized())
296 initializeValueByActivate();
301 //**************************************************************
302 void ModuleBase_ModelWidget::deactivate()
304 #ifdef DEBUG_WIDGET_INSTANCE
305 qDebug("ModuleBase_ModelWidget::deactivate");
307 myIsValueStateBlocked = false;
309 if (myWidgetValidator)
310 myWidgetValidator->clearValidatedCash();
313 //**************************************************************
314 void ModuleBase_ModelWidget::initializeValueByActivate()
316 if (isComputedDefault()) {
317 if (myFeature->compute(myAttributeID)) {
326 //**************************************************************
327 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
329 QWidget* aControl = 0;
331 QList<QWidget*> aControls = getControls();
332 int aSize = aControls.size();
335 for (int i = 0; i < aSize && !aControl; i++) {
336 if (aControls[i]->focusPolicy() != Qt::NoFocus)
337 aControl = aControls[i];
341 for (int i = aSize - 1; i >= 0 && !aControl; i--) {
342 if (aControls[i]->focusPolicy() != Qt::NoFocus)
343 aControl = aControls[i];
349 //**************************************************************
350 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
352 myDefaultValue = theValue;
355 //**************************************************************
356 bool ModuleBase_ModelWidget::storeValue()
358 setValueState(Stored);
360 emit beforeValuesChanged();
362 // value is stored only in creation mode and in edition if there is not
363 // XML flag prohibited modification in edit mode(macro feature circle/arc)
364 if (!isEditingMode() || isModifiedInEdit().empty())
365 isDone = storeValueCustom();
367 /// store value in an alternative attribute if possible(attribute has the same type)
368 std::string aWidgetAttribute = attributeID();
369 myAttributeID = isModifiedInEdit();
371 myAttributeID = aWidgetAttribute;
372 // operation will be restarted but if isDone == true, PagedContainer will try to set focus
373 // to the current widget, but will be already deleted
377 emit afterValuesChanged();
381 #ifdef DEBUG_VALUE_STATE
383 //**************************************************************
384 std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
388 case ModuleBase_ModelWidget::Stored: anInfo = "Stored "; break;
389 case ModuleBase_ModelWidget::ModifiedInPP: anInfo = "ModifiedInPP "; break;
390 case ModuleBase_ModelWidget::ModifiedInViewer: anInfo = "ModifiedInViewer"; break;
391 case ModuleBase_ModelWidget::Reset: anInfo = "Reset "; break;
398 //**************************************************************
399 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
400 (const ModuleBase_ModelWidget::ValueState& theState)
402 ValueState aState = myState;
404 if (myState != theState && !myIsValueStateBlocked) {
405 #ifdef DEBUG_VALUE_STATE
406 qDebug(QString("setValueState: previous state = %1,\t new state = %2")
407 .arg(getDebugInfo(myState).c_str())
408 .arg(getDebugInfo(theState).c_str()).toStdString().c_str());
411 emit valueStateChanged(aState);
416 //**************************************************************
417 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
419 bool isBlocked = myIsValueStateBlocked;
420 myIsValueStateBlocked = theBlocked;
424 //**************************************************************
425 bool ModuleBase_ModelWidget::restoreValue()
427 emit beforeValuesRestored();
428 bool isDone = restoreValueCustom();
429 emit afterValuesRestored();
434 //**************************************************************
435 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
437 if (!myFlushUpdateBlocked) {
438 #ifdef DEBUG_WIDGET_INSTANCE
439 qDebug("ModuleBase_ModelWidget::updateObject");
441 ModuleBase_Tools::flushUpdated(theObject);
442 emit objectUpdated();
446 //**************************************************************
447 bool ModuleBase_ModelWidget::canProcessAction(ModuleBase_ActionType theActionType,
448 bool& isActionEnabled)
450 isActionEnabled = false;
451 switch (theActionType) {
452 case ActionEnter: return false;
453 case ActionEscape: return false;
454 case ActionDelete: return true;
455 case ActionSelection: return true;
463 //**************************************************************
464 bool ModuleBase_ModelWidget::processAction(ModuleBase_ActionType theActionType,
465 const ActionParamPtr& theParam)
467 switch (theActionType) {
469 return processEnter();
471 return processEscape();
473 return processDelete();
474 case ActionSelection:
483 //**************************************************************
484 bool ModuleBase_ModelWidget::processEnter()
489 //**************************************************************
490 bool ModuleBase_ModelWidget::processEscape()
495 //**************************************************************
496 bool ModuleBase_ModelWidget::processDelete()
498 // we consider that model objects eats delete key in order to
499 // do nothing by for example symbol delete in line edit or spin box
503 //**************************************************************
504 bool ModuleBase_ModelWidget::processSelection()
509 //**************************************************************
510 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
512 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
513 if (theEvent->type() == QEvent::FocusIn) {
514 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
515 Qt::FocusReason aReason = aFocusEvent->reason();
516 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
517 /*aReason == Qt::TabFocusReason ||
518 //aReason == Qt::BacktabFocusReason ||*/
519 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
520 if (aMouseOrKey && getControls().contains(aWidget)) {
521 //if (getControls().contains(aWidget)) {
525 else if (theEvent->type() == QEvent::FocusOut) {
526 QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
528 Qt::FocusReason aReason = aFocusEvent->reason();
529 bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
530 aReason == Qt::TabFocusReason ||
531 aReason == Qt::BacktabFocusReason ||
532 aReason == Qt::OtherFocusReason; // to process widget->setFocus()
533 if (aMouseOrKey && getControls().contains(aWidget)) {
534 if (getValueState() == ModifiedInPP) {
539 // pass the event on to the parent class
541 return QObject::eventFilter(theObject, theEvent);
544 //**************************************************************
545 void ModuleBase_ModelWidget::onWidgetValuesChanged()
550 //**************************************************************
551 void ModuleBase_ModelWidget::onWidgetValuesModified()
553 setValueState(ModifiedInPP);
556 //**************************************************************
557 QString ModuleBase_ModelWidget::translate(const std::string& theStr) const
559 return ModuleBase_Tools::translate(context(), theStr);
562 //**************************************************************
563 ModuleBase_ModelWidget* ModuleBase_ModelWidget::findModelWidget(ModuleBase_IPropertyPanel* theProp,
566 ModuleBase_ModelWidget* aModelWidget = 0;
570 QObject* aParent = theWidget->parent();
572 aModelWidget = qobject_cast<ModuleBase_ModelWidget*>(aParent);
575 aParent = aParent->parent();