2012-03-04 18:30:34 +01:00
|
|
|
/* ============================================================
|
|
|
|
* Mouse Gestures plugin for QupZilla
|
2017-01-22 09:45:24 +01:00
|
|
|
* Copyright (C) 2013-2017 David Rosca <nowrep@gmail.com>
|
2012-03-04 18:30:34 +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/>.
|
|
|
|
* ============================================================ */
|
2012-02-24 21:03:44 +01:00
|
|
|
#include "mousegestures.h"
|
2012-04-04 17:41:33 +02:00
|
|
|
#include "webpage.h"
|
2012-09-08 20:10:41 +02:00
|
|
|
#include "tabbedwebview.h"
|
|
|
|
#include "tabwidget.h"
|
2012-02-24 21:03:44 +01:00
|
|
|
#include "mainapplication.h"
|
2014-02-19 22:07:21 +01:00
|
|
|
#include "browserwindow.h"
|
2016-04-20 10:12:10 +02:00
|
|
|
#include "locationbar.h"
|
2012-02-24 21:03:44 +01:00
|
|
|
#include "mousegesturessettingsdialog.h"
|
|
|
|
|
2012-03-04 18:30:34 +01:00
|
|
|
#include "QjtMouseGestureFilter.h"
|
|
|
|
#include "QjtMouseGesture.h"
|
|
|
|
|
2012-04-04 17:41:33 +02:00
|
|
|
#include <QMouseEvent>
|
2015-10-02 11:14:10 +02:00
|
|
|
#include <QWebEngineHistory>
|
2014-04-05 17:46:05 +02:00
|
|
|
#include <QSettings>
|
2012-04-04 17:41:33 +02:00
|
|
|
|
2014-04-05 17:46:05 +02:00
|
|
|
MouseGestures::MouseGestures(const QString &settingsPath, QObject* parent)
|
2014-04-05 17:04:41 +02:00
|
|
|
: QObject(parent)
|
2014-04-05 17:46:05 +02:00
|
|
|
, m_filter(0)
|
|
|
|
, m_settingsFile(settingsPath + QL1S("/extensions.ini"))
|
|
|
|
, m_button(Qt::MiddleButton)
|
|
|
|
, m_enableRockerNavigation(false)
|
2014-04-05 17:04:41 +02:00
|
|
|
, m_blockNextRightMouseRelease(false)
|
|
|
|
, m_blockNextLeftMouseRelease(false)
|
2012-02-24 21:03:44 +01:00
|
|
|
{
|
2014-04-05 17:46:05 +02:00
|
|
|
loadSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MouseGestures::initFilter()
|
|
|
|
{
|
|
|
|
if (m_filter) {
|
|
|
|
m_filter->clearGestures(true);
|
|
|
|
delete m_filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_filter = new QjtMouseGestureFilter(false, m_button, 20);
|
2012-02-24 21:03:44 +01:00
|
|
|
|
|
|
|
QjtMouseGesture* upGesture = new QjtMouseGesture(DirectionList() << Up, m_filter);
|
|
|
|
connect(upGesture, SIGNAL(gestured()), this, SLOT(upGestured()));
|
|
|
|
|
|
|
|
QjtMouseGesture* downGesture = new QjtMouseGesture(DirectionList() << Down, m_filter);
|
|
|
|
connect(downGesture, SIGNAL(gestured()), this, SLOT(downGestured()));
|
|
|
|
|
|
|
|
QjtMouseGesture* leftGesture = new QjtMouseGesture(DirectionList() << Left, m_filter);
|
|
|
|
connect(leftGesture, SIGNAL(gestured()), this, SLOT(leftGestured()));
|
|
|
|
|
|
|
|
QjtMouseGesture* rightGesture = new QjtMouseGesture(DirectionList() << Right, m_filter);
|
|
|
|
connect(rightGesture, SIGNAL(gestured()), this, SLOT(rightGestured()));
|
|
|
|
|
|
|
|
QjtMouseGesture* downRightGesture = new QjtMouseGesture(DirectionList() << Down << Right, m_filter);
|
|
|
|
connect(downRightGesture, SIGNAL(gestured()), this, SLOT(downRightGestured()));
|
|
|
|
|
|
|
|
QjtMouseGesture* downLeftGesture = new QjtMouseGesture(DirectionList() << Down << Left, m_filter);
|
|
|
|
connect(downLeftGesture, SIGNAL(gestured()), this, SLOT(downLeftGestured()));
|
|
|
|
|
2014-11-20 01:32:31 +01:00
|
|
|
QjtMouseGesture* downUpGesture = new QjtMouseGesture(DirectionList() << Down << Up, m_filter);
|
|
|
|
connect(downUpGesture, SIGNAL(gestured()), this, SLOT(downUpGestured()));
|
|
|
|
|
2012-02-24 21:03:44 +01:00
|
|
|
QjtMouseGesture* upDownGesture = new QjtMouseGesture(DirectionList() << Up << Down, m_filter);
|
|
|
|
connect(upDownGesture, SIGNAL(gestured()), this, SLOT(upDownGestured()));
|
|
|
|
|
2012-09-08 20:10:41 +02:00
|
|
|
QjtMouseGesture* upLeftGesture = new QjtMouseGesture(DirectionList() << Up << Left, m_filter);
|
|
|
|
connect(upLeftGesture, SIGNAL(gestured()), this, SLOT(upLeftGestured()));
|
|
|
|
|
|
|
|
QjtMouseGesture* upRightGesture = new QjtMouseGesture(DirectionList() << Up << Right, m_filter);
|
|
|
|
connect(upRightGesture, SIGNAL(gestured()), this, SLOT(upRightGestured()));
|
|
|
|
|
2012-02-24 21:03:44 +01:00
|
|
|
m_filter->addGesture(upGesture);
|
|
|
|
m_filter->addGesture(downGesture);
|
|
|
|
m_filter->addGesture(leftGesture);
|
|
|
|
m_filter->addGesture(rightGesture);
|
|
|
|
|
|
|
|
m_filter->addGesture(downRightGesture);
|
|
|
|
m_filter->addGesture(downLeftGesture);
|
2014-11-20 01:32:31 +01:00
|
|
|
m_filter->addGesture(downUpGesture);
|
2012-02-24 21:03:44 +01:00
|
|
|
m_filter->addGesture(upDownGesture);
|
2012-09-08 20:10:41 +02:00
|
|
|
m_filter->addGesture(upLeftGesture);
|
|
|
|
m_filter->addGesture(upRightGesture);
|
2012-02-24 21:03:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MouseGestures::mousePress(QObject* obj, QMouseEvent* event)
|
|
|
|
{
|
|
|
|
m_view = qobject_cast<WebView*>(obj);
|
|
|
|
|
2014-04-05 17:04:41 +02:00
|
|
|
if (m_enableRockerNavigation && event->buttons() == (Qt::RightButton | Qt::LeftButton)) {
|
|
|
|
bool accepted = false;
|
|
|
|
|
|
|
|
if (event->button() == Qt::LeftButton && m_view.data()->history()->canGoBack()) {
|
|
|
|
m_view.data()->back();
|
|
|
|
accepted = true;
|
|
|
|
}
|
|
|
|
else if (event->button() == Qt::RightButton && m_view.data()->history()->canGoForward()) {
|
|
|
|
m_view.data()->forward();
|
|
|
|
accepted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (accepted) {
|
|
|
|
m_blockNextLeftMouseRelease = true;
|
|
|
|
m_blockNextRightMouseRelease = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-24 21:03:44 +01:00
|
|
|
m_filter->mouseButtonPressEvent(event);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MouseGestures::mouseRelease(QObject* obj, QMouseEvent* event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(obj)
|
|
|
|
|
2014-04-05 17:04:41 +02:00
|
|
|
if (m_blockNextRightMouseRelease && event->button() == Qt::RightButton) {
|
|
|
|
m_blockNextRightMouseRelease = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_blockNextLeftMouseRelease && event->button() == Qt::LeftButton) {
|
|
|
|
m_blockNextLeftMouseRelease = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-24 21:03:44 +01:00
|
|
|
return m_filter->mouseButtonReleaseEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MouseGestures::mouseMove(QObject* obj, QMouseEvent* event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(obj)
|
|
|
|
|
|
|
|
m_filter->mouseMoveEvent(event);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MouseGestures::showSettings(QWidget* parent)
|
|
|
|
{
|
2012-09-11 11:31:28 +02:00
|
|
|
if (!m_settings) {
|
2014-04-05 17:46:05 +02:00
|
|
|
m_settings = new MouseGesturesSettingsDialog(this, parent);
|
2012-09-11 11:31:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m_settings.data()->show();
|
|
|
|
m_settings.data()->raise();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MouseGestures::unloadPlugin()
|
|
|
|
{
|
|
|
|
delete m_settings.data();
|
2012-02-24 21:03:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MouseGestures::upGestured()
|
|
|
|
{
|
2012-03-26 17:47:05 +02:00
|
|
|
if (!m_view) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_view.data()->stop();
|
2012-02-24 21:03:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MouseGestures::downGestured()
|
|
|
|
{
|
2015-10-02 11:14:10 +02:00
|
|
|
TabbedWebView* view = qobject_cast<TabbedWebView*>(m_view.data());
|
|
|
|
if (!view)
|
2012-03-26 17:47:05 +02:00
|
|
|
return;
|
|
|
|
|
2015-10-02 11:14:10 +02:00
|
|
|
BrowserWindow* window = view->browserWindow();
|
|
|
|
if (!window)
|
|
|
|
return;
|
|
|
|
|
|
|
|
TabWidget* tabWidget = window->tabWidget();
|
2016-10-19 09:43:56 +02:00
|
|
|
tabWidget->addView(QUrl(), Qz::NT_SelectedNewEmptyTab, true);
|
|
|
|
tabWidget->setCurrentTabFresh(true);
|
|
|
|
|
|
|
|
if (window->isFullScreen())
|
|
|
|
window->showNavigationWithFullScreen();
|
2012-02-24 21:03:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MouseGestures::leftGestured()
|
|
|
|
{
|
2012-03-26 17:47:05 +02:00
|
|
|
if (!m_view) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-09 12:31:55 +02:00
|
|
|
if (QApplication::isRightToLeft()) {
|
|
|
|
m_view.data()->forward();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_view.data()->back();
|
|
|
|
}
|
2012-02-24 21:03:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MouseGestures::rightGestured()
|
|
|
|
{
|
2012-03-26 17:47:05 +02:00
|
|
|
if (!m_view) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-09 12:31:55 +02:00
|
|
|
if (QApplication::isRightToLeft()) {
|
|
|
|
m_view.data()->back();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_view.data()->forward();
|
|
|
|
}
|
2012-02-24 21:03:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MouseGestures::downRightGestured()
|
|
|
|
{
|
2015-10-12 12:04:48 +02:00
|
|
|
TabbedWebView *view = qobject_cast<TabbedWebView*>(m_view.data());
|
|
|
|
if (!view)
|
|
|
|
return;
|
|
|
|
|
|
|
|
BrowserWindow *window = view->browserWindow();
|
|
|
|
if (!window)
|
|
|
|
return;
|
|
|
|
|
|
|
|
TabWidget *tabWidget = window->tabWidget();
|
2012-03-26 17:47:05 +02:00
|
|
|
if (!m_view) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-12 12:04:48 +02:00
|
|
|
tabWidget->requestCloseTab(view->tabIndex());
|
2012-02-24 21:03:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MouseGestures::downLeftGestured()
|
|
|
|
{
|
2012-03-26 17:47:05 +02:00
|
|
|
if (!m_view) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_view.data()->load(mApp->getWindow()->homepageUrl());
|
2012-02-24 21:03:44 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 01:32:31 +01:00
|
|
|
void MouseGestures::downUpGestured()
|
|
|
|
{
|
|
|
|
TabbedWebView* view = qobject_cast<TabbedWebView*>(m_view.data());
|
|
|
|
if (!view)
|
|
|
|
return;
|
|
|
|
|
|
|
|
BrowserWindow* window = view->browserWindow();
|
|
|
|
if (!window)
|
|
|
|
return;
|
2015-10-02 11:14:10 +02:00
|
|
|
|
2014-11-21 00:47:40 +01:00
|
|
|
TabWidget* tabWidget = window->tabWidget();
|
|
|
|
tabWidget->duplicateTab(tabWidget->currentIndex());
|
2014-11-20 01:32:31 +01:00
|
|
|
}
|
|
|
|
|
2012-02-24 21:03:44 +01:00
|
|
|
void MouseGestures::upDownGestured()
|
|
|
|
{
|
2012-03-26 17:47:05 +02:00
|
|
|
if (!m_view) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_view.data()->reload();
|
2012-02-24 21:03:44 +01:00
|
|
|
}
|
|
|
|
|
2012-09-08 20:10:41 +02:00
|
|
|
void MouseGestures::upLeftGestured()
|
|
|
|
{
|
|
|
|
TabbedWebView* view = qobject_cast<TabbedWebView*>(m_view.data());
|
2014-04-11 22:23:42 +02:00
|
|
|
if (!view)
|
2012-09-08 20:10:41 +02:00
|
|
|
return;
|
|
|
|
|
2014-04-11 22:23:42 +02:00
|
|
|
BrowserWindow* window = view->browserWindow();
|
|
|
|
if (!window)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (QApplication::isRightToLeft())
|
|
|
|
window->tabWidget()->nextTab();
|
|
|
|
else
|
|
|
|
window->tabWidget()->previousTab();
|
2012-09-08 20:10:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MouseGestures::upRightGestured()
|
|
|
|
{
|
|
|
|
TabbedWebView* view = qobject_cast<TabbedWebView*>(m_view.data());
|
2014-04-11 22:23:42 +02:00
|
|
|
if (!view)
|
2012-09-08 20:10:41 +02:00
|
|
|
return;
|
|
|
|
|
2014-04-11 22:23:42 +02:00
|
|
|
BrowserWindow* window = view->browserWindow();
|
|
|
|
if (!window)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (QApplication::isRightToLeft())
|
|
|
|
window->tabWidget()->previousTab();
|
|
|
|
else
|
|
|
|
window->tabWidget()->nextTab();
|
2012-09-08 20:10:41 +02:00
|
|
|
}
|
|
|
|
|
2014-04-05 17:46:05 +02:00
|
|
|
void MouseGestures::init()
|
|
|
|
{
|
|
|
|
initFilter();
|
|
|
|
|
|
|
|
// We need to override right mouse button events
|
2017-01-22 09:45:24 +01:00
|
|
|
m_oldWebViewForceContextMenuOnRelease = WebView::forceContextMenuOnMouseRelease();
|
2014-04-05 17:46:05 +02:00
|
|
|
WebView::setForceContextMenuOnMouseRelease(m_button == Qt::RightButton || m_enableRockerNavigation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MouseGestures::setGestureButton(Qt::MouseButton button)
|
|
|
|
{
|
|
|
|
m_button = button;
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MouseGestures::setGestureButtonByIndex(int index)
|
|
|
|
{
|
|
|
|
switch (index) {
|
|
|
|
case 0:
|
|
|
|
m_button = Qt::MiddleButton;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
m_button = Qt::RightButton;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
m_button = Qt::NoButton;
|
|
|
|
}
|
|
|
|
|
|
|
|
setGestureButton(m_button);
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::MouseButton MouseGestures::gestureButton() const
|
|
|
|
{
|
|
|
|
return m_button;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MouseGestures::buttonToIndex() const
|
|
|
|
{
|
|
|
|
switch (m_button) {
|
|
|
|
case Qt::MiddleButton:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case Qt::RightButton:
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MouseGestures::rockerNavigationEnabled() const
|
|
|
|
{
|
|
|
|
return m_enableRockerNavigation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MouseGestures::setRockerNavigationEnabled(bool enable)
|
|
|
|
{
|
|
|
|
m_enableRockerNavigation = enable;
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MouseGestures::loadSettings()
|
|
|
|
{
|
|
|
|
QSettings settings(m_settingsFile, QSettings::IniFormat);
|
|
|
|
|
|
|
|
settings.beginGroup("MouseGestures");
|
|
|
|
setGestureButtonByIndex(settings.value("Button", 0).toInt());
|
|
|
|
m_enableRockerNavigation = settings.value("RockerNavigation", true).toBool();
|
|
|
|
settings.endGroup();
|
|
|
|
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MouseGestures::saveSettings()
|
|
|
|
{
|
|
|
|
QSettings settings(m_settingsFile, QSettings::IniFormat);
|
|
|
|
|
|
|
|
settings.beginGroup("MouseGestures");
|
|
|
|
settings.setValue("Button", buttonToIndex());
|
|
|
|
settings.setValue("RockerNavigation", m_enableRockerNavigation);
|
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
|
2012-02-24 21:03:44 +01:00
|
|
|
MouseGestures::~MouseGestures()
|
|
|
|
{
|
|
|
|
m_filter->clearGestures(true);
|
|
|
|
delete m_filter;
|
2017-01-22 09:45:24 +01:00
|
|
|
|
|
|
|
// Restore original value
|
|
|
|
WebView::setForceContextMenuOnMouseRelease(m_oldWebViewForceContextMenuOnRelease);
|
2012-02-24 21:03:44 +01:00
|
|
|
}
|