KXmlGui

khelpmenu.cpp
1/*
2 This file is part of the KDE Libraries
3 SPDX-FileCopyrightText: 1999-2000 Espen Sand <espen@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8// I (espen) prefer that header files are included alphabetically
9
10#include "khelpmenu.h"
11
12#include <QAction>
13#include <QApplication>
14#include <QDesktopServices>
15#include <QDialogButtonBox>
16#include <QLabel>
17#include <QMenu>
18#include <QStandardPaths>
19#include <QStyle>
20#include <QTimer>
21#include <QUrl>
22#include <QWhatsThis>
23#include <QWidget>
24
25#include "kaboutapplicationdialog.h"
26#include "kaboutkdedialog_p.h"
27#include "kbugreport.h"
28#include "kswitchlanguagedialog_p.h"
29
30#include <KAboutData>
31#include <KAuthorized>
32#include <KLocalizedString>
33#include <KStandardActions>
34
35using namespace KDEPrivate;
36
37class KHelpMenuPrivate
38{
39public:
40 KHelpMenuPrivate()
41 : mAboutData(KAboutData::applicationData())
42 {
43 }
44 ~KHelpMenuPrivate()
45 {
46 delete mMenu;
47 delete mAboutApp;
48 delete mAboutKDE;
49 delete mBugReport;
50 delete mSwitchApplicationLanguage;
51 }
52
53 void createActions(KHelpMenu *q, bool showWhatsThis);
54
55 QMenu *mMenu = nullptr;
56 QDialog *mAboutApp = nullptr;
57 KAboutKdeDialog *mAboutKDE = nullptr;
58 KBugReport *mBugReport = nullptr;
59 QAction *mDonateAction = nullptr;
60 KSwitchLanguageDialog *mSwitchApplicationLanguage = nullptr;
61
62 // TODO evaluate if we use static_cast<QWidget*>(parent()) instead of mParent to win that bit of memory
63 QWidget *mParent = nullptr;
64
65 QAction *mHandBookAction = nullptr;
66 QAction *mWhatsThisAction = nullptr;
67 QAction *mReportBugAction = nullptr;
68 QAction *mSwitchApplicationLanguageAction = nullptr;
69 QAction *mAboutAppAction = nullptr;
70 QAction *mAboutKDEAction = nullptr;
71
72 KAboutData mAboutData;
73};
74
75#if KXMLGUI_BUILD_DEPRECATED_SINCE(6, 9)
76KHelpMenu::KHelpMenu(QWidget *parent, const QString &, bool showWhatsThis)
77 : QObject(parent)
78 , d(new KHelpMenuPrivate)
79{
80 d->mParent = parent;
81 d->createActions(this, showWhatsThis);
82}
83#endif
84
86 : QObject(parent)
87 , d(new KHelpMenuPrivate)
88{
89 d->mParent = parent;
90 d->createActions(this, true);
91}
92
93#if KXMLGUI_BUILD_DEPRECATED_SINCE(6, 9)
94KHelpMenu::KHelpMenu(QWidget *parent, const KAboutData &aboutData, bool showWhatsThis)
95 : QObject(parent)
96 , d(new KHelpMenuPrivate)
97{
98 d->mParent = parent;
99 d->mAboutData = aboutData;
100 d->createActions(this, showWhatsThis);
101}
102#endif
103
104KHelpMenu::KHelpMenu(QWidget *parent, const KAboutData &aboutData)
105 : QObject(parent)
106 , d(new KHelpMenuPrivate)
107{
108 d->mParent = parent;
109 d->mAboutData = aboutData;
110 d->createActions(this, true);
111}
112
114{
115 delete d;
116}
117
118void KHelpMenu::setShowWhatsThis(bool showWhatsThis)
119{
120 if (!showWhatsThis) {
121 delete d->mWhatsThisAction;
122 d->mWhatsThisAction = nullptr;
123 } else if (KAuthorized::authorizeAction(QStringLiteral("help_whats_this"))) {
124 d->mWhatsThisAction = KStandardActions::whatsThis(this, &KHelpMenu::contextHelpActivated, this);
125 }
126}
127
128void KHelpMenuPrivate::createActions(KHelpMenu *q, bool showWhatsThis)
129{
130 if (KAuthorized::authorizeAction(QStringLiteral("help_contents"))) {
131 mHandBookAction = KStandardActions::helpContents(q, &KHelpMenu::appHelpActivated, q);
132 }
133 if (showWhatsThis && KAuthorized::authorizeAction(QStringLiteral("help_whats_this"))) {
134 mWhatsThisAction = KStandardActions::whatsThis(q, &KHelpMenu::contextHelpActivated, q);
135 }
136
137 if (KAuthorized::authorizeAction(QStringLiteral("help_report_bug")) && !mAboutData.bugAddress().isEmpty()) {
138 mReportBugAction = KStandardActions::reportBug(q, &KHelpMenu::reportBug, q);
139 }
140
141 if (KAuthorized::authorizeAction(QStringLiteral("help_donate")) && mAboutData.bugAddress() == QLatin1String("submit@bugs.kde.org")) {
142 mDonateAction = KStandardActions::donate(q, &KHelpMenu::donate, q);
143 }
144
145 if (KAuthorized::authorizeAction(QStringLiteral("switch_application_language"))) {
146 mSwitchApplicationLanguageAction = KStandardActions::switchApplicationLanguage(q, &KHelpMenu::switchApplicationLanguage, q);
147 }
148
149 if (KAuthorized::authorizeAction(QStringLiteral("help_about_app"))) {
150 mAboutAppAction = KStandardActions::aboutApp(q, &KHelpMenu::aboutApplication, q);
151 }
152
153 if (KAuthorized::authorizeAction(QStringLiteral("help_about_kde"))) {
154 mAboutKDEAction = KStandardActions::aboutKDE(q, &KHelpMenu::aboutKDE, q);
155 }
156}
157
158// Used in the non-xml-gui case, like kfind or ksnapshot's help button.
160{
161 if (!d->mMenu) {
162 d->mMenu = new QMenu(d->mParent);
163 connect(d->mMenu, &QObject::destroyed, this, &KHelpMenu::menuDestroyed);
164
165 d->mMenu->setTitle(i18n("&Help"));
166
167 bool need_separator = false;
168 if (d->mHandBookAction) {
169 d->mMenu->addAction(d->mHandBookAction);
170 need_separator = true;
171 }
172
173 if (d->mWhatsThisAction) {
174 d->mMenu->addAction(d->mWhatsThisAction);
175 need_separator = true;
176 }
177
178 if (d->mReportBugAction) {
179 if (need_separator) {
180 d->mMenu->addSeparator();
181 }
182 d->mMenu->addAction(d->mReportBugAction);
183 need_separator = true;
184 }
185
186 if (d->mDonateAction) {
187 if (need_separator) {
188 d->mMenu->addSeparator();
189 }
190 d->mMenu->addAction(d->mDonateAction);
191 need_separator = true;
192 }
193
194 if (d->mSwitchApplicationLanguageAction) {
195 if (need_separator) {
196 d->mMenu->addSeparator();
197 }
198 d->mMenu->addAction(d->mSwitchApplicationLanguageAction);
199 need_separator = true;
200 }
201
202 if (need_separator) {
203 d->mMenu->addSeparator();
204 }
205
206 if (d->mAboutAppAction) {
207 d->mMenu->addAction(d->mAboutAppAction);
208 }
209
210 if (d->mAboutKDEAction) {
211 d->mMenu->addAction(d->mAboutKDEAction);
212 }
213 }
214
215 return d->mMenu;
216}
217
219{
220 switch (id) {
221 case menuHelpContents:
222 return d->mHandBookAction;
223
224 case menuWhatsThis:
225 return d->mWhatsThisAction;
226
227 case menuReportBug:
228 return d->mReportBugAction;
229
230 case menuSwitchLanguage:
231 return d->mSwitchApplicationLanguageAction;
232
233 case menuAboutApp:
234 return d->mAboutAppAction;
235
236 case menuAboutKDE:
237 return d->mAboutKDEAction;
238
239 case menuDonate:
240 return d->mDonateAction;
241 }
242
243 return nullptr;
244}
245
247{
248 QDesktopServices::openUrl(QUrl(QStringLiteral("help:/")));
249}
250
252{
253 if (receivers(SIGNAL(showAboutApplication())) > 0) {
255 } else {
256 if (!d->mAboutApp) {
257 d->mAboutApp = new KAboutApplicationDialog(d->mAboutData, d->mParent);
258 connect(d->mAboutApp, &QDialog::finished, this, &KHelpMenu::dialogFinished);
259 }
260 d->mAboutApp->show();
261 }
262}
263
265{
266 if (!d->mAboutKDE) {
267 d->mAboutKDE = new KAboutKdeDialog(d->mParent);
268 connect(d->mAboutKDE, &QDialog::finished, this, &KHelpMenu::dialogFinished);
269 }
270 d->mAboutKDE->show();
271}
272
274{
275 if (!d->mBugReport) {
276 d->mBugReport = new KBugReport(d->mAboutData, d->mParent);
277 connect(d->mBugReport, &QDialog::finished, this, &KHelpMenu::dialogFinished);
278 }
279 d->mBugReport->show();
280}
281
283{
284 if (!d->mSwitchApplicationLanguage) {
285 d->mSwitchApplicationLanguage = new KSwitchLanguageDialog(d->mParent);
286 connect(d->mSwitchApplicationLanguage, &QDialog::finished, this, &KHelpMenu::dialogFinished);
287 }
288 d->mSwitchApplicationLanguage->show();
289}
290
292{
293 QDesktopServices::openUrl(QUrl(QLatin1String("https://www.kde.org/donate?app=") + d->mAboutData.componentName()));
294}
295
296void KHelpMenu::dialogFinished()
297{
298 QTimer::singleShot(0, this, &KHelpMenu::timerExpired);
299}
300
301void KHelpMenu::timerExpired()
302{
303 if (d->mAboutKDE && !d->mAboutKDE->isVisible()) {
304 delete d->mAboutKDE;
305 d->mAboutKDE = nullptr;
306 }
307
308 if (d->mBugReport && !d->mBugReport->isVisible()) {
309 delete d->mBugReport;
310 d->mBugReport = nullptr;
311 }
312
313 if (d->mSwitchApplicationLanguage && !d->mSwitchApplicationLanguage->isVisible()) {
314 delete d->mSwitchApplicationLanguage;
315 d->mSwitchApplicationLanguage = nullptr;
316 }
317
318 if (d->mAboutApp && !d->mAboutApp->isVisible()) {
319 delete d->mAboutApp;
320 d->mAboutApp = nullptr;
321 }
322}
323
324void KHelpMenu::menuDestroyed()
325{
326 d->mMenu = nullptr;
327}
328
333
334#include "moc_khelpmenu.cpp"
Standard "About Application" dialog box.
QString componentName() const
QString bugAddress() const
static Q_INVOKABLE bool authorizeAction(const QString &action)
A dialog box for sending bug reports.
Definition kbugreport.h:34
Standard KDE help menu with dialog boxes.
Definition khelpmenu.h:109
QAction * action(MenuId id) const
Returns the QAction * associated with the given parameter Will return a nullptr if menu() has not bee...
void showAboutApplication()
This signal is emitted from aboutApplication() if no "about application" string has been defined.
void appHelpActivated()
Opens the help page for the application.
void setShowWhatsThis(bool showWhatsThis)
Set whether to show the What's This menu entry in the help menu.
void contextHelpActivated()
Activates What's This help for the application.
KHelpMenu(QWidget *parent, const QString &unused, bool showWhatsThis=true)
Constructor.
Definition khelpmenu.cpp:76
QMenu * menu()
Returns a popup menu you can use in the menu bar or where you need it.
void aboutKDE()
Opens the standard "About KDE" dialog box.
~KHelpMenu() override
Destructor.
void switchApplicationLanguage()
Opens the changing default application language dialog box.
void reportBug()
Opens the standard "Report Bugs" dialog box.
void aboutApplication()
Opens an application specific dialog box.
void donate()
Opens the donate url.
QString i18n(const char *text, const TYPE &arg...)
bool openUrl(const QUrl &url)
void finished(int result)
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
QAction * addSeparator()
void setTitle(const QString &title)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void destroyed(QObject *obj)
QObject * parent() const const
int receivers(const char *signal) const const
bool isEmpty() const const
void enterWhatsThisMode()
void show()
bool isVisible() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:19:32 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.