Skip to content

Commit 867304c

Browse files
committed
Add FileDialog class
1 parent e041a14 commit 867304c

File tree

5 files changed

+227
-0
lines changed

5 files changed

+227
-0
lines changed

src/uicomponents/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ set(MODULE_SRC
1616
menumodel.h
1717
menuitemmodel.cpp
1818
menuitemmodel.h
19+
filedialog.cpp
20+
filedialog.h
1921
)
2022

2123
include(${PROJECT_SOURCE_DIR}/build/module.cmake)

src/uicomponents/filedialog.cpp

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

src/uicomponents/filedialog.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
#pragma once
4+
5+
#include <QQmlEngine>
6+
7+
namespace scratchcpp::uicomponents
8+
{
9+
10+
class FileDialog : public QObject
11+
{
12+
Q_OBJECT
13+
QML_ELEMENT
14+
Q_PROPERTY(QStringList nameFilters READ nameFilters WRITE setNameFilters NOTIFY nameFiltersChanged)
15+
Q_PROPERTY(bool showAllFiles READ showAllFiles WRITE setShowAllFiles NOTIFY showAllFilesChanged)
16+
Q_PROPERTY(QString fileName READ fileName NOTIFY fileNameChanged)
17+
Q_PROPERTY(QString shortFileName READ shortFileName NOTIFY shortFileNameChanged)
18+
Q_PROPERTY(QString defaultSuffix READ defaultSuffix WRITE setDefaultSuffix NOTIFY defaultSuffixChanged)
19+
20+
public:
21+
explicit FileDialog(QObject *parent = nullptr);
22+
23+
const QStringList &nameFilters(void) const;
24+
void setNameFilters(const QStringList &filters);
25+
26+
bool showAllFiles(void) const;
27+
void setShowAllFiles(bool value);
28+
29+
const QString &fileName(void) const;
30+
QString shortFileName(void) const;
31+
32+
const QString &defaultSuffix() const;
33+
void setDefaultSuffix(const QString &newDefaultSuffix);
34+
35+
Q_INVOKABLE void getOpenFileContent(void);
36+
Q_INVOKABLE QString getOpenFileName(void) const;
37+
Q_INVOKABLE QString getSaveFileName() const;
38+
39+
private:
40+
QString getFilters() const;
41+
42+
QStringList m_nameFilters;
43+
bool m_showAllFiles = true;
44+
QString m_fileName;
45+
QString m_defaultSuffix;
46+
47+
signals:
48+
void nameFiltersChanged();
49+
void showAllFilesChanged();
50+
void fileNameChanged();
51+
void shortFileNameChanged();
52+
void defaultSuffixChanged();
53+
void fileContentReady(const QByteArray &content);
54+
};
55+
56+
} // namespace scratchcpp::uicomponents

src/uicomponents/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ set(MODULE_TEST_SRC
22
menuitemmodel.cpp
33
menumodel.cpp
44
menubarmodel.cpp
5+
filedialog.cpp
56
)
67

78
include(${PROJECT_SOURCE_DIR}/build/module_test.cmake)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "filedialog.h"
4+
5+
using namespace scratchcpp::uicomponents;
6+
7+
TEST(FileDialogTest, Constructor)
8+
{
9+
FileDialog dialog1;
10+
FileDialog dialog2(&dialog1);
11+
ASSERT_EQ(dialog1.parent(), nullptr);
12+
ASSERT_EQ(dialog2.parent(), &dialog1);
13+
}
14+
15+
TEST(FileDialogTest, NameFilters)
16+
{
17+
FileDialog dialog;
18+
ASSERT_TRUE(dialog.nameFilters().isEmpty());
19+
20+
QStringList filters({ "a", "b", "c" });
21+
dialog.setNameFilters(filters);
22+
ASSERT_EQ(dialog.nameFilters(), filters);
23+
}
24+
25+
TEST(FileDialogTest, ShowAllFiles)
26+
{
27+
FileDialog dialog;
28+
ASSERT_TRUE(dialog.showAllFiles());
29+
30+
dialog.setShowAllFiles(false);
31+
ASSERT_FALSE(dialog.showAllFiles());
32+
33+
dialog.setShowAllFiles(false);
34+
ASSERT_FALSE(dialog.showAllFiles());
35+
36+
dialog.setShowAllFiles(true);
37+
ASSERT_TRUE(dialog.showAllFiles());
38+
}
39+
40+
TEST(FileDialogTest, DefaultSuffix)
41+
{
42+
FileDialog dialog;
43+
ASSERT_TRUE(dialog.defaultSuffix().isEmpty());
44+
45+
dialog.setDefaultSuffix(".txt");
46+
ASSERT_EQ(dialog.defaultSuffix(), ".txt");
47+
}

0 commit comments

Comments
 (0)