Plasma
webview.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright 2006-2007 Aaron Seigo <aseigo@kde.org> 00003 * Copyright 2010 Davide Bettio <davide.bettio@kdemail.net> 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU Library General Public License as 00007 * published by the Free Software Foundation; either version 2, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details 00014 * 00015 * You should have received a copy of the GNU Library General Public 00016 * License along with this program; if not, write to the 00017 * Free Software Foundation, Inc., 00018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include <QtGui/QApplication> 00022 #include <QtGui/QStyleOptionGraphicsItem> 00023 00024 #include <fixx11h.h> 00025 #include <QtWebKit/QWebFrame> 00026 #include <QtWebKit/QWebPage> 00027 00028 #include <QtCore/QTimer> 00029 00030 #include <config-plasma.h> 00031 00032 #ifndef PLASMA_NO_KIO 00033 #include <kio/accessmanager.h> 00034 #endif 00035 #include <kdebug.h> 00036 00037 #include "animator.h" 00038 #include "plasma.h" 00039 #include "widgets/webview.h" 00040 #include "widgets/scrollwidget.h" 00041 #include "private/animablegraphicswebview_p.h" 00042 00043 namespace Plasma 00044 { 00045 00046 class WebViewPrivate 00047 { 00048 public: 00049 WebViewPrivate(WebView *parent) 00050 : q(parent) 00051 { 00052 } 00053 00054 void loadingFinished(bool success); 00055 void dragTimeoutExpired(); 00056 00057 WebView *q; 00058 AnimableGraphicsWebView *webView; 00059 ScrollWidget *scrollWidget; 00060 bool loaded; 00061 }; 00062 00063 WebView::WebView(QGraphicsItem *parent) 00064 : QGraphicsWidget(parent), 00065 d(new WebViewPrivate(this)) 00066 { 00067 d->loaded = false; 00068 setAcceptTouchEvents(true); 00069 setAcceptsHoverEvents(true); 00070 setFlags(QGraphicsItem::ItemIsFocusable); 00071 00072 d->scrollWidget = new Plasma::ScrollWidget(this); 00073 d->webView = new AnimableGraphicsWebView(d->scrollWidget); 00074 d->scrollWidget->setWidget(d->webView); 00075 d->scrollWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 00076 d->scrollWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 00077 setDragToScroll(false); 00078 QPalette palette = qApp->palette(); 00079 palette.setBrush(QPalette::Base, Qt::transparent); 00080 d->webView->page()->setPalette(palette); 00081 #ifndef PLASMA_NO_KIO 00082 d->webView->page()->setNetworkAccessManager(new KIO::AccessManager(d->webView->page())); 00083 #endif 00084 00085 connect(d->webView, SIGNAL(loadProgress(int)), 00086 this, SIGNAL(loadProgress(int))); 00087 connect(d->webView, SIGNAL(loadFinished(bool)), 00088 this, SLOT(loadingFinished(bool))); 00089 connect(d->webView, SIGNAL(urlChanged(const QUrl &)), 00090 this, SIGNAL(urlChanged(const QUrl &))); 00091 } 00092 00093 WebView::~WebView() 00094 { 00095 delete d; 00096 } 00097 00098 void WebView::setUrl(const KUrl &url) 00099 { 00100 d->loaded = false; 00101 d->webView->load(url); 00102 } 00103 00104 KUrl WebView::url() const 00105 { 00106 return d->webView->url(); 00107 } 00108 00109 void WebView::setHtml(const QByteArray &html, const KUrl &baseUrl) 00110 { 00111 d->loaded = false; 00112 d->webView->setContent(html, QString(), baseUrl); 00113 } 00114 00115 void WebView::setHtml(const QString &html, const KUrl &baseUrl) 00116 { 00117 d->loaded = false; 00118 d->webView->setHtml(html, baseUrl); 00119 } 00120 00121 QString WebView::html() const 00122 { 00123 return d->webView->page()->mainFrame()->toHtml(); 00124 } 00125 00126 QRectF WebView::geometry() const 00127 { 00128 if (d->loaded) { 00129 return QRectF(pos(), d->webView->page()->mainFrame()->geometry().size()); 00130 } else { 00131 return QGraphicsWidget::geometry(); 00132 } 00133 } 00134 00135 QSizeF WebView::contentsSize() const 00136 { 00137 return d->webView->page()->mainFrame()->contentsSize(); 00138 } 00139 00140 void WebView::setScrollPosition(const QPointF &position) 00141 { 00142 d->webView->setScrollPosition(position); 00143 } 00144 00145 QPointF WebView::scrollPosition() const 00146 { 00147 return d->webView->scrollPosition(); 00148 } 00149 00150 QRectF WebView::viewportGeometry() const 00151 { 00152 return d->webView->page()->mainFrame()->geometry(); 00153 } 00154 00155 qreal WebView::zoomFactor() const 00156 { 00157 return d->webView->zoomFactor(); 00158 } 00159 00160 void WebView::setZoomFactor(const qreal zoom) 00161 { 00162 d->webView->setZoomFactor(zoom); 00163 } 00164 00165 void WebView::setPage(QWebPage *page) 00166 { 00167 d->webView->setPage(page); 00168 } 00169 00170 QWebPage *WebView::page() const 00171 { 00172 return d->webView->page(); 00173 } 00174 00175 QWebFrame *WebView::mainFrame() const 00176 { 00177 return d->webView->page()->mainFrame(); 00178 } 00179 00180 void WebView::setDragToScroll(bool drag) 00181 { 00182 d->webView->setDragToScroll(drag); 00183 d->scrollWidget->setFiltersChildEvents(drag); 00184 } 00185 00186 bool WebView::dragToScroll() 00187 { 00188 return d->webView->dragToScroll(); 00189 } 00190 00191 void WebView::back() 00192 { 00193 d->webView->back(); 00194 } 00195 00196 void WebView::forward() 00197 { 00198 d->webView->forward(); 00199 } 00200 00201 void WebView::reload() 00202 { 00203 d->webView->reload(); 00204 } 00205 00206 void WebView::stop() 00207 { 00208 d->webView->stop(); 00209 } 00210 00211 void WebView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 00212 { 00213 QGraphicsWidget::paint(painter, option, widget); 00214 } 00215 00216 void WebView::mouseMoveEvent(QGraphicsSceneMouseEvent *event) 00217 { 00218 QGraphicsWidget::mouseMoveEvent(event); 00219 } 00220 00221 void WebView::hoverMoveEvent(QGraphicsSceneHoverEvent *event) 00222 { 00223 QGraphicsWidget::hoverMoveEvent(event); 00224 } 00225 00226 void WebView::mousePressEvent(QGraphicsSceneMouseEvent *event) 00227 { 00228 QGraphicsWidget::mousePressEvent(event); 00229 } 00230 00231 void WebView::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) 00232 { 00233 QGraphicsWidget::mouseDoubleClickEvent(event); 00234 } 00235 00236 void WebView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) 00237 { 00238 QGraphicsWidget::mouseReleaseEvent(event); 00239 } 00240 00241 void WebView::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) 00242 { 00243 QGraphicsWidget::contextMenuEvent(event); 00244 } 00245 00246 void WebView::wheelEvent(QGraphicsSceneWheelEvent *event) 00247 { 00248 QGraphicsWidget::wheelEvent(event); 00249 } 00250 00251 void WebView::keyPressEvent(QKeyEvent * event) 00252 { 00253 QGraphicsWidget::keyPressEvent(event); 00254 } 00255 00256 void WebView::keyReleaseEvent(QKeyEvent * event) 00257 { 00258 QGraphicsWidget::keyReleaseEvent(event); 00259 } 00260 00261 void WebView::focusInEvent(QFocusEvent * event) 00262 { 00263 QGraphicsWidget::focusInEvent(event); 00264 } 00265 00266 void WebView::focusOutEvent(QFocusEvent * event) 00267 { 00268 QGraphicsWidget::focusOutEvent(event); 00269 } 00270 00271 void WebView::dragEnterEvent(QGraphicsSceneDragDropEvent * event) 00272 { 00273 QGraphicsWidget::dragEnterEvent(event); 00274 } 00275 00276 void WebView::dragLeaveEvent(QGraphicsSceneDragDropEvent * event) 00277 { 00278 QGraphicsWidget::dragLeaveEvent(event); 00279 } 00280 00281 void WebView::dragMoveEvent(QGraphicsSceneDragDropEvent * event) 00282 { 00283 QGraphicsWidget::dragMoveEvent(event); 00284 } 00285 00286 void WebView::dropEvent(QGraphicsSceneDragDropEvent * event) 00287 { 00288 QGraphicsWidget::dropEvent(event); 00289 } 00290 00291 QVariant WebView::itemChange(GraphicsItemChange change, const QVariant &value) 00292 { 00293 if (change == QGraphicsItem::ItemSceneHasChanged) { 00294 //FIXME: QWebPage _requires_ a QWidget view to not crash in places such as 00295 // WebCore::PopupMenu::show() due to hostWindow()->platformPageClient() == NULL 00296 // because QWebPage::d->client is NULL 00297 //d->webView->page()->setView(viewFor(this)); 00298 } 00299 return QGraphicsWidget::itemChange(change, value); 00300 } 00301 00302 void WebView::setGeometry(const QRectF &geometry) 00303 { 00304 QGraphicsWidget::setGeometry(geometry); 00305 d->scrollWidget->setGeometry(QRectF(0, 0, geometry.width(), geometry.height())); 00306 d->webView->setGeometry(d->scrollWidget->viewportGeometry()); 00307 } 00308 00309 QSizeF WebView::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const 00310 { 00311 if (which == Qt::PreferredSize) { 00312 return d->webView->page()->mainFrame()->contentsSize(); 00313 } else { 00314 return QGraphicsWidget::sizeHint(which, constraint); 00315 } 00316 } 00317 00318 void WebViewPrivate::loadingFinished(bool success) 00319 { 00320 loaded = success; 00321 q->updateGeometry(); 00322 emit q->loadFinished(success); 00323 q->update(); 00324 } 00325 00326 } // namespace Plasma 00327 00328 #include "webview.moc" 00329
KDE 4.6 API Reference