diff --git a/src/editor/editorsettings.cpp b/src/editor/editorsettings.cpp index d78b9c0..5ae4e7c 100644 --- a/src/editor/editorsettings.cpp +++ b/src/editor/editorsettings.cpp @@ -83,6 +83,29 @@ TransparentBackgroundPattern EditorSettings::transparentBackgroundPattern() cons }; } +QStringList EditorSettings::recentFiles() const +{ + return value("recentFiles").toStringList(); +} + +void EditorSettings::pushRecentFile(const QString &file) +{ + auto files = recentFiles(); + files.removeAll(file); + files.prepend(file); + setRecentFiles(files); +} + +void EditorSettings::clearRecentFiles() +{ + setRecentFiles({}); +} + +void EditorSettings::setRecentFiles(const QStringList &recentFiles) +{ + setValue("recentFiles", recentFiles); +} + bool EditorSettings::showRecentFiles() const { return value("showRecentFiles", true).toBool(); diff --git a/src/editor/editorsettings.h b/src/editor/editorsettings.h index bf5e5c2..268baec 100644 --- a/src/editor/editorsettings.h +++ b/src/editor/editorsettings.h @@ -34,6 +34,11 @@ public: TransparentBackgroundPattern transparentBackgroundPattern() const; + QStringList recentFiles() const; + void pushRecentFile(const QString &file); + void clearRecentFiles(); + void setRecentFiles(const QStringList &recentFiles); + bool showRecentFiles() const; void setShowRecentFiles(bool showRecentFiles); diff --git a/src/editor/mainwindow.cpp b/src/editor/mainwindow.cpp index f7cb682..ca4ddfa 100644 --- a/src/editor/mainwindow.cpp +++ b/src/editor/mainwindow.cpp @@ -561,6 +561,9 @@ void MainWindow::saveFileAs() dataStream << m_project; } + m_settings.pushRecentFile(path); + updateRecentFiles(); + m_filePath = path; m_unsavedChanges = false; @@ -928,6 +931,9 @@ void MainWindow::loadFile(const QString &path) } } + m_settings.pushRecentFile(path); + updateRecentFiles(); + m_project = std::move(project); m_projectTreeModel->setProject(&m_project); @@ -950,6 +956,29 @@ void MainWindow::updateVisibilities() m_ui->logo->setHidden(m_settings.hideWebsiteImage()); } +void MainWindow::updateRecentFiles() +{ + m_ui->menuRecentFiles->clear(); + for (const auto &path : m_settings.recentFiles()) + { + QFileInfo fileInfo{path}; + if (!fileInfo.exists()) + continue; + m_ui->menuRecentFiles->addAction(fileInfo.fileName(), this, [path,this](){ loadFile(path); }); + } + + if (m_ui->menuRecentFiles->isEmpty()) + m_ui->menuRecentFiles->addAction(tr("(none)"))->setEnabled(false); + else + { + m_ui->menuRecentFiles->addSeparator(); + m_ui->menuRecentFiles->addAction(tr("&Clear Recent Files"), this, [this](){ + m_settings.clearRecentFiles(); + updateRecentFiles(); + }); + } +} + QMdiSubWindow *MainWindow::addSubWindow(QDialog *dialog) { auto subwindow = m_ui->mdiArea->addSubWindow(dialog); diff --git a/src/editor/mainwindow.h b/src/editor/mainwindow.h index b8ce405..bcd7d11 100644 --- a/src/editor/mainwindow.h +++ b/src/editor/mainwindow.h @@ -86,6 +86,7 @@ private: void loadFile(const QString &path); void updateTitle(); void updateVisibilities(); + void updateRecentFiles(); QMdiSubWindow *addSubWindow(QDialog *dialog); diff --git a/src/main.cpp b/src/main.cpp index 7fedcd0..5f011a5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "editorsettings.h" #include "editorguiutils.h" @@ -44,7 +45,11 @@ int main(int argc, char *argv[]) EditorSettings settings; - MainWindow mainWindow{parser.positionalArguments().value(0), settings}; + QString path = parser.positionalArguments().value(0); + if (!path.isEmpty()) + path = QFileInfo{path}.absoluteFilePath(); + + MainWindow mainWindow{path, settings}; mainWindow.show(); return app.exec();