blob: 57ecb6cccf7b6e9e39dd661b596caaa273585dfe (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
#include "recent_files.h"
#include <QFileInfo>
#include <QDir>
RecentFiles::RecentFiles(unsigned int size, bool force_empty) : size(size) {
if (not force_empty)
list = settings_interface.read_setting("ui/recent_files").value<QStringList>();
}
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<QMenu>(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);
});
}
|