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-04-04 01:35:41 +02:00
|
|
|
#include "plasma/widgets/webcontent.h"
|
2008-04-04 00:49:19 +02:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class WebContent::Private
|
|
|
|
{
|
|
|
|
public:
|
2008-04-04 01:35:41 +02:00
|
|
|
QWebPage *page;
|
2008-04-04 00:49:19 +02:00
|
|
|
bool loaded;
|
|
|
|
};
|
|
|
|
|
2008-04-13 23:21:50 +02:00
|
|
|
WebContent::WebContent(QGraphicsItem *parent)
|
|
|
|
: QGraphicsWidget(parent),
|
2008-04-04 00:49:19 +02:00
|
|
|
d(new Private)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
WebContent::~WebContent()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContent::setUrl(const QUrl &url)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void WebContent::setHtml(const QByteArray &html, const QUrl &baseUrl)
|
|
|
|
{
|
|
|
|
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-04-13 23:21:50 +02:00
|
|
|
QSizeF WebContent::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-04 00:49:19 +02:00
|
|
|
return d->page->mainFrame()->contentsSize();
|
|
|
|
}
|
|
|
|
|
2008-04-13 23:21:50 +02:00
|
|
|
return QGraphicsWidget::geometry().size();
|
2008-04-04 00:49:19 +02:00
|
|
|
}
|
|
|
|
|
2008-04-04 01:35:41 +02:00
|
|
|
void WebContent::setPage(QWebPage *page)
|
|
|
|
{
|
|
|
|
if (page == d->page) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d->page && d->page->parent() == this) {
|
|
|
|
delete d->page;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->page = page;
|
|
|
|
|
|
|
|
if (d->page) {
|
|
|
|
connect(d->page, SIGNAL(loadProgress(int)), this, SIGNAL(loadProgress(int)));
|
|
|
|
connect(d->page->mainFrame(), SIGNAL(loadDone(bool)), this, SLOT(loadingComplete(bool)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-04 00:49:19 +02:00
|
|
|
QWebPage* WebContent::page() const
|
|
|
|
{
|
|
|
|
return d->page;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWebFrame* WebContent::mainFrame() const
|
|
|
|
{
|
2008-04-04 01:35:41 +02:00
|
|
|
return d->page ? d->page->mainFrame() : 0;
|
2008-04-04 00:49:19 +02:00
|
|
|
}
|
|
|
|
|
2008-04-13 23:21:50 +02:00
|
|
|
void WebContent::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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContent::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContent::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContent::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContent::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContent::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
|
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContent::wheelEvent(QGraphicsSceneWheelEvent *event)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContent::keyPressEvent(QKeyEvent * event)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContent::keyReleaseEvent(QKeyEvent * event)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContent::focusInEvent(QFocusEvent * event)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void WebContent::focusOutEvent(QFocusEvent * event)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void WebContent::dragEnterEvent(QGraphicsSceneDragDropEvent * event)
|
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContent::dragLeaveEvent(QGraphicsSceneDragDropEvent * event)
|
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContent::dragMoveEvent(QGraphicsSceneDragDropEvent * event)
|
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContent::dropEvent(QGraphicsSceneDragDropEvent * event)
|
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContent::setGeometry(const QRectF &geometry)
|
|
|
|
{
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContent::loadingComplete(bool success)
|
|
|
|
{
|
|
|
|
d->loaded = success;
|
|
|
|
emit loadDone(success);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "webcontent.moc"
|
|
|
|
|