2009-05-13 02:55:25 +02:00
|
|
|
/*
|
2010-02-22 12:33:21 +01:00
|
|
|
* Copyright 2007-2008,2010 Richard J. Moore <rich@kde.org>
|
2009-12-16 23:05:46 +01:00
|
|
|
* Copyright 2009 Aaron J. Seigo <aseigo@kde.org>
|
2009-05-13 02:55:25 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "simplejavascriptapplet.h"
|
|
|
|
|
2009-11-14 02:09:44 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <QFile>
|
2010-04-02 03:03:55 +02:00
|
|
|
#include <QKeyEvent>
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <QGraphicsLayout>
|
2010-04-01 02:46:44 +02:00
|
|
|
#include <QGraphicsSceneHoverEvent>
|
2010-04-02 03:03:55 +02:00
|
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
#include <QGraphicsSceneWheelEvent>
|
2009-12-06 16:34:41 +01:00
|
|
|
#include <QParallelAnimationGroup>
|
2010-02-13 01:53:46 +01:00
|
|
|
#include <QPauseAnimation>
|
2009-12-06 16:34:41 +01:00
|
|
|
#include <QSequentialAnimationGroup>
|
2010-04-02 03:03:55 +02:00
|
|
|
#include <QUiLoader>
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <QWidget>
|
2010-02-22 12:33:21 +01:00
|
|
|
#include <QScriptEngine>
|
2009-05-13 02:55:25 +02:00
|
|
|
|
2009-11-28 01:42:22 +01:00
|
|
|
#include <KConfigGroup>
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <KDebug>
|
2009-11-14 08:28:16 +01:00
|
|
|
#include <KFileDialog>
|
2009-11-11 03:09:25 +01:00
|
|
|
#include <KIcon>
|
2009-12-16 23:05:46 +01:00
|
|
|
#include <KIO/Job>
|
2009-11-28 01:42:22 +01:00
|
|
|
#include <KMimeType>
|
|
|
|
#include <KShell>
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <KStandardDirs>
|
2009-11-28 01:42:22 +01:00
|
|
|
#include <KLocale>
|
|
|
|
#include <KRun>
|
2009-05-13 02:55:25 +02:00
|
|
|
|
2009-12-03 20:32:42 +01:00
|
|
|
#include <Plasma/Animation>
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <Plasma/Applet>
|
2010-01-20 22:40:55 +01:00
|
|
|
#include <Plasma/Extender>
|
|
|
|
#include <Plasma/ExtenderItem>
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <Plasma/FrameSvg>
|
|
|
|
#include <Plasma/Package>
|
2009-12-16 23:05:46 +01:00
|
|
|
#include <Plasma/PopupApplet>
|
2010-01-20 22:40:55 +01:00
|
|
|
#include <Plasma/Svg>
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <Plasma/VideoWidget>
|
|
|
|
|
2010-01-07 03:04:59 +01:00
|
|
|
#include "appletauthorization.h"
|
2010-08-06 04:01:40 +02:00
|
|
|
#include "appletinterface.h"
|
2010-01-07 03:04:59 +01:00
|
|
|
#include "scriptenv.h"
|
2009-12-14 22:54:23 +01:00
|
|
|
#include "simplebindings/animationgroup.h"
|
2010-09-25 00:51:16 +02:00
|
|
|
#include "simplebindings/bytearrayclass.h"
|
2009-12-16 23:05:46 +01:00
|
|
|
#include "simplebindings/dataengine.h"
|
2010-03-11 00:10:33 +01:00
|
|
|
#include "simplebindings/dataenginereceiver.h"
|
2009-12-16 23:05:46 +01:00
|
|
|
#include "simplebindings/i18n.h"
|
2010-09-25 00:51:16 +02:00
|
|
|
#include "themedsvg.h"
|
2009-05-13 02:55:25 +02:00
|
|
|
|
|
|
|
using namespace Plasma;
|
|
|
|
|
2010-04-01 02:46:44 +02:00
|
|
|
Q_DECLARE_METATYPE(QKeyEvent*)
|
2009-05-13 02:55:25 +02:00
|
|
|
Q_DECLARE_METATYPE(QPainter*)
|
|
|
|
Q_DECLARE_METATYPE(QStyleOptionGraphicsItem*)
|
2010-04-01 02:46:44 +02:00
|
|
|
Q_DECLARE_METATYPE(QGraphicsSceneHoverEvent *)
|
|
|
|
Q_DECLARE_METATYPE(QGraphicsSceneMouseEvent *)
|
|
|
|
Q_DECLARE_METATYPE(QGraphicsSceneWheelEvent *)
|
2009-05-13 02:55:25 +02:00
|
|
|
|
2009-11-14 08:28:16 +01:00
|
|
|
QScriptValue constructColorClass(QScriptEngine *engine);
|
2010-01-13 00:32:56 +01:00
|
|
|
QScriptValue constructEasingCurveClass(QScriptEngine *engine);
|
2009-10-21 01:15:33 +02:00
|
|
|
QScriptValue constructFontClass(QScriptEngine *engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
QScriptValue constructGraphicsItemClass(QScriptEngine *engine);
|
2010-02-24 01:35:37 +01:00
|
|
|
QScriptValue constructIconClass(QScriptEngine *engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
QScriptValue constructKUrlClass(QScriptEngine *engine);
|
2009-10-21 01:15:33 +02:00
|
|
|
QScriptValue constructLinearLayoutClass(QScriptEngine *engine);
|
2009-11-29 05:17:30 +01:00
|
|
|
QScriptValue constructGridLayoutClass(QScriptEngine *engine);
|
2009-11-03 04:06:04 +01:00
|
|
|
QScriptValue constructAnchorLayoutClass(QScriptEngine *engine);
|
2009-10-21 01:15:33 +02:00
|
|
|
QScriptValue constructPainterClass(QScriptEngine *engine);
|
2009-11-27 21:29:14 +01:00
|
|
|
QScriptValue constructPenClass(QScriptEngine *engine);
|
2009-10-21 01:15:33 +02:00
|
|
|
QScriptValue constructQPixmapClass(QScriptEngine *engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
QScriptValue constructQPointClass(QScriptEngine *engine);
|
2009-10-21 01:15:33 +02:00
|
|
|
QScriptValue constructQRectFClass(QScriptEngine *engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
QScriptValue constructQSizeFClass(QScriptEngine *engine);
|
2010-03-25 00:56:07 +01:00
|
|
|
QScriptValue constructQSizePolicyClass(QScriptEngine *engine);
|
2009-10-21 01:15:33 +02:00
|
|
|
QScriptValue constructTimerClass(QScriptEngine *engine);
|
2010-12-15 12:15:26 +01:00
|
|
|
void registerNonGuiMetaTypes(QScriptEngine *engine);
|
2009-12-16 23:05:46 +01:00
|
|
|
void registerSimpleAppletMetaTypes(QScriptEngine *engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
|
|
|
|
KSharedPtr<UiLoader> SimpleJavaScriptApplet::s_widgetLoader;
|
2009-10-21 04:26:44 +02:00
|
|
|
QHash<QString, Plasma::Animator::Animation> SimpleJavaScriptApplet::s_animationDefs;
|
2009-05-13 02:55:25 +02:00
|
|
|
|
|
|
|
SimpleJavaScriptApplet::SimpleJavaScriptApplet(QObject *parent, const QVariantList &args)
|
2010-10-13 18:12:43 +02:00
|
|
|
: AbstractJsAppletScript(parent)
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
2010-02-22 12:33:21 +01:00
|
|
|
Q_UNUSED(args);
|
2009-05-13 02:55:25 +02:00
|
|
|
// kDebug() << "Script applet launched, args" << applet()->startupArguments();
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
// TODO this will be set to the engine we get from QML
|
|
|
|
m_engine = new QScriptEngine(this);
|
|
|
|
m_env = new ScriptEnv(this, m_engine);
|
2010-09-03 23:59:55 +02:00
|
|
|
connect(m_env, SIGNAL(reportError(ScriptEnv*,bool)), this, SLOT(reportError(ScriptEnv*,bool)));
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SimpleJavaScriptApplet::~SimpleJavaScriptApplet()
|
|
|
|
{
|
2010-09-04 02:04:40 +02:00
|
|
|
delete m_interface;
|
2009-05-13 02:55:25 +02:00
|
|
|
if (s_widgetLoader.count() == 1) {
|
|
|
|
s_widgetLoader.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
void SimpleJavaScriptApplet::reportError(ScriptEnv *env, bool fatal)
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
2010-02-22 12:33:21 +01:00
|
|
|
const QScriptValue error = env->engine()->uncaughtException();
|
2010-02-16 18:48:54 +01:00
|
|
|
QString file = error.property("fileName").toString();
|
2010-09-04 02:04:40 +02:00
|
|
|
file.remove(package()->path());
|
2010-02-16 18:48:54 +01:00
|
|
|
|
|
|
|
const QString failureMsg = i18n("Error in %1 on line %2.<br><br>%3",
|
|
|
|
file, error.property("lineNumber").toString(),
|
|
|
|
error.toString());
|
2010-09-04 02:04:40 +02:00
|
|
|
if (fatal) {
|
|
|
|
setFailedToLaunch(true, failureMsg);
|
2009-11-11 03:09:25 +01:00
|
|
|
} else {
|
2010-09-04 02:04:40 +02:00
|
|
|
showMessage(KIcon("dialog-error"), failureMsg, Plasma::ButtonOk);
|
2009-11-11 03:09:25 +01:00
|
|
|
}
|
|
|
|
|
2010-09-04 02:04:40 +02:00
|
|
|
kDebug() << failureMsg;
|
2010-02-22 12:33:21 +01:00
|
|
|
kDebug() << env->engine()->uncaughtExceptionBacktrace();
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleJavaScriptApplet::configChanged()
|
|
|
|
{
|
2010-08-04 05:53:41 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
2010-09-23 14:13:08 +02:00
|
|
|
if (!env || !env->callEventListeners("configchanged")) {
|
2010-09-03 23:00:25 +02:00
|
|
|
callPlasmoidFunction("configChanged", QScriptValueList(), env);
|
2010-04-01 02:46:44 +02:00
|
|
|
}
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleJavaScriptApplet::dataUpdated(const QString &name, const DataEngine::Data &data)
|
|
|
|
{
|
2010-08-04 05:53:41 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
2010-09-03 23:00:25 +02:00
|
|
|
if (!env) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-05 15:32:14 +02:00
|
|
|
QScriptValueList args;
|
|
|
|
args << m_engine->toScriptValue(name) << m_engine->toScriptValue(data);
|
|
|
|
|
|
|
|
if (!env->callEventListeners("dataUpdated", args)) {
|
2010-09-03 23:00:25 +02:00
|
|
|
callPlasmoidFunction("dataUpdated", args, env);
|
2010-04-01 02:46:44 +02:00
|
|
|
}
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2010-01-20 22:40:55 +01:00
|
|
|
void SimpleJavaScriptApplet::extenderItemRestored(Plasma::ExtenderItem* item)
|
|
|
|
{
|
2010-09-03 23:00:25 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
|
|
|
if (!env) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-20 22:40:55 +01:00
|
|
|
QScriptValueList args;
|
|
|
|
args << m_engine->newQObject(item, QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject);
|
2010-09-03 23:00:25 +02:00
|
|
|
|
2010-10-05 15:30:38 +02:00
|
|
|
if (!env->callEventListeners("initExtenderItem", args)) {
|
2010-09-03 23:00:25 +02:00
|
|
|
callPlasmoidFunction("initExtenderItem", args, env);
|
2010-04-01 02:46:44 +02:00
|
|
|
}
|
2010-01-20 22:40:55 +01:00
|
|
|
}
|
|
|
|
|
2010-03-09 06:18:52 +01:00
|
|
|
void SimpleJavaScriptApplet::activate()
|
|
|
|
{
|
2010-08-04 05:53:41 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
|
|
|
if (!env || !env->callEventListeners("activate")) {
|
2010-09-03 23:00:25 +02:00
|
|
|
callPlasmoidFunction("activate", QScriptValueList(), env);
|
2010-04-01 02:46:44 +02:00
|
|
|
}
|
2010-03-09 06:18:52 +01:00
|
|
|
}
|
|
|
|
|
2010-02-28 17:11:48 +01:00
|
|
|
void SimpleJavaScriptApplet::popupEvent(bool popped)
|
|
|
|
{
|
2010-09-03 23:00:25 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
|
|
|
if (!env) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-28 17:11:48 +01:00
|
|
|
QScriptValueList args;
|
|
|
|
args << popped;
|
2010-09-03 23:00:25 +02:00
|
|
|
|
|
|
|
if (!env->callEventListeners("popupEvent", args)) {
|
|
|
|
callPlasmoidFunction("popupEvent", args, env);
|
2010-04-01 02:46:44 +02:00
|
|
|
}
|
2010-02-28 17:11:48 +01:00
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
void SimpleJavaScriptApplet::executeAction(const QString &name)
|
|
|
|
{
|
2010-08-04 05:53:41 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
2010-09-03 23:00:25 +02:00
|
|
|
if (!env) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString func("action_" + name);
|
|
|
|
if (!env->callEventListeners(func)) {
|
|
|
|
callPlasmoidFunction(func, QScriptValueList(), env);
|
2010-04-01 02:46:44 +02:00
|
|
|
}
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleJavaScriptApplet::paintInterface(QPainter *p, const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
|
|
|
|
{
|
2010-09-03 23:00:25 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
|
|
|
if (!env) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
QScriptValueList args;
|
|
|
|
args << m_engine->toScriptValue(p);
|
|
|
|
args << m_engine->toScriptValue(const_cast<QStyleOptionGraphicsItem*>(option));
|
2010-02-16 01:10:30 +01:00
|
|
|
args << m_engine->toScriptValue(QRectF(contentsRect));
|
2010-04-01 02:46:44 +02:00
|
|
|
|
2010-09-03 23:00:25 +02:00
|
|
|
if (!env->callEventListeners("paintInterface")) {
|
|
|
|
callPlasmoidFunction("paintInterface", args, env);
|
2010-04-01 02:46:44 +02:00
|
|
|
}
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QList<QAction*> SimpleJavaScriptApplet::contextualActions()
|
|
|
|
{
|
|
|
|
return m_interface->contextualActions();
|
|
|
|
}
|
|
|
|
|
2010-09-03 23:00:25 +02:00
|
|
|
void SimpleJavaScriptApplet::callPlasmoidFunction(const QString &functionName, const QScriptValueList &args, ScriptEnv *env)
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
2010-09-03 23:00:25 +02:00
|
|
|
if (!env) {
|
|
|
|
env = ScriptEnv::findScriptEnv(m_engine);
|
|
|
|
}
|
|
|
|
|
2010-08-04 05:53:41 +02:00
|
|
|
if (env) {
|
2010-09-03 23:00:25 +02:00
|
|
|
QScriptValue func = m_self.property(functionName);
|
2010-08-04 05:53:41 +02:00
|
|
|
env->callFunction(func, args, m_self);
|
|
|
|
}
|
2010-04-01 02:46:44 +02:00
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
void SimpleJavaScriptApplet::constraintsEvent(Plasma::Constraints constraints)
|
|
|
|
{
|
2010-08-04 05:53:41 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
2010-09-03 23:00:25 +02:00
|
|
|
if (!env) {
|
|
|
|
return;
|
|
|
|
}
|
2009-05-13 02:55:25 +02:00
|
|
|
|
|
|
|
if (constraints & Plasma::FormFactorConstraint) {
|
2010-09-03 23:00:25 +02:00
|
|
|
if (!env->callEventListeners("formFactorChanged")) {
|
|
|
|
callPlasmoidFunction("formFactorChanged", QScriptValueList(), env);
|
2010-04-01 02:46:44 +02:00
|
|
|
}
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (constraints & Plasma::LocationConstraint) {
|
2010-09-03 23:00:25 +02:00
|
|
|
if (!env->callEventListeners("locationChanged")) {
|
|
|
|
callPlasmoidFunction("locationChanged", QScriptValueList(), env);
|
2010-04-01 02:46:44 +02:00
|
|
|
}
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (constraints & Plasma::ContextConstraint) {
|
2010-09-03 23:00:25 +02:00
|
|
|
if (!env->callEventListeners("currentActivityChanged")) {
|
|
|
|
callPlasmoidFunction("currentActivityChanged", QScriptValueList(), env);
|
2010-04-01 02:46:44 +02:00
|
|
|
}
|
2009-11-13 23:21:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (constraints & Plasma::SizeConstraint) {
|
2010-08-04 05:53:41 +02:00
|
|
|
if (!env || !env->callEventListeners("sizeChanged")) {
|
2010-09-03 23:00:25 +02:00
|
|
|
callPlasmoidFunction("sizeChanged", QScriptValueList(), env);
|
2010-04-01 02:46:44 +02:00
|
|
|
}
|
2009-11-13 23:21:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (constraints & Plasma::ImmutableConstraint) {
|
2010-09-03 23:00:25 +02:00
|
|
|
if (!env->callEventListeners("immutabilityChanged")) {
|
|
|
|
callPlasmoidFunction("immutabilityChanged", QScriptValueList(), env);
|
2010-04-01 02:46:44 +02:00
|
|
|
}
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-14 06:19:19 +01:00
|
|
|
bool SimpleJavaScriptApplet::include(const QString &path)
|
|
|
|
{
|
2010-02-22 12:33:21 +01:00
|
|
|
return m_env->include(path);
|
2009-11-14 06:19:19 +01:00
|
|
|
}
|
|
|
|
|
2009-10-21 04:26:44 +02:00
|
|
|
void SimpleJavaScriptApplet::populateAnimationsHash()
|
|
|
|
{
|
|
|
|
if (s_animationDefs.isEmpty()) {
|
|
|
|
s_animationDefs.insert("fade", Plasma::Animator::FadeAnimation);
|
2009-12-08 10:24:24 +01:00
|
|
|
s_animationDefs.insert("geometry", Plasma::Animator::GeometryAnimation);
|
2009-10-21 04:26:44 +02:00
|
|
|
s_animationDefs.insert("grow", Plasma::Animator::GrowAnimation);
|
|
|
|
s_animationDefs.insert("pulse", Plasma::Animator::PulseAnimation);
|
|
|
|
s_animationDefs.insert("rotate", Plasma::Animator::RotationAnimation);
|
|
|
|
s_animationDefs.insert("rotateStacked", Plasma::Animator::RotationStackedAnimation);
|
|
|
|
s_animationDefs.insert("slide", Plasma::Animator::SlideAnimation);
|
2009-12-08 10:24:24 +01:00
|
|
|
s_animationDefs.insert("zoom", Plasma::Animator::ZoomAnimation);
|
2009-10-21 04:26:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
bool SimpleJavaScriptApplet::init()
|
|
|
|
{
|
2010-01-20 22:40:55 +01:00
|
|
|
connect(applet(), SIGNAL(extenderItemRestored(Plasma::ExtenderItem*)),
|
|
|
|
this, SLOT(extenderItemRestored(Plasma::ExtenderItem*)));
|
2010-03-09 06:18:52 +01:00
|
|
|
connect(applet(), SIGNAL(activate()),
|
|
|
|
this, SLOT(activate()));
|
2009-05-13 02:55:25 +02:00
|
|
|
setupObjects();
|
|
|
|
|
2010-01-07 03:04:59 +01:00
|
|
|
AppletAuthorization auth(this);
|
2010-02-22 12:33:21 +01:00
|
|
|
if (!m_env->importExtensions(description(), m_self, auth)) {
|
2009-11-11 03:09:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
kDebug() << "ScriptName:" << applet()->name();
|
|
|
|
kDebug() << "ScriptCategory:" << applet()->category();
|
2010-04-01 02:46:44 +02:00
|
|
|
applet()->installEventFilter(this);
|
2010-02-22 12:33:21 +01:00
|
|
|
return m_env->include(mainScript());
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2010-04-02 03:03:55 +02:00
|
|
|
QScriptValue SimpleJavaScriptApplet::createKeyEventObject(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
QScriptValue v = m_env->engine()->newObject();
|
|
|
|
v.setProperty("count", event->count());
|
|
|
|
v.setProperty("key", event->key());
|
|
|
|
v.setProperty("modifiers", static_cast<int>(event->modifiers()));
|
|
|
|
v.setProperty("text", event->text());
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue SimpleJavaScriptApplet::createHoverEventObject(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
QScriptEngine *engine = m_env->engine();
|
|
|
|
QScriptValue v = engine->newObject();
|
|
|
|
v.setProperty("pos", engine->toScriptValue<QPoint>(event->pos().toPoint()));
|
|
|
|
v.setProperty("scenePos", engine->toScriptValue<QPoint>(event->scenePos().toPoint()));
|
|
|
|
v.setProperty("screenPos", engine->toScriptValue<QPoint>(event->screenPos()));
|
|
|
|
v.setProperty("lastPos", engine->toScriptValue<QPoint>(event->lastPos().toPoint()));
|
|
|
|
v.setProperty("lastScenePos", engine->toScriptValue<QPoint>(event->lastScenePos().toPoint()));
|
|
|
|
v.setProperty("lastScreenPos", engine->toScriptValue<QPoint>(event->lastScreenPos()));
|
|
|
|
v.setProperty("modifiers", static_cast<int>(event->modifiers()));
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue SimpleJavaScriptApplet::createMouseEventObject(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
QScriptEngine *engine = m_env->engine();
|
|
|
|
QScriptValue v = engine->newObject();
|
|
|
|
v.setProperty("button", static_cast<int>(event->button()));
|
|
|
|
v.setProperty("buttons", static_cast<int>(event->buttons()));
|
|
|
|
v.setProperty("modifiers", static_cast<int>(event->modifiers()));
|
|
|
|
v.setProperty("pos", engine->toScriptValue<QPoint>(event->pos().toPoint()));
|
|
|
|
v.setProperty("scenePos", engine->toScriptValue<QPoint>(event->scenePos().toPoint()));
|
|
|
|
v.setProperty("screenPos", engine->toScriptValue<QPoint>(event->screenPos()));
|
|
|
|
v.setProperty("lastPos", engine->toScriptValue<QPoint>(event->lastPos().toPoint()));
|
|
|
|
v.setProperty("lastScenePos", engine->toScriptValue<QPoint>(event->lastScenePos().toPoint()));
|
|
|
|
v.setProperty("lastScreenPos", engine->toScriptValue<QPoint>(event->lastScreenPos()));
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue SimpleJavaScriptApplet::createWheelEventObject(QGraphicsSceneWheelEvent *event)
|
|
|
|
{
|
|
|
|
QScriptEngine *engine = m_env->engine();
|
|
|
|
QScriptValue v = engine->newObject();
|
|
|
|
v.setProperty("delta", event->delta());
|
|
|
|
v.setProperty("buttons", static_cast<int>(event->buttons()));
|
|
|
|
v.setProperty("modifiers", static_cast<int>(event->modifiers()));
|
|
|
|
v.setProperty("orientation", static_cast<int>(event->orientation()));
|
|
|
|
v.setProperty("pos", engine->toScriptValue<QPoint>(event->pos().toPoint()));
|
|
|
|
v.setProperty("scenePos", engine->toScriptValue<QPoint>(event->scenePos().toPoint()));
|
|
|
|
v.setProperty("screenPos", engine->toScriptValue<QPoint>(event->screenPos()));
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2010-04-01 02:46:44 +02:00
|
|
|
bool SimpleJavaScriptApplet::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
{
|
|
|
|
switch (event->type()) {
|
|
|
|
case QEvent::KeyPress: {
|
2010-08-04 05:53:41 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
2010-09-23 00:22:37 +02:00
|
|
|
if (env && env->hasEventListeners("keypress")) {
|
2010-04-01 02:46:44 +02:00
|
|
|
QScriptValueList args;
|
2010-04-02 03:03:55 +02:00
|
|
|
args << createKeyEventObject(static_cast<QKeyEvent *>(event));
|
2010-08-04 05:53:41 +02:00
|
|
|
env->callEventListeners("keypress", args);
|
2010-04-01 02:46:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case QEvent::KeyRelease: {
|
2010-08-04 05:53:41 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
2010-09-23 00:22:37 +02:00
|
|
|
if (env && env->hasEventListeners("keyrelease")) {
|
2010-04-01 02:46:44 +02:00
|
|
|
QScriptValueList args;
|
2010-04-02 03:03:55 +02:00
|
|
|
args << createKeyEventObject(static_cast<QKeyEvent *>(event));
|
2010-08-04 05:53:41 +02:00
|
|
|
env->callEventListeners("keyrelease", args);
|
2010-04-01 02:46:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::GraphicsSceneHoverEnter: {
|
2010-08-04 05:53:41 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
2010-09-23 00:22:37 +02:00
|
|
|
if (env && env->hasEventListeners("hoverenter")) {
|
2010-04-01 02:46:44 +02:00
|
|
|
QScriptValueList args;
|
2010-04-02 03:03:55 +02:00
|
|
|
args << createHoverEventObject(static_cast<QGraphicsSceneHoverEvent *>(event));
|
2010-08-04 05:53:41 +02:00
|
|
|
env->callEventListeners("hoverenter", args);
|
2010-04-01 02:46:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::GraphicsSceneHoverLeave: {
|
2010-08-04 05:53:41 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
2010-09-23 00:22:37 +02:00
|
|
|
if (env && env->hasEventListeners("hoverleave")) {
|
2010-04-01 02:46:44 +02:00
|
|
|
QScriptValueList args;
|
2010-04-02 03:03:55 +02:00
|
|
|
args << createHoverEventObject(static_cast<QGraphicsSceneHoverEvent *>(event));
|
2010-08-04 05:53:41 +02:00
|
|
|
env->callEventListeners("hoverleave", args);
|
2010-04-01 02:46:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::GraphicsSceneHoverMove: {
|
2010-08-04 05:53:41 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
2010-09-23 00:22:37 +02:00
|
|
|
if (env && env->hasEventListeners("hovermove")) {
|
2010-04-01 02:46:44 +02:00
|
|
|
QScriptValueList args;
|
2010-04-02 03:03:55 +02:00
|
|
|
args << createHoverEventObject(static_cast<QGraphicsSceneHoverEvent *>(event));
|
2010-08-04 05:53:41 +02:00
|
|
|
env->callEventListeners("hovermove", args);
|
2010-04-01 02:46:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::GraphicsSceneMousePress: {
|
2010-08-04 05:53:41 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
2010-09-23 00:22:37 +02:00
|
|
|
if (env && env->hasEventListeners("mousepress")) {
|
2010-04-01 02:46:44 +02:00
|
|
|
QScriptValueList args;
|
2010-04-02 03:03:55 +02:00
|
|
|
args << createMouseEventObject(static_cast<QGraphicsSceneMouseEvent *>(event));
|
2010-08-04 05:53:41 +02:00
|
|
|
env->callEventListeners("mousepress", args);
|
2010-04-01 02:46:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::GraphicsSceneMouseRelease: {
|
2010-08-04 05:53:41 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
2010-09-23 00:22:37 +02:00
|
|
|
if (env && env->hasEventListeners("mouserelease")) {
|
2010-04-01 02:46:44 +02:00
|
|
|
QScriptValueList args;
|
2010-04-02 03:03:55 +02:00
|
|
|
args << createMouseEventObject(static_cast<QGraphicsSceneMouseEvent *>(event));
|
2010-08-04 05:53:41 +02:00
|
|
|
env->callEventListeners("mouserelease", args);
|
2010-04-01 02:46:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::GraphicsSceneMouseMove: {
|
2010-08-04 05:53:41 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
2010-09-23 00:22:37 +02:00
|
|
|
if (env && env->hasEventListeners("mousemove")) {
|
2010-04-01 02:46:44 +02:00
|
|
|
QScriptValueList args;
|
2010-04-02 03:03:55 +02:00
|
|
|
args << createMouseEventObject(static_cast<QGraphicsSceneMouseEvent *>(event));
|
2010-08-04 05:53:41 +02:00
|
|
|
env->callEventListeners("mousemove", args);
|
2010-04-01 02:46:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::GraphicsSceneMouseDoubleClick: {
|
2010-08-04 05:53:41 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
2010-09-23 00:22:37 +02:00
|
|
|
if (env && env->hasEventListeners("mousedoubleclick")) {
|
2010-04-01 02:46:44 +02:00
|
|
|
QScriptValueList args;
|
2010-04-02 03:03:55 +02:00
|
|
|
args << createMouseEventObject(static_cast<QGraphicsSceneMouseEvent *>(event));
|
2010-08-04 05:53:41 +02:00
|
|
|
env->callEventListeners("mousedoubleclick", args);
|
2010-04-01 02:46:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::GraphicsSceneWheel: {
|
2010-08-04 05:53:41 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
2010-09-23 00:22:37 +02:00
|
|
|
if (env && env->hasEventListeners("wheel")) {
|
2010-04-01 02:46:44 +02:00
|
|
|
QScriptValueList args;
|
2010-04-02 03:03:55 +02:00
|
|
|
args << createWheelEventObject(static_cast<QGraphicsSceneWheelEvent *>(event));
|
2010-08-04 05:53:41 +02:00
|
|
|
env->callEventListeners("wheel", args);
|
2010-04-01 02:46:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Plasma::AppletScript::eventFilter(watched, event);
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
void SimpleJavaScriptApplet::setupObjects()
|
|
|
|
{
|
|
|
|
QScriptValue global = m_engine->globalObject();
|
|
|
|
|
2009-10-21 04:26:44 +02:00
|
|
|
// Bindings for animations
|
|
|
|
global.setProperty("animation", m_engine->newFunction(SimpleJavaScriptApplet::animation));
|
2009-10-21 09:53:49 +02:00
|
|
|
global.setProperty("AnimationGroup", m_engine->newFunction(SimpleJavaScriptApplet::animationGroup));
|
2009-12-06 16:34:41 +01:00
|
|
|
global.setProperty("ParallelAnimationGroup", m_engine->newFunction(SimpleJavaScriptApplet::parallelAnimationGroup));
|
2009-10-21 04:26:44 +02:00
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
// Bindings for data engine
|
|
|
|
|
2009-07-04 06:30:57 +02:00
|
|
|
bindI18N(m_engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
global.setProperty("dataEngine", m_engine->newFunction(SimpleJavaScriptApplet::dataEngine));
|
|
|
|
global.setProperty("service", m_engine->newFunction(SimpleJavaScriptApplet::service));
|
2010-10-24 14:17:22 +02:00
|
|
|
global.setProperty("loadService", m_engine->newFunction(SimpleJavaScriptApplet::loadService));
|
2009-05-13 02:55:25 +02:00
|
|
|
|
|
|
|
// Expose applet interface
|
2009-12-16 23:05:46 +01:00
|
|
|
const bool isPopupApplet = qobject_cast<Plasma::PopupApplet *>(applet());
|
|
|
|
m_interface = isPopupApplet ? new PopupAppletInterface(this) : new AppletInterface(this);
|
2009-05-13 02:55:25 +02:00
|
|
|
m_self = m_engine->newQObject(m_interface);
|
2010-09-03 23:31:37 +02:00
|
|
|
m_env->addMainObjectProperties(m_self);
|
2009-05-13 02:55:25 +02:00
|
|
|
m_self.setScope(global);
|
|
|
|
global.setProperty("plasmoid", m_self);
|
|
|
|
|
2010-02-28 17:11:48 +01:00
|
|
|
if (isPopupApplet) {
|
|
|
|
connect(applet(), SIGNAL(popupEvent(bool)), this, SLOT(popupEvent(bool)));
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
QScriptValue args = m_engine->newArray();
|
|
|
|
int i = 0;
|
|
|
|
foreach (const QVariant &arg, applet()->startupArguments()) {
|
2010-10-13 20:08:07 +02:00
|
|
|
args.setProperty(i, m_engine->newVariant(arg));
|
2009-05-13 02:55:25 +02:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
global.setProperty("startupArguments", args);
|
|
|
|
|
2010-03-25 00:31:08 +01:00
|
|
|
ScriptEnv::registerEnums(global, AppletInterface::staticMetaObject);
|
2009-05-13 02:55:25 +02:00
|
|
|
|
|
|
|
// Add a global loadui method for ui files
|
|
|
|
QScriptValue fun = m_engine->newFunction(SimpleJavaScriptApplet::loadui);
|
|
|
|
global.setProperty("loadui", fun);
|
|
|
|
|
|
|
|
// Work around bug in 4.3.0
|
|
|
|
qMetaTypeId<QVariant>();
|
|
|
|
|
2009-10-21 01:31:12 +02:00
|
|
|
// Add stuff from Qt
|
2009-05-13 02:55:25 +02:00
|
|
|
global.setProperty("QPainter", constructPainterClass(m_engine));
|
|
|
|
global.setProperty("QGraphicsItem", constructGraphicsItemClass(m_engine));
|
2010-02-24 01:35:37 +01:00
|
|
|
global.setProperty("QIcon", constructIconClass(m_engine));
|
2009-05-13 02:55:25 +02:00
|
|
|
global.setProperty("QTimer", constructTimerClass(m_engine));
|
|
|
|
global.setProperty("QFont", constructFontClass(m_engine));
|
2009-11-14 08:28:16 +01:00
|
|
|
global.setProperty("QColor", constructColorClass(m_engine));
|
2010-01-13 00:32:56 +01:00
|
|
|
global.setProperty("QEasingCurve", constructEasingCurveClass(m_engine));
|
2009-05-13 02:55:25 +02:00
|
|
|
global.setProperty("QRectF", constructQRectFClass(m_engine));
|
2009-11-27 21:29:14 +01:00
|
|
|
global.setProperty("QPen", constructPenClass(m_engine));
|
2009-10-21 01:31:12 +02:00
|
|
|
global.setProperty("QPixmap", constructQPixmapClass(m_engine));
|
2009-05-13 02:55:25 +02:00
|
|
|
global.setProperty("QSizeF", constructQSizeFClass(m_engine));
|
2010-03-25 00:56:07 +01:00
|
|
|
global.setProperty("QSizePolicy", constructQSizePolicyClass(m_engine));
|
2009-05-13 02:55:25 +02:00
|
|
|
global.setProperty("QPoint", constructQPointClass(m_engine));
|
|
|
|
global.setProperty("LinearLayout", constructLinearLayoutClass(m_engine));
|
2009-11-29 05:17:30 +01:00
|
|
|
global.setProperty("GridLayout", constructGridLayoutClass(m_engine));
|
2009-11-03 04:06:04 +01:00
|
|
|
global.setProperty("AnchorLayout", constructAnchorLayoutClass(m_engine));
|
2009-12-16 23:05:46 +01:00
|
|
|
ByteArrayClass *baClass = new ByteArrayClass(m_engine);
|
|
|
|
global.setProperty("ByteArray", baClass->constructor());
|
2009-10-21 01:31:12 +02:00
|
|
|
|
|
|
|
// Add stuff from KDE libs
|
2009-11-28 01:42:22 +01:00
|
|
|
qScriptRegisterSequenceMetaType<KUrl::List>(m_engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
global.setProperty("Url", constructKUrlClass(m_engine));
|
|
|
|
|
2009-10-21 01:31:12 +02:00
|
|
|
// Add stuff from Plasma
|
|
|
|
global.setProperty("PlasmaSvg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaSvg));
|
|
|
|
global.setProperty("PlasmaFrameSvg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaFrameSvg));
|
2009-11-27 03:21:49 +01:00
|
|
|
global.setProperty("Svg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaSvg));
|
|
|
|
global.setProperty("FrameSvg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaFrameSvg));
|
2010-01-20 22:40:55 +01:00
|
|
|
global.setProperty("ExtenderItem", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaExtenderItem));
|
2009-10-21 01:31:12 +02:00
|
|
|
|
2010-12-15 12:15:26 +01:00
|
|
|
registerNonGuiMetaTypes(m_engine);
|
2009-12-16 23:05:46 +01:00
|
|
|
registerSimpleAppletMetaTypes(m_engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
installWidgets(m_engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue SimpleJavaScriptApplet::dataEngine(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() != 1) {
|
|
|
|
return context->throwError(i18n("dataEngine() takes one argument"));
|
|
|
|
}
|
|
|
|
|
2010-08-06 04:01:40 +02:00
|
|
|
AppletInterface *interface = AppletInterface::extract(engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
if (!interface) {
|
|
|
|
return context->throwError(i18n("Could not extract the Applet"));
|
|
|
|
}
|
|
|
|
|
2010-03-11 00:10:33 +01:00
|
|
|
const QString dataEngineName = context->argument(0).toString();
|
|
|
|
DataEngine *dataEngine = interface->dataEngine(dataEngineName);
|
|
|
|
QScriptValue v = engine->newQObject(dataEngine, QScriptEngine::QtOwnership, QScriptEngine::PreferExistingWrapperObject);
|
|
|
|
v.setProperty("connectSource", engine->newFunction(DataEngineReceiver::connectSource));
|
|
|
|
v.setProperty("disconnectSource", engine->newFunction(DataEngineReceiver::disconnectSource));
|
|
|
|
return v;
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue SimpleJavaScriptApplet::service(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() != 2) {
|
|
|
|
return context->throwError(i18n("service() takes two arguments"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString dataEngine = context->argument(0).toString();
|
|
|
|
|
2010-08-06 04:01:40 +02:00
|
|
|
AppletInterface *interface = AppletInterface::extract(engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
if (!interface) {
|
|
|
|
return context->throwError(i18n("Could not extract the Applet"));
|
|
|
|
}
|
|
|
|
|
|
|
|
DataEngine *data = interface->dataEngine(dataEngine);
|
|
|
|
QString source = context->argument(1).toString();
|
|
|
|
Service *service = data->serviceForSource(source);
|
|
|
|
//kDebug( )<< "lets try to get" << source << "from" << dataEngine;
|
2010-10-24 14:17:22 +02:00
|
|
|
return engine->newQObject(service, QScriptEngine::AutoOwnership);
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue SimpleJavaScriptApplet::loadService(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() != 1) {
|
|
|
|
return context->throwError(i18n("service() takes one argument"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString pluginName = context->argument(0).toString();
|
|
|
|
|
|
|
|
AppletInterface *interface = AppletInterface::extract(engine);
|
|
|
|
if (!interface) {
|
|
|
|
return context->throwError(i18n("Could not extract the Applet"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::Service *service = Plasma::Service::load(pluginName, interface);
|
|
|
|
|
|
|
|
//kDebug( )<< "lets try to get" << source << "from" << dataEngine;
|
2009-12-03 21:58:17 +01:00
|
|
|
return engine->newQObject(service, QScriptEngine::AutoOwnership);
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2009-10-21 04:26:44 +02:00
|
|
|
QScriptValue SimpleJavaScriptApplet::animation(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() != 1) {
|
|
|
|
return context->throwError(i18n("animation() takes one argument"));
|
|
|
|
}
|
|
|
|
|
2009-12-04 20:14:40 +01:00
|
|
|
populateAnimationsHash();
|
2010-05-13 02:53:49 +02:00
|
|
|
QString name = context->argument(0).toString();
|
|
|
|
QString animName = name.toLower();
|
2009-12-04 20:14:40 +01:00
|
|
|
const bool isPause = animName == "pause";
|
2010-01-12 12:16:33 +01:00
|
|
|
const bool isProperty = animName == "property";
|
2009-10-21 04:26:44 +02:00
|
|
|
|
2009-12-14 22:52:40 +01:00
|
|
|
bool parentIsApplet = false;
|
|
|
|
QGraphicsWidget *parent = extractParent(context, engine, 0, &parentIsApplet);
|
2010-04-30 01:58:01 +02:00
|
|
|
QAbstractAnimation *anim = 0;
|
|
|
|
Plasma::Animation *plasmaAnim = 0;
|
2009-12-04 20:14:40 +01:00
|
|
|
if (isPause) {
|
2010-04-30 01:58:01 +02:00
|
|
|
anim = new QPauseAnimation(parent);
|
2010-01-12 12:16:33 +01:00
|
|
|
} else if (isProperty) {
|
2010-04-30 01:58:01 +02:00
|
|
|
anim = new QPropertyAnimation(parent);
|
|
|
|
} else if (s_animationDefs.contains(animName)) {
|
|
|
|
plasmaAnim = Plasma::Animator::create(s_animationDefs.value(animName), parent);
|
2009-12-04 20:14:40 +01:00
|
|
|
} else {
|
2010-05-13 02:53:49 +02:00
|
|
|
SimpleJavaScriptApplet *jsApplet = qobject_cast<SimpleJavaScriptApplet *>(engine->parent());
|
|
|
|
if (jsApplet) {
|
|
|
|
//kDebug() << "trying to load it from the package";
|
|
|
|
plasmaAnim = jsApplet->loadAnimationFromPackage(name, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!plasmaAnim) {
|
|
|
|
plasmaAnim = Plasma::Animator::create(animName, parent);
|
|
|
|
}
|
2010-04-30 01:58:01 +02:00
|
|
|
}
|
2009-12-14 22:52:40 +01:00
|
|
|
|
2010-04-30 01:58:01 +02:00
|
|
|
if (plasmaAnim) {
|
|
|
|
if (!parentIsApplet) {
|
|
|
|
plasmaAnim->setTargetWidget(parent);
|
2009-12-14 22:52:40 +01:00
|
|
|
}
|
2010-04-30 01:58:01 +02:00
|
|
|
anim = plasmaAnim;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (anim) {
|
|
|
|
QScriptValue value = engine->newQObject(anim);
|
|
|
|
ScriptEnv::registerEnums(value, *anim->metaObject());
|
|
|
|
return value;
|
2009-12-04 20:14:40 +01:00
|
|
|
}
|
2009-12-14 22:52:40 +01:00
|
|
|
|
2010-05-13 02:53:49 +02:00
|
|
|
context->throwError(i18n("%1 is not a known animation type", animName));
|
|
|
|
|
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(engine);
|
|
|
|
if (env) {
|
|
|
|
env->checkForErrors(false);
|
|
|
|
}
|
|
|
|
return engine->undefinedValue();
|
2009-10-21 04:26:44 +02:00
|
|
|
}
|
|
|
|
|
2009-10-21 09:53:49 +02:00
|
|
|
QScriptValue SimpleJavaScriptApplet::animationGroup(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
2009-12-06 16:34:41 +01:00
|
|
|
QGraphicsWidget *parent = extractParent(context, engine);
|
2009-12-14 22:54:23 +01:00
|
|
|
SequentialAnimationGroup *group = new SequentialAnimationGroup(parent);
|
2009-12-06 16:34:41 +01:00
|
|
|
return engine->newQObject(group);
|
|
|
|
}
|
2009-10-21 09:53:49 +02:00
|
|
|
|
2009-12-06 16:34:41 +01:00
|
|
|
QScriptValue SimpleJavaScriptApplet::parallelAnimationGroup(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
QGraphicsWidget *parent = extractParent(context, engine);
|
2009-12-14 22:54:23 +01:00
|
|
|
ParallelAnimationGroup *group = new ParallelAnimationGroup(parent);
|
2009-10-21 09:53:49 +02:00
|
|
|
return engine->newQObject(group);
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
QScriptValue SimpleJavaScriptApplet::loadui(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() != 1) {
|
|
|
|
return context->throwError(i18n("loadui() takes one argument"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString filename = context->argument(0).toString();
|
|
|
|
QFile f(filename);
|
|
|
|
if (!f.open(QIODevice::ReadOnly)) {
|
|
|
|
return context->throwError(i18n("Unable to open '%1'",filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
QUiLoader loader;
|
|
|
|
QWidget *w = loader.load(&f);
|
|
|
|
f.close();
|
|
|
|
|
2009-12-03 21:58:17 +01:00
|
|
|
return engine->newQObject(w, QScriptEngine::AutoOwnership);
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue SimpleJavaScriptApplet::newPlasmaSvg(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() == 0) {
|
|
|
|
return context->throwError(i18n("Constructor takes at least 1 argument"));
|
|
|
|
}
|
|
|
|
|
2009-12-06 16:34:41 +01:00
|
|
|
const QString filename = context->argument(0).toString();
|
2009-05-13 02:55:25 +02:00
|
|
|
bool parentedToApplet = false;
|
2009-12-06 16:34:41 +01:00
|
|
|
QGraphicsWidget *parent = extractParent(context, engine, 1, &parentedToApplet);
|
2010-09-25 00:51:16 +02:00
|
|
|
Svg *svg = new ThemedSvg(parent);
|
|
|
|
svg->setImagePath(ThemedSvg::findSvg(engine, filename));
|
|
|
|
|
|
|
|
QScriptValue obj = engine->newQObject(svg);
|
|
|
|
ScriptEnv::registerEnums(obj, *svg->metaObject());
|
|
|
|
|
|
|
|
return obj;
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue SimpleJavaScriptApplet::newPlasmaFrameSvg(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() == 0) {
|
|
|
|
return context->throwError(i18n("Constructor takes at least 1 argument"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString filename = context->argument(0).toString();
|
|
|
|
|
|
|
|
bool parentedToApplet = false;
|
2009-12-06 16:34:41 +01:00
|
|
|
QGraphicsWidget *parent = extractParent(context, engine, 1, &parentedToApplet);
|
2010-09-25 00:51:16 +02:00
|
|
|
FrameSvg *frameSvg = new ThemedFrameSvg(parent);
|
|
|
|
frameSvg->setImagePath(ThemedSvg::findSvg(engine, filename));
|
2010-01-13 04:14:02 +01:00
|
|
|
|
2010-09-25 00:51:16 +02:00
|
|
|
QScriptValue obj = engine->newQObject(frameSvg);
|
|
|
|
ScriptEnv::registerEnums(obj, *frameSvg->metaObject());
|
|
|
|
|
|
|
|
return obj;
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2010-01-20 22:40:55 +01:00
|
|
|
QScriptValue SimpleJavaScriptApplet::newPlasmaExtenderItem(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
Plasma::Extender *extender = 0;
|
|
|
|
if (context->argumentCount() > 0) {
|
|
|
|
extender = qobject_cast<Plasma::Extender *>(context->argument(0).toQObject());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!extender) {
|
2010-08-06 04:01:40 +02:00
|
|
|
AppletInterface *interface = AppletInterface::extract(engine);
|
2010-01-20 22:40:55 +01:00
|
|
|
if (!interface) {
|
|
|
|
engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
extender = interface->extender();
|
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::ExtenderItem *extenderItem = new Plasma::ExtenderItem(extender);
|
|
|
|
QScriptValue fun = engine->newQObject(extenderItem);
|
2010-03-25 00:31:08 +01:00
|
|
|
ScriptEnv::registerEnums(fun, *extenderItem->metaObject());
|
2010-01-20 22:40:55 +01:00
|
|
|
return fun;
|
|
|
|
}
|
|
|
|
|
2009-11-27 21:47:31 +01:00
|
|
|
QScriptValue SimpleJavaScriptApplet::widgetAdjustSize(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
QGraphicsWidget *widget = qobject_cast<QGraphicsWidget*>(context->thisObject().toQObject());
|
|
|
|
if (widget) {
|
|
|
|
widget->adjustSize();
|
|
|
|
}
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
void SimpleJavaScriptApplet::installWidgets(QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
QScriptValue globalObject = engine->globalObject();
|
|
|
|
if (!s_widgetLoader) {
|
|
|
|
s_widgetLoader = new UiLoader;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const QString &widget, s_widgetLoader->availableWidgets()) {
|
|
|
|
QScriptValue fun = engine->newFunction(createWidget);
|
|
|
|
QScriptValue name = engine->toScriptValue(widget);
|
|
|
|
fun.setProperty(QString("functionName"), name,
|
|
|
|
QScriptValue::ReadOnly | QScriptValue::Undeletable | QScriptValue::SkipInEnumeration);
|
2009-12-16 23:05:46 +01:00
|
|
|
fun.setProperty(QString("prototype"), engine->newObject());
|
2009-05-13 02:55:25 +02:00
|
|
|
globalObject.setProperty(widget, fun);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-06 16:34:41 +01:00
|
|
|
QGraphicsWidget *SimpleJavaScriptApplet::extractParent(QScriptContext *context, QScriptEngine *engine,
|
|
|
|
int argIndex, bool *parentedToApplet)
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
2009-12-06 16:34:41 +01:00
|
|
|
if (parentedToApplet) {
|
|
|
|
*parentedToApplet = false;
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsWidget *parent = 0;
|
2009-12-06 16:34:41 +01:00
|
|
|
if (context->argumentCount() >= argIndex) {
|
|
|
|
parent = qobject_cast<QGraphicsWidget*>(context->argument(argIndex).toQObject());
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!parent) {
|
2010-08-06 04:01:40 +02:00
|
|
|
AppletInterface *interface = AppletInterface::extract(engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
if (!interface) {
|
2009-12-06 16:34:41 +01:00
|
|
|
return 0;
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2009-10-22 03:55:53 +02:00
|
|
|
//kDebug() << "got the applet!";
|
2009-05-13 02:55:25 +02:00
|
|
|
parent = interface->applet();
|
2009-12-06 16:34:41 +01:00
|
|
|
|
|
|
|
if (parentedToApplet) {
|
|
|
|
*parentedToApplet = true;
|
|
|
|
}
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2009-12-06 16:34:41 +01:00
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue SimpleJavaScriptApplet::createWidget(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
QGraphicsWidget *parent = extractParent(context, engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
QString self = context->callee().property("functionName").toString();
|
|
|
|
if (!s_widgetLoader) {
|
|
|
|
s_widgetLoader = new UiLoader;
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsWidget *w = s_widgetLoader->createWidget(self, parent);
|
|
|
|
|
|
|
|
if (!w) {
|
|
|
|
return QScriptValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue fun = engine->newQObject(w);
|
|
|
|
fun.setPrototype(context->callee().property("prototype"));
|
2009-11-27 21:47:31 +01:00
|
|
|
fun.setProperty("adjustSize", engine->newFunction(widgetAdjustSize));
|
2009-05-13 02:55:25 +02:00
|
|
|
|
|
|
|
//register enums will be accessed for instance as frame.Sunken for Frame shadow...
|
2010-03-25 00:31:08 +01:00
|
|
|
ScriptEnv::registerEnums(fun, *w->metaObject());
|
2009-05-13 02:55:25 +02:00
|
|
|
return fun;
|
|
|
|
}
|
|
|
|
|
2009-12-16 23:05:46 +01:00
|
|
|
void SimpleJavaScriptApplet::collectGarbage()
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
2009-12-16 23:05:46 +01:00
|
|
|
m_engine->collectGarbage();
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2010-09-05 00:51:13 +02:00
|
|
|
QString SimpleJavaScriptApplet::filePath(const QString &type, const QString &file) const
|
|
|
|
{
|
|
|
|
const QString path = m_env->filePathFromScriptContext(type.toLocal8Bit().constData(), file);
|
|
|
|
if (!path.isEmpty()) {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return package()->filePath(type.toLocal8Bit().constData(), file);
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
QScriptValue SimpleJavaScriptApplet::variantToScriptValue(QVariant var)
|
|
|
|
{
|
2010-10-13 20:08:07 +02:00
|
|
|
return m_engine->newVariant(var);
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
K_EXPORT_PLASMA_APPLETSCRIPTENGINE(qscriptapplet, SimpleJavaScriptApplet)
|
2010-02-16 18:48:54 +01:00
|
|
|
|