Skip to content

Commit ae62db7

Browse files
committed
Finalize
1 parent 1c989ab commit ae62db7

File tree

9 files changed

+46
-43
lines changed

9 files changed

+46
-43
lines changed

Desktop/data/datasetpackage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2523,7 +2523,7 @@ bool DataSetPackage::filePathIsNonSaveable(const QString & path) const
25232523
{
25242524
QFileInfo fileDir(path);
25252525

2526-
return fileDir.dir() == QDir(AppDirs::examples()) || fileDir.dir() == QDir(AppDirs::autoSaveDir());
2526+
return path.startsWith(AppDirs::examples()) || fileDir.dir() == QDir(AppDirs::autoSaveDir());
25272527
}
25282528

25292529
void DataSetPackage::setAnalysesData(const Json::Value &analysesData)

Desktop/data/fileevent.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ FileEvent::FileEvent(FileMenu *parent, FileEvent::FileMode fileMode)
3838
default: _exporter = nullptr; break;
3939
}
4040

41-
connect(this, &FileEvent::started, parent, &FileMenu::dataSetIORequestStarted);
42-
connect(this, &FileEvent::completed, parent, &FileMenu::dataSetIORequestCompleted);
41+
// Only the FileMenu handles the started signal
42+
connect(this, &FileEvent::started, parent, &FileMenu::fileEventRequestDispatcher);
4343
}
4444

4545
FileEvent::~FileEvent()
@@ -99,14 +99,14 @@ bool FileEvent::setPath(const QString & path)
9999

100100
void FileEvent::setStarted()
101101
{
102-
_started = true;
102+
_status = EventStatus::EventStarted;
103103

104104
emit started();
105105
}
106106

