From 5982960d3b8b17e5ab2f795e49f9daa00383e49c Mon Sep 17 00:00:00 2001 From: nds Date: Tue, 12 Jul 2016 10:46:19 +0300 Subject: [PATCH] #1658 New widget supporting nested option buttons --- src/ModuleBase/ModuleBase_ToolBox.cpp | 67 ++++++++++++--------- src/ModuleBase/ModuleBase_ToolBox.h | 12 ++-- src/ModuleBase/ModuleBase_WidgetToolbox.cpp | 11 +++- 3 files changed, 53 insertions(+), 37 deletions(-) diff --git a/src/ModuleBase/ModuleBase_ToolBox.cpp b/src/ModuleBase/ModuleBase_ToolBox.cpp index d1b52ef89..ee7630c96 100644 --- a/src/ModuleBase/ModuleBase_ToolBox.cpp +++ b/src/ModuleBase/ModuleBase_ToolBox.cpp @@ -13,50 +13,57 @@ #include #include -ModuleBase_ToolBox::ModuleBase_ToolBox( QWidget* theParent ) - : QFrame( theParent ) +#include + +ModuleBase_ToolBox::ModuleBase_ToolBox(QWidget* theParent, const bool theUseFrameStyleBox) +: QFrame(theParent) { - QVBoxLayout* aMainLayout = new QVBoxLayout( this ); + QVBoxLayout* aMainLayout = new QVBoxLayout(this); aMainLayout->setMargin(0); aMainLayout->setSpacing(2); - myButtonsFrame = new QFrame( this ); + if (theUseFrameStyleBox) { + setFrameStyle(QFrame::Box | QFrame::Raised); + aMainLayout->setMargin(2); + } + + myButtonsFrame = new QFrame(this); - myStack = new QStackedWidget( this ); + myStack = new QStackedWidget(this); - aMainLayout->addWidget( myButtonsFrame, 0 ); - aMainLayout->addWidget( myStack, 1 ); + aMainLayout->addWidget(myButtonsFrame, 0); + aMainLayout->addWidget(myStack, 1); myButtonsGroup = new QButtonGroup(this); - myButtonsGroup->setExclusive( true ); - myButtonsLayout = new QHBoxLayout( myButtonsFrame ); - myButtonsLayout->setMargin( 0 ); - myButtonsLayout->setSpacing( 5 ); - myButtonsLayout->addStretch( 1 ); - - connect( myStack, SIGNAL( currentChanged( int ) ), this, SIGNAL( currentChanged( int ) ) ); - connect( myButtonsGroup, SIGNAL( buttonPressed( int ) ), this, SLOT( onButton( int ) ) ); + myButtonsGroup->setExclusive(true); + myButtonsLayout = new QHBoxLayout(myButtonsFrame); + myButtonsLayout->setMargin(0); + myButtonsLayout->setSpacing(5); + myButtonsLayout->addStretch(1); + + connect(myStack, SIGNAL(currentChanged(int)), this, SIGNAL(currentChanged(int))); + connect(myButtonsGroup, SIGNAL(buttonPressed(int)), this, SLOT(onButton(int))); } ModuleBase_ToolBox::~ModuleBase_ToolBox() { } -void ModuleBase_ToolBox::addItem( QWidget* thePage, const QString& theName, const QPixmap& theIcon ) +void ModuleBase_ToolBox::addItem(QWidget* thePage, const QString& theName, const QPixmap& theIcon) { int anOldCount = myStack->count(); - myStack->addWidget( thePage ); + myStack->addWidget(thePage); - QToolButton* aButton = new QToolButton( myButtonsFrame ); + QToolButton* aButton = new QToolButton(myButtonsFrame); aButton->setFocusPolicy(Qt::StrongFocus); - aButton->setCheckable( true ); - aButton->setIcon( theIcon ); - aButton->setIconSize( theIcon.size() ); - aButton->setToolTip( theName ); - aButton->setObjectName( theName ); - myButtonsGroup->addButton( aButton, anOldCount ); - myButtonsLayout->insertWidget( anOldCount, aButton ); + aButton->setCheckable(true); + aButton->setIcon(theIcon); + aButton->setIconSize(theIcon.size()); + aButton->setToolTip(theName); + aButton->setObjectName(theName); + myButtonsGroup->addButton(aButton, anOldCount); + myButtonsLayout->insertWidget(anOldCount, aButton); } int ModuleBase_ToolBox::count() const @@ -69,15 +76,15 @@ int ModuleBase_ToolBox::currentIndex() const return myStack->currentIndex(); } -void ModuleBase_ToolBox::setCurrentIndex( const int theIndex ) +void ModuleBase_ToolBox::setCurrentIndex(const int theIndex) { - myStack->setCurrentIndex( theIndex ); - myButtonsGroup->button( theIndex )->setChecked( true ); + myStack->setCurrentIndex(theIndex); + myButtonsGroup->button(theIndex)->setChecked(true); } -void ModuleBase_ToolBox::onButton( int theIndex ) +void ModuleBase_ToolBox::onButton(int theIndex) { - myStack->setCurrentIndex( theIndex ); + myStack->setCurrentIndex(theIndex); } bool ModuleBase_ToolBox::isOffToolBoxParent(ModuleBase_ModelWidget* theWidget) diff --git a/src/ModuleBase/ModuleBase_ToolBox.h b/src/ModuleBase/ModuleBase_ToolBox.h index cab3a788d..280f5d7f9 100644 --- a/src/ModuleBase/ModuleBase_ToolBox.h +++ b/src/ModuleBase/ModuleBase_ToolBox.h @@ -28,14 +28,16 @@ class MODULEBASE_EXPORT ModuleBase_ToolBox : public QFrame public: /// Constructor /// \param theParent a parent widget - ModuleBase_ToolBox( QWidget* theParent ); + /// \param theUseFrameStyleBox a flag if the tool box should have box covered + /// buttons and current page + ModuleBase_ToolBox(QWidget* theParent, const bool theUseFrameStyleBox = false); virtual ~ModuleBase_ToolBox(); /// Add a new item to the tool box /// \param thePage a widget of the new item /// \param theName a name of the item /// \param theIcon an icon of the item - void addItem( QWidget* thePage, const QString& theName, const QPixmap& theIcon ); + void addItem(QWidget* thePage, const QString& theName, const QPixmap& theIcon); /// \return number of items int count() const; @@ -45,7 +47,7 @@ public: /// Set current item /// \param theIdx an index - void setCurrentIndex( const int theIdx); + void setCurrentIndex(const int theIdx); /// Found in the controls of the model widget parent in Stacked Widget /// returns whether this controls are in the current widget of the stacked widgets @@ -55,11 +57,11 @@ public: signals: /// A signal which is emited on current item changed - void currentChanged( int ); + void currentChanged(int); private slots: /// A slot called on button press - void onButton( int ); + void onButton(int); private: QButtonGroup* myButtonsGroup; diff --git a/src/ModuleBase/ModuleBase_WidgetToolbox.cpp b/src/ModuleBase/ModuleBase_WidgetToolbox.cpp index 05759642d..48a44df86 100644 --- a/src/ModuleBase/ModuleBase_WidgetToolbox.cpp +++ b/src/ModuleBase/ModuleBase_WidgetToolbox.cpp @@ -23,7 +23,15 @@ ModuleBase_WidgetToolbox::ModuleBase_WidgetToolbox(QWidget* theParent, const Con { QVBoxLayout* aMainLayout = new QVBoxLayout(this); ModuleBase_Tools::zeroMargins(aMainLayout); - myToolBox = new ModuleBase_ToolBox(this); + + bool aHasContainerParent = false; + QWidget* aParent = dynamic_cast(parent()); + while(aParent && !aHasContainerParent) { + ModuleBase_PagedContainer* aPagedContainer = dynamic_cast(aParent); + aHasContainerParent = aPagedContainer; + aParent = dynamic_cast(aParent->parent()); + } + myToolBox = new ModuleBase_ToolBox(this, aHasContainerParent); // Dark-grey rounded tabs with button-like border #and bold font // TODO: apply style to custom widget QString css = "QToolBox::tab{background-color:#c8c8c8;" @@ -49,7 +57,6 @@ int ModuleBase_WidgetToolbox::addPage(ModuleBase_PageBase* thePage, { ModuleBase_PagedContainer::addPage(thePage, theName, theCaseId, theIcon); QFrame* aFrame = dynamic_cast(thePage); - aFrame->setFrameStyle(QFrame::Box | QFrame::Raised); myToolBox->addItem(aFrame, theName, theIcon ); return myToolBox->count(); } -- 2.39.2