From d1a7a0014b216fd874406799268841ec8be01624 Mon Sep 17 00:00:00 2001 From: Etienne Tourigny Date: Mon, 10 Sep 2012 18:16:14 -0300 Subject: [PATCH] add save/load state and titleRect() --- src/gui/qgscollapsiblegroupbox.cpp | 84 +++++++++++++++++++++++++----- src/gui/qgscollapsiblegroupbox.h | 13 +++-- 2 files changed, 79 insertions(+), 18 deletions(-) diff --git a/src/gui/qgscollapsiblegroupbox.cpp b/src/gui/qgscollapsiblegroupbox.cpp index b3f164965bc3..8c5f88b5e43d 100644 --- a/src/gui/qgscollapsiblegroupbox.cpp +++ b/src/gui/qgscollapsiblegroupbox.cpp @@ -23,23 +23,29 @@ #include #include #include +#include QIcon QgsCollapsibleGroupBox::mCollapseIcon; QIcon QgsCollapsibleGroupBox::mExpandIcon; QgsCollapsibleGroupBox::QgsCollapsibleGroupBox( QWidget *parent ) - : QGroupBox( parent ), mCollapsed( false ) + : QGroupBox( parent ), mCollapsed( false ), mSaveState( true ) { init(); } QgsCollapsibleGroupBox::QgsCollapsibleGroupBox( const QString &title, QWidget *parent ) - : QGroupBox( title, parent ), mCollapsed( false ) + : QGroupBox( title, parent ), mCollapsed( false ), mSaveState( true ) { init(); } +QgsCollapsibleGroupBox::~QgsCollapsibleGroupBox() +{ + saveState(); +} + void QgsCollapsibleGroupBox::init() { // init icons @@ -64,7 +70,7 @@ void QgsCollapsibleGroupBox::init() void QgsCollapsibleGroupBox::showEvent( QShowEvent * event ) { - QGroupBox::showEvent( event ); + loadState(); updateStyle(); @@ -79,14 +85,15 @@ void QgsCollapsibleGroupBox::showEvent( QShowEvent * event ) still emit signal for connections using expanded state */ emit collapsedStateChanged( this ); } + event->accept(); } -void QgsCollapsibleGroupBox::mouseReleaseEvent( QMouseEvent *event ) +void QgsCollapsibleGroupBox::mouseReleaseEvent( QMouseEvent *event ) { // catch mouse release over title when non checkable, to collapse/expand - if ( !isCheckable() && event->button() == Qt::LeftButton ) + if ( !isCheckable() && event->button() == Qt::LeftButton ) { - if ( mTitleRect.contains( event->pos() ) ) + if ( titleRect().contains( event->pos() ) ) { toggleCollapsed(); return; @@ -96,6 +103,62 @@ void QgsCollapsibleGroupBox::mouseReleaseEvent( QMouseEvent *event ) QGroupBox::mouseReleaseEvent( event ); } +QRect QgsCollapsibleGroupBox::titleRect() const +{ + QStyleOptionGroupBox box; + initStyleOption( &box ); + return style()->subControlRect( QStyle::CC_GroupBox, &box, + QStyle::SC_GroupBoxLabel, this ); +} + +QString QgsCollapsibleGroupBox::saveKey() const +{ + // save key for load/save state + // currently QgsCollapsibleGroupBox/window()/object + QString saveKey = "/" + objectName(); + // QObject* parentWidget = parent(); + // while ( parentWidget != NULL ) + // { + // saveKey = "/" + parentWidget->objectName() + saveKey; + // parentWidget = parentWidget->parent(); + // } + // if ( parent() != NULL ) + // saveKey = "/" + parent()->objectName() + saveKey; + saveKey = "/" + window()->objectName() + saveKey; + saveKey = "QgsCollapsibleGroupBox" + saveKey; + return saveKey; +} + +void QgsCollapsibleGroupBox::loadState() +{ + if ( ! mSaveState ) + return; + + setUpdatesEnabled( false ); + + QSettings settings; + QString key = saveKey(); + QVariant val = settings.value( key + "/checked" ); + if ( ! val.isNull() ) + setChecked( val.toBool() ); + val = settings.value( key + "/collapsed" ); + if ( ! val.isNull() ) + setCollapsed( val.toBool() ); + + setUpdatesEnabled( true ); +} + +void QgsCollapsibleGroupBox::saveState() +{ + if ( ! mSaveState ) + return; + QgsDebugMsg( "key = " + saveKey() + " objectName = " + objectName() ); + QSettings settings; + QString key = saveKey(); + settings.setValue( key + "/checked", isChecked() ); + settings.setValue( key + "/collapsed", isCollapsed() ); +} + void QgsCollapsibleGroupBox::checkToggled( bool chkd ) { mCollapseButton->setEnabled( true ); // always keep enabled @@ -135,13 +198,6 @@ void QgsCollapsibleGroupBox::updateStyle() mCollapseButton->setStyleSheet( ssd ); setUpdatesEnabled( true ); - - // init title rect for later use - should not change during execution - // if it needs updating (e.g. in options dlg), call slot when style changes - QStyleOptionGroupBox box; - initStyleOption( &box ); - mTitleRect = style()->subControlRect( QStyle::CC_GroupBox, &box, - QStyle::SC_GroupBoxLabel, this ); } void QgsCollapsibleGroupBox::setCollapsed( bool collapse ) @@ -157,7 +213,7 @@ void QgsCollapsibleGroupBox::setCollapsed( bool collapse ) QApplication::processEvents(); // set maximum height to hide contents - does this work in all envs? // setMaximumHeight( collapse ? 25 : 16777215 ); - setMaximumHeight( collapse ? mTitleRect.height() + 2 : 16777215 ); + setMaximumHeight( collapse ? titleRect().bottom() + 2 : 16777215 ); mCollapseButton->setIcon( collapse ? mExpandIcon : mCollapseIcon ); emit collapsedStateChanged( this ); diff --git a/src/gui/qgscollapsiblegroupbox.h b/src/gui/qgscollapsiblegroupbox.h index 910d1f1540ef..a63d0a5acb67 100644 --- a/src/gui/qgscollapsiblegroupbox.h +++ b/src/gui/qgscollapsiblegroupbox.h @@ -36,9 +36,10 @@ class GUI_EXPORT QgsCollapsibleGroupBox : public QGroupBox public: QgsCollapsibleGroupBox( QWidget *parent = 0 ); QgsCollapsibleGroupBox( const QString &title, QWidget *parent = 0 ); - + ~QgsCollapsibleGroupBox(); bool isCollapsed() const { return mCollapsed; } void setCollapsed( bool collapse ); + void setSaveState( bool save ) { mSaveState = save; } signals: void collapsedStateChanged( QWidget* ); @@ -48,16 +49,20 @@ class GUI_EXPORT QgsCollapsibleGroupBox : public QGroupBox void toggleCollapsed(); void updateStyle(); + protected slots: + void loadState(); + void saveState(); + protected: void init(); void showEvent( QShowEvent *event ); void mouseReleaseEvent( QMouseEvent *event ); + QRect titleRect() const; + QString saveKey() const; - private: bool mCollapsed; - QList< QWidget* > mHiddenWidgets; + bool mSaveState; QToolButton* mCollapseButton; - QRect mTitleRect; static QIcon mCollapseIcon; static QIcon mExpandIcon;