File indexing completed on 2025-01-26 04:57:25

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "zoomactionmenu.h"
0008 #include <KActionCollection>
0009 #include <KLocalizedString>
0010 
0011 using namespace WebEngineViewer;
0012 namespace
0013 {
0014 constexpr qreal zoomBy()
0015 {
0016     return 20;
0017 }
0018 }
0019 
0020 class WebEngineViewer::ZoomActionMenuPrivate
0021 {
0022 public:
0023     explicit ZoomActionMenuPrivate(KActionMenu *qq)
0024         : q(qq)
0025     {
0026     }
0027 
0028     void createMenu();
0029     qreal mZoomFactor = 100;
0030     QAction *mZoomInAction = nullptr;
0031     QAction *mZoomOutAction = nullptr;
0032     QAction *mZoomResetAction = nullptr;
0033     KActionCollection *mActionCollection = nullptr;
0034     KActionMenu *const q;
0035 };
0036 
0037 ZoomActionMenu::ZoomActionMenu(QObject *parent)
0038     : KActionMenu(parent)
0039     , d(new WebEngineViewer::ZoomActionMenuPrivate(this))
0040 {
0041 }
0042 
0043 ZoomActionMenu::~ZoomActionMenu() = default;
0044 
0045 void ZoomActionMenu::setActionCollection(KActionCollection *ac)
0046 {
0047     d->mActionCollection = ac;
0048 }
0049 
0050 void ZoomActionMenu::createZoomActions()
0051 {
0052     // Zoom actions
0053     d->mZoomInAction = KStandardAction::zoomIn(this, &ZoomActionMenu::slotZoomIn, this);
0054     d->mActionCollection->addAction(QStringLiteral("zoom_in"), d->mZoomInAction);
0055 
0056     d->mZoomOutAction = KStandardAction::zoomOut(this, &ZoomActionMenu::slotZoomOut, this);
0057     d->mActionCollection->addAction(QStringLiteral("zoom_out"), d->mZoomOutAction);
0058 
0059     d->mZoomResetAction = KStandardAction::actualSize(this, &ZoomActionMenu::slotZoomReset, this);
0060     d->mActionCollection->addAction(QStringLiteral("zoom_reset"), d->mZoomResetAction);
0061 
0062     d->createMenu();
0063 }
0064 
0065 QAction *ZoomActionMenu::zoomInAction() const
0066 {
0067     return d->mZoomInAction;
0068 }
0069 
0070 QAction *ZoomActionMenu::zoomOutAction() const
0071 {
0072     return d->mZoomOutAction;
0073 }
0074 
0075 QAction *ZoomActionMenu::zoomResetAction() const
0076 {
0077     return d->mZoomResetAction;
0078 }
0079 
0080 void ZoomActionMenu::setZoomFactor(qreal zoomFactor)
0081 {
0082     d->mZoomFactor = zoomFactor;
0083 }
0084 
0085 void ZoomActionMenu::setWebViewerZoomFactor(qreal zoomFactor)
0086 {
0087     Q_EMIT zoomChanged(zoomFactor);
0088 }
0089 
0090 void ZoomActionMenu::slotZoomIn()
0091 {
0092     if (d->mZoomFactor >= 300) {
0093         return;
0094     }
0095     d->mZoomFactor += zoomBy();
0096     if (d->mZoomFactor > 300) {
0097         d->mZoomFactor = 300;
0098     }
0099     Q_EMIT zoomChanged(d->mZoomFactor / 100.0);
0100 }
0101 
0102 void ZoomActionMenu::slotZoomOut()
0103 {
0104     if (d->mZoomFactor <= 10) {
0105         return;
0106     }
0107     d->mZoomFactor -= zoomBy();
0108     if (d->mZoomFactor < 10) {
0109         d->mZoomFactor = 10;
0110     }
0111     Q_EMIT zoomChanged(d->mZoomFactor / 100.0);
0112 }
0113 
0114 void ZoomActionMenu::slotZoomReset()
0115 {
0116     d->mZoomFactor = 100;
0117     Q_EMIT zoomChanged(1.0);
0118 }
0119 
0120 qreal ZoomActionMenu::zoomFactor() const
0121 {
0122     return d->mZoomFactor;
0123 }
0124 
0125 void ZoomActionMenuPrivate::createMenu()
0126 {
0127     q->setText(i18n("Zoom"));
0128     q->addAction(mZoomInAction);
0129     q->addAction(mZoomOutAction);
0130     q->addSeparator();
0131     q->addAction(mZoomResetAction);
0132     mActionCollection->addAction(QStringLiteral("zoom_menu"), q);
0133 }
0134 
0135 #include "moc_zoomactionmenu.cpp"