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() );
// 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();

View File

@ -22,17 +22,17 @@
#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 )
{
}
@ -40,15 +40,31 @@ 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() )
@ -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;
@ -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 )
@ -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 )

View File

@ -20,6 +20,8 @@
#include "qgisgui.h"
#include <QDialog>
#include <QPointer>
#include <QSettings>
class QDialogButtonBox;
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 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.
@ -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();
@ -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