Falkon Develop
Cross-platform Qt-based web browser
webscrollbar.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2016-2017 David Rosca <nowrep@gmail.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
19#include "webscrollbar.h"
20#include "webview.h"
21#include "webpage.h"
22
23#include <QPaintEvent>
24#include <QPainter>
25
26WebScrollBar::WebScrollBar(Qt::Orientation orientation, WebView *view)
27 : QScrollBar(orientation)
28 , m_view(view)
29{
30 setFocusProxy(m_view);
31 resize(sizeHint());
32
33 connect(this, &QScrollBar::valueChanged, this, &WebScrollBar::performScroll);
34 connect(view, &WebView::focusChanged, this, [this]() { update(); });
35}
36
38{
39 return orientation() == Qt::Vertical ? width() : height();
40}
41
42void WebScrollBar::updateValues(const QSize &viewport)
43{
44 setMinimum(0);
45 setParent(m_view->overlayWidget());
46
47 int newValue;
48
49 m_blockScrolling = true;
50
51 if (orientation() == Qt::Vertical) {
52 setFixedHeight(m_view->height() - (m_view->height() - viewport.height()) * devicePixelRatioF());
53 move(m_view->width() - width(), 0);
54 setPageStep(viewport.height());
55 setMaximum(qMax(0, m_view->page()->contentsSize().toSize().height() - viewport.height()));
56 newValue = m_view->page()->scrollPosition().toPoint().y();
57 } else {
58 setFixedWidth(m_view->width() - (m_view->width() - viewport.width()) * devicePixelRatioF());
59 move(0, m_view->height() - height());
60 setPageStep(viewport.width());
61 setMaximum(qMax(0, m_view->page()->contentsSize().toSize().width() - viewport.width()));
62 newValue = m_view->page()->scrollPosition().toPoint().x();
63 }
64
65 if (!isSliderDown()) {
66 setValue(newValue);
67 }
68
69 m_blockScrolling = false;
70}
71
72void WebScrollBar::performScroll()
73{
74 if (m_blockScrolling) {
75 return;
76 }
77
78 QPointF pos = m_view->page()->scrollPosition();
79
80 if (orientation() == Qt::Vertical) {
81 pos.setY(value());
82 } else {
83 pos.setX(value());
84 }
85
86 m_view->page()->setScrollPosition(pos);
87}
88
89void WebScrollBar::paintEvent(QPaintEvent *ev)
90{
91 QPainter painter(this);
92 painter.fillRect(ev->rect(), palette().window());
93 QScrollBar::paintEvent(ev);
94}
void setScrollPosition(const QPointF &pos)
Definition: webpage.cpp:199
int thickness() const
void updateValues(const QSize &viewport)
WebScrollBar(Qt::Orientation orientation, WebView *view)
virtual QWidget * overlayWidget()=0
WebPage * page() const
Definition: webview.cpp:132
void focusChanged(bool)
int value(const QColor &c)
Definition: colors.cpp:238