2008-11-04 00:08:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
|
2010-02-18 00:59:14 +01:00
|
|
|
* Copyright 2010 Davide Bettio <davide.bettio@kdemail.net>
|
2008-11-04 00:08:39 +01:00
|
|
|
*
|
|
|
|
* 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/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>
|
2010-02-18 00:59:14 +01:00
|
|
|
#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>
|
2010-02-18 00:59:14 +01:00
|
|
|
#include <kgraphicswebview.h>
|
2009-09-02 04:27:16 +02:00
|
|
|
#include <kio/accessmanager.h>
|
2009-07-10 19:47:08 +02:00
|
|
|
#include <accessmanager.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2010-02-16 02:07:25 +01:00
|
|
|
#include "animator.h"
|
|
|
|
#include "plasma.h"
|
|
|
|
#include "widgets/webview.h"
|
2010-03-27 18:44:16 +01:00
|
|
|
#include "widgets/scrollwidget.h"
|
2010-02-18 00:59:14 +01:00
|
|
|
#include "private/animablegraphicswebview_p.h"
|
2009-10-03 17:06:41 +02:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2009-10-20 00:10:25 +02:00
|
|
|
class WebViewPrivate
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
WebViewPrivate(WebView *parent)
|
2010-02-18 00:59:14 +01:00
|
|
|
: q(parent)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void loadingFinished(bool success);
|
2009-03-30 13:58:23 +02:00
|
|
|
void dragTimeoutExpired();
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
WebView *q;
|
2010-02-18 00:59:14 +01:00
|
|
|
AnimableGraphicsWebView *webView;
|
2010-03-27 18:44:16 +01:00
|
|
|
ScrollWidget *scrollWidget;
|
2008-11-04 00:08:39 +01:00
|
|
|
bool loaded;
|
|
|
|
};
|
|
|
|
|
|
|
|
WebView::WebView(QGraphicsItem *parent)
|
2010-02-18 00:59:14 +01:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
: QGraphicsWidget(parent),
|
|
|
|
d(new WebViewPrivate(this))
|
|
|
|
{
|
|
|
|
d->loaded = false;
|
2009-11-17 18:57:54 +01:00
|
|
|
setAcceptTouchEvents(true);
|
2008-11-20 16:38:51 +01:00
|
|
|
setAcceptsHoverEvents(true);
|
|
|
|
setFlags(QGraphicsItem::ItemIsFocusable);
|
2009-04-28 18:47:50 +02:00
|
|
|
|
2010-03-27 18:44:16 +01:00
|
|
|
d->scrollWidget = new Plasma::ScrollWidget(this);
|
|
|
|
d->webView = new AnimableGraphicsWebView(d->scrollWidget);
|
|
|
|
d->scrollWidget->setWidget(d->webView);
|
|
|
|
d->scrollWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
d->scrollWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
setDragToScroll(false);
|
2009-04-28 18:47:50 +02:00
|
|
|
QPalette palette = qApp->palette();
|
|
|
|
palette.setBrush(QPalette::Base, Qt::transparent);
|
2010-02-18 00:59:14 +01:00
|
|
|
d->webView->page()->setPalette(palette);
|
|
|
|
|
|
|
|
connect(d->webView, SIGNAL(loadProgress(int)),
|
|
|
|
this, SIGNAL(loadProgress(int)));
|
|
|
|
connect(d->webView, SIGNAL(loadFinished(bool)),
|
|
|
|
this, SLOT(loadingFinished(bool)));
|
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;
|
2010-02-18 00:59:14 +01:00
|
|
|
d->webView->load(url);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-04-07 10:33:51 +02:00
|
|
|
KUrl WebView::url() const
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
return d->webView->url();
|
2009-04-07 10:33:51 +02:00
|
|
|
}
|
|
|
|
|
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;
|
2010-02-18 00:59:14 +01:00
|
|
|
d->webView->setContent(html, QString(), baseUrl);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2010-02-18 00:59:14 +01:00
|
|
|
d->webView->setHtml(html, baseUrl);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-04-07 10:33:51 +02:00
|
|
|
QString WebView::html() const
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
return d->webView->page()->mainFrame()->toHtml();
|
2009-04-07 10:33:51 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
QRectF WebView::geometry() const
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
if (d->loaded) {
|
|
|
|
return QRectF(pos(), d->webView->page()->mainFrame()->geometry().size());
|
|
|
|
} else {
|
|
|
|
return QGraphicsWidget::geometry();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-08 18:04:56 +02:00
|
|
|
QSizeF WebView::contentsSize() const
|
2009-10-03 17:06:41 +02:00
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
return d->webView->page()->mainFrame()->contentsSize();
|
2009-10-03 17:06:41 +02:00
|
|
|
}
|
|
|
|
|
2009-10-08 18:04:56 +02:00
|
|
|
void WebView::setScrollPosition(const QPointF &position)
|
2009-10-03 17:06:41 +02:00
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
d->webView->setScrollPosition(position);
|
2009-10-03 17:06:41 +02:00
|
|
|
}
|
|
|
|
|
2009-10-08 18:04:56 +02:00
|
|
|
QPointF WebView::scrollPosition() const
|
2009-10-03 17:06:41 +02:00
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
return d->webView->scrollPosition();
|
2009-10-03 17:06:41 +02:00
|
|
|
}
|
|
|
|
|
2009-10-08 18:04:56 +02:00
|
|
|
QRectF WebView::viewportGeometry() const
|
2009-10-03 17:06:41 +02:00
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
return d->webView->page()->mainFrame()->geometry();
|
2009-10-03 17:06:41 +02:00
|
|
|
}
|
|
|
|
|
2009-11-17 18:57:54 +01:00
|
|
|
qreal WebView::zoomFactor() const
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
return d->webView->zoomFactor();
|
2009-11-17 18:57:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::setZoomFactor(const qreal zoom)
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
d->webView->setZoomFactor(zoom);
|
2009-11-17 18:57:54 +01:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void WebView::setPage(QWebPage *page)
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
d->webView->setPage(page);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QWebPage *WebView::page() const
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
return d->webView->page();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QWebFrame *WebView::mainFrame() const
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
return d->webView->page()->mainFrame();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-03-30 13:58:23 +02:00
|
|
|
void WebView::setDragToScroll(bool drag)
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
d->webView->setDragToScroll(drag);
|
2010-03-27 18:44:16 +01:00
|
|
|
d->scrollWidget->setFiltersChildEvents(drag);
|
2009-03-30 13:58:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebView::dragToScroll()
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
return d->webView->dragToScroll();
|
2009-03-30 13:58:23 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void WebView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
QGraphicsWidget::paint(painter, option, widget);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
QGraphicsWidget::mouseMoveEvent(event);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2008-11-20 16:38:51 +01:00
|
|
|
void WebView::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
QGraphicsWidget::hoverMoveEvent(event);
|
2008-11-20 16:38:51 +01:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void WebView::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
QGraphicsWidget::mousePressEvent(event);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
QGraphicsWidget::mouseDoubleClickEvent(event);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
QGraphicsWidget::mouseReleaseEvent(event);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
QGraphicsWidget::contextMenuEvent(event);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::wheelEvent(QGraphicsSceneWheelEvent *event)
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
QGraphicsWidget::wheelEvent(event);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::keyPressEvent(QKeyEvent * event)
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
QGraphicsWidget::keyPressEvent(event);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::keyReleaseEvent(QKeyEvent * event)
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
QGraphicsWidget::keyReleaseEvent(event);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::focusInEvent(QFocusEvent * event)
|
|
|
|
{
|
|
|
|
QGraphicsWidget::focusInEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::focusOutEvent(QFocusEvent * event)
|
|
|
|
{
|
|
|
|
QGraphicsWidget::focusOutEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::dragEnterEvent(QGraphicsSceneDragDropEvent * event)
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
QGraphicsWidget::dragEnterEvent(event);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::dragLeaveEvent(QGraphicsSceneDragDropEvent * event)
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
QGraphicsWidget::dragLeaveEvent(event);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::dragMoveEvent(QGraphicsSceneDragDropEvent * event)
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
QGraphicsWidget::dragMoveEvent(event);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebView::dropEvent(QGraphicsSceneDragDropEvent * event)
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
QGraphicsWidget::dropEvent(event);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2010-02-16 02:07:25 +01:00
|
|
|
QVariant WebView::itemChange(GraphicsItemChange change, const QVariant &value)
|
|
|
|
{
|
2010-02-18 00:59:14 +01:00
|
|
|
if (change == QGraphicsItem::ItemSceneHasChanged) {
|
2010-02-16 02:07:25 +01:00
|
|
|
//FIXME: QWebPage _requires_ a QWidget view to not crash in places such as
|
|
|
|
// WebCore::PopupMenu::show() due to hostWindow()->platformPageClient() == NULL
|
|
|
|
// because QWebPage::d->client is NULL
|
2010-03-19 13:09:08 +01:00
|
|
|
//d->webView->page()->setView(viewFor(this));
|
2010-02-16 02:07:25 +01:00
|
|
|
}
|
2010-02-18 20:54:47 +01:00
|
|
|
return QGraphicsWidget::itemChange(change, value);
|
2010-02-16 02:07:25 +01:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void WebView::setGeometry(const QRectF &geometry)
|
|
|
|
{
|
|
|
|
QGraphicsWidget::setGeometry(geometry);
|
2010-03-27 18:44:16 +01:00
|
|
|
d->scrollWidget->setGeometry(QRectF(0, 0, geometry.width(), geometry.height()));
|
|
|
|
d->webView->setGeometry(d->scrollWidget->viewportGeometry());
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-11-04 15:16:14 +01:00
|
|
|
QSizeF WebView::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
|
|
|
|
{
|
|
|
|
if (which == Qt::PreferredSize) {
|
2010-02-18 00:59:14 +01:00
|
|
|
return d->webView->page()->mainFrame()->contentsSize();
|
2009-11-04 15:16:14 +01:00
|
|
|
} else {
|
|
|
|
return QGraphicsWidget::sizeHint(which, constraint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void WebViewPrivate::loadingFinished(bool success)
|
|
|
|
{
|
|
|
|
loaded = success;
|
2009-11-04 15:48:23 +01:00
|
|
|
q->updateGeometry();
|
2008-11-04 00:08:39 +01:00
|
|
|
emit q->loadFinished(success);
|
|
|
|
q->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "webview.moc"
|
|
|
|
|