Messagelib

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

KDE's Doxygen guidelines are available online.