Messagelib

zoomactionmenu.cpp
1 /*
2  SPDX-FileCopyrightText: 2015-2023 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "zoomactionmenu.h"
8 #include <KActionCollection>
9 #include <KLocalizedString>
10 
11 using namespace WebEngineViewer;
12 namespace
13 {
14 constexpr qreal zoomBy()
15 {
16  return 20;
17 }
18 }
19 
20 class WebEngineViewer::ZoomActionMenuPrivate
21 {
22 public:
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 
37 ZoomActionMenu::ZoomActionMenu(QObject *parent)
38  : KActionMenu(parent)
39  , d(new WebEngineViewer::ZoomActionMenuPrivate(this))
40 {
41 }
42 
43 ZoomActionMenu::~ZoomActionMenu() = default;
44 
45 void ZoomActionMenu::setActionCollection(KActionCollection *ac)
46 {
47  d->mActionCollection = ac;
48 }
49 
50 void 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 
65 QAction *ZoomActionMenu::zoomInAction() const
66 {
67  return d->mZoomInAction;
68 }
69 
70 QAction *ZoomActionMenu::zoomOutAction() const
71 {
72  return d->mZoomOutAction;
73 }
74 
75 QAction *ZoomActionMenu::zoomResetAction() const
76 {
77  return d->mZoomResetAction;
78 }
79 
80 void ZoomActionMenu::setZoomFactor(qreal zoomFactor)
81 {
82  d->mZoomFactor = zoomFactor;
83 }
84 
85 void ZoomActionMenu::setWebViewerZoomFactor(qreal zoomFactor)
86 {
87  Q_EMIT zoomChanged(zoomFactor);
88 }
89 
90 void 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 
102 void 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 
114 void ZoomActionMenu::slotZoomReset()
115 {
116  d->mZoomFactor = 100;
117  Q_EMIT zoomChanged(1.0);
118 }
119 
120 qreal ZoomActionMenu::zoomFactor() const
121 {
122  return d->mZoomFactor;
123 }
124 
125 void 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 }
QAction * zoomOut(const QObject *recvr, const char *slot, QObject *parent)
QString i18n(const char *text, const TYPE &arg...)
QAction * actualSize(const QObject *recvr, const char *slot, QObject *parent)
QAction * zoomIn(const QObject *recvr, const char *slot, QObject *parent)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Mar 26 2023 04:08:12 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.