cabana: create autosave file when there are unsaved changes (#27542)

pull/27562/head
Willem Melching 2 years ago committed by GitHub
parent c32a45b5b5
commit ee615a3077
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 31
      tools/cabana/mainwin.cc
  2. 4
      tools/cabana/mainwin.h

@ -214,6 +214,7 @@ void MainWindow::undoStackIndexChanged(int index) {
}
prev_undostack_index = index;
prev_undostack_count = count;
autoSave();
}
void MainWindow::undoStackCleanChanged(bool clean) {
@ -255,7 +256,18 @@ void MainWindow::openFile() {
void MainWindow::loadFile(const QString &fn) {
if (!fn.isEmpty()) {
QFile file(fn);
QString dbc_fn = fn;
// Prompt user to load auto saved file if it exists.
if (QFile::exists(fn + AUTO_SAVE_EXTENSION)) {
auto ret = QMessageBox::question(this, tr("Auto saved DBC found"), tr("Auto saved DBC file from previous session found. Do you want to load it instead?"));
if (ret == QMessageBox::Yes) {
dbc_fn += AUTO_SAVE_EXTENSION;
UndoStack::instance()->resetClean(); // Force user to save on close so the auto saved file is not lost
}
}
QFile file(dbc_fn);
if (file.open(QIODevice::ReadOnly)) {
auto dbc_name = QFileInfo(fn).baseName();
QString error;
@ -340,7 +352,23 @@ void MainWindow::save() {
}
}
void MainWindow::autoSave() {
if (!current_file.isEmpty() && !UndoStack::instance()->isClean()) {
QFile file(current_file + AUTO_SAVE_EXTENSION);
if (file.open(QIODevice::WriteOnly)) {
file.write(dbc()->generateDBC().toUtf8());
}
}
}
void MainWindow::cleanupAutoSaveFile() {
if (!current_file.isEmpty()) {
QFile::remove(current_file + AUTO_SAVE_EXTENSION);
}
}
void MainWindow::saveFile(const QString &fn) {
cleanupAutoSaveFile();
QFile file(fn);
if (file.open(QIODevice::WriteOnly)) {
file.write(dbc()->generateDBC().toUtf8());
@ -445,6 +473,7 @@ void MainWindow::dockCharts(bool dock) {
}
void MainWindow::closeEvent(QCloseEvent *event) {
cleanupAutoSaveFile();
remindSaveChanges();
main_win = nullptr;

@ -13,6 +13,8 @@
#include "tools/cabana/videowidget.h"
#include "tools/cabana/tools/findsimilarbits.h"
const QString AUTO_SAVE_EXTENSION = ".tmp";
class MainWindow : public QMainWindow {
Q_OBJECT
@ -42,6 +44,8 @@ signals:
protected:
void remindSaveChanges();
void saveFile(const QString &fn);
void autoSave();
void cleanupAutoSaveFile();
void setCurrentFile(const QString &fn);
void updateRecentFileActions();
void createActions();

Loading…
Cancel
Save