2013-07-14 08:04:24 +02:00
|
|
|
#include <QStyleOptionFrameV3>
|
2013-07-21 11:26:57 +02:00
|
|
|
#include <QAction>
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QWebFrame>
|
|
|
|
#include <QContextMenuEvent>
|
2013-07-14 08:04:24 +02:00
|
|
|
|
2013-07-24 19:06:09 +02:00
|
|
|
#include "core/defs.h"
|
|
|
|
#include "core/settings.h"
|
2013-07-20 20:30:27 +02:00
|
|
|
#include "core/basewebpage.h"
|
2013-07-08 20:58:24 +02:00
|
|
|
#include "gui/basewebview.h"
|
2013-08-05 21:31:41 +02:00
|
|
|
#include "gui/iconthemefactory.h"
|
2013-07-14 08:04:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
BaseWebView::BaseWebView(QWidget *parent)
|
|
|
|
: QWebView(parent), m_page(new BaseWebPage(this)) {
|
|
|
|
setPage(m_page);
|
2013-08-30 20:26:39 +02:00
|
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
2013-07-21 21:02:34 +02:00
|
|
|
initializeActions();
|
2013-07-20 20:30:27 +02:00
|
|
|
createConnections();
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseWebView::~BaseWebView() {
|
2013-07-21 21:02:34 +02:00
|
|
|
qDebug("Destroying BaseWebView.");
|
2013-07-20 20:30:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BaseWebView::onLoadFinished(bool ok) {
|
|
|
|
// If page was not loaded, then display custom error page.
|
|
|
|
if (!ok) {
|
|
|
|
displayErrorPage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-30 17:40:17 +02:00
|
|
|
void BaseWebView::openLinkInNewTab() {
|
|
|
|
emit linkMiddleClicked(m_contextLinkUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseWebView::openImageInNewTab() {
|
|
|
|
emit linkMiddleClicked(m_contextImageUrl);
|
|
|
|
}
|
|
|
|
|
2013-07-20 20:30:27 +02:00
|
|
|
void BaseWebView::createConnections() {
|
2013-08-30 17:40:17 +02:00
|
|
|
connect(this, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
|
|
|
|
connect(this, SIGNAL(customContextMenuRequested(QPoint)),
|
|
|
|
this, SLOT(popupContextMenu(QPoint)));
|
|
|
|
|
|
|
|
connect(m_actionOpenLinkNewTab,SIGNAL(triggered()), this, SLOT(openLinkInNewTab()));
|
|
|
|
connect(m_actionOpenImageNewTab, SIGNAL(triggered()),
|
|
|
|
this, SLOT(openImageInNewTab()));
|
2013-07-20 20:30:27 +02:00
|
|
|
}
|
|
|
|
|
2013-07-21 21:02:34 +02:00
|
|
|
void BaseWebView::setupIcons() {
|
2013-08-05 21:31:41 +02:00
|
|
|
m_actionReload->setIcon(IconThemeFactory::getInstance()->fromTheme("view-refresh"));
|
|
|
|
m_actionCopyLink->setIcon(IconThemeFactory::getInstance()->fromTheme("edit-copy"));
|
|
|
|
m_actionCopyImage->setIcon(IconThemeFactory::getInstance()->fromTheme("insert-image"));
|
|
|
|
m_actionCopyImageUrl->setIcon(IconThemeFactory::getInstance()->fromTheme("edit-copy"));
|
2013-08-30 17:40:17 +02:00
|
|
|
m_actionOpenLinkThisTab->setIcon(IconThemeFactory::getInstance()->fromTheme("text-html"));
|
|
|
|
m_actionOpenLinkNewTab->setIcon(IconThemeFactory::getInstance()->fromTheme("text-html"));
|
|
|
|
m_actionOpenImageNewTab->setIcon(IconThemeFactory::getInstance()->fromTheme("insert-image"));
|
2013-07-21 21:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BaseWebView::initializeActions() {
|
|
|
|
// Create needed actions.
|
|
|
|
m_actionReload = pageAction(QWebPage::Reload);
|
|
|
|
m_actionReload->setParent(this);
|
|
|
|
m_actionReload->setText(tr("Reload web page"));
|
|
|
|
m_actionReload->setToolTip(tr("Reload current web page"));
|
|
|
|
|
|
|
|
m_actionCopyLink = pageAction(QWebPage::CopyLinkToClipboard);
|
|
|
|
m_actionCopyLink->setParent(this);
|
|
|
|
m_actionCopyLink->setText(tr("Copy link url"));
|
|
|
|
m_actionCopyLink->setToolTip(tr("Copy link url to clipboard"));
|
|
|
|
|
|
|
|
m_actionCopyImage = pageAction(QWebPage::CopyImageToClipboard);
|
|
|
|
m_actionCopyImage->setParent(this);
|
|
|
|
m_actionCopyImage->setText(tr("Copy image"));
|
|
|
|
m_actionCopyImage->setToolTip(tr("Copy image to clipboard"));
|
|
|
|
|
|
|
|
m_actionCopyImageUrl = pageAction(QWebPage::CopyImageUrlToClipboard);
|
|
|
|
m_actionCopyImageUrl->setParent(this);
|
|
|
|
m_actionCopyImageUrl->setText(tr("Copy image url"));
|
|
|
|
m_actionCopyImageUrl->setToolTip(tr("Copy image url to clipboard"));
|
2013-08-05 21:31:41 +02:00
|
|
|
|
|
|
|
m_actionOpenLinkNewTab = pageAction(QWebPage::OpenLinkInNewWindow);
|
|
|
|
m_actionOpenLinkNewTab->setParent(this);
|
|
|
|
m_actionOpenLinkNewTab->setText(tr("Open link in new tab"));
|
|
|
|
m_actionOpenLinkNewTab->setToolTip(tr("Open this hyperlink in new tab"));
|
2013-08-30 17:40:17 +02:00
|
|
|
|
|
|
|
m_actionOpenLinkThisTab = pageAction(QWebPage::OpenLink);
|
|
|
|
m_actionOpenLinkThisTab->setParent(this);
|
|
|
|
m_actionOpenLinkThisTab->setText(tr("Follow link"));
|
|
|
|
m_actionOpenLinkThisTab->setToolTip(tr("Open the hyperlink in this tab"));
|
|
|
|
|
|
|
|
m_actionOpenImageNewTab = pageAction(QWebPage::OpenImageInNewWindow);
|
|
|
|
m_actionOpenImageNewTab->setParent(this);
|
|
|
|
m_actionOpenImageNewTab->setText(tr("Open image in new tab"));
|
|
|
|
m_actionOpenImageNewTab->setToolTip(tr("Open this image in this tab"));
|
2013-07-21 21:02:34 +02:00
|
|
|
}
|
|
|
|
|
2013-07-20 20:30:27 +02:00
|
|
|
void BaseWebView::displayErrorPage() {
|
|
|
|
// TODO: Add better custom error page.
|
|
|
|
setHtml("error", url());
|
2013-07-14 08:04:24 +02:00
|
|
|
}
|
2013-07-08 20:58:24 +02:00
|
|
|
|
2013-07-24 19:06:09 +02:00
|
|
|
void BaseWebView::popupContextMenu(const QPoint &pos) {
|
2013-07-21 11:26:57 +02:00
|
|
|
QMenu context_menu(tr("Web browser"), this);
|
|
|
|
QMenu image_submenu(tr("Image"), &context_menu);
|
2013-08-30 17:40:17 +02:00
|
|
|
QMenu link_submenu(tr("Hyperlink"), this);
|
2013-07-24 19:06:09 +02:00
|
|
|
QWebHitTestResult hit_result = page()->mainFrame()->hitTestContent(pos);
|
2013-07-21 11:26:57 +02:00
|
|
|
|
2013-08-05 21:31:41 +02:00
|
|
|
image_submenu.setIcon(IconThemeFactory::getInstance()->fromTheme("image-x-generic"));
|
2013-08-30 17:40:17 +02:00
|
|
|
link_submenu.setIcon(IconThemeFactory::getInstance()->fromTheme("text-html"));
|
2013-07-21 21:02:34 +02:00
|
|
|
|
|
|
|
// Assemble the menu from actions.
|
|
|
|
context_menu.addAction(m_actionReload);
|
2013-07-21 11:26:57 +02:00
|
|
|
|
2013-08-30 17:40:17 +02:00
|
|
|
QUrl hit_url = hit_result.linkUrl();
|
|
|
|
QUrl hit_image_url = hit_result.imageUrl();
|
|
|
|
|
|
|
|
if (hit_url.isValid()) {
|
|
|
|
m_contextLinkUrl = hit_url;
|
|
|
|
|
|
|
|
context_menu.addMenu(&link_submenu);
|
|
|
|
link_submenu.addAction(m_actionOpenLinkThisTab);
|
|
|
|
link_submenu.addAction(m_actionOpenLinkNewTab);
|
|
|
|
link_submenu.addAction(m_actionCopyLink);
|
2013-07-21 11:26:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!hit_result.pixmap().isNull()) {
|
2013-07-21 21:02:34 +02:00
|
|
|
// Add 'Image' menu, because if user clicked image it needs to be visible.
|
2013-07-21 11:26:57 +02:00
|
|
|
context_menu.addMenu(&image_submenu);
|
|
|
|
|
2013-08-30 17:40:17 +02:00
|
|
|
if (hit_image_url.isValid()) {
|
|
|
|
m_contextImageUrl = hit_image_url;
|
|
|
|
image_submenu.addAction(m_actionOpenImageNewTab);
|
|
|
|
image_submenu.addAction(m_actionCopyImageUrl);
|
|
|
|
}
|
2013-07-21 21:02:34 +02:00
|
|
|
image_submenu.addAction(m_actionCopyImage);
|
2013-07-21 11:26:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Display the menu.
|
2013-07-24 19:06:09 +02:00
|
|
|
context_menu.exec(mapToGlobal(pos));
|
2013-07-21 11:26:57 +02:00
|
|
|
}
|
|
|
|
|
2013-07-23 20:42:06 +02:00
|
|
|
void BaseWebView::mousePressEvent(QMouseEvent *event) {
|
2013-08-30 20:26:39 +02:00
|
|
|
if (event->button() & Qt::LeftButton && event->modifiers() & Qt::ControlModifier) {
|
2013-07-23 20:42:06 +02:00
|
|
|
QWebHitTestResult hit_result = page()->mainFrame()->hitTestContent(event->pos());
|
|
|
|
|
|
|
|
// Check if user clicked with middle mouse button on some
|
|
|
|
// hyperlink.
|
2013-08-03 16:22:40 +02:00
|
|
|
QUrl link_url = hit_result.linkUrl();
|
|
|
|
QUrl image_url = hit_result.imageUrl();
|
|
|
|
|
|
|
|
if (link_url.isValid()) {
|
|
|
|
emit linkMiddleClicked(link_url);
|
2013-07-23 20:42:06 +02:00
|
|
|
// No more handling of event is now needed. Return.
|
|
|
|
return;
|
|
|
|
}
|
2013-08-03 16:22:40 +02:00
|
|
|
else if (image_url.isValid()) {
|
|
|
|
emit linkMiddleClicked(image_url);
|
|
|
|
return;
|
|
|
|
}
|
2013-07-23 20:42:06 +02:00
|
|
|
}
|
2013-08-30 20:26:39 +02:00
|
|
|
else if (event->button() & Qt::MiddleButton) {
|
2013-07-24 19:06:09 +02:00
|
|
|
m_gestureOrigin = event->pos();
|
2013-08-30 20:26:39 +02:00
|
|
|
return;
|
2013-07-24 19:06:09 +02:00
|
|
|
}
|
2013-07-23 20:42:06 +02:00
|
|
|
|
|
|
|
QWebView::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseWebView::mouseReleaseEvent(QMouseEvent *event) {
|
2013-08-30 20:26:39 +02:00
|
|
|
if (event->button() & Qt::MiddleButton) {
|
2013-07-24 19:06:09 +02:00
|
|
|
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();
|
2013-08-30 20:26:39 +02:00
|
|
|
int right_move = -left_move;
|
2013-07-24 19:06:09 +02:00
|
|
|
int top_move = m_gestureOrigin.y() - release_point.y();
|
2013-08-30 20:26:39 +02:00
|
|
|
int bottom_move = -top_move;
|
2013-07-24 19:06:09 +02:00
|
|
|
int total_max = qMax(qMax(qMax(left_move, right_move),
|
|
|
|
qMax(top_move, bottom_move)),
|
|
|
|
40);
|
|
|
|
|
2013-08-30 20:26:39 +02:00
|
|
|
if (total_max == left_move) {
|
2013-07-24 19:06:09 +02:00
|
|
|
back();
|
|
|
|
}
|
2013-08-30 20:26:39 +02:00
|
|
|
else if (total_max == right_move) {
|
2013-07-24 19:06:09 +02:00
|
|
|
forward();
|
|
|
|
}
|
2013-08-30 20:26:39 +02:00
|
|
|
else if (total_max == top_move) {
|
2013-07-24 19:06:09 +02:00
|
|
|
reload();
|
|
|
|
}
|
2013-08-30 20:26:39 +02:00
|
|
|
else if (total_max == bottom_move) {
|
2013-07-24 19:06:09 +02:00
|
|
|
emit newTabRequested();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-30 20:26:39 +02:00
|
|
|
|
|
|
|
QWebView::mouseReleaseEvent(event);
|
2013-07-23 20:42:06 +02:00
|
|
|
}
|
|
|
|
|
2013-07-14 08:04:24 +02:00
|
|
|
void BaseWebView::paintEvent(QPaintEvent *event) {
|
|
|
|
QWebView::paintEvent(event);
|
2013-07-08 20:58:24 +02:00
|
|
|
|
2013-07-17 20:42:42 +02:00
|
|
|
// Draw additional frame.
|
|
|
|
QPainter painter(this);
|
|
|
|
QStyleOptionFrameV3 style_option;
|
2013-07-20 20:30:27 +02:00
|
|
|
int frame_shape = QFrame::Sunken & QFrame::Shape_Mask;
|
2013-07-17 20:42:42 +02:00
|
|
|
|
|
|
|
style_option.init(this);
|
|
|
|
style_option.frameShape = QFrame::Shape(int(style_option.frameShape) |
|
|
|
|
QFrame::StyledPanel |
|
|
|
|
frame_shape);
|
|
|
|
style_option.rect = rect();
|
|
|
|
style_option.lineWidth = 1;
|
|
|
|
style_option.midLineWidth = 0;
|
|
|
|
|
|
|
|
style()->drawControl(QStyle::CE_ShapedFrame, &style_option, &painter, this);
|
2013-07-08 20:58:24 +02:00
|
|
|
}
|