Commit 28843faa authored by Keith Kyzivat's avatar Keith Kyzivat Committed by Alessandro Portale
Browse files

Split out touch navigation into separate files.

Make the html5applicationviewer class much less cluttered by splitting out all
the touch navigation classes into separate files in a subdirectory.

This should make it much more maintainable.
parent bcd8740a
......@@ -8,3 +8,5 @@ QT += webkit
SOURCES += $$PWD/html5applicationviewer.cpp
HEADERS += $$PWD/html5applicationviewer.h
INCLUDEPATH += $$PWD
include(touchnavigation/touchnavigation.pri)
/*
This file was generated by the Html5 Application wizard of Qt Creator.
Html5ApplicationViewer is a convenience class containing mobile device specific
code such as screen orientation handling.
It is recommended not to modify this file, since newer versions of Qt Creator
may offer an updated version of it.
*/
#include <QGraphicsWebView>
#include <QWebPage>
#include "navigationcontroller.h"
#include "webnavigation.h"
#include "webtouchnavigation.h"
class NavigationControllerPrivate
{
public:
NavigationControllerPrivate(QWidget *parent, QGraphicsWebView *webView);
~NavigationControllerPrivate();
QWebPage *m_webPage;
QWidget *m_webWidget;
QGraphicsWebView *m_graphicsWebView;
WebNavigation *m_webNavigation;
};
NavigationControllerPrivate::NavigationControllerPrivate(QWidget *parent, QGraphicsWebView *webView)
: m_webPage(0)
, m_webWidget(0)
, m_graphicsWebView(webView)
, m_webNavigation(0)
{
Q_UNUSED(parent);
m_graphicsWebView->setAcceptTouchEvents(true);
m_webPage = new QWebPage;
m_graphicsWebView->setPage(m_webPage);
m_webNavigation = new WebNavigation(m_graphicsWebView, m_webPage);
m_webNavigation->setParent(m_graphicsWebView);
}
NavigationControllerPrivate::~NavigationControllerPrivate()
{
if (m_webNavigation)
delete m_webNavigation;
if (m_webPage)
delete m_webPage;
if (m_graphicsWebView)
delete m_graphicsWebView;
}
NavigationController::NavigationController(QWidget *parent, QGraphicsWebView *webView)
: m_d(new NavigationControllerPrivate(parent, webView))
{
}
NavigationController::~NavigationController()
{
delete m_d;
}
QWebPage* NavigationController::webPage() const
{
return m_d->m_webPage;
}
QGraphicsWebView* NavigationController::graphicsWebView() const
{
return m_d->m_graphicsWebView;
}
/*
This file was generated by the Html5 Application wizard of Qt Creator.
Html5ApplicationViewer is a convenience class containing mobile device specific
code such as screen orientation handling.
It is recommended not to modify this file, since newer versions of Qt Creator
may offer an updated version of it.
*/
#ifndef NAVIGATIONCONTROLLER_H
#define NAVIGATIONCONTROLLER_H
#include <QObject>
class QGraphicsWebView;
class QWebPage;
class NavigationController : public QObject
{
Q_OBJECT
public:
NavigationController(QWidget *parent, QGraphicsWebView *webView);
virtual ~NavigationController();
QWidget *webWidget() const;
QWebPage* webPage() const;
QGraphicsWebView* graphicsWebView() const;
signals:
void pauseNavigation();
void resumeNavigation();
private:
class NavigationControllerPrivate *m_d;
};
#endif // NAVIGATIONCONTROLLER_H
# This file was generated by the Html5 Application wizard of Qt Creator.
# The code below adds the Html5ApplicationViewer to the project.
# It is recommended not to modify this file, since newer versions of Qt Creator
# may offer an updated version of it.
SOURCES += \
$$PWD/webtouchphysicsinterface.cpp \
$$PWD/webtouchphysics.cpp \
$$PWD/webtouchevent.cpp \
$$PWD/webtouchscroller.cpp \
$$PWD/webtouchnavigation.cpp \
$$PWD/navigationcontroller.cpp \
$$PWD/webnavigation.cpp
HEADERS += \
$$PWD/webtouchphysicsinterface.h \
$$PWD/webtouchphysics.h \
$$PWD/webtouchevent.h \
$$PWD/webtouchscroller.h \
$$PWD/webtouchnavigation.h \
$$PWD/navigationcontroller.h \
$$PWD/webnavigation.h
INCLUDEPATH += $$PWD
/*
This file was generated by the Html5 Application wizard of Qt Creator.
Html5ApplicationViewer is a convenience class containing mobile device specific
code such as screen orientation handling.
It is recommended not to modify this file, since newer versions of Qt Creator
may offer an updated version of it.
*/
#include <QWebPage>
#include "webnavigation.h"
#include "webtouchnavigation.h"
WebNavigation::WebNavigation(QObject *parent, QWebPage *webPage)
: m_viewObject(parent)
, m_webPage(webPage)
, m_webTouchNavigation(0)
{
m_webTouchNavigation = new WebTouchNavigation(m_viewObject, m_webPage);
m_viewObject->installEventFilter(m_webTouchNavigation);
}
WebNavigation::~WebNavigation()
{
delete m_webTouchNavigation;
}
/*
This file was generated by the Html5 Application wizard of Qt Creator.
Html5ApplicationViewer is a convenience class containing mobile device specific
code such as screen orientation handling.
It is recommended not to modify this file, since newer versions of Qt Creator
may offer an updated version of it.
*/
#ifndef WEBNAVIGATION_H
#define WEBNAVIGATION_H
#include <QObject>
class QWebPage;
class WebTouchNavigation;
class WebNavigation : public QObject
{
Q_OBJECT
public:
WebNavigation(QObject *parent, QWebPage *webPage);
virtual ~WebNavigation();
private:
QObject *m_viewObject;
QWebPage *m_webPage;
WebTouchNavigation *m_webTouchNavigation;
};
#endif // WEBNAVIGATION_H
/*
This file was generated by the Html5 Application wizard of Qt Creator.
Html5ApplicationViewer is a convenience class containing mobile device specific
code such as screen orientation handling.
It is recommended not to modify this file, since newer versions of Qt Creator
may offer an updated version of it.
*/
#include "webtouchevent.h"
WebTouchEvent::WebTouchEvent()
: m_type(QEvent::None)
, m_button(Qt::NoButton)
, m_buttons(Qt::NoButton)
, m_graphicsSceneEvent(false)
, m_fired(false)
, m_editable(false)
, m_modifier(Qt::NoModifier)
{}
WebTouchEvent::WebTouchEvent(const QMouseEvent *mouseEvent)
{
Q_ASSERT(mouseEvent != 0);
m_type = mouseEvent->type();
m_pos = mouseEvent->pos();
m_button = mouseEvent->button();
m_buttons = mouseEvent->buttons();
m_modifier = mouseEvent->modifiers();
m_fired = false;
m_editable = false;
m_graphicsSceneEvent = false;
}
WebTouchEvent::WebTouchEvent(const QGraphicsSceneMouseEvent *graphicsSceneMouseEvent)
{
Q_ASSERT(graphicsSceneMouseEvent != 0);
m_type = graphicsSceneMouseEvent->type();
m_pos = graphicsSceneMouseEvent->pos().toPoint();
m_button = graphicsSceneMouseEvent->button();
m_buttons = graphicsSceneMouseEvent->buttons();
m_modifier = graphicsSceneMouseEvent->modifiers();
m_scenePos = graphicsSceneMouseEvent->scenePos();
m_screenPos = graphicsSceneMouseEvent->screenPos();
m_buttonDownPos = graphicsSceneMouseEvent->buttonDownPos(graphicsSceneMouseEvent->button());
m_buttonDownScenePos = graphicsSceneMouseEvent->buttonDownScenePos(graphicsSceneMouseEvent->button());
m_buttonDownScreenPos = graphicsSceneMouseEvent->buttonDownScreenPos(graphicsSceneMouseEvent->button());
m_lastPos = graphicsSceneMouseEvent->lastPos();
m_lastScenePos = graphicsSceneMouseEvent->lastScenePos();
m_lastScreenPos = graphicsSceneMouseEvent->lastScreenPos();
m_fired = false;
m_editable = false;
m_graphicsSceneEvent = true;
}
/*
This file was generated by the Html5 Application wizard of Qt Creator.
Html5ApplicationViewer is a convenience class containing mobile device specific
code such as screen orientation handling.
It is recommended not to modify this file, since newer versions of Qt Creator
may offer an updated version of it.
*/
#ifndef WEBTOUCHEVENT_H
#define WEBTOUCHEVENT_H
#include <QEvent>
#include <QGraphicsSceneMouseEvent>
#include <QMouseEvent>
#include <QPoint>
class WebTouchEvent
{
public:
WebTouchEvent();
WebTouchEvent(const QMouseEvent *mouseEvent);
WebTouchEvent(const QGraphicsSceneMouseEvent *graphicsSceneMouseEvent);
QPoint m_pos;
QEvent::Type m_type;
Qt::MouseButton m_button;
Qt::MouseButtons m_buttons;
bool m_graphicsSceneEvent;
bool m_fired;
bool m_editable;
Qt::KeyboardModifiers m_modifier;
//Graphics scene event members
QPointF m_scenePos;
QPoint m_screenPos;
QPointF m_buttonDownPos;
QPointF m_buttonDownScenePos;
QPoint m_buttonDownScreenPos;
QPointF m_lastPos;
QPointF m_lastScenePos;
QPoint m_lastScreenPos;
};
#endif // WEBTOUCHEVENT_H
/*
This file was generated by the Html5 Application wizard of Qt Creator.
Html5ApplicationViewer is a convenience class containing mobile device specific
code such as screen orientation handling.
It is recommended not to modify this file, since newer versions of Qt Creator
may offer an updated version of it.
*/
#include <QCoreApplication>
#include <QPoint>
#include <QWidget>
#include <QWebPage>
#include <QWebFrame>
#include <QWebElement>
#include "webtouchnavigation.h"
int xInRect(const QRect &r, int x)
{
int x1 = qMin(x, r.right()-2);
return qMax(x1, r.left()+2);
}
int yInRect(const QRect &r, int y)
{
int y1 = qMin(y, r.bottom()-2);
return qMax(y1, r.top()+2);
}
int wtnFastDistance(const QPoint &p, const QPoint &q)
{
return (p.x() - q.x()) * (p.x() - q.x()) + (p.y() - q.y()) * (p.y() - q.y());
}
QPoint frameViewPosition(QWebFrame *frame)
{
QPoint fp;
QWebFrame* f = frame;
while (1) {
fp += f->pos();
f = f->parentFrame();
if (f) {
fp -= f->scrollPosition();
} else
break;
}
return fp;
}
QPoint closestElement(QObject *viewObject, QWebFrame *frame, WebTouchEvent &touchEvent, int searchThreshold)
{
Q_UNUSED(viewObject)
QPoint adjustedPoint(touchEvent.m_pos);
QWebHitTestResult htr = frame->hitTestContent(adjustedPoint);
if (htr.isContentEditable()) {
QString type = htr.element().attribute("type").toLower();
// Don't summon the vkb for a hidden input element.
// This should never happen anyway.
if (type == "hidden") {
touchEvent.m_editable = false;
return adjustedPoint;
}
// Don't summon the vkb for a disabled input; in fact, close the vkbd.
QString disabled = htr.element().attribute("disabled").toLower();
if (disabled == "disabled" || disabled == "true") {
touchEvent.m_editable = false;
return adjustedPoint;
}
// QtWebKit EditorClientQt already enables these input hints. Thus, this should only be
// enabled if QtWebKit happens to be old. Qt::ImhNoPredictiveText is only enalbed for Symbian / Maemo on WebKit side.
touchEvent.m_editable = true;
return adjustedPoint;
}
if (!htr.element().tagName().toLower().compare("select") && htr.element().hasAttribute("multiple")) {
touchEvent.m_modifier = Qt::ControlModifier;
return adjustedPoint;
}
// If clicked element need receive event, do NOT adjust
// like : achor, input, map, button, textarea
QString tagName = htr.element().tagName().toLower();
if (!htr.linkElement().isNull() ||
!tagName.compare("input") ||
!tagName.compare("map") ||
!tagName.compare("button") ||
!tagName.compare("textarea") ||
htr.element().hasAttribute("onClick"))
return adjustedPoint;
// Attempt to find the closest radio button or checkbox. Their areas can be so
// small that we need to adjust the our position to be exactly on them if in the vicinity.
QString selector = "input[type=\"radio\"], input[type=\"checkbox\"]";
QWebElementCollection elementList = frame->findAllElements(selector);
QWebElementCollection::iterator it(elementList.begin());
// find the origin of current frame position in view based coordinate
QPoint originPoint = frameViewPosition(frame);
// transfer the event position from view based coordinate to the current frame's content based coordinate
QPoint frameScrollPoint = frame->scrollPosition();
QPoint framePoint = adjustedPoint - originPoint + frameScrollPoint;
QPoint adjustedFramePoint = framePoint;
// find the closest element
int maxDist = searchThreshold;
while (it != elementList.end()) {
QRect nRect((*it).geometry());
if (nRect.isValid()) {
QPoint pt(xInRect(nRect, framePoint.x()), yInRect(nRect, framePoint.y()));
int dist = wtnFastDistance(pt, framePoint);
if (dist < maxDist) {
adjustedFramePoint = pt;
maxDist = dist;
}
}
++it;
}
// transfer the adjusted position from the current frame's content based coordinate to view based coordinate
adjustedPoint = adjustedFramePoint - frameScrollPoint + originPoint;
return adjustedPoint;
}
WebTouchNavigation::WebTouchNavigation(QObject *parent, QWebPage *webPage)
: m_viewObject(parent)
, m_webPage(webPage)
, m_suppressMouseEvents(false)
{
Q_ASSERT(m_webPage);
connect(&m_downTimer, SIGNAL(timeout()), this, SLOT(downTimerFired()));
connect(&m_hoverTimer, SIGNAL(timeout()), this, SLOT(hoverTimerFired()));
connect(&m_quickDownTimer, SIGNAL(timeout()), this, SLOT(quickDownTimerFired()));
connect(&m_quickUpTimer, SIGNAL(timeout()), this, SLOT(quickUpTimerFired()));
m_physics = WebTouchPhysicsInterface::getSingleton();
m_physics->setParent(this);
m_scroller = new WebTouchScroller(this);
connect(m_physics, SIGNAL(positionChanged(QPointF, QPoint)), m_scroller, SLOT(scroll(QPointF, QPoint)));
connect(m_scroller, SIGNAL(rangeChanged(QRectF)), m_physics, SLOT(setRange(QRectF)));
}
WebTouchNavigation::~WebTouchNavigation()
{
}
bool WebTouchNavigation::eventFilter(QObject *object, QEvent *event)
{
if (object != m_viewObject)
return false;
bool eventHandled = false;
switch (event->type()) {
case QEvent::MouseButtonPress:
if (static_cast<QMouseEvent*>(event)->buttons() & Qt::LeftButton) {
WebTouchEvent e(static_cast<QMouseEvent*>(event));
handleDownEvent(e);
}
eventHandled = true;
break;
case QEvent::MouseMove:
if (static_cast<QMouseEvent*>(event)->buttons() & Qt::LeftButton) {
WebTouchEvent e(static_cast<QMouseEvent*>(event));
handleMoveEvent(e);
}
eventHandled = true;
break;
case QEvent::MouseButtonRelease:
{
WebTouchEvent e(static_cast<QMouseEvent*>(event));
handleReleaseEvent(e);
eventHandled = true;
}
break;
case QEvent::GraphicsSceneMousePress:
if (static_cast<QGraphicsSceneMouseEvent*>(event)->buttons() & Qt::LeftButton) {
WebTouchEvent e(static_cast<QGraphicsSceneMouseEvent*>(event));
handleDownEvent(e);
}
eventHandled = true;
break;
case QEvent::GraphicsSceneMouseMove:
if (static_cast<QGraphicsSceneMouseEvent *>(event)->buttons() & Qt::LeftButton) {
WebTouchEvent e(static_cast<QGraphicsSceneMouseEvent*>(event));
handleMoveEvent(e);
}
eventHandled = true;
break;
case QEvent::GraphicsSceneMouseRelease:
{
WebTouchEvent e(static_cast<QGraphicsSceneMouseEvent*>(event));
handleReleaseEvent(e);
eventHandled = true;
}
break;
case QEvent::MouseButtonDblClick:
case QEvent::ContextMenu:
case QEvent::CursorChange:
case QEvent::DragEnter:
case QEvent::DragLeave:
case QEvent::DragMove:
case QEvent::Drop:
case QEvent::GrabMouse:
case QEvent::GraphicsSceneContextMenu:
case QEvent::GraphicsSceneDragEnter:
case QEvent::GraphicsSceneDragLeave:
case QEvent::GraphicsSceneDragMove:
case QEvent::GraphicsSceneDrop:
case QEvent::GraphicsSceneHelp:
case QEvent::GraphicsSceneHoverEnter:
case QEvent::GraphicsSceneHoverLeave:
case QEvent::GraphicsSceneHoverMove:
case QEvent::HoverEnter:
case QEvent::HoverLeave:
case QEvent::HoverMove:
case QEvent::Gesture:
case QEvent::GestureOverride:
eventHandled = true;
break;
default:
break;
}
return eventHandled;
}
void WebTouchNavigation::quickDownTimerFired()
{
m_touchDown.m_type = (m_touchDown.m_graphicsSceneEvent) ? QEvent::GraphicsSceneMousePress : QEvent::MouseButtonPress;
m_touchDown.m_pos = closestElement(m_viewObject, m_webFrame, m_touchDown, KNodeSearchThreshold);
m_touchDown.m_button = Qt::LeftButton;
m_touchDown.m_buttons = Qt::NoButton;
generateMouseEvent(m_touchDown);
m_quickUpTimer.setSingleShot(true);
m_quickUpTimer.start(0);
}
void WebTouchNavigation::quickUpTimerFired()
{
m_touchDown.m_type = (m_touchDown.m_graphicsSceneEvent) ? QEvent::GraphicsSceneMouseRelease : QEvent::MouseButtonRelease;
m_touchDown.m_button = Qt::LeftButton;
m_touchDown.m_buttons = Qt::NoButton;
generateMouseEvent(m_touchDown);
}
void WebTouchNavigation::downTimerFired()
{
m_touchDown.m_type = (m_touchDown.m_graphicsSceneEvent) ? QEvent::GraphicsSceneMousePress : QEvent::MouseButtonPress;
m_touchDown.m_pos = closestElement(m_viewObject, m_webFrame, m_touchDown, KNodeSearchThreshold);
m_touchDown.m_button = Qt::LeftButton;