From fd3d6a691feb0305f22c8c56debe7f66035452d1 Mon Sep 17 00:00:00 2001 From: vsv Date: Wed, 27 Dec 2017 16:56:49 +0300 Subject: [PATCH] Issue #2388: Remember last user choice for all dialog boxes with a choice --- src/ModuleBase/ModuleBase_ChoiceCtrl.cpp | 15 ++++++++ src/ModuleBase/ModuleBase_ChoiceCtrl.h | 4 +++ src/ModuleBase/ModuleBase_ModelWidget.h | 4 +++ src/ModuleBase/ModuleBase_PagedContainer.cpp | 30 +++++++++++++--- src/ModuleBase/ModuleBase_PagedContainer.h | 6 +++- src/ModuleBase/ModuleBase_WidgetChoice.cpp | 28 +++++++++++++-- src/ModuleBase/ModuleBase_WidgetChoice.h | 13 +++---- .../ModuleBase_WidgetMultiSelector.cpp | 34 ++++++++++++++++--- .../ModuleBase_WidgetMultiSelector.h | 6 ++++ src/XGUI/XGUI_PropertyPanel.cpp | 16 +++++++-- src/XGUI/XGUI_PropertyPanel.h | 2 ++ 11 files changed, 138 insertions(+), 20 deletions(-) diff --git a/src/ModuleBase/ModuleBase_ChoiceCtrl.cpp b/src/ModuleBase/ModuleBase_ChoiceCtrl.cpp index 38afff3ca..67b5b6f18 100644 --- a/src/ModuleBase/ModuleBase_ChoiceCtrl.cpp +++ b/src/ModuleBase/ModuleBase_ChoiceCtrl.cpp @@ -132,6 +132,21 @@ void ModuleBase_ChoiceCtrl::setValue(int theVal) } } +void ModuleBase_ChoiceCtrl::setValue(const QString& theVal) +{ + switch (myType) { + case RadioButtons: + foreach (QAbstractButton* aBtn, myButtons->buttons()) { + aBtn->setChecked(aBtn->toolTip() == theVal); + } + break; + case ComboBox: + myCombo->setCurrentText(theVal); + break; + } +} + + void ModuleBase_ChoiceCtrl::setTooltip(QString theTip) { if (myType == ComboBox) diff --git a/src/ModuleBase/ModuleBase_ChoiceCtrl.h b/src/ModuleBase/ModuleBase_ChoiceCtrl.h index 09530b929..a072cd1ad 100644 --- a/src/ModuleBase/ModuleBase_ChoiceCtrl.h +++ b/src/ModuleBase/ModuleBase_ChoiceCtrl.h @@ -75,6 +75,10 @@ public: /// \param theVal a value (from 0 to number of items) void setValue(int theVal); + /// Set value: text of button or item of combo box. + /// \param theVal a value (one of text items) + void setValue(const QString& theVal); + /// Set tool tip for label. Used only for combo box. void setTooltip(QString theTip); diff --git a/src/ModuleBase/ModuleBase_ModelWidget.h b/src/ModuleBase/ModuleBase_ModelWidget.h index 2e215404f..d2d0fd804 100644 --- a/src/ModuleBase/ModuleBase_ModelWidget.h +++ b/src/ModuleBase/ModuleBase_ModelWidget.h @@ -300,6 +300,10 @@ Q_OBJECT /// If not then it means that the widget do not need attribute at all. virtual bool usesAttribute() const { return true; } + /// It is called when user press Ok or OkPlus buttons in the parent property panel + /// By default this slot does nothing + virtual void onFeatureAccepted() {} + signals: /// The signal about widget values are to be changed void beforeValuesChanged(); diff --git a/src/ModuleBase/ModuleBase_PagedContainer.cpp b/src/ModuleBase/ModuleBase_PagedContainer.cpp index b5e1aeb4d..27c685f8b 100644 --- a/src/ModuleBase/ModuleBase_PagedContainer.cpp +++ b/src/ModuleBase/ModuleBase_PagedContainer.cpp @@ -30,14 +30,18 @@ #include +static QMap defaultValues; + ModuleBase_PagedContainer::ModuleBase_PagedContainer(QWidget* theParent, const Config_WidgetAPI* theData) : ModuleBase_ModelWidget(theParent, theData), - myIsFocusOnCurrentPage(false) + myIsFocusOnCurrentPage(false), myIsFirst(true) { // it is not obligatory to be ignored when property panel tries to activate next active widget // but if focus is moved to this control, it can accept it. myIsObligatory = false; + if (defaultValues.contains(myFeatureId)) + myDefValue = defaultValues[myFeatureId]; } ModuleBase_PagedContainer::~ModuleBase_PagedContainer() @@ -93,10 +97,12 @@ bool ModuleBase_PagedContainer::restoreValueCustom() return false; DataPtr aData = myFeature->data(); AttributeStringPtr aStringAttr = aData->string(attributeID()); - QString aCaseId = QString::fromStdString(aStringAttr->value()); + QString aCaseId = QString::fromStdString(myDefValue.empty()? + aStringAttr->value() : myDefValue); + myIsFirst = false; int idx = myCaseIds.indexOf(aCaseId); if (idx == -1) - return false; + idx = currentPageIndex(); setCurrentPageIndex(idx); return true; } @@ -113,9 +119,19 @@ bool ModuleBase_PagedContainer::storeValueCustom() if(!myFeature) return false; DataPtr aData = myFeature->data(); + AttributeStringPtr aStringAttr = aData->string(attributeID()); - QString aWidgetValue = myCaseIds.at(currentPageIndex()); - aStringAttr->setValue(aWidgetValue.toStdString()); + std::string aWidgetValue; + if (myIsFirst) + aWidgetValue = myDefValue.empty()? + myCaseIds.at(currentPageIndex()).toStdString() : myDefValue; + else + aWidgetValue = myCaseIds.at(currentPageIndex()).toStdString(); + myDefValue = aWidgetValue; + aStringAttr->setValue(aWidgetValue); + + myIsFirst = false; + updateObject(myFeature); // for preview return true; } @@ -132,4 +148,8 @@ void ModuleBase_PagedContainer::onPageChanged() focusTo(); } +void ModuleBase_PagedContainer::onFeatureAccepted() +{ + defaultValues[myFeatureId] = myDefValue; +} diff --git a/src/ModuleBase/ModuleBase_PagedContainer.h b/src/ModuleBase/ModuleBase_PagedContainer.h index 2a731b18c..cd525cdba 100644 --- a/src/ModuleBase/ModuleBase_PagedContainer.h +++ b/src/ModuleBase/ModuleBase_PagedContainer.h @@ -62,6 +62,9 @@ class MODULEBASE_EXPORT ModuleBase_PagedContainer : public ModuleBase_ModelWidge /// Redefinition of virtual function virtual void enableFocusProcessing(); + /// The slot is called when user press Ok or OkPlus buttons in the parent property panel + virtual void onFeatureAccepted(); + protected: /// Returns index of current page virtual int currentPageIndex() const = 0; @@ -86,7 +89,8 @@ class MODULEBASE_EXPORT ModuleBase_PagedContainer : public ModuleBase_ModelWidge bool myIsFocusOnCurrentPage; QStringList myCaseIds; QList myPages; - + bool myIsFirst; + std::string myDefValue; }; #endif /* MODULEBASE_PAGEDCONTAINER_H_ */ diff --git a/src/ModuleBase/ModuleBase_WidgetChoice.cpp b/src/ModuleBase/ModuleBase_WidgetChoice.cpp index 555fbf78b..9d48799b6 100644 --- a/src/ModuleBase/ModuleBase_WidgetChoice.cpp +++ b/src/ModuleBase/ModuleBase_WidgetChoice.cpp @@ -38,10 +38,18 @@ #include #include +static QMap defaultValues; + ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent, const Config_WidgetAPI* theData) -: ModuleBase_ModelWidget(theParent, theData)//, myCombo(0), myButtons(0) +: ModuleBase_ModelWidget(theParent, theData), myIsFirst(true) { + myHasValue = defaultValues.contains(myFeatureId); + if (myHasValue) + myDefValue = defaultValues[myFeatureId]; + else + myDefValue = 0; + QString aLabelText = translate(theData->widgetLabel()); QString aLabelIcon = QString::fromStdString(theData->widgetIcon()); std::string aTypes = theData->getProperty("string_list"); @@ -94,7 +102,16 @@ bool ModuleBase_WidgetChoice::storeValueCustom() DataPtr aData = myFeature->data(); std::shared_ptr aIntAttr = aData->integer(attributeID()); - aIntAttr->setValue(myChoiceCtrl->value()); + int aCase = 0; + if (myIsFirst) + aCase = myHasValue? myDefValue : myChoiceCtrl->value(); + else + aCase = myChoiceCtrl->value(); + + aIntAttr->setValue(aCase); + myDefValue = aCase; + myIsFirst = false; + updateObject(myFeature); return true; } @@ -125,6 +142,8 @@ bool ModuleBase_WidgetChoice::restoreValueCustom() } myChoiceCtrl->blockSignals(isBlocked); emit itemSelected(this, aIntAttr->value()); + myDefValue = aIntAttr->value(); + myIsFirst = false; } return true; } @@ -155,3 +174,8 @@ void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex) emit itemSelected(this, theIndex); } + +void ModuleBase_WidgetChoice::onFeatureAccepted() +{ + defaultValues[myFeatureId] = myDefValue; +} diff --git a/src/ModuleBase/ModuleBase_WidgetChoice.h b/src/ModuleBase/ModuleBase_WidgetChoice.h index de5b0e565..dedec5978 100644 --- a/src/ModuleBase/ModuleBase_WidgetChoice.h +++ b/src/ModuleBase/ModuleBase_WidgetChoice.h @@ -71,6 +71,9 @@ Q_OBJECT /// \return the title value QString getPropertyPanelTitle(int theIndex); + /// The slot is called when user press Ok or OkPlus buttons in the parent property panel + virtual void onFeatureAccepted(); + signals: /// Segnal about selected item /// \param theWidget selected widget @@ -89,17 +92,15 @@ private slots: void onCurrentIndexChanged(int theIndex); private: - /// The label - //QLabel* myLabel; - - /// The control - //QComboBox* myCombo; - //QButtonGroup* myButtons; ModuleBase_ChoiceCtrl* myChoiceCtrl; // XML definition of titles QStringList myButtonTitles; std::string myStringListAttribute; + + bool myIsFirst; + int myDefValue; + bool myHasValue; }; #endif diff --git a/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp b/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp index 48df5b61b..a13bdf1d0 100755 --- a/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp @@ -97,13 +97,17 @@ QStringList getIconsList(const QStringList& theNames) return aIcons; } +/// Stores default values of selected option (selection mode) +/// It is used only in case if myTypeCtrl is used +static QMap defaultValues; ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent, ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData) : ModuleBase_WidgetSelector(theParent, theWorkshop, theData), - myIsSetSelectionBlocked(false), myCurrentHistoryIndex(-1) + myIsSetSelectionBlocked(false), myCurrentHistoryIndex(-1), + myIsFirst(true) { std::string aPropertyTypes = theData->getProperty("type_choice"); QString aTypesStr = aPropertyTypes.c_str(); @@ -118,6 +122,7 @@ ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParen myTypeCtrl->setLabel(tr("Type")); myTypeCtrl->setValue(0); aMainLay->addWidget(myTypeCtrl, 0, 0, 1, 2); + myDefMode = myShapeTypes.first().toStdString(); // There is no sense to parameterize list of types while we can not parameterize selection mode // if the xml definition contains one type, the controls to select a type should not be shown @@ -154,6 +159,12 @@ ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParen connect(myTypeCtrl, SIGNAL(valueChanged(int)), this, SLOT(onSelectionTypeChanged())); myIsNeutralPointClear = theData->getBooleanAttribute("clear_in_neutral_point", true); + if (myShapeTypes.size() > 1 || myIsUseChoice) { + if (defaultValues.contains(myFeatureId)) { + myDefMode = defaultValues[myFeatureId]; + myTypeCtrl->setValue(myDefMode.c_str()); + } + } } ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector() @@ -205,7 +216,13 @@ bool ModuleBase_WidgetMultiSelector::storeValueCustom() std::string aType = anAttribute->attributeType(); if (aType == ModelAPI_AttributeSelectionList::typeId()) { AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID()); - aSelectionListAttr->setSelectionType(myTypeCtrl->textValue().toStdString()); + + std::string aMode = myTypeCtrl->textValue().toStdString(); + if (myTypeCtrl->isVisible() && myIsFirst && (!myDefMode.empty())) + aMode = myDefMode; + + aSelectionListAttr->setSelectionType(aMode); + myIsFirst = false; } return true; } @@ -223,8 +240,11 @@ bool ModuleBase_WidgetMultiSelector::restoreValueCustom() AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID()); // Restore shape type std::string aSelectionType = aSelectionListAttr->selectionType().c_str(); - if (!aSelectionType.empty()) + if (!aSelectionType.empty()) { setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionType.c_str())); + myDefMode = aSelectionType; + myIsFirst = false; + } } updateSelectionList(); return true; @@ -965,4 +985,10 @@ QList } } return aList; -} \ No newline at end of file +} + + +void ModuleBase_WidgetMultiSelector::onFeatureAccepted() +{ + defaultValues[myFeatureId] = myDefMode; +} diff --git a/src/ModuleBase/ModuleBase_WidgetMultiSelector.h b/src/ModuleBase/ModuleBase_WidgetMultiSelector.h index be8bfee91..221ba0e6b 100755 --- a/src/ModuleBase/ModuleBase_WidgetMultiSelector.h +++ b/src/ModuleBase/ModuleBase_WidgetMultiSelector.h @@ -111,6 +111,9 @@ class MODULEBASE_EXPORT ModuleBase_WidgetMultiSelector : public ModuleBase_Widge /// \param theActionType type of action. It can be ActionUndo or ActionRedo. virtual QList actionsList(ModuleBase_ActionType theActionType) const; + /// The slot is called when user press Ok or OkPlus buttons in the parent property panel + virtual void onFeatureAccepted(); + public slots: /// Slot is called on selection type changed void onSelectionTypeChanged(); @@ -239,6 +242,9 @@ protected: /// Position in a container of selected values int myCurrentHistoryIndex; + + bool myIsFirst; + std::string myDefMode; }; #endif /* MODULEBASE_WIDGETFILESELECTOR_H_ */ diff --git a/src/XGUI/XGUI_PropertyPanel.cpp b/src/XGUI/XGUI_PropertyPanel.cpp index 5d287669f..bc172e5f9 100755 --- a/src/XGUI/XGUI_PropertyPanel.cpp +++ b/src/XGUI/XGUI_PropertyPanel.cpp @@ -167,7 +167,8 @@ void XGUI_PropertyPanel::cleanContent() void XGUI_PropertyPanel::setModelWidgets(const QList& theWidgets) { myWidgets = theWidgets; - if (theWidgets.empty()) return; + if (theWidgets.empty()) + return; foreach (ModuleBase_ModelWidget* aWidget, theWidgets) { connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)), this, SLOT(onFocusInWidget(ModuleBase_ModelWidget*))); @@ -177,7 +178,6 @@ void XGUI_PropertyPanel::setModelWidgets(const QList& t this, SIGNAL(keyReleased(QObject*, QKeyEvent*))); connect(aWidget, SIGNAL(enterClicked(QObject*)), this, SIGNAL(enterClicked(QObject*))); - } } @@ -564,8 +564,20 @@ void XGUI_PropertyPanel::setupActions(XGUI_ActionsMgr* theMgr) QAction* anAct = theMgr->operationStateAction(aActionIds.at(i)); aBtn->setDefaultAction(anAct); } + QToolButton* aBtn = findButton(PROP_PANEL_OK); + connect(aBtn->defaultAction(), SIGNAL(triggered(bool)), this, SLOT(onAcceptData())); + aBtn = findButton(PROP_PANEL_OK_PLUS); + connect(aBtn->defaultAction(), SIGNAL(triggered(bool)), this, SLOT(onAcceptData())); } +void XGUI_PropertyPanel::onAcceptData() +{ + foreach (ModuleBase_ModelWidget* aWidget, myWidgets) { + aWidget->onFeatureAccepted(); + } +} + + ModuleBase_ModelWidget* XGUI_PropertyPanel::preselectionWidget() const { return myPreselectionWidget; diff --git a/src/XGUI/XGUI_PropertyPanel.h b/src/XGUI/XGUI_PropertyPanel.h index 5b1a68b88..21971af11 100644 --- a/src/XGUI/XGUI_PropertyPanel.h +++ b/src/XGUI/XGUI_PropertyPanel.h @@ -160,6 +160,8 @@ public slots: /// \param theWidget the current widget void onActivateNextWidget(ModuleBase_ModelWidget* theWidget); + void onAcceptData(); + signals: /// The signal is emitted if the enter is clicked in the control of the widget /// \param theObject a sender of the event -- 2.39.2