From 48a338b1321d9e169c73b71c01cd2f9ba00f9c53 Mon Sep 17 00:00:00 2001 From: ConfuSomu Date: Sat, 1 Jun 2024 18:05:30 -0400 Subject: Implement Recent files list and menu --- src/mainwindow.cpp | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'src/mainwindow.cpp') diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 89351ae..58fb991 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -2,6 +2,7 @@ #include "./ui_mainwindow.h" #include "src/archive/base_archive.h" #include "src/command_line.h" +#include "src/recent_files.h" #include "src/settingsdialog.h" #include "src/aboutdialog.h" #include "src/widgets/tab_activity_list.h" @@ -17,6 +18,15 @@ MainWindow::MainWindow(QWidget *parent) , ui(new Ui::MainWindow) { ui->setupUi(this); + + recent_files_menu = recent_files.create_menu(tr("Recent files…")); + ui->menuFile->insertMenu(ui->actionSettings, recent_files_menu.get()); + connect(recent_files_menu.get(), &QMenu::triggered, this, [&](QAction* action) { + // Needed for interepreting a QAction as a RecentFiles::Action so that we can retrieve the file_path and open it + if (auto* ac = qobject_cast(action); ac) + this->open_file(ac->file_path); + }); + create_initial_tabs(); connect(&archive_thread_watcher, &QFutureWatcher::finished, this, &MainWindow::archive_thread_watcher_done); } @@ -27,11 +37,9 @@ MainWindow::~MainWindow() } void MainWindow::create_initial_tabs() { - welcome_tab = new TabWelcome(); + welcome_tab = new TabWelcome(recent_files_menu); ui->tabWidget->addTab(welcome_tab, tr("Welcome")); connect(welcome_tab, &TabWelcome::show_file_open_dialog, this, &MainWindow::on_actionOpen_triggered); - // TODO: recent files - //connect(welcome_tab, &TabWelcome::show_recent_files, this, &MainWindow::); activity_list_tab = new TabActivityList(&data_archive); ui->tabWidget->addTab(activity_list_tab, tr("Activity List")); @@ -101,12 +109,6 @@ void MainWindow::on_actionOpen_triggered() { if (fileDialog.exec()) { QStringList files = fileDialog.selectedFiles(); open_file(files[0]); - - // Close the welcome tab as it's not needed anymore - if (welcome_tab) { - welcome_tab->deleteLater(); - welcome_tab = nullptr; - } } } @@ -137,12 +139,20 @@ void MainWindow::on_actionAbout_triggered(bool checked) { } void MainWindow::open_file(const QString &filename) { + // Close the welcome tab as it's not needed anymore + // Done here as this method is directly called when opening a recent file + if (welcome_tab) { + welcome_tab->deleteLater(); + welcome_tab = nullptr; + } + if (data_archive) { delete data_archive; data_archive = nullptr; } open_file_filename = filename; + recent_files.add_file(filename); // BUG: the overwritten cursor seems to get "lost" and revert in moc_mainwindow.cpp (after the end of the function we're in) when opening a file through a file dialog. // The cursor behaves properly when opening a file throught the command line. -- cgit v1.2.3-54-g00ecf