SILICON
Loading...
Searching...
No Matches
fileDialogUtils.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 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#include "fileDialogUtils.hpp"
19
20#include <ui/lifetime.hpp>
21
22#include <utility>
23
24#include <QFile>
25#include <QFileDialog>
26#include <QMessageBox>
27#include <QObject>
28#include <QWidget>
29
31
32void openFileContent(QWidget* parent, const QString& caption, const QString& filter,
33 OpenContentCallback callback, QObject* context)
34{
35 callback = guardCallback(context ? context : parent, std::move(callback));
36#ifdef __EMSCRIPTEN__
37 QFileDialog::getOpenFileContent(
38 filter, [callback = std::move(callback)](const QString& fileName,
39 const QByteArray& fileContent) {
40 if (!fileName.isEmpty())
41 callback(fileName, fileContent);
42 });
43#else
44 const QString fileName =
45 QFileDialog::getOpenFileName(parent, caption, QString(), filter);
46 if (fileName.isEmpty())
47 return;
48
49 QFile file(fileName);
50 if (!file.open(QIODevice::ReadOnly)) {
51 QMessageBox::warning(
52 parent, caption,
53 QObject::tr("Cannot open file for reading:\n%1").arg(file.errorString()));
54 return;
55 }
56
57 callback(fileName, file.readAll());
58#endif
59}
60
61std::optional<QString> saveFileContent(QWidget* parent, const QString& caption,
62 const QString& suggestedFileName,
63 const QString& filter, const QByteArray& content)
64{
65#ifdef __EMSCRIPTEN__
66 QFileDialog::saveFileContent(content, suggestedFileName);
67 return suggestedFileName;
68#else
69 const QString fileName =
70 QFileDialog::getSaveFileName(parent, caption, suggestedFileName, filter);
71 if (fileName.isEmpty())
72 return std::nullopt;
73
74 QFile file(fileName);
75 if (!file.open(QIODevice::WriteOnly) || file.write(content) != content.size()) {
76 QMessageBox::warning(parent, caption,
77 QObject::tr("Cannot write file:\n%1").arg(file.errorString()));
78 return std::nullopt;
79 }
80
81 return fileName;
82#endif
83}
84
85} // namespace SILICON::ui::fileDialog
std::optional< QString > saveFileContent(QWidget *parent, const QString &caption, const QString &suggestedFileName, const QString &filter, const QByteArray &content)
std::function< void(const QString &, const QByteArray &)> OpenContentCallback
void openFileContent(QWidget *parent, const QString &caption, const QString &filter, OpenContentCallback callback, QObject *context)
auto guardCallback(const QObject *owner, Callback callback)
Definition lifetime.hpp:20