diff --git a/isettings.cpp b/isettings.cpp index 2f2cb60..b97e83f 100644 --- a/isettings.cpp +++ b/isettings.cpp @@ -11,6 +11,8 @@ namespace QuasarAppUtils { +ISettings* ISettings::_settings = nullptr; + ISettings::ISettings(SettingsSaveMode mode) { _mode = mode; } @@ -27,6 +29,10 @@ void ISettings::setMode(const SettingsSaveMode &mode) { _mode = mode; } +ISettings *ISettings::instance(){ + return _settings; +} + QVariant ISettings::getValue(const QString &key, const QVariant &def) { if (!_cache.contains(key)) { diff --git a/isettings.h b/isettings.h index b0347d0..bb8d197 100644 --- a/isettings.h +++ b/isettings.h @@ -9,7 +9,6 @@ #define ISETTINGS_H #include "quasarapp_global.h" -#include "service.h" #include <QObject> #include <QVariant> @@ -28,9 +27,19 @@ enum class SettingsSaveMode: quint64 { }; /** - * @brief The ISettings class This is base implementation of the settings object. usaly it is used as a service - * So see the ISettingsService class wrapper. - * @see ISettingsService + * @brief The Settings class base interface for implementation settings backends. + * Available implementations: + * Setting (based on QSettings backend) + * @note This is singleton object. + * + * @note The all child classes should be initialized before used. + * + * ``` + * auto settingsInstance = Setting::init<Setting>(); + * ``` + * + * @see ISettings::init method. + * */ class QUASARAPPSHARED_EXPORT ISettings : public QObject { @@ -38,6 +47,33 @@ class QUASARAPPSHARED_EXPORT ISettings : public QObject public: + /** + * @brief init This method return instance of the settings object and initialize new settings model if object not exists. + * @code{cpp} + auto settingsObject = ISettings::init<SettingsModelClass>(arg...) + * @endcode + * @return pointer to a settings object; + * @see ISettings::instance + */ + template <class SettingsType, class... Args> + static ISettings* init(Args&&... args) { + static_assert (std::is_base_of<ISettings, SettingsType>::value, + "the Settingstype type must be ISettings"); + + if(!_settings){ + _settings = new SettingsType(std::forward<Args>(args)...); + } + + return _settings; + } + + /** + * @brief instance This method return pointer to current settings model. if this model not initialized then return nullptr. + * @return pointer to current settings model if object initialized else nullptr. + * @see ISettings::init + */ + static ISettings *instance(); + /** * @brief getValue This method return the value of the settings. * @param key This is name of the required settings value. @@ -140,27 +176,10 @@ private: SettingsSaveMode _mode = SettingsSaveMode::Auto; QHash<QString, QVariant> _cache; + + static ISettings* _settings; }; - -/** - * @brief The ISettingsService class base interface for implementation settings backends. - * Available implementations: - * Setting (based on QSettings backend) - * @note This is singleton object. - * - * @note The all child classes should be initialized before used. - * - * ``` - * auto settingsInstance = Setting::init<Setting>(); - * ``` - * - * @see Service::init method. - * - */ -class QUASARAPPSHARED_EXPORT ISettingsService: public QuasarAppUtils::Service<ISettings> {}; - } ; - #endif // ISETTINGS_H diff --git a/locales.cpp b/locales.cpp index 7eb905f..3a11e1e 100644 --- a/locales.cpp +++ b/locales.cpp @@ -82,6 +82,21 @@ bool Locales::setLocalePrivate(const QLocale &locale) { return _translations.size(); } +const QLocale &Locales::currentLocate() { + auto obj = instance(); + return obj->currentLocatePrivate(); +} + +bool Locales::setLocale(const QLocale &locale) { + auto obj = instance(); + return obj->setLocalePrivate(locale); +} + +bool Locales::init(const QLocale &locale, const QSet<QString> & location) { + auto obj = instance(); + return obj->initPrivate(locale, location); +} + bool Locales::initPrivate(const QLocale &locale, const QSet<QString> & locations) { #if QT_VERSION <= QT_VERSION_CHECK(6, 0, 0) @@ -97,6 +112,11 @@ bool Locales::initPrivate(const QLocale &locale, const QSet<QString> & locations return setLocalePrivate(locale); } +Locales *Locales::instance() { + static auto instance = new Locales(); + return instance; +} + void Locales::removeOldTranslation() { for (const auto & tr :qAsConst(_translations)) { QCoreApplication::removeTranslator(tr); @@ -113,44 +133,11 @@ const QLocale &Locales::currentLocatePrivate() const { return _currentLocate; } - -bool Locales::setLocale(const QLocale &locale) { - return setLocalePrivate(locale); -} - -bool Locales::init(const QLocale &locale, const QSet<QString> &location) { - return initPrivate(locale, location); -} - void Locales::addLocation(const QString &location) { - return addLocationPrivate(location); + auto obj = instance(); + obj->addLocationPrivate(location); } -const QLocale &Locales::currentLocate() { - return currentLocatePrivate(); -} - - Locales::~Locales() { removeOldTranslation(); } - -void LocalesService::addLocation(const QString &location) { - auto obj = instance(); - obj->addLocation(location); -} - -const QLocale &LocalesService::currentLocate() { - auto obj = instance(); - return obj->currentLocate(); -} - -bool LocalesService::setLocale(const QLocale &locale) { - auto obj = instance(); - return obj->setLocale(locale); -} - -bool LocalesService::init(const QLocale &locale, const QSet<QString> & location) { - auto obj = instance(); - return obj->init(locale, location); -} diff --git a/locales.h b/locales.h index 0ec2533..b1977dd 100644 --- a/locales.h +++ b/locales.h @@ -10,7 +10,6 @@ #define LOCALES_H #include "quasarapp_global.h" -#include "service.h" #include <QLocale> #include <QSet> @@ -47,7 +46,7 @@ public: * @param locale This is new locale. * @return true if the all ltranstations files loaded successful. */ - bool setLocale(const QLocale &locale); + static bool setLocale(const QLocale &locale); /** * @brief init This method initialize translation of applictaion. @@ -55,20 +54,26 @@ public: * @param location Path to folder with qm files. example (:/tr). * @return return true if locale set for application. */ - bool init(const QLocale &locale = QLocale::system(), - const QSet<QString> & location = {}); + static bool init(const QLocale &locale = QLocale::system(), + const QSet<QString> & location = {}); /** * @brief addLocation This method add location for qm files. Use This method if you create a own library with translations supports. * @param location This is a new location of the qm files. */ - void addLocation(const QString& location); + static void addLocation(const QString& location); + + /** + * @brief instance This method return pointer to the Locales service. + * @return return pointer to the Locales static object + */ + static Locales *instance(); /** * @brief currentLocate This method return current locate of applicatuon. * @return current or last sets locate of applciation. */ - const QLocale ¤tLocate(); + static const QLocale ¤tLocate(); signals: /** @@ -96,43 +101,6 @@ private: QList<QTranslator *> _translations; }; - - -/** - * @brief SettingsService This is Locales service object. - * @see Service - * @see Locales - */ -class QUASARAPPSHARED_EXPORT LocalesService: public Service<Locales> { - /** - * @brief setLocale This method sets locale for application and loaded all translations for this locale. - * @param locale This is new locale. - * @return true if the all ltranstations files loaded successful. - */ - static bool setLocale(const QLocale &locale); - - /** - * @brief init This method initialize translation of applictaion. - * @param locale See info about QLocale. - * @param location Path to folder with qm files. example (:/tr). - * @return return true if locale set for application. - */ - static bool init(const QLocale &locale = QLocale::system(), - const QSet<QString> & location = {}); - - /** - * @brief addLocation This method add location for qm files. Use This method if you create a own library with translations supports. - * @param location This is a new location of the qm files. - */ - static void addLocation(const QString& location); - - /** - * @brief currentLocate This method return current locate of applicatuon. - * @return current or last sets locate of applciation. - */ - static const QLocale ¤tLocate(); -}; - } diff --git a/service.cpp b/service.cpp deleted file mode 100644 index a869b25..0000000 --- a/service.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include "service.h" - - diff --git a/service.h b/service.h deleted file mode 100644 index cf64572..0000000 --- a/service.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (C) 2018-2022 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 QA_SERVICE_H -#define QA_SERVICE_H - -#include "quasarapp_global.h" - - -namespace QuasarAppUtils { - -/** - * @brief The ServicePrivate struct This is a waraper for initialize the private pointers of the service instance. - * @see Service class. - */ -struct ServicePrivate { - void* ptr = nullptr; -}; - -/** - * @brief The Service class is a template class for creating a singleton services objects. - * This is manual control wrapper. You should be manually initializing your service object and manually deinitializing. - * If you don't destroy your service, then service object will be automatically destroyed when application will be closed. - * @warning If service was destroyed automatically, then destructor of your base class will be not invoked. Use The deinit method for this. - * @todo Remove the template Base class. Instead, it needs to use a general inherit paradigm - */ -template<class Base> -class Service -{ -public: - Service(); - - /** - * @brief init This method initialize the @a Base object as a service. - * @param args This is argumets of a constructo of the @a Base class. - * @return instance pointer. If the service alredy initialized then return pointer to current service object. - */ - template<class BaseClass = Base, class... Args> - static Base* init(Args&&... args) { - - if(!_data.ptr){ - _data = new BaseClass(std::forward<Args>(args)...); - } - - return static_cast<Base*>(_data.ptr); - } - - /** - * @brief instance This method return pointerer to current service object. - * @note If object was not initialized, then return false. - * @return pointerer to current service object if service initialized else nullptr. - */ - static Base* instance() { - return static_cast<Base*>(_data.ptr); - } - - /** - * @brief deinit This is distructor method for the service. - * @note do nothink if this object alredy distroyed. - */ - static void deinit() { - if (_data.ptr) { - delete static_cast<Base*>(_data.ptr); - _data.ptr = nullptr; - } - } - -private: - static ServicePrivate _data; - -}; - - -} -#endif // QA_SERVICE_H diff --git a/settings.cpp b/settings.cpp index 65cc2af..f681307 100644 --- a/settings.cpp +++ b/settings.cpp @@ -26,6 +26,10 @@ Settings::Settings() { _settings = new QSettings(QSettings::IniFormat, QSettings::Scope::UserScope, company, name); } +ISettings *Settings::init() { + return ISettings::init<Settings>(); +} + void Settings::syncImplementation() { return _settings->sync(); } diff --git a/settings.h b/settings.h index c1dd652..10ac93a 100644 --- a/settings.h +++ b/settings.h @@ -25,14 +25,22 @@ namespace QuasarAppUtils { * * @see Settings::init */ -class QUASARAPPSHARED_EXPORT Settings: public ISettingsService +class QUASARAPPSHARED_EXPORT Settings: public ISettings { public: Settings(); + /** + * @brief init This is simple wrapper of the Settings::init method for convenient access to initialisation. + * @return instance of the setting. + */ + static ISettings* init(); + // ISettings interface protected: + + void syncImplementation(); QVariant getValueImplementation(const QString &key, const QVariant &def); void setValueImplementation(const QString key, const QVariant &value); @@ -44,5 +52,4 @@ private: } - #endif // SETTINGS_H diff --git a/settingslistner.cpp b/settingslistner.cpp index 5221849..0cb90ad 100644 --- a/settingslistner.cpp +++ b/settingslistner.cpp @@ -12,7 +12,7 @@ namespace QuasarAppUtils { SettingsListner::SettingsListner() { - auto settings = ISettingsService::instance(); + auto settings = ISettings::instance(); if (settings) { auto listner = [this](QString key, QVariant val){