DiagramDesigner/PropertyEditor/resources/Qml/ValueEditor/FileSelector.qml

101 lines
2.6 KiB
QML
Raw Normal View History

2026-02-06 14:07:08 +08:00
import QtQuick
import QtQuick.Controls
2026-01-27 16:36:59 +08:00
import QtQuick.Dialogs
2026-02-06 14:07:08 +08:00
import QtQuick.Layouts
import QtCore
import ColorPalette
2026-01-27 16:36:59 +08:00
2026-02-06 14:07:08 +08:00
Item {
2026-01-27 16:36:59 +08:00
id: control
2026-02-06 14:07:08 +08:00
// 属性接口
2026-01-27 16:36:59 +08:00
property var value
2026-02-06 14:07:08 +08:00
property string fileFilter: "所有文件 (*.*)" // 文件过滤器
property bool selectMultiple: false // 是否多选
implicitHeight: fileBox.implicitHeight
signal asValueChanged(text: var)
2026-01-27 16:36:59 +08:00
2026-02-06 14:07:08 +08:00
function setValue(newValue: var) {
if (newValue !== value) {
2026-01-27 16:36:59 +08:00
value = newValue
2026-02-06 14:07:08 +08:00
fileBox.value = value
2026-01-27 16:36:59 +08:00
asValueChanged(value)
}
}
2026-02-06 14:07:08 +08:00
LineTextBox {
id: fileBox
value: control.value
anchors.left: parent.left
anchors.right: button.left
anchors.verticalCenter: parent.verticalCenter
onValueChanged: {
control.setValue(value)
}
}
Button {
id: button
anchors.right: parent.right
anchors.verticalCenter: parent.verticalCenter
width: 30
height: 25
text: "..."
palette.buttonText: ColorPalette.theme.textPrimary
2026-01-27 16:36:59 +08:00
background: Rectangle {
2026-02-06 14:07:08 +08:00
color: ColorPalette.theme.buttonBackground
2026-01-27 16:36:59 +08:00
}
2026-02-06 14:07:08 +08:00
2026-01-27 16:36:59 +08:00
onClicked: {
2026-02-06 14:07:08 +08:00
fileDialog.open()
2026-01-27 16:36:59 +08:00
}
}
2026-02-06 14:07:08 +08:00
FileDialog {
id: fileDialog
title: control.selectMultiple ? "选择多个文件" : "选择文件"
fileMode: control.selectMultiple ? FileDialog.OpenFiles : FileDialog.OpenFile
// 设置文件过滤器
nameFilters: {
if (control.fileFilter) {
return control.fileFilter.split(";;")
}
return ["所有文件 (*.*)"]
}
2026-01-27 16:36:59 +08:00
onAccepted: {
2026-02-06 14:07:08 +08:00
if (control.selectMultiple) {
// 多选模式
var filePaths = []
for (var i = 0; i < selectedFiles.length; i++) {
var filePath = selectedFiles[i].toString()
filePath = removeFileProtocol(filePath)
filePaths.push(filePath)
}
// 多个文件用分号分隔
control.setValue(filePaths.join(";"))
} else {
// 单选模式
var filePath = selectedFile.toString()
filePath = removeFileProtocol(filePath)
control.setValue(filePath)
}
2026-01-27 16:36:59 +08:00
}
}
2026-02-06 14:07:08 +08:00
// 辅助函数:移除文件协议前缀
function removeFileProtocol(path) {
if (path.startsWith("file:///")) {
return path.substring(8)
} else if (path.startsWith("file://")) {
return path.substring(7)
}
return path
}
2026-01-27 16:36:59 +08:00
}