4 #include <QCoreApplication>
35 m_menu->setTitle(tr(
"&Recent"));
36 m_menu->setIcon(QIcon::fromTheme(QStringLiteral(
"document-open-recent")));
37 m_sep = m_menu->addSeparator();
46 QAction *action =
nullptr;
47 for (
const QString &path : savedEntries) {
48 if (!path.isEmpty()) {
49 action =
new QAction(path, m_menu);
50 action->setProperty(
"file_path", path);
51 m_menu->insertAction(m_sep, action);
52 connect(action, &QAction::triggered,
this, &RecentMenuManager::handleActionTriggered);
56 m_menu->actions().front()->setShortcut(QKeySequence(Qt::Key_F6));
57 m_menu->setEnabled(
true);
66 QStringList existingEntires;
67 QList<QAction *> entryActions = m_menu->actions();
68 existingEntires.reserve(entryActions.size());
69 for (
const QAction *action : entryActions) {
70 QVariant path = action->property(
"file_path");
72 existingEntires << path.toString();
75 return existingEntires;
84 QList<QAction *> existingEntries = m_menu->actions();
85 QAction *entry =
nullptr;
87 for (QAction *existingEntry : existingEntries) {
88 existingEntry->setShortcut(QKeySequence());
90 if (existingEntry->property(
"file_path").toString() == path) {
91 entry = existingEntry;
97 for (
int i = existingEntries.size() - 1; i > 8; --i) {
98 delete existingEntries[i];
100 existingEntries = m_menu->actions();
102 entry =
new QAction(path,
this);
103 entry->setProperty(
"file_path", path);
104 connect(entry, &QAction::triggered,
this, &RecentMenuManager::handleActionTriggered);
107 m_menu->removeAction(entry);
110 entry->setShortcut(QKeySequence(Qt::Key_F6));
112 m_menu->setEnabled(
true);
114 m_menu->insertAction(m_menu->isEmpty() ?
nullptr : m_menu->actions().front(), entry);
122 QList<QAction *> entries = m_menu->actions();
123 for (
auto i = entries.begin(), end = entries.end() - 2; i != end; ++i) {
124 if (*i != m_clearAction) {
128 m_menu->setEnabled(
false);
135 void RecentMenuManager::handleActionTriggered()
137 if (QAction *action = qobject_cast<QAction *>(sender())) {
138 const QString path = action->property(
"file_path").toString();
139 if (!path.isEmpty()) {
140 if (QFile::exists(path)) {
144 msg.setWindowTitle(tr(
"Recently opened files - ") + QCoreApplication::applicationName());
145 msg.setText(tr(
"The selected file can't be found anymore. Do you want "
146 "to delete the obsolete entry from the list?"));
147 msg.setIcon(QMessageBox::Warning);
148 QPushButton *keepEntryButton = msg.addButton(tr(
"keep entry"), QMessageBox::NoRole);
149 QPushButton *deleteEntryButton = msg.addButton(tr(
"delete entry"), QMessageBox::YesRole);
150 msg.setEscapeButton(keepEntryButton);
152 if (msg.clickedButton() == deleteEntryButton) {
154 QList<QAction *> remainingActions = m_menu->actions();
155 if (!remainingActions.isEmpty() && remainingActions.front() != m_sep && remainingActions.front() != m_clearAction) {
156 remainingActions.front()->setShortcut(QKeySequence(Qt::Key_F6));
157 m_menu->setEnabled(
true);
159 m_menu->setEnabled(
false);