add setDragToScroll, makes the webview to work like a touchscreen, it

selects the text instead of dragging if the move is less than 250
milliseconds after the mouse press, not really reliable but don't know
how to save both functionalities

svn path=/trunk/KDE/kdelibs/; revision=946794
This commit is contained in:
Marco Martin 2009-03-30 11:58:23 +00:00
parent 115b87704f
commit 810a3a293c
2 changed files with 63 additions and 8 deletions

View File

@ -26,6 +26,7 @@
#include <fixx11h.h> #include <fixx11h.h>
#include <QtWebKit/QWebFrame> #include <QtWebKit/QWebFrame>
#include <QtWebKit/QWebPage> #include <QtWebKit/QWebPage>
#include <QtCore/QTimer>
#include <kdebug.h> #include <kdebug.h>
@ -38,17 +39,26 @@ class WebViewPrivate
{ {
public: public:
WebViewPrivate(WebView *parent) WebViewPrivate(WebView *parent)
: q(parent) : q(parent),
dragToScroll(false),
dragging(false),
dragTimeout(false),
dragTimeoutTimer(0)
{ {
} }
void loadingFinished(bool success); void loadingFinished(bool success);
void updateRequested(const QRect &dirtyRect); void updateRequested(const QRect &dirtyRect);
void scrollRequested(int dx, int dy, const QRect &scrollRect); void scrollRequested(int dx, int dy, const QRect &scrollRect);
void dragTimeoutExpired();
WebView *q; WebView *q;
QWebPage *page; QWebPage *page;
bool loaded; bool loaded;
bool dragToScroll;
bool dragging;
bool dragTimeout;
QTimer *dragTimeoutTimer;
}; };
WebView::WebView(QGraphicsItem *parent) WebView::WebView(QGraphicsItem *parent)
@ -138,6 +148,16 @@ QWebFrame *WebView::mainFrame() const
return d->page ? d->page->mainFrame() : 0; return d->page ? d->page->mainFrame() : 0;
} }
void WebView::setDragToScroll(bool drag)
{
d->dragToScroll = drag;
}
bool WebView::dragToScroll()
{
return d->dragToScroll;
}
void WebView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) void WebView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{ {
Q_UNUSED(widget) Q_UNUSED(widget)
@ -155,11 +175,21 @@ void WebView::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
return; return;
} }
QMouseEvent me(QEvent::MouseMove, event->pos().toPoint(), event->button(), if (d->dragToScroll && d->dragTimeout) {
event->buttons(), event->modifiers()); QPointF deltaPos = event->pos() - event->lastPos();
d->page->event(&me); d->page->mainFrame()->setScrollBarValue(Qt::Horizontal, d->page->mainFrame()->scrollBarValue(Qt::Horizontal) - deltaPos.x());
if (me.isAccepted()) { d->page->mainFrame()->setScrollBarValue(Qt::Vertical, d->page->mainFrame()->scrollBarValue(Qt::Vertical) - deltaPos.y());
event->accept(); d->dragging = true;
} else {
if (d->dragTimeoutTimer) {
d->dragTimeoutTimer->stop();
}
QMouseEvent me(QEvent::MouseMove, event->pos().toPoint(), event->button(),
event->buttons(), event->modifiers());
d->page->event(&me);
if (me.isAccepted()) {
event->accept();
}
} }
} }
@ -184,6 +214,14 @@ void WebView::mousePressEvent(QGraphicsSceneMouseEvent *event)
return; return;
} }
d->dragTimeout = false;
if (!d->dragTimeoutTimer) {
d->dragTimeoutTimer = new QTimer(this);
d->dragTimeoutTimer->setSingleShot(true);
connect(d->dragTimeoutTimer, SIGNAL(timeout()), this, SLOT(dragTimeoutExpired()));
}
d->dragTimeoutTimer->start(250);
setFocus(); setFocus();
QMouseEvent me(QEvent::MouseButtonPress, event->pos().toPoint(), event->button(), QMouseEvent me(QEvent::MouseButtonPress, event->pos().toPoint(), event->button(),
@ -211,7 +249,8 @@ void WebView::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
void WebView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) void WebView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{ {
if (!d->page) { if (!d->page || d->dragging) {
d->dragging = false;
QGraphicsWidget::mouseReleaseEvent(event); QGraphicsWidget::mouseReleaseEvent(event);
return; return;
} }
@ -273,7 +312,7 @@ void WebView::keyPressEvent(QKeyEvent * event)
} }
d->page->event(event); d->page->event(event);
kWarning()<<event;
if (!event->isAccepted()) { if (!event->isAccepted()) {
QGraphicsWidget::keyPressEvent(event); QGraphicsWidget::keyPressEvent(event);
} }
@ -402,6 +441,11 @@ void WebViewPrivate::scrollRequested(int dx, int dy, const QRect &scrollRect)
updateRequested(scrollRect); updateRequested(scrollRect);
} }
void WebViewPrivate::dragTimeoutExpired()
{
dragTimeout = true;
}
} // namespace Plasma } // namespace Plasma
#include "webview.moc" #include "webview.moc"

View File

@ -104,6 +104,16 @@ class PLASMA_EXPORT WebView : public QGraphicsWidget
*/ */
QWebFrame *mainFrame() const; QWebFrame *mainFrame() const;
/**
* Sets if the page can be scrolled around by dragging the contents with the mouse
*/
void setDragToScroll(bool drag);
/**
* @return true if the page can be scrolled by dragging the mouse
*/
bool dragToScroll();
/** /**
* Reimplementation * Reimplementation
*/ */
@ -151,6 +161,7 @@ class PLASMA_EXPORT WebView : public QGraphicsWidget
Q_PRIVATE_SLOT(d, void loadingFinished(bool success)) Q_PRIVATE_SLOT(d, void loadingFinished(bool success))
Q_PRIVATE_SLOT(d, void updateRequested(const QRect& dirtyRect)) Q_PRIVATE_SLOT(d, void updateRequested(const QRect& dirtyRect))
Q_PRIVATE_SLOT(d, void scrollRequested(int dx, int dy, const QRect &scrollRect)) Q_PRIVATE_SLOT(d, void scrollRequested(int dx, int dy, const QRect &scrollRect))
Q_PRIVATE_SLOT(d, void dragTimeoutExpired())
WebViewPrivate * const d; WebViewPrivate * const d;
friend class WebViewPrivate; friend class WebViewPrivate;