diff options
author | Roopesh Chander <roop@forwardbias.in> | 2009-10-09 12:40:25 +0530 |
---|---|---|
committer | Roopesh Chander <roop@forwardbias.in> | 2009-10-09 12:40:25 +0530 |
commit | 4913142da7bebab7c083aa9540a980d4f0e75c10 (patch) | |
tree | 8ed6c489f74f909fd03f4bc8d8eeb04169d09280 | |
parent | 65c42f7af818f79623402818e6540bf10629d096 (diff) |
try highlighting the web element on hover
-rw-r--r-- | WebScraps.pro | 24 | ||||
-rw-r--r-- | mainwindow.cpp | 7 | ||||
-rw-r--r-- | webview.cpp | 41 | ||||
-rw-r--r-- | webview.h | 19 |
4 files changed, 72 insertions, 19 deletions
diff --git a/WebScraps.pro b/WebScraps.pro index 0cf9d5a..695dcdf 100644 --- a/WebScraps.pro +++ b/WebScraps.pro @@ -1,18 +1,12 @@ -#------------------------------------------------- -# +# ------------------------------------------------- # Project created by QtCreator 2009-10-09T09:55:02 -# -#------------------------------------------------- - -QT += webkit - +# ------------------------------------------------- +QT += webkit TARGET = WebScraps TEMPLATE = app - - -SOURCES += main.cpp\ - mainwindow.cpp - -HEADERS += mainwindow.h - -FORMS += mainwindow.ui +SOURCES += main.cpp \ + mainwindow.cpp \ + webview.cpp +HEADERS += mainwindow.h \ + webview.h +FORMS += mainwindow.ui diff --git a/mainwindow.cpp b/mainwindow.cpp index 041a517..1f995e0 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,15 +1,14 @@ -#include <QWebView> - #include "mainwindow.h" #include "ui_mainwindow.h" +#include "webview.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); - m_webView = new QWebView(this); - m_webView->load(QUrl("http://www.forwardbias.in/")); + m_webView = new WebView(this); + m_webView->load(QUrl("http://www.dilbert.com/")); connect(m_webView, SIGNAL(loadFinished(bool)), SLOT(adjustLocation())); connect(m_webView, SIGNAL(titleChanged(const QString&)), SLOT(adjustTitle())); diff --git a/webview.cpp b/webview.cpp new file mode 100644 index 0000000..4d10a4b --- /dev/null +++ b/webview.cpp @@ -0,0 +1,41 @@ +#include "webview.h" +#include <QMouseEvent> +#include <QWebFrame> +#include <QWebHitTestResult> +#include <QWebElement> +#include <QDebug> + +WebView::WebView(QWidget * parent) + : QWebView(parent) + , m_scrapRect(QRect()) +{ + setMouseTracking(true); +} + +void WebView::mouseMoveEvent(QMouseEvent* event) +{ + QWebFrame* hitFrame = page()->frameAt(event->pos()); + QWebHitTestResult hitTestResult = hitFrame->hitTestContent(event->pos()); + QRect hitRect = hitTestResult.boundingRect().translated(-hitFrame->scrollPosition()); + if (hitRect.contains(event->pos())) { + m_scrapRect = hitRect; + } else { + m_scrapRect = QRect(); + } + QWebView::mousePressEvent(event); + update(); +} + +void WebView::paintEvent(QPaintEvent *event) +{ + QWebView::paintEvent(event); + + if (m_scrapRect.isValid()) { + QPainter painter(this); + painter.setPen(QPen(QBrush(Qt::magenta), 1)); + painter.setBrush(QBrush(Qt::blue)); + painter.setOpacity(0.4); + painter.drawRect(m_scrapRect); + painter.end(); + } +} diff --git a/webview.h b/webview.h new file mode 100644 index 0000000..467e917 --- /dev/null +++ b/webview.h @@ -0,0 +1,19 @@ +#ifndef WEBVIEW_H +#define WEBVIEW_H + +#include <QWebView> + +class WebView : public QWebView +{ +public: + WebView(QWidget * parent = 0); + +protected: + void mouseMoveEvent(QMouseEvent* event); + void paintEvent(QPaintEvent* event); + +private: + QRect m_scrapRect; +}; + +#endif // WEBVIEW_H |