From 5e3357221f5ec940d892100358b7ad09e456093d Mon Sep 17 00:00:00 2001 From: "Aaron J. Seigo" Date: Wed, 24 Feb 2010 08:00:46 +0000 Subject: [PATCH] icon, to make IconWidget usable svn path=/branches/KDE/4.4/kdebase/runtime/; revision=1095391 --- scriptengines/javascript/CMakeLists.txt | 1 + .../javascript/simplebindings/icon.cpp | 111 ++++++++++++++++++ .../javascript/simplejavascriptapplet.cpp | 2 + 3 files changed, 114 insertions(+) create mode 100644 scriptengines/javascript/simplebindings/icon.cpp diff --git a/scriptengines/javascript/CMakeLists.txt b/scriptengines/javascript/CMakeLists.txt index b2d7e5f7a..e104d26c4 100644 --- a/scriptengines/javascript/CMakeLists.txt +++ b/scriptengines/javascript/CMakeLists.txt @@ -12,6 +12,7 @@ set(simple_javascript_engine_SRCS simplebindings/font.cpp simplebindings/filedialogproxy.cpp simplebindings/graphicsitem.cpp + simplebindings/icon.cpp simplebindings/linearlayout.cpp simplebindings/gridlayout.cpp simplebindings/painter.cpp diff --git a/scriptengines/javascript/simplebindings/icon.cpp b/scriptengines/javascript/simplebindings/icon.cpp new file mode 100644 index 000000000..49feaade6 --- /dev/null +++ b/scriptengines/javascript/simplebindings/icon.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2009 Aaron J. Seigo + * + * 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 +#include +#include + +#include + +#include "../backportglobal.h" + +Q_DECLARE_METATYPE(QIcon) +Q_DECLARE_METATYPE(QIcon*) +Q_DECLARE_METATYPE(KIcon) +Q_DECLARE_METATYPE(KIcon*) + +static QScriptValue ctor(QScriptContext *ctx, QScriptEngine *eng) +{ + if (ctx->argumentCount() > 0) { + QScriptValue v = ctx->argument(0); + if (v.isString()) { + return qScriptValueFromValue(eng, KIcon(v.toString())); + } else if (v.isVariant()) { + QVariant variant = v.toVariant(); + QPixmap p = variant.value(); + if (!p.isNull()) { + return qScriptValueFromValue(eng, QIcon(p)); + } + } + } + return qScriptValueFromValue(eng, QIcon()); +} + +static QScriptValue addPixmap(QScriptContext *ctx, QScriptEngine *eng) +{ + DECLARE_SELF(QIcon, addPixmap); + + if (ctx->argumentCount() > 0) { + QScriptValue arg = ctx->argument(0); + + if (arg.isVariant()) { + QVariant variant = arg.toVariant(); + QPixmap p = variant.value(); + if (!p.isNull()) { + self->addPixmap(p); + } + } + } + + return eng->undefinedValue(); +} + +static QScriptValue addFile(QScriptContext *ctx, QScriptEngine *eng) +{ + DECLARE_SELF(QIcon, addFile); + + if (ctx->argumentCount() > 0) { + QScriptValue arg = ctx->argument(0); + + if (arg.isString()) { + self->addFile(arg.toString()); + } + } + + return eng->undefinedValue(); +} + +static QScriptValue isNull(QScriptContext *ctx, QScriptEngine *eng) +{ + Q_UNUSED(eng) + DECLARE_SELF(QIcon, isNull); + return self->isNull(); +} + +QScriptValue constructIconClass(QScriptEngine *eng) +{ + QScriptValue proto = qScriptValueFromValue(eng, QIcon()); + QScriptValue::PropertyFlags getter = QScriptValue::PropertyGetter; + QScriptValue::PropertyFlags setter = QScriptValue::PropertySetter; + proto.setProperty("addPixmap", eng->newFunction(addPixmap)); + proto.setProperty("addFile", eng->newFunction(addFile)); + proto.setProperty("null", eng->newFunction(isNull), getter); + + QScriptValue ctorFun = eng->newFunction(ctor, proto); + ADD_ENUM_VALUE(ctorFun, QIcon, Normal); + ADD_ENUM_VALUE(ctorFun, QIcon, Disabled); + ADD_ENUM_VALUE(ctorFun, QIcon, Active); + ADD_ENUM_VALUE(ctorFun, QIcon, Selected); + ADD_ENUM_VALUE(ctorFun, QIcon, Off); + ADD_ENUM_VALUE(ctorFun, QIcon, On); + + eng->setDefaultPrototype(qMetaTypeId(), proto); + + return ctorFun; +} + diff --git a/scriptengines/javascript/simplejavascriptapplet.cpp b/scriptengines/javascript/simplejavascriptapplet.cpp index a1b819cfe..d4aa58abb 100644 --- a/scriptengines/javascript/simplejavascriptapplet.cpp +++ b/scriptengines/javascript/simplejavascriptapplet.cpp @@ -68,6 +68,7 @@ QScriptValue constructColorClass(QScriptEngine *engine); QScriptValue constructEasingCurveClass(QScriptEngine *engine); QScriptValue constructFontClass(QScriptEngine *engine); QScriptValue constructGraphicsItemClass(QScriptEngine *engine); +QScriptValue constructIconClass(QScriptEngine *engine); QScriptValue constructKUrlClass(QScriptEngine *engine); QScriptValue constructLinearLayoutClass(QScriptEngine *engine); QScriptValue constructGridLayoutClass(QScriptEngine *engine); @@ -572,6 +573,7 @@ void SimpleJavaScriptApplet::setupObjects() // Add stuff from Qt global.setProperty("QPainter", constructPainterClass(m_engine)); global.setProperty("QGraphicsItem", constructGraphicsItemClass(m_engine)); + global.setProperty("QIcon", constructIconClass(m_engine)); global.setProperty("QTimer", constructTimerClass(m_engine)); global.setProperty("QFont", constructFontClass(m_engine)); global.setProperty("QColor", constructColorClass(m_engine));