wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
FileDialogProvider.cpp
Go to the documentation of this file.
1// Copyright 2015 - 2026, GIBIS-UNIFESP and the wiRedPanda contributors
2// SPDX-License-Identifier: GPL-3.0-or-later
3
5
6#include <QFileDialog>
7
8// ---------------------------------------------------------------------------
9// Global state
10// ---------------------------------------------------------------------------
11
14
19
21{
22 auto *old = s_provider;
23 s_provider = newProvider ? newProvider : &s_realProvider;
24 return old;
25}
26
27// ---------------------------------------------------------------------------
28// RealFileDialogProvider
29// ---------------------------------------------------------------------------
30
32 QWidget *parent, const QString &caption,
33 const QString &dir, const QString &filter)
34{
35 return QFileDialog::getOpenFileName(parent, caption, dir, filter);
36}
37
39 QWidget *parent, const QString &caption,
40 const QString &dir, const QString &filter)
41{
42 QFileDialog dialog(parent, caption, dir, filter);
43 dialog.setAcceptMode(QFileDialog::AcceptSave);
44 dialog.setFileMode(QFileDialog::AnyFile);
45
46 if (dialog.exec() == QDialog::Rejected) {
47 return {};
48 }
49
50 const auto files = dialog.selectedFiles();
51 if (files.isEmpty()) {
52 // Unreachable via any real interaction with the dialog: QFileDialog::accept()
53 // itself refuses to close (silently no-ops) when selectedFiles() would be empty,
54 // confirmed empirically -- even force-enabling and clicking the accept button with
55 // no filename selected leaves exec() blocked forever rather than returning Accepted.
56 return {}; // LCOV_EXCL_LINE
57 }
58
59 return {files.constFirst(), dialog.selectedNameFilter()};
60}
static FileDialogProvider * s_provider
static RealFileDialogProvider s_realProvider
Abstract file dialog interface for testability.
Production implementation that delegates to QFileDialog.
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter) override
FileDialogResult getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter) override
FileDialogProvider * provider()
Returns the active provider. Never null.
FileDialogProvider * setProvider(FileDialogProvider *newProvider)
Result from a save file dialog operation.