Plasma
webapplet.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00092
00093
00094
00095
00096 }
00097
00098 void WebApplet::load(const QUrl &url)
00099 {
00100 kDebug() << "Loading" << url;
00101 d->page->setUrl(url);
00102
00103
00104
00105
00106
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
00147
00148
00149
00150
00151 kDebug() << "loading" << baseUrl;
00152 d->page->mainFrame()->setHtml(html, baseUrl);
00153 }
00154
00155 void WebApplet::loadHtml(const QUrl &url)
00156 {
00157
00158
00159
00160
00161
00162 kDebug() << "loading" << url;
00163 d->page->mainFrame()->load(url);
00164 }
00165
00166 #include "webapplet.moc"