mirror of https://github.com/vitalif/openscad
Adapted wil1471's pull request #385 to master
parent
ada0609b9c
commit
78baae599b
|
@ -88,6 +88,8 @@ private:
|
||||||
static void consoleOutput(const std::string &msg, void *userdata);
|
static void consoleOutput(const std::string &msg, void *userdata);
|
||||||
void loadViewSettings();
|
void loadViewSettings();
|
||||||
void loadDesignSettings();
|
void loadDesignSettings();
|
||||||
|
void saveBackup();
|
||||||
|
void writeBackup(class QFile *file);
|
||||||
|
|
||||||
class QMessageBox *openglbox;
|
class QMessageBox *openglbox;
|
||||||
|
|
||||||
|
@ -199,7 +201,8 @@ private:
|
||||||
|
|
||||||
char const * afterCompileSlot;
|
char const * afterCompileSlot;
|
||||||
bool procevents;
|
bool procevents;
|
||||||
|
class QTemporaryFile *tempFile;
|
||||||
|
|
||||||
class ProgressWidget *progresswidget;
|
class ProgressWidget *progresswidget;
|
||||||
class CGALWorker *cgalworker;
|
class CGALWorker *cgalworker;
|
||||||
QMutex consolemutex;
|
QMutex consolemutex;
|
||||||
|
|
|
@ -41,6 +41,40 @@ std::string PlatformUtils::libraryPath()
|
||||||
return boosty::stringy( path );
|
return boosty::stringy( path );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::string PlatformUtils::backupPath()
|
||||||
|
{
|
||||||
|
fs::path path;
|
||||||
|
try {
|
||||||
|
std::string pathstr = PlatformUtils::documentsPath();
|
||||||
|
if (pathstr=="") return "";
|
||||||
|
path = boosty::canonical(fs::path( pathstr ));
|
||||||
|
if (path.empty()) return "";
|
||||||
|
path /= "OpenSCAD";
|
||||||
|
path /= "backups";
|
||||||
|
} catch (const fs::filesystem_error& ex) {
|
||||||
|
PRINTB("ERROR: %s",ex.what());
|
||||||
|
}
|
||||||
|
return boosty::stringy( path );
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PlatformUtils::createBackupPath()
|
||||||
|
{
|
||||||
|
std::string path = PlatformUtils::backupPath();
|
||||||
|
bool OK = false;
|
||||||
|
try {
|
||||||
|
if (!fs::exists(fs::path(path))) {
|
||||||
|
OK = fs::create_directories( path );
|
||||||
|
}
|
||||||
|
if (!OK) {
|
||||||
|
PRINTB("ERROR: Cannot create %s", path );
|
||||||
|
}
|
||||||
|
} catch (const fs::filesystem_error& ex) {
|
||||||
|
PRINTB("ERROR: %s",ex.what());
|
||||||
|
}
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
#include "version_check.h"
|
#include "version_check.h"
|
||||||
#define STRINGIFY(x) #x
|
#define STRINGIFY(x) #x
|
||||||
#define TOSTRING(x) STRINGIFY(x)
|
#define TOSTRING(x) STRINGIFY(x)
|
||||||
|
@ -121,4 +155,3 @@ std::string PlatformUtils::info()
|
||||||
;
|
;
|
||||||
return s.str();
|
return s.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,8 @@ namespace PlatformUtils {
|
||||||
std::string documentsPath();
|
std::string documentsPath();
|
||||||
std::string libraryPath();
|
std::string libraryPath();
|
||||||
bool createLibraryPath();
|
bool createLibraryPath();
|
||||||
|
std::string backupPath();
|
||||||
|
bool createBackupPath();
|
||||||
std::string info();
|
std::string info();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -75,6 +75,7 @@
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
#include <QProgressDialog>
|
#include <QProgressDialog>
|
||||||
#include <QMutexLocker>
|
#include <QMutexLocker>
|
||||||
|
#include <QTemporaryFile>
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
|
@ -155,7 +156,7 @@ settings_valueList(const QString &key, const QList<int> &defaultList = QList<int
|
||||||
}
|
}
|
||||||
|
|
||||||
MainWindow::MainWindow(const QString &filename)
|
MainWindow::MainWindow(const QString &filename)
|
||||||
: root_inst("group"), progresswidget(NULL)
|
: root_inst("group"), tempFile(NULL), progresswidget(NULL)
|
||||||
{
|
{
|
||||||
setupUi(this);
|
setupUi(this);
|
||||||
|
|
||||||
|
@ -668,6 +669,7 @@ void MainWindow::compile(bool reload, bool forcedone)
|
||||||
|
|
||||||
if (shouldcompiletoplevel) {
|
if (shouldcompiletoplevel) {
|
||||||
console->clear();
|
console->clear();
|
||||||
|
saveBackup();
|
||||||
compileTopLevelDocument();
|
compileTopLevelDocument();
|
||||||
didcompile = true;
|
didcompile = true;
|
||||||
}
|
}
|
||||||
|
@ -1010,6 +1012,57 @@ void MainWindow::actionOpenExample()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::writeBackup(QFile *file)
|
||||||
|
{
|
||||||
|
// see MainWindow::saveBackup()
|
||||||
|
file->resize(0);
|
||||||
|
QTextStream writer(file);
|
||||||
|
writer.setCodec("UTF-8");
|
||||||
|
writer << this->editor->toPlainText();
|
||||||
|
|
||||||
|
PRINTB("Saved backup file: %s", file->fileName().toLocal8Bit().constData());
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::saveBackup()
|
||||||
|
{
|
||||||
|
std::string path = PlatformUtils::backupPath();
|
||||||
|
if ((!fs::exists(path)) && (!PlatformUtils::createBackupPath())) {
|
||||||
|
PRINTB("WARNING: Cannot create backup path: %s", path);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString backupPath = QString::fromStdString(path);
|
||||||
|
if (!backupPath.endsWith("/")) backupPath.append("/");
|
||||||
|
|
||||||
|
if (this->fileName.isEmpty()) {
|
||||||
|
if (!this->tempFile) {
|
||||||
|
this->tempFile = new QTemporaryFile(backupPath.append("unsaved-backup-XXXXXXXX.scad"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((!this->tempFile->isOpen()) && (! this->tempFile->open())) {
|
||||||
|
PRINT("WARNING: Failed to create backup file");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return writeBackup(this->tempFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
QFileInfo fileInfo = QFileInfo(this->fileName);
|
||||||
|
|
||||||
|
backupPath.append(fileInfo.baseName())
|
||||||
|
.append("-backup.")
|
||||||
|
.append(fileInfo.suffix());
|
||||||
|
|
||||||
|
QFile file(backupPath);
|
||||||
|
|
||||||
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||||
|
PRINTB("WARNING: Failed to open backup file for writing: %s (%s)", backupPath.toLocal8Bit().constData() % file.errorString().toLocal8Bit().constData());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeBackup(&file);
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
|
||||||
void MainWindow::actionSave()
|
void MainWindow::actionSave()
|
||||||
{
|
{
|
||||||
if (this->fileName.isEmpty()) {
|
if (this->fileName.isEmpty()) {
|
||||||
|
@ -2017,6 +2070,10 @@ void MainWindow::closeEvent(QCloseEvent *event)
|
||||||
settings.setValue("window/position", pos());
|
settings.setValue("window/position", pos());
|
||||||
settings_setValueList("window/splitter1sizes",splitter1->sizes());
|
settings_setValueList("window/splitter1sizes",splitter1->sizes());
|
||||||
settings_setValueList("window/splitter2sizes",splitter2->sizes());
|
settings_setValueList("window/splitter2sizes",splitter2->sizes());
|
||||||
|
if (this->tempFile) {
|
||||||
|
delete this->tempFile;
|
||||||
|
this->tempFile = NULL;
|
||||||
|
}
|
||||||
event->accept();
|
event->accept();
|
||||||
} else {
|
} else {
|
||||||
event->ignore();
|
event->ignore();
|
||||||
|
|
Loading…
Reference in New Issue