• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

Plasma

  • plasma
  • widgets
webview.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
3 * Copyright 2010 Davide Bettio <davide.bettio@kdemail.net>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Library General Public License as
7 * published by the Free Software Foundation; either version 2, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21#include <QtGui/QApplication>
22#include <QtGui/QStyleOptionGraphicsItem>
23
24#include <fixx11h.h>
25#include <QtWebKit/QWebFrame>
26#include <QtWebKit/QWebPage>
27
28#include <QtCore/QTimer>
29
30#include <config-plasma.h>
31
32#ifndef PLASMA_NO_KIO
33#include <kio/accessmanager.h>
34#endif
35#include <kdebug.h>
36
37#include "animator.h"
38#include "plasma.h"
39#include "widgets/webview.h"
40#include "widgets/scrollwidget.h"
41#include "private/animablegraphicswebview_p.h"
42
43namespace Plasma
44{
45
46class WebViewPrivate
47{
48public:
49 WebViewPrivate(WebView *parent)
50 : q(parent)
51 {
52 }
53
54 void loadingFinished(bool success);
55 void dragTimeoutExpired();
56
57 WebView *q;
58 AnimableGraphicsWebView *webView;
59 ScrollWidget *scrollWidget;
60 bool loaded;
61};
62
63WebView::WebView(QGraphicsItem *parent)
64 : QGraphicsWidget(parent),
65 d(new WebViewPrivate(this))
66{
67 d->loaded = false;
68 setAcceptTouchEvents(true);
69 setAcceptsHoverEvents(true);
70 setFlags(QGraphicsItem::ItemIsFocusable);
71
72 d->scrollWidget = new Plasma::ScrollWidget(this);
73 d->webView = new AnimableGraphicsWebView(d->scrollWidget);
74 d->scrollWidget->setWidget(d->webView);
75 d->scrollWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
76 d->scrollWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
77 setDragToScroll(false);
78 QPalette palette = qApp->palette();
79 palette.setBrush(QPalette::Base, Qt::transparent);
80 d->webView->page()->setPalette(palette);
81#ifndef PLASMA_NO_KIO
82 d->webView->page()->setNetworkAccessManager(new KIO::AccessManager(d->webView->page()));
83#endif
84
85 connect(d->webView, SIGNAL(loadProgress(int)),
86 this, SIGNAL(loadProgress(int)));
87 connect(d->webView, SIGNAL(loadFinished(bool)),
88 this, SLOT(loadingFinished(bool)));
89 connect(d->webView, SIGNAL(urlChanged(QUrl)),
90 this, SIGNAL(urlChanged(QUrl)));
91}
92
93WebView::~WebView()
94{
95 delete d;
96}
97
98void WebView::setUrl(const KUrl &url)
99{
100 d->loaded = false;
101 d->webView->load(url);
102}
103
104KUrl WebView::url() const
105{
106 return d->webView->url();
107}
108
109void WebView::setHtml(const QByteArray &html, const KUrl &baseUrl)
110{
111 d->loaded = false;
112 d->webView->setContent(html, QString(), baseUrl);
113}
114
115void WebView::setHtml(const QString &html, const KUrl &baseUrl)
116{
117 d->loaded = false;
118 d->webView->setHtml(html, baseUrl);
119}
120
121QString WebView::html() const
122{
123 return d->webView->page()->mainFrame()->toHtml();
124}
125
126QRectF WebView::geometry() const
127{
128 if (d->loaded) {
129 return QRectF(pos(), d->webView->page()->mainFrame()->geometry().size());
130 } else {
131 return QGraphicsWidget::geometry();
132 }
133}
134
135QSizeF WebView::contentsSize() const
136{
137 return d->webView->page()->mainFrame()->contentsSize();
138}
139
140void WebView::setScrollPosition(const QPointF &position)
141{
142 d->webView->setScrollPosition(position);
143}
144
145QPointF WebView::scrollPosition() const
146{
147 return d->webView->scrollPosition();
148}
149
150QRectF WebView::viewportGeometry() const
151{
152 return d->webView->page()->mainFrame()->geometry();
153}
154
155qreal WebView::zoomFactor() const
156{
157 return d->webView->zoomFactor();
158}
159
160void WebView::setZoomFactor(const qreal zoom)
161{
162 d->webView->setZoomFactor(zoom);
163}
164
165void WebView::setPage(QWebPage *page)
166{
167 d->webView->setPage(page);
168}
169
170QWebPage *WebView::page() const
171{
172 return d->webView->page();
173}
174
175QWebFrame *WebView::mainFrame() const
176{
177 return d->webView->page()->mainFrame();
178}
179
180void WebView::setDragToScroll(bool drag)
181{
182 // enable / disable scrollbars
183 if (drag) {
184 mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
185 mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
186 } else {
187 mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAsNeeded);
188 mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAsNeeded);
189 }
190 d->webView->setDragToScroll(drag);
191 d->scrollWidget->setFiltersChildEvents(drag);
192}
193
194bool WebView::dragToScroll()
195{
196 return d->webView->dragToScroll();
197}
198
199void WebView::back()
200{
201 d->webView->back();
202}
203
204void WebView::forward()
205{
206 d->webView->forward();
207}
208
209void WebView::reload()
210{
211 d->webView->reload();
212}
213
214void WebView::stop()
215{
216 d->webView->stop();
217}
218
219void WebView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
220{
221 QGraphicsWidget::paint(painter, option, widget);
222}
223
224void WebView::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
225{
226 QGraphicsWidget::mouseMoveEvent(event);
227}
228
229void WebView::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
230{
231 QGraphicsWidget::hoverMoveEvent(event);
232}
233
234void WebView::mousePressEvent(QGraphicsSceneMouseEvent *event)
235{
236 QGraphicsWidget::mousePressEvent(event);
237}
238
239void WebView::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
240{
241 QGraphicsWidget::mouseDoubleClickEvent(event);
242}
243
244void WebView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
245{
246 QGraphicsWidget::mouseReleaseEvent(event);
247}
248
249void WebView::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
250{
251 QGraphicsWidget::contextMenuEvent(event);
252}
253
254void WebView::wheelEvent(QGraphicsSceneWheelEvent *event)
255{
256 QGraphicsWidget::wheelEvent(event);
257}
258
259void WebView::keyPressEvent(QKeyEvent * event)
260{
261 QGraphicsWidget::keyPressEvent(event);
262}
263
264void WebView::keyReleaseEvent(QKeyEvent * event)
265{
266 QGraphicsWidget::keyReleaseEvent(event);
267}
268
269void WebView::focusInEvent(QFocusEvent * event)
270{
271 QGraphicsWidget::focusInEvent(event);
272}
273
274void WebView::focusOutEvent(QFocusEvent * event)
275{
276 QGraphicsWidget::focusOutEvent(event);
277}
278
279void WebView::dragEnterEvent(QGraphicsSceneDragDropEvent * event)
280{
281 QGraphicsWidget::dragEnterEvent(event);
282}
283
284void WebView::dragLeaveEvent(QGraphicsSceneDragDropEvent * event)
285{
286 QGraphicsWidget::dragLeaveEvent(event);
287}
288
289void WebView::dragMoveEvent(QGraphicsSceneDragDropEvent * event)
290{
291 QGraphicsWidget::dragMoveEvent(event);
292}
293
294void WebView::dropEvent(QGraphicsSceneDragDropEvent * event)
295{
296 QGraphicsWidget::dropEvent(event);
297}
298
299QVariant WebView::itemChange(GraphicsItemChange change, const QVariant &value)
300{
301 if (change == QGraphicsItem::ItemSceneHasChanged) {
302 //FIXME: QWebPage _requires_ a QWidget view to not crash in places such as
303 // WebCore::PopupMenu::show() due to hostWindow()->platformPageClient() == NULL
304 // because QWebPage::d->client is NULL
305 //d->webView->page()->setView(viewFor(this));
306 }
307 return QGraphicsWidget::itemChange(change, value);
308}
309
310void WebView::setGeometry(const QRectF &geometry)
311{
312 QGraphicsWidget::setGeometry(geometry);
313 d->scrollWidget->setGeometry(QRectF(0, 0, geometry.width(), geometry.height()));
314 d->webView->setGeometry(d->scrollWidget->viewportGeometry());
315}
316
317QSizeF WebView::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
318{
319 if (which == Qt::PreferredSize) {
320 return d->webView->page()->mainFrame()->contentsSize();
321 } else {
322 return QGraphicsWidget::sizeHint(which, constraint);
323 }
324}
325
326void WebViewPrivate::loadingFinished(bool success)
327{
328 loaded = success;
329 q->updateGeometry();
330 emit q->loadFinished(success);
331 q->update();
332}
333
334} // namespace Plasma
335
336#include "webview.moc"
337
animator.h
Plasma::ScrollWidget
A container of widgets that can have scrollbars.
Definition: scrollwidget.h:44
Plasma::WebView::html
QString html
Definition: webview.h:51
Plasma::WebView::mainFrame
QWebFrame * mainFrame() const
The main web frame associated with this item.
Definition: webview.cpp:175
Plasma::WebView::url
KUrl url
Definition: webview.h:50
Plasma::WebView::urlChanged
void urlChanged(const QUrl &url)
url displayed by the web page changed
Plasma::WebView::geometry
QRectF geometry() const
Reimplementation.
Definition: webview.cpp:126
Plasma::WebView::loadProgress
void loadProgress(int percent)
During loading progress, this signal is emitted.
Plasma::WebView::loadFinished
void loadFinished(bool success)
This signal is emitted when loading is completed.
Plasma::WebView::setDragToScroll
void setDragToScroll(bool drag)
Sets if the page can be scrolled around by dragging the contents with the mouse.
Definition: webview.cpp:180
Plasma::WebView::page
QWebPage * page() const
The QWebPage associated with this item.
Definition: webview.cpp:170
QGraphicsWidget
QStyleOptionGraphicsItem
QWidget
Plasma
Namespace for everything in libplasma.
Definition: abstractdialogmanager.cpp:25
plasma.h
scrollwidget.h
webview.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal