|
| 1 | +// CustomFolderDialog.qml |
| 2 | +import QtQuick 6.5 |
| 3 | +import QtQuick.Controls 6.5 |
| 4 | +import QtQuick.Layouts 6.5 |
| 5 | +import Qt.labs.folderlistmodel 2.2 |
| 6 | +import QtQuick.VirtualKeyboard 6.5 |
| 7 | +import QtCore |
| 8 | + |
| 9 | +Dialog { |
| 10 | + id: root |
| 11 | + modal: true |
| 12 | + width: 800 |
| 13 | + height: 600 |
| 14 | + standardButtons: Dialog.NoButton |
| 15 | + |
| 16 | + property url currentFolder: StandardPaths.writableLocation(StandardPaths.PicturesLocation) |
| 17 | + |
| 18 | + signal canceled() |
| 19 | + |
| 20 | + property bool createMode: false |
| 21 | + |
| 22 | + Rectangle { |
| 23 | + anchors.fill: parent |
| 24 | + color: Material.background |
| 25 | + border.color: Material.foreground |
| 26 | + radius: 8 |
| 27 | + |
| 28 | + ColumnLayout { |
| 29 | + anchors.fill: parent |
| 30 | + spacing: 10 |
| 31 | + |
| 32 | + Text { |
| 33 | + text: qsTr("Select Folder") |
| 34 | + font.pixelSize: 20 |
| 35 | + Layout.alignment: Qt.AlignHCenter |
| 36 | + } |
| 37 | + |
| 38 | + RowLayout { |
| 39 | + id: breadcrumbBar |
| 40 | + Layout.fillWidth: true |
| 41 | + spacing: 4 |
| 42 | + |
| 43 | + // Root Button "/" |
| 44 | + Button { |
| 45 | + text: "/" |
| 46 | + font.pixelSize: 14 |
| 47 | + onClicked: { |
| 48 | + root.currentFolder = "file:///" |
| 49 | + folderModel.folder = root.currentFolder |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + Repeater { |
| 54 | + model:{ |
| 55 | + var path = root.currentFolder.toString().replace("file://", "") |
| 56 | + var segments = path.split("/").filter(s => s.length > 0) |
| 57 | + return segments |
| 58 | + } |
| 59 | + |
| 60 | + delegate: RowLayout { |
| 61 | + spacing: 2 |
| 62 | + |
| 63 | + Button { |
| 64 | + text: modelData === "/" ? "/" : modelData |
| 65 | + font.pixelSize: 14 |
| 66 | + onClicked: { |
| 67 | + let segments = root.currentFolder.toString().replace("file://", "").split("/").filter(s => s.length > 0) |
| 68 | + let newPath = "/" + segments.slice(0, index + 1).join("/") |
| 69 | + root.currentFolder = "file://" + newPath |
| 70 | + folderModel.folder = root.currentFolder |
| 71 | + |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + Text { |
| 76 | + text: index < breadcrumbBarRepeater.count - 1 ? " / " : "" |
| 77 | + font.pixelSize: 14 |
| 78 | + color: Material.foreground |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + id: breadcrumbBarRepeater |
| 83 | + } |
| 84 | + |
| 85 | + Item { |
| 86 | + Layout.fillWidth: true |
| 87 | + } |
| 88 | + |
| 89 | + Button { |
| 90 | + text: createMode ? qsTr("Cancel") : qsTr("New Folder") |
| 91 | + onClicked: { |
| 92 | + createMode = !createMode |
| 93 | + if (!createMode) |
| 94 | + newFolderNameField.text = "" |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + RowLayout { |
| 100 | + ToolSeparator { |
| 101 | + Layout.fillWidth: true |
| 102 | + orientation: Qt.Horizontal |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Inline New Folder Creation |
| 107 | + RowLayout { |
| 108 | + visible: createMode |
| 109 | + Layout.fillWidth: true |
| 110 | + spacing: 10 |
| 111 | + |
| 112 | + TextField { |
| 113 | + id: newFolderNameField |
| 114 | + placeholderText: "New folder name" |
| 115 | + Layout.fillWidth: true |
| 116 | + inputMethodHints: Qt.ImhNoPredictiveText |
| 117 | + focus: createMode |
| 118 | + onActiveFocusChanged: { |
| 119 | + if (activeFocus && createMode) { |
| 120 | + Qt.inputMethod.show() |
| 121 | + } else { |
| 122 | + Qt.inputMethod.hide() |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + Button { |
| 128 | + text: "Create" |
| 129 | + Button { |
| 130 | + text: "Create" |
| 131 | + onClicked: { |
| 132 | + const name = newFolderNameField.text.trim() |
| 133 | + if (name.length > 0) { |
| 134 | + var basePath = currentFolder.toString().replace("file://", "") |
| 135 | + var newPath = basePath + "/" + name |
| 136 | + var success = filesystem.createFolder(newPath) |
| 137 | + console.log("Folder created:", success, newPath) |
| 138 | + |
| 139 | + if (success) { |
| 140 | + createMode = false |
| 141 | + newFolderNameField.text = "" |
| 142 | + Qt.inputMethod.hide() |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + ListView { |
| 151 | + id: folderList |
| 152 | + Layout.fillWidth: true |
| 153 | + Layout.fillHeight: true |
| 154 | + clip: true |
| 155 | + |
| 156 | + ScrollBar.vertical: ScrollBar { |
| 157 | + policy: ScrollBar.AlwaysOn |
| 158 | + } |
| 159 | + |
| 160 | + model: FolderListModel { |
| 161 | + id: folderModel |
| 162 | + folder: currentFolder |
| 163 | + showDirs: true |
| 164 | + showFiles: false |
| 165 | + sortField: FolderListModel.Name |
| 166 | + } |
| 167 | + |
| 168 | + delegate: Rectangle { |
| 169 | + width: folderList.width |
| 170 | + height: 48 |
| 171 | + color: model.fileURL === currentFolder ? "#ddeeff" : "transparent" |
| 172 | + |
| 173 | + Text { |
| 174 | + text: fileName |
| 175 | + anchors.verticalCenter: parent.verticalCenter |
| 176 | + anchors.left: parent.left |
| 177 | + anchors.leftMargin: 12 |
| 178 | + font.pixelSize: 16 |
| 179 | + color: Material.foreground |
| 180 | + } |
| 181 | + |
| 182 | + MouseArea { |
| 183 | + anchors.fill: parent |
| 184 | + onClicked: { |
| 185 | + root.currentFolder = model.fileURL |
| 186 | + folderModel.folder = model.fileURL |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + RowLayout { |
| 193 | + Layout.alignment: Qt.AlignHCenter |
| 194 | + spacing: 20 |
| 195 | + |
| 196 | + Button { |
| 197 | + text: "Select" |
| 198 | + onClicked: { |
| 199 | + root.accepted() |
| 200 | + root.close() |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + Button { |
| 205 | + text: "Cancel" |
| 206 | + onClicked: { |
| 207 | + root.close() |
| 208 | + root.canceled() |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + // Virtual Keyboard - only visible if TextField is active & createMode is on |
| 215 | + InputPanel { |
| 216 | + id: inputPanel |
| 217 | + y: parent.height - height |
| 218 | + width: parent.width > 600 ? 600 : parent.width |
| 219 | + visible: Qt.inputMethod.visible && createMode |
| 220 | + anchors.horizontalCenter: parent.horizontalCenter |
| 221 | + z: 100 |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments