FluentUI/example/qml/page/T_TreeView.qml

206 lines
6.9 KiB
QML
Raw Permalink Normal View History

2023-08-24 15:50:37 +08:00
import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import FluentUI 1.0
2023-08-26 17:20:30 +08:00
import "../component"
2023-03-07 00:05:27 +08:00
2023-09-22 00:11:58 +08:00
FluContentPage {
2023-03-10 18:08:32 +08:00
2024-03-09 15:35:48 +08:00
title: qsTr("TreeView")
2023-03-07 18:43:03 +08:00
2024-04-09 20:53:52 +08:00
2023-09-15 01:28:03 +08:00
function treeData(){
2024-04-09 20:53:52 +08:00
const names = ["孙悟空", "猪八戒", "沙和尚", "唐僧","白骨夫人","金角大王","熊山君","黄风怪","银角大王"]
function getRandomName(){
var randomIndex = Math.floor(Math.random() * names.length)
return names[randomIndex]
}
const addresses = ["傲来国界花果山水帘洞","傲来国界坎源山脏水洞","大唐国界黑风山黑风洞","大唐国界黄风岭黄风洞","大唐国界骷髅山白骨洞","宝象国界碗子山波月洞","宝象国界平顶山莲花洞","宝象国界压龙山压龙洞","乌鸡国界号山枯松涧火云洞","乌鸡国界衡阳峪黑水河河神府"]
function getRandomAddresses(){
var randomIndex = Math.floor(Math.random() * addresses.length)
return addresses[randomIndex]
}
const avatars = ["qrc:/example/res/svg/avatar_1.svg", "qrc:/example/res/svg/avatar_2.svg", "qrc:/example/res/svg/avatar_3.svg", "qrc:/example/res/svg/avatar_4.svg","qrc:/example/res/svg/avatar_5.svg","qrc:/example/res/svg/avatar_6.svg","qrc:/example/res/svg/avatar_7.svg","qrc:/example/res/svg/avatar_8.svg","qrc:/example/res/svg/avatar_9.svg","qrc:/example/res/svg/avatar_10.svg","qrc:/example/res/svg/avatar_11.svg","qrc:/example/res/svg/avatar_12.svg"]
function getRandomAvatar(){
var randomIndex = Math.floor(Math.random() * avatars.length);
return avatars[randomIndex];
}
const dig = (path = '0', level = 5) => {
2023-09-15 01:28:03 +08:00
const list = [];
2024-04-14 23:58:15 +08:00
for (let i = 0; i < 4; i += 1) {
2023-09-15 01:28:03 +08:00
const key = `${path}-${i}`;
const treeNode = {
title: key,
2024-04-09 20:53:52 +08:00
_key: key,
name: getRandomName(),
avatar:tree_view.customItem(com_avatar,{avatar:getRandomAvatar()}),
address: getRandomAddresses()
2023-09-15 01:28:03 +08:00
};
if (level > 0) {
treeNode.children = dig(key, level - 1);
}
list.push(treeNode);
}
return list;
};
return dig();
2023-03-07 23:27:32 +08:00
}
2023-03-07 18:43:03 +08:00
2024-04-09 20:53:52 +08:00
Component{
id:com_avatar
Item{
FluClip{
anchors.centerIn: parent
width: height
height: parent.height/3*2
radius: [height/2,height/2,height/2,height/2]
Image{
anchors.fill: parent
source: {
if(options && options.avatar){
return options.avatar
}
return ""
}
sourceSize: Qt.size(80,80)
}
2023-09-15 19:11:55 +08:00
}
2023-09-22 00:11:58 +08:00
}
2024-04-09 20:53:52 +08:00
}
FluFrame{
id:layout_controls
anchors{
left: parent.left
right: parent.right
top: parent.top
topMargin: 10
}
height: 80
clip: true
Row{
spacing: 12
anchors{
left: parent.left
leftMargin: 10
verticalCenter: parent.verticalCenter
2023-09-22 00:11:58 +08:00
}
2024-04-09 20:53:52 +08:00
Column{
anchors.verticalCenter: parent.verticalCenter
RowLayout{
spacing: 10
FluText{
text: "cellHeight:"
Layout.alignment: Qt.AlignVCenter
}
FluSlider{
id: slider_cell_height
value: 38
from: 38
to:100
}
}
RowLayout{
spacing: 10
FluText{
text: "depthPadding:"
Layout.alignment: Qt.AlignVCenter
}
FluSlider{
id: slider_depth_padding
value: 15
from: 15
to:100
}
}
2023-09-22 00:11:58 +08:00
}
2024-04-09 20:53:52 +08:00
Column{
spacing: 8
anchors.verticalCenter: parent.verticalCenter
FluToggleSwitch{
id: switch_showline
text:"showLine"
checked: false
}
FluToggleSwitch{
id: switch_checkable
text:"checkable"
checked: false
}
2023-09-22 00:11:58 +08:00
}
2024-04-09 20:53:52 +08:00
Column{
spacing: 8
anchors.verticalCenter: parent.verticalCenter
FluButton{
text: "all expand"
onClicked: {
tree_view.allExpand()
}
}
FluButton{
text: "all collapse"
onClicked: {
tree_view.allCollapse()
}
}
2023-09-15 19:11:55 +08:00
}
2024-04-14 23:09:41 +08:00
FluButton{
text: "print selection model"
onClicked: {
var printData = []
2024-04-14 23:58:15 +08:00
var data = tree_view.selectionModel();
console.debug(data.length)
2024-04-14 23:09:41 +08:00
for(var i = 0; i <= data.length-1 ; i++){
const newObj = Object.assign({}, data[i].data);
delete newObj["__parent"];
delete newObj["children"];
printData.push(newObj)
}
console.debug(JSON.stringify(printData))
}
}
2023-03-07 23:27:32 +08:00
}
2023-04-06 17:32:21 +08:00
}
2024-04-09 20:53:52 +08:00
2024-04-17 21:39:55 +08:00
FluTreeView{
id:tree_view
2023-09-22 00:11:58 +08:00
anchors{
2024-04-09 20:53:52 +08:00
left: parent.left
top: layout_controls.bottom
topMargin: 10
2023-09-22 00:11:58 +08:00
bottom: parent.bottom
right: parent.right
2023-09-15 19:11:55 +08:00
}
2024-04-17 21:39:55 +08:00
cellHeight: slider_cell_height.value
showLine: switch_showline.checked
checkable:switch_checkable.checked
depthPadding: slider_depth_padding.value
onCurrentChanged: {
showInfo(current.data.title)
}
columnSource:[
{
title: qsTr("Title"),
dataIndex: 'title',
width: 300
},{
title: qsTr("Name"),
dataIndex: 'name',
width: 100
},{
title: qsTr("Avatar"),
dataIndex: 'avatar',
width: 100
},{
title: qsTr("Address"),
dataIndex: 'address',
width: 200
},
]
Component.onCompleted: {
var data = treeData()
dataSource = data
2023-03-07 23:27:32 +08:00
}
2023-04-06 17:32:21 +08:00
}
2023-03-07 00:05:27 +08:00
}