diff -r 5d73a6717321 -r 4dea24d3eda0 src/model.cpp --- a/src/model.cpp Tue Sep 28 22:17:52 2021 +0300 +++ b/src/model.cpp Tue Sep 28 23:07:23 2021 +0300 @@ -20,6 +20,7 @@ #include #include #include +#include #include "model.h" #include "modeleditcontext.h" @@ -302,22 +303,28 @@ } /** - * @brief Write out the model as text + * @brief Attempts the save the model + * @param errors Where to write any errors + * @returns whether it succeeded */ bool Model::save(QTextStream &errors) const { - QFile file{this->storedPath}; - if (file.open(QIODevice::WriteOnly)) + // Write the model first into a temporary file + QSaveFile file{this->path()}; + file.setDirectWriteFallback(true); + if (file.open(QSaveFile::WriteOnly)) { QTextStream out{&file}; for (const ModelObjectPointer& object : this->body) { out << object.get()->toLDrawCode() << "\r\n"; } - file.close(); - if (out.status() != QTextStream::Ok) + const bool commitSucceeded = file.commit(); + if (not commitSucceeded) { - errors << tr("Write error while writing to %1").arg(this->storedPath); + errors << tr("Could not save to %1: %2") + .arg(this->path()) + .arg(file.errorString()); return false; } else @@ -328,7 +335,7 @@ else { errors << tr("Could not open %1 for writing: %2") - .arg(this->storedPath) + .arg(file.fileName()) .arg(file.errorString()); return false; }