Skip to content

Commit 868788b

Browse files
committed
AutoSave Feature Implemented
Signed-off-by: Vinayakjeet Singh Karki <139736674+vinayakjeet@users.noreply.github.com>
1 parent 782aa89 commit 868788b

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

avogadro/mainwindow.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,42 @@ void MainWindow::buildMenu()
19791979
m_menuBuilder->addAction(path, action, 960);
19801980
m_fileToolBar->addAction(action);
19811981
connect(action, SIGNAL(triggered()), SLOT(saveFileAs()));
1982+
// Initialize autosave feature
1983+
m_autosaveInterval = 5; // Autosave interval in minutes
1984+
m_autosaveTimer = new QTimer(this);
1985+
connect(m_autosaveTimer, &QTimer::timeout, this, &MainWindow::autosaveDocument);
1986+
m_autosaveTimer->start(m_autosaveInterval * 60000); // Convert minutes to milliseconds
1987+
1988+
void MainWindow::autosaveDocument()
1989+
{
1990+
if (!m_molecule || !m_moleculeDirty) {
1991+
return; // No molecule loaded or no changes made since the last save.
1992+
}
1993+
1994+
QString autosaveDirPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/autosave";
1995+
QDir autosaveDir(autosaveDirPath);
1996+
if (!autosaveDir.exists()) {
1997+
autosaveDir.mkpath(".");
1998+
}
1999+
2000+
// Construct autosave file name
2001+
QString autosaveFilename;
2002+
if (m_molecule->hasData("fileName")) {
2003+
QFileInfo fileInfo(m_molecule->data("fileName").toString().c_str());
2004+
autosaveFilename = fileInfo.baseName() + "_autosave.cjson";
2005+
} else {
2006+
autosaveFilename = "unsaved_autosave.cjson";
2007+
}
2008+
QString autosaveFilePath = autosaveDirPath + "/" + autosaveFilename;
2009+
2010+
// Use CJSON format for autosaving
2011+
Io::CjsonFormat writer;
2012+
if (!writer.writeFile(autosaveFilePath, *m_molecule)) {
2013+
qWarning() << "Failed to autosave the document to" << autosaveFilePath;
2014+
} else {
2015+
qDebug() << "Document autosaved to" << autosaveFilePath;
2016+
}
2017+
}
19822018

19832019
// Export action for menu
19842020
QStringList exportPath = path;
@@ -2635,4 +2671,4 @@ bool MainWindow::handleCommand(const QString& command,
26352671
return false;
26362672
}
26372673

2638-
} // End of Avogadro namespace
2674+
} // End of Avogadro namespace

avogadro/mainwindow.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class MainWindow : public QMainWindow
6969

7070
public slots:
7171
void setMolecule(Avogadro::QtGui::Molecule* molecule);
72-
72+
void autosaveDocument(); //line to declare the autosave slot
7373
/**
7474
* Update internal state to reflect that the molecule has been modified.
7575
*/
@@ -391,14 +391,16 @@ private slots:
391391
void setProjectionPerspective();
392392

393393
private:
394+
394395
QtGui::Molecule* m_molecule;
395396
QtGui::RWMolecule* m_rwMolecule;
396397
QtGui::MoleculeModel* m_moleculeModel;
397398
QtGui::LayerModel* m_layerModel;
398399
QtGui::ScenePlugin* m_activeScenePlugin;
399400
bool m_queuedFilesStarted;
400401
QStringList m_queuedFiles;
401-
402+
QTimer* m_autosaveTimer; // for the autosave timer
403+
int m_autosaveInterval; // for autosave interval in minutes
402404
QStringList m_recentFiles;
403405
QList<QAction*> m_actionRecentFiles;
404406

@@ -502,4 +504,4 @@ private slots:
502504

503505
} // End Avogadro namespace
504506

505-
#endif
507+
#endif

0 commit comments

Comments
 (0)