1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 17:42:10 +01:00
falkonOfficial/src/lib/webengine/webscrollbar.cpp
2016-12-27 13:41:30 +01:00

94 lines
2.9 KiB
C++

/* ============================================================
* QupZilla - Qt web browser
* Copyright (C) 2016 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "webscrollbar.h"
#include "webview.h"
#include "webpage.h"
#include <QPaintEvent>
WebScrollBar::WebScrollBar(Qt::Orientation orientation, WebView *view)
: QScrollBar(orientation)
, m_view(view)
{
setFocusProxy(m_view);
resize(sizeHint());
connect(this, &QScrollBar::valueChanged, this, &WebScrollBar::performScroll);
connect(view, &WebView::focusChanged, this, [this]() { update(); });
}
int WebScrollBar::thickness() const
{
return orientation() == Qt::Vertical ? width() : height();
}
void WebScrollBar::updateValues(const QSize &viewport)
{
setMinimum(0);
setParent(m_view->overlayWidget());
int newValue;
if (orientation() == Qt::Vertical) {
setFixedHeight(m_view->height() - (m_view->height() - viewport.height()) * devicePixelRatioF());
move(m_view->width() - width(), 0);
setPageStep(viewport.height());
setMaximum(std::max(0, m_view->page()->contentsSize().toSize().height() - viewport.height()));
newValue = m_view->page()->scrollPosition().toPoint().y();
} else {
setFixedWidth(m_view->width() - (m_view->width() - viewport.width()) * devicePixelRatioF());
move(0, m_view->height() - height());
setPageStep(viewport.width());
setMaximum(std::max(0, m_view->page()->contentsSize().toSize().width() - viewport.width()));
newValue = m_view->page()->scrollPosition().toPoint().x();
}
if (!isSliderDown()) {
m_blockScrolling = true;
setValue(newValue);
m_blockScrolling = false;
}
setVisible(maximum() > minimum());
}
void WebScrollBar::performScroll()
{
if (m_blockScrolling) {
return;
}
QPointF pos = m_view->page()->scrollPosition();
if (orientation() == Qt::Vertical) {
pos.setY(value());
} else {
pos.setX(value());
}
m_view->page()->setScrollPosition(pos);
}
void WebScrollBar::paintEvent(QPaintEvent *ev)
{
QPainter painter(this);
painter.fillRect(ev->rect(), m_view->page()->backgroundColor());
QScrollBar::paintEvent(ev);
}