2008-11-04 00:08:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2009-04-28 18:47:50 +02:00
|
|
|
#include <QtGui/QApplication>
|
2008-11-04 00:08:39 +01:00
|
|
|
#include <QtGui/QGraphicsSceneContextMenuEvent>
|
|
|
|
#include <QtGui/QGraphicsSceneDragDropEvent>
|
|
|
|
#include <QtGui/QGraphicsSceneMouseEvent>
|
|
|
|
#include <QtGui/QGraphicsSceneWheelEvent>
|
|
|
|
#include <QtGui/QKeyEvent>
|
|
|
|
#include <QtGui/QStyleOptionGraphicsItem>
|
2009-04-28 18:47:50 +02:00
|
|
|
|
2009-03-04 22:28:02 +01:00
|
|
|
#include <fixx11h.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
#include <QtWebKit/QWebFrame>
|
|
|
|
#include <QtWebKit/QWebPage>
|
2009-04-28 18:47:50 +02:00
|
|
|
|
2009-03-30 13:58:23 +02:00
|
|
|
#include <QtCore/QTimer>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2008-11-04 03:55:37 +01:00
|
|
|
#include <kdebug.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
#include "plasma/widgets/webview.h"
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class WebViewPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WebViewPrivate(WebView *parent)
|
2009-03-30 13:58:23 +02:00
|
|
|
: q(parent),
|
|
|
|
dragToScroll(false),
|
|
|
|
dragging(false),
|
|
|
|
dragTimeout(false),
|
|
|
|
dragTimeoutTimer(0)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void loadingFinished(bool success);
|
|
|
|
void updateRequested(const QRect &dirtyRect);
|
|
|
|
void scrollRequested(int dx, int dy, const QRect &scrollRect);
|
2009-03-30 13:58:23 +02:00
|
|
|
void dragTimeoutExpired();
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
WebView *q;
|
|
|
|
QWebPage *page;
|
|
|
|
bool loaded;
|
2009-03-30 13:58:23 +02:00
|
|
|
bool dragToScroll;
|
|
|
|
bool dragging;
|
|
|
|
bool dragTimeout;
|
|
|
|
QTimer *dragTimeoutTimer;
|
2008-11-04 00:08:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
WebView::WebView(QGraphicsItem *parent)
|
|
|
|
: QGraphicsWidget(parent),
|
|
|
|
d(new WebViewPrivate(this))
|
|
|
|
{
|
|
|
|
d->page = 0;
|
|
|
|
d->loaded = false;
|
2008-11-20 16:38:51 +01:00
|
|
|
setAcceptsHoverEvents(true);
|
|
|
|
setFlags(QGraphicsItem::ItemIsFocusable);
|
2009-04-28 18:47:50 +02:00
|
|
|
|
|
|
|
QWebPage *page = new QWebPage(this);
|
|
|
|
QPalette palette = qApp->palette();
|
|
|
|
palette.setBrush(QPalette::Base, Qt::transparent);
|
|
|
|
page->setPalette(palette);
|
|
|
|
setPage(page);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WebView::~WebView()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2008-11-06 00:39:30 +01:00
|
|
|
void WebView::setUrl(const KUrl &url)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
d->loaded = false;
|
|
|
|
if (d->page) {
|
|
|
|
d->page->mainFrame()->load(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-07 10:33:51 +02:00
|
|
|
KUrl WebView::url() const
|
|
|
|
{
|
|
|
|
return d->page ? d->page->mainFrame()->url() : KUrl();
|
|
|
|
}
|
|
|
|
|
2008-11-06 00:39:30 +01:00
|
|
|
void WebView::setHtml(const QByteArray &html, const KUrl &baseUrl)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
d->loaded = false;
|
|
|
|
if (d->page) {
|
|
|
|
d->page->mainFrame()->setContent(html, QString(), baseUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-06 00:39:30 +01:00
|
|
|
void WebView::setHtml(const QString &html, const KUrl &baseUrl)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
d->loaded = false;
|
|
|
|
if (d->page) {
|
|
|
|
d->page->mainFrame()->setHtml(html, baseUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-07 10:33:51 +02:00
|
|
|
QString WebView::html() const
|
|
|
|
{
|
|
|
|
return d->page ? d->page->mainFrame()->toHtml() : QByteArray();
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
QRectF WebView::geometry() const
|
|
|
|
{
|
|
|
|
if (d->loaded && d->page) {
|
|
|
|
return d->page->mainFrame()->geometry();
|
|
|
|
}
|
|
|
|
|
|
|
|
return QGraphicsWidget::geometry();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::setPage(QWebPage *page)
|
|
|
|
{
|
|
|
|
if (page == d->page) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-14 09:04:23 +01:00
|
|
|
if (d->page) {
|
|
|
|
if (d->page->parent() == this) {
|
|
|
|
delete d->page;
|
|
|
|
} else {
|
|
|
|
disconnect(d->page, 0, this, 0);
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d->page = page;
|
|
|
|
|
|
|
|
if (d->page) {
|
|
|
|
connect(d->page, SIGNAL(loadProgress(int)),
|
|
|
|
this, SIGNAL(loadProgress(int)));
|
|
|
|
connect(d->page, SIGNAL(loadFinished(bool)),
|
|
|
|
this, SLOT(loadingFinished(bool)));
|
|
|
|
connect(d->page, SIGNAL(repaintRequested(const QRect&)),
|
|
|
|
this, SLOT(updateRequested(const QRect&)));
|
|
|
|
connect(d->page, SIGNAL(scrollRequested(int, int, const QRect &)),
|
|
|
|
this, SLOT(scrollRequested(int, int, const QRect &)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QWebPage *WebView::page() const
|
|
|
|
{
|
|
|
|
return d->page;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWebFrame *WebView::mainFrame() const
|
|
|
|
{
|
|
|
|
return d->page ? d->page->mainFrame() : 0;
|
|
|
|
}
|
|
|
|
|
2009-03-30 13:58:23 +02:00
|
|
|
void WebView::setDragToScroll(bool drag)
|
|
|
|
{
|
|
|
|
d->dragToScroll = drag;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebView::dragToScroll()
|
|
|
|
{
|
|
|
|
return d->dragToScroll;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void WebView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
|
|
|
|
if (d->loaded && d->page) {
|
|
|
|
//kDebug() << "painting page";
|
|
|
|
d->page->mainFrame()->render(painter, option->rect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (!d->page) {
|
|
|
|
QGraphicsWidget::mouseMoveEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-30 13:58:23 +02:00
|
|
|
if (d->dragToScroll && d->dragTimeout) {
|
|
|
|
QPointF deltaPos = event->pos() - event->lastPos();
|
|
|
|
d->page->mainFrame()->setScrollBarValue(Qt::Horizontal, d->page->mainFrame()->scrollBarValue(Qt::Horizontal) - deltaPos.x());
|
|
|
|
d->page->mainFrame()->setScrollBarValue(Qt::Vertical, d->page->mainFrame()->scrollBarValue(Qt::Vertical) - deltaPos.y());
|
|
|
|
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();
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-20 16:38:51 +01:00
|
|
|
void WebView::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
if (!d->page) {
|
|
|
|
QGraphicsWidget::hoverMoveEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMouseEvent me(QEvent::MouseMove, event->pos().toPoint(), Qt::NoButton, Qt::NoButton, Qt::NoModifier);
|
|
|
|
d->page->event(&me);
|
|
|
|
if (me.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void WebView::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (!d->page) {
|
|
|
|
QGraphicsWidget::mousePressEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-30 13:58:23 +02:00
|
|
|
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);
|
|
|
|
|
2008-11-20 16:38:51 +01:00
|
|
|
setFocus();
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
QMouseEvent me(QEvent::MouseButtonPress, event->pos().toPoint(), event->button(),
|
|
|
|
event->buttons(), event->modifiers());
|
|
|
|
d->page->event(&me);
|
|
|
|
if (me.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (!d->page) {
|
|
|
|
QGraphicsWidget::mouseDoubleClickEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMouseEvent me(QEvent::MouseButtonDblClick, event->pos().toPoint(), event->button(),
|
|
|
|
event->buttons(), event->modifiers());
|
|
|
|
d->page->event(&me);
|
|
|
|
if (me.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2009-03-30 13:58:23 +02:00
|
|
|
if (!d->page || d->dragging) {
|
|
|
|
d->dragging = false;
|
2008-11-04 00:08:39 +01:00
|
|
|
QGraphicsWidget::mouseReleaseEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMouseEvent me(QEvent::MouseButtonRelease, event->pos().toPoint(), event->button(),
|
|
|
|
event->buttons(), event->modifiers());
|
|
|
|
d->page->event(&me);
|
|
|
|
if (me.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
|
|
|
|
{
|
|
|
|
if (!d->page) {
|
|
|
|
QGraphicsWidget::contextMenuEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QContextMenuEvent ce(static_cast<QContextMenuEvent::Reason>(event->reason()),
|
|
|
|
event->pos().toPoint(), event->screenPos());
|
2009-02-01 15:55:00 +01:00
|
|
|
|
|
|
|
if (d->page->swallowContextMenuEvent(&ce)) {
|
2008-11-04 00:08:39 +01:00
|
|
|
event->accept();
|
2009-02-01 15:55:00 +01:00
|
|
|
} else {
|
|
|
|
d->page->updatePositionDependentActions(event->pos().toPoint());
|
|
|
|
|
|
|
|
d->page->event(&ce);
|
|
|
|
if (ce.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::wheelEvent(QGraphicsSceneWheelEvent *event)
|
|
|
|
{
|
|
|
|
if (!d->page) {
|
|
|
|
QGraphicsWidget::wheelEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWheelEvent we(event->pos().toPoint(), event->delta(), event->buttons(),
|
|
|
|
event->modifiers(), event->orientation());
|
|
|
|
|
|
|
|
d->page->event(&we);
|
|
|
|
|
|
|
|
if (we.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
} else {
|
|
|
|
QGraphicsWidget::wheelEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::keyPressEvent(QKeyEvent * event)
|
|
|
|
{
|
|
|
|
if (!d->page) {
|
|
|
|
QGraphicsWidget::keyPressEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->page->event(event);
|
2009-03-30 13:58:23 +02:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
if (!event->isAccepted()) {
|
|
|
|
QGraphicsWidget::keyPressEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::keyReleaseEvent(QKeyEvent * event)
|
|
|
|
{
|
|
|
|
if (!d->page) {
|
|
|
|
QGraphicsWidget::keyReleaseEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->page->event(event);
|
|
|
|
|
|
|
|
if (!event->isAccepted()) {
|
|
|
|
QGraphicsWidget::keyPressEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::focusInEvent(QFocusEvent * event)
|
|
|
|
{
|
|
|
|
if (d->page) {
|
|
|
|
d->page->event(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsWidget::focusInEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::focusOutEvent(QFocusEvent * event)
|
|
|
|
{
|
|
|
|
if (d->page) {
|
|
|
|
d->page->event(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsWidget::focusOutEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::dragEnterEvent(QGraphicsSceneDragDropEvent * event)
|
|
|
|
{
|
|
|
|
if (!d->page) {
|
|
|
|
QGraphicsWidget::dragEnterEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDragEnterEvent de(event->pos().toPoint(), event->possibleActions(), event->mimeData(),
|
|
|
|
event->buttons(), event->modifiers());
|
|
|
|
d->page->event(&de);
|
|
|
|
|
|
|
|
if (de.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::dragLeaveEvent(QGraphicsSceneDragDropEvent * event)
|
|
|
|
{
|
|
|
|
if (!d->page) {
|
|
|
|
QGraphicsWidget::dragLeaveEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDragLeaveEvent de;
|
|
|
|
d->page->event(&de);
|
|
|
|
|
|
|
|
if (de.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::dragMoveEvent(QGraphicsSceneDragDropEvent * event)
|
|
|
|
{
|
|
|
|
if (!d->page) {
|
|
|
|
QGraphicsWidget::dragMoveEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, so the docs say "don't make a QDragMoveEvent yourself" but we're just
|
|
|
|
// replicating it here, not really creating a new one. hopefully we get away with it ;)
|
|
|
|
QDragMoveEvent de(event->pos().toPoint(), event->possibleActions(), event->mimeData(),
|
|
|
|
event->buttons(), event->modifiers());
|
|
|
|
d->page->event(&de);
|
|
|
|
|
|
|
|
if (de.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::dropEvent(QGraphicsSceneDragDropEvent * event)
|
|
|
|
{
|
|
|
|
if (!d->page) {
|
|
|
|
QGraphicsWidget::dropEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDragMoveEvent de(event->pos().toPoint(), event->possibleActions(), event->mimeData(),
|
|
|
|
event->buttons(), event->modifiers());
|
|
|
|
d->page->event(&de);
|
|
|
|
|
|
|
|
if (de.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::setGeometry(const QRectF &geometry)
|
|
|
|
{
|
|
|
|
QGraphicsWidget::setGeometry(geometry);
|
|
|
|
d->page->setViewportSize(geometry.size().toSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewPrivate::loadingFinished(bool success)
|
|
|
|
{
|
|
|
|
loaded = success;
|
|
|
|
emit q->loadFinished(success);
|
|
|
|
q->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewPrivate::updateRequested(const QRect &dirtyRect)
|
|
|
|
{
|
|
|
|
if (loaded && page) {
|
|
|
|
q->update(QRectF(dirtyRect.topLeft().x(), dirtyRect.topLeft().y(),
|
|
|
|
dirtyRect.width(), dirtyRect.height()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewPrivate::scrollRequested(int dx, int dy, const QRect &scrollRect)
|
|
|
|
{
|
2009-04-07 10:33:51 +02:00
|
|
|
Q_UNUSED(dx)
|
|
|
|
Q_UNUSED(dy)
|
2008-11-04 00:08:39 +01:00
|
|
|
updateRequested(scrollRect);
|
|
|
|
}
|
|
|
|
|
2009-03-30 13:58:23 +02:00
|
|
|
void WebViewPrivate::dragTimeoutExpired()
|
|
|
|
{
|
|
|
|
dragTimeout = true;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "webview.moc"
|
|
|
|
|