X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModuleBase%2FModuleBase_WidgetChoice.cpp;h=82ddf9cc6e062cb7300cff2ea93a4f41c390796e;hb=fc72d43b677baa05ae7fd317346fd8b723b799ed;hp=1bc6987e13ca85abce3a4453421d18871d0226da;hpb=60a67b71f3adcdcc8a27d4f3042efb89bcc81a51;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_WidgetChoice.cpp b/src/ModuleBase/ModuleBase_WidgetChoice.cpp index 1bc6987e1..82ddf9cc6 100644 --- a/src/ModuleBase/ModuleBase_WidgetChoice.cpp +++ b/src/ModuleBase/ModuleBase_WidgetChoice.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2017 CEA/DEN, EDF R&D +// Copyright (C) 2014-2023 CEA/DEN, EDF R&D // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -12,36 +12,41 @@ // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // -// See http://www.salome-platform.org/ or -// email : webmaster.salome@opencascade.com +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // #include "ModuleBase_WidgetChoice.h" #include "ModuleBase_Tools.h" #include "ModuleBase_IconFactory.h" +#include "ModuleBase_ChoiceCtrl.h" #include +#include #include #include +#include #include #include #include -#include #include #include #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) { - QHBoxLayout* aLayout = new QHBoxLayout(this); - ModuleBase_Tools::adjustMargins(aLayout); + myHasValue = defaultValues.contains(myFeatureId + attributeID()); + if (myHasValue) + myDefValue = defaultValues[myFeatureId + attributeID()]; + else + myDefValue = 0; QString aLabelText = translate(theData->widgetLabel()); QString aLabelIcon = QString::fromStdString(theData->widgetIcon()); @@ -51,78 +56,39 @@ ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent, foreach(QString aType, QString(aTypes.c_str()).split(' ')) { aList.append(translate(aType.toStdString())); } - + if (aTypes.empty()) { + myStringListAttribute = theData->getProperty("string_list_attribute"); + if (!myStringListAttribute.empty()) + aList.clear(); + } if (theData->getBooleanAttribute("use_in_title", false)) myButtonTitles = aList; bool aHasDefaultValue; int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue); + // Widget type can be combobox or radiobuttons std::string aWgtType = theData->getProperty("widget_type"); - if ((aWgtType.length() > 0) && (aWgtType == "radiobuttons")) { - myButtons = new QButtonGroup(this); - QGroupBox* aGroupBox = new QGroupBox(aLabelText, this); - aLayout->addWidget(aGroupBox); - - - QLayout* aBtnLayout = 0; - std::string aWgtDir = theData->getProperty("buttons_dir"); - if (aWgtDir == "horizontal") - aBtnLayout = new QHBoxLayout(aGroupBox); - else - aBtnLayout = new QVBoxLayout(aGroupBox); - ModuleBase_Tools::adjustMargins(aBtnLayout); - - std::string aIcons = theData->getProperty("icons_list"); - QStringList aIconList = QString(aIcons.c_str()).split(' '); - if (aIconList.length() == aList.length()) { - int aId = 0; - foreach(QString aBtnTxt, aList) { - QToolButton* aBtn = new QToolButton(aGroupBox); - aBtn->setFocusPolicy(Qt::StrongFocus); - aBtn->setCheckable(true); - aBtn->setToolTip(aBtnTxt); - - QPixmap aIcon = ModuleBase_IconFactory::loadPixmap(aIconList.at(aId)); - aBtn->setIcon(aIcon); - aBtn->setIconSize(aIcon.size()); - - aBtnLayout->addWidget(aBtn); - myButtons->addButton(aBtn, aId++); - } + std::string aIcons = theData->getProperty("icons_list"); + QStringList aIconList = QString(aIcons.c_str()).split(' '); - } else { - int aId = 0; - foreach(QString aBtnTxt, aList) { - QRadioButton* aBtn = new QRadioButton(aBtnTxt, aGroupBox); - aBtnLayout->addWidget(aBtn); - myButtons->addButton(aBtn, aId++); - } - } - int aCheckedId = aHasDefaultValue ? aDefaultVal : 0; - myButtons->button(aDefaultVal)->setChecked(true); - connect(myButtons, SIGNAL(buttonClicked(int)), this, SLOT(onCurrentIndexChanged(int))); - } else { - myLabel = new QLabel(aLabelText, this); - if (!aLabelIcon.isEmpty()) - myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon)); - aLayout->addWidget(myLabel); - - std::string aToolstr = theData->widgetTooltip(); - if (!aToolstr.empty()) { - myLabel->setToolTip(QString::fromStdString(aToolstr)); - } + std::string aWgtDir = theData->getProperty("buttons_dir"); - myCombo = new QComboBox(this); - aLayout->addWidget(myCombo, 1); + QHBoxLayout* aLayout = new QHBoxLayout(this); + myChoiceCtrl = new ModuleBase_ChoiceCtrl(this, aList, aIconList, + (aWgtType == "radiobuttons")? ModuleBase_ChoiceCtrl::RadioButtons : + ModuleBase_ChoiceCtrl::ComboBox, + (aWgtDir == "horizontal")? Qt::Horizontal : Qt::Vertical); + myChoiceCtrl->setLabel(aLabelText); - myCombo->addItems(aList); + if (!aLabelIcon.isEmpty()) + myChoiceCtrl->setLabelIcon(aLabelIcon); - if (aHasDefaultValue && aDefaultVal < aList.size()) - myCombo->setCurrentIndex(aDefaultVal); + connect(myChoiceCtrl, SIGNAL(valueChanged(int)), this, SLOT(onCurrentIndexChanged(int))); - connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int))); - } + int aCheckedId = aHasDefaultValue ? aDefaultVal : 0; + myChoiceCtrl->setValue(aCheckedId); + aLayout->addWidget(myChoiceCtrl); } ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice() @@ -134,10 +100,16 @@ bool ModuleBase_WidgetChoice::storeValueCustom() DataPtr aData = myFeature->data(); std::shared_ptr aIntAttr = aData->integer(attributeID()); - if (myCombo) - aIntAttr->setValue(myCombo->currentIndex()); + int aCase = 0; + if (myIsFirst) + aCase = myHasValue? myDefValue : myChoiceCtrl->value(); else - aIntAttr->setValue(myButtons->checkedId()); + aCase = myChoiceCtrl->value(); + + aIntAttr->setValue(aCase); + myDefValue = aCase; + myIsFirst = false; + updateObject(myFeature); return true; } @@ -148,46 +120,47 @@ bool ModuleBase_WidgetChoice::restoreValueCustom() std::shared_ptr aIntAttr = aData->integer(attributeID()); if (aIntAttr->value() != -1) { - if (myCombo) { - bool isBlocked = myCombo->blockSignals(true); - myCombo->setCurrentIndex(aIntAttr->value()); - myCombo->blockSignals(isBlocked); - } else { - bool isBlocked = myButtons->blockSignals(true); - if (aIntAttr->isInitialized()) - myButtons->button(aIntAttr->value())->setChecked(true); - else { - bool aHasDefaultValue; - int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue); - myButtons->button(aHasDefaultValue ? aDefaultVal : 0)->setChecked(true); + bool isBlocked = myChoiceCtrl->blockSignals(true); + if (!myStringListAttribute.empty()) { + AttributeStringArrayPtr aStrAttr = aData->stringArray(myStringListAttribute); + QStringList aChoiceList; + if (aStrAttr) { + for (int i = 0; i < aStrAttr->size(); i++) { + aChoiceList << aStrAttr->value(i).c_str(); + } + myChoiceCtrl->setChoiceList(aChoiceList); } - myButtons->blockSignals(isBlocked); + } + if (aIntAttr->isInitialized()) { + myChoiceCtrl->setValue(aIntAttr->value()); + + myChoiceCtrl->blockSignals(isBlocked); emit itemSelected(this, aIntAttr->value()); + myDefValue = aIntAttr->value(); + } + else { + bool aHasDefaultValue; + int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue); + int aVal = aHasDefaultValue ? aDefaultVal : 0; + myChoiceCtrl->setValue(aVal); + + myChoiceCtrl->blockSignals(isBlocked); + emit itemSelected(this, aVal); + myDefValue = aVal; } + myIsFirst = false; } return true; } bool ModuleBase_WidgetChoice::focusTo() { - if (myCombo) - ModuleBase_Tools::setFocus(myCombo, "ModuleBase_WidgetChoice::focusTo()"); - else - return false; - return true; + return myChoiceCtrl->focusTo(); } QList ModuleBase_WidgetChoice::getControls() const { - QList aControls; - if (myCombo) - aControls.append(myCombo); - //else { - // //foreach(QAbstractButton* aBtn, myButtons->buttons()) - // //if (myButtons->checkedId() != -1) - // // aControls.append(myButtons->button(myButtons->checkedId())); - //} - return aControls; + return myChoiceCtrl->getControls(); } QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex) @@ -206,3 +179,8 @@ void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex) emit itemSelected(this, theIndex); } + +void ModuleBase_WidgetChoice::onFeatureAccepted() +{ + defaultValues[myFeatureId + attributeID()] = myDefValue; +}