4 #include <QCoreApplication> 33 m_menu->setTitle(tr(
"&Recent"));
34 m_menu->setIcon(QIcon::fromTheme(QStringLiteral(
"document-open-recent")));
35 m_sep = m_menu->addSeparator();
44 QAction *action =
nullptr;
45 for(
const QString &path : savedEntries) {
47 action =
new QAction(path, m_menu);
48 action->setProperty(
"file_path", path);
49 m_menu->insertAction(m_sep, action);
50 connect(action, &QAction::triggered,
this, &RecentMenuManager::handleActionTriggered);
54 m_menu->actions().front()->setShortcut(QKeySequence(Qt::Key_F6));
55 m_menu->setEnabled(
true);
64 QStringList existingEntires;
65 QList<QAction *> entryActions = m_menu->actions();
66 existingEntires.reserve(entryActions.size());
67 for(
const QAction *action : entryActions) {
68 QVariant path = action->property(
"file_path");
70 existingEntires << path.toString();
73 return existingEntires;
81 QList<QAction *> existingEntries = m_menu->actions();
82 QAction *entry =
nullptr;
84 for(QAction *existingEntry : existingEntries) {
85 existingEntry->setShortcut(QKeySequence());
87 if(existingEntry->property(
"file_path").toString() == path) {
88 entry = existingEntry;
94 for(
int i = existingEntries.size() - 1; i > 8; --i) {
95 delete existingEntries[i];
97 existingEntries = m_menu->actions();
99 entry =
new QAction(path,
this);
100 entry->setProperty(
"file_path", path);
101 connect(entry, &QAction::triggered,
this, &RecentMenuManager::handleActionTriggered);
104 m_menu->removeAction(entry);
107 entry->setShortcut(QKeySequence(Qt::Key_F6));
109 m_menu->setEnabled(
true);
111 m_menu->insertAction(m_menu->isEmpty() ? nullptr : m_menu->actions().front(), entry);
119 QList<QAction *> entries = m_menu->actions();
120 for(
auto i = entries.begin(), end = entries.end() - 2; i != end; ++i) {
121 if(*i != m_clearAction) {
125 m_menu->setEnabled(
false);
131 void RecentMenuManager::handleActionTriggered()
133 if(QAction *action = qobject_cast<QAction *>(sender())) {
134 const QString path = action->property(
"file_path").toString();
135 if(!path.isEmpty()) {
136 if(QFile::exists(path)) {
140 msg.setWindowTitle(tr(
"Recently opened files - ") + QCoreApplication::applicationName());
141 msg.setText(tr(
"The selected file can't be found anymore. Do you want to delete the obsolete entry from the list?"));
143 QPushButton *keepEntryButton = msg.addButton(tr(
"keep entry"), QMessageBox::NoRole);
144 QPushButton *deleteEntryButton = msg.addButton(tr(
"delete entry"), QMessageBox::YesRole);
145 msg.setEscapeButton(keepEntryButton);
147 if(msg.clickedButton() == deleteEntryButton) {
149 QList<QAction *> remainingActions = m_menu->actions();
150 if(!remainingActions.isEmpty() && remainingActions.front() != m_sep && remainingActions.front() != m_clearAction) {
151 remainingActions.front()->setShortcut(QKeySequence(Qt::Key_F6));
152 m_menu->setEnabled(
true);
154 m_menu->setEnabled(
false);