From bc6b0275ce7f8a27e3282647a8f5b8f049d12b08 Mon Sep 17 00:00:00 2001 From: EndrII Date: Sat, 29 Sep 2018 15:56:04 +0300 Subject: [PATCH] version2 --- QuasarApp.pro | 17 +++++------ locales.cpp | 31 ++++++++++++++++++++ locales.h | 40 ++++++++++++++++++++++++++ params.cpp | 47 +++++++++++++++++++++++++++++++ params.h | 57 +++++++++++++++++++++++++++++++++++++ quasarapp.cpp | 51 --------------------------------- quasarapp.h | 54 ++++------------------------------- settings.cpp | 45 +++++++++++++++++++++++++++++ settings.h | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ targetdir.pri | 26 +++++++++++++++++ 10 files changed, 338 insertions(+), 108 deletions(-) create mode 100644 locales.cpp create mode 100644 locales.h create mode 100644 params.cpp create mode 100644 params.h create mode 100644 settings.cpp create mode 100644 settings.h create mode 100755 targetdir.pri diff --git a/QuasarApp.pro b/QuasarApp.pro index 8e0d9dd..6c25748 100644 --- a/QuasarApp.pro +++ b/QuasarApp.pro @@ -22,19 +22,20 @@ DEFINES += QT_DEPRECATED_WARNINGS # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 - -CONFIG(debug, debug|release): { - DESTDIR = "$$PWD/build/debug" -} else: { - DESTDIR = "$$PWD/build/release" -} +include(targetdir.pri) SOURCES += \ - quasarapp.cpp + quasarapp.cpp \ + params.cpp \ + locales.cpp \ + settings.cpp HEADERS += \ quasarapp.h \ - quasarapp_global.h + quasarapp_global.h \ + params.h \ + locales.h \ + settings.h unix { target.path = /usr/lib diff --git a/locales.cpp b/locales.cpp new file mode 100644 index 0000000..039114e --- /dev/null +++ b/locales.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2018 QuasarApp. + * Distributed under the lgplv3 software license, see the accompanying + * Everyone is permitted to copy and distribute verbatim copies + * of this license document, but changing it is not allowed. +*/ + + +#include "locales.h" + +#include +#include +#include + +using namespace QuasarAppUtils; + +bool Locales::initLocale(const QString &locale, QCoreApplication *app, QTranslator *translator){ + + QString defaultLocale = QLocale::system().name(); + defaultLocale.truncate(defaultLocale.lastIndexOf('_')); + + if(!locale.isEmpty() && translator->load(QString(":/languages/%0").arg(locale))){ + return app->installTranslator(translator); + } + + if(!translator->load(QString(":/languages/%0").arg(defaultLocale))){ + return false; + } + + return app->installTranslator(translator); +} diff --git a/locales.h b/locales.h new file mode 100644 index 0000000..7245fb9 --- /dev/null +++ b/locales.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2018 QuasarApp. + * Distributed under the lgplv3 software license, see the accompanying + * Everyone is permitted to copy and distribute verbatim copies + * of this license document, but changing it is not allowed. +*/ + + +#ifndef LOCALES_H +#define LOCALES_H + +#include "quasarapp_global.h" + +class QCoreApplication; +class QTranslator; + +namespace QuasarAppUtils { + +/** + * @brief The Locales class for parese local files + */ +class QUASARAPPSHARED_EXPORT Locales +{ +public: + Locales() = delete; + + /** + * @brief initLocale init translation of applictaion + * @param locale - string value of locale. example (en) + * @param app - app core of qt + * @param translator - translator core of qt + * @return return true if locale funded + */ + static bool initLocale(const QString &locale, QCoreApplication* app, QTranslator *translator); + +}; +} + + +#endif // LOCALES_H diff --git a/params.cpp b/params.cpp new file mode 100644 index 0000000..f31976b --- /dev/null +++ b/params.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2018 QuasarApp. + * Distributed under the lgplv3 software license, see the accompanying + * Everyone is permitted to copy and distribute verbatim copies + * of this license document, but changing it is not allowed. +*/ + +#include "params.h" +#include +#include + +using namespace QuasarAppUtils; + +static QVariantMap params = QVariantMap(); + + +bool Params::isEndable(const QString& key) { + return params.contains(key); +} + +bool Params::parseParams(int argc, char *argv[]) { + params.clear(); + params ["appPath"] = argv[0]; + + for (int i = 1; i < argc; i++) { + if (argv[i][0] == '-' && i ) { + if (i < (argc - 1) && argv[i + 1][0] != '-') { + params[&(argv[i][1])] = argv[i + 1]; + i++; + } else { + qWarning() << "Missing argument for " + QString(argv[i]) ; + } + } else { + params[argv[i]] = ""; + } + } + + return true; +} + +QString Params::getStrArg(const QString& key) { + return params.value(key, "").toString(); +} + +QVariant Params::getArg(const QString& key) { + return params.value(key, ""); +} diff --git a/params.h b/params.h new file mode 100644 index 0000000..1e55512 --- /dev/null +++ b/params.h @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2018 QuasarApp. + * Distributed under the lgplv3 software license, see the accompanying + * Everyone is permitted to copy and distribute verbatim copies + * of this license document, but changing it is not allowed. +*/ + +#ifndef PARAMS_H +#define PARAMS_H + +#include +#include "quasarapp_global.h" + + +namespace QuasarAppUtils { + +/** + * @brief The Params class for parese app params + */ + +class QUASARAPPSHARED_EXPORT Params +{ +public: + Params() = delete; + + /** + * @brief parseParams - parase input data of started application + * @param argc - count of arguments + * @param argv - arrat of arguments + * @return true if all arguments read else false + */ + static bool parseParams(int argc, char *argv[]); + + /** + * @brief getStrArg - get string value of key + * @param key + * @return string value of argument + */ + static QString getStrArg(const QString& key); + + /** + * @brief getArg - get string value of key + * @param key + * @return string value of argument + */ + static QVariant getArg(const QString& key); + + /** + * @brief isEndable - check if enable argument of key + * @param key + * @return true if argument enabled + */ + static bool isEndable(const QString& key); +}; +} + +#endif // PARAMS_H diff --git a/quasarapp.cpp b/quasarapp.cpp index 5d24337..acf7bb6 100644 --- a/quasarapp.cpp +++ b/quasarapp.cpp @@ -6,57 +6,6 @@ */ #include "quasarapp.h" -#include -#include -#include -#include -static QVariantMap params = QVariantMap(); -bool QuasarAppUtils::parseParams(int argc, char *argv[]) { - params.clear(); - params ["appPath"] = argv[0]; - for (int i = 1; i < argc; i++) { - if (argv[i][0] == '-' && i ) { - if (i < (argc - 1) && argv[i + 1][0] != '-') { - params[&(argv[i][1])] = argv[i + 1]; - i++; - } else { - qDebug() << "Missing argument for " + QString(argv[i]) ; - } - } else { - params[argv[i]] = ""; - } - } - - return true; -} - -QString QuasarAppUtils::getStrArg(const QString& key) { - return params.value(key, "").toString(); -} - -QVariant QuasarAppUtils::getArg(const QString& key) { - return params.value(key, ""); -} - -bool QuasarAppUtils::isEndable(const QString& key) { - return params.contains(key); -} - -bool initLocale(const QString &locale, QCoreApplication *app, QTranslator *translator){ - - QString defaultLocale = QLocale::system().name(); - defaultLocale.truncate(defaultLocale.lastIndexOf('_')); - - if(!locale.isEmpty() && translator->load(QString(":/languages/%0").arg(locale))){ - return app->installTranslator(translator); - } - - if(!translator->load(QString(":/languages/%0").arg(defaultLocale))){ - return false; - } - - return app->installTranslator(translator); -} diff --git a/quasarapp.h b/quasarapp.h index 4430d24..13babfb 100644 --- a/quasarapp.h +++ b/quasarapp.h @@ -8,61 +8,17 @@ #ifndef QUASARAAPP_H #define QUASARAAPP_H -#include "quasarapp_global.h" -#include - - -class QGuiApplication; -class QTranslator; +#include "params.h" +#include "locales.h" +#include "settings.h" /** * @brief The QuasaraAppUtils class * this lib include base functions for the all applications of QuasarApp group. * all methods of the Quasar AppUtils is static */ -class QUASARAPPSHARED_EXPORT QuasarAppUtils -{ +namespace QuasarAppUtils{ -public: - QuasarAppUtils() = delete; - - /** - * @brief parseParams - parase input data of started application - * @param argc - count of arguments - * @param argv - arrat of arguments - * @return true if all arguments read else false - */ - static bool parseParams(int argc, char *argv[]); - - /** - * @brief getStrArg - get string value of key - * @param key - * @return string value of argument - */ - static QString getStrArg(const QString& key); - - /** - * @brief getArg - get string value of key - * @param key - * @return string value of argument - */ - static QVariant getArg(const QString& key); - - /** - * @brief isEndable - check if enable argument of key - * @param key - * @return true if argument enabled - */ - static bool isEndable(const QString& key); - - /** - * @brief initLocale init translation of applictaion - * @param locale - string value of locale. example (en) - * @param app - app core of qt - * @param translator - translator core of qt - * @return return true if locale funded - */ - static bool initLocale(const QString &locale, QGuiApplication* app, QTranslator *translator); -}; +} ; #endif // QUASARAAPP_H diff --git a/settings.cpp b/settings.cpp new file mode 100644 index 0000000..10d4f2d --- /dev/null +++ b/settings.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2018 QuasarApp. + * Distributed under the lgplv3 software license, see the accompanying + * Everyone is permitted to copy and distribute verbatim copies + * of this license document, but changing it is not allowed. +*/ + +#include "settings.h" +#include + +using namespace QuasarAppUtils; + +Settings::Settings() { + _settings = new QSettings(); +} + +Settings *Settings::get() { + static Settings* res = new Settings(); + return res; +} + +const Settings *Settings::getConst() { + static Settings* res = new Settings(); + return res; +} + +QVariant Settings::getValue(const QString &key, const QVariant &def) { + return _settings->value(key, def); +} + +QString Settings::getStrValue(const QString &key, const QString &def) { + return getValue(key, QVariant(def)).toString(); +} + +void Settings::setValue(const QString key, const QVariant &value) { + _settings->setValue(key, value); + + emit valueChanged(key, value); + emit valueStrChanged(key, value.toString()); + +} + +void Settings::setStrValue(const QString &key, const QString &value) { + setValue(key, value); +} diff --git a/settings.h b/settings.h new file mode 100644 index 0000000..959166f --- /dev/null +++ b/settings.h @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2018 QuasarApp. + * Distributed under the lgplv3 software license, see the accompanying + * Everyone is permitted to copy and distribute verbatim copies + * of this license document, but changing it is not allowed. +*/ + +#ifndef SETTINGS_H +#define SETTINGS_H + +#include "quasarapp_global.h" +#include +#include + +class QSettings; + +namespace QuasarAppUtils { + +/** + * @brief The Settings class - singleton for QSettings + */ +class QUASARAPPSHARED_EXPORT Settings : public QObject +{ + Q_OBJECT +private: + explicit Settings(); + QSettings *_settings; + +public: + + /** + * @brief get + * @return object of all settings app; + */ + static Settings* get(); + + /** + * @brief get + * @return const object of all settings app; + */ + static const Settings* getConst(); + + /** + * @brief getValue + * @param key - key of value + * @param def - default value if is value not finded + * @return value of key + */ + QVariant getValue(const QString &key, const QVariant& def); + + /** + * @brief getStrValue some as getValue but work with QString + */ + QString getStrValue(const QString &key, const QString& def); + +public slots: + /** + * @brief setValue - set new value of key + * @param key - key pf settings + * @param value - new value + */ + void setValue(const QString key, const QVariant& value); + + /** + * @brief setStrValue - some as setValue< but use QString + */ + void setStrValue(const QString& key, const QString& value); + + +signals: + void valueChanged(QString key, QVariant value); + void valueStrChanged(QString key, QString value); + +}; +} ; + + +#endif // SETTINGS_H diff --git a/targetdir.pri b/targetdir.pri new file mode 100755 index 0000000..2c21bf1 --- /dev/null +++ b/targetdir.pri @@ -0,0 +1,26 @@ + +isEmpty( MAIN_PWD ) { + MAIN_PWD=$$PWD + warning(you use targetdir but not added MAIN_PWD=\$\$PWD into main pro file) +} + +equals( TEMPLATE, app) { + + isEmpty( TARGET_PATH_APP ) { + + DESTDIR = $$MAIN_PWD/build + message("$$TARGET use default ($$DESTDIR) TARGET PATH. if you want changed path then set TARGET_PATH_APP in main pro file") + } else { + DESTDIR = $$TARGET_PATH_APP + } +} + +equals( TEMPLATE, lib) { + isEmpty( TARGET_PATH_LIB ) { + + DESTDIR = $$MAIN_PWD/build + message("$$TARGET use default ($$DESTDIR) TARGET PATH. if you want changed path then set TARGET_PATH_LIB in main pro file") + } else { + DESTDIR = $$TARGET_PATH_LIB + } +}