add more spacing
[personal-kdebase.git] / workspace / plasma / scriptengines / webkit / plasmawebapplet.cpp
blobdff0f9962a99abc7b102abf04bfb2d1fb60d21d7
1 /*
2 Copyright (c) 2007 Zack Rusin <zack@kde.org>
3 Copyright (c) 2008 Petri Damstén <damu@iki.fi>
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
24 #include "plasmawebapplet.h"
26 #include <QWebPage>
27 #include <QWebFrame>
29 #include <KColorScheme>
31 #include <Plasma/WebView>
32 #include <Plasma/Applet>
33 #include <Plasma/Theme>
35 #define JS_CONSTANTS_CONSTRAINT \
36 "var NoConstraint = %1;\n"\
37 "var FormFactorConstraint = %2;\n"\
38 "var LocationConstraint = %3;\n"\
39 "var ScreenConstraint = %4;\n"\
40 "var SizeConstraint = %5;\n"\
41 "var ImmutableConstraint = %6;\n"\
42 "var StartupCompletedConstraint = %7;\n"\
43 "var ContextConstraint = %8;\n"\
44 "var AllConstraints = %9;\n"
46 #define JS_CONSTANTS_BACKGROUND \
47 "var NoBackground = %1;\n"\
48 "var StandardBackground = %2;\n"\
49 "var TranslucentBackground = %3;\n"\
50 "var DefaultBackground = %5;\n"
52 #define JS_CONSTANTS_SCROLLBAR \
53 "var QtHorizontal = %1;\n"\
54 "var QtVertical = %2;\n"\
55 "var ScrollBarAsNeeded = %3;\n"\
56 "var ScrollBarAlwaysOff = %4;\n"\
57 "var ScrollBarAlwaysOn = %5;\n"\
59 #define JS_CONSTANTS_ASPECTRATIO \
60 "var InvalidAspectRatioMode = %1;\n"\
61 "var IgnoreAspectRatio = %2;\n"\
62 "var KeepAspectRatio = %3;\n"\
63 "var Square = %4;\n"\
64 "var ConstrainedSquare = %5;\n"\
65 "var FixedSize = %6;\n"
67 #define JS_CONSTANTS_FORMFACTOR \
68 "var Planar = %1;\n"\
69 "var MediaCenter = %2;\n"\
70 "var Horizontal = %3;\n"\
71 "var Vertical = %4;\n"\
73 #define JS_CONSTANTS_LOCATION \
74 "var Floating = %1;\n"\
75 "var Desktop = %2;\n"\
76 "var FullScreen = %3;\n"\
77 "var TopEdge = %4;\n"\
78 "var BottomEdge = %5;\n"\
79 "var LeftEdge = %6;\n"\
80 "var RightEdge = %7;\n"
82 #define JS_CONSTANTS_OTHER \
83 "var size_width = 0;\n"\
84 "var size_height = 1;\n"\
85 "var point_x = 0;\n"\
86 "var point_y = 1;\n"\
87 "var rect_x = 0;\n"\
88 "var rect_y = 1;\n"\
89 "var rect_width = 2;\n"\
90 "var rect_height = 3;\n"\
91 "var margin_left = 0;\n"\
92 "var margin_top = 1;\n"\
93 "var margin_right = 2;\n"\
94 "var margin_bottom = 3;\n"\
96 #define CSS "body { font-family: %3; font-size: %4pt; color:%1; background-color:%2 }\n"
98 QString PlasmaWebApplet::m_jsConstants;
100 PlasmaWebApplet::PlasmaWebApplet(QObject *parent, const QVariantList &args)
101 : WebApplet(parent, args)
103 if (m_jsConstants.isEmpty()) {
104 m_jsConstants = JS_CONSTANTS_OTHER;
105 m_jsConstants += QString(JS_CONSTANTS_CONSTRAINT)
106 .arg(Plasma::NoConstraint)
107 .arg(Plasma::FormFactorConstraint)
108 .arg(Plasma::LocationConstraint)
109 .arg(Plasma::ScreenConstraint)
110 .arg(Plasma::SizeConstraint)
111 .arg(Plasma::ImmutableConstraint)
112 .arg(Plasma::StartupCompletedConstraint)
113 .arg(Plasma::ContextConstraint)
114 .arg(Plasma::AllConstraints);
115 m_jsConstants += QString(JS_CONSTANTS_BACKGROUND)
116 .arg(Plasma::Applet::NoBackground)
117 .arg(Plasma::Applet::StandardBackground)
118 .arg(Plasma::Applet::TranslucentBackground)
119 .arg(Plasma::Applet::DefaultBackground);
120 m_jsConstants += QString(JS_CONSTANTS_SCROLLBAR)
121 .arg(Qt::Horizontal)
122 .arg(Qt::Vertical)
123 .arg(Qt::ScrollBarAsNeeded)
124 .arg(Qt::ScrollBarAlwaysOff)
125 .arg(Qt::ScrollBarAlwaysOn);
126 m_jsConstants += QString(JS_CONSTANTS_ASPECTRATIO)
127 .arg(Plasma::InvalidAspectRatioMode)
128 .arg(Plasma::IgnoreAspectRatio)
129 .arg(Plasma::KeepAspectRatio)
130 .arg(Plasma::Square)
131 .arg(Plasma::ConstrainedSquare)
132 .arg(Plasma::FixedSize);
133 m_jsConstants += QString(JS_CONSTANTS_FORMFACTOR)
134 .arg(Plasma::Planar)
135 .arg(Plasma::MediaCenter)
136 .arg(Plasma::Horizontal)
137 .arg(Plasma::Vertical);
138 m_jsConstants += QString(JS_CONSTANTS_LOCATION)
139 .arg(Plasma::Floating)
140 .arg(Plasma::Desktop)
141 .arg(Plasma::FullScreen)
142 .arg(Plasma::TopEdge)
143 .arg(Plasma::BottomEdge)
144 .arg(Plasma::LeftEdge)
145 .arg(Plasma::RightEdge);
149 PlasmaWebApplet::~PlasmaWebApplet()
153 bool PlasmaWebApplet::init()
155 m_useDefaultSize = (applet()->contentsRect().size() == QSizeF(0, 0));
156 m_config.setConfig(applet()->config());
157 m_globalConfig.setConfig(applet()->globalConfig());
158 if (WebApplet::init()) {
159 connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
160 this, SLOT(themeChanged()));
161 makeStylesheet();
162 return true;
164 return false;
167 void PlasmaWebApplet::makeStylesheet()
169 if (m_temp.open()) {
170 KColorScheme plasmaColorTheme = KColorScheme(QPalette::Active, KColorScheme::View,
171 Plasma::Theme::defaultTheme()->colorScheme());
172 QColor textColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
173 QColor backgroundColor =
174 Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor);
175 QFont font = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont);
177 QString css = QString(CSS).arg(textColor.name())
178 .arg(backgroundColor.name())
179 .arg(font.family())
180 .arg(font.pointSize());
181 m_temp.write(css.toUtf8());
182 page()->page()->settings()->setUserStyleSheetUrl(QUrl(m_temp.fileName()));
183 m_temp.close();
187 void PlasmaWebApplet::themeChanged()
189 makeStylesheet();
190 callJsFunction("themeChanged");
193 void PlasmaWebApplet::loadFinished(bool success)
195 WebApplet::loadFinished(success);
196 if (success) {
197 page()->mainFrame()->evaluateJavaScript(m_jsConstants);
198 callJsFunction("init");
202 void PlasmaWebApplet::constraintsEvent(Plasma::Constraints constraints)
204 if (page() && constraints & Plasma::SizeConstraint) {
205 qreal left;
206 qreal top;
207 qreal right;
208 qreal bottom;
209 applet()->getContentsMargins(&left, &top, &right, &bottom);
210 page()->setPos(QPointF(left, top));
211 page()->resize(WebApplet::size() - QSizeF(left + right, top + bottom));
212 //kDebug() << WebApplet::size() << left << right << top << bottom << page()->size();
214 callJsFunction("constraintsEvent", QVariantList() << (int)constraints);
217 QVariant PlasmaWebApplet::arg(int index) const
219 return m_args[index];
222 QObject* PlasmaWebApplet::objArg(int index) const
224 return m_args[index].value<QObject*>();
227 QString PlasmaWebApplet::name() const
229 return applet()->name();
232 uint PlasmaWebApplet::id() const
234 return applet()->id();
237 int PlasmaWebApplet::formFactor() const
239 return (int)applet()->formFactor();
242 int PlasmaWebApplet::location() const
244 return (int)applet()->location();
247 QString PlasmaWebApplet::pluginName() const
249 return applet()->pluginName();
252 QString PlasmaWebApplet::icon() const
254 return applet()->icon();
257 QString PlasmaWebApplet::category() const
259 return applet()->category();
262 bool PlasmaWebApplet::shouldConserveResources() const
264 return applet()->shouldConserveResources();
267 QObject* PlasmaWebApplet::dataEngine(const QString& name)
269 QString id = QString("%1").arg(applet()->id());
270 Plasma::DataEngine *de = applet()->dataEngine(name);
271 DataEngineWrapper *wrapper = de->findChild<DataEngineWrapper*>(id);
272 if (!wrapper) {
273 wrapper = new DataEngineWrapper(de, this);
274 wrapper->setObjectName(id);
276 return wrapper;
279 QObject* PlasmaWebApplet::config()
281 return &m_config;
284 QObject* PlasmaWebApplet::globalConfig()
286 return &m_globalConfig;
289 void PlasmaWebApplet::resize(qreal w, qreal h)
291 applet()->resize(w, h);
294 void PlasmaWebApplet::setBackgroundHints(int hints)
296 applet()->setBackgroundHints((Plasma::Applet::BackgroundHints)hints);
299 void PlasmaWebApplet::setScrollBarPolicy(int orientation, int policy)
301 page()->mainFrame()->setScrollBarPolicy((Qt::Orientation)orientation,
302 (Qt::ScrollBarPolicy)policy);
305 void PlasmaWebApplet::setAspectRatioMode(int mode)
307 applet()->setAspectRatioMode((Plasma::AspectRatioMode)mode);
310 QVariant PlasmaWebApplet::callJsFunction(const QString& func, const QVariantList& args)
312 if (loaded()) {
313 m_args = args;
314 QString cmd = func + '(';
315 for(int i = 0; i < args.count(); ++i) {
316 if (i > 0) {
317 cmd += ',';
319 if (args[i].canConvert<QObject*>()) {
320 cmd += QString("window.applet.objArg(%1)").arg(i);
321 } else {
322 cmd += QString("window.applet.arg(%1)").arg(i);
325 cmd += ')';
326 //kDebug() << cmd;
327 return page()->mainFrame()->evaluateJavaScript(cmd);
329 return QVariant();
332 void PlasmaWebApplet::dataUpdated(const QString& source, const Plasma::DataEngine::Data &data)
334 m_dataEngineData.setData(data);
335 callJsFunction("dataUpdated",
336 QVariantList() << source << QVariant::fromValue((QObject*)&m_dataEngineData));
339 void PlasmaWebApplet::configChanged()
341 callJsFunction("configChanged");
344 void PlasmaWebApplet::initJsObjects()
346 QWebFrame *frame = qobject_cast<QWebFrame*>(sender());
347 Q_ASSERT(frame);
348 frame->addToJavaScriptWindowObject(QLatin1String("applet"), this);
349 frame->addToJavaScriptWindowObject(QLatin1String("plasma"), new PlasmaJs(this));
352 void PlasmaWebApplet::setDefaultSize(qreal w, qreal h)
354 if (m_useDefaultSize) {
355 applet()->resize(w, h);
359 QVariantList PlasmaWebApplet::geometry()
361 return QVariantList() << applet()->geometry().left() << applet()->geometry().top()
362 << applet()->geometry().width() << applet()->geometry().height();
365 QVariantList PlasmaWebApplet::screenRect()
367 return QVariantList() << applet()->screenRect().left() << applet()->screenRect().top()
368 << applet()->screenRect().width() << applet()->screenRect().height();
371 int PlasmaWebApplet::backgroundHints()
373 return applet()->backgroundHints();
376 int PlasmaWebApplet::aspectRatioMode()
378 return applet()->aspectRatioMode();
381 void PlasmaWebApplet::setConfigurationRequired(bool needsConfiguring)
383 // configurationRequired is protected
384 applet()->setProperty("configurationRequired", needsConfiguring);
387 void PlasmaWebApplet::setMaximumSize(qreal w, qreal h)
389 applet()->setMaximumSize(w, h);
392 void PlasmaWebApplet::setMinimumSize(qreal w, qreal h)
394 applet()->setMinimumSize(w, h);
397 void PlasmaWebApplet::setPreferredSize(qreal w, qreal h)
399 applet()->setPreferredSize(w, h);
402 QVariantList PlasmaWebApplet::maximumSize()
404 return QVariantList() << applet()->maximumSize().width() << applet()->maximumSize().height();
407 QVariantList PlasmaWebApplet::minimumSize()
409 return QVariantList() << applet()->minimumSize().width() << applet()->minimumSize().height();
412 QVariantList PlasmaWebApplet::preferredSize()
414 return QVariantList() << applet()->preferredSize().width()
415 << applet()->preferredSize().height();
418 QVariantList PlasmaWebApplet::getContentsMargins()
420 qreal left;
421 qreal top;
422 qreal right;
423 qreal bottom;
424 applet()->getContentsMargins(&left, &top, &right, &bottom);
425 return QVariantList() << left << top << right << bottom;
428 void PlasmaWebApplet::setGeometry(qreal x, qreal y, qreal w, qreal h)
430 applet()->setGeometry(x, y, w, h);
433 void PlasmaWebApplet::setPos(qreal x, qreal y)
435 applet()->setPos(x, y);
438 QVariantList PlasmaWebApplet::pos()
440 return QVariantList() << applet()->pos().x() << applet()->pos().y();
443 QVariantList PlasmaWebApplet::size()
445 return QVariantList() << applet()->size().width() << applet()->size().height();
448 #include "plasmawebapplet.moc"