mirror of
https://github.com/QuasarApp/QuasarAppLib.git
synced 2025-05-02 04:29:41 +00:00
version2
This commit is contained in:
parent
920ca5e046
commit
bc6b0275ce
@ -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
|
||||
|
31
locales.cpp
Normal file
31
locales.cpp
Normal file
@ -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 <QCoreApplication>
|
||||
#include <QTranslator>
|
||||
#include <QLocale>
|
||||
|
||||
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);
|
||||
}
|
40
locales.h
Normal file
40
locales.h
Normal file
@ -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
|
47
params.cpp
Normal file
47
params.cpp
Normal file
@ -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 <QVariantMap>
|
||||
#include <QDebug>
|
||||
|
||||
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, "");
|
||||
}
|
57
params.h
Normal file
57
params.h
Normal file
@ -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 <QVariant>
|
||||
#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
|
@ -6,57 +6,6 @@
|
||||
*/
|
||||
|
||||
#include "quasarapp.h"
|
||||
#include <QVariantMap>
|
||||
#include <QDebug>
|
||||
#include <QCoreApplication>
|
||||
#include <QTranslator>
|
||||
|
||||
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);
|
||||
}
|
||||
|
54
quasarapp.h
54
quasarapp.h
@ -8,61 +8,17 @@
|
||||
#ifndef QUASARAAPP_H
|
||||
#define QUASARAAPP_H
|
||||
|
||||
#include "quasarapp_global.h"
|
||||
#include <QVariant>
|
||||
|
||||
|
||||
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
|
||||
|
45
settings.cpp
Normal file
45
settings.cpp
Normal file
@ -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 <QSettings>
|
||||
|
||||
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);
|
||||
}
|
78
settings.h
Normal file
78
settings.h
Normal file
@ -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 <QObject>
|
||||
#include <QVariant>
|
||||
|
||||
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
|
26
targetdir.pri
Executable file
26
targetdir.pri
Executable file
@ -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
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user