KConfigWidgets

kopenaction.cpp
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()) {
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(popupMenu(), &QMenu::aboutToShow, this, [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("KActionCollection")) {
84 QMetaObject::invokeMethod(parent(), "action", Q_RETURN_ARG(QAction *, action), Q_ARG(QString, openRecentActionId));
85 }
86
87 d->recentFilesAction = qobject_cast<KRecentFilesAction *>(action);
88 if (d->recentFilesAction) {
89 connect(d->recentFilesAction.data(), &QAction::enabledChanged, this, [this] {
90 d->updatePopupMode();
91 });
92 }
93 }
94
95 d->updatePopupMode();
96
97 return KToolBarPopupAction::createWidget(parentWidget);
98}
99
100#include "moc_kopenaction_p.cpp"
QWidget * createWidget(QWidget *parent) override
QString name(StandardAction id)
This will return the internal name of a given standard action.
@ OpenRecent
Open a recently used document.
void enabledChanged(bool enabled)
void aboutToShow()
bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:50:09 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.