From: APTX Date: Sat, 11 Mar 2017 22:27:06 +0000 (+0100) Subject: Implement basic settings X-Git-Url: https://gitweb.tyo.aptx.org/?a=commitdiff_plain;h=18fc23133b5480bc9a09d8893515fb8164e838b2;p=aniplayer.git Implement basic settings A ui instance gets a Settings QObject to save whatever settings the ui wants. Currently the ui is responsible for saving the player state. The settings layout is very basic and will change in the future in incompatible ways. JS makes it difficult to read the settings properly as all values return as strings and not their correct type. --- diff --git a/core/core.pro b/core/core.pro index 30ca62d..9a04597 100644 --- a/core/core.pro +++ b/core/core.pro @@ -10,7 +10,8 @@ SOURCES += main.cpp \ player.cpp \ pluginmanager.cpp \ videoelement.cpp \ - instancemanager.cpp + instancemanager.cpp \ + settings.cpp HEADERS += \ player.h \ @@ -20,7 +21,8 @@ HEADERS += \ include/aniplayer/uipluginbase.h \ pluginmanager.h \ videoelement.h \ - instancemanager.h + instancemanager.h \ + settings.h include(qtsingleapplication/qtsingleapplication.pri) diff --git a/core/include/aniplayer/uipluginbase.h b/core/include/aniplayer/uipluginbase.h index 16572e4..04b0479 100644 --- a/core/include/aniplayer/uipluginbase.h +++ b/core/include/aniplayer/uipluginbase.h @@ -12,7 +12,8 @@ class UiPluginBase { public: virtual ~UiPluginBase() = default; - virtual UiInstance *createUi(QObject *player, QObject *parent = nullptr) = 0; + virtual UiInstance *createUi(QObject *player, QObject *settings, + QObject *parent = nullptr) = 0; }; #define ANIPLAYER_UI_PLUGIN_INTERFACE_IID "org.aptx.aniplayer.UiPluginInterface" diff --git a/core/instancemanager.cpp b/core/instancemanager.cpp index 1ad5495..0a03574 100644 --- a/core/instancemanager.cpp +++ b/core/instancemanager.cpp @@ -8,6 +8,7 @@ #include "aniplayer/uipluginbase.h" #include "player.h" #include "pluginmanager.h" +#include "settings.h" Q_LOGGING_CATEGORY(imCategory, "InstanceManager") @@ -60,7 +61,10 @@ void InstanceManager::startFirstInstance() { throw std::runtime_error{std::string("Failed to load ui: ") + qPrintable(m_uiPluginManager->errorString())}; - auto ui = uiInstance->createUi(player, this); + auto settings = new Settings; + Q_CHECK_PTR(settings); + + auto ui = uiInstance->createUi(player, settings, this); Q_CHECK_PTR(ui); if (!positionalArgs.empty()) { diff --git a/core/main.cpp b/core/main.cpp index be9e57b..6585b5e 100644 --- a/core/main.cpp +++ b/core/main.cpp @@ -13,8 +13,10 @@ int main(int argc, char *argv[]) { QtSingleApplication app(argc, argv); app.setApplicationDisplayName("AniPlayer"); - app.setApplicationName("AniPlayer"); - app.setApplicationVersion("1.0"); + app.setApplicationName("AniPlayer3"); + app.setOrganizationName("APTX"); + app.setOrganizationDomain("aptx.org"); + app.setApplicationVersion("3.0"); InstanceManager im; QObject::connect(&app, SIGNAL(messageReceived(QString)), &im, diff --git a/core/settings.cpp b/core/settings.cpp new file mode 100644 index 0000000..6b3d059 --- /dev/null +++ b/core/settings.cpp @@ -0,0 +1,39 @@ +#include "settings.h" + +#include +#include + +#include "player.h" + +Settings::Settings(QObject *parent) : QObject(parent) { + m_settings = + new QSettings{QSettings::IniFormat, QSettings::UserScope, + qApp->organizationName(), qApp->applicationName(), this}; +} + +Settings::~Settings() +{ + m_settings->sync(); +} + +void Settings::savePlayerState(Player *player) +{ + set("playerVolume", player->volume()); + set("playerMuted", player->volume()); +} + +void Settings::loadPlayerState(Player *player) const +{ + player->setVolume(get("playerVolume", player->volume()).toDouble()); + player->setMuted(get("playerMuted", player->muted()).toBool()); +} + +void Settings::set(const QString &name, const QVariant &value) +{ + m_settings->setValue(name, value); +} + +QVariant Settings::get(const QString &name, const QVariant &defaultValue) const +{ + return m_settings->value(name, defaultValue); +} diff --git a/core/settings.h b/core/settings.h new file mode 100644 index 0000000..7dfb91f --- /dev/null +++ b/core/settings.h @@ -0,0 +1,31 @@ +#ifndef SETTINGS_H +#define SETTINGS_H + +#include +#include + +class QSettings; +class Player; + +class Settings : public QObject +{ + Q_OBJECT +public: + explicit Settings(QObject *parent = 0); + ~Settings() override; + +signals: + void settingsChanged(); + +public slots: + void savePlayerState(Player *); + void loadPlayerState(Player *) const; + + void set(const QString &name, const QVariant &value); + QVariant get(const QString &name, const QVariant &defaultValue = QVariant{}) const; + +private: + QSettings *m_settings; +}; + +#endif // SETTINGS_H diff --git a/uiplugins/ui_desktop_qml_default/qml/PlayerControls.qml b/uiplugins/ui_desktop_qml_default/qml/PlayerControls.qml index a8c14a9..aaeaee3 100644 --- a/uiplugins/ui_desktop_qml_default/qml/PlayerControls.qml +++ b/uiplugins/ui_desktop_qml_default/qml/PlayerControls.qml @@ -13,6 +13,30 @@ Row { fullscreenButton.checked = !fullscreenButton.checked; } + function saveSettings() { + settings.set("fullScreen", fullscreenButton.checked); + settings.set("stayOnTop", stayOnTopButton.checked); + settings.set("frameless", framelessButton.checked); + } + + function loadSettings() { + fullscreenButton.checked = "true" === settings.get("fullScreen", fullscreenButton.checked); + stayOnTopButton.checked = "true" === settings.get("stayOnTop", stayOnTopButton.checked); + framelessButton.checked = "true" === settings.get("frameless", framelessButton.checked); + } + + Timer { + running: true + interval: 100 + onTriggered: { + if (!fullscreenButton.checked) { + console.log("Fixing onTop"); + stayOnTopButton.checked = !stayOnTopButton.checked; + stayOnTopButton.checked = !stayOnTopButton.checked; + } + } + } + Action { id: openAction enabled: controlledPlayer diff --git a/uiplugins/ui_desktop_qml_default/qml/main.qml b/uiplugins/ui_desktop_qml_default/qml/main.qml index b52ec00..540c583 100644 --- a/uiplugins/ui_desktop_qml_default/qml/main.qml +++ b/uiplugins/ui_desktop_qml_default/qml/main.qml @@ -4,16 +4,45 @@ import QtQuick 2.7 Window { id: window - visible: true width: 300 height: 300 - property int clicks: 0 + property bool controlsVisible: true //property Visibility previousVisibility: Window.Normal + Component.onCompleted: { + loadSettings(); + window.visible = true + } + + onClosing: saveSettings() + function isFullScreen() { return visibility === Window.FullScreen } + function saveSettings() { + console.log("SAVING SETTINGS"); + settings.savePlayerState(player); + settings.set("x", x); + settings.set("y", y); + settings.set("width", width); + settings.set("height", height); + settings.set("controlsVisible", controls.visible); + controls.saveSettings(); + } + + function loadSettings() { + console.log("LOADING SETTINGS"); + settings.loadPlayerState(player); + x = settings.get("x", x) - 0; + y = settings.get("y", y) - 0; + width = settings.get("width", width) - 0; + height = settings.get("height", height) - 0; + controlsVisible = "true" === settings.get("controlsVisible", controlsVisible); + controls.loadSettings(); + } + + VideoElement { source: player anchors.fill: parent @@ -88,9 +117,10 @@ Window { anchors.bottom: parent.bottom anchors.left: parent.left anchors.right: parent.right + visible: controlsVisible function toggleVisible() { - visible = !visible; + controlsVisible = !controlsVisible; } } } diff --git a/uiplugins/ui_desktop_qml_default/uidesktopqmldefault.cpp b/uiplugins/ui_desktop_qml_default/uidesktopqmldefault.cpp index d1bec91..c38d5f0 100644 --- a/uiplugins/ui_desktop_qml_default/uidesktopqmldefault.cpp +++ b/uiplugins/ui_desktop_qml_default/uidesktopqmldefault.cpp @@ -1,26 +1,32 @@ #include "uidesktopqmldefault.h" +#include #include #include -#include #include "timeformatter.h" Q_LOGGING_CATEGORY(uidqdCategory, "UiDefault") -UiDesktopQmlDefaultInstance::UiDesktopQmlDefaultInstance(QObject *player, QObject *parent) -: QObject{parent} { +UiDesktopQmlDefaultInstance::UiDesktopQmlDefaultInstance(QObject *player, + QObject *settings, + QObject *parent) + : QObject{parent} { player->setParent(this); + settings->setParent(this); QQmlApplicationEngine *engine = new QQmlApplicationEngine{this}; Q_CHECK_PTR(engine); auto timeFormatter = new TimeFormatter{this}; engine->rootContext()->setContextProperty("player", player); + engine->rootContext()->setContextProperty("settings", settings); engine->rootContext()->setContextProperty("timeFormatter", timeFormatter); qCDebug(uidqdCategory, "Player Added"); - engine->load(QUrl(QStringLiteral("qrc:/ui_desktop_qml_default/qml/main.qml"))); + engine->load( + QUrl(QStringLiteral("qrc:/ui_desktop_qml_default/qml/main.qml"))); qCDebug(uidqdCategory, "QML engine loaded"); } -UiInstance *UiDesktopQmlDefault::createUi(QObject *player, QObject *parent) { - return new UiDesktopQmlDefaultInstance{player, parent}; +UiInstance *UiDesktopQmlDefault::createUi(QObject *player, QObject *settings, + QObject *parent) { + return new UiDesktopQmlDefaultInstance{player, settings, parent}; } diff --git a/uiplugins/ui_desktop_qml_default/uidesktopqmldefault.h b/uiplugins/ui_desktop_qml_default/uidesktopqmldefault.h index 389c5d7..928a779 100644 --- a/uiplugins/ui_desktop_qml_default/uidesktopqmldefault.h +++ b/uiplugins/ui_desktop_qml_default/uidesktopqmldefault.h @@ -9,7 +9,7 @@ class UI_DESKTOP_QML_DEFAULTSHARED_EXPORT UiDesktopQmlDefaultInstance public UiInstance { Q_OBJECT public: - explicit UiDesktopQmlDefaultInstance(QObject *player, + explicit UiDesktopQmlDefaultInstance(QObject *player, QObject *settings, QObject *parent = nullptr); }; @@ -20,8 +20,10 @@ class UI_DESKTOP_QML_DEFAULTSHARED_EXPORT UiDesktopQmlDefault Q_PLUGIN_METADATA(IID ANIPLAYER_UI_PLUGIN_INTERFACE_IID FILE "ui_desktop_qml_default.json") Q_INTERFACES(UiPluginBase) + public: - UiInstance *createUi(QObject *player, QObject *parent) override; + UiInstance *createUi(QObject *player, QObject *settings, + QObject *parent) override; }; #endif // UIDESKTOPQMLDEFAULT_H