Skip to content
Permalink
Browse files
Add option to use custom QSettings for QgsCollapsibleGroupBox
- Useful for plugins that may want to store collapsed state in their own QSettings file
  • Loading branch information
dakcarto committed Feb 3, 2013
1 parent 8e7db45 commit 1095f9817316634a8447041198b3546ceb21b734
Showing with 51 additions and 15 deletions.
  1. +5 −2 python/gui/qgscollapsiblegroupbox.sip
  2. +34 −11 src/gui/qgscollapsiblegroupbox.cpp
  3. +12 −2 src/gui/qgscollapsiblegroupbox.h
@@ -4,10 +4,13 @@ class QgsCollapsibleGroupBox : QGroupBox
#include <qgscollapsiblegroupbox.h>
%End
public:
QgsCollapsibleGroupBox( QWidget *parent = 0 );
QgsCollapsibleGroupBox( const QString &title, QWidget *parent = 0 );
QgsCollapsibleGroupBox( QWidget *parent = 0, QSettings* settings = 0 );
QgsCollapsibleGroupBox( const QString &title, QWidget *parent = 0, QSettings* settings = 0 );
~QgsCollapsibleGroupBox();

// set custom QSettings pointer if group box was already created from QtDesigner promotion
void setSettings( QSettings* settings );

bool isCollapsed() const;
void setCollapsed( bool collapse );

@@ -29,26 +29,46 @@
QIcon QgsCollapsibleGroupBox::mCollapseIcon;
QIcon QgsCollapsibleGroupBox::mExpandIcon;

QgsCollapsibleGroupBox::QgsCollapsibleGroupBox( QWidget *parent )
: QGroupBox( parent )
QgsCollapsibleGroupBox::QgsCollapsibleGroupBox( QWidget *parent, QSettings* settings )
: QGroupBox( parent ), mSettings( settings )
{
init();
}

QgsCollapsibleGroupBox::QgsCollapsibleGroupBox( const QString &title,
QWidget *parent )
: QGroupBox( title, parent )
QWidget *parent, QSettings* settings )
: QGroupBox( title, parent ), mSettings( settings )
{
init();
}

QgsCollapsibleGroupBox::~QgsCollapsibleGroupBox()
{
saveState();
if ( mDelSettings ) // local settings obj to delete
delete mSettings;
mSettings = 0; // null the pointer (in case of outside settings obj)
}


void QgsCollapsibleGroupBox::setSettings( QSettings* settings )
{
if ( mDelSettings ) // local settings obj to delete
delete mSettings;
mSettings = settings;
mDelSettings = false; // don't delete outside obj
}

void QgsCollapsibleGroupBox::init()
{
// use pointer to app qsettings if no custom qsettings specified
// custom qsettings object may be from Python plugin
mDelSettings = false;
if ( !mSettings )
{
mSettings = new QSettings();
mDelSettings = true; // only delete obj created by class
}
// variables
mCollapsed = false;
mSaveCollapsedState = true;
@@ -183,23 +203,25 @@ QString QgsCollapsibleGroupBox::saveKey() const

void QgsCollapsibleGroupBox::loadState()
{
if ( !mSettings )
return;

if ( !isEnabled() || ( !mSaveCollapsedState && !mSaveCheckedState ) )
return;

setUpdatesEnabled( false );

QSettings settings;
QString key = saveKey();
QVariant val;
if ( mSaveCheckedState )
{
val = settings.value( key + "/checked" );
val = mSettings->value( key + "/checked" );
if ( ! val.isNull() )
setChecked( val.toBool() );
}
if ( mSaveCollapsedState )
{
val = settings.value( key + "/collapsed" );
val = mSettings->value( key + "/collapsed" );
if ( ! val.isNull() )
setCollapsed( val.toBool() );
}
@@ -209,16 +231,18 @@ void QgsCollapsibleGroupBox::loadState()

void QgsCollapsibleGroupBox::saveState()
{
if ( !mSettings )
return;

if ( !isEnabled() || ( !mSaveCollapsedState && !mSaveCheckedState ) )
return;

QSettings settings;
QString key = saveKey();

if ( mSaveCheckedState )
settings.setValue( key + "/checked", isChecked() );
mSettings->setValue( key + "/checked", isChecked() );
if ( mSaveCollapsedState )
settings.setValue( key + "/collapsed", isCollapsed() );
mSettings->setValue( key + "/collapsed", isCollapsed() );
}

void QgsCollapsibleGroupBox::checkToggled( bool chkd )
@@ -340,4 +364,3 @@ void QgsCollapsibleGroupBox::setCollapsed( bool collapse )
}
emit collapsedStateChanged( this );
}

@@ -26,6 +26,8 @@
*/

#include <QGroupBox>
#include <QSettings>
#include <QPointer>

class QToolButton;
class QScrollArea;
@@ -35,10 +37,13 @@ class GUI_EXPORT QgsCollapsibleGroupBox : public QGroupBox
Q_OBJECT

public:
QgsCollapsibleGroupBox( QWidget *parent = 0 );
QgsCollapsibleGroupBox( const QString &title, QWidget *parent = 0 );
QgsCollapsibleGroupBox( QWidget *parent = 0, QSettings* settings = 0 );
QgsCollapsibleGroupBox( const QString &title, QWidget *parent = 0, QSettings* settings = 0 );
~QgsCollapsibleGroupBox();

// set custom QSettings pointer if group box was already created from QtDesigner promotion
void setSettings( QSettings* settings );

bool isCollapsed() const { return mCollapsed; }
void setCollapsed( bool collapse );

@@ -80,6 +85,11 @@ class GUI_EXPORT QgsCollapsibleGroupBox : public QGroupBox
QRect titleRect() const;
QString saveKey() const;

// pointer to app or custom, external QSettings
// QPointer in case custom settings obj gets deleted while groupbox's dialog is open
QPointer<QSettings> mSettings;
bool mDelSettings;

bool mCollapsed;
bool mSaveCollapsedState;
bool mSaveCheckedState;

0 comments on commit 1095f98

Please sign in to comment.