@@ -45,10 +45,7 @@ ProjectLoader::ProjectLoader(QObject *parent) :
4545
4646ProjectLoader::~ProjectLoader ()
4747{
48- m_stopLoading = true ;
49-
50- if (m_loadThread.isRunning ())
51- m_loadThread.waitForFinished ();
48+ stopLoading ();
5249
5350 for (SpriteModel *sprite : m_sprites)
5451 sprite->deleteLater ();
@@ -61,8 +58,11 @@ const QString &ProjectLoader::fileName() const
6158
6259void ProjectLoader::setFileName (const QString &newFileName)
6360{
64- if (m_loadThread.isRunning ())
61+ if (m_loadThread.isRunning ()) {
62+ stopLoading ();
6563 m_loadThread.waitForFinished ();
64+ QCoreApplication::processEvents ();
65+ }
6666
6767 if (newFileName.isEmpty ())
6868 return ;
@@ -72,7 +72,7 @@ void ProjectLoader::setFileName(const QString &newFileName)
7272 clear ();
7373
7474 m_project.setFileName (m_fileName.toStdString ());
75- m_loadStatus = false ;
75+ m_loadStatus = LoadStatus::Loading ;
7676
7777 // TODO: Do not set these to 0 after libscratchcpp starts doing it itself
7878 m_downloadedAssets = 0 ;
@@ -87,14 +87,23 @@ void ProjectLoader::setFileName(const QString &newFileName)
8787 m_loadThread = QtConcurrent::run (&callLoad, this );
8888}
8989
90- bool ProjectLoader::loadStatus () const
90+ ProjectLoader::LoadStatus ProjectLoader::loadStatus () const
9191{
9292 if (m_loadThread.isRunning ())
93- return false ;
93+ return LoadStatus::Loading ;
9494
9595 return m_loadStatus;
9696}
9797
98+ void ProjectLoader::stopLoading ()
99+ {
100+ if (m_loadThread.isRunning ()) {
101+ m_project.stopLoading ();
102+ m_stopLoading = true ;
103+ m_loadThread.waitForFinished ();
104+ }
105+ }
106+
98107bool ProjectLoader::running () const
99108{
100109 return m_running;
@@ -122,15 +131,17 @@ void ProjectLoader::setEngine(libscratchcpp::IEngine *engine)
122131StageModel *ProjectLoader::stage ()
123132{
124133 if (m_loadThread.isRunning ())
125- m_loadThread. waitForFinished () ;
134+ return nullptr ;
126135
127136 return &m_stage;
128137}
129138
130139QQmlListProperty<SpriteModel> ProjectLoader::sprites ()
131140{
132- if (m_loadThread.isRunning ())
133- m_loadThread.waitForFinished ();
141+ if (m_loadThread.isRunning ()) {
142+ m_emptySpriteList.clear ();
143+ return QQmlListProperty<SpriteModel>(this , &m_emptySpriteList);
144+ }
134145
135146 return QQmlListProperty<SpriteModel>(this , &m_sprites);
136147}
@@ -168,9 +179,9 @@ const QStringList &ProjectLoader::unsupportedBlocks() const
168179void ProjectLoader::start ()
169180{
170181 if (m_loadThread.isRunning ())
171- m_loadThread. waitForFinished () ;
182+ return ;
172183
173- if (m_loadStatus) {
184+ if (m_loadStatus == LoadStatus::Loaded ) {
174185 Q_ASSERT (m_engine);
175186 m_engine->start ();
176187 }
@@ -179,9 +190,9 @@ void ProjectLoader::start()
179190void ProjectLoader::stop ()
180191{
181192 if (m_loadThread.isRunning ())
182- m_loadThread. waitForFinished () ;
193+ return ;
183194
184- if (m_loadStatus) {
195+ if (m_loadStatus == LoadStatus::Loaded ) {
185196 Q_ASSERT (m_engine);
186197 m_engine->stop ();
187198 }
@@ -276,12 +287,16 @@ void ProjectLoader::clear()
276287void ProjectLoader::load ()
277288{
278289 m_unpositionedMonitors.clear ();
279- m_loadStatus = m_project.load ();
290+ m_loadStatus = m_project.load () ? LoadStatus::Loaded : LoadStatus::Failed ;
280291 m_engineMutex.lock ();
281292 m_engine = m_project.engine ().get ();
282293
283294 if (!m_engine || m_stopLoading) {
284295 m_engineMutex.unlock ();
296+
297+ if (m_stopLoading)
298+ m_loadStatus = LoadStatus::Aborted;
299+
285300 emit fileNameChanged ();
286301 emit loadStatusChanged ();
287302 emit loadingFinished ();
@@ -330,6 +345,7 @@ void ProjectLoader::load()
330345
331346 if (m_stopLoading) {
332347 m_engineMutex.unlock ();
348+ m_loadStatus = LoadStatus::Aborted;
333349 emit fileNameChanged ();
334350 emit loadStatusChanged ();
335351 emit loadingFinished ();
@@ -364,7 +380,7 @@ void ProjectLoader::initTimer()
364380void ProjectLoader::redraw ()
365381{
366382 if (m_loadThread.isRunning ())
367- m_loadThread. waitForFinished () ;
383+ return ;
368384
369385 auto stage = m_stage.renderedTarget ();
370386
0 commit comments