|
| 1 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | + |
| 3 | +#include <QFileInfo> |
| 4 | +#include <QFileDialog> |
| 5 | +#include <QStandardPaths> |
| 6 | + |
| 7 | +#include "filedialog.h" |
| 8 | + |
| 9 | +using namespace scratchcpp::uicomponents; |
| 10 | + |
| 11 | +FileDialog::FileDialog(QObject *parent) : |
| 12 | + QObject(parent) |
| 13 | +{ |
| 14 | +} |
| 15 | + |
| 16 | +const QStringList &FileDialog::nameFilters(void) const |
| 17 | +{ |
| 18 | + return m_nameFilters; |
| 19 | +} |
| 20 | + |
| 21 | +void FileDialog::setNameFilters(const QStringList &filters) |
| 22 | +{ |
| 23 | + m_nameFilters = filters; |
| 24 | + emit nameFiltersChanged(); |
| 25 | +} |
| 26 | + |
| 27 | +bool FileDialog::showAllFiles(void) const |
| 28 | +{ |
| 29 | + return m_showAllFiles; |
| 30 | +} |
| 31 | + |
| 32 | +void FileDialog::setShowAllFiles(bool value) |
| 33 | +{ |
| 34 | + m_showAllFiles = value; |
| 35 | + emit showAllFilesChanged(); |
| 36 | +} |
| 37 | + |
| 38 | +const QString &FileDialog::fileName(void) const |
| 39 | +{ |
| 40 | + return m_fileName; |
| 41 | +} |
| 42 | + |
| 43 | +QString FileDialog::shortFileName(void) const |
| 44 | +{ |
| 45 | + QFileInfo fileInfo(m_fileName); |
| 46 | + return fileInfo.fileName(); |
| 47 | +} |
| 48 | + |
| 49 | +const QString &FileDialog::defaultSuffix() const |
| 50 | +{ |
| 51 | + return m_defaultSuffix; |
| 52 | +} |
| 53 | + |
| 54 | +void FileDialog::setDefaultSuffix(const QString &newDefaultSuffix) |
| 55 | +{ |
| 56 | + if (m_defaultSuffix == newDefaultSuffix) |
| 57 | + return; |
| 58 | + |
| 59 | + m_defaultSuffix = newDefaultSuffix; |
| 60 | + emit defaultSuffixChanged(); |
| 61 | +} |
| 62 | + |
| 63 | +void FileDialog::getOpenFileContent(void) |
| 64 | +{ |
| 65 | + auto fileContentReadyLambda = [this](const QString &fileName, const QByteArray &fileContent) { |
| 66 | + if (!fileName.isEmpty()) { |
| 67 | + m_fileName = fileName; |
| 68 | + emit fileNameChanged(); |
| 69 | + emit shortFileNameChanged(); |
| 70 | + emit fileContentReady(fileContent); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | +#ifdef Q_OS_WASM |
| 75 | + QFileDialog::getOpenFileContent(QString(), fileContentReadyLambda); |
| 76 | +#else |
| 77 | + QString fileName = QFileDialog::getOpenFileName(nullptr, QString(), QStandardPaths::standardLocations(QStandardPaths::HomeLocation)[0], getFilters()); |
| 78 | + |
| 79 | + if (fileName != "") { |
| 80 | + QFile file(fileName); |
| 81 | + |
| 82 | + if (file.open(QIODevice::ReadOnly)) |
| 83 | + fileContentReadyLambda(fileName, file.readAll()); |
| 84 | + } |
| 85 | +#endif |
| 86 | +} |
| 87 | + |
| 88 | +QString FileDialog::getOpenFileName() const |
| 89 | +{ |
| 90 | + QFileDialog dialog(nullptr, QString(), QStandardPaths::standardLocations(QStandardPaths::HomeLocation)[0], getFilters()); |
| 91 | + dialog.setFileMode(QFileDialog::AnyFile); |
| 92 | + dialog.setAcceptMode(QFileDialog::AcceptOpen); |
| 93 | + dialog.setDefaultSuffix(m_defaultSuffix); |
| 94 | + |
| 95 | + if (dialog.exec() == QDialog::Accepted) |
| 96 | + return dialog.selectedFiles().at(0); |
| 97 | + else |
| 98 | + return ""; |
| 99 | +} |
| 100 | + |
| 101 | +QString FileDialog::getSaveFileName() const |
| 102 | +{ |
| 103 | + QFileDialog dialog(nullptr, QString(), QStandardPaths::standardLocations(QStandardPaths::HomeLocation)[0], getFilters()); |
| 104 | + dialog.setFileMode(QFileDialog::AnyFile); |
| 105 | + dialog.setAcceptMode(QFileDialog::AcceptSave); |
| 106 | + dialog.setDefaultSuffix(m_defaultSuffix); |
| 107 | + |
| 108 | + if (dialog.exec() == QDialog::Accepted) |
| 109 | + return dialog.selectedFiles().at(0); |
| 110 | + else |
| 111 | + return ""; |
| 112 | +} |
| 113 | + |
| 114 | +QString FileDialog::getFilters() const |
| 115 | +{ |
| 116 | + QString filtersStr = m_nameFilters.join(";;"); |
| 117 | + |
| 118 | + if (m_showAllFiles) |
| 119 | + filtersStr += ";;" + tr("All files") + " (*)"; |
| 120 | + |
| 121 | + return filtersStr; |
| 122 | +} |
0 commit comments