2016-12-26 12:38:51 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - Qt web browser
|
2017-01-21 21:22:11 +01:00
|
|
|
* Copyright (C) 2016-2017 David Rosca <nowrep@gmail.com>
|
2016-12-26 12:38:51 +01:00
|
|
|
*
|
|
|
|
* 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());
|
|
|
|
|
2016-12-27 12:20:16 +01:00
|
|
|
int newValue;
|
2016-12-26 12:38:51 +01:00
|
|
|
|
|
|
|
if (orientation() == Qt::Vertical) {
|
2016-12-27 13:41:12 +01:00
|
|
|
setFixedHeight(m_view->height() - (m_view->height() - viewport.height()) * devicePixelRatioF());
|
2016-12-26 12:38:51 +01:00
|
|
|
move(m_view->width() - width(), 0);
|
|
|
|
setPageStep(viewport.height());
|
2017-01-21 21:22:11 +01:00
|
|
|
setMaximum(qMax(0, m_view->page()->contentsSize().toSize().height() - viewport.height()));
|
2016-12-27 12:20:16 +01:00
|
|
|
newValue = m_view->page()->scrollPosition().toPoint().y();
|
2016-12-26 12:38:51 +01:00
|
|
|
} else {
|
2016-12-27 13:41:12 +01:00
|
|
|
setFixedWidth(m_view->width() - (m_view->width() - viewport.width()) * devicePixelRatioF());
|
2016-12-26 12:38:51 +01:00
|
|
|
move(0, m_view->height() - height());
|
|
|
|
setPageStep(viewport.width());
|
2017-01-21 21:22:11 +01:00
|
|
|
setMaximum(qMax(0, m_view->page()->contentsSize().toSize().width() - viewport.width()));
|
2016-12-27 12:20:16 +01:00
|
|
|
newValue = m_view->page()->scrollPosition().toPoint().x();
|
2016-12-26 12:38:51 +01:00
|
|
|
}
|
|
|
|
|
2016-12-27 12:20:16 +01:00
|
|
|
if (!isSliderDown()) {
|
|
|
|
m_blockScrolling = true;
|
|
|
|
setValue(newValue);
|
|
|
|
m_blockScrolling = false;
|
|
|
|
}
|
2016-12-26 12:38:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2017-01-31 22:06:48 +01:00
|
|
|
painter.fillRect(ev->rect(), palette().background());
|
2016-12-26 12:38:51 +01:00
|
|
|
QScrollBar::paintEvent(ev);
|
|
|
|
}
|