• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Plasma

webapplet.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2007 Zack Rusin <zack@kde.org>
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010 
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013 
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 THE SOFTWARE.
00021  */
00022 #include "webapplet.h"
00023 
00024 #include "webpage.h"
00025 #include "plasmajs.h"
00026 
00027 #include <QDebug>
00028 #include <QPainter>
00029 #include <QWebView>
00030 #include <QWebFrame>
00031 #include <QWebPage>
00032 
00033 #include <plasma/applet.h>
00034 #include <plasma/widgets/webcontent.h>
00035 
00036 using namespace Plasma;
00037 
00038 class WebApplet::Private
00039 {
00040 public:
00041     Private()
00042         : page(0)
00043     {
00044     }
00045 
00046     void init(WebApplet *q)
00047     {
00048         loaded = false;
00049 
00050         Plasma::Applet *applet = q->applet();
00051         applet->resize(150, 150);
00052 
00053         page = new Plasma::WebContent(applet);
00054         page->setPage(new WebPage(page));
00055         QObject::connect(page, SIGNAL(loadFinished(bool)),
00056                          q, SLOT(loadFinished(bool)));
00057         QObject::connect(page->page(), SIGNAL(frameCreated(QWebFrame *)),
00058                          q, SLOT(connectFrame(QWebFrame *)));
00059         q->connectFrame(page->mainFrame());
00060 
00061         page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
00062         page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
00063     }
00064 
00065     Plasma::WebContent *page;
00066     bool loaded;
00067 };
00068 
00069 WebApplet::WebApplet(QObject *parent, const QVariantList &args)
00070     : AppletScript(parent),
00071       d(new Private)
00072 {
00073     Q_UNUSED(args)
00074 }
00075 
00076 WebApplet::~WebApplet()
00077 {
00078     delete d;
00079 }
00080 
00081 bool WebApplet::init()
00082 {
00083     d->init(this);
00084     return true;
00085 }
00086 
00087 void WebApplet::paintInterface(QPainter *painter,
00088                                const QStyleOptionGraphicsItem *,
00089                                const QRect & contentsRect)
00090 {
00091     //painter->save();
00092     /*QPalette pal = painter->palette();
00093     pal.setBrush(QPalette::Background, Qt::transparent);
00094     painter.setPalette(pal);*/
00095     //painter->restore();
00096 }
00097 
00098 void WebApplet::load(const QUrl &url)
00099 {
00100     kDebug() << "Loading" << url;
00101     d->page->setUrl(url);
00102 
00103     //done to make sure we have very little layout space for
00104     //html which will mean that the returned contents-size will be
00105     //the minimum size for the widget.
00106     //d->->resize(10, 10);
00107 }
00108 
00109 Plasma::WebContent* WebApplet::view() const
00110 {
00111     return d->page;
00112 }
00113 
00114 void WebApplet::loadFinished(bool success)
00115 {
00116     kDebug() << success;
00117     if (success) {
00118         QSize newSize = d->page->mainFrame()->contentsSize();
00119         applet()->setGeometry(QRectF(QPoint(), newSize));
00120     }
00121 }
00122 
00123 void WebApplet::constraintsEvent(Plasma::Constraints constraints)
00124 {
00125     if (d->page && constraints & Plasma::SizeConstraint) {
00126         d->page->resize(size());
00127     }
00128 }
00129 
00130 void WebApplet::connectFrame(QWebFrame *frame)
00131 {
00132     connect(frame, SIGNAL(javaScriptWindowObjectCleared()),
00133             this, SLOT(initJsObjects()));
00134 }
00135 
00136 void WebApplet::initJsObjects()
00137 {
00138     QWebFrame *frame = qobject_cast<QWebFrame*>(sender());
00139     Q_ASSERT(frame);
00140     frame->addToJavaScriptWindowObject(QLatin1String("applet"), this);
00141     frame->addToJavaScriptWindowObject(QLatin1String("plasma"), new PlasmaJs(this));
00142 }
00143 
00144 void WebApplet::setHtml(const QByteArray &html, const QUrl &baseUrl)
00145 {
00146     //done to make sure we have very little layout space for
00147     //html which will mean that the returned contents-size will be
00148     //the minimum size for the widget.
00149     //d->webView->resize(10, 10);
00150 
00151     kDebug() << "loading" << baseUrl;
00152     d->page->mainFrame()->setHtml(html, baseUrl);
00153 }
00154 
00155 void WebApplet::loadHtml(const QUrl &url)
00156 {
00157     //done to make sure we have very little layout space for
00158     //html which will mean that the returned contents-size will be
00159     //the minimum size for the widget.
00160     //d->webView->resize(10, 10);
00161 
00162     kDebug() << "loading" << url;
00163     d->page->mainFrame()->load(url);
00164 }
00165 
00166 #include "webapplet.moc"

Plasma

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

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
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