diff --git a/Tuner.pro b/Tuner.pro index 8085f9a..7e34ae3 100644 --- a/Tuner.pro +++ b/Tuner.pro @@ -9,6 +9,7 @@ PKGCONFIG += libpulse-simple SOURCES += \ src/desktop.cpp \ + src/ObjectSaver.cpp \ src/PitchDetection.cpp \ src/Tuner.cpp \ src/TunerWorker.cpp \ @@ -19,6 +20,7 @@ SOURCES += \ HEADERS += \ src/PitchDetection.hpp \ + src/ObjectSaver.hpp \ src/Tuner.hpp \ src/TunerWorker.hpp \ src/audio/LinearFilter.hpp \ diff --git a/harbour-sailtuner.pro b/harbour-sailtuner.pro index 934c233..12ec1ea 100644 --- a/harbour-sailtuner.pro +++ b/harbour-sailtuner.pro @@ -14,6 +14,7 @@ RESOURCES += \ SOURCES += \ src/sailfish.cpp \ src/PitchDetection.cpp \ + src/ObjectSaver.cpp \ src/Tuner.cpp \ src/TunerWorker.cpp \ src/audio/LinearFilter.cpp \ @@ -23,6 +24,7 @@ SOURCES += \ HEADERS += \ src/PitchDetection.hpp \ + src/ObjectSaver.hpp \ src/Tuner.cpp \ src/Tuner.hpp \ src/TunerWorker.hpp \ diff --git a/qml/Config.qml b/qml/Config.qml new file mode 100644 index 0000000..44f87d5 --- /dev/null +++ b/qml/Config.qml @@ -0,0 +1,9 @@ +import QtQuick 2.0 + +pragma Singleton + +QtObject { + property double la: 441 + property int temperament_idx: 1 + property int notes_style: 0 +} diff --git a/qml/Desktop.qml b/qml/Desktop.qml index 84cf56a..76d92e9 100644 --- a/qml/Desktop.qml +++ b/qml/Desktop.qml @@ -17,6 +17,9 @@ import QtQuick 2.0 import LJTuner 1.0 +import LJUtils 1.0 + +import "." Item { width: 600 @@ -25,6 +28,13 @@ Item { Tuner { id: tuner running: false + temperament_idx: Config.temperament_idx + la: Config.la + } + + ObjectSaver { + filename: "config.dat" + object: Config } DesktopTheme { diff --git a/qml/Sailfish.qml b/qml/Sailfish.qml index e0be184..68b6221 100644 --- a/qml/Sailfish.qml +++ b/qml/Sailfish.qml @@ -18,6 +18,8 @@ import QtQuick 2.0 import Sailfish.Silica 1.0 import harbour.sailtuner.tuner 1.0 +import harbour.sailtuner.objectsaver 1.0 +import "." /** * Sailfish main page @@ -32,6 +34,11 @@ ApplicationWindow { property int dbFontSize: 100 property QtObject tuner + ObjectSaver { + filename: "config.dat" + object: Config + } + initialPage: Component { Page { id: page @@ -52,6 +59,8 @@ ApplicationWindow { Tuner { id: tunerObject running: Qt.application.active && app.userRunning + temperament_idx: Config.temperament_idx + la: Config.la } MouseArea { diff --git a/qml/ScaleToise.qml b/qml/ScaleToise.qml index 7ef1ffb..7937139 100644 --- a/qml/ScaleToise.qml +++ b/qml/ScaleToise.qml @@ -16,6 +16,7 @@ */ import QtQuick 2.0 +import "." /** * ScaleToise @@ -31,7 +32,7 @@ Toise { // octave property int octave: 4 // en or fr - property int notes_style: 1 + property int notes_style: Config.notes_style property variant notes_fr: [ "do", "do#", "ré", "mib", "mi", "fa", "fa#", "sol", "sol#", "la", "sib", "si"] diff --git a/qml/desktop.qrc b/qml/desktop.qrc index 6a386f6..f9113ad 100644 --- a/qml/desktop.qrc +++ b/qml/desktop.qrc @@ -8,5 +8,7 @@ ScaleToise.qml Toise.qml Led.qml +Config.qml +qmldir diff --git a/qml/qmldir b/qml/qmldir new file mode 100644 index 0000000..126440d --- /dev/null +++ b/qml/qmldir @@ -0,0 +1 @@ +singleton Config Config.qml diff --git a/src/ObjectSaver.cpp b/src/ObjectSaver.cpp new file mode 100644 index 0000000..a1e71be --- /dev/null +++ b/src/ObjectSaver.cpp @@ -0,0 +1,112 @@ +/* Copyright 2016 (C) Louis-Joseph Fournier + * louisjoseph.fournier@gmail.com + * + * This file is part of SailTuner. + * + * SailTuner is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * SailTuner 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 General Public License for more details. + * + */ + +#include +#include +#include +#include + +#include "ObjectSaver.hpp" + +#define name_(x) #x +#define name(x) name_(x) +#define NAME name(TARGET) + +ObjectSaver::ObjectSaver() : + object(NULL) +{ + +} + +ObjectSaver::~ObjectSaver() +{ +} + +QString ObjectSaver::getFilename() +{ + return filename; +} + +void ObjectSaver::setFilename(QString name) +{ + filename = name; + if (object && filename != "") load(); +} + +QObject * ObjectSaver::getObject() +{ + return object; +} + +void ObjectSaver::setObject(QObject *obj) +{ + object = obj; + if (filename != "") load(); +} + +/// Get the data file object opened, or NULL +QFile * ObjectSaver::getDataFile(bool is_write) +{ + if (filename == "") { + qDebug() << __func__ << "filename empty"; + return NULL; + } + if (!object) { + qDebug() << __func__ << "object null"; + return NULL; + } + QString qDataDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation); + if (qDataDir == "") { + qDebug() << __func__ << "config path empty. abort"; + return NULL; + } + if (is_write) { + QDir dir(qDataDir); + dir.mkpath(NAME); + } + QString path = qDataDir + "/" + NAME + "/" + filename; + + QFile *file = new QFile(path); + + if (!is_write && !file->exists()) { + qDebug() << __func__ << path << " don't exist"; + delete file; + return NULL; + } + if (!file->open(is_write ? (QIODevice::WriteOnly | QIODevice::Truncate) : QIODevice::ReadOnly)) { + qDebug() << __func__ << "file not opened"; + delete file; + return NULL; + } + return file; +} + +void ObjectSaver::load() +{ + QFile *file = getDataFile(false); + if (!file) return; + file->close(); + delete file; +} + +void ObjectSaver::save() +{ + QFile *file = getDataFile(true); + if (!file) return; + file->close(); + delete file; +} diff --git a/src/ObjectSaver.hpp b/src/ObjectSaver.hpp new file mode 100644 index 0000000..eeebc2d --- /dev/null +++ b/src/ObjectSaver.hpp @@ -0,0 +1,63 @@ +/* Copyright 2016 (C) Louis-Joseph Fournier + * louisjoseph.fournier@gmail.com + * + * This file is part of SailTuner. + * + * SailTuner is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * SailTuner 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 General Public License for more details. + * + */ + +#ifndef _OBJECT_SAVER_HPP +#define _OBJECT_SAVER_HPP + +#include +#include + +/** + * Class to load/save qml objects simply, + * with limitations: + * - only 1 depth properties + * - double, int, string only (for the moment) + */ +class ObjectSaver : public QObject { + Q_OBJECT + + /// object to load/save + Q_PROPERTY(QObject* object READ getObject WRITE setObject NOTIFY objectChanged) + /// name of save file + Q_PROPERTY(QString filename READ getFilename WRITE setFilename) + + private: + QObject *object; + QString filename; + + QFile * getDataFile(bool is_write); + + public: + ObjectSaver(); + ~ObjectSaver(); + + QString getFilename(); + void setFilename(QString name); + QObject* getObject(); + void setObject(QObject *obj); + + public slots: + /// load the object. auto called if object and filename setted + void load(); + /// save object to disk + void save(); + + signals: + void objectChanged(); +}; + +#endif diff --git a/src/desktop.cpp b/src/desktop.cpp index 35b041b..7ce3ed5 100644 --- a/src/desktop.cpp +++ b/src/desktop.cpp @@ -22,6 +22,7 @@ #include #include +#include "ObjectSaver.hpp" #include "PitchDetection.hpp" #include "Tuner.hpp" @@ -33,6 +34,7 @@ Q_DECL_EXPORT int main(int argc, char* argv[]) } qmlRegisterType("LJTuner", 1, 0, "Tuner"); + qmlRegisterType("LJUtils", 1, 0, "ObjectSaver"); QGuiApplication app(argc, argv); QQuickView view; diff --git a/src/sailfish.cpp b/src/sailfish.cpp index 768464f..f0239dd 100644 --- a/src/sailfish.cpp +++ b/src/sailfish.cpp @@ -53,6 +53,8 @@ Q_DECL_EXPORT int main(int argc, char* argv[]) } qmlRegisterType("harbour.sailtuner.tuner", 1, 0, "Tuner"); + qmlRegisterType("harbour.sailtuner.objectsaver", 1, 0, "ObjectSaver"); + Main *appli = new Main(argc, argv); return appli->Launch(); }