diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,6 +6,7 @@ set(kirigami2gallery_SRCS main.cpp InfoData.cpp + Config.cpp ) qt5_add_resources(RESOURCES resources.qrc) diff --git a/src/Config.h b/src/Config.h new file mode 100644 --- /dev/null +++ b/src/Config.h @@ -0,0 +1,58 @@ +/* + * Copyright 2020 Carson Black + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#pragma once + +#include +#include + +class Config : public QObject +{ + Q_OBJECT + +#ifndef Q_OS_ANDROID + Q_PROPERTY(int windowX WRITE setWindowX MEMBER m_windowX NOTIFY coordinatesChanged) + Q_PROPERTY(int windowY WRITE setWindowY MEMBER m_windowY NOTIFY coordinatesChanged) + Q_PROPERTY(int windowWidth WRITE setWindowWidth MEMBER m_windowWidth NOTIFY coordinatesChanged) + Q_PROPERTY(int windowHeight WRITE setWindowHeight MEMBER m_windowHeight NOTIFY coordinatesChanged) +#endif + Q_PROPERTY(QString lastPage WRITE setLastPage MEMBER m_lastPage NOTIFY lastPageChanged) + +public: + explicit Config(QObject *parent = nullptr); + ~Config(); + +#ifndef Q_OS_ANDROID + void setWindowX(int); + void setWindowY(int); + void setWindowWidth(int); + void setWindowHeight(int); + Q_SIGNAL void coordinatesChanged(); +#endif + + void setLastPage(const QString&); + Q_SIGNAL void lastPageChanged(); + +private: +#ifndef Q_OS_ANDROID + int m_windowX, m_windowY, m_windowWidth, m_windowHeight; +#endif + QString m_lastPage; + QSettings* m_settings; +}; \ No newline at end of file diff --git a/src/Config.cpp b/src/Config.cpp new file mode 100644 --- /dev/null +++ b/src/Config.cpp @@ -0,0 +1,99 @@ +/* + * Copyright 2020 Carson Black + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "Config.h" + +Config::Config(QObject *parent) : QObject(parent) +{ + m_settings = new QSettings("KDE", "Kirigami Gallery", this); +#ifndef Q_OS_ANDROID + auto map = QList>{ + {QStringLiteral("x"), 0, &m_windowX}, + {QStringLiteral("y"), 0, &m_windowY}, + {QStringLiteral("width"), 600, &m_windowWidth}, + {QStringLiteral("height"), 800, &m_windowHeight}, + }; + for (const auto &x: map) { + *std::get<2>(x) = m_settings->value(std::get<0>(x), std::get<1>(x)).toInt(); + } + Q_EMIT coordinatesChanged(); +#endif + m_lastPage = m_settings->value(QStringLiteral("lastPage"), QStringLiteral("")).value(); + Q_EMIT lastPageChanged(); +} + +Config::~Config() +{ +#ifndef Q_OS_ANDROID + auto map = QList>{ + {QStringLiteral("x"), &m_windowX}, + {QStringLiteral("y"), &m_windowY}, + {QStringLiteral("width"), &m_windowWidth}, + {QStringLiteral("height"), &m_windowHeight}, + }; + for (const auto &x: map) { + m_settings->setValue(std::get<0>(x), *std::get<1>(x)); + } +#endif + m_settings->setValue(QStringLiteral("lastPage"), m_lastPage); +} + +#ifndef Q_OS_ANDROID + +void Config::setWindowX(int x) +{ + if (m_windowX != x) { + m_windowX = x; + Q_EMIT coordinatesChanged(); + } +} + +void Config::setWindowY(int y) +{ + if (m_windowY != y) { + m_windowY = y; + Q_EMIT coordinatesChanged(); + } +} + +void Config::setWindowWidth(int width) +{ + if (m_windowWidth != width) { + m_windowWidth = width; + Q_EMIT coordinatesChanged(); + } +} + +void Config::setWindowHeight(int height) +{ + if (m_windowHeight != height) { + m_windowHeight = height; + Q_EMIT coordinatesChanged(); + } +} + +#endif + +void Config::setLastPage(const QString& page) +{ + if (m_lastPage != page) { + m_lastPage = page; + Q_EMIT lastPageChanged(); + } +} \ No newline at end of file diff --git a/src/data/contents/ui/BaseApp.qml b/src/data/contents/ui/BaseApp.qml --- a/src/data/contents/ui/BaseApp.qml +++ b/src/data/contents/ui/BaseApp.qml @@ -22,10 +22,22 @@ import QtQuick.Layouts 1.2 import org.kde.kirigami 2.11 as Kirigami import "gallery" +import org.kde.kirigami2.gallery 1.0 Kirigami.ApplicationWindow { id: root + x: GalleryConfig.windowX + y: GalleryConfig.windowY + width: GalleryConfig.windowWidth + height: GalleryConfig.windowHeight + + // This isn't a binding loop, it's a bidirectional binding. + Binding { target: GalleryConfig; property: "windowX"; value: root.x } + Binding { target: GalleryConfig; property: "windowY"; value: root.y } + Binding { target: GalleryConfig; property: "windowWidth"; value: root.width } + Binding { target: GalleryConfig; property: "windowHeight"; value: root.height } + globalDrawer: Kirigami.GlobalDrawer { id: globalDrawer title: "Widget gallery" diff --git a/src/data/contents/ui/MainPage.qml b/src/data/contents/ui/MainPage.qml --- a/src/data/contents/ui/MainPage.qml +++ b/src/data/contents/ui/MainPage.qml @@ -22,6 +22,7 @@ import QtQuick.Layouts 1.2 import org.kde.kirigami 2.11 as Kirigami import org.kde.kitemmodels 1.0 +import org.kde.kirigami2.gallery 1.0 Kirigami.ScrollablePage { id: pageRoot @@ -58,9 +59,13 @@ ] } + Component.onCompleted: { + root.pageStack.push(mainPagePool.loadPage(GalleryConfig.lastPage)) + } Kirigami.PagePool { id: mainPagePool } + ListModel { id: galleryModel ListElement { @@ -220,6 +225,9 @@ pagePool: mainPagePool basePage: pageRoot page: targetPage + onTriggered: { + GalleryConfig.lastPage = targetPage + } } } } diff --git a/src/main.cpp b/src/main.cpp --- a/src/main.cpp +++ b/src/main.cpp @@ -26,6 +26,7 @@ #include #include #include +#include "Config.h" #include "InfoData.h" #ifdef Q_OS_ANDROID @@ -56,6 +57,7 @@ QQmlApplicationEngine engine; qmlRegisterType("Data", 1, 0, "InfoData"); + qmlRegisterSingletonType("org.kde.kirigami2.gallery", 1, 0, "GalleryConfig", [](QQmlEngine*,QJSEngine*) -> QObject* {return new Config;}); //we want different main files on desktop or mobile //very small difference as they as they are subclasses of the same thing