From f80ed25f41f21d567d0c99534e3c5d3670bfdb91 Mon Sep 17 00:00:00 2001 From: SeeLook <945374+SeeLook@users.noreply.github.com> Date: Wed, 11 Jan 2017 22:51:42 +0100 Subject: [PATCH] Tglobals inherits from QObject, added static instance for access to it. Added method for reading settings value by key name, exposing it to QML. Tglobals is accessible for QML through GLOB --- src/libs/core/tglobals.cpp | 31 ++++++++++++++++++++----------- src/libs/core/tglobals.h | 26 ++++++++++++++++++++------ src/main.cpp | 1 + src/qml/MainWindow.qml | 22 +++++++++++++++++++--- 4 files changed, 60 insertions(+), 20 deletions(-) diff --git a/src/libs/core/tglobals.cpp b/src/libs/core/tglobals.cpp index 6e52e0d17..9f3707a00 100755 --- a/src/libs/core/tglobals.cpp +++ b/src/libs/core/tglobals.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2011-2016 by Tomasz Bojczuk * + * Copyright (C) 2011-2017 by Tomasz Bojczuk * * seelook@gmail.com * * * * This program is free software; you can redistribute it and/or modify * @@ -32,14 +32,15 @@ #if defined (Q_OS_ANDROID) #include <Android/tandroid.h> #endif -#include <QDir> -#include <QSettings> -#include <QCoreApplication> -#include <QDebug> +#include <QtCore/qdir.h> +#include <QtCore/qsettings.h> +#include <QtCore/qcoreapplication.h> +#include <QtCore/qdebug.h> /*static*/ QString& Tglobals::path = Tpath::main; +Tglobals* Tglobals::m_instance = nullptr; QString Tglobals::getInstPath(QString appInstPath) { QString p; @@ -64,7 +65,8 @@ TtouchProxy* onlyOneTouchProxy = 0; // It is available through TtouchProxy::inst /*end static*/ -Tglobals::Tglobals() : +Tglobals::Tglobals(QObject* parent) : + QObject(parent), m_tune(0) { version = NOOTKA_VERSION; @@ -91,11 +93,12 @@ Tglobals::Tglobals() : #endif loadSettings(config); - if (Tcore::gl() == 0) - Tcore::setGlobals(this); - else { - qDebug() << "Tglobals instance has already existed. Application is terminating!"; - exit(109); + if (!m_instance) { + Tcore::setGlobals(this); // TODO remove it, use GLOB macro instead + m_instance = this; + } else { + qDebug() << "Tglobals instance has already existed. Application is terminating!"; + exit(109); } onlyOneTouchProxy = new TtouchProxy(); } @@ -112,6 +115,7 @@ Tglobals::~Tglobals() { delete onlyOneTouchProxy; delete TtouchParams::i(); Tcore::reset(); + m_instance = nullptr; } @@ -119,6 +123,11 @@ Tglobals::~Tglobals() { //####################### PUBLIC ########################################### //########################################################################################## +QVariant Tglobals::getVar(const QString& key) { + return config->value(key); +} + + void Tglobals::loadSettings(QSettings* cfg) { cfg->beginGroup("common"); isFirstRun = cfg->value("isFirstRun", true).toBool(); diff --git a/src/libs/core/tglobals.h b/src/libs/core/tglobals.h index 25962184e..257973c8d 100644 --- a/src/libs/core/tglobals.h +++ b/src/libs/core/tglobals.h @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2011-2015 by Tomasz Bojczuk * + * Copyright (C) 2011-2017 by Tomasz Bojczuk * * seelook@gmail.com * * * * This program is free software; you can redistribute it and/or modify * @@ -21,7 +21,8 @@ #define TGLOBALS_H #include <nootkacoreglobal.h> -#include <QtCore/qstring.h> +#include <QtCore/qobject.h> +#include <QtCore/qvariant.h> #include <QtGui/qcolor.h> #include <music/tnote.h> #include <music/tinstrument.h> @@ -34,15 +35,27 @@ class QSettings; class TexamParams; class TaudioParams; -class NOOTKACORE_EXPORT Tglobals +#define GLOB Tglobals::instance() + + +class NOOTKACORE_EXPORT Tglobals : public QObject { + Q_OBJECT public: /** If @p true, setting are loaded from temporary config file */ - Tglobals(); + Tglobals(QObject* parent = nullptr); ~Tglobals(); + /** + * Instance (single for whole Nootka) of Tglobals class. + * Also avail through @p GLOB macro + */ + static Tglobals* instance() { return m_instance; } + + Q_INVOKABLE QVariant getVar(const QString& key); + /** This method return application install path - path from where Nootka was started. */ static QString getInstPath(QString appInstPath); static QString& path; /**< Reference to Tpath::main - Nootka resources path */ @@ -112,8 +125,9 @@ public: TlayoutParams *L; /**< Main window Layout params. */ private: - Ttune *m_tune; /**< current guitar tune */ - qint8 m_order[6]; /**< Strings order is determined in @param setTune() method */ + Ttune *m_tune; /**< current guitar tune */ + qint8 m_order[6]; /**< Strings order is determined in @param setTune() method */ + static Tglobals *m_instance; }; #endif // TGLOBALS_H diff --git a/src/main.cpp b/src/main.cpp index 0a88b2922..9f42f7873 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -107,6 +107,7 @@ int main(int argc, char *argv[]) // creating main window e = new QQmlApplicationEngine; e->rootContext()->setContextProperty(QStringLiteral("Tpath"), &pathObj); + e->rootContext()->setContextProperty(QStringLiteral("GLOB"), GLOB); e->load(QUrl(QStringLiteral("qrc:/MainWindow.qml"))); // #if defined (Q_OS_ANDROID) diff --git a/src/qml/MainWindow.qml b/src/qml/MainWindow.qml index dceab9dd4..76e4fe041 100644 --- a/src/qml/MainWindow.qml +++ b/src/qml/MainWindow.qml @@ -23,13 +23,29 @@ import QtQuick.Window 2.0 ApplicationWindow { - id: qrabWindow + id: nootkaWindow visible: true title: "Nootka" - width: Screen.desktopAvailableWidth / 2 - height: Screen.desktopAvailableHeight / 2 + width: { + var r = GLOB.getVar("General/geometry") + return (r && r.width >= 720) ? r.width : Math.max(Screen.desktopAvailableWidth * 0.75, 720) + } + height: { + var r = GLOB.getVar("General/geometry") + return (r && r.height >= 480) ? r.height : Math.max(Screen.desktopAvailableHeight * 0.75, 480) + } + x: { + var r = GLOB.getVar("General/geometry") + return (r && r.x >= 0) ? r.x : (Screen.desktopAvailableWidth - nootkaWindow.width) / 2 + } + y: { + var r = GLOB.getVar("General/geometry") + return (r && r.y >= 0) ? r.y : (Screen.desktopAvailableHeight - nootkaWindow.height) / 2 + } header: TtoolBar {} + Component.onCompleted: {} + } -- GitLab