Skip to content

Commit 83ad678

Browse files
committed
AppMenuBar: Add open file action
1 parent 867304c commit 83ad678

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

src/app/appmenubar.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,70 @@
11
// SPDX-License-Identifier: GPL-3.0-or-later
22

3+
#include <QTemporaryFile>
4+
35
#include "appmenubar.h"
46
#include "uicomponents/menubarmodel.h"
57
#include "uicomponents/menumodel.h"
68
#include "uicomponents/menuitemmodel.h"
9+
#include "uicomponents/filedialog.h"
710

811
using namespace scratchcpp;
912
using namespace scratchcpp::uicomponents;
1013

1114
AppMenuBar::AppMenuBar(QObject *parent) :
1215
QObject(parent),
13-
m_model(new MenuBarModel(this))
16+
m_model(new MenuBarModel(this)),
17+
m_openFileDialog(new FileDialog(this))
1418
{
19+
m_openFileDialog->setShowAllFiles(false);
20+
m_openFileDialog->setNameFilters({ tr("Scratch 3 projects (%1)").arg("*.sb3") });
21+
22+
// File menu
23+
m_fileMenu = new MenuModel(m_model);
24+
m_fileMenu->setTitle(tr("File"));
25+
m_model->addMenu(m_fileMenu);
26+
27+
// File -> Open
28+
m_openFileItem = new MenuItemModel(m_fileMenu);
29+
m_openFileItem->setText(tr("Open..."));
30+
m_fileMenu->addItem(m_openFileItem);
31+
32+
connect(m_openFileItem, &MenuItemModel::clicked, this, &AppMenuBar::openFile);
33+
#ifdef Q_OS_WASM
34+
connect(m_openFileDialog, &FileDialog::fileContentReady, this, &AppMenuBar::loadOpenedFile);
35+
#endif
1536
}
1637

1738
MenuBarModel *AppMenuBar::model() const
1839
{
1940
return m_model;
2041
}
42+
43+
void AppMenuBar::openFile()
44+
{
45+
#ifdef Q_OS_WASM
46+
m_openFileDialog->getOpenFileContent();
47+
#else
48+
QString fileName = m_openFileDialog->getOpenFileName();
49+
50+
if (!fileName.isEmpty())
51+
emit fileOpened(fileName);
52+
#endif
53+
}
54+
55+
#ifdef Q_OS_WASM
56+
void AppMenuBar::loadOpenedFile(const QByteArray &content)
57+
{
58+
if (m_tmpFile)
59+
m_tmpFile->deleteLater();
60+
61+
m_tmpFile = new QTemporaryFile(this);
62+
63+
if (m_tmpFile->open()) {
64+
m_tmpFile->write(content);
65+
m_tmpFile->close();
66+
emit fileOpened(m_tmpFile->fileName());
67+
} else
68+
qWarning("Failed to create temporary file.");
69+
}
70+
#endif

src/app/appmenubar.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@
66

77
Q_MOC_INCLUDE("uicomponents/menubarmodel.h")
88

9+
class QTemporaryFile;
10+
911
namespace scratchcpp
1012
{
1113

1214
namespace uicomponents
1315
{
1416

1517
class MenuBarModel;
18+
class MenuModel;
19+
class MenuItemModel;
20+
class FileDialog;
1621

17-
}
22+
} // namespace uicomponents
1823

1924
class AppMenuBar : public QObject
2025
{
@@ -30,9 +35,19 @@ class AppMenuBar : public QObject
3035

3136
signals:
3237
void modelChanged();
38+
void fileOpened(const QString &fileName);
3339

3440
private:
41+
void openFile();
42+
#ifdef Q_OS_WASM
43+
void loadOpenedFile(const QByteArray &content);
44+
#endif
45+
3546
uicomponents::MenuBarModel *m_model = nullptr;
47+
uicomponents::MenuModel *m_fileMenu = nullptr;
48+
uicomponents::MenuItemModel *m_openFileItem = nullptr;
49+
uicomponents::FileDialog *m_openFileDialog = nullptr;
50+
QTemporaryFile *m_tmpFile = nullptr;
3651
};
3752

3853
} // namespace scratchcpp

0 commit comments

Comments
 (0)