FluentUI/src/FluApp.cpp

107 lines
2.8 KiB
C++
Raw Normal View History

2023-05-22 16:17:51 +08:00
#include "FluApp.h"
2023-02-26 23:47:07 +08:00
#include <QQmlEngine>
#include <QGuiApplication>
#include <QQmlContext>
2023-02-27 18:46:39 +08:00
#include <QQuickItem>
2023-02-27 23:04:52 +08:00
#include <QTimer>
2023-03-30 17:16:57 +08:00
#include <QUuid>
2023-04-27 09:38:57 +08:00
#include <QFontDatabase>
2023-03-02 12:20:16 +08:00
#include <QClipboard>
2024-03-13 17:54:11 +08:00
#include <QTranslator>
2023-04-27 17:29:39 +08:00
2023-09-13 15:11:22 +08:00
FluApp::FluApp(QObject *parent):QObject{parent}{
2023-11-23 19:58:54 +08:00
useSystemAppBar(false);
2023-04-27 17:29:39 +08:00
}
FluApp::~FluApp(){
2023-02-27 18:46:39 +08:00
}
2024-03-13 17:54:11 +08:00
void FluApp::init(QObject *target,QLocale locale){
2024-03-15 00:04:15 +08:00
_locale = locale;
2024-02-27 12:23:24 +08:00
_engine = qmlEngine(target);
2024-03-13 17:54:11 +08:00
_translator = new QTranslator(this);
qApp->installTranslator(_translator);
2024-03-15 00:04:15 +08:00
const QStringList uiLanguages = _locale.uiLanguages();
2024-03-13 17:54:11 +08:00
for (const QString &name : uiLanguages) {
const QString baseName = "fluentuiplugin_" + QLocale(name).name();
if (_translator->load(":/qt/qml/FluentUI/i18n/"+ baseName)) {
_engine->retranslate();
break;
}
}
2023-02-26 23:47:07 +08:00
}
void FluApp::run(){
2023-02-27 23:04:52 +08:00
navigate(initialRoute());
2023-02-26 23:47:07 +08:00
}
2024-03-06 21:00:49 +08:00
void FluApp::navigate(const QString& route,const QJsonObject& argument,FluWindowRegister* windowRegister){
2023-02-26 23:47:07 +08:00
if(!routes().contains(route)){
2024-02-27 12:23:24 +08:00
qCritical()<<"Not Found Route "<<route;
2023-02-26 23:47:07 +08:00
return;
}
2024-02-27 12:23:24 +08:00
QQmlComponent component(_engine, routes().value(route).toString());
2023-06-18 13:56:30 +08:00
if (component.isError()) {
2023-06-25 23:17:44 +08:00
qCritical() << component.errors();
2023-06-18 13:56:30 +08:00
return;
}
2023-04-11 23:12:31 +08:00
QVariantMap properties;
2023-08-11 22:47:36 +08:00
properties.insert("_route",route);
2024-03-06 21:00:49 +08:00
if(windowRegister){
properties.insert("_windowRegister",QVariant::fromValue(windowRegister));
2023-03-13 21:18:51 +08:00
}
2023-04-11 23:12:31 +08:00
properties.insert("argument",argument);
2023-09-27 15:18:10 +08:00
QQuickWindow *win=nullptr;
for (const auto& pair : _windows.toStdMap()) {
QString r = pair.second->property("_route").toString();
2023-05-10 10:32:37 +08:00
if(r == route){
2023-09-27 15:18:10 +08:00
win = pair.second;
2023-05-10 10:32:37 +08:00
break;
}
2023-05-10 10:32:37 +08:00
}
2023-09-27 15:18:10 +08:00
if(win){
int launchMode = win->property("launchMode").toInt();
2023-05-10 10:32:37 +08:00
if(launchMode == 1){
2023-09-27 15:18:10 +08:00
win->setProperty("argument",argument);
win->show();
win->raise();
win->requestActivate();
2023-05-10 10:32:37 +08:00
return;
}else if(launchMode == 2){
2023-09-27 15:18:10 +08:00
win->close();
}
}
2023-09-27 15:18:10 +08:00
win = qobject_cast<QQuickWindow*>(component.createWithInitialProperties(properties));
2024-03-06 21:00:49 +08:00
if(windowRegister){
windowRegister->to(win);
2023-03-05 23:39:13 +08:00
}
2023-02-27 18:46:39 +08:00
}
2023-03-02 12:20:16 +08:00
2023-09-27 15:18:10 +08:00
void FluApp::exit(int retCode){
for (const auto& pair : _windows.toStdMap()) {
2023-10-19 22:52:36 +08:00
pair.second->close();
2023-09-27 15:18:10 +08:00
removeWindow(pair.second);
2023-03-02 12:20:16 +08:00
}
2023-09-27 15:18:10 +08:00
qApp->exit(retCode);
2023-03-02 12:20:16 +08:00
}
2023-09-27 15:18:10 +08:00
void FluApp::addWindow(QQuickWindow* window){
_windows.insert(window->winId(),window);
2023-04-19 09:41:08 +08:00
}
2023-06-28 02:28:34 +08:00
2023-09-27 15:18:10 +08:00
void FluApp::removeWindow(QQuickWindow* window){
2023-06-28 13:13:39 +08:00
if(window){
2023-09-27 15:18:10 +08:00
_windows.remove(window->winId());
2023-06-28 13:13:39 +08:00
window->deleteLater();
window = nullptr;
}
2023-06-28 02:28:34 +08:00
}
2024-03-06 21:00:49 +08:00
QVariant FluApp::createWindowRegister(QQuickWindow* window,const QString& path){
FluWindowRegister *p = new FluWindowRegister(window);
p->from(window);
p->path(path);
return QVariant::fromValue(p);
}