Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuzichu520 committed Sep 12, 2023
1 parent 05040ec commit fd30819
Show file tree
Hide file tree
Showing 20 changed files with 333 additions and 82 deletions.
2 changes: 1 addition & 1 deletion example/example.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@
<file>qml/component/CustomWindow.qml</file>
<file>qml/global/ItemsFooter.qml</file>
<file>qml/global/ItemsOriginal.qml</file>
<file>qml/global/MainEvent.qml</file>
<file>qml/global/qmldir</file>
<file>qml/page/T_Acrylic.qml</file>
<file>qml/page/T_Awesome.qml</file>
Expand Down Expand Up @@ -186,5 +185,6 @@
<file>res/image/image_1.jpg</file>
<file>qml/window/PageWindow.qml</file>
<file>qml/page/T_StaggeredView.qml</file>
<file>qml/viewmodel/SettingsViewModel.qml</file>
</qresource>
</RCC>
9 changes: 0 additions & 9 deletions example/qml-Qt6/global/MainEvent.qml

This file was deleted.

1 change: 0 additions & 1 deletion example/qml-Qt6/global/qmldir
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
singleton ItemsOriginal 1.0 ItemsOriginal.qml
singleton ItemsFooter 1.0 ItemsFooter.qml
singleton MainEvent 1.0 MainEvent.qml
8 changes: 6 additions & 2 deletions example/qml-Qt6/page/T_Settings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import QtQuick.Controls
import FluentUI
import "qrc:///example/qml/global"
import "qrc:///example/qml/component"
import "qrc:///example/qml/viewmodel"

