Skip to content
Permalink
Browse files
Editor: Added the autosave functionality
  • Loading branch information
Wohlstand committed Jun 9, 2022
1 parent 9277577 commit a1d74e6aef9d4630f66eba39e87b42056e2d273b
Show file tree
Hide file tree
Showing 14 changed files with 526 additions and 140 deletions.
@@ -67,6 +67,7 @@ public slots:
bool savePGEXLVL(QString fileName, bool silent = false); //!< Saves a PGE Extended Level file format
bool saveSMBX64LVL(QString fileName, bool silent = false, bool *out_WarningIsAborted = nullptr); //!< Saves a SMBX Level file format
bool saveSMBX38aLVL(QString fileName, bool silent = false); //!< Saves SMBX38A Level file format
void runAutoSave();
QString userFriendlyCurrentFile();
QString currentFile()
{
@@ -119,6 +120,7 @@ public slots:
bool m_isUntitled = false;

QString curFile;
QString lastAutoSaveFile;

QTimer *updateTimer = nullptr;
void setAutoUpdateTimer(int ms);
@@ -334,6 +334,14 @@ bool LevelEdit::saveFile(const QString &fileName, const bool addToRecent, bool *
{
m_mw->AddToRecentFiles(fileName);
m_mw->SyncRecentFiles();

// Delete the autosave file as real file was been saved
if(!lastAutoSaveFile.isEmpty())
{
if(QFile::exists(lastAutoSaveFile))
QFile::remove(lastAutoSaveFile);
lastAutoSaveFile.clear();
}
}

//Refresh Strict SMBX64 flag
@@ -384,6 +392,52 @@ bool LevelEdit::saveSMBX38aLVL(QString fileName, bool silent)
return true;
}

void LevelEdit::runAutoSave()
{
if(!LvlData.meta.modified && !LvlData.meta.untitled)
return; // Don't auto-save unmodified files

if(lastAutoSaveFile.isEmpty())
{
// Untitled file will be saved at the auto-generated directory
if(LvlData.meta.untitled)
{
QDir crashSave;
crashSave.setCurrent(AppPathManager::userAppDir());

if(!crashSave.exists("__autosave"))
crashSave.mkdir("__autosave");
crashSave.cd("__autosave");

int counter = 0;
do // Find the suitable name and don't override existing files
{
lastAutoSaveFile = crashSave.absoluteFilePath(QString("Untitled-%1.lvlx").arg(counter++));
}
while(QFile::exists(lastAutoSaveFile));
}
else
{
lastAutoSaveFile = LvlData.meta.path + "/" + LvlData.meta.filename + "_autosave.lvlx";
int counter = 0;
// Find the suitable name and don't override existing files
while(QFile::exists(lastAutoSaveFile))
{
lastAutoSaveFile = LvlData.meta.path + "/" + LvlData.meta.filename + QString("_autosave-%1.lvlx").arg(counter++);
}

}
}

auto data = LvlData;

data.meta.configPackId = getMainWindow()->configs.configPackId;
data.meta.smbx64strict = false;
FileKeeper fileKeeper = FileKeeper(lastAutoSaveFile);
FileFormats::SaveLevelFile(data, fileKeeper.tempPath(), FileFormats::LVL_PGEX);
fileKeeper.restore();
}



bool LevelEdit::saveSMBX64LVL(QString fileName, bool silent, bool *out_WarningIsAborted)
@@ -64,6 +64,7 @@ public slots:
bool trySave();

bool saveFile(const QString &fileName, const bool addToRecent = true);
void runAutoSave();

QString userFriendlyCurrentFile();
QString currentFile()
@@ -79,6 +80,7 @@ public slots:
void markForForceClose();

QString curFile;
QString lastAutoSaveFile;

QString customSavePath;

@@ -18,6 +18,7 @@

#include <common_features/main_window_ptr.h>
#include <common_features/file_keeper.h>
#include <common_features/app_path.h>
#include <main_window/global_settings.h>
#include <PGE_File_Formats/file_formats.h>
#include <PGE_File_Formats/pge_x.h>
@@ -185,13 +186,65 @@ bool NpcEdit::saveFile(const QString &fileName, const bool addToRecent)

if(addToRecent)
{
MainWinConnect::pMainWin->AddToRecentFiles(fileName);
MainWinConnect::pMainWin->SyncRecentFiles();
m_mw->AddToRecentFiles(fileName);
m_mw->SyncRecentFiles();

// Delete the autosave file as real file was been saved
if(!lastAutoSaveFile.isEmpty())
{
if(QFile::exists(lastAutoSaveFile))
QFile::remove(lastAutoSaveFile);
lastAutoSaveFile.clear();
}
}

return true;
}

