2008-04-04 00:49:19 +02: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QtCore/QUrl>
|
|
|
|
#include <QtGui/QGraphicsSceneContextMenuEvent>
|
|
|
|
#include <QtGui/QGraphicsSceneDragDropEvent>
|
|
|
|
#include <QtGui/QGraphicsSceneMouseEvent>
|
|
|
|
#include <QtGui/QGraphicsSceneWheelEvent>
|
|
|
|
#include <QtGui/QKeyEvent>
|
|
|
|
#include <QtGui/QStyleOptionGraphicsItem>
|
|
|
|
#include <QtWebKit/QWebFrame>
|
2008-04-04 01:35:41 +02:00
|
|
|
#include <QtWebKit/QWebPage>
|
2008-04-04 00:49:19 +02:00
|
|
|
|
|
|
|
#include <KDE/KDebug>
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
#include "plasma/widgets/webview.h"
|
2008-04-04 00:49:19 +02:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
class WebViewPrivate
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
|
|
|
public:
|
2008-10-29 18:53:30 +01:00
|
|
|
WebViewPrivate(WebView *parent)
|
2008-06-29 02:20:31 +02:00
|
|
|
: q(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void loadingFinished(bool success);
|
2008-10-12 03:31:54 +02:00
|
|
|
void updateRequested(const QRect &dirtyRect);
|
2008-08-22 20:38:50 +02:00
|
|
|
void scrollRequested(int dx, int dy, const QRect &scrollRect);
|
2008-06-29 02:20:31 +02:00
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
WebView *q;
|
2008-04-04 01:35:41 +02:00
|
|
|
QWebPage *page;
|
2008-04-04 00:49:19 +02:00
|
|
|
bool loaded;
|
|
|
|
};
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
WebView::WebView(QGraphicsItem *parent)
|
2008-04-13 23:21:50 +02:00
|
|
|
: QGraphicsWidget(parent),
|
2008-10-29 18:53:30 +01:00
|
|
|
d(new WebViewPrivate(this))
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-04-04 02:13:57 +02:00
|
|
|
d->page = 0;
|
2008-04-04 00:49:19 +02:00
|
|
|
d->loaded = false;
|
2008-04-04 01:35:41 +02:00
|
|
|
setPage(new QWebPage(this));
|
2008-04-04 00:49:19 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
WebView::~WebView()
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-06-30 09:15:37 +02:00
|
|
|
delete d;
|
2008-04-04 00:49:19 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::setUrl(const QUrl &url)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
|
|
|
d->loaded = false;
|
2008-04-04 01:35:41 +02:00
|
|
|
if (d->page) {
|
|
|
|
d->page->mainFrame()->load(url);
|
|
|
|
}
|
2008-04-04 00:49:19 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::setHtml(const QByteArray &html, const QUrl &baseUrl)
|
2008-06-17 19:01:08 +02:00
|
|
|
{
|
|
|
|
d->loaded = false;
|
|
|
|
if (d->page) {
|
|
|
|
d->page->mainFrame()->setContent(html, QString(), baseUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::setHtml(const QString &html, const QUrl &baseUrl)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
|
|
|
d->loaded = false;
|
2008-04-04 01:35:41 +02:00
|
|
|
if (d->page) {
|
|
|
|
d->page->mainFrame()->setHtml(html, baseUrl);
|
|
|
|
}
|
2008-04-04 00:49:19 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
QRectF WebView::geometry() const
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-04-04 01:35:41 +02:00
|
|
|
if (d->loaded && d->page) {
|
2008-04-24 04:56:48 +02:00
|
|
|
return d->page->mainFrame()->geometry();
|
2008-04-04 00:49:19 +02:00
|
|
|
}
|
|
|
|
|
2008-04-24 04:56:48 +02:00
|
|
|
return QGraphicsWidget::geometry();
|
2008-04-04 00:49:19 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::setPage(QWebPage *page)
|
2008-04-04 01:35:41 +02:00
|
|
|
{
|
|
|
|
if (page == d->page) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d->page && d->page->parent() == this) {
|
|
|
|
delete d->page;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->page = page;
|
|
|
|
|
|
|
|
if (d->page) {
|
2008-10-12 03:31:54 +02:00
|
|
|
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 &)));
|
2008-04-04 01:35:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
QWebPage *WebView::page() const
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
|
|
|
return d->page;
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
QWebFrame *WebView::mainFrame() const
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-04-04 01:35:41 +02:00
|
|
|
return d->page ? d->page->mainFrame() : 0;
|
2008-04-04 00:49:19 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
|
2008-04-04 01:35:41 +02:00
|
|
|
if (d->loaded && d->page) {
|
2008-04-04 00:49:19 +02:00
|
|
|
//kDebug() << "painting page";
|
|
|
|
d->page->mainFrame()->render(painter, option->rect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-04-04 01:35:41 +02:00
|
|
|
if (!d->page) {
|
2008-04-13 23:21:50 +02:00
|
|
|
QGraphicsWidget::mouseMoveEvent(event);
|
2008-04-04 01:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-04 00:49:19 +02:00
|
|
|
QMouseEvent me(QEvent::MouseMove, event->pos().toPoint(), event->button(),
|
|
|
|
event->buttons(), event->modifiers());
|
|
|
|
d->page->event(&me);
|
|
|
|
if (me.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-04-04 01:35:41 +02:00
|
|
|
if (!d->page) {
|
2008-04-13 23:21:50 +02:00
|
|
|
QGraphicsWidget::mousePressEvent(event);
|
2008-04-04 01:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-04 00:49:19 +02:00
|
|
|
QMouseEvent me(QEvent::MouseButtonPress, event->pos().toPoint(), event->button(),
|
|
|
|
event->buttons(), event->modifiers());
|
|
|
|
d->page->event(&me);
|
|
|
|
if (me.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-04-04 01:35:41 +02:00
|
|
|
if (!d->page) {
|
2008-04-13 23:21:50 +02:00
|
|
|
QGraphicsWidget::mouseDoubleClickEvent(event);
|
2008-04-04 01:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-04 00:49:19 +02:00
|
|
|
QMouseEvent me(QEvent::MouseButtonDblClick, event->pos().toPoint(), event->button(),
|
|
|
|
event->buttons(), event->modifiers());
|
|
|
|
d->page->event(&me);
|
|
|
|
if (me.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-04-04 01:35:41 +02:00
|
|
|
if (!d->page) {
|
2008-04-13 23:21:50 +02:00
|
|
|
QGraphicsWidget::mouseReleaseEvent(event);
|
2008-04-04 01:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-04 00:49:19 +02:00
|
|
|
QMouseEvent me(QEvent::MouseButtonRelease, event->pos().toPoint(), event->button(),
|
|
|
|
event->buttons(), event->modifiers());
|
|
|
|
d->page->event(&me);
|
|
|
|
if (me.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-04-04 01:35:41 +02:00
|
|
|
if (!d->page) {
|
2008-04-13 23:21:50 +02:00
|
|
|
QGraphicsWidget::contextMenuEvent(event);
|
2008-04-04 01:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-04 00:49:19 +02:00
|
|
|
QContextMenuEvent ce(static_cast<QContextMenuEvent::Reason>(event->reason()),
|
|
|
|
event->pos().toPoint(), event->screenPos());
|
|
|
|
d->page->event(&ce);
|
|
|
|
if (ce.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::wheelEvent(QGraphicsSceneWheelEvent *event)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-04-04 01:35:41 +02:00
|
|
|
if (!d->page) {
|
2008-04-13 23:21:50 +02:00
|
|
|
QGraphicsWidget::wheelEvent(event);
|
2008-04-04 01:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-04 00:49:19 +02:00
|
|
|
QWheelEvent we(event->pos().toPoint(), event->delta(), event->buttons(),
|
|
|
|
event->modifiers(), event->orientation());
|
|
|
|
|
|
|
|
d->page->event(&we);
|
|
|
|
|
|
|
|
if (we.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
} else {
|
2008-04-13 23:21:50 +02:00
|
|
|
QGraphicsWidget::wheelEvent(event);
|
2008-04-04 00:49:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::keyPressEvent(QKeyEvent * event)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-04-04 01:35:41 +02:00
|
|
|
if (!d->page) {
|
2008-04-13 23:21:50 +02:00
|
|
|
QGraphicsWidget::keyPressEvent(event);
|
2008-04-04 01:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-04 00:49:19 +02:00
|
|
|
d->page->event(event);
|
|
|
|
|
|
|
|
if (!event->isAccepted()) {
|
2008-04-13 23:21:50 +02:00
|
|
|
QGraphicsWidget::keyPressEvent(event);
|
2008-04-04 00:49:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::keyReleaseEvent(QKeyEvent * event)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-04-04 01:35:41 +02:00
|
|
|
if (!d->page) {
|
2008-04-13 23:21:50 +02:00
|
|
|
QGraphicsWidget::keyReleaseEvent(event);
|
2008-04-04 01:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-04 00:49:19 +02:00
|
|
|
d->page->event(event);
|
|
|
|
|
|
|
|
if (!event->isAccepted()) {
|
2008-04-13 23:21:50 +02:00
|
|
|
QGraphicsWidget::keyPressEvent(event);
|
2008-04-04 00:49:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::focusInEvent(QFocusEvent * event)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-04-04 01:35:41 +02:00
|
|
|
if (d->page) {
|
|
|
|
d->page->event(event);
|
|
|
|
}
|
|
|
|
|
2008-04-13 23:21:50 +02:00
|
|
|
QGraphicsWidget::focusInEvent(event);
|
2008-04-04 00:49:19 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::focusOutEvent(QFocusEvent * event)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-04-04 01:35:41 +02:00
|
|
|
if (d->page) {
|
|
|
|
d->page->event(event);
|
|
|
|
}
|
|
|
|
|
2008-04-13 23:21:50 +02:00
|
|
|
QGraphicsWidget::focusOutEvent(event);
|
2008-04-04 00:49:19 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::dragEnterEvent(QGraphicsSceneDragDropEvent * event)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-04-04 01:35:41 +02:00
|
|
|
if (!d->page) {
|
2008-04-13 23:21:50 +02:00
|
|
|
QGraphicsWidget::dragEnterEvent(event);
|
2008-04-04 01:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-04 00:49:19 +02:00
|
|
|
QDragEnterEvent de(event->pos().toPoint(), event->possibleActions(), event->mimeData(),
|
|
|
|
event->buttons(), event->modifiers());
|
|
|
|
d->page->event(&de);
|
|
|
|
|
|
|
|
if (de.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::dragLeaveEvent(QGraphicsSceneDragDropEvent * event)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-04-04 01:35:41 +02:00
|
|
|
if (!d->page) {
|
2008-04-13 23:21:50 +02:00
|
|
|
QGraphicsWidget::dragLeaveEvent(event);
|
2008-04-04 01:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-04 00:49:19 +02:00
|
|
|
QDragLeaveEvent de;
|
|
|
|
d->page->event(&de);
|
|
|
|
|
|
|
|
if (de.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::dragMoveEvent(QGraphicsSceneDragDropEvent * event)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-04-04 01:35:41 +02:00
|
|
|
if (!d->page) {
|
2008-04-13 23:21:50 +02:00
|
|
|
QGraphicsWidget::dragMoveEvent(event);
|
2008-04-04 01:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-04 00:49:19 +02:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::dropEvent(QGraphicsSceneDragDropEvent * event)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-04-04 01:35:41 +02:00
|
|
|
if (!d->page) {
|
2008-04-13 23:21:50 +02:00
|
|
|
QGraphicsWidget::dropEvent(event);
|
2008-04-04 01:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-04 00:49:19 +02:00
|
|
|
QDragMoveEvent de(event->pos().toPoint(), event->possibleActions(), event->mimeData(),
|
|
|
|
event->buttons(), event->modifiers());
|
|
|
|
d->page->event(&de);
|
|
|
|
|
|
|
|
if (de.isAccepted()) {
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebView::setGeometry(const QRectF &geometry)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-04-13 23:21:50 +02:00
|
|
|
QGraphicsWidget::setGeometry(geometry);
|
2008-04-04 00:49:19 +02:00
|
|
|
d->page->setViewportSize(geometry.size().toSize());
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebViewPrivate::loadingFinished(bool success)
|
2008-04-04 00:49:19 +02:00
|
|
|
{
|
2008-06-29 02:20:31 +02:00
|
|
|
loaded = success;
|
|
|
|
emit q->loadFinished(success);
|
2008-07-13 22:50:53 +02:00
|
|
|
q->update();
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebViewPrivate::updateRequested(const QRect &dirtyRect)
|
2008-07-13 22:50:53 +02:00
|
|
|
{
|
|
|
|
if (loaded && page) {
|
2008-10-12 03:31:54 +02:00
|
|
|
q->update(QRectF(dirtyRect.topLeft().x(), dirtyRect.topLeft().y(),
|
|
|
|
dirtyRect.width(), dirtyRect.height()));
|
2008-07-13 22:50:53 +02:00
|
|
|
}
|
2008-04-04 00:49:19 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
void WebViewPrivate::scrollRequested(int dx, int dy, const QRect &scrollRect)
|
2008-08-22 20:38:50 +02:00
|
|
|
{
|
|
|
|
updateRequested(scrollRect);
|
|
|
|
}
|
|
|
|
|
2008-04-04 00:49:19 +02:00
|
|
|
} // namespace Plasma
|
|
|
|
|
2008-10-29 18:53:30 +01:00
|
|
|
#include "webview.moc"
|
2008-04-04 00:49:19 +02:00
|
|
|
|