From c069b6f146dd45e4d9b377eb2578348aded52948 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Sun, 1 Aug 2021 07:12:37 +0200 Subject: [PATCH] UI: Now we should use native dialogues with the custom theme + icons (it seems we can't hinib the qss with QFileDialogs...) --- src/UI/MainWindow.cc | 40 ++++++++++++++++++++++++++++++++++------ src/UI/MainWindow.hh | 5 +++-- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/UI/MainWindow.cc b/src/UI/MainWindow.cc index 90200df2..950eeb57 100644 --- a/src/UI/MainWindow.cc +++ b/src/UI/MainWindow.cc @@ -248,12 +248,12 @@ void MainWindow::openDocument() noexcept { const QString separator = QStringLiteral(";;"); - const QString filename = QFileDialog::getOpenFileName( - this, "Select a document to open", QDir::homePath(), - QStringLiteral("Vivy documents (*.vivy);;" - "Vivy scripts (*.lua *.vvs);;") + - Utils::getAudioFileSuffixFilter() + separator + Utils::getVideoFileSuffixFilter() + - separator + Utils::getAssFileSuffixFilter()); + const QString filename = dialogOpenFileName("Select a document to open", QDir::homePath(), + QStringLiteral("Vivy documents (*.vivy);;" + "Vivy scripts (*.lua *.vvs);;") + + Utils::getAudioFileSuffixFilter() + separator + + Utils::getVideoFileSuffixFilter() + separator + + Utils::getAssFileSuffixFilter()); if (filename.isEmpty()) { qWarning() << "Found an empty filename, don't open a file"; return; @@ -393,3 +393,31 @@ MainWindow::documentViewActionsChanged() noexcept qInfo() << "No view to display:" << e.what(); } } +QString +MainWindow::dialogOpenFileName(const QString &title, const QString &folder, + const QString &filter) noexcept +{ + QFileDialog dialog(this, title, folder, filter); + bool dialogAccepted = false; + std::unique_ptr<VivyFileIconProvider> iconProvider(new VivyFileIconProvider()); + + dialog.setOption(QFileDialog::ReadOnly); + dialog.setIconProvider(iconProvider.get()); + dialog.setFileMode(QFileDialog::ExistingFile); + connect(&dialog, &QFileDialog::accepted, this, + [&dialogAccepted]() noexcept -> void { dialogAccepted = true; }); + + dialog.exec(); + + if (!dialogAccepted) { + return QStringLiteral(""); + } + + const QStringList resList = dialog.selectedFiles(); + if (resList.size() != 1) { + qCritical() << "You must select only one file"; + return QStringLiteral(""); + } + + return resList.at(0); +} diff --git a/src/UI/MainWindow.hh b/src/UI/MainWindow.hh index c0d7a6a2..17386985 100644 --- a/src/UI/MainWindow.hh +++ b/src/UI/MainWindow.hh @@ -46,14 +46,15 @@ private: AbstractDocumentView *getCurrentDocumentView() const; int findFirstUntouchedDocument() const noexcept; + QString dialogOpenFileName(const QString &title, const QString &folder, + const QString &filter) noexcept; // Do an action with the selected filename. The 'call' variable must be // callable like this: call(DocumentView, Document, QString) template <typename DV, typename D> void withOpenFileNameDialog(const QString &title, const QString &filter, auto call) noexcept { - const QString filename = - QFileDialog::getOpenFileName(this, title, QDir::homePath(), filter); + const QString filename = dialogOpenFileName(title, QDir::homePath(), filter); if (filename.isEmpty()) { qWarning() << "Found an empty filename, don't open a file"; -- GitLab