-
Notifications
You must be signed in to change notification settings - Fork 0
/
QRecentFilesMenu.cpp
122 lines (99 loc) · 2.77 KB
/
QRecentFilesMenu.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
#include "QRecentFilesMenu.h"
#include <QFileInfo>
static const qint32 RecentFilesMenuMagic = 0xff;
QRecentFilesMenu::QRecentFilesMenu(QWidget * parent)
: QMenu(parent)
, m_maxCount(5)
, m_format(QLatin1String("%d %s"))
{
connect(this, SIGNAL(triggered(QAction*)), this, SLOT(menuTriggered(QAction*)));
setMaxCount(m_maxCount);
}
QRecentFilesMenu::QRecentFilesMenu(const QString & title, QWidget * parent)
: QMenu(title, parent)
, m_maxCount(5)
, m_format(QLatin1String("%d %s"))
{
connect(this, SIGNAL(triggered(QAction*)), this, SLOT(menuTriggered(QAction*)));
setMaxCount(m_maxCount);
}
void QRecentFilesMenu::addRecentFile(const QString &fileName)
{
m_files.removeAll(fileName);
m_files.prepend(fileName);
while (m_files.size() > maxCount())
m_files.removeLast();
updateRecentFileActions();
}
void QRecentFilesMenu::clearMenu()
{
m_files.clear();
updateRecentFileActions();
}
int QRecentFilesMenu::maxCount() const
{
return m_maxCount;
}
void QRecentFilesMenu::setFormat(const QString &format)
{
if (m_format == format)
return;
m_format = format;
updateRecentFileActions();
}
const QString & QRecentFilesMenu::format() const
{
return m_format;
}
QByteArray QRecentFilesMenu::saveState() const
{
int version = 0;
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
stream << qint32(RecentFilesMenuMagic);
stream << qint32(version);
stream << m_files;
return data;
}
bool QRecentFilesMenu::restoreState(const QByteArray &state)
{
int version = 0;
QByteArray sd = state;
QDataStream stream(&sd, QIODevice::ReadOnly);
qint32 marker;
qint32 v;
stream >> marker;
stream >> v;
if (marker != RecentFilesMenuMagic || v != version)
return false;
stream >> m_files;
updateRecentFileActions();
return true;
}
void QRecentFilesMenu::setMaxCount(int count)
{
m_maxCount = count;
updateRecentFileActions();
}
void QRecentFilesMenu::menuTriggered(QAction* action)
{
if (action->data().isValid())
emit recentFileTriggered(action->data().toString());
}
void QRecentFilesMenu::updateRecentFileActions()
{
int numRecentFiles = qMin(m_files.size(), maxCount());
clear();
for (int i = 0; i < numRecentFiles; ++i) {
QString strippedName = QFileInfo(m_files[i]).fileName();
QString text = m_format;
text.replace(QLatin1String("%d"), "");
text.replace(QLatin1String("%s"), strippedName);
//打开多个文件后,有多个文本,每次重新定义指针
QAction* recentFileAct = addAction(text);
recentFileAct->setData(m_files[i]);
}
addSeparator();
addAction(tr("Clear Menu"), this, SLOT(clearMenu()));
setEnabled(numRecentFiles > 0);
}