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

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     // enable / disable scrollbars
00183     if (drag) {
00184         mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
00185         mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
00186     } else {
00187         mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAsNeeded);
00188         mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAsNeeded);
00189     }
00190     d->webView->setDragToScroll(drag);
00191     d->scrollWidget->setFiltersChildEvents(drag);
00192 }
00193 
00194 bool WebView::dragToScroll()
00195 {
00196     return d->webView->dragToScroll();
00197 }
00198 
00199 void WebView::back()
00200 {
00201     d->webView->back();
00202 }
00203 
00204 void WebView::forward()
00205 {
00206     d->webView->forward();
00207 }
00208 
00209 void WebView::reload()
00210 {
00211     d->webView->reload();
00212 }
00213 
00214 void WebView::stop()
00215 {
00216     d->webView->stop();
00217 }
00218 
00219 void WebView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
00220 {
00221     QGraphicsWidget::paint(painter, option, widget);
00222 }
00223 
00224 void WebView::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
00225 {
00226     QGraphicsWidget::mouseMoveEvent(event);
00227 }
00228 
00229 void WebView::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
00230 {
00231     QGraphicsWidget::hoverMoveEvent(event);
00232 }
00233 
00234 void WebView::mousePressEvent(QGraphicsSceneMouseEvent *event)
00235 {
00236     QGraphicsWidget::mousePressEvent(event);
00237 }
00238 
00239 void WebView::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
00240 {
00241     QGraphicsWidget::mouseDoubleClickEvent(event);
00242 }
00243 
00244 void WebView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
00245 {
00246     QGraphicsWidget::mouseReleaseEvent(event);
00247 }
00248 
00249 void WebView::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
00250 {
00251     QGraphicsWidget::contextMenuEvent(event);
00252 }
00253 
00254 void WebView::wheelEvent(QGraphicsSceneWheelEvent *event)
00255 {
00256     QGraphicsWidget::wheelEvent(event);
00257 }
00258 
00259 void WebView::keyPressEvent(QKeyEvent * event)
00260 {
00261     QGraphicsWidget::keyPressEvent(event);
00262 }
00263 
00264 void WebView::keyReleaseEvent(QKeyEvent * event)
00265 {
00266     QGraphicsWidget::keyReleaseEvent(event);
00267 }
00268 
00269 void WebView::focusInEvent(QFocusEvent * event)
00270 {
00271     QGraphicsWidget::focusInEvent(event);
00272 }
00273 
00274 void WebView::focusOutEvent(QFocusEvent * event)
00275 {
00276     QGraphicsWidget::focusOutEvent(event);
00277 }
00278 
00279 void WebView::dragEnterEvent(QGraphicsSceneDragDropEvent * event)
00280 {
00281     QGraphicsWidget::dragEnterEvent(event);
00282 }
00283 
00284 void WebView::dragLeaveEvent(QGraphicsSceneDragDropEvent * event)
00285 {
00286     QGraphicsWidget::dragLeaveEvent(event);
00287 }
00288 
00289 void WebView::dragMoveEvent(QGraphicsSceneDragDropEvent * event)
00290 {
00291     QGraphicsWidget::dragMoveEvent(event);
00292 }
00293 
00294 void WebView::dropEvent(QGraphicsSceneDragDropEvent * event)
00295 {
00296     QGraphicsWidget::dropEvent(event);
00297 }
00298 
00299 QVariant WebView::itemChange(GraphicsItemChange change, const QVariant &value)
00300 {
00301     if (change == QGraphicsItem::ItemSceneHasChanged) {
00302         //FIXME: QWebPage _requires_ a QWidget view to not crash in places such as 
00303         // WebCore::PopupMenu::show() due to hostWindow()->platformPageClient() == NULL
00304         // because QWebPage::d->client is NULL
00305         //d->webView->page()->setView(viewFor(this));
00306     }
00307     return QGraphicsWidget::itemChange(change, value);
00308 }
00309 
00310 void WebView::setGeometry(const QRectF &geometry)
00311 {
00312     QGraphicsWidget::setGeometry(geometry);
00313     d->scrollWidget->setGeometry(QRectF(0, 0, geometry.width(), geometry.height()));
00314     d->webView->setGeometry(d->scrollWidget->viewportGeometry());
00315 }
00316 
00317 QSizeF WebView::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
00318 {
00319     if (which == Qt::PreferredSize) {
00320         return d->webView->page()->mainFrame()->contentsSize();
00321     } else {
00322         return QGraphicsWidget::sizeHint(which, constraint);
00323     }
00324 }
00325 
00326 void WebViewPrivate::loadingFinished(bool success)
00327 {
00328     loaded = success;
00329     q->updateGeometry();
00330     emit q->loadFinished(success);
00331     q->update();
00332 }
00333 
00334 } // namespace Plasma
00335 
00336 #include "webview.moc"
00337 

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

Skip menu "kdelibs"
  • 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
Generated for kdelibs by doxygen 1.7.5
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal