FileDialogue.qml 11.8 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2018 CZ.NIC
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * 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: (onlyDir || selectedFileIndex != -1)
            visible: (onlyDir || 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
    property bool onlyDir: false
64

65 66
    signal finished(variant pathListModel)

67
    function raise(title, filters, showFiles) {
68
        root.title = title
69
        folderModel.folder = folderModel.folder
70
        folderModel.showFiles = showFiles
71
        onlyDir = !showFiles
72
        if (filters !== "") {
73 74
            folderModel.nameFilters = [filters]
        }
75 76 77 78 79 80 81
        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
        }
82 83 84 85 86
        if (onlyDir) {
            var path = stripUrlPrefix(folderModel.folder)
            pathListModel.clear()
            pathListModel.append({path: path})
        }
87 88 89 90 91 92 93
        root.open()
    }

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

94 95 96 97
    function standardLocationUrl(location) {
        return "file://" + interactionFilesystem.locate(location)
    }

98 99 100 101 102 103 104 105 106 107 108 109
    // 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
    }

110 111 112 113
    InteractionFilesystem {
        id: interactionFilesystem
    }

114 115 116 117
    ListModel {
        id: pathListModel
    }

118
    contentItem: ColumnLayout {
119
        spacing: formItemVerticalSpacing
120
        AccessibleComboBox {
121 122 123 124
            anchors {
                left: parent.left;
                right: parent.right;
            }
125
            accessibleDescription: qsTr("Select location type")
126
            model: ListModel {
127 128 129 130 131
                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" }
132
            }
Karel Slaný's avatar
Karel Slaný committed
133
            onCurrentIndexChanged: {
134
                var location = InteractionFilesystem.DESKTOP_LOCATION
135
                if (currentKey() === "document") {
136
                    location = InteractionFilesystem.DOCUMENTS_LOCATION
137
                } else if (currentKey() === "download") {
138
                    location = InteractionFilesystem.DOWNLOAD_LOCATION
139
                } else if (currentKey() === "picture") {
140
                    location = InteractionFilesystem.PICTURE_LOCATION
141
                } else if (currentKey() === "temp") {
142
                    location = InteractionFilesystem.TEMP_LOCATION
143 144 145
                }
                folderModel.folder = standardLocationUrl(location)
            }
146
        }
147 148 149 150 151
        RowLayout {
            anchors {
                left: parent.left;
                right: parent.right;
            }
152
            spacing: formItemVerticalSpacing
153
            AccessibleButton {
154 155
                id: upButton
                text: "<"
Karel Slaný's avatar
Karel Slaný committed
156
                accessibleName: qsTr("Up") /* Needs to be specified as "<" is not read. */
157 158 159 160
                onClicked: {
                    /* Navigate to parent folder. */
                    if (folderModel.parentFolder !== "") {
                        folderModel.folder = folderModel.parentFolder
161 162 163
                    }
                }
            }
164
            AccessibleTextField {
165 166 167 168 169 170
                id: pathField
                Layout.fillWidth: true
                text: stripUrlPrefix(folderModel.folder)
                onEditingFinished: {
                    /* Navigate to supplied location if location exists. */
                    var path = interactionFilesystem.absoluteDirPath(text)
171
                    if (path !== "") {
172 173 174
                        folderModel.folder = "file://" + path
                    } else {
                        path = interactionFilesystem.absolutePath(text)
175
                        if (path !== "") {
176 177 178 179 180 181 182
                            folderModel.folder = "file://" + path
                        } else {
                            /* Restore original location. */
                            text = stripUrlPrefix(folderModel.folder)
                        }
                    }
                }
183
            }
184
        } // RowLayout
185
        ScrollableListView {
186
            id: fileList
187 188 189

            delegateHeight: imgHeightHeader

190 191 192
            /* Fill remaining space with the list content. */
            Layout.fillHeight: true
            Layout.fillWidth: true
193 194 195 196
            FolderListModel {
                id: folderModel
                showDirsFirst: true
                nameFilters: ["*.*"]
197
                folder: standardLocationUrl(InteractionFilesystem.DESKTOP_LOCATION)
198
                onFolderChanged: {
199
                    selectedFileIndex = -1
200 201
                    pathField.text = stripUrlPrefix(folder)
                }
202
            } // FolderListModel
203 204 205
            Component {
                id: fileDelegate
                Rectangle {
206 207 208 209 210 211 212 213
                    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
                        }
214
                    width: fileList.width
215
                    height: fileList.delegateHeight
216
                    RowLayout {
217
                        anchors.fill: parent
218
                        Text {
219
                            anchors.left: parent.left
220 221 222 223 224 225
                            Layout.fillWidth: true
                            elide: Text.ElideRight
                            /* Add '>' to directory names. */
                            text: fileName
                        }
                        Text {
226
                            anchors.right: parent.right
227 228 229
                            /* Add '>' to directory names. */
                            text: fileIsDir ? " >" : ""
                        }
230 231
                    }
                    MouseArea {
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
                        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() {
247
                            var path
248
                            if (fileIsDir) {
249
                                /* Navigate to selected directory. */
250
                                folderModel.folder = fileURL
251 252 253 254 255
                                if (onlyDir) {
                                    pathListModel.clear()
                                    path = stripUrlPrefix(folderModel.folder)
                                    pathListModel.append({path: path})
                                }
256
                            } else {
257 258
                                /* Select file. */
                                selectedFileIndex = index
259
                                path = stripUrlPrefix(folderModel.folder) + "/" + folderModel.get(selectedFileIndex, "fileName")
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
                                if (!multiSelect) {
                                    // only one file can be selected and append to list
                                    pathListModel.clear()
                                    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})
                                    }
                                }
275 276
                            }
                        }
277 278 279 280 281 282

                        anchors.fill: parent

                        Accessible.role: Accessible.Button
                        Accessible.description: accessibleDescriptionText()
                        Accessible.name: fileName
283 284
                        Accessible.onScrollDownAction: fileList.scrollDown()
                        Accessible.onScrollUpAction: fileList.scrollUp()
285 286 287 288 289 290
                        Accessible.onPressAction: {
                            handleClick()
                        }
                        onClicked: {
                            handleClick()
                        }
291 292
                    }
                }
293
            } // Component
294 295
            model: folderModel
            delegate: fileDelegate
296 297
        } // ListView
    } // ColumnLayout
298
    onAccepted: {
299 300
        selectedFileIndex = -1
        finished(pathListModel)
301 302
    }
    onRejected: {
303 304
        selectedFileIndex = -1
        pathListModel.clear()
305 306
    }
}