summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAriya Hidayat <ariya.hidayat@nokia.com>2008-11-12 18:19:27 +0000
committerDavid Boddie <dboddie@trolltech.com>2008-11-12 18:19:27 +0000
commit0bbc0a3fe177aecc0d76d7773dfd73523b6f4b77 (patch)
treea865da39843ee3386e2419941e09e370578576ae
parent084af7bdc81222a58733e9b1ace917ff1e3f34f0 (diff)
Another dojo example: web view with panning support
-rw-r--r--panwebview/panwebview.cpp133
-rw-r--r--panwebview/panwebview.pro4
2 files changed, 137 insertions, 0 deletions
diff --git a/panwebview/panwebview.cpp b/panwebview/panwebview.cpp
new file mode 100644
index 0000000..2631133
--- /dev/null
+++ b/panwebview/panwebview.cpp
@@ -0,0 +1,133 @@
+/****************************************************************************
+**
+** Copyright (C) 2008-2008 Trolltech ASA. All rights reserved.
+**
+** This file is part of the Graphics Dojo project on Trolltech Labs.
+**
+** This file may be used under the terms of the GNU General Public
+** License version 2.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of
+** this file. Please review the following information to ensure GNU
+** General Public Licensing requirements will be met:
+** http://www.trolltech.com/products/qt/opensource.html
+**
+** If you are unsure which license is appropriate for your use, please
+** review the following information:
+** http://www.trolltech.com/products/qt/licensing.html or contact the
+** sales department at sales@trolltech.com.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+****************************************************************************/
+
+#include <QtGui>
+#include <QtWebKit>
+
+class PanWebView : public QWebView
+{
+ Q_OBJECT
+
+private:
+ bool pressed;
+ bool scrolling;
+ QPoint position;
+ QPoint offset;
+ QList<QEvent*> ignored;
+
+public:
+ PanWebView(QWidget *parent = 0): QWebView(parent), pressed(false), scrolling(false) {
+ QWebFrame *frame = page()->mainFrame();
+ frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
+ frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
+ }
+
+protected:
+
+ void mousePressEvent(QMouseEvent *mouseEvent) {
+
+ if (ignored.removeAll(mouseEvent))
+ return QWebView::mousePressEvent(mouseEvent);
+
+ if (!pressed && !scrolling && mouseEvent->modifiers() == Qt::NoModifier)
+ if (mouseEvent->buttons() == Qt::LeftButton) {
+ pressed = true;
+ scrolling = false;
+ position = mouseEvent->pos();
+ QWebFrame *frame = page()->mainFrame();
+ int x = frame->evaluateJavaScript("window.scrollX").toInt();
+ int y = frame->evaluateJavaScript("window.scrollY").toInt();
+ offset = QPoint(x, y);
+ QApplication::setOverrideCursor(Qt::OpenHandCursor);
+ return;
+ }
+
+ return QWebView::mousePressEvent(mouseEvent);
+ }
+
+ void mouseReleaseEvent(QMouseEvent *mouseEvent) {
+
+ if (ignored.removeAll(mouseEvent))
+ return QWebView::mouseReleaseEvent(mouseEvent);
+
+ if (scrolling) {
+ pressed = false;
+ scrolling = false;
+ QApplication::restoreOverrideCursor();
+ return;
+ }
+
+ if (pressed) {
+ pressed = false;
+ scrolling = false;
+
+ QMouseEvent *event1 = new QMouseEvent(QEvent::MouseButtonPress,
+ position, Qt::LeftButton,
+ Qt::LeftButton, Qt::NoModifier);
+ QMouseEvent *event2 = new QMouseEvent(*mouseEvent);
+
+ ignored << event1;
+ ignored << event2;
+ QApplication::postEvent(this, event1);
+ QApplication::postEvent(this, event2);
+ QApplication::restoreOverrideCursor();
+ return;
+ }
+
+ return QWebView::mouseReleaseEvent(mouseEvent);
+ }
+
+ void mouseMoveEvent(QMouseEvent *mouseEvent) {
+
+ if (scrolling) {
+ QPoint delta = mouseEvent->pos() - position;
+ QPoint p = offset - delta;
+ QWebFrame *frame = page()->mainFrame();
+ frame->evaluateJavaScript(QString("window.scrollTo(%1,%2);").arg(p.x()).arg(p.y()));
+ return;
+ }
+
+ if (pressed) {
+ pressed = false;
+ scrolling = true;
+ return;
+ }
+
+ return QWebView::mouseMoveEvent(mouseEvent);
+ }
+
+};
+
+#include "panwebview.moc"
+
+int main(int argc, char *argv[])
+{
+ QApplication app(argc, argv);
+
+ PanWebView web;
+ web.setUrl(QUrl("http://news.google.com"));
+ web.setWindowTitle("Web View - use mouse to drag and pan around");
+ web.show();
+
+ return app.exec();
+}
diff --git a/panwebview/panwebview.pro b/panwebview/panwebview.pro
new file mode 100644
index 0000000..0ec055e
--- /dev/null
+++ b/panwebview/panwebview.pro
@@ -0,0 +1,4 @@
+TEMPLATE = app
+TARGET = panwebview
+SOURCES = panwebview.cpp
+QT += network webkit