2019-07-16 06:14:08 +10:00
|
|
|
import QtQuick 2.12
|
|
|
|
import Qt.labs.platform 1.1
|
|
|
|
|
|
|
|
Item {
|
2019-07-16 18:37:39 +10:00
|
|
|
id: opener
|
2019-08-29 01:54:25 +10:00
|
|
|
anchors.fill: fill ? parent : undefined
|
|
|
|
|
2019-11-06 23:55:47 +11:00
|
|
|
|
2019-11-07 08:13:15 +11:00
|
|
|
signal filePicked(string file)
|
|
|
|
signal filesPicked(var files)
|
2019-11-07 07:45:33 +11:00
|
|
|
signal cancelled()
|
2019-11-06 23:55:47 +11:00
|
|
|
|
|
|
|
|
2019-08-29 01:54:25 +10:00
|
|
|
property bool fill: true
|
2019-07-16 06:14:08 +10:00
|
|
|
|
|
|
|
property alias dialog: fileDialog
|
2019-07-16 18:37:39 +10:00
|
|
|
property string selectedFile: ""
|
|
|
|
property string file: ""
|
2019-11-07 08:13:15 +11:00
|
|
|
property var selectedFiles: []
|
|
|
|
property var files: []
|
2019-07-16 06:14:08 +10:00
|
|
|
|
2019-10-28 06:07:58 +11:00
|
|
|
property string selectSubject:
|
|
|
|
dialog.fileMode === FileDialog.SaveFile ? qsTr("file") : qsTr("open")
|
|
|
|
|
2019-07-16 06:14:08 +10:00
|
|
|
enum FileType { All, Images }
|
2019-07-25 16:43:52 +10:00
|
|
|
property int fileType: HFileDialogOpener.FileType.All
|
2019-07-16 06:14:08 +10:00
|
|
|
|
2019-11-06 23:55:47 +11:00
|
|
|
|
2019-08-29 01:54:25 +10:00
|
|
|
TapHandler { enabled: fill; onTapped: fileDialog.open() }
|
2019-07-16 06:14:08 +10:00
|
|
|
|
|
|
|
FileDialog {
|
|
|
|
id: fileDialog
|
|
|
|
|
|
|
|
property var filters: ({
|
|
|
|
all: qsTr("All files") + " (*)",
|
|
|
|
images: qsTr("Image files") +
|
|
|
|
" (*.jpg *.jpeg *.png *.gif *.bmp *.webp)"
|
|
|
|
})
|
|
|
|
|
|
|
|
nameFilters:
|
|
|
|
fileType == HFileDialogOpener.FileType.Images ?
|
|
|
|
[filters.images, filters.all] :
|
|
|
|
[filters.all]
|
|
|
|
|
|
|
|
folder: StandardPaths.writableLocation(
|
|
|
|
fileType == HFileDialogOpener.FileType.Images ?
|
|
|
|
StandardPaths.PicturesLocation :
|
|
|
|
StandardPaths.HomeLocation
|
|
|
|
)
|
|
|
|
|
2019-10-28 06:07:58 +11:00
|
|
|
title: fileMode === FileDialog.OpenFile ?
|
|
|
|
qsTr("Select a file to open") :
|
|
|
|
|
|
|
|
fileMode === FileDialog.OpenFiles ?
|
|
|
|
qsTr("Select files to open") :
|
|
|
|
|
|
|
|
fileMode === FileDialog.SaveFile ?
|
|
|
|
qsTr("Save as...") :
|
|
|
|
|
|
|
|
""
|
|
|
|
|
2019-07-16 10:10:43 +10:00
|
|
|
modality: Qt.NonModal
|
2019-07-16 06:14:08 +10:00
|
|
|
|
|
|
|
onVisibleChanged: if (visible) {
|
2019-11-07 08:13:15 +11:00
|
|
|
opener.selectedFile = Qt.binding(() => Qt.resolvedUrl(currentFile))
|
|
|
|
opener.file = Qt.binding(() => Qt.resolvedUrl(file))
|
|
|
|
opener.files = Qt.binding(() => Qt.resolvedUrl(files))
|
|
|
|
opener.selectedFiles =
|
|
|
|
Qt.binding(() => Qt.resolvedUrl(currentFiles))
|
2019-07-16 06:14:08 +10:00
|
|
|
}
|
2019-11-07 08:13:15 +11:00
|
|
|
|
2019-11-06 23:55:47 +11:00
|
|
|
onAccepted: {
|
2019-11-07 08:13:15 +11:00
|
|
|
opener.selectedFile = currentFile
|
|
|
|
opener.selectedFiles = currentFiles
|
|
|
|
opener.file = file
|
|
|
|
opener.files = files
|
|
|
|
|
2019-11-06 23:55:47 +11:00
|
|
|
opener.filePicked(file)
|
2019-11-07 08:13:15 +11:00
|
|
|
opener.filesPicked(files)
|
2019-11-06 23:55:47 +11:00
|
|
|
}
|
2019-11-07 08:13:15 +11:00
|
|
|
|
2019-11-07 07:45:33 +11:00
|
|
|
onRejected: {
|
2019-11-07 08:13:15 +11:00
|
|
|
selectedFile = ""
|
|
|
|
file = ""
|
|
|
|
selectedFiles = ""
|
|
|
|
files = ""
|
2019-11-07 07:45:33 +11:00
|
|
|
cancelled()
|
|
|
|
}
|
2019-07-16 06:14:08 +10:00
|
|
|
}
|
|
|
|
}
|