From: nds Date: Wed, 13 Jul 2016 09:08:53 +0000 (+0300) Subject: #1659 New widget for supporting optional inputs X-Git-Tag: V_2.5.0~199 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=6ab4dbd77b8412fb60d23c5c8937af0121bdaa39;p=modules%2Fshaper.git #1659 New widget for supporting optional inputs --- diff --git a/src/ModuleBase/ModuleBase_PageBase.cpp b/src/ModuleBase/ModuleBase_PageBase.cpp index 38eff9ce7..8d012cbf9 100644 --- a/src/ModuleBase/ModuleBase_PageBase.cpp +++ b/src/ModuleBase/ModuleBase_PageBase.cpp @@ -80,7 +80,7 @@ bool ModuleBase_PageBase::takeFocus() return myWidgetList.first()->focusTo(); } -QList ModuleBase_PageBase::modelWidgets() +QList ModuleBase_PageBase::modelWidgets() const { return myWidgetList; } diff --git a/src/ModuleBase/ModuleBase_PageBase.h b/src/ModuleBase/ModuleBase_PageBase.h index 7f695f7bd..80fcca758 100644 --- a/src/ModuleBase/ModuleBase_PageBase.h +++ b/src/ModuleBase/ModuleBase_PageBase.h @@ -39,7 +39,7 @@ class MODULEBASE_EXPORT ModuleBase_PageBase /// Passes focus from page to the first ModuleBase_ModelWidget contained on the page bool takeFocus(); /// Returns list of ModuleBase_ModelWidgets contained on the page - QList modelWidgets(); + QList modelWidgets() const; /// Aligns top all widgets on page void alignToTop(); diff --git a/src/ModuleBase/ModuleBase_WidgetCheckGroupBox.cpp b/src/ModuleBase/ModuleBase_WidgetCheckGroupBox.cpp index a2bf7d60e..4a233ee2d 100755 --- a/src/ModuleBase/ModuleBase_WidgetCheckGroupBox.cpp +++ b/src/ModuleBase/ModuleBase_WidgetCheckGroupBox.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -20,47 +21,41 @@ ModuleBase_WidgetCheckGroupBox::ModuleBase_WidgetCheckGroupBox(QWidget* theParent, const Config_WidgetAPI* theData) : ModuleBase_ModelWidget(theParent, theData), - ModuleBase_PageBase() + ModuleBase_PageBase(), + myOptionType(CheckBox), + myCheckBoxFrame(0), + myCheckBox(0), + myCheckBoxLayout(0), + myCheckBoxWidget(0), + myGroupBox(0), + myGroupBoxLayout(0) { - QString aToolTip = QString::fromStdString(theData->widgetTooltip()); + myToolTip = theData->widgetTooltip(); + myGroupTitle = theData->getProperty(CONTAINER_PAGE_NAME); + bool isChecked = theData->getBooleanAttribute(ATTR_DEFAULT, false); setDefaultValue(isChecked ? "true" : "false"); - QVBoxLayout* aMainLayout = new QVBoxLayout(this); - ModuleBase_Tools::zeroMargins(aMainLayout); - aMainLayout->setMargin(2); - myGroupBox = new QGroupBox(this); - myGroupBox->setCheckable(true); - myGroupBox->setToolTip(aToolTip); - myGroupBox->setChecked(isChecked); - - myMainLayout = new QGridLayout(myGroupBox); - ModuleBase_Tools::zeroMargins(myMainLayout); - myGroupBox->setLayout(myMainLayout); - - // default vertical size policy is preferred - aMainLayout->addWidget(myGroupBox); - connect(myGroupBox, SIGNAL(clicked(bool)), this, SLOT(onPageClicked())); + myMainLayout = new QVBoxLayout(this); + ModuleBase_Tools::adjustMargins(myMainLayout); } ModuleBase_WidgetCheckGroupBox::~ModuleBase_WidgetCheckGroupBox() { } -void ModuleBase_WidgetCheckGroupBox::setTitle(const QString& theTitle) -{ - myGroupBox->setTitle(theTitle); -} - QWidget* ModuleBase_WidgetCheckGroupBox::pageWidget() { - return myGroupBox; + return myOptionType == GroupBox ? myGroupBox : (QWidget*)myCheckBoxFrame; } QList ModuleBase_WidgetCheckGroupBox::getControls() const { QList aControls; - aControls.append(myGroupBox); + if (myOptionType == GroupBox) + aControls.append(myGroupBox); + else + aControls.append(myCheckBoxFrame); return aControls; } @@ -77,19 +72,40 @@ void ModuleBase_WidgetCheckGroupBox::addPageStretch() void ModuleBase_WidgetCheckGroupBox::placeModelWidget(ModuleBase_ModelWidget* theWidget) { - const int kCol = 0; - const int kRow = myMainLayout->count(); - // it seems, that the align on left is not necessary here, but leads to widgets, which are - // not extended on full width of the parent page. The case is grouped widgets in - // the sketch translation operation - myMainLayout->addWidget(theWidget, kRow, kCol, Qt::AlignTop);// | Qt::AlignLeft); - myMainLayout->setRowStretch(kRow, 0); + if(!isCheckBoxFilled() && myOptionType == CheckBox) { + createControl(CheckBox); + setOptionType(CheckBox); - theWidget->setVisible(myGroupBox->isChecked()); + myCheckBoxWidget = theWidget; /// check box frame becomes filled + myCheckBoxLayout->addWidget(theWidget); + } + else { + createControl(GroupBox); + setOptionType(GroupBox); + + ModuleBase_ModelWidget* aCheckBoxWidget = myCheckBoxWidget; + myCheckBoxWidget = 0; + if (aCheckBoxWidget) + placeModelWidget(aCheckBoxWidget); + + const int kCol = 0; + const int kRow = myGroupBoxLayout->count(); + // it seems, that the align on left is not necessary here, but leads to widgets, which are + // not extended on full width of the parent page. The case is grouped widgets in + // the sketch translation operation + myGroupBoxLayout->addWidget(theWidget, kRow, kCol, Qt::AlignTop);// | Qt::AlignLeft); + myGroupBoxLayout->setRowStretch(kRow, 0); + } } void ModuleBase_WidgetCheckGroupBox::placeWidget(QWidget* theWidget) { + createControl(GroupBox); + setOptionType(GroupBox); + if (isCheckBoxFilled()) { + placeModelWidget(myCheckBoxWidget); + } + if (!theWidget) { #ifdef _DEBUG std::cout << "ModuleBase_PageGroupBox::placePageWidget: can not cast page" << std::endl; @@ -97,21 +113,54 @@ void ModuleBase_WidgetCheckGroupBox::placeWidget(QWidget* theWidget) return; } const int kCol = 0; - const int kRow = myMainLayout->count(); - myMainLayout->addWidget(theWidget, kRow, kCol); - myMainLayout->setRowStretch(kRow, 0); + const int kRow = myGroupBoxLayout->count(); + myGroupBoxLayout->addWidget(theWidget, kRow, kCol); + myGroupBoxLayout->setRowStretch(kRow, 0); } QLayout* ModuleBase_WidgetCheckGroupBox::pageLayout() { - return myMainLayout; + return myOptionType == GroupBox ? myGroupBoxLayout : (QLayout*)myCheckBoxLayout; +} + +void ModuleBase_WidgetCheckGroupBox::createControl(const OptionType& theType) +{ + if (theType == GroupBox && !myGroupBox) { + // group box: more than one model widget is inside + myGroupBox = new QGroupBox(this); + myGroupBox->setTitle(QString::fromStdString(myGroupTitle)); + myGroupBox->setVisible(false); + myGroupBox->setCheckable(true); + myGroupBox->setChecked(getDefaultValue() == "true"); + myGroupBox->setToolTip(QString::fromStdString(myToolTip)); + + myGroupBoxLayout = new QGridLayout(myGroupBox); + ModuleBase_Tools::zeroMargins(myGroupBoxLayout); + myGroupBox->setLayout(myGroupBoxLayout); + + // default vertical size policy is preferred + myMainLayout->addWidget(myGroupBox); + connect(myGroupBox, SIGNAL(clicked(bool)), this, SLOT(onPageClicked())); + } + else if (theType == CheckBox && !myCheckBoxFrame) { + myCheckBoxFrame = new QFrame(this); + myMainLayout->addWidget(myCheckBoxFrame); + + myCheckBoxLayout = new QHBoxLayout(myCheckBoxFrame); + ModuleBase_Tools::adjustMargins(myCheckBoxLayout); + myCheckBox = new QCheckBox(myCheckBoxFrame); + myCheckBox->setChecked(getDefaultValue() == "true"); + myCheckBoxLayout->addWidget(myCheckBox); + + connect(myCheckBox, SIGNAL(clicked(bool)), this, SLOT(onPageClicked())); + } } bool ModuleBase_WidgetCheckGroupBox::storeValueCustom() { DataPtr aData = myFeature->data(); AttributeStringPtr aStringAttr = aData->string(attributeID()); - aStringAttr->setValue(myGroupBox->isChecked() ? attributeID() : ""); + aStringAttr->setValue(getCurrentValue() ? attributeID() : ""); updateObject(myFeature); @@ -122,28 +171,68 @@ bool ModuleBase_WidgetCheckGroupBox::restoreValueCustom() { DataPtr aData = myFeature->data(); AttributeStringPtr aStringAttr = aData->string(attributeID()); - - bool isBlocked = myGroupBox->blockSignals(true); - myGroupBox->setChecked(!aStringAttr->value().empty()); - updatePagesVisibility(); - myGroupBox->blockSignals(isBlocked); + setCurrentValue(!aStringAttr->value().empty()); return true; } -void ModuleBase_WidgetCheckGroupBox::updatePagesVisibility() +void ModuleBase_WidgetCheckGroupBox::setOptionType( + const ModuleBase_WidgetCheckGroupBox::OptionType& theType) { - bool aVisible = myGroupBox->isChecked(); + myOptionType = theType; - if (aVisible) - ModuleBase_Tools::zeroMargins(myMainLayout); - else - ModuleBase_Tools::adjustMargins(myMainLayout); + bool isGroupBox = myOptionType == GroupBox; + if (myCheckBoxFrame) + myCheckBoxFrame->setVisible(!isGroupBox); + if (myGroupBox) + myGroupBox->setVisible(isGroupBox); +} + +bool ModuleBase_WidgetCheckGroupBox::isCheckBoxFilled() const +{ + return myCheckBoxWidget != 0; +} + +bool ModuleBase_WidgetCheckGroupBox::getCurrentValue() const +{ + bool isGroupBox = myOptionType == GroupBox; + return isGroupBox ? myGroupBox->isChecked() : myCheckBox->isChecked(); +} - int aNbSubControls = myMainLayout->count(); - for (int i = 0; i < aNbSubControls; i++) { - QWidget* aWidget = myMainLayout->itemAt(i)->widget(); - if (aWidget) - aWidget->setVisible(aVisible); +void ModuleBase_WidgetCheckGroupBox::setCurrentValue(const bool& theValue) +{ + bool isGroupBox = myOptionType == GroupBox; + if (isGroupBox) { + bool isBlocked = myGroupBox->blockSignals(true); + myGroupBox->setChecked(theValue); + myGroupBox->blockSignals(isBlocked); + } + else { + bool isBlocked = myCheckBox->blockSignals(true); + myCheckBox->setChecked(theValue); + myCheckBox->blockSignals(isBlocked); + } + updatePagesVisibility(); +} + +void ModuleBase_WidgetCheckGroupBox::updatePagesVisibility() +{ + if (myOptionType == GroupBox) { + bool aChecked = myGroupBox->isChecked(); + if (aChecked) + ModuleBase_Tools::zeroMargins(myGroupBoxLayout); + else + ModuleBase_Tools::adjustMargins(myGroupBoxLayout); + + int aNbSubControls = myGroupBoxLayout->count(); + for (int i = 0; i < aNbSubControls; i++) { + QWidget* aWidget = myGroupBoxLayout->itemAt(i)->widget(); + if (aWidget) + aWidget->setVisible(aChecked); + } + } + else { + bool aChecked = myCheckBox->isChecked(); + myCheckBoxWidget->setEnabled(aChecked); } } diff --git a/src/ModuleBase/ModuleBase_WidgetCheckGroupBox.h b/src/ModuleBase/ModuleBase_WidgetCheckGroupBox.h index 5cf821eef..8db7491d7 100755 --- a/src/ModuleBase/ModuleBase_WidgetCheckGroupBox.h +++ b/src/ModuleBase/ModuleBase_WidgetCheckGroupBox.h @@ -12,6 +12,10 @@ #include class QGroupBox; +class QFrame; +class QCheckBox; +class QVBoxLayout; +class QHBoxLayout; class QGridLayout; /** @@ -22,6 +26,8 @@ class MODULEBASE_EXPORT ModuleBase_WidgetCheckGroupBox : public ModuleBase_Model public ModuleBase_PageBase { Q_OBJECT + + enum OptionType {CheckBox, GroupBox}; public: /// Constructor /// \param theParent the parent object @@ -33,10 +39,6 @@ public: /// \return false because this is an info widget virtual bool canAcceptFocus() const { return false; }; - /// Set the title of the group box - /// \param theTitle the text of the title - void setTitle(const QString& theTitle); - /// Methods to be redefined from ModuleBase_PageBase: start /// Cast the page to regular QWidget virtual QWidget* pageWidget(); @@ -74,13 +76,43 @@ protected: /// Methods to be redefined from ModuleBase_ModelWidget: end private: - /// Makes the children page of the group box visible if the checked state is on - /// otherwise, make the sub-pages is not visible. - void updatePagesVisibility(); + /// Sets whether Check box or Group box is used currently + /// \param theType a type of the control + void setOptionType(const OptionType& theType); + + /// Returns true if the model widget is set into the check box frame + /// \return boolean value + bool isCheckBoxFilled() const; + + /// Creates controls for the given type if it was not perfomed yet + /// \param theType a type to create whether check box or group box controls + void createControl(const OptionType& theType); + + /// Returns true if control is checked + /// \return boolean value + bool getCurrentValue() const; + + /// Returns true if control is checked + /// \param theValue a new value to fill the control state + void setCurrentValue(const bool& theValue); + + /// Makes model widget controls either disabled(for check box mode) or hidden(for group box mode) + void updateControlsVisibility(); private: - QGroupBox* myGroupBox; - QGridLayout* myMainLayout; ///< page's layout + std::string myToolTip; ///< tool tip defined in XML + std::string myGroupTitle; ///< a title for group box + + OptionType myOptionType; ///< current option state + QVBoxLayout* myMainLayout; ///< the main layout + // controls to fill check box frame + QFrame* myCheckBoxFrame; ///< frame with check box, to be hidden when group box is used + QCheckBox* myCheckBox; ///< control used if only one model widget is placed inside + QHBoxLayout* myCheckBoxLayout; ///< layout of check box frame + ModuleBase_ModelWidget* myCheckBoxWidget; /// model widget, placed in the check box frame + // controls to fill group box frame + QGroupBox* myGroupBox; ///< control used if more than model widget is placed inside + QGridLayout* myGroupBoxLayout; ///< page's layout }; #endif /* ModuleBase_WidgetCheckGroupBox_H_ */ diff --git a/src/ModuleBase/ModuleBase_WidgetFactory.cpp b/src/ModuleBase/ModuleBase_WidgetFactory.cpp index d8e6bc262..8016ab6ea 100644 --- a/src/ModuleBase/ModuleBase_WidgetFactory.cpp +++ b/src/ModuleBase/ModuleBase_WidgetFactory.cpp @@ -258,10 +258,8 @@ ModuleBase_PageBase* ModuleBase_WidgetFactory::createPageByType(const std::strin aResult = aPage; } else if (theType == WDG_CHECK_GROUP) { - QString aGroupName = qs(myWidgetApi->getProperty(CONTAINER_PAGE_NAME)); ModuleBase_WidgetCheckGroupBox* aPage = new ModuleBase_WidgetCheckGroupBox(theParent, myWidgetApi); - aPage->setTitle(aGroupName); aResult = aPage; } if (!aResult)