src/model.cpp

changeset 145
4dea24d3eda0
parent 141
185eb297dc1e
child 146
235941b7322f
equal deleted inserted replaced
144:5d73a6717321 145:4dea24d3eda0
18 18
19 #include <QBrush> 19 #include <QBrush>
20 #include <QFile> 20 #include <QFile>
21 #include <QFileInfo> 21 #include <QFileInfo>
22 #include <QFont> 22 #include <QFont>
23 #include <QSaveFile>
23 #include "model.h" 24 #include "model.h"
24 #include "modeleditcontext.h" 25 #include "modeleditcontext.h"
25 26
26 /** 27 /**
27 * @brief Constructs a model 28 * @brief Constructs a model
300 { 301 {
301 return this->body[unsigned_cast(index.row())].get(); 302 return this->body[unsigned_cast(index.row())].get();
302 } 303 }
303 304
304 /** 305 /**
305 * @brief Write out the model as text 306 * @brief Attempts the save the model
307 * @param errors Where to write any errors
308 * @returns whether it succeeded
306 */ 309 */
307 bool Model::save(QTextStream &errors) const 310 bool Model::save(QTextStream &errors) const
308 { 311 {
309 QFile file{this->storedPath}; 312 // Write the model first into a temporary file
310 if (file.open(QIODevice::WriteOnly)) 313 QSaveFile file{this->path()};
314 file.setDirectWriteFallback(true);
315 if (file.open(QSaveFile::WriteOnly))
311 { 316 {
312 QTextStream out{&file}; 317 QTextStream out{&file};
313 for (const ModelObjectPointer& object : this->body) 318 for (const ModelObjectPointer& object : this->body)
314 { 319 {
315 out << object.get()->toLDrawCode() << "\r\n"; 320 out << object.get()->toLDrawCode() << "\r\n";
316 } 321 }
317 file.close(); 322 const bool commitSucceeded = file.commit();
318 if (out.status() != QTextStream::Ok) 323 if (not commitSucceeded)
319 { 324 {
320 errors << tr("Write error while writing to %1").arg(this->storedPath); 325 errors << tr("Could not save to %1: %2")
326 .arg(this->path())
327 .arg(file.errorString());
321 return false; 328 return false;
322 } 329 }
323 else 330 else
324 { 331 {
325 return true; 332 return true;
326 } 333 }
327 } 334 }
328 else 335 else
329 { 336 {
330 errors << tr("Could not open %1 for writing: %2") 337 errors << tr("Could not open %1 for writing: %2")
331 .arg(this->storedPath) 338 .arg(file.fileName())
332 .arg(file.errorString()); 339 .arg(file.errorString());
333 return false; 340 return false;
334 } 341 }
335 } 342 }
336 343

mercurial