diff options
author | Andras Becsi <andras.becsi@theqtcompany.com> | 2015-07-31 17:00:22 +0200 |
---|---|---|
committer | Andras Becsi <andras.becsi@theqtcompany.com> | 2015-08-12 17:22:03 +0200 |
commit | 8fae8466f66190db6e710e5e6da9dde68ca025e1 (patch) | |
tree | 28e85aedc74ea0c51dd79e0c8e65c096779cc67e /src | |
parent | 8e0d455a73278fd223bcb863de778019c74caaa3 (diff) |
Add persistence to home screen bookmarks
Diffstat (limited to 'src')
-rw-r--r-- | src/engine.cpp | 21 | ||||
-rw-r--r-- | src/engine.h | 21 | ||||
-rw-r--r-- | src/qml/HomeScreen.qml | 21 | ||||
-rw-r--r-- | src/qml/NavigationBar.qml | 6 | ||||
-rw-r--r-- | src/touchtracker.cpp | 2 |
5 files changed, 57 insertions, 14 deletions
diff --git a/src/engine.cpp b/src/engine.cpp index 84b0c0e..8da944e 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -37,6 +37,17 @@ #include "engine.h" +#include <QtCore/QDir> +#include <QtCore/QStandardPaths> +#include <QStringBuilder> + +Engine::Engine(QObject *parent) + : QObject(parent) + , m_bookmarks(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) % QDir::separator() % "bookmarks.ini", QSettings::IniFormat, this) +{ + qsrand(255); +} + QUrl Engine::fromUserInput(const QString& userInput) { QFileInfo fileInfo(userInput); @@ -91,3 +102,13 @@ QString Engine::oppositeColor(const QString &color) const QColor c(QColor(color).toHsv()); return QColor::fromHsv(c.hue(), c.saturation(), c.value() < 127 ? 255 : c.value() - 100).name(); } + +QString Engine::restoreBookmarks() +{ + return m_bookmarks.value("bookmarks").toString(); +} + +void Engine::saveBookmarks(const QString & list) +{ + m_bookmarks.setValue("bookmarks", list); +} diff --git a/src/engine.h b/src/engine.h index 36cb561..3fb30e2 100644 --- a/src/engine.h +++ b/src/engine.h @@ -40,6 +40,7 @@ #include <QtCore/QEvent> #include <QtCore/QFileInfo> +#include <QtCore/QSettings> #include <QtCore/QUrl> #include <QtGui/QColor> #include <QtQuick/QQuickItemGrabResult> @@ -82,22 +83,22 @@ class Engine : public QObject { Q_PROPERTY(QObject * rootWindow READ rootWindow FINAL CONSTANT) + QSettings m_bookmarks; + public: - Engine(QObject *parent) - : QObject(parent) - { - qsrand(255); - } + Engine(QObject *parent); QObject *rootWindow() { return parent(); } - Q_INVOKABLE static QUrl fromUserInput(const QString& userInput); - Q_INVOKABLE static QString domainFromString(const QString& urlString); - Q_INVOKABLE static QString randomColor(); - Q_INVOKABLE static QString colorForIcon(QQuickItemGrabResult *result); - Q_INVOKABLE static QString oppositeColor(const QString & color); + Q_INVOKABLE QUrl fromUserInput(const QString& userInput); + Q_INVOKABLE QString domainFromString(const QString& urlString); + Q_INVOKABLE QString randomColor(); + Q_INVOKABLE QString colorForIcon(QQuickItemGrabResult *result); + Q_INVOKABLE QString oppositeColor(const QString & color); + Q_INVOKABLE QString restoreBookmarks(); + Q_INVOKABLE void saveBookmarks(const QString & list); }; #endif // ENGINE_H diff --git a/src/qml/HomeScreen.qml b/src/qml/HomeScreen.qml index 44d15f0..4905ca9 100644 --- a/src/qml/HomeScreen.qml +++ b/src/qml/HomeScreen.qml @@ -59,6 +59,10 @@ Rectangle { listModel.append(element) } + function get(index) { + return listModel.get(index) + } + function contains(url) { for (var idx = 0; idx < listModel.count; ++idx) { if (listModel.get(idx).url === url) @@ -93,6 +97,21 @@ Rectangle { ListModel { id: listModel + Component.onCompleted: { + listModel.clear() + var string = engine.restoreBookmarks() + var list = JSON.parse(string) + for (var i = 0; i < list.length; ++i) { + listModel.append(list[i]) + } + } + Component.onDestruction: { + var list = [] + for (var i = 0; i < listModel.count; ++i) { + list[i] = listModel.get(i) + } + engine.saveBookmarks(JSON.stringify(list)) + } } GridView { @@ -422,7 +441,7 @@ Rectangle { color: "transparent" anchors.centerIn: parent width: 500 - height: 100 + height: 300 Text { anchors.centerIn: parent color: placeholderColor diff --git a/src/qml/NavigationBar.qml b/src/qml/NavigationBar.qml index 1089cd7..0bf0b78 100644 --- a/src/qml/NavigationBar.qml +++ b/src/qml/NavigationBar.qml @@ -277,13 +277,15 @@ ToolBar { if (!webView) return + console.log(webView.loading) + var icon = webView.loading ? "" : webView.icon var idx = homeScreen.contains(webView.url.toString()) if (idx != -1) { + homeScreen.get(idx).iconUrl = icon.toString() homeScreen.state = "enabled" - homeScreen.currentIndex = idx + homeScreen.set(idx) return } - var icon = webView.loading ? "" : webView.icon homeScreen.add(webView.title, webView.url, icon, engine.randomColor()) } } diff --git a/src/touchtracker.cpp b/src/touchtracker.cpp index 81dba5c..aa4d8a1 100644 --- a/src/touchtracker.cpp +++ b/src/touchtracker.cpp @@ -2,7 +2,7 @@ #include <QDateTime> -#include "utils.h" +#include "engine.h" using namespace utils; |