FluScrollablePage{

title:"Settings"

SettingsViewModel{
id:viewmodel_settings
}

FluEvent{
id:event_checkupdate_finish
Expand Down Expand Up @@ -103,10 +107,10 @@ FluScrollablePage{
Repeater{
model: [{title:"Open",mode:FluNavigationViewType.Open},{title:"Compact",mode:FluNavigationViewType.Compact},{title:"Minimal",mode:FluNavigationViewType.Minimal},{title:"Auto",mode:FluNavigationViewType.Auto}]
delegate: FluRadioButton{
checked : MainEvent.displayMode===modelData.mode
checked : viewmodel_settings.displayMode===modelData.mode
text:modelData.title
clickListener:function(){
MainEvent.displayMode = modelData.mode
viewmodel_settings.displayMode = modelData.mode
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions example/qml-Qt6/viewmodel/SettingsViewModel.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import QtQuick
import FluentUI
import "qrc:///example/qml/component"

FluViewModel{

objectName: "SettingsViewModel"
property int displayMode

onInitData: {
displayMode = FluNavigationViewType.Auto
}

}
7 changes: 6 additions & 1 deletion example/qml-Qt6/window/MainWindow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import FluentUI
import example
import "qrc:///example/qml/component"
import "qrc:///example/qml/global"
import "qrc:///example/qml/viewmodel"

CustomWindow {

Expand All @@ -20,6 +21,10 @@ CustomWindow {
appBarVisible: false
launchMode: FluWindowType.SingleTask

SettingsViewModel{
id:viewmodel_settings
}

closeFunc:function(event){
dialog_close.open()
event.accepted = false
Expand Down Expand Up @@ -183,7 +188,7 @@ CustomWindow {
items: ItemsOriginal
footerItems:ItemsFooter
topPadding:FluTools.isMacos() ? 20 : 0
displayMode:MainEvent.displayMode
displayMode:viewmodel_settings.displayMode
logo: "qrc:/example/res/image/favicon.ico"
title:"FluentUI"
onLogoClicked:{
Expand Down
9 changes: 0 additions & 9 deletions example/qml/global/MainEvent.qml

This file was deleted.

1 change: 0 additions & 1 deletion example/qml/global/qmldir
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
singleton ItemsOriginal 1.0 ItemsOriginal.qml
singleton ItemsFooter 1.0 ItemsFooter.qml
singleton MainEvent 1.0 MainEvent.qml
3 changes: 3 additions & 0 deletions example/qml/page/T_Pivot.qml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ FluScrollablePage{
height: 400
paddings: 10



FluPivot{
anchors.fill: parent
currentIndex: 2

FluPivotItem{
title:"All"
contentItem:FluText{
Expand Down
11 changes: 8 additions & 3 deletions example/qml/page/T_Settings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ import QtQuick.Controls 2.15
import FluentUI 1.0
import "qrc:///example/qml/global"
import "qrc:///example/qml/component"
import "qrc:///example/qml/viewmodel"
import "../component"
import "../viewmodel"
import "../global"

FluScrollablePage{

title:"Settings"

SettingsViewModel{
id:viewmodel_settings
}

FluEvent{
id:event_checkupdate_finish
Expand Down Expand Up @@ -104,10 +110,10 @@ FluScrollablePage{
Repeater{
model: [{title:"Open",mode:FluNavigationViewType.Open},{title:"Compact",mode:FluNavigationViewType.Compact},{title:"Minimal",mode:FluNavigationViewType.Minimal},{title:"Auto",mode:FluNavigationViewType.Auto}]
delegate: FluRadioButton{
checked : MainEvent.displayMode===modelData.mode
checked : viewmodel_settings.displayMode===modelData.mode
text:modelData.title
clickListener:function(){
MainEvent.displayMode = modelData.mode
viewmodel_settings.displayMode = modelData.mode
}
}
}
Expand Down Expand Up @@ -150,4 +156,3 @@ FluScrollablePage{
}

}

14 changes: 14 additions & 0 deletions example/qml/viewmodel/SettingsViewModel.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import QtQuick 2.15
import FluentUI 1.0
import "qrc:///example/qml/component"

FluViewModel{

objectName: "SettingsViewModel"
property int displayMode

onInitData: {
displayMode = FluNavigationViewType.Auto
}

}
11 changes: 9 additions & 2 deletions example/qml/window/MainWindow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import Qt.labs.platform 1.1
import FluentUI 1.0
import example 1.0
import "qrc:///example/qml/component"
import "../component"
import "qrc:///example/qml/global"
import "qrc:///example/qml/viewmodel"
import "../component"
import "../viewmodel"
import "../global"

CustomWindow {

Expand All @@ -21,6 +24,10 @@ CustomWindow {
appBarVisible: false
launchMode: FluWindowType.SingleTask

SettingsViewModel{
id:viewmodel_settings
}

closeFunc:function(event){
dialog_close.open()
event.accepted = false
Expand Down Expand Up @@ -184,7 +191,7 @@ CustomWindow {
items: ItemsOriginal
footerItems:ItemsFooter
topPadding:FluTools.isMacos() ? 20 : 0
displayMode:MainEvent.displayMode
displayMode:viewmodel_settings.displayMode
logo: "qrc:/example/res/image/favicon.ico"
title:"FluentUI"
onLogoClicked:{
Expand Down
10 changes: 10 additions & 0 deletions src/Def.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
#include <QObject>
#include <QtQml/qqml.h>

namespace FluViewModelType {
Q_NAMESPACE
enum Scope {
Window = 0x0000,
Application = 0x0001
};
Q_ENUM_NS(Scope)
QML_NAMED_ELEMENT(FluViewModelType)
}

namespace FluHttpType {
Q_NAMESPACE
enum CacheMode {
Expand Down
139 changes: 139 additions & 0 deletions src/FluViewModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#include "FluViewModel.h"

#include <QQuickItem>
#include "Def.h"

ViewModelManager* ViewModelManager::m_instance = nullptr;

ViewModelManager *ViewModelManager::getInstance()
{
if(ViewModelManager::m_instance == nullptr){
ViewModelManager::m_instance = new ViewModelManager;
}
return ViewModelManager::m_instance;
}

Model::Model(QObject *parent)
: QObject{parent}
{

}

Model::~Model()
{
qDebug()<<"Model delete~";
}

ViewModelManager::ViewModelManager(QObject *parent)
: QObject{parent}
{

}

void ViewModelManager::insertViewModel(FluViewModel* value){
m_viewmodel.append(value);
}

void ViewModelManager::deleteViewModel(FluViewModel* value){
m_viewmodel.removeOne(value);
}

QObject* ViewModelManager::getModel(const QString& key){
return m_data.value(key);
}

void ViewModelManager::insert(const QString& key,QObject* value){
m_data.insert(key,value);
}

bool ViewModelManager::exist(const QString& key){
return m_data.contains(key);
}

void ViewModelManager::refreshViewModel(FluViewModel* viewModel,QString key,QVariant value){
foreach (auto item, m_viewmodel) {
if(item->getKey() == viewModel->getKey()){
item->setProperty(key.toStdString().c_str(),value);
}
}
}

PropertyObserver::PropertyObserver(QString name,QObject* model,QObject *parent)
: QObject{parent}
{
_name = name;
_model = model;
_property = QQmlProperty(parent,_name);
_property.connectNotifySignal(this,SLOT(_propertyChange()));
}

PropertyObserver::~PropertyObserver(){
qDebug()<<"PropertyObserver delete~";
}

void PropertyObserver::_propertyChange(){
auto value = _property.read();
_model->setProperty(_name.toStdString().c_str(),value);
ViewModelManager::getInstance()->refreshViewModel((FluViewModel*)parent(),_name,value);
}

FluViewModel::FluViewModel(QObject *parent)
: QObject{parent}
{
ViewModelManager::getInstance()->insertViewModel(this);
scope(FluViewModelType::Scope::Window);
}

FluViewModel::~FluViewModel(){
ViewModelManager::getInstance()->deleteViewModel(this);
}

void FluViewModel::classBegin()
{
}

void FluViewModel::componentComplete()
{
auto o = parent();
while (nullptr != o) {
_window = o;
o = o->parent();
}
const QMetaObject* obj = metaObject();
if(_scope == FluViewModelType::Scope::Window){
_key = property("objectName_").toString()+QString::number(reinterpret_cast<qulonglong>(_window), 16);
}else{
_key = property("objectName").toString();
}
QObject * model;
if(!ViewModelManager::getInstance()->exist(_key)){
if(_scope == FluViewModelType::Scope::Window){
model = new Model(_window);
}else{
model = new Model();
}
Q_EMIT initData();
for (int i = 0; i < obj->propertyCount(); ++i) {
const QMetaProperty property = obj->property(i);
QString propertyName = property.name();
auto value = property.read(this);
model->setProperty(propertyName.toStdString().c_str(),value);
new PropertyObserver(propertyName,model,this);
}
ViewModelManager::getInstance()->insert(_key,model);
}else{
model = ViewModelManager::getInstance()->getModel(_key);
for (int i = 0; i < obj->propertyCount(); ++i) {
const QMetaProperty property = obj->property(i);
QString propertyName = property.name();
new PropertyObserver(propertyName,model,this);
}
}
foreach (auto key, model->dynamicPropertyNames()) {
setProperty(key,model->property(key));
}
}

QString FluViewModel::getKey(){
return _key;
}
Loading

0 comments on commit fd30819

Please sign in to comment.