Messagelib

zoomactionmenu.cpp
1/*
2 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "zoomactionmenu.h"
8#include <KActionCollection>
9#include <KLocalizedString>
10
11using namespace WebEngineViewer;
12namespace
13{
14constexpr qreal zoomBy()
15{
16 return 20;
17}
18}
19
20class WebEngineViewer::ZoomActionMenuPrivate
21{
22public:
23 explicit ZoomActionMenuPrivate(KActionMenu *qq)
24 : q(qq)
25 {
26 }
27
28 void createMenu();
29 qreal mZoomFactor = 100;
30 QAction *mZoomInAction = nullptr;
31 QAction *mZoomOutAction = nullptr;
32 QAction *mZoomResetAction = nullptr;
33 KActionCollection *mActionCollection = nullptr;
34 KActionMenu *const q;
35};
36
37ZoomActionMenu::ZoomActionMenu(QObject *parent)
38 : KActionMenu(parent)
39 , d(new WebEngineViewer::ZoomActionMenuPrivate(this))
40{
41}
42
43ZoomActionMenu::~ZoomActionMenu() = default;
44
45void ZoomActionMenu::setActionCollection(KActionCollection *ac)
46{
47 d->mActionCollection = ac;
48}
49
50void ZoomActionMenu::createZoomActions()
51{
52 // Zoom actions
53 d->mZoomInAction = KStandardAction::zoomIn(this, &ZoomActionMenu::slotZoomIn, this);
54 d->mActionCollection->addAction(QStringLiteral("zoom_in"), d->mZoomInAction);
55
56 d->mZoomOutAction = KStandardAction::zoomOut(this, &ZoomActionMenu::slotZoomOut, this);
57 d->mActionCollection->addAction(QStringLiteral("zoom_out"), d->mZoomOutAction);
58
59 d->mZoomResetAction = KStandardAction::actualSize(this, &ZoomActionMenu::slotZoomReset, this);
60 d->mActionCollection->addAction(QStringLiteral("zoom_reset"), d->mZoomResetAction);
61
62 d->createMenu();
63}
64
65QAction *ZoomActionMenu::zoomInAction() const
66{
67 return d->mZoomInAction;
68}
69
70QAction *ZoomActionMenu::zoomOutAction() const
71{
72 return d->mZoomOutAction;
73}
74
75QAction *ZoomActionMenu::zoomResetAction() const
76{
77 return d->mZoomResetAction;
78}
79
80void ZoomActionMenu::setZoomFactor(qreal zoomFactor)
81{
82 d->mZoomFactor = zoomFactor;
83}
84
85void ZoomActionMenu::setWebViewerZoomFactor(qreal zoomFactor)
86{
87 Q_EMIT zoomChanged(zoomFactor);
88}
89
90void ZoomActionMenu::slotZoomIn()
91{
92 if (d->mZoomFactor >= 300) {
93 return;
94 }
95 d->mZoomFactor += zoomBy();
96 if (d->mZoomFactor > 300) {
97 d->mZoomFactor = 300;
98 }
99 Q_EMIT zoomChanged(d->mZoomFactor / 100.0);
100}
101
102void ZoomActionMenu::slotZoomOut()
103{
104 if (d->mZoomFactor <= 10) {
105 return;
106 }
107 d->mZoomFactor -= zoomBy();
108 if (d->mZoomFactor < 10) {
109 d->mZoomFactor = 10;
110 }
111 Q_EMIT zoomChanged(d->mZoomFactor / 100.0);
112}
113
114void ZoomActionMenu::slotZoomReset()
115{
116 d->mZoomFactor = 100;
117 Q_EMIT zoomChanged(1.0);
118}
119
120qreal ZoomActionMenu::zoomFactor() const
121{
122 return d->mZoomFactor;
123}
124
125void ZoomActionMenuPrivate::createMenu()
126{
127 q->setText(i18n("Zoom"));
128 q->addAction(mZoomInAction);
129 q->addAction(mZoomOutAction);
130 q->addSeparator();
131 q->addAction(mZoomResetAction);
132 mActionCollection->addAction(QStringLiteral("zoom_menu"), q);
133}
134
135#include "moc_zoomactionmenu.cpp"
QAction * addAction(const QString &name, const QObject *receiver=nullptr, const char *member=nullptr)
void addAction(QAction *action)
QString i18n(const char *text, const TYPE &arg...)
QAction * zoomIn(const QObject *recvr, const char *slot, QObject *parent)
QAction * zoomOut(const QObject *recvr, const char *slot, QObject *parent)
QAction * actualSize(const QObject *recvr, const char *slot, QObject *parent)
void setText(const QString &text)
Q_EMITQ_EMIT
T qobject_cast(QObject *object)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 14 2024 11:49:08 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.