2015-10-02 14:48:25 +02:00
|
|
|
/* ============================================================
|
|
|
|
* AutoScroll - Autoscroll for QupZilla
|
2017-01-21 20:23:42 +01:00
|
|
|
* Copyright (C) 2014-2017 David Rosca <nowrep@gmail.com>
|
2015-10-02 14:48:25 +02: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 "autoscroller.h"
|
|
|
|
#include "framescroller.h"
|
|
|
|
#include "webview.h"
|
|
|
|
#include "webpage.h"
|
2015-10-02 15:23:59 +02:00
|
|
|
#include "webhittestresult.h"
|
2015-10-02 14:48:25 +02:00
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QLabel>
|
2017-01-21 20:23:42 +01:00
|
|
|
#include <QIcon>
|
2015-10-02 14:48:25 +02:00
|
|
|
|
2017-01-27 11:04:32 +01:00
|
|
|
ScrollIndicator::ScrollIndicator(QWidget *parent)
|
|
|
|
: QLabel(parent)
|
|
|
|
{
|
|
|
|
resize(33, 33);
|
|
|
|
setContentsMargins(0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
2017-01-27 11:10:09 +01:00
|
|
|
Qt::Orientations ScrollIndicator::orientations() const
|
|
|
|
{
|
|
|
|
return m_orientations;
|
|
|
|
}
|
|
|
|
|
2017-01-27 11:04:32 +01:00
|
|
|
void ScrollIndicator::setOrientations(Qt::Orientations orientations)
|
|
|
|
{
|
|
|
|
m_orientations = orientations;
|
|
|
|
|
|
|
|
if (m_orientations == Qt::Vertical) {
|
|
|
|
setPixmap(QIcon(QSL(":/autoscroll/data/scroll_vertical.png")).pixmap(33));
|
|
|
|
} else if (m_orientations == Qt::Horizontal) {
|
|
|
|
setPixmap(QIcon(QSL(":/autoscroll/data/scroll_horizontal.png")).pixmap(33));
|
|
|
|
} else {
|
|
|
|
setPixmap(QIcon(QSL(":/autoscroll/data/scroll_all.png")).pixmap(33));
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollIndicator::paintEvent(QPaintEvent *event)
|
|
|
|
{
|
|
|
|
QPainter p(this);
|
|
|
|
p.setRenderHint(QPainter::Antialiasing);
|
|
|
|
|
|
|
|
QRectF r(rect());
|
|
|
|
r.adjust(1, 1, -1, -1);
|
|
|
|
|
|
|
|
QColor c1(Qt::gray);
|
|
|
|
c1.setAlpha(190);
|
|
|
|
|
|
|
|
QColor c2(Qt::white);
|
|
|
|
c2.setAlpha(190);
|
|
|
|
|
|
|
|
QRadialGradient g(r.center(), r.height() / 2.0);
|
|
|
|
g.setColorAt(1, c1);
|
|
|
|
g.setColorAt(0.7, c2);
|
|
|
|
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(g);
|
|
|
|
p.drawEllipse(r);
|
|
|
|
|
|
|
|
QLabel::paintEvent(event);
|
|
|
|
}
|
|
|
|
|
2015-10-02 14:48:25 +02:00
|
|
|
AutoScroller::AutoScroller(const QString &settingsFile, QObject* parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_view(0)
|
|
|
|
, m_settingsFile(settingsFile)
|
|
|
|
{
|
2017-01-27 11:04:32 +01:00
|
|
|
m_indicator = new ScrollIndicator;
|
2015-10-02 14:48:25 +02:00
|
|
|
m_indicator->installEventFilter(this);
|
|
|
|
|
|
|
|
QSettings settings(m_settingsFile, QSettings::IniFormat);
|
|
|
|
settings.beginGroup("AutoScroll");
|
|
|
|
|
|
|
|
m_frameScroller = new FrameScroller(this);
|
|
|
|
m_frameScroller->setScrollDivider(settings.value("ScrollDivider", 8.0).toDouble());
|
|
|
|
|
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoScroller::~AutoScroller()
|
|
|
|
{
|
|
|
|
delete m_indicator;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AutoScroller::mouseMove(QObject* obj, QMouseEvent* event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(obj)
|
|
|
|
|
|
|
|
if (m_indicator->isVisible()) {
|
|
|
|
QRect rect = indicatorGlobalRect();
|
|
|
|
int xlength = 0;
|
|
|
|
int ylength = 0;
|
|
|
|
|
|
|
|
if (rect.left() > event->globalPos().x()) {
|
|
|
|
xlength = event->globalPos().x() - rect.left();
|
|
|
|
}
|
|
|
|
else if (rect.right() < event->globalPos().x()) {
|
|
|
|
xlength = event->globalPos().x() - rect.right();
|
|
|
|
}
|
|
|
|
if (rect.top() > event->globalPos().y()) {
|
|
|
|
ylength = event->globalPos().y() - rect.top();
|
|
|
|
}
|
|
|
|
else if (rect.bottom() < event->globalPos().y()) {
|
|
|
|
ylength = event->globalPos().y() - rect.bottom();
|
|
|
|
}
|
|
|
|
|
2017-01-27 11:10:09 +01:00
|
|
|
if (!m_indicator->orientations().testFlag(Qt::Vertical)) {
|
|
|
|
ylength = 0;
|
|
|
|
}
|
|
|
|
if (!m_indicator->orientations().testFlag(Qt::Horizontal)) {
|
|
|
|
xlength = 0;
|
|
|
|
}
|
|
|
|
|
2015-10-02 14:48:25 +02:00
|
|
|
m_frameScroller->startScrolling(xlength, ylength);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AutoScroller::mousePress(QObject* obj, QMouseEvent* event)
|
|
|
|
{
|
|
|
|
bool middleButton = event->buttons() == Qt::MiddleButton;
|
|
|
|
WebView* view = qobject_cast<WebView*>(obj);
|
|
|
|
Q_ASSERT(view);
|
|
|
|
|
|
|
|
// Start?
|
|
|
|
if (m_view != view && middleButton) {
|
|
|
|
return showIndicator(view, event->pos());
|
|
|
|
}
|
|
|
|
else if (!m_indicator->isVisible() && middleButton) {
|
|
|
|
return showIndicator(view, event->pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop
|
|
|
|
if (m_indicator->isVisible()) {
|
|
|
|
stopScrolling();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AutoScroller::mouseRelease(QObject* obj, QMouseEvent* event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(obj)
|
|
|
|
|
|
|
|
if (m_indicator->isVisible()) {
|
|
|
|
if (!indicatorGlobalRect().contains(event->globalPos())) {
|
|
|
|
stopScrolling();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-02 15:23:59 +02:00
|
|
|
bool AutoScroller::wheel(QObject *obj, QWheelEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(obj)
|
|
|
|
Q_UNUSED(event);
|
|
|
|
|
|
|
|
if (m_indicator->isVisible()) {
|
|
|
|
stopScrolling();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-02 14:48:25 +02:00
|
|
|
double AutoScroller::scrollDivider() const
|
|
|
|
{
|
|
|
|
return m_frameScroller->scrollDivider();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutoScroller::setScrollDivider(double divider)
|
|
|
|
{
|
|
|
|
QSettings settings(m_settingsFile, QSettings::IniFormat);
|
|
|
|
settings.beginGroup("AutoScroll");
|
|
|
|
settings.setValue("ScrollDivider", divider);
|
|
|
|
settings.endGroup();
|
|
|
|
|
|
|
|
m_frameScroller->setScrollDivider(divider);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AutoScroller::eventFilter(QObject* obj, QEvent* event)
|
|
|
|
{
|
|
|
|
if (obj == m_indicator) {
|
|
|
|
switch (event->type()) {
|
|
|
|
case QEvent::Enter:
|
|
|
|
m_frameScroller->stopScrolling();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::Wheel:
|
|
|
|
case QEvent::Hide:
|
|
|
|
case QEvent::MouseButtonPress:
|
|
|
|
stopScrolling();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AutoScroller::showIndicator(WebView* view, const QPoint &pos)
|
|
|
|
{
|
2015-10-02 15:23:59 +02:00
|
|
|
const WebHitTestResult res = view->page()->hitTestContent(pos);
|
2015-10-02 14:48:25 +02:00
|
|
|
|
2016-12-26 13:13:22 +01:00
|
|
|
if (res.isContentEditable() || !res.linkUrl().isEmpty() || res.tagName().endsWith(QL1S("frame"))) {
|
2015-10-02 14:48:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-02 15:23:59 +02:00
|
|
|
QString source = QL1S("var out = {"
|
|
|
|
" vertical: window.innerWidth > document.documentElement.clientWidth,"
|
|
|
|
" horizontal: window.innerHeight > document.documentElement.clientHeight"
|
|
|
|
"};"
|
|
|
|
"out;");
|
2015-10-02 14:48:25 +02:00
|
|
|
|
2016-05-10 10:47:35 +02:00
|
|
|
const QVariantMap &map = view->page()->execJavaScript(source, WebPage::SafeJsWorld).toMap();
|
2015-10-02 14:48:25 +02:00
|
|
|
|
2015-10-02 15:23:59 +02:00
|
|
|
bool vertical = map.value(QSL("vertical")).toBool();
|
|
|
|
bool horizontal = map.value(QSL("horizontal")).toBool();
|
2015-10-02 14:48:25 +02:00
|
|
|
|
|
|
|
if (!vertical && !horizontal) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-27 11:04:32 +01:00
|
|
|
Qt::Orientations orientations;
|
|
|
|
if (vertical) {
|
|
|
|
orientations |= Qt::Vertical;
|
2015-10-02 14:48:25 +02:00
|
|
|
}
|
2017-01-27 11:04:32 +01:00
|
|
|
if (horizontal) {
|
|
|
|
orientations |= Qt::Horizontal;
|
2015-10-02 14:48:25 +02:00
|
|
|
}
|
2017-01-27 11:04:32 +01:00
|
|
|
m_indicator->setOrientations(orientations);
|
2015-10-02 14:48:25 +02:00
|
|
|
|
|
|
|
m_view = view;
|
|
|
|
|
|
|
|
QPoint p;
|
2017-01-21 20:23:42 +01:00
|
|
|
p.setX(pos.x() - m_indicator->width() / 2);
|
|
|
|
p.setY(pos.y() - m_indicator->height() / 2);
|
2015-10-02 14:48:25 +02:00
|
|
|
|
2015-10-08 23:51:14 +02:00
|
|
|
m_indicator->setParent(m_view->overlayWidget());
|
|
|
|
m_indicator->move(m_view->mapTo(m_view->overlayWidget(), p));
|
2015-10-02 14:48:25 +02:00
|
|
|
m_indicator->show();
|
|
|
|
|
2015-10-02 15:23:59 +02:00
|
|
|
m_frameScroller->setPage(view->page());
|
2015-10-02 14:48:25 +02:00
|
|
|
|
2015-10-02 15:23:59 +02:00
|
|
|
m_view->inputWidget()->grabMouse();
|
2015-10-02 14:48:25 +02:00
|
|
|
QApplication::setOverrideCursor(Qt::ArrowCursor);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutoScroller::stopScrolling()
|
|
|
|
{
|
2015-10-02 15:23:59 +02:00
|
|
|
m_view->inputWidget()->releaseMouse();
|
2015-10-02 14:48:25 +02:00
|
|
|
QApplication::restoreOverrideCursor();
|
|
|
|
|
|
|
|
m_indicator->hide();
|
|
|
|
m_indicator->setParent(0);
|
|
|
|
m_frameScroller->stopScrolling();
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect AutoScroller::indicatorGlobalRect() const
|
|
|
|
{
|
|
|
|
QPoint pos = m_indicator->parentWidget()->mapToGlobal(m_indicator->geometry().topLeft());
|
|
|
|
return QRect(pos.x(), pos.y(), m_indicator->width(), m_indicator->height());
|
|
|
|
}
|