Skip to content

Commit d378eef

Browse files
committed
allow to use custom QSettings for managing QgsOptionsDialog parameters.
Useful for plugins and custom apps
1 parent b1dd6d3 commit d378eef

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

python/gui/qgsoptionsdialogbase.sip

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class QgsOptionsDialogBase : QDialog
1818
*/
1919
void initOptionsBase( bool restoreUi = true, QString title = QString() );
2020

21+
// set custom QSettings pointer if dialog used outside QGIS (in plugin)
22+
void setSettings( QSettings* settings );
23+
2124
/** Restore the base ui.
2225
* Sometimes useful to do at end of subclass's constructor.
2326
* @param title the window title (it does not need to be defined if previously given to initOptionsBase();

src/gui/qgsoptionsdialogbase.cpp

+38-13
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,49 @@
2222
#include <QListWidget>
2323
#include <QMessageBox>
2424
#include <QScrollBar>
25-
#include <QSettings>
2625
#include <QStackedWidget>
2726
#include <QSplitter>
2827
#include <QTimer>
2928

3029

31-
QgsOptionsDialogBase::QgsOptionsDialogBase( QString settingsKey, QWidget* parent, Qt::WFlags fl )
30+
QgsOptionsDialogBase::QgsOptionsDialogBase( QString settingsKey, QWidget* parent, Qt::WFlags fl, QSettings* settings )
3231
: QDialog( parent, fl )
3332
, mOptsKey( settingsKey )
3433
, mInit( false )
3534
, mDialogTitle( "" )
35+
, mSettings( settings )
3636
{
3737
}
3838

3939
QgsOptionsDialogBase::~QgsOptionsDialogBase()
4040
{
4141
if ( mInit )
4242
{
43-
QSettings settings;
44-
settings.setValue( QString( "/Windows/%1/geometry" ).arg( mOptsKey ), saveGeometry() );
45-
settings.setValue( QString( "/Windows/%1/splitState" ).arg( mOptsKey ), mOptSplitter->saveState() );
46-
settings.setValue( QString( "/Windows/%1/tab" ).arg( mOptsKey ), mOptStackedWidget->currentIndex() );
43+
mSettings->setValue( QString( "/Windows/%1/geometry" ).arg( mOptsKey ), saveGeometry() );
44+
mSettings->setValue( QString( "/Windows/%1/splitState" ).arg( mOptsKey ), mOptSplitter->saveState() );
45+
mSettings->setValue( QString( "/Windows/%1/tab" ).arg( mOptsKey ), mOptStackedWidget->currentIndex() );
4746
}
47+
48+
if ( mDelSettings ) // local settings obj to delete
49+
{
50+
delete mSettings;
51+
}
52+
53+
mSettings = 0; // null the pointer (in case of outside settings obj)
4854
}
4955

5056
void QgsOptionsDialogBase::initOptionsBase( bool restoreUi, QString title )
5157
{
58+
// use pointer to app QSettings if no custom QSettings specified
59+
// custom QSettings object may be from Python plugin
60+
mDelSettings = false;
61+
62+
if ( !mSettings )
63+
{
64+
mSettings = new QSettings();
65+
mDelSettings = true; // only delete obj created by class
66+
}
67+
5268
// save dialog title so it can be used to be concatenated
5369
// with category title in icon-only mode
5470
if ( title.isEmpty() )
@@ -76,8 +92,7 @@ void QgsOptionsDialogBase::initOptionsBase( bool restoreUi, QString title )
7692
return;
7793
}
7894

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

133+
void QgsOptionsDialogBase::setSettings( QSettings* settings )
134+
{
135+
if ( mDelSettings ) // local settings obj to delete
136+
{
137+
delete mSettings;
138+
}
139+
140+
mSettings = settings;
141+
mDelSettings = false; // don't delete outside obj
142+
}
143+
118144
void QgsOptionsDialogBase::restoreOptionsBaseUi( QString title )
119145
{
120146
if ( !mInit )
@@ -131,14 +157,13 @@ void QgsOptionsDialogBase::restoreOptionsBaseUi( QString title )
131157
// re-save original dialog title in case it was changed after dialog initialization
132158
mDialogTitle = windowTitle();
133159

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

143168
// if the last used tab is out of range or not enabled display the first enabled one
144169
if ( mOptStackedWidget->count() < ( curIndx + 1 )

src/gui/qgsoptionsdialogbase.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "qgisgui.h"
2121

2222
#include <QDialog>
23+
#include <QPointer>
24+
#include <QSettings>
2325

2426
class QDialogButtonBox;
2527
class QListWidget;
@@ -53,8 +55,9 @@ class GUI_EXPORT QgsOptionsDialogBase : public QDialog
5355
* @param settingsKey QSettings subgroup key for saving/restore ui states, e.g. "ProjectProperties".
5456
* @param parent parent object (owner)
5557
* @param fl widget flags
58+
* @param settings custom QSettings pointer
5659
*/
57-
QgsOptionsDialogBase( QString settingsKey, QWidget* parent = 0, Qt::WFlags fl = 0 );
60+
QgsOptionsDialogBase( QString settingsKey, QWidget* parent = 0, Qt::WFlags fl = 0, QSettings* settings = 0 );
5861
~QgsOptionsDialogBase();
5962

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

69+
// set custom QSettings pointer if dialog used outside QGIS (in plugin)
70+
void setSettings( QSettings* settings );
71+
6672
/** Restore the base ui.
6773
* Sometimes useful to do at end of subclass's constructor.
6874
* @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
9399
QDialogButtonBox* mOptButtonBox;
94100
QString mDialogTitle;
95101
bool mIconOnly;
102+
// pointer to app or custom, external QSettings
103+
// QPointer in case custom settings obj gets deleted while dialog is open
104+
QPointer<QSettings> mSettings;
105+
bool mDelSettings;
96106
};
97107

98108
#endif // QGSOPTIONSDIALOGBASE_H

0 commit comments

Comments
 (0)