107107
void FileEvent::setComplete(bool success, const QString & message)
108108
{
109-
_completed = true;
109+
_status = EventStatus::EventCompleted;
110110
_success = success;
111111
_message = message;
112112

@@ -115,17 +115,17 @@ void FileEvent::setComplete(bool success, const QString & message)
115115

116116
void FileEvent::setFinalized()
117117
{
118-
_finalized = true;
118+
_status = EventStatus::EventFinalized;
119119
emit finalized();
120120
deleteLater();
121121
}
122122

123123

124124
void FileEvent::chain(FileEvent *event)
125125
{
126-
if (_started)
126+
if (isStarted())
127127
{
128-
if (_completed)
128+
if (isCompleted())
129129
Log::log() << "Event " << getProgressMsg().toStdString() << " is completed before Event " << event->getProgressMsg().toStdString() << " was completed" << std::endl;
130130
else // The `this` event is already started, but it should be set complete (and start the finalize process) only when `event` is finalized
131131
connect(event, &FileEvent::finalized, this, [this, event]() { setComplete(event->isSuccessful(), event->message()); });

Desktop/data/fileevent.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class FileEvent : public QObject
3636

3737
public:
3838
enum FileMode { FileSave, FileNew, FileOpen, FileExportResults, FileExportData, FileGenerateData, FileSyncData, FileClose };
39+
enum EventStatus { EventInitialized, EventStarted, EventCompleted, EventFinalized };
40+
3941

4042
FileEvent(FileMenu *parent, FileMode fileMode = FileEvent::FileOpen);
4143
virtual ~FileEvent();
@@ -56,9 +58,9 @@ class FileEvent : public QObject
5658
bool isOnlineNode() const { return _path.startsWith("http"); }
5759
bool isExample() const;
5860
bool isReadOnly() const { return isExample() || isDatabase(); }
59-
bool isStarted() const { return _started; }
60-
bool isCompleted() const { return _completed; }
61-
bool isFinalized() const { return _finalized; }
61+
bool isStarted() const { return _status != EventStatus::EventInitialized; }
62+
bool isCompleted() const { return _status == EventStatus::EventCompleted || _status == EventStatus::EventFinalized; }
63+
bool isFinalized() const { return _status == EventStatus::EventFinalized; }
6264
bool isSuccessful() const { return _success; }
6365
bool isTmp() const { return _tmp; }
6466
static bool autoSaveExists();
@@ -92,10 +94,8 @@ class FileEvent : public QObject
9294
_dataFilePath,
9395
_last_error = "Unknown error",
9496
_message;
95-
bool _started = false,
96-
_completed = false,
97-
_finalized = false,
98-
_success = false,
97+
EventStatus _status = EventStatus::EventInitialized;
98+
bool _success = false,
9999
_tmp = false;
100100
Exporter * _exporter = nullptr;
101101
Json::Value _database = Json::nullValue;

Desktop/mainwindow.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ bool MainWindow::startDetached(const QString & applicationPath, const QStringLis
12421242
return worked;
12431243
}
12441244

1245-
void MainWindow::dataSetIORequestHandler(FileEvent *event)
1245+
void MainWindow::fileEventRequestHandler(FileEvent *event)
12461246
{
12471247
if (event->operation() == FileEvent::FileNew)
12481248
{
@@ -1367,7 +1367,7 @@ void MainWindow::closeVariablesPage()
13671367
_columnModel->setVisible(false);
13681368
}
13691369

1370-
void MainWindow::dataSetIOCompleted(FileEvent *event)
1370+
void MainWindow::fileEventRequestFinalize(FileEvent *event)
13711371
{
13721372
hideProgress();
13731373

@@ -1517,8 +1517,6 @@ void MainWindow::dataSetIOCompleted(FileEvent *event)
15171517
if(!event->path().endsWith(".pdf") && _preferences->currentThemeName() != "lightTheme")
15181518
_resultsJsInterface->setThemeCss(_preferences->currentThemeName());
15191519
}
1520-
1521-
event->setFinalized();
15221520
}
15231521

15241522

@@ -1860,11 +1858,9 @@ bool MainWindow::startDataEditorHandler()
18601858

18611859
if(!justOpenItAlready)
18621860
{
1863-
connect(event, &FileEvent::completed, this, [this, event]() { startDataEditorEventCompleted(event); });
1864-
connect(event, &FileEvent::completed, _fileMenu, [this, event]() { _fileMenu->setSyncFile(event); });
18651861
event->setPath(dataFilePath);
1866-
_loader->io(event);
1867-
showProgress();
1862+
connect(event, &FileEvent::completed, this, [this, event]() { startDataEditorEventCompleted(event); });
1863+
event->setStarted();
18681864
}
18691865
else
18701866
{
@@ -1927,6 +1923,7 @@ void MainWindow::startDataEditorEventCompleted(FileEvent* event)
19271923

19281924
if (event->isSuccessful())
19291925
{
1926+
_fileMenu->setSyncFile(event);
19301927
_package->setDataFilePath(event->path().toStdString());
19311928
_package->setDataFileReadOnly(false);
19321929
_package->setModified(true);
@@ -1985,11 +1982,9 @@ void MainWindow::startDataEditor(QString path)
19851982
path.append(".csv");
19861983

19871984
FileEvent *event = new FileEvent(_fileMenu, FileEvent::FileGenerateData);
1988-
connect(event, &FileEvent::completed, this, [this, event]() { startDataEditorEventCompleted(event); });
1989-
connect(event, &FileEvent::completed, _fileMenu, [this, event]() { _fileMenu->setSyncFile(event); });
1990-
event->setPath(path);
1991-
_loader->io(event);
1992-
showProgress();
1985+
event->setPath(path);
1986+
connect(event, &FileEvent::completed, this, [this, event]() { startDataEditorEventCompleted(event); });
1987+
event->setStarted();
19931988
}
19941989
}
19951990
}

Desktop/mainwindow.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ private slots:
265265
void analysisImageSavedHandler(Analysis* analysis);
266266
void removeAllAnalyses();
267267

268-
void dataSetIORequestHandler(FileEvent *event);
269-
void dataSetIOCompleted(FileEvent *event);
268+
void fileEventRequestHandler(FileEvent *event);
269+
void fileEventRequestFinalize(FileEvent *event);
270270
void populateUIfromDataSet();
271271
void startDataEditorEventCompleted(FileEvent *event);
272272
void analysisAdded(Analysis *analysis);

Desktop/widgets/filemenu/filemenu.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,19 +261,28 @@ void FileMenu::buttonsForEmptyWorkspace()
261261
_actionButtons->setEnabled(ActionButtons::Close, false);
262262
}
263263

264-
void FileMenu::dataSetIORequestStarted()
264+
void FileMenu::fileEventRequestDispatcher()
265265
{
266266
FileEvent* event = qobject_cast<FileEvent*>(sender());
267-
connect(event, &FileEvent::completed, _mainWindow, [this, event]() { _mainWindow->dataSetIOCompleted(event); }, Qt::QueuedConnection);
268-
_mainWindow->dataSetIORequestHandler(event);
269267

270-
if(event->operation() != FileEvent::FileClose)
271-
setVisible(false); //If we just did something we are now done with the filemenu right? Except if we just closed a file
268+
// When an event starts, many objects can be involved. It's important to be sure that when an event is set completed, that all computing is effectly done.
269+
// The event is set finalized only here, when the MainWindow has finished the finalizing work for the event: all other objects connected with the completed signal should use direct connection.
270+
// This is to ensure that when the event is set finalized, no other objects are still using the event.
271+
// The finalize signal can be used to compute other stuff (like in chaining event), but the event self should not be used anymore since it is then deleted.
272+
connect(event, &FileEvent::completed, this, &FileMenu::fileEventRequestFinalize);
273+
connect(event, &FileEvent::completed, _mainWindow, [this, event]() { _mainWindow->fileEventRequestFinalize(event); event->setFinalized(); }, Qt::QueuedConnection);
274+
275+
_mainWindow->fileEventRequestHandler(event);
276+
272277
}
273278

274-
void FileMenu::dataSetIORequestCompleted()
279+
void FileMenu::fileEventRequestFinalize()
275280
{
276281
FileEvent* event = qobject_cast<FileEvent*>(sender());
282+
283+
if(event->operation() != FileEvent::FileClose)
284+
setVisible(false); //If we just did something we are now done with the filemenu right? Except if we just closed a file
285+
277286
if (event->operation() == FileEvent::FileSave || event->operation() == FileEvent::FileOpen)
278287
{
279288
if (event->isSuccessful() && !event->isTmp())

Desktop/widgets/filemenu/filemenu.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ public slots:
124124
void workspaceModified();
125125
void setSyncFile(FileEvent *event);
126126
void dataAutoSynchronizationChanged(bool on) { setDataFileWatcher(on); }
127-
void dataSetIORequestStarted();
128-
void dataSetIORequestCompleted();
127+
void fileEventRequestDispatcher();
128+
void fileEventRequestFinalize();
129129
void dataFileModifiedHandler(QString path);
130130
void setFileoperation(const ActionButtons::FileOperation fo);
131131
void actionButtonClicked(const ActionButtons::FileOperation action);

Desktop/widgets/filemenu/osf.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ void OSF::openSaveFile(const QString & nodePath, const QString & filename, const
287287
{
288288
setSavefilename(filename);
289289

290-
connect(event, SIGNAL(completed(FileEvent*)), this, SLOT(openSaveCompleted(FileEvent*)));
290+
connect(event, &FileEvent::completed, this, &OSF::openSaveCompleted);
291291
}
292292
}
293293
else
@@ -307,13 +307,12 @@ void OSF::userDetailsReceived()
307307
userNode->deleteLater();
308308
}
309309

310-
void OSF::openSaveCompleted(FileEvent* event)
310+
void OSF::openSaveCompleted()
311311
{
312+
FileEvent* event = qobject_cast<FileEvent*>(sender());
312313

313314
if (event->isSuccessful())
314-
{
315315
_osfFileSystem->refresh();
316-
}
317316

318317
setProcessing(false);
319318
}

Desktop/widgets/filemenu/osf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private slots:
9696
void saveClicked();
9797
void openSaveFile(const QString & nodePath, const QString & filename, const QString & osfpath = "");
9898
void userDetailsReceived();
99-
void openSaveCompleted(FileEvent * event);
99+
void openSaveCompleted();
100100
void updateUserDetails();
101101
void newFolderCreated();
102102
void resetOSFListModel();

0 commit comments

Comments
 (0)