FileDialogue.qml 12.6 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2019 CZ.NIC
3 4 5 6 7 8 9 10 11 12 13 14
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23 24
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations including
 * the two.
 */

import QtQuick 2.7
25
import QtQuick.Controls 2.2
26 27
import QtQuick.Layouts 1.3
import Qt.labs.folderlistmodel 2.1
28
import cz.nic.mobileDatovka 1.0
29 30 31 32 33 34 35
import cz.nic.mobileDatovka.qmlInteraction 1.0

Dialog {
    id: root

    focus: true
    modal: true
36
    title: qsTr("Select path")
37 38 39 40 41 42 43 44
    //standardButtons: (selectedFileIndex != -1) ? (Dialog.Ok | Dialog.Cancel) : Dialog.Cancel

    footer: DialogButtonBox {
        AccessibleButton {
            text: qsTr("Cancel")
            DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
        }
        AccessibleButton {
45
            text: qsTr("OK")
46 47
            enabled: ((root.showDirs && !root.showFiles) || (selectedFileIndex != -1))
            visible: ((root.showDirs && !root.showFiles) || (selectedFileIndex != -1))
48 49 50
            DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
        }
    }
51 52

    /* Place the dialogue in the centre. */
53 54
    x: 2 * defaultMargin
    y: 2 * defaultMargin
55

56 57
    height: parent.height - 4 * defaultMargin
    width: parent.width - 4 * defaultMargin
58

59 60
    /* This property must be set by caller. */
    property bool multiSelect: false
61

62
    property int selectedFileIndex: -1
63 64
    property bool showDirs: true
    property bool showFiles: true
65

66 67
    signal finished(variant pathListModel)

68 69 70 71
    function directoryOnly() {
        return (root.showDirs && !root.showFiles);
    }

72
    function raise(title, filters, showFiles, targetLocation) {
73 74 75 76
        if (targetLocation !== "") {
            folderNavigation.visible = false
            folderModel.folder = "file://" + targetLocation
        }
77
        root.title = title
78
        folderModel.folder = folderModel.folder
79
        root.showFiles = showFiles
80
        if (filters !== "") {
81
            folderModel.nameFilters = filters
82
        }
83 84 85 86 87 88 89
        selectedFileIndex = -1
        // clear add path list model
        pathListModel.clear()
        // deselect all item in the file idalog
        for (fileList.currentIndex = 0; fileList.currentIndex < fileList.count; ++fileList.currentIndex) {
            fileList.currentItem.color = datovkaPalette.window
        }
90
        if (directoryOnly()) {
91 92 93 94
            var path = stripUrlPrefix(folderModel.folder)
            pathListModel.clear()
            pathListModel.append({path: path})
        }
95 96 97 98 99 100 101
        root.open()
    }

    function stripUrlPrefix(url) {
        return url.toString().replace(/^(file:\/{2})/, "")
    }

102 103 104 105
    function standardLocationUrl(location) {
        return "file://" + interactionFilesystem.locate(location)
    }

106 107 108 109 110 111 112 113 114 115 116 117
    // retrun valid index if item is selected or invalid index
    function isSelected(pathListModel, fileIndex) {
        var path = stripUrlPrefix(folderModel.folder) + "/" + folderModel.get(fileIndex, "fileName")
        var listLength = pathListModel.count
        for (var j = 0; j < listLength; ++j) {
            if (pathListModel.get(j).path === path) {
                return j
            }
        }
        return -1
    }

118 119 120 121
    InteractionFilesystem {
        id: interactionFilesystem
    }

122 123 124 125
    ListModel {
        id: pathListModel
    }

126
    contentItem: ColumnLayout {
127
        spacing: formItemVerticalSpacing
128 129 130
        ColumnLayout {
            id: folderNavigation
            AccessibleComboBox {
131
                Layout.fillWidth: true
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
                accessibleDescription: qsTr("Select location type")
                model: ListModel {
                    ListElement { label: qsTr("Desktop"); key: "desktop" }
                    ListElement { label: qsTr("Documents"); key: "document" }
                    ListElement { label: qsTr("Downloads"); key: "download" }
                    ListElement { label: qsTr("Pictures"); key: "picture" }
                    ListElement { label: qsTr("Temp"); key: "temp" }
                }
                onCurrentIndexChanged: {
                    var location = InteractionFilesystem.DESKTOP_LOCATION
                    if (currentKey() === "document") {
                        location = InteractionFilesystem.DOCUMENTS_LOCATION
                    } else if (currentKey() === "download") {
                        location = InteractionFilesystem.DOWNLOAD_LOCATION
                    } else if (currentKey() === "picture") {
                        location = InteractionFilesystem.PICTURE_LOCATION
                    } else if (currentKey() === "temp") {
                        location = InteractionFilesystem.TEMP_LOCATION
150
                    }
151
                    folderModel.folder = standardLocationUrl(location)
152 153
                }
            }
154
            RowLayout {
155
                Layout.fillWidth: true
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
                spacing: formItemVerticalSpacing
                AccessibleButton {
                    id: upButton
                    text: "<"
                    accessibleName: qsTr("Up") /* Needs to be specified as "<" is not read. */
                    onClicked: {
                        /* Navigate to parent folder. */
                        if (folderModel.parentFolder !== "") {
                            folderModel.folder = folderModel.parentFolder
                        }
                    }
                }
                AccessibleTextField {
                    id: pathField
                    Layout.fillWidth: true
                    text: stripUrlPrefix(folderModel.folder)
                    onEditingFinished: {
                        /* Navigate to supplied location if location exists. */
                        var path = interactionFilesystem.absoluteDirPath(text)
175
                        if (path !== "") {
176 177
                            folderModel.folder = "file://" + path
                        } else {
178 179 180 181 182 183 184
                            path = interactionFilesystem.absolutePath(text)
                            if (path !== "") {
                                folderModel.folder = "file://" + path
                            } else {
                                /* Restore original location. */
                                text = stripUrlPrefix(folderModel.folder)
                            }
185 186 187
                        }
                    }
                }
188 189
            } // RowLayout
        } // ColumnLayout
190
        ScrollableListView {
191
            id: fileList
192 193 194

            delegateHeight: imgHeightHeader

195 196 197
            /* Fill remaining space with the list content. */
            Layout.fillHeight: true
            Layout.fillWidth: true
198 199
            FolderListModel {
                id: folderModel
200 201
                showDirs: root.showDirs
                showFiles: root.showFiles
202 203
                showDirsFirst: true
                nameFilters: ["*.*"]
204
                folder: standardLocationUrl(InteractionFilesystem.DOCUMENTS_LOCATION)
205
                onFolderChanged: {
206
                    selectedFileIndex = -1
207 208
                    pathField.text = stripUrlPrefix(folder)
                }
209
            } // FolderListModel
210 211 212
            Component {
                id: fileDelegate
                Rectangle {
213 214 215 216 217 218 219 220
                    id: fileRectangle
                    color: if (multiSelect) {
                            // if files are in the pathList, select them
                            (isSelected(pathListModel, index) >= 0) ? datovkaPalette.highlight : datovkaPalette.window
                        } else {
                            // only one file can be selected
                            (index === selectedFileIndex) ? datovkaPalette.highlight : datovkaPalette.window
                        }
221
                    width: fileList.width
222
                    height: fileList.delegateHeight
223 224 225 226 227 228 229 230 231
                    Text {
                        anchors.left: parent.left
                        elide: Text.ElideRight
                        text: fileName
                    }
                    Text {
                        anchors.right: parent.right
                        /* Add '>' to directory names. */
                        text: fileIsDir ? " >" : ""
232 233
                    }
                    MouseArea {
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
                        function accessibleDescriptionText() {
                            var aText = "";
                            if (fileIsDir) {
                                aText += qsTr("Open directory.");
                            } else {
                                if (isSelected(pathListModel, index) >= 0) {
                                    aText += qsTr("File is selected.")
                                } else {
                                    aText += qsTr("File is not selected.")
                                }
                            }
                            return aText;
                        }

                        function handleClick() {
249
                            var path
250
                            if (fileIsDir) {
251
                                /* Navigate to selected directory. */
252
                                folderModel.folder = fileURL
253
                                if (directoryOnly()) {
254 255 256 257
                                    pathListModel.clear()
                                    path = stripUrlPrefix(folderModel.folder)
                                    pathListModel.append({path: path})
                                }
258
                            } else {
259 260
                                /* Select file. */
                                selectedFileIndex = index
261
                                path = stripUrlPrefix(folderModel.folder) + "/" + folderModel.get(selectedFileIndex, "fileName")
262
                                if (!multiSelect) {
263 264 265 266
                                    // deselect all item in the file idalog
                                    for (fileList.currentIndex = 0; fileList.currentIndex < fileList.count; ++fileList.currentIndex) {
                                        fileList.currentItem.color = datovkaPalette.window
                                    }
267 268
                                    // only one file can be selected and append to list
                                    pathListModel.clear()
269
                                    fileRectangle.color = datovkaPalette.highlight
270 271 272 273 274 275 276 277 278 279 280 281
                                    pathListModel.append({path: path})
                                } else {
                                    // file can be append to list or removed and deselected
                                    var idx = isSelected(pathListModel, index);
                                    if (idx >= 0) {
                                        fileRectangle.color = datovkaPalette.window
                                        pathListModel.remove(idx)
                                    } else {
                                        fileRectangle.color = datovkaPalette.highlight
                                        pathListModel.append({path: path})
                                    }
                                }
282 283
                            }
                        }
284 285 286 287 288 289

                        anchors.fill: parent

                        Accessible.role: Accessible.Button
                        Accessible.description: accessibleDescriptionText()
                        Accessible.name: fileName
290 291
                        Accessible.onScrollDownAction: fileList.scrollDown()
                        Accessible.onScrollUpAction: fileList.scrollUp()
292 293 294 295 296 297
                        Accessible.onPressAction: {
                            handleClick()
                        }
                        onClicked: {
                            handleClick()
                        }
298 299
                    }
                }
300
            } // Component
301 302
            model: folderModel
            delegate: fileDelegate
303 304
        } // ListView
    } // ColumnLayout
305
    onAccepted: {
306 307
        selectedFileIndex = -1
        finished(pathListModel)
308 309
    }
    onRejected: {
310 311
        selectedFileIndex = -1
        pathListModel.clear()
312 313
    }
}