-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecentfilemanager.cpp
124 lines (113 loc) · 3.56 KB
/
recentfilemanager.cpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "recentfilemanager.h"
#include <DInputDialog>
#include <DMenu>
#include <DMessageManager>
#include <QApplication>
#include <QFile>
#include <QSettings>
#define ICONRES(name) QIcon(":/WingHexPy/img/" name ".png")
RecentFileManager::RecentFileManager(DMenu *menu, DMainWindow *parent)
: QObject(parent), m_menu(menu), m_parent(parent) {}
void RecentFileManager::apply() {
QAction *a;
a = new QAction(m_menu);
a->setText(tr("ClearHistory"));
a->setIcon(ICONRES("clearhis"));
connect(a, &QAction::triggered, this, &RecentFileManager::clearFile);
m_menu->addAction(a);
a = new QAction(m_menu);
a->setText(tr("RemoveItem"));
a->setIcon(ICONRES("del"));
connect(a, &QAction::triggered, [=] {
if (hitems.count() == 0) {
DMessageManager::instance()->sendMessage(m_parent, ICONRES("clearhis"),
tr("NoHistoryDel"));
return;
}
bool ok;
auto d = QInputDialog::getInt(nullptr, tr("Input"), tr("InputIndex"), 0, 0,
m_recents.count(), 1, &ok);
if (ok) {
m_menu->removeAction(hitems.at(d));
m_recents.removeAt(d);
for (auto it = hitems.begin() + d; it != hitems.end(); it++) {
(*it)->setIconText(QString::number(d++));
}
}
});
m_menu->addAction(a);
m_menu->addSeparator();
QSettings settings(QApplication::organizationName(), "WingHexPy");
auto s = settings.value("recent").toStringList();
int i = 0;
for (auto item : s) {
if (QFile::exists(item)) {
if (m_recents.count() > 10)
break;
m_recents << item;
a = new QAction(m_menu);
a->setText(QString("%1 : %2").arg(i++).arg(item));
a->setData(item);
connect(a, &QAction::triggered, this, &RecentFileManager::trigger);
hitems.push_back(a);
m_menu->addAction(a);
}
}
}
RecentFileManager::~RecentFileManager() {
QSettings settings(QApplication::organizationName(), "WingHexPy");
settings.setValue("recent", m_recents);
}
void RecentFileManager::addRecentFile(QString filename) {
while (m_recents.count() >= 10) {
m_recents.pop_back();
}
if (QFile::exists(filename) && m_recents.indexOf(filename) < 0) {
auto a = new QAction(m_menu);
a = new QAction(m_menu);
a->setData(filename);
connect(a, &QAction::triggered, this, &RecentFileManager::trigger);
m_recents.push_front(filename);
if (hitems.count())
m_menu->insertAction(hitems.first(), a);
else
m_menu->addAction(a);
hitems.push_front(a);
auto i = 0;
for (auto item : hitems) {
item->setText(QString("%1 : %2").arg(i++).arg(item->data().toString()));
}
}
}
void RecentFileManager::trigger() {
auto send = qobject_cast<QAction *>(sender());
if (send) {
auto f = send->data().toString();
if (QFile::exists(f)) {
emit openFile(f);
return;
}
}
auto index = hitems.indexOf(send);
if (index >= 0) {
m_menu->removeAction(send);
hitems.removeAt(index);
m_recents.removeAt(index);
}
}
void RecentFileManager::clearFile() {
if (hitems.count() == 0) {
DMessageManager::instance()->sendMessage(m_parent, ICONRES("clearhis"),
tr("NoHistoryDel"));
return;
}
for (auto item : hitems) {
m_menu->removeAction(item);
}
m_recents.clear();
hitems.clear();
QSettings settings(QApplication::organizationName(), "WingHexPy");
settings.setValue("recent", m_recents);
DMessageManager::instance()->sendMessage(m_parent, ICONRES("clearhis"),
tr("HistoryClearFinished"));
}