1/*
2 * SPDX-FileCopyrightText: 2023 Kai Uwe Broulik <kde@broulik.de>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6
7#include "kopenaction_p.h"
8
9#include <KRecentFilesAction>
10#include <KStandardAction>
11
12#include <QMenu>
13#include <QPointer>
14
15class KOpenActionPrivate
16{
17public:
18 KOpenActionPrivate(KOpenAction *q);
19
20 void updatePopupMode();
21 void onPopupMenuAboutToShow();
22
23 KOpenAction *q;
24 QPointer<KRecentFilesAction> recentFilesAction;
25};
26
27KOpenActionPrivate::KOpenActionPrivate(KOpenAction *q)
28 : q(q)
29{
30}
31
32KOpenAction::~KOpenAction() = default;
33
34void KOpenActionPrivate::updatePopupMode()
35{
36 if (recentFilesAction && recentFilesAction->isEnabled()) {
37 q->setPopupMode(KToolBarPopupAction::MenuButtonPopup);
38 } else {
39 q->setPopupMode(KToolBarPopupAction::NoPopup);
40 }
41}
42
43void KOpenActionPrivate::onPopupMenuAboutToShow()
44{
45 q->popupMenu()->clear();
46
47 if (recentFilesAction) {
48 // Using the menu explicitly rather than the actions so we also get the "Forget..." entry.
49 if (auto *recentMenu = recentFilesAction->menu()) {
50 // Trigger a menu update.
51 Q_EMIT recentMenu->aboutToShow();
52
53 const auto actions = recentMenu->actions();
54 for (QAction *action : actions) {
55 q->popupMenu()->addAction(action);
56 }
57 }
58 }
59}
60
61KOpenAction::KOpenAction(QObject *parent)
62 : KOpenAction(QIcon(), QString(), parent)
63{
64}
65
66KOpenAction::KOpenAction(const QIcon &icon, const QString &text, QObject *parent)
67 : KToolBarPopupAction(icon, text, parent)
68 , d(new KOpenActionPrivate(this))
69{
70 setPopupMode(KToolBarPopupAction::NoPopup);
71 connect(sender: popupMenu(), signal: &QMenu::aboutToShow, context: this, slot: [this] {
72 d->onPopupMenuAboutToShow();
73 });
74}
75
76QWidget *KOpenAction::createWidget(QWidget *parentWidget)
77{
78 if (!d->recentFilesAction) {
79 // Find the accompanying file_open_recent action.
80 QAction *action = nullptr;
81
82 if (parent() && parent()->inherits(classname: "KActionCollection")) {
83 const QString openRecentActionId = KStandardAction::name(id: KStandardAction::OpenRecent);
84 QMetaObject::invokeMethod(obj: parent(), member: "action", Q_RETURN_ARG(QAction *, action), Q_ARG(QString, openRecentActionId));
85 }
86
87 d->recentFilesAction = qobject_cast<KRecentFilesAction *>(object: action);
88 if (d->recentFilesAction) {
89 connect(sender: d->recentFilesAction.data(), signal: &QAction::enabledChanged, context: this, slot: [this] {
90 d->updatePopupMode();
91 });
92 }
93 }
94
95 d->updatePopupMode();
96
97 return KToolBarPopupAction::createWidget(parent: parentWidget);
98}
99
100#include "moc_kopenaction_p.cpp"
101

source code of kconfigwidgets/src/kopenaction.cpp