Falkon Develop
Cross-platform Qt-based web browser
zoomlabel.cpp
Go to the documentation of this file.
1/* ============================================================
2 * ZoomLabel - Shows current zoom level in locationbar
3 * Copyright (C) 2023 Juraj Oravec <jurajoravec@mailo.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * ============================================================ */
18#include "zoomlabel.h"
19#include "locationbar.h"
20#include "mainapplication.h"
21#include "tabbedwebview.h"
22#include "qzsettings.h"
23
24#include <QApplication>
25#include <QPainter>
26
28 : ClickableLabel(parent)
29 , m_locationBar(parent)
30 , m_view(nullptr)
31{
32 setObjectName(QSL("locationbar-zoomlabel"));
33 setCursor(Qt::PointingHandCursor);
34 setFocusPolicy(Qt::NoFocus);
35 setScaledContents(true);
36 setToolTip(tr("Reset zoom level"));
37
38 connect(mApp, &MainApplication::settingsReloaded, this, [this]() {
39 if (this->m_view) {
40 this->valueChanged(m_view->zoomLevel());
41 }
42 });
43}
44
46{
47 m_view = view;
48 connect(view, &WebView::zoomLevelChanged, this, &ZoomLabel::valueChanged);
49 connect(this, &ZoomLabel::clicked, view, &WebView::zoomReset);
50 valueChanged(m_view->zoomLevel());
51}
52
54{
55 valueChanged(m_view->zoomLevel());
56}
57
58void ZoomLabel::valueChanged(int value)
59{
60 if ((m_view) && (value != qzSettings->defaultZoomLevel) && (qzSettings->showZoomLabel)) {
61 setText(tr("%1%").arg(m_view->zoomFactor() * 100));
62 show();
63 }
64 else {
65 hide();
66 }
67}
68
69void ZoomLabel::paintEvent(QPaintEvent* e)
70{
71 QPainter p(this);
72
73 QFontMetrics fmNormalFont(font());
74 QFont smallFont(font());
75 smallFont.setPointSizeF(smallFont.pointSizeF() * 0.8);
76 p.setFont(smallFont);
77
78 QFontMetrics fmSmallFont(smallFont);
79 int fontSizeDiff = fmNormalFont.height() - fmSmallFont.height();
80
81 QRect rect = e->rect();
82 rect.setY(rect.y() + (fontSizeDiff * 2));
83 rect.setHeight(fmSmallFont.height());
84 p.fillRect(rect, QApplication::palette().color(QPalette::Base));
85
86 rect.setX(rect.x() + (fmNormalFont.horizontalAdvance(text()) - fmSmallFont.horizontalAdvance(text())) / 2);
87 p.drawText(rect, text());
88}
void clicked(QPoint)
void settingsReloaded()
void zoomReset()
Definition: webview.cpp:314
int zoomLevel() const
Definition: webview.cpp:229
void zoomLevelChanged(int)
void paintEvent(QPaintEvent *e) override
Definition: zoomlabel.cpp:69
void setWebView(WebView *view)
Definition: zoomlabel.cpp:45
ZoomLabel(LocationBar *parent)
Definition: zoomlabel.cpp:27
void requestShow()
Definition: zoomlabel.cpp:53
#define mApp
int value(const QColor &c)
Definition: colors.cpp:238
#define QSL(x)
Definition: qzcommon.h:40
#define qzSettings
Definition: qzsettings.h:69