From b28f15c23b3d1d595ddbb2eea48c9317ff98fba7 Mon Sep 17 00:00:00 2001 From: zhuzihcu Date: Fri, 14 Apr 2023 17:07:54 +0800 Subject: [PATCH] update --- example/AppInfo.cpp | 18 +++++++++++++ example/AppInfo.h | 3 +++ example/T_Settings.qml | 44 +++++++++++++++++++++++++++++--- example/example.pro | 6 +++++ example/global/ItemsFooter.qml | 4 +-- example/global/ItemsOriginal.qml | 16 ++++++------ example/lang/En.cpp | 20 +++++++++++++++ example/lang/En.h | 17 ++++++++++++ example/lang/Lang.cpp | 7 +++++ example/lang/Lang.h | 29 +++++++++++++++++++++ example/lang/zh.cpp | 21 +++++++++++++++ example/lang/zh.h | 17 ++++++++++++ example/main.cpp | 10 +++++++- example/page/MainPage.qml | 4 +-- src/FluApp.cpp | 2 +- 15 files changed, 201 insertions(+), 17 deletions(-) create mode 100644 example/lang/En.cpp create mode 100644 example/lang/En.h create mode 100644 example/lang/Lang.cpp create mode 100644 example/lang/Lang.h create mode 100644 example/lang/zh.cpp create mode 100644 example/lang/zh.h diff --git a/example/AppInfo.cpp b/example/AppInfo.cpp index 905470b..58585ff 100644 --- a/example/AppInfo.cpp +++ b/example/AppInfo.cpp @@ -1,7 +1,25 @@ #include "AppInfo.h" +#include "lang/En.h" +#include "lang/Zh.h" + AppInfo::AppInfo(QObject *parent) : QObject{parent} { version("1.2.4"); + changeLang("En"); +} + +void AppInfo::changeLang(const QString& locale){ + if(_lang){ + _lang->deleteLater(); + } + if(locale=="Zh"){ + lang(new Zh()); + }else if(locale=="En"){ + lang(new En()); + }else { + lang(new En()); + } + } diff --git a/example/AppInfo.h b/example/AppInfo.h index fb518ee..1c09604 100644 --- a/example/AppInfo.h +++ b/example/AppInfo.h @@ -2,14 +2,17 @@ #define APPINFO_H #include +#include "lang/Lang.h" #include "stdafx.h" class AppInfo : public QObject { Q_OBJECT Q_PROPERTY_AUTO(QString,version) + Q_PROPERTY_AUTO(Lang*,lang) public: explicit AppInfo(QObject *parent = nullptr); + Q_INVOKABLE void changeLang(const QString& locale); }; #endif // APPINFO_H diff --git a/example/T_Settings.qml b/example/T_Settings.qml index eb232a8..a445e71 100644 --- a/example/T_Settings.qml +++ b/example/T_Settings.qml @@ -23,12 +23,12 @@ FluScrollablePage{ ColumnLayout{ spacing: 10 anchors{ - verticalCenter: parent.verticalCenter + top: parent.top left: parent.left } FluText{ - text:"NavigationView Display Mode" + text:lang.navigation_view_display_mode fontStyle: FluText.BodyStrong Layout.bottomMargin: 4 } @@ -40,11 +40,49 @@ FluScrollablePage{ selected : MainEvent.displayMode===modelData.mode text:modelData.title onClicked:{ - MainEvent.displayMode = modelData.mode + MainEvent.displayMode = modelData.mode } } } } } + + FluArea{ + Layout.fillWidth: true + Layout.topMargin: 20 + height: 168 + paddings: 10 + + ColumnLayout{ + spacing: 10 + anchors{ + top: parent.top + left: parent.left + } + + FluText{ + text:lang.locale + fontStyle: FluText.BodyStrong + Layout.bottomMargin: 4 + } + + Flow{ + spacing: 5 + Repeater{ + model: ["Zh","En"] + delegate: FluRadioButton{ + selected : appInfo.lang.objectName === modelData + text:modelData + onClicked:{ + console.debug(modelData) + appInfo.changeLang(modelData) + } + } + } + } + } + + } + } diff --git a/example/example.pro b/example/example.pro index 6d0faed..38ecac6 100644 --- a/example/example.pro +++ b/example/example.pro @@ -3,6 +3,9 @@ CONFIG += c++17 DEFINES += QT_DEPRECATED_WARNINGS QT_NO_WARNING_OUTPUT HEADERS += \ + lang/En.h \ + lang/Lang.h \ + lang/Zh.h \ stdafx.h \ ChatController.h \ AppInfo.h @@ -10,6 +13,9 @@ HEADERS += \ SOURCES += \ ChatController.cpp \ AppInfo.cpp \ + lang/En.cpp \ + lang/Lang.cpp \ + lang/Zh.cpp \ main.cpp RESOURCES += qml.qrc diff --git a/example/global/ItemsFooter.qml b/example/global/ItemsFooter.qml index 9e34533..5f34ffa 100644 --- a/example/global/ItemsFooter.qml +++ b/example/global/ItemsFooter.qml @@ -10,14 +10,14 @@ FluObject{ FluPaneItemSeparator{} FluPaneItem{ - title:"About" + title:lang.about icon:FluentIcons.Contact tapFunc:function(){ FluApp.navigate("/about") } } FluPaneItem{ - title:"Settings" + title:lang.settings icon:FluentIcons.Settings onTap:{ navigationView.push("qrc:/T_Settings.qml") diff --git a/example/global/ItemsOriginal.qml b/example/global/ItemsOriginal.qml index d0ed554..268bfcf 100644 --- a/example/global/ItemsOriginal.qml +++ b/example/global/ItemsOriginal.qml @@ -8,7 +8,7 @@ FluObject{ property var navigationView FluPaneItem{ - title:"Home" + title:lang.home // icon:FluentIcons.Home cusIcon: Image{ anchors.centerIn: parent @@ -23,7 +23,7 @@ FluObject{ } FluPaneItemExpander{ - title:"Basic input" + title:lang.basic_input icon:FluentIcons.CheckboxComposite FluPaneItem{ title:"Buttons" @@ -61,7 +61,7 @@ FluObject{ } FluPaneItemExpander{ - title:"Form" + title:lang.form icon:FluentIcons.GridView FluPaneItem{ title:"TextBox" @@ -96,7 +96,7 @@ FluObject{ } FluPaneItemExpander{ - title:"Surface" + title:lang.surface icon:FluentIcons.SurfaceHub FluPaneItem{ title:"InfoBar" @@ -152,7 +152,7 @@ FluObject{ } FluPaneItemExpander{ - title:"Popus" + title:lang.popus icon:FluentIcons.ButtonMenu FluPaneItem{ title:"Dialog" @@ -175,7 +175,7 @@ FluObject{ } FluPaneItemExpander{ - title:"Navigation" + title:lang.navigation icon:FluentIcons.AllApps FluPaneItem{ title:"Pivot" @@ -232,7 +232,7 @@ FluObject{ } FluPaneItemExpander{ - title:"Theming" + title:lang.theming icon:FluentIcons.Brightness FluPaneItem{ title:"Theme" @@ -255,7 +255,7 @@ FluObject{ } FluPaneItemExpander{ - title:"Media" + title:lang.media icon:FluentIcons.Media FluPaneItem{ title:"MediaPlayer" diff --git a/example/lang/En.cpp b/example/lang/En.cpp new file mode 100644 index 0000000..3bd192b --- /dev/null +++ b/example/lang/En.cpp @@ -0,0 +1,20 @@ +#include "En.h" + +En::En(QObject *parent) + : Lang{parent} +{ + setObjectName("En"); + home("Home"); + basic_input("Basic Input"); + form("Form"); + surface("Surfaces"); + popus("Popus"); + navigation("Navigation"); + theming("Theming"); + media("Media"); + dark_mode("Dark Mode"); + search("Search"); + about("About"); + settings("Settings"); + navigation_view_display_mode("NavigationView Display Mode"); +} diff --git a/example/lang/En.h b/example/lang/En.h new file mode 100644 index 0000000..f59e685 --- /dev/null +++ b/example/lang/En.h @@ -0,0 +1,17 @@ +#ifndef EN_H +#define EN_H + +#include +#include "Lang.h" + +class En : public Lang +{ + Q_OBJECT +public: + explicit En(QObject *parent = nullptr); + +signals: + +}; + +#endif // EN_H diff --git a/example/lang/Lang.cpp b/example/lang/Lang.cpp new file mode 100644 index 0000000..396fb30 --- /dev/null +++ b/example/lang/Lang.cpp @@ -0,0 +1,7 @@ +#include "Lang.h" + +Lang::Lang(QObject *parent) + : QObject{parent} +{ + +} diff --git a/example/lang/Lang.h b/example/lang/Lang.h new file mode 100644 index 0000000..9725c52 --- /dev/null +++ b/example/lang/Lang.h @@ -0,0 +1,29 @@ +#ifndef LANG_H +#define LANG_H + +#include +#include "stdafx.h" + +class Lang : public QObject +{ + Q_OBJECT + Q_PROPERTY_AUTO(QString,home); + Q_PROPERTY_AUTO(QString,basic_input); + Q_PROPERTY_AUTO(QString,form); + Q_PROPERTY_AUTO(QString,surface); + Q_PROPERTY_AUTO(QString,popus); + Q_PROPERTY_AUTO(QString,navigation); + Q_PROPERTY_AUTO(QString,theming); + Q_PROPERTY_AUTO(QString,media); + Q_PROPERTY_AUTO(QString,dark_mode); + Q_PROPERTY_AUTO(QString,search); + Q_PROPERTY_AUTO(QString,about); + Q_PROPERTY_AUTO(QString,settings); + Q_PROPERTY_AUTO(QString,navigation_view_display_mode); + Q_PROPERTY_AUTO(QString,locale); +public: + explicit Lang(QObject *parent = nullptr); + +}; + +#endif // LANG_H diff --git a/example/lang/zh.cpp b/example/lang/zh.cpp new file mode 100644 index 0000000..b101a34 --- /dev/null +++ b/example/lang/zh.cpp @@ -0,0 +1,21 @@ +#include "zh.h" + +Zh::Zh(QObject *parent) + : Lang{parent} +{ + setObjectName("Zh"); + home("首页"); + basic_input("基本输入"); + form("表单"); + surface("表面"); + popus("弹窗"); + navigation("导航"); + theming("主题"); + media("媒体"); + dark_mode("夜间模式"); + search("查找"); + about("关于"); + settings("设置"); + locale("语言环境"); + navigation_view_display_mode("导航视图显示模式"); +} diff --git a/example/lang/zh.h b/example/lang/zh.h new file mode 100644 index 0000000..106c4e1 --- /dev/null +++ b/example/lang/zh.h @@ -0,0 +1,17 @@ +#ifndef ZH_H +#define ZH_H + +#include +#include "Lang.h" + +class Zh : public Lang +{ + Q_OBJECT +public: + explicit Zh(QObject *parent = nullptr); + +signals: + +}; + +#endif // ZH_H diff --git a/example/main.cpp b/example/main.cpp index 38244a2..9e8358c 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -5,6 +5,7 @@ #include #include #include +#include "lang/Lang.h" #include "AppInfo.h" #include "ChatController.h" @@ -17,7 +18,14 @@ int main(int argc, char *argv[]) QGuiApplication app(argc, argv); QQmlApplicationEngine engine; qmlRegisterType("Controller",1,0,"ChatController"); - engine.rootContext()->setContextProperty("appInfo",new AppInfo()); + AppInfo* appInfo = new AppInfo(); + QQmlContext * context = engine.rootContext(); + Lang* lang = appInfo->lang(); + context->setContextProperty("lang",lang); + QObject::connect(appInfo,&AppInfo::langChanged,&app,[context,appInfo]{ + context->setContextProperty("lang",appInfo->lang()); + }); + context->setContextProperty("appInfo",appInfo); const QUrl url(QStringLiteral("qrc:/App.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { diff --git a/example/page/MainPage.qml b/example/page/MainPage.qml index b97e7a4..c14bb9b 100644 --- a/example/page/MainPage.qml +++ b/example/page/MainPage.qml @@ -18,7 +18,7 @@ FluWindow { z:9 showDark: true width:parent.width - darkText: "Dark Mode" + darkText: lang.dark_mode } FluNavigationView{ @@ -35,7 +35,7 @@ FluWindow { anchors.centerIn: parent iconSource: FluentIcons.Search items: ItemsOriginal.getSearchData() - placeholderText: "Search" + placeholderText: lang.search onItemClicked: (data)=>{ ItemsOriginal.startPageByItem(data) diff --git a/src/FluApp.cpp b/src/FluApp.cpp index 53a6a9a..60dca18 100644 --- a/src/FluApp.cpp +++ b/src/FluApp.cpp @@ -71,7 +71,7 @@ void FluApp::navigate(const QString& route,const QJsonObject& argument,FluRegist QString r = pair->property("route").toString(); if(r == route){ pair->requestActivate(); - delete view; + view->deleteLater(); return; } }