aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskari Timperi <oskari.timperi@iki.fi>2014-01-11 21:34:35 +0200
committerOskari Timperi <oskari.timperi@iki.fi>2014-02-03 23:32:08 +0200
commite1bb6186c353dcfffb0b9e01ae40cdb365652c1b (patch)
treeb29e697d27571649148379af4d62083bb2fc0809
parent7d9c1b0da32bed3ee2413bafce89e01e80a5508f (diff)
downloadunitmaster-e1bb6186c353dcfffb0b9e01ae40cdb365652c1b.tar.gz
unitmaster-e1bb6186c353dcfffb0b9e01ae40cdb365652c1b.zip
add Settings class
-rw-r--r--rpm/harbour-unitmaster.yaml2
-rw-r--r--src/main.cpp28
-rw-r--r--src/settings.cpp97
-rw-r--r--src/settings.h35
-rw-r--r--src/src.pro8
5 files changed, 159 insertions, 11 deletions
diff --git a/rpm/harbour-unitmaster.yaml b/rpm/harbour-unitmaster.yaml
index 5976bb3..655d957 100644
--- a/rpm/harbour-unitmaster.yaml
+++ b/rpm/harbour-unitmaster.yaml
@@ -23,6 +23,8 @@ Files:
- '%{_datadir}/%{name}/qml'
- '%{_bindir}/%{name}'
PkgBR: []
+QMakeOptions:
+- UNITMASTER_VERSION=%{version}-%{release}
SubPackages:
- Name: test
diff --git a/src/main.cpp b/src/main.cpp
index 0c04372..3727f1f 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -4,18 +4,26 @@
#include <sailfishapp.h>
+#include "settings.h"
+
+#define STRINGIFY(X) #X
int main(int argc, char *argv[])
{
- // SailfishApp::main() will display "qml/template.qml", if you need more
- // control over initialization, you can use:
- //
- // - SailfishApp::application(int, char *[]) to get the QGuiApplication *
- // - SailfishApp::createView() to get a new QQuickView * instance
- // - SailfishApp::pathTo(QString) to get a QUrl to a resource file
- //
- // To display the view, call "show()" (will show fullscreen on device).
-
- return SailfishApp::main(argc, argv);
+ QCoreApplication::setOrganizationName("oswjk");
+ QCoreApplication::setApplicationName("unitmaster");
+ QCoreApplication::setApplicationVersion(STRINGIFY(UNITMASTER_VERSION));
+
+ QGuiApplication *app = SailfishApp::application(argc, argv);
+ QQuickView *view = SailfishApp::createView();
+
+ Settings settings;
+ view->rootContext()->setContextProperty("settings", &settings);
+
+ view->setSource(SailfishApp::pathTo("qml/harbour-unitmaster.qml"));
+
+ view->showFullScreen();
+
+ return app->exec();
}
diff --git a/src/settings.cpp b/src/settings.cpp
new file mode 100644
index 0000000..f221854
--- /dev/null
+++ b/src/settings.cpp
@@ -0,0 +1,97 @@
+/**
+ * Copyright (c) 2012 Nokia Corporation.
+ * All rights reserved.
+ *
+ * For the applicable distribution terms see the license text file included in
+ * the distribution.
+ */
+
+#include "settings.h"
+#include <QtCore/QSettings>
+#include <QDebug>
+#include <QGuiApplication>
+
+/*!
+ \class Settings
+ \brief This class loads and stores settings used by client application.
+*/
+
+
+/*!
+ Constructor.
+*/
+Settings::Settings(QObject *parent)
+ : QObject(parent)
+{
+}
+
+
+/*!
+ Copy constructor.
+ Not used.
+*/
+Settings::Settings(const Settings &settings)
+ : QObject(0)
+{
+ Q_UNUSED(settings)
+}
+
+
+/*!
+ Stores the setting as a key value pair.
+*/
+void Settings::setSetting(const QString &setting, const QVariant &value)
+{
+ QSettings settings;
+ settings.setValue(setting, value);
+ //qDebug() << "changed setting: " << setting << "to value: " << value;
+ emit settingChanged(setting, value);
+}
+
+
+/*!
+ Loads the setting by key. Returns value or the default value if no setting is found.
+*/
+QVariant Settings::setting(const QString &setting, const QVariant &defaultValue)
+{
+ QSettings settings;
+ //qDebug() << "returning settings value: " << settings.value(setting);
+ return settings.value(setting, defaultValue);
+}
+
+/*!
+ Loads the setting by key. Returns value.
+*/
+QVariant Settings::setting(const QString &setting)
+{
+ QSettings settings;
+ //qDebug() << "returning settings value: " << settings.value(setting);
+ return settings.value(setting);
+}
+
+
+/*!
+ Removes the setting by key. Returns true, if setting removed
+ successfully. Otherwise false.
+*/
+bool Settings::removeSetting(const QString &setting)
+{
+ QSettings settings;
+ if (!settings.contains(setting)) {
+ return false;
+ }
+
+ settings.remove(setting);
+ return true;
+}
+
+
+/*!
+ Removes all settings.
+*/
+void Settings::clear()
+{
+ QSettings settings;
+ settings.clear();
+}
+
diff --git a/src/settings.h b/src/settings.h
new file mode 100644
index 0000000..e05f216
--- /dev/null
+++ b/src/settings.h
@@ -0,0 +1,35 @@
+/**
+ * Copyright (c) 2012 Nokia Corporation.
+ * All rights reserved.
+ *
+ * For the applicable distribution terms see the license text file included in
+ * the distribution.
+ */
+
+#ifndef SETTINGS_H
+#define SETTINGS_H
+
+#include <QtCore/QObject>
+#include <QtCore/QVariant>
+
+
+class Settings : public QObject
+{
+ Q_OBJECT
+
+public:
+ explicit Settings(QObject *parent = 0);
+ explicit Settings(const Settings &settings);
+
+public slots:
+ void setSetting(const QString &setting, const QVariant &value);
+ QVariant setting(const QString &setting, const QVariant &defaultValue);
+ QVariant setting(const QString &setting);
+ bool removeSetting(const QString &setting);
+ void clear();
+
+signals:
+ void settingChanged(const QString &setting, const QVariant &value);
+};
+
+#endif // SETTINGS_H
diff --git a/src/src.pro b/src/src.pro
index 82aa2df..3593adb 100644
--- a/src/src.pro
+++ b/src/src.pro
@@ -16,6 +16,8 @@ CONFIG += link_pkgconfig
PKGCONFIG += sailfishapp
INCLUDEPATH += /usr/include/sailfishapp
+DEFINES += UNITMASTER_VERSION=$${UNITMASTER_VERSION}
+
target.path = /usr/bin
qml.files = qml
@@ -31,7 +33,8 @@ INSTALLS += target qml desktop icon
#CONFIG += sailfishapp
-SOURCES += main.cpp
+SOURCES += main.cpp \
+ settings.cpp
OTHER_FILES += \
qml/pages/QuantityModel.qml \
@@ -45,3 +48,6 @@ OTHER_FILES += \
harbour-unitmaster.desktop \
qml/harbour-unitmaster.qml \
../rpm/$${TARGET}.spec
+
+HEADERS += \
+ settings.h