mirror of
https://github.com/zhuzichu520/FluentUI.git
synced 2025-07-03 08:35:28 +08:00
update
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
|
||||
#include <QQmlContext>
|
||||
#include <QDebug>
|
||||
#include <QGuiApplication>
|
||||
#include "lang/En.h"
|
||||
#include "lang/Zh.h"
|
||||
#include "Version.h"
|
||||
@ -20,7 +21,6 @@ void AppInfo::init(QQmlApplicationEngine *engine){
|
||||
QObject::connect(this,&AppInfo::langChanged,this,[=]{
|
||||
context->setContextProperty("lang",this->lang());
|
||||
});
|
||||
context->setContextProperty("appInfo",this);
|
||||
}
|
||||
|
||||
void AppInfo::changeLang(const QString& locale){
|
||||
@ -35,3 +35,7 @@ void AppInfo::changeLang(const QString& locale){
|
||||
lang(new En());
|
||||
}
|
||||
}
|
||||
|
||||
void AppInfo::restart(){
|
||||
qApp->exit(931);
|
||||
}
|
||||
|
@ -5,16 +5,20 @@
|
||||
#include <QQmlApplicationEngine>
|
||||
#include "lang/Lang.h"
|
||||
#include "stdafx.h"
|
||||
#include "singleton.h"
|
||||
|
||||
class AppInfo : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY_AUTO(QString,version)
|
||||
Q_PROPERTY_AUTO(Lang*,lang)
|
||||
public:
|
||||
private:
|
||||
explicit AppInfo(QObject *parent = nullptr);
|
||||
public:
|
||||
SINGLETONG(AppInfo)
|
||||
void init(QQmlApplicationEngine *engine);
|
||||
Q_INVOKABLE void changeLang(const QString& locale);
|
||||
Q_INVOKABLE void restart();
|
||||
};
|
||||
|
||||
#endif // APPINFO_H
|
||||
|
40
example/src/helper/SettingsHelper.cpp
Normal file
40
example/src/helper/SettingsHelper.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
#include "SettingsHelper.h"
|
||||
|
||||
#include <QDataStream>
|
||||
|
||||
SettingsHelper::SettingsHelper(QObject *parent) : QObject(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
SettingsHelper::~SettingsHelper() = default;
|
||||
|
||||
void SettingsHelper::save(const QString& key,QVariant val)
|
||||
{
|
||||
QByteArray data = {};
|
||||
QDataStream stream(&data, QIODevice::WriteOnly);
|
||||
stream.setVersion(QDataStream::Qt_5_6);
|
||||
stream << val;
|
||||
m_settings->setValue(key, data);
|
||||
}
|
||||
|
||||
QVariant SettingsHelper::get(const QString& key){
|
||||
const QByteArray data = m_settings->value(key).toByteArray();
|
||||
if (data.isEmpty()) {
|
||||
return {};
|
||||
}
|
||||
QDataStream stream(data);
|
||||
stream.setVersion(QDataStream::Qt_5_6);
|
||||
QVariant val;
|
||||
stream >> val;
|
||||
return val;
|
||||
}
|
||||
|
||||
void SettingsHelper::init(char *argv[]){
|
||||
auto applicationPath = QString::fromStdString(argv[0]);
|
||||
const QFileInfo fileInfo(applicationPath);
|
||||
const QString iniFileName = fileInfo.completeBaseName() + ".ini";
|
||||
const QString iniFilePath = fileInfo.dir().path() + "/" + iniFileName;
|
||||
qDebug()<<iniFilePath;
|
||||
m_settings.reset(new QSettings(iniFilePath, QSettings::IniFormat));
|
||||
}
|
33
example/src/helper/SettingsHelper.h
Normal file
33
example/src/helper/SettingsHelper.h
Normal file
@ -0,0 +1,33 @@
|
||||
#ifndef SETTINGSHELPER_H
|
||||
#define SETTINGSHELPER_H
|
||||
|
||||
#include <QtCore/qobject.h>
|
||||
#include <QtQml/qqml.h>
|
||||
#include <QSettings>
|
||||
#include <QScopedPointer>
|
||||
#include <QFileInfo>
|
||||
#include <QCoreApplication>
|
||||
#include <QDir>
|
||||
#include "src/singleton.h"
|
||||
|
||||
class SettingsHelper : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
private:
|
||||
explicit SettingsHelper(QObject* parent = nullptr);
|
||||
public:
|
||||
SINGLETONG(SettingsHelper)
|
||||
~SettingsHelper() override;
|
||||
void init(char *argv[]);
|
||||
Q_INVOKABLE void saveRender(const QString& render){save("render",render);}
|
||||
Q_INVOKABLE QString getReander(){return get("render").toString();}
|
||||
Q_INVOKABLE void saveDarkMode(int darkModel){save("darkMode",darkModel);}
|
||||
Q_INVOKABLE int getDarkMode(){return get("darkMode").toInt(0);}
|
||||
private:
|
||||
void save(const QString& key,QVariant val);
|
||||
QVariant get(const QString& key);
|
||||
private:
|
||||
QScopedPointer<QSettings> m_settings;
|
||||
};
|
||||
|
||||
#endif // SETTINGSHELPER_H
|
@ -13,6 +13,8 @@
|
||||
#include "src/component/CircularReveal.h"
|
||||
#include "src/component/FileWatcher.h"
|
||||
#include "src/component/FpsItem.h"
|
||||
#include "src/helper/SettingsHelper.h"
|
||||
|
||||
#ifdef FLUENTUI_BUILD_STATIC_LIB
|
||||
#if (QT_VERSION > QT_VERSION_CHECK(6, 2, 0))
|
||||
Q_IMPORT_QML_PLUGIN(FluentUIPlugin)
|
||||
@ -22,8 +24,9 @@ Q_IMPORT_QML_PLUGIN(FluentUIPlugin)
|
||||
|
||||
FRAMELESSHELPER_USE_NAMESPACE
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
SettingsHelper::getInstance()->init(argv);
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
|
||||
QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
|
||||
@ -36,6 +39,13 @@ FRAMELESSHELPER_USE_NAMESPACE
|
||||
QGuiApplication::setOrganizationName("ZhuZiChu");
|
||||
QGuiApplication::setOrganizationDomain("https://zhuzichu520.github.io");
|
||||
QGuiApplication::setApplicationName("FluentUI");
|
||||
if(SettingsHelper::getInstance()->getReander()=="software"){
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
||||
QQuickWindow::setGraphicsApi(QSGRendererInterface::Software);
|
||||
#elif (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
|
||||
QQuickWindow::setSceneGraphBackend(QSGRendererInterface::Software);
|
||||
#endif
|
||||
}
|
||||
QGuiApplication app(argc, argv);
|
||||
FramelessConfig::instance()->set(Global::Option::DisableLazyInitializationForMicaMaterial);
|
||||
FramelessConfig::instance()->set(Global::Option::CenterWindowBeforeShow);
|
||||
@ -48,8 +58,10 @@ FRAMELESSHELPER_USE_NAMESPACE
|
||||
#ifdef Q_OS_MACOS
|
||||
FramelessConfig::instance()->set(Global::Option::ForceNonNativeBackgroundBlur,false);
|
||||
#endif
|
||||
AppInfo* appInfo = new AppInfo();
|
||||
QQmlApplicationEngine engine;
|
||||
AppInfo::getInstance()->init(&engine);
|
||||
engine.rootContext()->setContextProperty("AppInfo",AppInfo::getInstance());
|
||||
engine.rootContext()->setContextProperty("SettingsHelper",SettingsHelper::getInstance());
|
||||
FramelessHelper::Quick::registerTypes(&engine);
|
||||
#ifdef FLUENTUI_BUILD_STATIC_LIB
|
||||
FluentUI::getInstance()->registerTypes(&engine);
|
||||
@ -58,7 +70,6 @@ FRAMELESSHELPER_USE_NAMESPACE
|
||||
qmlRegisterType<CircularReveal>("example", 1, 0, "CircularReveal");
|
||||
qmlRegisterType<FileWatcher>("example", 1, 0, "FileWatcher");
|
||||
qmlRegisterType<FpsItem>("example", 1, 0, "FpsItem");
|
||||
appInfo->init(&engine);
|
||||
const QUrl url(QStringLiteral("qrc:/example/qml/App.qml"));
|
||||
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
|
||||
&app, [url](QObject *obj, const QUrl &objUrl) {
|
||||
@ -66,5 +77,9 @@ FRAMELESSHELPER_USE_NAMESPACE
|
||||
QCoreApplication::exit(-1);
|
||||
}, Qt::QueuedConnection);
|
||||
engine.load(url);
|
||||
return app.exec();
|
||||
const int exec = QGuiApplication::exec();
|
||||
if (exec == 931) {
|
||||
QProcess::startDetached(qApp->applicationFilePath(), QStringList());
|
||||
}
|
||||
return exec;
|
||||
}
|
||||
|
47
example/src/singleton.h
Normal file
47
example/src/singleton.h
Normal file
@ -0,0 +1,47 @@
|
||||
#ifndef SINGLETON_H
|
||||
#define SINGLETON_H
|
||||
|
||||
#include <QMutex>
|
||||
#include <QScopedPointer>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
template <typename T>
|
||||
class Singleton {
|
||||
public:
|
||||
static T* getInstance();
|
||||
|
||||
Singleton(const Singleton& other) = delete;
|
||||
Singleton<T>& operator=(const Singleton& other) = delete;
|
||||
|
||||
private:
|
||||
static std::mutex mutex;
|
||||
static T* instance;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
std::mutex Singleton<T>::mutex;
|
||||
template <typename T>
|
||||
T* Singleton<T>::instance;
|
||||
template <typename T>
|
||||
T* Singleton<T>::getInstance() {
|
||||
if (instance == nullptr) {
|
||||
std::lock_guard<std::mutex> locker(mutex);
|
||||
if (instance == nullptr) {
|
||||
instance = new T();
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
#define SINGLETONG(Class) \
|
||||
private: \
|
||||
friend class Singleton<Class>; \
|
||||
friend struct QScopedPointerDeleter<Class>; \
|
||||
\
|
||||
public: \
|
||||
static Class* getInstance() { \
|
||||
return Singleton<Class>::getInstance(); \
|
||||
}
|
||||
|
||||
#endif // SINGLETON_H
|
Reference in New Issue
Block a user