X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModuleBase%2FModuleBase_WidgetChoice.cpp;h=4f52685f379732730a6f8f46b69f03aa8260ec4f;hb=refs%2Fheads%2FV9_11_BR;hp=65c6c189077c27f97a42a1eeafac4bbfb49cddc3;hpb=f4f01be0d0f2642aaca4844b34c98a062e2afbb9;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_WidgetChoice.cpp b/src/ModuleBase/ModuleBase_WidgetChoice.cpp index 65c6c1890..4f52685f3 100644 --- a/src/ModuleBase/ModuleBase_WidgetChoice.cpp +++ b/src/ModuleBase/ModuleBase_WidgetChoice.cpp @@ -1,85 +1,186 @@ -// File: ModuleBase_WidgetChoice.cpp -// Created: 03 Sept 2014 -// Author: Vitaly Smetannikov +// Copyright (C) 2014-2023 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// 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 +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// #include "ModuleBase_WidgetChoice.h" -#include +#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 -ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent, - const Config_WidgetAPI* theData, - const std::string& theParentId) - : ModuleBase_ModelWidget(theParent, theData, theParentId) +static QMap defaultValues; + +ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent, + const Config_WidgetAPI* theData) +: ModuleBase_ModelWidget(theParent, theData), myIsFirst(true) { - myContainer = new QWidget(theParent); - QHBoxLayout* aLayout = new QHBoxLayout(myContainer); - ModuleBase_Tools::adjustMargins(aLayout); + myHasValue = defaultValues.contains(myFeatureId + attributeID()); + if (myHasValue) + myDefValue = defaultValues[myFeatureId + attributeID()]; + else + myDefValue = 0; - QString aLabelText = QString::fromStdString(theData->widgetLabel()); + QString aLabelText = translate(theData->widgetLabel()); QString aLabelIcon = QString::fromStdString(theData->widgetIcon()); - myLabel = new QLabel(aLabelText, myContainer); + std::string aTypes = theData->getProperty("string_list"); + QStringList aList; + + 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"); + std::string aIcons = theData->getProperty("icons_list"); + QStringList aIconList = QString(aIcons.c_str()).split(' '); + + std::string aWgtDir = theData->getProperty("buttons_dir"); + + 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); + if (!aLabelIcon.isEmpty()) - myLabel->setPixmap(QPixmap(aLabelIcon)); - aLayout->addWidget(myLabel); + myChoiceCtrl->setLabelIcon(aLabelIcon); - myCombo = new QComboBox(myContainer); - aLayout->addWidget(myCombo, 1); - - std::string aTypes = theData->getProperty("string_list"); - QStringList aList = QString(aTypes.c_str()).split(' '); - myCombo->addItems(aList); + 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() { } - -bool ModuleBase_WidgetChoice::storeValue() const + +bool ModuleBase_WidgetChoice::storeValueCustom() { DataPtr aData = myFeature->data(); - boost::shared_ptr aIntAttr = aData->integer(attributeID()); + std::shared_ptr aIntAttr = aData->integer(attributeID()); + + int aCase = 0; + if (myIsFirst) + aCase = myHasValue? myDefValue : myChoiceCtrl->value(); + else + aCase = myChoiceCtrl->value(); + + aIntAttr->setValue(aCase); + myDefValue = aCase; + myIsFirst = false; - aIntAttr->setValue(myCombo->currentIndex()); updateObject(myFeature); return true; } -bool ModuleBase_WidgetChoice::restoreValue() +bool ModuleBase_WidgetChoice::restoreValueCustom() { DataPtr aData = myFeature->data(); - boost::shared_ptr aIntAttr = aData->integer(attributeID()); + std::shared_ptr aIntAttr = aData->integer(attributeID()); + + if (aIntAttr->value() != -1) { + 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); + } + } + 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); - bool isBlocked = myCombo->blockSignals(true); - myCombo->setCurrentIndex(aIntAttr->value()); - myCombo->blockSignals(isBlocked); + myChoiceCtrl->blockSignals(isBlocked); + emit itemSelected(this, aVal); + myDefValue = aVal; + } + myIsFirst = false; + } return true; } bool ModuleBase_WidgetChoice::focusTo() { - myCombo->setFocus(); - return true; + return myChoiceCtrl->focusTo(); } QList ModuleBase_WidgetChoice::getControls() const { - QList aControls; - aControls.append(myCombo); - return aControls; + return myChoiceCtrl->getControls(); +} + +QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex) +{ + QString aTitle; + if (myButtonTitles.length() > theIndex) + aTitle = myButtonTitles[theIndex]; + return aTitle; } void ModuleBase_WidgetChoice::onCurrentIndexChanged(int theIndex) { emit valuesChanged(); - emit focusOutWidget(this); + // Don't transfer focus + // emit focusOutWidget(this); + + emit itemSelected(this, theIndex); +} + +void ModuleBase_WidgetChoice::onFeatureAccepted() +{ + defaultValues[myFeatureId + attributeID()] = myDefValue; }