File indexing completed on 2024-05-12 04:58:10

0001 /* ============================================================
0002  * ZoomLabel - Shows current zoom level in locationbar
0003  * Copyright (C) 2023 Juraj Oravec <jurajoravec@mailo.com>
0004  *
0005  * This program is free software: you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation, either version 3 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017  * ============================================================ */
0018 #include "zoomlabel.h"
0019 #include "locationbar.h"
0020 #include "mainapplication.h"
0021 #include "tabbedwebview.h"
0022 #include "qzsettings.h"
0023 
0024 #include <QApplication>
0025 #include <QPainter>
0026 
0027 ZoomLabel::ZoomLabel(LocationBar* parent)
0028     : ClickableLabel(parent)
0029     , m_locationBar(parent)
0030     , m_view(nullptr)
0031 {
0032     setObjectName(QSL("locationbar-zoomlabel"));
0033     setCursor(Qt::PointingHandCursor);
0034     setFocusPolicy(Qt::NoFocus);
0035     setScaledContents(true);
0036     setToolTip(tr("Reset zoom level"));
0037 
0038     connect(mApp, &MainApplication::settingsReloaded, this, [this]() {
0039         if (this->m_view) {
0040             this->valueChanged(m_view->zoomLevel());
0041         }
0042     });
0043 }
0044 
0045 void ZoomLabel::setWebView(WebView* view)
0046 {
0047     m_view = view;
0048     connect(view, &WebView::zoomLevelChanged, this, &ZoomLabel::valueChanged);
0049     connect(this, &ZoomLabel::clicked, view, &WebView::zoomReset);
0050     valueChanged(m_view->zoomLevel());
0051 }
0052 
0053 void ZoomLabel::requestShow()
0054 {
0055     valueChanged(m_view->zoomLevel());
0056 }
0057 
0058 void ZoomLabel::valueChanged(int value)
0059 {
0060     if ((m_view) && (value != qzSettings->defaultZoomLevel) && (qzSettings->showZoomLabel)) {
0061         setText(tr("%1%").arg(m_view->zoomFactor() * 100));
0062         show();
0063     }
0064     else {
0065         hide();
0066     }
0067 }
0068 
0069 void ZoomLabel::paintEvent(QPaintEvent* e)
0070 {
0071     QPainter p(this);
0072 
0073     QFontMetrics fmNormalFont(font());
0074     QFont smallFont(font());
0075     smallFont.setPointSizeF(smallFont.pointSizeF() * 0.8);
0076     p.setFont(smallFont);
0077 
0078     QFontMetrics fmSmallFont(smallFont);
0079     int fontSizeDiff = fmNormalFont.height() - fmSmallFont.height();
0080 
0081     QRect rect = e->rect();
0082     rect.setY(rect.y() + (fontSizeDiff * 2));
0083     rect.setHeight(fmSmallFont.height());
0084     p.fillRect(rect, QApplication::palette().color(QPalette::Base));
0085 
0086     rect.setX(rect.x() + (fmNormalFont.horizontalAdvance(text()) - fmSmallFont.horizontalAdvance(text())) / 2);
0087     p.drawText(rect, text());
0088 }