From fcb257022bd57ad2dda16ff27fe5c40c1f67e9c9 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Wed, 24 Jul 2013 19:06:09 +0200 Subject: [PATCH] Added mouse gestures. --- src/core/defs.h.in | 1 + src/gui/basewebview.cpp | 58 +++++++++++++++++++++++++++++++----- src/gui/basewebview.h | 11 +++++-- src/gui/formabout.ui | 44 +++++++++++++++++---------- src/gui/formsettings.cpp | 44 +++++++++++++++++---------- src/gui/formsettings.h | 3 ++ src/gui/formsettings.ui | 38 +++++++++++++++++++++-- src/gui/locationlineedit.cpp | 4 +-- 8 files changed, 155 insertions(+), 48 deletions(-) diff --git a/src/core/defs.h.in b/src/core/defs.h.in index 58a15138c..7ae0b64f6 100644 --- a/src/core/defs.h.in +++ b/src/core/defs.h.in @@ -21,6 +21,7 @@ #define APP_CFG_GEN "main" #define APP_CFG_PROXY "proxy" #define APP_CFG_CUTS "keyboard" +#define APP_CFG_BROWSER "browser" #define APP_DB_PATH "data/storage/database.db" #define APP_PREFIX "@CMAKE_INSTALL_PREFIX@" diff --git a/src/gui/basewebview.cpp b/src/gui/basewebview.cpp index fbceeff09..47534c84a 100644 --- a/src/gui/basewebview.cpp +++ b/src/gui/basewebview.cpp @@ -4,6 +4,8 @@ #include #include +#include "core/defs.h" +#include "core/settings.h" #include "core/basewebpage.h" #include "gui/basewebview.h" #include "gui/themefactory.h" @@ -12,6 +14,7 @@ BaseWebView::BaseWebView(QWidget *parent) : QWebView(parent), m_page(new BaseWebPage(this)) { setPage(m_page); + setContextMenuPolicy(Qt::NoContextMenu); initializeActions(); createConnections(); } @@ -28,7 +31,10 @@ void BaseWebView::onLoadFinished(bool ok) { } void BaseWebView::createConnections() { - connect(this, &BaseWebView::loadFinished, this, &BaseWebView::onLoadFinished); + connect(this, &BaseWebView::loadFinished, + this, &BaseWebView::onLoadFinished); + connect(this, &BaseWebView::customContextMenuRequested, + this, &BaseWebView::popupContextMenu); } void BaseWebView::setupIcons() { @@ -68,10 +74,10 @@ void BaseWebView::displayErrorPage() { setHtml("error", url()); } -void BaseWebView::contextMenuEvent(QContextMenuEvent *event) { +void BaseWebView::popupContextMenu(const QPoint &pos) { QMenu context_menu(tr("Web browser"), this); QMenu image_submenu(tr("Image"), &context_menu); - QWebHitTestResult hit_result = page()->mainFrame()->hitTestContent(event->pos()); + QWebHitTestResult hit_result = page()->mainFrame()->hitTestContent(pos); image_submenu.setIcon(ThemeFactory::fromTheme("image-x-generic")); @@ -94,11 +100,11 @@ void BaseWebView::contextMenuEvent(QContextMenuEvent *event) { } // Display the menu. - context_menu.exec(mapToGlobal(event->pos())); + context_menu.exec(mapToGlobal(pos)); } void BaseWebView::mousePressEvent(QMouseEvent *event) { - if (event->buttons() & Qt::MiddleButton) { + if (event->button() & Qt::MiddleButton) { QWebHitTestResult hit_result = page()->mainFrame()->hitTestContent(event->pos()); // Check if user clicked with middle mouse button on some @@ -110,15 +116,51 @@ void BaseWebView::mousePressEvent(QMouseEvent *event) { return; } } - - + else if (event->button() & Qt::RightButton) { + m_gestureOrigin = event->pos(); + } // TODO: Add mouse gestures (from quite-rss). QWebView::mousePressEvent(event); } void BaseWebView::mouseReleaseEvent(QMouseEvent *event) { - QWebView::mousePressEvent(event); + QWebView::mouseReleaseEvent(event); + + if (event->button() & Qt::RightButton) { + bool are_gestures_enabled = Settings::getInstance()->value(APP_CFG_BROWSER, + "gestures_enabled", + true).toBool(); + if (are_gestures_enabled) { + QPoint release_point = event->pos(); + int left_move = m_gestureOrigin.x() - release_point.x(); + int right_move = release_point.x() - m_gestureOrigin.x(); + int top_move = m_gestureOrigin.y() - release_point.y(); + int bottom_move = release_point.y() - m_gestureOrigin.y(); + int total_max = qMax(qMax(qMax(left_move, right_move), + qMax(top_move, bottom_move)), + 40); + + if (total_max == left_move && are_gestures_enabled) { + back(); + } + else if (total_max == right_move && are_gestures_enabled) { + forward(); + } + else if (total_max == top_move && are_gestures_enabled) { + reload(); + } + else if (total_max == bottom_move && are_gestures_enabled) { + emit newTabRequested(); + } + else { + emit customContextMenuRequested(event->pos()); + } + } + else { + emit customContextMenuRequested(event->pos()); + } + } } void BaseWebView::paintEvent(QPaintEvent *event) { diff --git a/src/gui/basewebview.h b/src/gui/basewebview.h index 64173d3db..74fcdb84c 100644 --- a/src/gui/basewebview.h +++ b/src/gui/basewebview.h @@ -23,10 +23,16 @@ class BaseWebView : public QWebView { // web browser tab. void linkMiddleClicked(const QUrl &link_url); + // User wants to open new empty web browser tab. + void newTabRequested(); + protected slots: // Executes if loading of any page is done. void onLoadFinished(bool ok); + // Provides custom context menu. + void popupContextMenu(const QPoint &pos); + protected: void initializeActions(); @@ -39,9 +45,6 @@ class BaseWebView : public QWebView { // Does additional painting. void paintEvent(QPaintEvent *event); - // Provides custom context menu. - void contextMenuEvent(QContextMenuEvent *event); - // Provides custom mouse actions. void mousePressEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event); @@ -53,6 +56,8 @@ class BaseWebView : public QWebView { QAction *m_actionCopyLink; QAction *m_actionCopyImage; QAction *m_actionCopyImageUrl; + + QPoint m_gestureOrigin; }; #endif // BASEWEBVIEW_H diff --git a/src/gui/formabout.ui b/src/gui/formabout.ui index 9efe75c49..a4a623c8a 100644 --- a/src/gui/formabout.ui +++ b/src/gui/formabout.ui @@ -107,7 +107,7 @@ - 0 + 1 @@ -135,8 +135,8 @@ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;"> -<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html> +</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;"> +<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif'; font-size:9pt;"><br /></p></body></html> false @@ -162,15 +162,15 @@ p, li { white-space: pre-wrap; } - 0 + 1 0 0 - 687 - 180 + 685 + 184 @@ -227,12 +227,15 @@ p, li { white-space: pre-wrap; } <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'DejaVu Sans Mono'; font-size:9pt; font-weight:400; font-style:normal;"> -<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif';"><br /></p></body></html> +</style></head><body style=" font-family:'DejaVu Sans Mono'; font-size:8.25pt; font-weight:400; font-style:normal;"> +<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif'; font-size:9pt;"><br /></p></body></html> Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse + + true + @@ -242,8 +245,8 @@ p, li { white-space: pre-wrap; } 0 0 - 687 - 180 + 685 + 184 @@ -294,12 +297,15 @@ p, li { white-space: pre-wrap; } <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'DejaVu Sans Mono'; font-size:9pt; font-weight:400; font-style:normal;"> -<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif';"><br /></p></body></html> +</style></head><body style=" font-family:'DejaVu Sans Mono'; font-size:8.25pt; font-weight:400; font-style:normal;"> +<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif'; font-size:9pt;"><br /></p></body></html> Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse + + true + @@ -332,8 +338,8 @@ p, li { white-space: pre-wrap; } <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;"> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html> +</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Sans Serif'; font-size:9pt;"><br /></span></p></body></html> 30 @@ -344,6 +350,9 @@ p, li { white-space: pre-wrap; } Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse + + true + @@ -365,8 +374,8 @@ p, li { white-space: pre-wrap; } <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;"> -<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html> +</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;"> +<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif'; font-size:9pt;"><br /></p></body></html> false @@ -374,6 +383,9 @@ p, li { white-space: pre-wrap; } Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse + + true + diff --git a/src/gui/formsettings.cpp b/src/gui/formsettings.cpp index 8f9404d8a..4bba0e7f6 100644 --- a/src/gui/formsettings.cpp +++ b/src/gui/formsettings.cpp @@ -51,6 +51,7 @@ FormSettings::FormSettings(QWidget *parent) : QDialog(parent), m_ui(new Ui::Form loadShortcuts(); loadInterface(); loadProxy(); + loadBrowser(); loadLanguage(); } @@ -82,6 +83,7 @@ void FormSettings::saveSettings() { saveShortcuts(); saveInterface(); saveProxy(); + saveBrowser(); saveLanguage(); Settings::getInstance()->checkSettings(); @@ -103,6 +105,32 @@ void FormSettings::onProxyTypeChanged(int index) { m_ui->m_lblProxyUsername->setEnabled(is_proxy_selected); } +void FormSettings::loadBrowser() { + // Load settings of web browser GUI. + m_initialSettings.m_webBrowserProgress = Settings::getInstance()->value(APP_CFG_BROWSER, + "browser_progress_color", + QColor(0, 255, 0, 100)).value(); + m_ui->m_checkBrowserProgressColor->setChecked(Settings::getInstance()->value(APP_CFG_BROWSER, + "browser_colored_progress_enabled", + true).toBool()); + m_ui->m_checkMouseGestures->setChecked(Settings::getInstance()->value(APP_CFG_BROWSER, + "gestures_enabled", + true).toBool()); +} + +void FormSettings::saveBrowser() { + // Save settings of GUI of web browser. + Settings::getInstance()->setValue(APP_CFG_BROWSER, + "browser_progress_color", + m_initialSettings.m_webBrowserProgress); + Settings::getInstance()->setValue(APP_CFG_BROWSER, + "browser_colored_progress_enabled", + m_ui->m_checkBrowserProgressColor->isChecked()); + Settings::getInstance()->setValue(APP_CFG_BROWSER, + "gestures_enabled", + m_ui->m_checkMouseGestures->isChecked()); +} + void FormSettings::loadProxy() { m_ui->m_cmbProxyType->addItem(tr("No proxy"), QNetworkProxy::NoProxy); m_ui->m_cmbProxyType->addItem(tr("Socks5"), QNetworkProxy::Socks5Proxy); @@ -260,14 +288,6 @@ void FormSettings::loadInterface() { m_ui->m_grpTray->setDisabled(true); } - // Load settings of web browser GUI. - m_initialSettings.m_webBrowserProgress = Settings::getInstance()->value(APP_CFG_GUI, - "browser_progress_color", - QColor(0, 255, 0, 100)).value(); - m_ui->m_checkBrowserProgressColor->setChecked(Settings::getInstance()->value(APP_CFG_GUI, - "browser_colored_progress_enabled", - true).toBool()); - // Load settings of icon theme. QString current_theme = ThemeFactory::getCurrentIconTheme(); @@ -315,14 +335,6 @@ void FormSettings::saveInterface() { } } - // Save settings of GUI of web browser. - Settings::getInstance()->setValue(APP_CFG_GUI, - "browser_progress_color", - m_initialSettings.m_webBrowserProgress); - Settings::getInstance()->setValue(APP_CFG_GUI, - "browser_colored_progress_enabled", - m_ui->m_checkBrowserProgressColor->isChecked()); - // Save selected icon theme. ThemeFactory::setCurrentIconTheme(m_ui->m_cmbIconTheme->itemData(m_ui->m_cmbIconTheme->currentIndex()).toString()); } diff --git a/src/gui/formsettings.h b/src/gui/formsettings.h index 12a149f8a..f03b1a4f0 100644 --- a/src/gui/formsettings.h +++ b/src/gui/formsettings.h @@ -39,6 +39,9 @@ class FormSettings : public QDialog { void loadShortcuts(); void saveShortcuts(); + void loadBrowser(); + void saveBrowser(); + void loadProxy(); void saveProxy(); void displayProxyPassword(int state); diff --git a/src/gui/formsettings.ui b/src/gui/formsettings.ui index 60e25903f..5aa5f850a 100644 --- a/src/gui/formsettings.ui +++ b/src/gui/formsettings.ui @@ -69,8 +69,8 @@ 0 0 - 506 - 367 + 100 + 30 @@ -314,7 +314,7 @@ - Mouse gestures work with left mouse button. Possible gestures are: + Mouse gestures work with right mouse button. Possible gestures are: <ul> <li>previous web page (drag mouse left)</li> <li>next web page (drag mouse right)</li> @@ -624,5 +624,37 @@ + + m_checkBrowserProgressColor + toggled(bool) + m_lblBrowserProgressColor + setEnabled(bool) + + + 300 + 48 + + + 275 + 73 + + + + + m_checkBrowserProgressColor + toggled(bool) + m_btnBrowserProgressColor + setEnabled(bool) + + + 300 + 48 + + + 544 + 74 + + + diff --git a/src/gui/locationlineedit.cpp b/src/gui/locationlineedit.cpp index 8c7d5ba69..7bc7c1f23 100644 --- a/src/gui/locationlineedit.cpp +++ b/src/gui/locationlineedit.cpp @@ -50,11 +50,11 @@ void LocationLineEdit::mousePressEvent(QMouseEvent *event) { void LocationLineEdit::paintEvent(QPaintEvent *event) { // Draw "progress bar" if needed. - if (m_progress > 0 && Settings::getInstance()->value(APP_CFG_GUI, + if (m_progress > 0 && Settings::getInstance()->value(APP_CFG_BROWSER, "browser_colored_progress_enabled", true).toBool()) { QPalette current_palette = palette(); - QColor loadingColor = Settings::getInstance()->value(APP_CFG_GUI, + QColor loadingColor = Settings::getInstance()->value(APP_CFG_BROWSER, "browser_progress_color", QColor(0, 255, 0, 100)).value(); QLinearGradient gradient(0, 0, width(), 0);