| 1 | // PathUrlHelper.cxx - manage a stack of QML items |
| 2 | // |
| 3 | // Written by James Turner, started March 2018 |
| 4 | // |
| 5 | // Copyright (C) 2018 James Turner <[email protected]> |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | |
| 21 | |
| 22 | #include "PathUrlHelper.hxx" |
| 23 | |
| 24 | #include <QFileDialog> |
| 25 | |
| 26 | FileDialogWrapper::FileDialogWrapper(QObject *parent) : QObject(parent) |
| 27 | { |
| 28 | |
| 29 | } |
| 30 | |
| 31 | QString FileDialogWrapper::urlToLocalFilePath(QUrl url) const |
| 32 | { |
| 33 | return url.toLocalFile(); |
| 34 | } |
| 35 | |
| 36 | QUrl FileDialogWrapper::urlFromLocalFilePath(QString path) const |
| 37 | { |
| 38 | return QUrl::fromLocalFile(path); |
| 39 | } |
| 40 | |
| 41 | void FileDialogWrapper::open() |
| 42 | { |
| 43 | QUrl u; |
| 44 | if (m_selectFolder) { |
| 45 | u = QFileDialog::getExistingDirectoryUrl(nullptr, m_dialogTitle, |
| 46 | m_currentFolder); |
| 47 | |
| 48 | } else { |
| 49 | u = QFileDialog::getOpenFileUrl(nullptr, m_dialogTitle, m_currentFolder, |
| 50 | m_filter); |
| 51 | } |
| 52 | |
| 53 | if (u.isValid()) { |
| 54 | m_fileUrl = u; |
| 55 | accepted(); |
| 56 | } else { |
| 57 | rejected(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | QUrl FileDialogWrapper::folder() const |
| 62 | { |
| 63 | return m_currentFolder; |
| 64 | } |
| 65 | |
| 66 | QString FileDialogWrapper::title() const |
| 67 | { |
| 68 | return m_dialogTitle; |
| 69 | } |
| 70 | |
| 71 | bool FileDialogWrapper::selectFolder() const |
| 72 | { |
| 73 | return m_selectFolder; |
| 74 | } |
| 75 | |
| 76 | QUrl FileDialogWrapper::fileUrl() const |
| 77 | { |
| 78 | return m_fileUrl; |
| 79 | } |
| 80 | |
| 81 | QString FileDialogWrapper::filePath() const |
| 82 | { |
| 83 | return urlToLocalFilePath(fileUrl()); |
| 84 | } |
| 85 | |
| 86 | QString FileDialogWrapper::filter() const |
| 87 | { |
| 88 | return m_filter; |
| 89 | } |
| 90 | |
| 91 | void FileDialogWrapper::setFolder(QUrl folder) |
| 92 | { |
| 93 | if (m_currentFolder == folder) |
| 94 | return; |
| 95 | |
| 96 | m_currentFolder = folder; |
| 97 | emit folderChanged(m_currentFolder); |
| 98 | } |
| 99 | |
| 100 | void FileDialogWrapper::setTitle(QString title) |
| 101 | { |
| 102 | if (m_dialogTitle == title) |
| 103 | return; |
| 104 | |
| 105 | m_dialogTitle = title; |
| 106 | emit titleChanged(m_dialogTitle); |
| 107 | } |
| 108 | |
| 109 | void FileDialogWrapper::setSelectFolder(bool selectFolder) |
| 110 | { |
| 111 | if (m_selectFolder == selectFolder) |
| 112 | return; |
| 113 | |
| 114 | m_selectFolder = selectFolder; |
| 115 | emit selectFolderChanged(m_selectFolder); |
| 116 | } |
| 117 | |
| 118 | void FileDialogWrapper::setFileUrl(QUrl fileUrl) |
| 119 | { |
| 120 | if (m_fileUrl == fileUrl) |
| 121 | return; |
| 122 | |
| 123 | m_fileUrl = fileUrl; |
| 124 | emit fileUrlChanged(m_fileUrl); |
| 125 | } |
| 126 | |
| 127 | void FileDialogWrapper::setFilePath(QString filePath) |
| 128 | { |
| 129 | setFileUrl(urlFromLocalFilePath(filePath)); |
| 130 | } |
| 131 | |
| 132 | void FileDialogWrapper::setFilter(QString filter) |
| 133 | { |
| 134 | if (m_filter == filter) |
| 135 | return; |
| 136 | |
| 137 | m_filter = filter; |
| 138 | emit filterChanged(m_filter); |
| 139 | } |