From b638be5b2eb140aafd7e32e5516a2929f0d50c4a Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sat, 14 Aug 2021 17:21:58 +0300 Subject: [PATCH] Use QSaveFile class for saving a layout. --- src/app/puzzle/vpmainwindow.cpp | 37 +++++++++++++++-------- src/app/puzzle/xml/vplayoutfilewriter.cpp | 2 +- src/app/puzzle/xml/vplayoutfilewriter.h | 2 +- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/app/puzzle/vpmainwindow.cpp b/src/app/puzzle/vpmainwindow.cpp index 0cf3211ea..3a532ecd3 100644 --- a/src/app/puzzle/vpmainwindow.cpp +++ b/src/app/puzzle/vpmainwindow.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include "ui_vpmainwindow.h" #include "dialogs/vpdialogabout.h" @@ -268,21 +269,33 @@ void VPMainWindow::SetCurrentFile(const QString &fileName) //--------------------------------------------------------------------------------------------------------------------- auto VPMainWindow::SaveLayout(const QString &path, QString &error) -> bool { - QFile file(path); - file.open(QIODevice::WriteOnly); - - VPLayoutFileWriter fileWriter; - fileWriter.WriteFile(m_layout, &file); - - if (fileWriter.hasError()) + bool success = false; + QSaveFile file(path); + // cppcheck-suppress ConfigurationNotChecked + if (file.open(QIODevice::WriteOnly)) { - error = tr("Fail to create layout."); - return false; + VPLayoutFileWriter fileWriter; + fileWriter.WriteFile(m_layout, &file); + + if (fileWriter.hasError()) + { + error = tr("Fail to create layout."); + return false; + } + + success = file.commit(); } - SetCurrentFile(path); - LayoutWasSaved(true); - return true; + if (success) + { + SetCurrentFile(path); + LayoutWasSaved(true); + } + else + { + error = file.errorString(); + } + return success; } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/puzzle/xml/vplayoutfilewriter.cpp b/src/app/puzzle/xml/vplayoutfilewriter.cpp index 8f3791fc8..a1e6faf22 100644 --- a/src/app/puzzle/xml/vplayoutfilewriter.cpp +++ b/src/app/puzzle/xml/vplayoutfilewriter.cpp @@ -137,7 +137,7 @@ auto GrainlineArrowDirrectionToString(GrainlineArrowDirection type) -> QString } // namespace //--------------------------------------------------------------------------------------------------------------------- -void VPLayoutFileWriter::WriteFile(VPLayout *layout, QFile *file) +void VPLayoutFileWriter::WriteFile(VPLayout *layout, QIODevice *file) { setDevice(file); setAutoFormatting(true); diff --git a/src/app/puzzle/xml/vplayoutfilewriter.h b/src/app/puzzle/xml/vplayoutfilewriter.h index dda791333..defebc56d 100644 --- a/src/app/puzzle/xml/vplayoutfilewriter.h +++ b/src/app/puzzle/xml/vplayoutfilewriter.h @@ -51,7 +51,7 @@ public: VPLayoutFileWriter()= default; ~VPLayoutFileWriter()= default; - void WriteFile(VPLayout *layout, QFile *file); + void WriteFile(VPLayout *layout, QIODevice *file); private: void WriteLayout(VPLayout *layout);