void NpcEdit::runAutoSave()
{
if(!m_isModified && !m_isUntitled)
return; // Don't auto-save unmodified files

if(lastAutoSaveFile.isEmpty())
{
// Untitled file will be saved at the auto-generated directory
if(m_isUntitled)
{
QDir crashSave;
crashSave.setCurrent(AppPathManager::userAppDir());

if(!crashSave.exists("__autosave"))
crashSave.mkdir("__autosave");
crashSave.cd("__autosave");

int counter = 0;
do // Find the suitable name and don't override existing files
{
lastAutoSaveFile = crashSave.absoluteFilePath(QString("Untitled-%1.txt").arg(counter++));
}
while(QFile::exists(lastAutoSaveFile));
}
else
{
lastAutoSaveFile = curFile + "_autosave.txt";
int counter = 0;
// Find the suitable name and don't override existing files
while(QFile::exists(lastAutoSaveFile))
{
lastAutoSaveFile = curFile + QString("_autosave-%1.lvlx").arg(counter++);
}

}
}

auto data = NpcData;

FileKeeper fileKeeper = FileKeeper(lastAutoSaveFile);
FileFormats::WriteNPCTxtFileF(fileKeeper.tempPath(), data);
fileKeeper.restore();
}



bool NpcEdit::maybeSave()
@@ -28,6 +28,7 @@
#include <common_features/util.h>
#include <common_features/main_window_ptr.h>
#include <common_features/file_keeper.h>
#include <common_features/app_path.h>
#include <editing/_scenes/world/wld_scene.h>
#include <editing/_dialogs/savingnotificationdialog.h>
#include <main_window/global_settings.h>
@@ -353,13 +354,67 @@ bool WorldEdit::saveFile(const QString &fileName, const bool addToRecent)

if(addToRecent)
{
MainWinConnect::pMainWin->AddToRecentFiles(fileName);
MainWinConnect::pMainWin->SyncRecentFiles();
m_mw->AddToRecentFiles(fileName);
m_mw->SyncRecentFiles();

// Delete the autosave file as real file was been saved
if(!lastAutoSaveFile.isEmpty())
{
if(QFile::exists(lastAutoSaveFile))
QFile::remove(lastAutoSaveFile);
lastAutoSaveFile.clear();
}
}

return true;
}

void WorldEdit::runAutoSave()
{
if(!WldData.meta.modified && !WldData.meta.untitled)
return; // Don't auto-save unmodified files

if(lastAutoSaveFile.isEmpty())
{
// Untitled file will be saved at the auto-generated directory
if(WldData.meta.untitled)
{
QDir crashSave;
crashSave.setCurrent(AppPathManager::userAppDir());

if(!crashSave.exists("__autosave"))
crashSave.mkdir("__autosave");
crashSave.cd("__autosave");

int counter = 0;
do // Find the suitable name and don't override existing files
{
lastAutoSaveFile = crashSave.absoluteFilePath(QString("Untitled-%1.wldx").arg(counter++));
}
while(QFile::exists(lastAutoSaveFile));
}
else
{
lastAutoSaveFile = WldData.meta.path + "/" + WldData.meta.filename + "_autosave.wldx";
int counter = 0;
// Find the suitable name and don't override existing files
while(QFile::exists(lastAutoSaveFile))
{
lastAutoSaveFile = WldData.meta.path + "/" + WldData.meta.filename + QString("_autosave-%1.wldx").arg(counter++);
}

}
}

auto data = WldData;

data.meta.configPackId = getMainWindow()->configs.configPackId;
data.meta.smbx64strict = false;
FileKeeper fileKeeper = FileKeeper(lastAutoSaveFile);
FileFormats::SaveWorldFile(data, fileKeeper.tempPath(), FileFormats::WLD_PGEX);
fileKeeper.restore();
}


