@@ -45,10 +45,7 @@ ProjectLoader::ProjectLoader(QObject *parent) :
45
45
46
46
ProjectLoader::~ProjectLoader ()
47
47
{
48
- m_stopLoading = true ;
49
-
50
- if (m_loadThread.isRunning ())
51
- m_loadThread.waitForFinished ();
48
+ stopLoading ();
52
49
53
50
for (SpriteModel *sprite : m_sprites)
54
51
sprite->deleteLater ();
@@ -61,8 +58,11 @@ const QString &ProjectLoader::fileName() const
61
58
62
59
void ProjectLoader::setFileName (const QString &newFileName)
63
60
{
64
- if (m_loadThread.isRunning ())
61
+ if (m_loadThread.isRunning ()) {
62
+ stopLoading ();
65
63
m_loadThread.waitForFinished ();
64
+ QCoreApplication::processEvents ();
65
+ }
66
66
67
67
if (newFileName.isEmpty ())
68
68
return ;
@@ -72,7 +72,7 @@ void ProjectLoader::setFileName(const QString &newFileName)
72
72
clear ();
73
73
74
74
m_project.setFileName (m_fileName.toStdString ());
75
- m_loadStatus = false ;
75
+ m_loadStatus = LoadStatus::Loading ;
76
76
77
77
// TODO: Do not set these to 0 after libscratchcpp starts doing it itself
78
78
m_downloadedAssets = 0 ;
@@ -87,14 +87,23 @@ void ProjectLoader::setFileName(const QString &newFileName)
87
87
m_loadThread = QtConcurrent::run (&callLoad, this );
88
88
}
89
89
90
- bool ProjectLoader::loadStatus () const
90
+ ProjectLoader::LoadStatus ProjectLoader::loadStatus () const
91
91
{
92
92
if (m_loadThread.isRunning ())
93
- return false ;
93
+ return LoadStatus::Loading ;
94
94
95
95
return m_loadStatus;
96
96
}
97
97
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
+
98
107
bool ProjectLoader::running () const
99
108
{
100
109
return m_running;
@@ -122,15 +131,17 @@ void ProjectLoader::setEngine(libscratchcpp::IEngine *engine)
122
131
StageModel *ProjectLoader::stage ()
123
132
{
124
133
if (m_loadThread.isRunning ())
125
- m_loadThread. waitForFinished () ;
134
+ return nullptr ;
126
135
127
136
return &m_stage;
128
137
}
129
138
130
139
QQmlListProperty<SpriteModel> ProjectLoader::sprites ()
131
140
{
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
+ }
134
145
135
146
return QQmlListProperty<SpriteModel>(this , &m_sprites);
136
147
}
@@ -168,9 +179,9 @@ const QStringList &ProjectLoader::unsupportedBlocks() const
168
179
void ProjectLoader::start ()
169
180
{
170
181
if (m_loadThread.isRunning ())
171
- m_loadThread. waitForFinished () ;
182
+ return ;
172
183
173
- if (m_loadStatus) {
184
+ if (m_loadStatus == LoadStatus::Loaded ) {
174
185
Q_ASSERT (m_engine);
175
186
m_engine->start ();
176
187
}
@@ -179,9 +190,9 @@ void ProjectLoader::start()
179
190
void ProjectLoader::stop ()
180
191
{
181
192
if (m_loadThread.isRunning ())
182
- m_loadThread. waitForFinished () ;
193
+ return ;
183
194
184
- if (m_loadStatus) {
195
+ if (m_loadStatus == LoadStatus::Loaded ) {
185
196
Q_ASSERT (m_engine);
186
197
m_engine->stop ();
187
198
}
@@ -276,12 +287,16 @@ void ProjectLoader::clear()
276
287
void ProjectLoader::load ()
277
288
{
278
289
m_unpositionedMonitors.clear ();
279
- m_loadStatus = m_project.load ();
290
+ m_loadStatus = m_project.load () ? LoadStatus::Loaded : LoadStatus::Failed ;
280
291
m_engineMutex.lock ();
281
292
m_engine = m_project.engine ().get ();
282
293
283
294
if (!m_engine || m_stopLoading) {
284
295
m_engineMutex.unlock ();
296
+
297
+ if (m_stopLoading)
298
+ m_loadStatus = LoadStatus::Aborted;
299
+
285
300
emit fileNameChanged ();
286
301
emit loadStatusChanged ();
287
302
emit loadingFinished ();
@@ -330,6 +345,7 @@ void ProjectLoader::load()
330
345
331
346
if (m_stopLoading) {
332
347
m_engineMutex.unlock ();
348
+ m_loadStatus = LoadStatus::Aborted;
333
349
emit fileNameChanged ();
334
350
emit loadStatusChanged ();
335
351
emit loadingFinished ();
@@ -364,7 +380,7 @@ void ProjectLoader::initTimer()
364
380
void ProjectLoader::redraw ()
365
381
{
366
382
if (m_loadThread.isRunning ())
367
- m_loadThread. waitForFinished () ;
383
+ return ;
368
384
369
385
auto stage = m_stage.renderedTarget ();
370
386
0 commit comments