diff --git a/src/UI/MainWindow.cc b/src/UI/MainWindow.cc index 90200df2aab2faa59079cfb501bb666feb98d5fc..950eeb5767ffc14fef24320d9daabc2e923ff10e 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 c0d7a6a24d3452c301de365399bec9bd6947e7e6..17386985d089c75caaef8345ebd1984cd29f241b 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";