bool WorldEdit::loadFile(const QString &fileName, WorldData FileData, DataConfig &configs, EditingSettings options)
{
@@ -65,6 +65,7 @@ public slots:
bool save(bool savOptionsDialog = false);
bool saveAs(bool savOptionsDialog = false);
bool saveFile(const QString &fileName, const bool addToRecent = true);
void runAutoSave();
QString userFriendlyCurrentFile();
QString currentFile()
{
@@ -111,6 +112,7 @@ public slots:
bool m_isUntitled = false;

QString curFile;
QString lastAutoSaveFile;

QTimer *updateTimer = nullptr;
void setAutoUpdateTimer(int ms);
@@ -406,6 +406,8 @@ void MainWindow::setUiDefults()
// Register hot key without adding into toolbar
addAction(ui->actionNewFile);

QObject::connect(&m_autoSaveTimer, &QTimer::timeout, this, &MainWindow::runAutoSave);

ui->menuWindow->menuAction()->setEnabled(true);
ui->menuLevel->menuAction()->setEnabled(false);
ui->menuWorld->menuAction()->setEnabled(false);
@@ -183,6 +183,13 @@ void MainWindow::loadSettings()
}
settings.endGroup();

settings.beginGroup("autosave");
{
GlobalSettings::LvlOpts.autoSave_enable = settings.value("enabled", true).toBool();
GlobalSettings::LvlOpts.autoSave_interval = settings.value("interval", 3).toInt();
}
settings.endGroup();

settings.beginGroup("ext-tools");
{
GlobalSettings::tools_sox_bin_path = settings.value("sox-bin-path", GlobalSettings::tools_sox_bin_path).toString();
@@ -321,6 +328,13 @@ void MainWindow::saveSettings()
}
settings.endGroup();

settings.beginGroup("autosave");
{
settings.setValue("enabled", GlobalSettings::LvlOpts.autoSave_enable);
settings.setValue("interval", GlobalSettings::LvlOpts.autoSave_interval);
}
settings.endGroup();

settings.beginGroup("ext-tools");
{
settings.setValue("sox-bin-path", GlobalSettings::tools_sox_bin_path);
@@ -434,4 +448,6 @@ void MainWindow::applySetup(bool startup)
{
qApp->setFont(*GlobalSettings::fontDefault);
}

updateAutoSaver();
}
@@ -145,4 +145,49 @@ void MainWindow::on_actionSave_all_triggered()
save_all();
}

void MainWindow::updateAutoSaver()
{
m_autoSaveTimer.stop();
if(GlobalSettings::LvlOpts.autoSave_enable)
{
if(GlobalSettings::LvlOpts.autoSave_interval < 1)
GlobalSettings::LvlOpts.autoSave_interval = 1;
else if(GlobalSettings::LvlOpts.autoSave_interval > 30)
GlobalSettings::LvlOpts.autoSave_interval = 30;

m_autoSaveTimer.setInterval(GlobalSettings::LvlOpts.autoSave_interval * 60000);
m_autoSaveTimer.start();
}
}

void MainWindow::runAutoSave()
{
QList<QMdiSubWindow *> listOfAllSubWindows = allEditWins();
for(QMdiSubWindow *subWin : listOfAllSubWindows)
{
if(activeChildWindow(subWin) == MainWindow::WND_Level)
{
LevelEdit *leveledit = activeLvlEditWin(subWin);

if(!leveledit)
continue;
leveledit->runAutoSave();
}
else if(activeChildWindow(subWin) == MainWindow::WND_NpcTxt)
{
NpcEdit *npcedit = activeNpcEditWin(subWin);

if(!npcedit)
continue;
npcedit->runAutoSave();
}
else if(activeChildWindow(subWin) == MainWindow::WND_World)
{
WorldEdit *worldedit = activeWldEditWin(subWin);

if(!worldedit)
continue;
worldedit->runAutoSave();
}
}
}
@@ -27,7 +27,7 @@
struct EditingSettings
{
EditingSettings();
//Common
// Common
bool animationEnabled = true;
bool collisionsEnabled = true;
bool grid_snap = true;
@@ -36,8 +36,11 @@ struct EditingSettings
bool grid_show = false;
bool camera_grid_show = false;
unsigned int default_zoom = 100;
//World map only
// World map only
bool semiTransparentPaths = false;
// Auto-saving
bool autoSave_enable = true;
int autoSave_interval = 3; //!< Autosave interval in minutes
};

struct SETTINGS_ItemDefaults

0 comments on commit a1d74e6

Please sign in to comment.