mirror of
https://github.com/QuasarApp/QuasarAppLib.git
synced 2025-04-29 11:14:39 +00:00
Revert "added the template service class"
This reverts commit 803870ea08adcea3a94e57e64a496447c05d55e0.
This commit is contained in:
parent
aa4e7971bb
commit
85185d2430
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace QuasarAppUtils {
|
namespace QuasarAppUtils {
|
||||||
|
|
||||||
|
ISettings* ISettings::_settings = nullptr;
|
||||||
|
|
||||||
ISettings::ISettings(SettingsSaveMode mode) {
|
ISettings::ISettings(SettingsSaveMode mode) {
|
||||||
_mode = mode;
|
_mode = mode;
|
||||||
}
|
}
|
||||||
@ -27,6 +29,10 @@ void ISettings::setMode(const SettingsSaveMode &mode) {
|
|||||||
_mode = mode;
|
_mode = mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ISettings *ISettings::instance(){
|
||||||
|
return _settings;
|
||||||
|
}
|
||||||
|
|
||||||
QVariant ISettings::getValue(const QString &key, const QVariant &def) {
|
QVariant ISettings::getValue(const QString &key, const QVariant &def) {
|
||||||
|
|
||||||
if (!_cache.contains(key)) {
|
if (!_cache.contains(key)) {
|
||||||
|
65
isettings.h
65
isettings.h
@ -9,7 +9,6 @@
|
|||||||
#define ISETTINGS_H
|
#define ISETTINGS_H
|
||||||
|
|
||||||
#include "quasarapp_global.h"
|
#include "quasarapp_global.h"
|
||||||
#include "service.h"
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QVariant>
|
#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
|
* @brief The Settings class base interface for implementation settings backends.
|
||||||
* So see the ISettingsService class wrapper.
|
* Available implementations:
|
||||||
* @see ISettingsService
|
* 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
|
class QUASARAPPSHARED_EXPORT ISettings : public QObject
|
||||||
{
|
{
|
||||||
@ -38,6 +47,33 @@ class QUASARAPPSHARED_EXPORT ISettings : public QObject
|
|||||||
|
|
||||||
public:
|
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.
|
* @brief getValue This method return the value of the settings.
|
||||||
* @param key This is name of the required settings value.
|
* @param key This is name of the required settings value.
|
||||||
@ -140,27 +176,10 @@ private:
|
|||||||
SettingsSaveMode _mode = SettingsSaveMode::Auto;
|
SettingsSaveMode _mode = SettingsSaveMode::Auto;
|
||||||
|
|
||||||
QHash<QString, QVariant> _cache;
|
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
|
#endif // ISETTINGS_H
|
||||||
|
57
locales.cpp
57
locales.cpp
@ -82,6 +82,21 @@ bool Locales::setLocalePrivate(const QLocale &locale) {
|
|||||||
return _translations.size();
|
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) {
|
bool Locales::initPrivate(const QLocale &locale, const QSet<QString> & locations) {
|
||||||
|
|
||||||
#if QT_VERSION <= QT_VERSION_CHECK(6, 0, 0)
|
#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);
|
return setLocalePrivate(locale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Locales *Locales::instance() {
|
||||||
|
static auto instance = new Locales();
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
void Locales::removeOldTranslation() {
|
void Locales::removeOldTranslation() {
|
||||||
for (const auto & tr :qAsConst(_translations)) {
|
for (const auto & tr :qAsConst(_translations)) {
|
||||||
QCoreApplication::removeTranslator(tr);
|
QCoreApplication::removeTranslator(tr);
|
||||||
@ -113,44 +133,11 @@ const QLocale &Locales::currentLocatePrivate() const {
|
|||||||
return _currentLocate;
|
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) {
|
void Locales::addLocation(const QString &location) {
|
||||||
return addLocationPrivate(location);
|
auto obj = instance();
|
||||||
|
obj->addLocationPrivate(location);
|
||||||
}
|
}
|
||||||
|
|
||||||
const QLocale &Locales::currentLocate() {
|
|
||||||
return currentLocatePrivate();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Locales::~Locales() {
|
Locales::~Locales() {
|
||||||
removeOldTranslation();
|
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);
|
|
||||||
}
|
|
||||||
|
52
locales.h
52
locales.h
@ -10,7 +10,6 @@
|
|||||||
#define LOCALES_H
|
#define LOCALES_H
|
||||||
|
|
||||||
#include "quasarapp_global.h"
|
#include "quasarapp_global.h"
|
||||||
#include "service.h"
|
|
||||||
|
|
||||||
#include <QLocale>
|
#include <QLocale>
|
||||||
#include <QSet>
|
#include <QSet>
|
||||||
@ -47,7 +46,7 @@ public:
|
|||||||
* @param locale This is new locale.
|
* @param locale This is new locale.
|
||||||
* @return true if the all ltranstations files loaded successful.
|
* @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.
|
* @brief init This method initialize translation of applictaion.
|
||||||
@ -55,20 +54,26 @@ public:
|
|||||||
* @param location Path to folder with qm files. example (:/tr).
|
* @param location Path to folder with qm files. example (:/tr).
|
||||||
* @return return true if locale set for application.
|
* @return return true if locale set for application.
|
||||||
*/
|
*/
|
||||||
bool init(const QLocale &locale = QLocale::system(),
|
static bool init(const QLocale &locale = QLocale::system(),
|
||||||
const QSet<QString> & location = {});
|
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.
|
* @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.
|
* @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.
|
* @brief currentLocate This method return current locate of applicatuon.
|
||||||
* @return current or last sets locate of applciation.
|
* @return current or last sets locate of applciation.
|
||||||
*/
|
*/
|
||||||
const QLocale ¤tLocate();
|
static const QLocale ¤tLocate();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
/**
|
/**
|
||||||
@ -96,43 +101,6 @@ private:
|
|||||||
QList<QTranslator *> _translations;
|
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();
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
#include "service.h"
|
|
||||||
|
|
||||||
|
|
79
service.h
79
service.h
@ -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
|
|
@ -26,6 +26,10 @@ Settings::Settings() {
|
|||||||
_settings = new QSettings(QSettings::IniFormat, QSettings::Scope::UserScope, company, name);
|
_settings = new QSettings(QSettings::IniFormat, QSettings::Scope::UserScope, company, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ISettings *Settings::init() {
|
||||||
|
return ISettings::init<Settings>();
|
||||||
|
}
|
||||||
|
|
||||||
void Settings::syncImplementation() {
|
void Settings::syncImplementation() {
|
||||||
return _settings->sync();
|
return _settings->sync();
|
||||||
}
|
}
|
||||||
|
11
settings.h
11
settings.h
@ -25,14 +25,22 @@ namespace QuasarAppUtils {
|
|||||||
*
|
*
|
||||||
* @see Settings::init
|
* @see Settings::init
|
||||||
*/
|
*/
|
||||||
class QUASARAPPSHARED_EXPORT Settings: public ISettingsService
|
class QUASARAPPSHARED_EXPORT Settings: public ISettings
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Settings();
|
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
|
// ISettings interface
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void syncImplementation();
|
void syncImplementation();
|
||||||
QVariant getValueImplementation(const QString &key, const QVariant &def);
|
QVariant getValueImplementation(const QString &key, const QVariant &def);
|
||||||
void setValueImplementation(const QString key, const QVariant &value);
|
void setValueImplementation(const QString key, const QVariant &value);
|
||||||
@ -44,5 +52,4 @@ private:
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif // SETTINGS_H
|
#endif // SETTINGS_H
|
||||||
|
@ -12,7 +12,7 @@ namespace QuasarAppUtils {
|
|||||||
|
|
||||||
SettingsListner::SettingsListner() {
|
SettingsListner::SettingsListner() {
|
||||||
|
|
||||||
auto settings = ISettingsService::instance();
|
auto settings = ISettings::instance();
|
||||||
if (settings) {
|
if (settings) {
|
||||||
|
|
||||||
auto listner = [this](QString key, QVariant val){
|
auto listner = [this](QString key, QVariant val){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user