From 20258c463a8bcd9f691f5815d718cdc32f9d18ca Mon Sep 17 00:00:00 2001 From: Vinayakjeet Singh Karki <139736674+vinayakjeet@users.noreply.github.com> Date: Wed, 27 Mar 2024 09:01:57 +0530 Subject: [PATCH 1/2] Update mainwindow.h Signed-off-by: vinayakjeet Introduced autosave functionality in mainwindow.h, defining the necessary slots and variables to support periodic saving of user work. This enhances data integrity and user experience by minimizing potential loss of work. Signed-off-by: vinayakjeet --- avogadro/mainwindow.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/avogadro/mainwindow.h b/avogadro/mainwindow.h index 3dadf613..2c2cb8ae 100644 --- a/avogadro/mainwindow.h +++ b/avogadro/mainwindow.h @@ -69,7 +69,7 @@ class MainWindow : public QMainWindow public slots: void setMolecule(Avogadro::QtGui::Molecule* molecule); - + void autosaveDocument(); //line to declare the autosave slot /** * Update internal state to reflect that the molecule has been modified. */ @@ -391,6 +391,7 @@ private slots: void setProjectionPerspective(); private: + QtGui::Molecule* m_molecule; QtGui::RWMolecule* m_rwMolecule; QtGui::MoleculeModel* m_moleculeModel; @@ -398,7 +399,8 @@ private slots: QtGui::ScenePlugin* m_activeScenePlugin; bool m_queuedFilesStarted; QStringList m_queuedFiles; - + QTimer* m_autosaveTimer; // for the autosave timer + int m_autosaveInterval; // for autosave interval in minutes QStringList m_recentFiles; QList m_actionRecentFiles; From deec1aa68dcb53d4941ff22eaa46eb660ed15369 Mon Sep 17 00:00:00 2001 From: Vinayakjeet Singh Karki <139736674+vinayakjeet@users.noreply.github.com> Date: Wed, 27 Mar 2024 09:03:37 +0530 Subject: [PATCH 2/2] Update mainwindow.cpp Signed-off-by: vinayakjeet This update enhances the Avogadro application with an autosave feature, ensuring users' progress is periodically saved to mitigate data loss risks. The implementation involves the creation of a QTimer to trigger autosave actions every 5 minutes, alongside logic to identify changes and execute saves efficiently. This feature is incorporated within the MainWindow class, utilizing the CJSON format for file saving. The approach optimizes resource usage by activating autosave only when necessary, providing a seamless and secure user experience. Signed-off-by: vinayakjeet --- avogadro/mainwindow.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/avogadro/mainwindow.cpp b/avogadro/mainwindow.cpp index 4118820d..adf80ec1 100644 --- a/avogadro/mainwindow.cpp +++ b/avogadro/mainwindow.cpp @@ -1979,6 +1979,42 @@ void MainWindow::buildMenu() m_menuBuilder->addAction(path, action, 960); m_fileToolBar->addAction(action); connect(action, SIGNAL(triggered()), SLOT(saveFileAs())); + // Initialize autosave feature +m_autosaveInterval = 5; // Autosave interval in minutes +m_autosaveTimer = new QTimer(this); +connect(m_autosaveTimer, &QTimer::timeout, this, &MainWindow::autosaveDocument); +m_autosaveTimer->start(m_autosaveInterval * 60000); // Convert minutes to milliseconds + +void MainWindow::autosaveDocument() +{ + if (!m_molecule || !m_moleculeDirty) { + return; // No molecule loaded or no changes made since the last save. + } + + QString autosaveDirPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/autosave"; + QDir autosaveDir(autosaveDirPath); + if (!autosaveDir.exists()) { + autosaveDir.mkpath("."); + } + + // Construct autosave file name + QString autosaveFilename; + if (m_molecule->hasData("fileName")) { + QFileInfo fileInfo(m_molecule->data("fileName").toString().c_str()); + autosaveFilename = fileInfo.baseName() + "_autosave.cjson"; + } else { + autosaveFilename = "unsaved_autosave.cjson"; + } + QString autosaveFilePath = autosaveDirPath + "/" + autosaveFilename; + + // Use CJSON format for autosaving + Io::CjsonFormat writer; + if (!writer.writeFile(autosaveFilePath, *m_molecule)) { + qWarning() << "Failed to autosave the document to" << autosaveFilePath; + } else { + qDebug() << "Document autosaved to" << autosaveFilePath; + } +} // Export action for menu QStringList exportPath = path;