Skip to content

Commit

Permalink
allow to use custom QSettings for managing QgsOptionsDialog parameters.
Browse files Browse the repository at this point in the history
Useful for plugins and custom apps
  • Loading branch information
alexbruy committed Mar 2, 2014
1 parent b1dd6d3 commit d378eef
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
3 changes: 3 additions & 0 deletions python/gui/qgsoptionsdialogbase.sip
Expand Up @@ -18,6 +18,9 @@ class QgsOptionsDialogBase : QDialog
*/
void initOptionsBase( bool restoreUi = true, QString title = QString() );

// set custom QSettings pointer if dialog used outside QGIS (in plugin)
void setSettings( QSettings* settings );

/** Restore the base ui.
* Sometimes useful to do at end of subclass's constructor.
* @param title the window title (it does not need to be defined if previously given to initOptionsBase();
Expand Down
51 changes: 38 additions & 13 deletions src/gui/qgsoptionsdialogbase.cpp
Expand Up @@ -22,33 +22,49 @@
#include <QListWidget>
#include <QMessageBox>
#include <QScrollBar>
#include <QSettings>
#include <QStackedWidget>
#include <QSplitter>
#include <QTimer>


QgsOptionsDialogBase::QgsOptionsDialogBase( QString settingsKey, QWidget* parent, Qt::WFlags fl )
QgsOptionsDialogBase::QgsOptionsDialogBase( QString settingsKey, QWidget* parent, Qt::WFlags fl, QSettings* settings )
: QDialog( parent, fl )
, mOptsKey( settingsKey )
, mInit( false )
, mDialogTitle( "" )
, mSettings( settings )
{
}

QgsOptionsDialogBase::~QgsOptionsDialogBase()
{
if ( mInit )
{
QSettings settings;
settings.setValue( QString( "/Windows/%1/geometry" ).arg( mOptsKey ), saveGeometry() );
settings.setValue( QString( "/Windows/%1/splitState" ).arg( mOptsKey ), mOptSplitter->saveState() );
settings.setValue( QString( "/Windows/%1/tab" ).arg( mOptsKey ), mOptStackedWidget->currentIndex() );
mSettings->setValue( QString( "/Windows/%1/geometry" ).arg( mOptsKey ), saveGeometry() );
mSettings->setValue( QString( "/Windows/%1/splitState" ).arg( mOptsKey ), mOptSplitter->saveState() );
mSettings->setValue( QString( "/Windows/%1/tab" ).arg( mOptsKey ), mOptStackedWidget->currentIndex() );
}

if ( mDelSettings ) // local settings obj to delete
{
delete mSettings;
}

mSettings = 0; // null the pointer (in case of outside settings obj)
}

void QgsOptionsDialogBase::initOptionsBase( bool restoreUi, QString title )
{
// 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
}

// save dialog title so it can be used to be concatenated
// with category title in icon-only mode
if ( title.isEmpty() )
Expand Down Expand Up @@ -76,8 +92,7 @@ void QgsOptionsDialogBase::initOptionsBase( bool restoreUi, QString title )
return;
}

QSettings settings;
int size = settings.value( "/IconSize", 24 ).toInt();
int size = mSettings->value( "/IconSize", 24 ).toInt();
// buffer size to match displayed icon size in toolbars, and expected geometry restore
// newWidth (above) may need adjusted if you adjust iconBuffer here
int iconBuffer = 4;
Expand Down Expand Up @@ -115,6 +130,17 @@ void QgsOptionsDialogBase::initOptionsBase( bool restoreUi, QString title )
restoreOptionsBaseUi( mDialogTitle );
}

void QgsOptionsDialogBase::setSettings( QSettings* settings )
{
if ( mDelSettings ) // local settings obj to delete
{
delete mSettings;
}

mSettings = settings;
mDelSettings = false; // don't delete outside obj
}

void QgsOptionsDialogBase::restoreOptionsBaseUi( QString title )
{
if ( !mInit )
Expand All @@ -131,14 +157,13 @@ void QgsOptionsDialogBase::restoreOptionsBaseUi( QString title )
// re-save original dialog title in case it was changed after dialog initialization
mDialogTitle = windowTitle();

QSettings settings;
restoreGeometry( settings.value( QString( "/Windows/%1/geometry" ).arg( mOptsKey ) ).toByteArray() );
restoreGeometry( mSettings->value( QString( "/Windows/%1/geometry" ).arg( mOptsKey ) ).toByteArray() );
// mOptListWidget width is fixed to take up less space in QtDesigner
// revert it now unless the splitter's state hasn't been saved yet
mOptListWidget->setMaximumWidth(
settings.value( QString( "/Windows/%1/splitState" ).arg( mOptsKey ) ).isNull() ? 150 : 16777215 );
mOptSplitter->restoreState( settings.value( QString( "/Windows/%1/splitState" ).arg( mOptsKey ) ).toByteArray() );
int curIndx = settings.value( QString( "/Windows/%1/tab" ).arg( mOptsKey ), 0 ).toInt();
mSettings->value( QString( "/Windows/%1/splitState" ).arg( mOptsKey ) ).isNull() ? 150 : 16777215 );
mOptSplitter->restoreState( mSettings->value( QString( "/Windows/%1/splitState" ).arg( mOptsKey ) ).toByteArray() );
int curIndx = mSettings->value( QString( "/Windows/%1/tab" ).arg( mOptsKey ), 0 ).toInt();

// if the last used tab is out of range or not enabled display the first enabled one
if ( mOptStackedWidget->count() < ( curIndx + 1 )
Expand Down
12 changes: 11 additions & 1 deletion src/gui/qgsoptionsdialogbase.h
Expand Up @@ -20,6 +20,8 @@
#include "qgisgui.h"

#include <QDialog>
#include <QPointer>
#include <QSettings>

class QDialogButtonBox;
class QListWidget;
Expand Down Expand Up @@ -53,8 +55,9 @@ class GUI_EXPORT QgsOptionsDialogBase : public QDialog
* @param settingsKey QSettings subgroup key for saving/restore ui states, e.g. "ProjectProperties".
* @param parent parent object (owner)
* @param fl widget flags
* @param settings custom QSettings pointer
*/
QgsOptionsDialogBase( QString settingsKey, QWidget* parent = 0, Qt::WFlags fl = 0 );
QgsOptionsDialogBase( QString settingsKey, QWidget* parent = 0, Qt::WFlags fl = 0, QSettings* settings = 0 );
~QgsOptionsDialogBase();

/** Set up the base ui connections for vertical tabs.
Expand All @@ -63,6 +66,9 @@ class GUI_EXPORT QgsOptionsDialogBase : public QDialog
*/
void initOptionsBase( bool restoreUi = true, QString title = QString() );

// set custom QSettings pointer if dialog used outside QGIS (in plugin)
void setSettings( QSettings* settings );

/** Restore the base ui.
* Sometimes useful to do at end of subclass's constructor.
* @param title the window title (it does not need to be defined if previously given to initOptionsBase();
Expand Down Expand Up @@ -93,6 +99,10 @@ class GUI_EXPORT QgsOptionsDialogBase : public QDialog
QDialogButtonBox* mOptButtonBox;
QString mDialogTitle;
bool mIconOnly;
// pointer to app or custom, external QSettings
// QPointer in case custom settings obj gets deleted while dialog is open
QPointer<QSettings> mSettings;
bool mDelSettings;
};

#endif // QGSOPTIONSDIALOGBASE_H

0 comments on commit d378eef

Please sign in to comment.