From 4d0ed9b6a97b4eacae1ce66dc6804c740751dfb9 Mon Sep 17 00:00:00 2001 From: sbh Date: Fri, 13 Mar 2015 19:06:25 +0300 Subject: [PATCH] Switch and Toolbox refactored: common methods are extracted into base class --- src/ConstructionPlugin/plane_widget.xml | 26 ++-- src/ModuleBase/CMakeLists.txt | 10 +- src/ModuleBase/ModuleBase_PagedContainer.cpp | 108 +++++++++++++++ src/ModuleBase/ModuleBase_PagedContainer.h | 50 +++++++ src/ModuleBase/ModuleBase_WidgetFactory.cpp | 10 +- src/ModuleBase/ModuleBase_WidgetSwitch.cpp | 133 +++++-------------- src/ModuleBase/ModuleBase_WidgetSwitch.h | 96 ++----------- src/ModuleBase/ModuleBase_WidgetToolbox.cpp | 80 ++--------- src/ModuleBase/ModuleBase_WidgetToolbox.h | 25 ++-- 9 files changed, 243 insertions(+), 295 deletions(-) create mode 100644 src/ModuleBase/ModuleBase_PagedContainer.cpp create mode 100644 src/ModuleBase/ModuleBase_PagedContainer.h diff --git a/src/ConstructionPlugin/plane_widget.xml b/src/ConstructionPlugin/plane_widget.xml index 492bffad0..7600c1b19 100644 --- a/src/ConstructionPlugin/plane_widget.xml +++ b/src/ConstructionPlugin/plane_widget.xml @@ -1,23 +1,20 @@ - - - - - + + + + - diff --git a/src/ModuleBase/CMakeLists.txt b/src/ModuleBase/CMakeLists.txt index 9b834e9ad..cf2020c12 100644 --- a/src/ModuleBase/CMakeLists.txt +++ b/src/ModuleBase/CMakeLists.txt @@ -23,7 +23,6 @@ SET(PROJECT_HEADERS ModuleBase_WidgetDoubleValue.h ModuleBase_WidgetEditor.h ModuleBase_WidgetFactory.h - ModuleBase_WidgetSwitch.h ModuleBase_WidgetShapeSelector.h ModuleBase_IWorkshop.h ModuleBase_Definitions.h @@ -44,10 +43,12 @@ SET(PROJECT_HEADERS ModuleBase_IPrefMgr.h ModuleBase_Preferences.h ModuleBase_ActionInfo.h - ModuleBase_WidgetToolbox.h ModuleBase_PageBase.h ModuleBase_PageWidget.h ModuleBase_PageGroupBox.h + ModuleBase_PagedContainer.h + ModuleBase_WidgetSwitch.h + ModuleBase_WidgetToolbox.h ModuleBase_WidgetValidated.h ) @@ -71,7 +72,6 @@ SET(PROJECT_SOURCES ModuleBase_WidgetDoubleValue.cpp ModuleBase_WidgetEditor.cpp ModuleBase_WidgetFactory.cpp - ModuleBase_WidgetSwitch.cpp ModuleBase_WidgetShapeSelector.cpp ModuleBase_WidgetChoice.cpp ModuleBase_WidgetFileSelector.cpp @@ -83,10 +83,12 @@ SET(PROJECT_SOURCES ModuleBase_WidgetLabel.cpp ModuleBase_Preferences.cpp ModuleBase_ActionInfo.cpp - ModuleBase_WidgetToolbox.cpp ModuleBase_PageBase.cpp ModuleBase_PageWidget.cpp ModuleBase_PageGroupBox.cpp + ModuleBase_PagedContainer.cpp + ModuleBase_WidgetSwitch.cpp + ModuleBase_WidgetToolbox.cpp ModuleBase_WidgetValidated.cpp ) diff --git a/src/ModuleBase/ModuleBase_PagedContainer.cpp b/src/ModuleBase/ModuleBase_PagedContainer.cpp new file mode 100644 index 000000000..d067e100c --- /dev/null +++ b/src/ModuleBase/ModuleBase_PagedContainer.cpp @@ -0,0 +1,108 @@ +/* + * ModuleBase_PagedContainer.cpp + * + * Created on: Mar 13, 2015 + * Author: sbh + */ + +#include +#include +#include +#include + +#include + +#include +#include +#include + + +ModuleBase_PagedContainer::ModuleBase_PagedContainer(QWidget* theParent, const Config_WidgetAPI* theData, + const std::string& theParentId) +: ModuleBase_ModelWidget(theParent, theData, theParentId), + myIsFocusOnCurrentPage(false) +{ +} + +ModuleBase_PagedContainer::~ModuleBase_PagedContainer() +{ +} + +int ModuleBase_PagedContainer::addPage(ModuleBase_PageBase* thePage, + const QString& theName, const QString& theCaseId) +{ + myCaseIds << theCaseId; + myPages << thePage; + return myPages.count(); +} + +QList ModuleBase_PagedContainer::getControls() const +{ + QList aResult; + int anIndex = currentPageIndex(); + QList aModelWidgets = myPages[anIndex]->modelWidgets(); + foreach(ModuleBase_ModelWidget* eachModelWidget, aModelWidgets) { + aResult << eachModelWidget->getControls(); + } + return aResult; +} + +bool ModuleBase_PagedContainer::focusTo() +{ + int anIndex = currentPageIndex(); + if (anIndex > myPages.count()) + return false; + return myPages[anIndex]->takeFocus(); +} + +void ModuleBase_PagedContainer::setHighlighted(bool) +{ + //page containers should not be highlighted, do nothing +} + +void ModuleBase_PagedContainer::enableFocusProcessing() +{ + myIsFocusOnCurrentPage = true; +} + +bool ModuleBase_PagedContainer::restoreValue() +{ + // A rare case when plugin was not loaded. + if(!myFeature) + return false; + DataPtr aData = myFeature->data(); + AttributeStringPtr aStringAttr = aData->string(attributeID()); + QString aCaseId = QString::fromStdString(aStringAttr->value()); + int idx = myCaseIds.indexOf(aCaseId); + if (idx == -1) + return false; + setCurrentPageIndex(idx); + return true; +} + +void ModuleBase_PagedContainer::activateCustom() +{ + // activate current page + focusTo(); +} + +bool ModuleBase_PagedContainer::storeValueCustom() const +{ + // A rare case when plugin was not loaded. + if(!myFeature) + return false; + DataPtr aData = myFeature->data(); + AttributeStringPtr aStringAttr = aData->string(attributeID()); + QString aWidgetValue = myCaseIds.at(currentPageIndex()); + aStringAttr->setValue(aWidgetValue.toStdString()); + return true; +} + + +void ModuleBase_PagedContainer::onPageChanged() +{ + storeValue(); + if (myIsFocusOnCurrentPage) focusTo(); +} + + diff --git a/src/ModuleBase/ModuleBase_PagedContainer.h b/src/ModuleBase/ModuleBase_PagedContainer.h new file mode 100644 index 000000000..d0677d3c0 --- /dev/null +++ b/src/ModuleBase/ModuleBase_PagedContainer.h @@ -0,0 +1,50 @@ +/* + * ModuleBase_PagedContainer.h + * + * Created on: Mar 13, 2015 + * Author: sbh + */ + +#ifndef MODULEBASE_PAGEDCONTAINER_H_ +#define MODULEBASE_PAGEDCONTAINER_H_ + +#include +#include + +class ModuleBase_PageBase; + +class MODULEBASE_EXPORT ModuleBase_PagedContainer : public ModuleBase_ModelWidget +{ + Q_OBJECT + public: + ModuleBase_PagedContainer(QWidget* theParent, const Config_WidgetAPI* theData, + const std::string& theParentId); + virtual ~ModuleBase_PagedContainer(); + + virtual int addPage(ModuleBase_PageBase* theWidget, + const QString& theName, const QString& theCaseId); + // ModuleBase_ModelWidget + virtual QList getControls() const; + virtual bool focusTo(); + virtual void setHighlighted(bool isHighlighted); + virtual void enableFocusProcessing(); + virtual bool restoreValue(); + + protected: + virtual int currentPageIndex() const = 0; + virtual void setCurrentPageIndex(int ) = 0; + // ModuleBase_ModelWidget + virtual void activateCustom(); + virtual bool storeValueCustom() const; + + protected slots: + void onPageChanged(); + + private: + bool myIsFocusOnCurrentPage; + QStringList myCaseIds; + QList myPages; + +}; + +#endif /* MODULEBASE_PAGEDCONTAINER_H_ */ diff --git a/src/ModuleBase/ModuleBase_WidgetFactory.cpp b/src/ModuleBase/ModuleBase_WidgetFactory.cpp index 1731271aa..93c3e7271 100644 --- a/src/ModuleBase/ModuleBase_WidgetFactory.cpp +++ b/src/ModuleBase/ModuleBase_WidgetFactory.cpp @@ -103,13 +103,9 @@ void ModuleBase_WidgetFactory::createWidget(ModuleBase_PageBase* thePage) QString aCaseId = qs(myWidgetApi->getProperty(_ID)); ModuleBase_PageBase* aPage = new ModuleBase_PageWidget(aWidget); createWidget(aPage); - QWidget* aCasePageWidget = dynamic_cast(aPage); - if (aWdgType == WDG_SWITCH) { - ModuleBase_WidgetSwitch* aSwitch = qobject_cast(aWidget); - aSwitch->addPage(aCasePageWidget, aPageName); - } else if (aWdgType == WDG_TOOLBOX) { - ModuleBase_WidgetToolbox* aToolbox = qobject_cast(aWidget); - aToolbox->addPage(aPage, aPageName, aCaseId); + if (aWdgType == WDG_SWITCH || aWdgType == WDG_TOOLBOX) { + ModuleBase_PagedContainer* aContainer = qobject_cast(aWidget); + aContainer->addPage(aPage, aPageName, aCaseId); } } while (myWidgetApi->toNextWidget()); } diff --git a/src/ModuleBase/ModuleBase_WidgetSwitch.cpp b/src/ModuleBase/ModuleBase_WidgetSwitch.cpp index aec77a6b9..8cd931574 100644 --- a/src/ModuleBase/ModuleBase_WidgetSwitch.cpp +++ b/src/ModuleBase/ModuleBase_WidgetSwitch.cpp @@ -10,128 +10,65 @@ #include #include #include +#include #include -#include +#include #include - +#include +#include ModuleBase_WidgetSwitch::ModuleBase_WidgetSwitch(QWidget* theParent, const Config_WidgetAPI* theData, const std::string& theParentId) -: ModuleBase_ModelWidget(theParent, theData, theParentId) +: ModuleBase_PagedContainer(theParent, theData, theParentId) { - myMainLay = new QVBoxLayout(this); - myMainLay->setContentsMargins(2, 4, 2, 2); + QVBoxLayout* aMainLay = new QVBoxLayout(this); + //aMainLay->setContentsMargins(2, 4, 2, 2); + ModuleBase_Tools::adjustMargins(aMainLay); myCombo = new QComboBox(this); myCombo->hide(); - myMainLay->addWidget(myCombo); - //this->setFrameShape(QFrame::StyledPanel); - connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(setCurrentIndex(int))); - connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SIGNAL(currentPageChanged(int))); + myPagesLayout = new QStackedLayout(this); + aMainLay->addWidget(myCombo); + aMainLay->addLayout(myPagesLayout, 1); + setLayout(aMainLay); + connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onPageChanged())); + connect(myCombo, SIGNAL(activated(int)), myPagesLayout, SLOT(setCurrentIndex(int))); } ModuleBase_WidgetSwitch::~ModuleBase_WidgetSwitch() { } -QList ModuleBase_WidgetSwitch::getControls() const -{ - QList aList; - aList << myCombo; - return aList; -} - -int ModuleBase_WidgetSwitch::addPage(QWidget* theWidget, const QString& theName) -{ - return insertPage(count(), theWidget, theName); -} - -int ModuleBase_WidgetSwitch::count() const -{ - return myCombo->count(); -} - -int ModuleBase_WidgetSwitch::currentIndex() const -{ - return myCombo->currentIndex(); -} - -QWidget* ModuleBase_WidgetSwitch::currentWidget() const -{ - int idx = currentIndex(); - return myCases[idx]; -} - -int ModuleBase_WidgetSwitch::indexOf(QWidget* theWidget) const -{ - return myCases.indexOf(theWidget); -} -int ModuleBase_WidgetSwitch::insertPage(int theIndex, QWidget* theWidget, const QString& theName) +int ModuleBase_WidgetSwitch::addPage(ModuleBase_PageBase* thePage, const QString& theName, + const QString& theCaseId) { - int index = theIndex < count() ? theIndex : count(); - if (count() == 0) + int aSuperCount = ModuleBase_PagedContainer::addPage(thePage, theName, theCaseId); + myCombo->addItem(theName); + int aResultCount = myCombo->count(); + if (aResultCount == 1) myCombo->show(); - myCombo->insertItem(index, theName); - myCases.insert(index, theWidget); - myMainLay->addWidget(theWidget); - setCurrentIndex(theIndex); - return index; + QFrame* aFrame = dynamic_cast(thePage); + aFrame->setFrameShape(QFrame::Box); + aFrame->setFrameStyle(QFrame::Sunken); + myPagesLayout->addWidget(aFrame); + return aResultCount; } -bool ModuleBase_WidgetSwitch::isPageEnabled(int index) const +int ModuleBase_WidgetSwitch::currentPageIndex() const { - return myCases[index]->isEnabled(); + int aComboIndex = myCombo->currentIndex(); + return aComboIndex; } -QString ModuleBase_WidgetSwitch::pageText(int index) const -{ - return myCombo->itemText(index); -} - -QString ModuleBase_WidgetSwitch::pageToolTip(int index) const -{ - return myCases[index]->toolTip(); -} - -void ModuleBase_WidgetSwitch::removePage(int index) -{ - myCombo->removeItem(index); - myCases.removeAt(index); - if (count() == 0) { - myCombo->hide(); - } -} - -void ModuleBase_WidgetSwitch::setPageEnabled(int index, bool enabled) -{ - myCases[index]->setEnabled(enabled); -} - -void ModuleBase_WidgetSwitch::setPageName(int index, const QString& theName) -{ - myCombo->setItemText(index, theName); -} - -void ModuleBase_WidgetSwitch::setPageToolTip(int index, const QString& toolTip) -{ - myCases[index]->setToolTip(toolTip); -} - -void ModuleBase_WidgetSwitch::setCurrentIndex(int index) -{ - myCombo->setCurrentIndex(index); - refresh(); -} -void ModuleBase_WidgetSwitch::refresh() +void ModuleBase_WidgetSwitch::setCurrentPageIndex(int theIndex) { - foreach(QWidget* eachWidget, myCases) - { - eachWidget->setVisible(false); - } - if (currentIndex() >= myCases.count()) - return; - myCases[currentIndex()]->setVisible(true); + bool isComboSignalsBlocked = myCombo->blockSignals(true); + bool isPagesLayoutSignalsBlocked = myPagesLayout->blockSignals(true); + myCombo->setCurrentIndex(theIndex); + myPagesLayout->setCurrentIndex(theIndex); + myCombo->blockSignals(isComboSignalsBlocked); + myPagesLayout->blockSignals(isPagesLayoutSignalsBlocked); } diff --git a/src/ModuleBase/ModuleBase_WidgetSwitch.h b/src/ModuleBase/ModuleBase_WidgetSwitch.h index 1f10c2eea..5772a3d08 100644 --- a/src/ModuleBase/ModuleBase_WidgetSwitch.h +++ b/src/ModuleBase/ModuleBase_WidgetSwitch.h @@ -7,20 +7,20 @@ * Author: sbh */ -#ifndef ModuleBase_WidgetSwitch_H_ -#define ModuleBase_WidgetSwitch_H_ +#ifndef MODULEBASE_WIDGETSWITCH_H_ +#define MODULEBASE_WIDGETSWITCH_H_ #include -#include +#include class QComboBox; -class QVBoxLayout; +class QStackedLayout; /** * \ingroup GUI * Implements a model widget for swithch as a container widget. It can be defined in XML with "switch" keyword */ -class MODULEBASE_EXPORT ModuleBase_WidgetSwitch : public ModuleBase_ModelWidget +class MODULEBASE_EXPORT ModuleBase_WidgetSwitch : public ModuleBase_PagedContainer { Q_OBJECT public: @@ -31,96 +31,22 @@ class MODULEBASE_EXPORT ModuleBase_WidgetSwitch : public ModuleBase_ModelWidget const std::string& theParentId); virtual ~ModuleBase_WidgetSwitch(); - virtual bool restoreValue() { - return false; - } - - virtual QList getControls() const; - - virtual bool focusTo() { - return false; - } - /// Add a page to the widget /// \param theWidget a page widget /// \param theName a name of page - int addPage(QWidget* theWidget, const QString & theName); - - /// Returns count of pages - int count() const; - - /// Returns index of current page - int currentIndex() const; - - /// Returns current widget (page) - QWidget * currentWidget() const; - - /// Returns index of widget (page) - /// \param theWidget a widget page - int indexOf(QWidget * theWidget) const; - - /// Insert page - /// \param index an index (position) to insert - /// \param theWidget a page widget - /// \param theName a name of the page - int insertPage(int index, QWidget * theWidget, const QString & theName); - - /// Returns True if a page by given index is enabled - /// \param index index of the page - bool isPageEnabled(int index) const; - - /// Returns text of the page by its id - /// \param index index of the page - QString pageText(int index) const; - - /// Returns tooltip of the page by its id - /// \param index index of the page - QString pageToolTip(int index) const; + virtual int addPage(ModuleBase_PageBase* theWidget, + const QString& theName, const QString& theCaseId); - /// Remove page by its id - /// \param index index of the page - void removePage(int index); - - /// Enale/disable a page by its Id - /// \param index index of the page - /// \param enabled an enable flag - void setPageEnabled(int index, bool enabled); - - /// Set page name - /// \param index index of the page - /// \param text a name of the page - void setPageName(int index, const QString & text); - - /// Set page tooltip - /// \param index index of the page - /// \param toolTip a tooltip of the page - void setPageToolTip(int index, const QString & toolTip); - - public slots: + protected: + virtual int currentPageIndex() const; /// Set current page by index /// \param index index of the page - void setCurrentIndex(int index); - -signals: - /// Emitted on current page change - void currentPageChanged(int); - - protected: - virtual bool storeValueCustom() const { - return false; - } - /// Update widget - void refresh(); + virtual void setCurrentPageIndex(int index); private: - /// Layout - QVBoxLayout* myMainLay; - /// Combo box QComboBox* myCombo; - - /// List of pages - QWidgetList myCases; + QStackedLayout* myPagesLayout; }; #endif /* ModuleBase_WidgetSwitch_H_ */ diff --git a/src/ModuleBase/ModuleBase_WidgetToolbox.cpp b/src/ModuleBase/ModuleBase_WidgetToolbox.cpp index c2e816e8d..6a16aa3d2 100644 --- a/src/ModuleBase/ModuleBase_WidgetToolbox.cpp +++ b/src/ModuleBase/ModuleBase_WidgetToolbox.cpp @@ -13,13 +13,12 @@ #include #include -#include +#include #include ModuleBase_WidgetToolbox::ModuleBase_WidgetToolbox(QWidget* theParent, const Config_WidgetAPI* theData, const std::string& theParentId) -: ModuleBase_ModelWidget(theParent, theData, theParentId), - myIsPassFocusToCurrentPage(false) +: ModuleBase_PagedContainer(theParent, theData, theParentId) { QVBoxLayout* aMainLayout = new QVBoxLayout(this); ModuleBase_Tools::zeroMargins(aMainLayout); @@ -44,81 +43,24 @@ ModuleBase_WidgetToolbox::~ModuleBase_WidgetToolbox() int ModuleBase_WidgetToolbox::addPage(ModuleBase_PageBase* thePage, const QString& theName, const QString& theCaseId) { - myCaseIds << theCaseId; - myPages << thePage; + ModuleBase_PagedContainer::addPage(thePage, theName, theCaseId); QFrame* aFrame = dynamic_cast(thePage); aFrame->setFrameShape(QFrame::Box); aFrame->setFrameStyle(QFrame::Sunken); - return myToolBox->addItem(aFrame, theName); + myToolBox->addItem(aFrame, theName); + return myToolBox->count(); } -bool ModuleBase_WidgetToolbox::restoreValue() +int ModuleBase_WidgetToolbox::currentPageIndex() const { - // A rare case when plugin was not loaded. - if(!myFeature) - return false; - DataPtr aData = myFeature->data(); - AttributeStringPtr aStringAttr = aData->string(attributeID()); - QString aCaseId = QString::fromStdString(aStringAttr->value()); - int idx = myCaseIds.indexOf(aCaseId); - if (idx == -1) - return false; - bool isSignalsBlocked = myToolBox->blockSignals(true); - myToolBox->setCurrentIndex(idx); - myToolBox->blockSignals(isSignalsBlocked); - return true; -} - -QList ModuleBase_WidgetToolbox::getControls() const -{ - QList aResult; - int idx = myToolBox->currentIndex(); - QList aModelWidgets = myPages[idx]->modelWidgets(); - foreach(ModuleBase_ModelWidget* eachModelWidget, aModelWidgets) { - aResult << eachModelWidget->getControls(); - } - return aResult; -} - -bool ModuleBase_WidgetToolbox::focusTo() -{ - int idx = myToolBox->currentIndex(); - if (idx > myPages.count()) - return false; - return myPages[idx]->takeFocus(); -} - -void ModuleBase_WidgetToolbox::setHighlighted(bool) -{ - //page containers sould not be highlighted, do nothing + return myToolBox->currentIndex(); } -void ModuleBase_WidgetToolbox::enableFocusProcessing() +void ModuleBase_WidgetToolbox::setCurrentPageIndex(int theIndex) { - myIsPassFocusToCurrentPage = true; -} - - -void ModuleBase_WidgetToolbox::activateCustom() -{ - // activate current page - focusTo(); + bool isSignalsBlocked = myToolBox->blockSignals(true); + myToolBox->setCurrentIndex(theIndex); + myToolBox->blockSignals(isSignalsBlocked); } -bool ModuleBase_WidgetToolbox::storeValueCustom() const -{ - // A rare case when plugin was not loaded. - if(!myFeature) - return false; - DataPtr aData = myFeature->data(); - AttributeStringPtr aStringAttr = aData->string(attributeID()); - QString aWidgetValue = myCaseIds.at(myToolBox->currentIndex()); - aStringAttr->setValue(aWidgetValue.toStdString()); - return true; -} -void ModuleBase_WidgetToolbox::onPageChanged() -{ - storeValue(); - if (myIsPassFocusToCurrentPage) focusTo(); -} diff --git a/src/ModuleBase/ModuleBase_WidgetToolbox.h b/src/ModuleBase/ModuleBase_WidgetToolbox.h index 21ab5a2c2..639e90f14 100644 --- a/src/ModuleBase/ModuleBase_WidgetToolbox.h +++ b/src/ModuleBase/ModuleBase_WidgetToolbox.h @@ -8,41 +8,32 @@ #ifndef MODULEBASE_WIDGETTOOLBOX_H_ #define MODULEBASE_WIDGETTOOLBOX_H_ -#include +#include +#include #include class ModuleBase_PageBase; -class ModuleBase_WidgetToolbox : public ModuleBase_ModelWidget +class MODULEBASE_EXPORT ModuleBase_WidgetToolbox : public ModuleBase_PagedContainer { Q_OBJECT public: ModuleBase_WidgetToolbox(QWidget* theParent, const Config_WidgetAPI* theData, const std::string& theParentId); virtual ~ModuleBase_WidgetToolbox(); - - virtual bool restoreValue(); - virtual QList getControls() const; - virtual bool focusTo(); - virtual void setHighlighted(bool isHighlighted); - virtual void enableFocusProcessing(); - + /// Overrides ModuleBase_PagedContainer int addPage(ModuleBase_PageBase* theWidget, const QString& theName, const QString& theCaseId); protected: - virtual void activateCustom(); - virtual bool storeValueCustom() const; - - protected slots: - void onPageChanged(); + /// Implements ModuleBase_PagedContainer + virtual int currentPageIndex() const; + /// Implements ModuleBase_PagedContainer + virtual void setCurrentPageIndex(int); private: - bool myIsPassFocusToCurrentPage; QToolBox* myToolBox; - QStringList myCaseIds; - QList myPages; }; #endif /* MODULEBASE_WIDGETTOOLBOX_H_ */ -- 2.39.2