allow to use custom QSettings for managing QgsOptionsDialog parameters.

Useful for plugins and custom apps
This commit is contained in:
Alexander Bruy 2014-03-02 11:28:45 +02:00
parent b1dd6d343d
commit d378eef0f0
3 changed files with 52 additions and 14 deletions

View File

@ -18,6 +18,9 @@ class QgsOptionsDialogBase : QDialog
*/ */
void initOptionsBase( bool restoreUi = true, QString title = QString() ); 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. /** Restore the base ui.
* Sometimes useful to do at end of subclass's constructor. * 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(); * @param title the window title (it does not need to be defined if previously given to initOptionsBase();

View File

@ -22,17 +22,17 @@
#include <QListWidget> #include <QListWidget>
#include <QMessageBox> #include <QMessageBox>
#include <QScrollBar> #include <QScrollBar>
#include <QSettings>
#include <QStackedWidget> #include <QStackedWidget>
#include <QSplitter> #include <QSplitter>
#include <QTimer> #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 ) : QDialog( parent, fl )
, mOptsKey( settingsKey ) , mOptsKey( settingsKey )
, mInit( false ) , mInit( false )
, mDialogTitle( "" ) , mDialogTitle( "" )
, mSettings( settings )
{ {
} }
@ -40,15 +40,31 @@ QgsOptionsDialogBase::~QgsOptionsDialogBase()
{ {
if ( mInit ) if ( mInit )
{ {
QSettings settings; mSettings->setValue( QString( "/Windows/%1/geometry" ).arg( mOptsKey ), saveGeometry() );
settings.setValue( QString( "/Windows/%1/geometry" ).arg( mOptsKey ), saveGeometry() ); mSettings->setValue( QString( "/Windows/%1/splitState" ).arg( mOptsKey ), mOptSplitter->saveState() );
settings.setValue( QString( "/Windows/%1/splitState" ).arg( mOptsKey ), mOptSplitter->saveState() ); mSettings->setValue( QString( "/Windows/%1/tab" ).arg( mOptsKey ), mOptStackedWidget->currentIndex() );
settings.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 ) 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 // save dialog title so it can be used to be concatenated
// with category title in icon-only mode // with category title in icon-only mode
if ( title.isEmpty() ) if ( title.isEmpty() )
@ -76,8 +92,7 @@ void QgsOptionsDialogBase::initOptionsBase( bool restoreUi, QString title )
return; return;
} }
QSettings settings; int size = mSettings->value( "/IconSize", 24 ).toInt();
int size = settings.value( "/IconSize", 24 ).toInt();
// buffer size to match displayed icon size in toolbars, and expected geometry restore // buffer size to match displayed icon size in toolbars, and expected geometry restore
// newWidth (above) may need adjusted if you adjust iconBuffer here // newWidth (above) may need adjusted if you adjust iconBuffer here
int iconBuffer = 4; int iconBuffer = 4;
@ -115,6 +130,17 @@ void QgsOptionsDialogBase::initOptionsBase( bool restoreUi, QString title )
restoreOptionsBaseUi( mDialogTitle ); 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 ) void QgsOptionsDialogBase::restoreOptionsBaseUi( QString title )
{ {
if ( !mInit ) if ( !mInit )
@ -131,14 +157,13 @@ void QgsOptionsDialogBase::restoreOptionsBaseUi( QString title )
// re-save original dialog title in case it was changed after dialog initialization // re-save original dialog title in case it was changed after dialog initialization
mDialogTitle = windowTitle(); mDialogTitle = windowTitle();
QSettings settings; restoreGeometry( mSettings->value( QString( "/Windows/%1/geometry" ).arg( mOptsKey ) ).toByteArray() );
restoreGeometry( settings.value( QString( "/Windows/%1/geometry" ).arg( mOptsKey ) ).toByteArray() );
// mOptListWidget width is fixed to take up less space in QtDesigner // mOptListWidget width is fixed to take up less space in QtDesigner
// revert it now unless the splitter's state hasn't been saved yet // revert it now unless the splitter's state hasn't been saved yet
mOptListWidget->setMaximumWidth( mOptListWidget->setMaximumWidth(
settings.value( QString( "/Windows/%1/splitState" ).arg( mOptsKey ) ).isNull() ? 150 : 16777215 ); mSettings->value( QString( "/Windows/%1/splitState" ).arg( mOptsKey ) ).isNull() ? 150 : 16777215 );
mOptSplitter->restoreState( settings.value( QString( "/Windows/%1/splitState" ).arg( mOptsKey ) ).toByteArray() ); mOptSplitter->restoreState( mSettings->value( QString( "/Windows/%1/splitState" ).arg( mOptsKey ) ).toByteArray() );
int curIndx = settings.value( QString( "/Windows/%1/tab" ).arg( mOptsKey ), 0 ).toInt(); 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 the last used tab is out of range or not enabled display the first enabled one
if ( mOptStackedWidget->count() < ( curIndx + 1 ) if ( mOptStackedWidget->count() < ( curIndx + 1 )

View File

@ -20,6 +20,8 @@
#include "qgisgui.h" #include "qgisgui.h"
#include <QDialog> #include <QDialog>
#include <QPointer>
#include <QSettings>
class QDialogButtonBox; class QDialogButtonBox;
class QListWidget; class QListWidget;
@ -53,8 +55,9 @@ class GUI_EXPORT QgsOptionsDialogBase : public QDialog
* @param settingsKey QSettings subgroup key for saving/restore ui states, e.g. "ProjectProperties". * @param settingsKey QSettings subgroup key for saving/restore ui states, e.g. "ProjectProperties".
* @param parent parent object (owner) * @param parent parent object (owner)
* @param fl widget flags * @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(); ~QgsOptionsDialogBase();
/** Set up the base ui connections for vertical tabs. /** Set up the base ui connections for vertical tabs.
@ -63,6 +66,9 @@ class GUI_EXPORT QgsOptionsDialogBase : public QDialog
*/ */
void initOptionsBase( bool restoreUi = true, QString title = QString() ); 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. /** Restore the base ui.
* Sometimes useful to do at end of subclass's constructor. * 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(); * @param title the window title (it does not need to be defined if previously given to initOptionsBase();
@ -93,6 +99,10 @@ class GUI_EXPORT QgsOptionsDialogBase : public QDialog
QDialogButtonBox* mOptButtonBox; QDialogButtonBox* mOptButtonBox;
QString mDialogTitle; QString mDialogTitle;
bool mIconOnly; 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 #endif // QGSOPTIONSDIALOGBASE_H