Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add save/load state and titleRect()
  • Loading branch information
etiennesky authored and dakcarto committed Sep 11, 2012
1 parent 234db45 commit d1a7a00
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 18 deletions.
84 changes: 70 additions & 14 deletions src/gui/qgscollapsiblegroupbox.cpp
Expand Up @@ -23,23 +23,29 @@
#include <QToolButton>
#include <QMouseEvent>
#include <QStyleOptionGroupBox>
#include <QSettings>

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
Expand All @@ -64,7 +70,7 @@ void QgsCollapsibleGroupBox::init()

void QgsCollapsibleGroupBox::showEvent( QShowEvent * event )
{
QGroupBox::showEvent( event );
loadState();

updateStyle();

Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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 )
Expand All @@ -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 );
Expand Down
13 changes: 9 additions & 4 deletions src/gui/qgscollapsiblegroupbox.h
Expand Up @@ -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* );
Expand All @@ -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;
Expand Down

0 comments on commit d1a7a00

Please sign in to comment.