#include "recent_files.h" #include #include RecentFiles::RecentFiles(unsigned int size, bool force_empty) : size(size) { if (not force_empty) list = settings_interface.read_setting("ui/recent_files").value(); } RecentFiles::~RecentFiles() { if (has_list_changed) { settings_interface.write_setting("ui/recent_files", list); settings_interface.commit(); } } RecentFiles::QMenuPtr RecentFiles::create_menu(const QString& title, QWidget* parent) { if (menu) return menu; menu = std::make_shared(title, parent); generate_menu_actions(); return menu; } void RecentFiles::generate_menu_actions() { menu->clear(); if (not list.isEmpty()) { int i = 1; for (const QString& path : list) { QFileInfo file(path); // Showing the parent directory's name allows knowing from which archive the file comes from Action* action = new Action(QStringLiteral("&%1. %2").arg(i++).arg( file.dir().dirName() + QStringLiteral("/") + file.fileName() )); action->file_path = path; menu->addAction(action); } } else { Action* action = new Action(QWidget::tr("No recent files")); action->setStatusTip(QWidget::tr("To view recent files, open a file")); action->setEnabled(false); menu->addAction(action); } } void RecentFiles::add_file(const QString& path) { if (not list.contains(path)) { list.prepend(path); if (list.size() > size) #if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) list.resize(size); #else do list.removeLast(); while (list.size() > size); #endif has_list_changed = true; generate_menu_actions(); } } const QStringList& RecentFiles::get_files() { return list; } void RecentFiles::clear() { list.empty(); } RecentFiles::Action::Action(const QString &text, QObject *parent) : QAction(text, parent) { connect(this, &QAction::triggered, this, [=](bool checked) { emit this->chosen_file(file_path); }); }