Skip to content

Commit 9712c99

Browse files
committed
ProjectLoader: Add properties for download progress
1 parent f78da3e commit 9712c99

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

ScratchCPPGui/projectloader.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ void runEventLoop(IEngine *engine)
2020
ProjectLoader::ProjectLoader(QObject *parent) :
2121
QObject(parent)
2222
{
23+
m_project.setDownloadProgressCallback([this](unsigned int finished, unsigned int all) {
24+
if (finished != m_downloadedAssets) {
25+
m_downloadedAssets = finished;
26+
emit downloadedAssetsChanged();
27+
}
28+
29+
if (all != m_assetCount) {
30+
m_assetCount = all;
31+
emit assetCountChanged();
32+
}
33+
});
34+
2335
initTimer();
2436

2537
// Update refresh rate when primary screen changes
@@ -66,6 +78,13 @@ void ProjectLoader::setFileName(const QString &newFileName)
6678
m_project.setScratchVersion(ScratchVersion::Scratch3);
6779
m_project.setFileName(m_fileName.toStdString());
6880
m_loadStatus = false;
81+
82+
// TODO: Do not set these to 0 after libscratchcpp starts doing it itself
83+
m_downloadedAssets = 0;
84+
m_assetCount = 0;
85+
emit downloadedAssetsChanged();
86+
emit assetCountChanged();
87+
6988
emit loadStatusChanged();
7089
emit fileNameChanged();
7190

@@ -397,3 +416,13 @@ void ProjectLoader::setEventLoopEnabled(bool newEventLoopEnabled)
397416
m_engineMutex.unlock();
398417
emit eventLoopEnabledChanged();
399418
}
419+
420+
unsigned int ProjectLoader::downloadedAssets() const
421+
{
422+
return m_downloadedAssets;
423+
}
424+
425+
unsigned int ProjectLoader::assetCount() const
426+
{
427+
return m_assetCount;
428+
}

ScratchCPPGui/projectloader.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class ProjectLoader : public QObject
3030
Q_PROPERTY(int cloneLimit READ cloneLimit WRITE setCloneLimit NOTIFY cloneLimitChanged)
3131
Q_PROPERTY(bool spriteFencing READ spriteFencing WRITE setSpriteFencing NOTIFY spriteFencingChanged)
3232
Q_PROPERTY(bool eventLoopEnabled READ eventLoopEnabled WRITE setEventLoopEnabled NOTIFY eventLoopEnabledChanged)
33+
Q_PROPERTY(unsigned int downloadedAssets READ downloadedAssets NOTIFY downloadedAssetsChanged)
34+
Q_PROPERTY(unsigned int assetCount READ assetCount NOTIFY assetCountChanged)
3335

3436
public:
3537
explicit ProjectLoader(QObject *parent = nullptr);
@@ -72,6 +74,10 @@ class ProjectLoader : public QObject
7274
bool eventLoopEnabled() const;
7375
void setEventLoopEnabled(bool newEventLoopEnabled);
7476

77+
unsigned int downloadedAssets() const;
78+
79+
unsigned int assetCount() const;
80+
7581
signals:
7682
void fileNameChanged();
7783
void loadStatusChanged();
@@ -86,6 +92,8 @@ class ProjectLoader : public QObject
8692
void cloneLimitChanged();
8793
void spriteFencingChanged();
8894
void eventLoopEnabledChanged();
95+
void downloadedAssetsChanged();
96+
void assetCountChanged();
8997

9098
protected:
9199
void timerEvent(QTimerEvent *event) override;
@@ -113,6 +121,8 @@ class ProjectLoader : public QObject
113121
int m_cloneLimit = 300;
114122
bool m_spriteFencing = true;
115123
bool m_eventLoopEnabled = true;
124+
std::atomic<unsigned int> m_downloadedAssets = 0;
125+
std::atomic<unsigned int> m_assetCount = 0;
116126
};
117127

118128
} // namespace scratchcppgui

0 commit comments

Comments
 (0)