[PATCH] Add support for remembering settings
Tomaz Canabrava
tcanabrava at kde.org
Sat Apr 27 05:03:39 PDT 2013
I'm not sure, but already asked dirk about this and he wanted to keep
things in dconf. Dirk?
Em 27/04/2013 06:10, <amit.k.chaudhuri at gmail.com> escreveu:
> From: Amit Chaudhuri <amit.k.chaudhuri at gmail.com>
>
> Use QSettings to provide persistent storage of settings. For example, we
> store and restore the size of the MainWindow.
>
> We use the organisation name hohndel.org and keep subsurface as the
> application name.
>
> A section is specified for things to do with the MainWindow; other
> sections could be added e.g. for preferred units?
>
> Signed-off-by: Amit Chaudhuri <amit.k.chaudhuri at gmail.com>
> ---
> qt-ui/mainwindow.cpp | 37 +++++++++++++++++++++++++++++++++++++
> qt-ui/mainwindow.h | 5 +++++
> 2 files changed, 42 insertions(+)
>
> diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp
> index 8cdc601..0c6691a 100644
> --- a/qt-ui/mainwindow.cpp
> +++ b/qt-ui/mainwindow.cpp
> @@ -12,6 +12,8 @@
> #include <QtDebug>
> #include <QDateTime>
> #include <QSortFilterProxyModel>
> +#include <QSettings>
> +#include <QCloseEvent>
>
> #include "divelistview.h"
> #include "starwidget.h"
> @@ -31,6 +33,7 @@ MainWindow::MainWindow() : ui(new Ui::MainWindow()),
> ui->ListWidget->setModel(sortModel);
>
> setWindowIcon(QIcon(":subsurface-icon"));
> + readSettings();
> }
>
> void MainWindow::on_actionNew_triggered()
> @@ -138,6 +141,11 @@ void MainWindow::on_actionPreferences_triggered()
> void MainWindow::on_actionQuit_triggered()
> {
> qDebug("actionQuit");
> + if (unsaved_changes() && (askSaveChanges() == FALSE))
> + {
> + return;
> + }
> +
> }
>
> void MainWindow::on_actionDownloadDC_triggered()
> @@ -284,3 +292,32 @@ bool MainWindow::askSaveChanges()
> }
> return false;
> }
> +
> +void MainWindow::readSettings()
> +{
> + QSettings settings("hohndel.org","subsurface");
> +
> + /* note: section/key i.e. forward slash to separate */
> + QSize sz = settings.value("MainWindow/size").value<QSize>();
> + resize(sz);
> +}
> +
> +void MainWindow::writeSettings()
> +{
> + QSettings settings("hohndel.org","subsurface");
> + settings.beginGroup("MainWindow");
> + settings.setValue("size",size());
> + settings.endGroup();
> + /* other groups here; avoid '/' and '\' in keys with setValue(...)
> please */
> +}
> +
> +void MainWindow::closeEvent(QCloseEvent *event)
> +{
> + if (unsaved_changes() && (askSaveChanges() == FALSE))
> + {
> + event->ignore();
> + return;
> + }
> + event->accept();
> + writeSettings();
> +}
> \ No newline at end of file
> diff --git a/qt-ui/mainwindow.h b/qt-ui/mainwindow.h
> index fdb100c..eece91a 100644
> --- a/qt-ui/mainwindow.h
> +++ b/qt-ui/mainwindow.h
> @@ -66,12 +66,17 @@ private Q_SLOTS:
> void on_actionAboutSubsurface_triggered();
> void on_actionUserManual_triggered();
>
> +protected:
> + void closeEvent(QCloseEvent *);
> +
> private:
> Ui::MainWindow *ui;
> DiveTripModel *model;
> QSortFilterProxyModel *sortModel;
> QString filter();
> bool askSaveChanges();
> + void readSettings();
> + void writeSettings();
>
> };
>
> --
> 1.7.10.4
>
> _______________________________________________
> subsurface mailing list
> subsurface at hohndel.org
> http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.hohndel.org/pipermail/subsurface/attachments/20130427/a15cd2d7/attachment.html>
More information about the subsurface
mailing list