Skip to content

Commit

Permalink
Autosave Feature Implemented
Browse files Browse the repository at this point in the history
Signed-off-by: Vinayakjeet Singh Karki <[email protected]>
  • Loading branch information
vinayakjeet committed Mar 28, 2024
1 parent 782aa89 commit 22b787f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
38 changes: 37 additions & 1 deletion avogadro/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2635,4 +2671,4 @@ bool MainWindow::handleCommand(const QString& command,
return false;
}

} // End of Avogadro namespace
} // End of Avogadro namespace
8 changes: 5 additions & 3 deletions avogadro/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -391,14 +391,16 @@ private slots:
void setProjectionPerspective();

private:

QtGui::Molecule* m_molecule;
QtGui::RWMolecule* m_rwMolecule;
QtGui::MoleculeModel* m_moleculeModel;
QtGui::LayerModel* m_layerModel;
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<QAction*> m_actionRecentFiles;

Expand Down Expand Up @@ -502,4 +504,4 @@ private slots:

} // End Avogadro namespace

#endif
#endif

0 comments on commit 22b787f

Please sign in to comment.