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

Plasma

  • sources
  • kde-4.12
  • kdelibs
  • 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 
43 namespace Plasma
44 {
45 
46 class WebViewPrivate
47 {
48 public:
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 
63 WebView::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 
93 WebView::~WebView()
94 {
95  delete d;
96 }
97 
98 void WebView::setUrl(const KUrl &url)
99 {
100  d->loaded = false;
101  d->webView->load(url);
102 }
103 
104 KUrl WebView::url() const
105 {
106  return d->webView->url();
107 }
108 
109 void WebView::setHtml(const QByteArray &html, const KUrl &baseUrl)
110 {
111  d->loaded = false;
112  d->webView->setContent(html, QString(), baseUrl);
113 }
114 
115 void WebView::setHtml(const QString &html, const KUrl &baseUrl)
116 {
117  d->loaded = false;
118  d->webView->setHtml(html, baseUrl);
119 }
120 
121 QString WebView::html() const
122 {
123  return d->webView->page()->mainFrame()->toHtml();
124 }
125 
126 QRectF 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 
135 QSizeF WebView::contentsSize() const
136 {
137  return d->webView->page()->mainFrame()->contentsSize();
138 }
139 
140 void WebView::setScrollPosition(const QPointF &position)
141 {
142  d->webView->setScrollPosition(position);
143 }
144 
145 QPointF WebView::scrollPosition() const
146 {
147  return d->webView->scrollPosition();
148 }
149 
150 QRectF WebView::viewportGeometry() const
151 {
152  return d->webView->page()->mainFrame()->geometry();
153 }
154 
155 qreal WebView::zoomFactor() const
156 {
157  return d->webView->zoomFactor();
158 }
159 
160 void WebView::setZoomFactor(const qreal zoom)
161 {
162  d->webView->setZoomFactor(zoom);
163 }
164 
165 void WebView::setPage(QWebPage *page)
166 {
167  d->webView->setPage(page);
168 }
169 
170 QWebPage *WebView::page() const
171 {
172  return d->webView->page();
173 }
174 
175 QWebFrame *WebView::mainFrame() const
176 {
177  return d->webView->page()->mainFrame();
178 }
179 
180 void 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 
194 bool WebView::dragToScroll()
195 {
196  return d->webView->dragToScroll();
197 }
198 
199 void WebView::back()
200 {
201  d->webView->back();
202 }
203 
204 void WebView::forward()
205 {
206  d->webView->forward();
207 }
208 
209 void WebView::reload()
210 {
211  d->webView->reload();
212 }
213 
214 void WebView::stop()
215 {
216  d->webView->stop();
217 }
218 
219 void WebView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
220 {
221  QGraphicsWidget::paint(painter, option, widget);
222 }
223 
224 void WebView::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
225 {
226  QGraphicsWidget::mouseMoveEvent(event);
227 }
228 
229 void WebView::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
230 {
231  QGraphicsWidget::hoverMoveEvent(event);
232 }
233 
234 void WebView::mousePressEvent(QGraphicsSceneMouseEvent *event)
235 {
236  QGraphicsWidget::mousePressEvent(event);
237 }
238 
239 void WebView::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
240 {
241  QGraphicsWidget::mouseDoubleClickEvent(event);
242 }
243 
244 void WebView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
245 {
246  QGraphicsWidget::mouseReleaseEvent(event);
247 }
248 
249 void WebView::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
250 {
251  QGraphicsWidget::contextMenuEvent(event);
252 }
253 
254 void WebView::wheelEvent(QGraphicsSceneWheelEvent *event)
255 {
256  QGraphicsWidget::wheelEvent(event);
257 }
258 
259 void WebView::keyPressEvent(QKeyEvent * event)
260 {
261  QGraphicsWidget::keyPressEvent(event);
262 }
263 
264 void WebView::keyReleaseEvent(QKeyEvent * event)
265 {
266  QGraphicsWidget::keyReleaseEvent(event);
267 }
268 
269 void WebView::focusInEvent(QFocusEvent * event)
270 {
271  QGraphicsWidget::focusInEvent(event);
272 }
273 
274 void WebView::focusOutEvent(QFocusEvent * event)
275 {
276  QGraphicsWidget::focusOutEvent(event);
277 }
278 
279 void WebView::dragEnterEvent(QGraphicsSceneDragDropEvent * event)
280 {
281  QGraphicsWidget::dragEnterEvent(event);
282 }
283 
284 void WebView::dragLeaveEvent(QGraphicsSceneDragDropEvent * event)
285 {
286  QGraphicsWidget::dragLeaveEvent(event);
287 }
288 
289 void WebView::dragMoveEvent(QGraphicsSceneDragDropEvent * event)
290 {
291  QGraphicsWidget::dragMoveEvent(event);
292 }
293 
294 void WebView::dropEvent(QGraphicsSceneDragDropEvent * event)
295 {
296  QGraphicsWidget::dropEvent(event);
297 }
298 
299 QVariant 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 
310 void 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 
317 QSizeF 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 
326 void 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 
Plasma::WebView::setScrollPosition
void setScrollPosition(const QPointF &position)
Sets the position of the webpage relative to this widget.
Definition: webview.cpp:140
Plasma::WebView::viewportGeometry
QRectF viewportGeometry() const
The geometry of the area that actually displays the web page.
Plasma::WebView::html
QString html() const
Plasma::Vertical
The applet is constrained horizontally, but can expand vertically.
Definition: plasma.h:77
Plasma::WebView::mouseReleaseEvent
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
Definition: webview.cpp:244
Plasma::WebView::keyReleaseEvent
void keyReleaseEvent(QKeyEvent *event)
Definition: webview.cpp:264
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::urlChanged
void urlChanged(const QUrl &url)
url displayed by the web page changed
Plasma::WebView::focusOutEvent
void focusOutEvent(QFocusEvent *event)
Definition: webview.cpp:274
Plasma::WebView::setZoomFactor
void setZoomFactor(const qreal zoom)
Sets the zoom factor of the page.
Definition: webview.cpp:160
Plasma::WebView::WebView
WebView(QGraphicsItem *parent=0)
Definition: webview.cpp:63
Plasma::WebView::mouseMoveEvent
void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
Definition: webview.cpp:224
QWidget
Plasma::WebView::zoomFactor
qreal zoomFactor() const
The zoom factor of the page.
Plasma::WebView::hoverMoveEvent
void hoverMoveEvent(QGraphicsSceneHoverEvent *event)
Definition: webview.cpp:229
Plasma::WebView::mouseDoubleClickEvent
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
Definition: webview.cpp:239
Plasma::Horizontal
The applet is constrained vertically, but can expand horizontally.
Definition: plasma.h:75
Plasma::WebView::dragEnterEvent
void dragEnterEvent(QGraphicsSceneDragDropEvent *event)
Definition: webview.cpp:279
Plasma::WebView::itemChange
QVariant itemChange(GraphicsItemChange change, const QVariant &value)
Definition: webview.cpp:299
Plasma::WebView::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Reimplementation.
Definition: webview.cpp:219
Plasma::WebView::dragLeaveEvent
void dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
Definition: webview.cpp:284
Plasma::WebView::focusInEvent
void focusInEvent(QFocusEvent *event)
Definition: webview.cpp:269
Plasma::WebView::wheelEvent
void wheelEvent(QGraphicsSceneWheelEvent *event)
Definition: webview.cpp:254
Plasma::WebView::contentsSize
QSizeF contentsSize() const
Plasma::WebView::contextMenuEvent
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
Definition: webview.cpp:249
Plasma::WebView::back
void back()
Loads the previous document in the list of documents built by navigating links.
Definition: webview.cpp:199
Plasma::WebView::reload
void reload()
Reloads the current document.
Definition: webview.cpp:209
Plasma::WebView::setUrl
void setUrl(const KUrl &url)
Sets the URL to display.
Definition: webview.cpp:98
Plasma::WebView::forward
void forward()
Loads the next document in the list of documents built by navigating links.
Definition: webview.cpp:204
Plasma::WebView::url
KUrl url() const
scrollwidget.h
Plasma::WebView::mousePressEvent
void mousePressEvent(QGraphicsSceneMouseEvent *event)
Definition: webview.cpp:234
Plasma::WebView::dragMoveEvent
void dragMoveEvent(QGraphicsSceneDragDropEvent *event)
Definition: webview.cpp:289
Plasma::WebView::setGeometry
void setGeometry(const QRectF &geometry)
Reimplementation.
Definition: webview.cpp:310
Plasma::WebView::dragToScroll
bool dragToScroll()
plasma.h
Plasma::WebView::page
QWebPage * page() const
The QWebPage associated with this item.
Definition: webview.cpp:170
Plasma::WebView::~WebView
~WebView()
Definition: webview.cpp:93
Plasma::WebView::scrollPosition
QPointF scrollPosition() const
Plasma::WebView::geometry
QRectF geometry() const
Reimplementation.
Definition: webview.cpp:126
webview.h
Plasma::ScrollWidget
A container of widgets that can have scrollbars.
Definition: scrollwidget.h:43
Plasma::WebView::mainFrame
QWebFrame * mainFrame() const
The main web frame associated with this item.
Definition: webview.cpp:175
animator.h
Plasma::WebView::loadProgress
void loadProgress(int percent)
During loading progress, this signal is emitted.
Plasma::WebView::setHtml
void setHtml(const QByteArray &html, const KUrl &baseUrl=KUrl())
Sets the html to be shown along with a base URL to be used to resolve relative references.
Definition: webview.cpp:109
Plasma::WebView::setPage
void setPage(QWebPage *page)
Sets the page to use in this item.
Definition: webview.cpp:165
QStyleOptionGraphicsItem
Plasma::WebView::keyPressEvent
void keyPressEvent(QKeyEvent *event)
Definition: webview.cpp:259
Plasma::WebView::loadFinished
void loadFinished(bool success)
This signal is emitted when loading is completed.
Plasma::WebView::dropEvent
void dropEvent(QGraphicsSceneDragDropEvent *event)
Definition: webview.cpp:294
Plasma::WebView::stop
void stop()
Stops loading the document.
Definition: webview.cpp:214
Plasma::WebView::sizeHint
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
Definition: webview.cpp:317
QGraphicsWidget
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:34 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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