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
00026 #include <QPainter>
00027 #include <QWebView>
00028 #include <QWebFrame>
00029 #include <QWebPage>
00030 #include <QFile>
00031
00032 #include <Plasma/Applet>
00033 #include <Plasma/Package>
00034 #include <Plasma/WebView>
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->setAcceptsHoverEvents(true);
00052
00053 page = new Plasma::WebView(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 QPalette palette = page->palette();
00065 palette.setBrush(QPalette::Background, QBrush(Qt::transparent));
00066 page->setPalette(palette);
00067 }
00068
00069 Plasma::WebView *page;
00070 bool loaded;
00071 };
00072
00073 WebApplet::WebApplet(QObject *parent, const QVariantList &args)
00074 : AppletScript(parent),
00075 d(new Private)
00076 {
00077 Q_UNUSED(args)
00078 }
00079
00080 WebApplet::~WebApplet()
00081 {
00082 delete d;
00083 }
00084
00085 bool WebApplet::init()
00086 {
00087 d->init(this);
00088
00089 QString webpage;
00090 webpage = package()->filePath("mainscript");
00091
00092 if (webpage.isEmpty()) {
00093 return false;
00094 }
00095
00096 d->page->mainFrame()->setHtml(dataFor(webpage), QUrl(package()->filePath("root")));
00097 return true;
00098 }
00099
00100 void WebApplet::paintInterface(QPainter *painter,
00101 const QStyleOptionGraphicsItem *option,
00102 const QRect &contentsRect)
00103 {
00104 Q_UNUSED(painter)
00105 Q_UNUSED(option)
00106 Q_UNUSED(contentsRect)
00107 }
00108
00109 Plasma::WebView* WebApplet::view() const
00110 {
00111 return d->page;
00112 }
00113
00114 void WebApplet::loadFinished(bool success)
00115 {
00116 d->loaded = success;
00117 }
00118
00119 void WebApplet::connectFrame(QWebFrame *frame)
00120 {
00121 connect(frame, SIGNAL(javaScriptWindowObjectCleared()),
00122 this, SLOT(initJsObjects()));
00123 }
00124
00125 void WebApplet::initJsObjects()
00126 {
00127 }
00128
00129 QByteArray WebApplet::dataFor(const QString &str)
00130 {
00131 QFile f(str);
00132 f.open(QIODevice::ReadOnly);
00133 QByteArray data = f.readAll();
00134 f.close();
00135 return data;
00136 }
00137
00138 Plasma::WebView* WebApplet::page()
00139 {
00140 return d->page;
00141 }
00142
00143 bool WebApplet::loaded()
00144 {
00145 return d->loaded;
00146 }
00147
00148 #include "webapplet.moc"