Plasma
easingcurve.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright 2010 Aaron Seigo <aseigo@kde.org> 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU Library General Public License version 2 as 00006 * published by the Free Software Foundation 00007 * 00008 * This program is distributed in the hope that it will be useful, 00009 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 * GNU General Public License for more details 00012 * 00013 * You should have received a copy of the GNU Library General Public 00014 * License along with this program; if not, write to the 00015 * Free Software Foundation, Inc., 00016 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #include <QEasingCurve> 00020 #include <QMetaEnum> 00021 #include <QScriptValue> 00022 #include <QScriptEngine> 00023 #include <QScriptContext> 00024 #include <QScriptable> 00025 00026 Q_DECLARE_METATYPE(QEasingCurve) 00027 Q_DECLARE_METATYPE(QEasingCurve*) 00028 #define ADD_ENUM_VALUE(__c__, __ns__, __v__) \ 00029 __c__.setProperty(#__v__, QScriptValue(__c__.engine(), __ns__::__v__)) 00030 00031 #define DECLARE_SELF(Class, __fn__) \ 00032 Class* self = qscriptvalue_cast<Class*>(ctx->thisObject()); \ 00033 if (!self) { \ 00034 return ctx->throwError(QScriptContext::TypeError, \ 00035 QString::fromLatin1("%0.prototype.%1: this object is not a %0") \ 00036 .arg(#Class).arg(#__fn__)); \ 00037 } 00038 00039 namespace Plasma 00040 { 00041 00042 static QScriptValue ctor(QScriptContext *ctx, QScriptEngine *eng) 00043 { 00044 if (ctx->argumentCount() > 0) { 00045 QScriptValue arg = ctx->argument(0); 00046 if (arg.isNumber()) { 00047 qint32 type = arg.toInt32(); 00048 if (type > -1 && type < QEasingCurve::Custom) { 00049 return qScriptValueFromValue(eng, QEasingCurve(static_cast<QEasingCurve::Type>(type))); 00050 } 00051 } 00052 } 00053 00054 return qScriptValueFromValue(eng, QEasingCurve()); 00055 } 00056 00057 static QScriptValue toString(QScriptContext *ctx, QScriptEngine *eng) 00058 { 00059 DECLARE_SELF(QEasingCurve, toString); 00060 return QScriptValue(eng, QString::fromLatin1("QEasingCurve(type=%0)").arg(self->type())); 00061 } 00062 00063 static QScriptValue type(QScriptContext *ctx, QScriptEngine *eng) 00064 { 00065 DECLARE_SELF(QEasingCurve, type); 00066 00067 if (ctx->argumentCount()) { 00068 QScriptValue arg = ctx->argument(0); 00069 00070 qint32 type = -1; 00071 if (arg.isNumber()) { 00072 type = arg.toInt32(); 00073 } else if (arg.isString()) { 00074 QMetaObject meta = QEasingCurve::staticMetaObject; 00075 QMetaEnum easingCurveEnum = meta.enumerator(meta.indexOfEnumerator("Type")); 00076 00077 type = easingCurveEnum.keyToValue(arg.toString().toAscii().data()); 00078 } 00079 if (type > -1 && type < QEasingCurve::Custom) { 00080 self->setType(static_cast<QEasingCurve::Type>(type)); 00081 } 00082 } 00083 00084 return QScriptValue(eng, self->type()); 00085 } 00086 00087 static QScriptValue valueForProgress(QScriptContext *ctx, QScriptEngine *eng) 00088 { 00089 DECLARE_SELF(QEasingCurve, valueForProgress); 00090 if (ctx->argumentCount() < 1 || !ctx->argument(0).isNumber()) { 00091 return eng->undefinedValue(); 00092 } 00093 00094 return self->valueForProgress(ctx->argument(0).toNumber()); 00095 } 00096 00097 QScriptValue constructEasingCurveClass(QScriptEngine *eng) 00098 { 00099 QScriptValue proto = qScriptValueFromValue(eng, QEasingCurve()); 00100 QScriptValue::PropertyFlags getter = QScriptValue::PropertyGetter; 00101 QScriptValue::PropertyFlags setter = QScriptValue::PropertySetter; 00102 00103 proto.setProperty("type", eng->newFunction(type), getter | setter); 00104 proto.setProperty("toString", eng->newFunction(toString), getter); 00105 proto.setProperty("valueForProgress", eng->newFunction(valueForProgress), getter); 00106 00107 QScriptValue ctorFun = eng->newFunction(ctor, proto); 00108 00109 ADD_ENUM_VALUE(ctorFun, QEasingCurve, Linear); 00110 00111 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InQuad); 00112 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutQuad); 00113 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InOutQuad); 00114 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutInQuad); 00115 00116 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InCubic); 00117 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutCubic); 00118 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InOutCubic); 00119 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutInCubic); 00120 00121 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InQuart); 00122 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutQuart); 00123 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InOutQuart); 00124 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutInQuart); 00125 00126 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InQuint); 00127 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutQuint); 00128 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InOutQuint); 00129 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutInQuint); 00130 00131 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InSine); 00132 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutSine); 00133 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InOutSine); 00134 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutInSine); 00135 00136 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InExpo); 00137 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutExpo); 00138 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InOutExpo); 00139 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutInExpo); 00140 00141 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InCirc); 00142 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutCirc); 00143 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InOutCirc); 00144 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutInCirc); 00145 00146 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InElastic); 00147 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutElastic); 00148 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InOutElastic); 00149 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutInElastic); 00150 00151 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InBack); 00152 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutBack); 00153 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InOutBack); 00154 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutInBack); 00155 00156 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InBounce); 00157 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutBounce); 00158 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InOutBounce); 00159 00160 ADD_ENUM_VALUE(ctorFun, QEasingCurve, InCurve); 00161 ADD_ENUM_VALUE(ctorFun, QEasingCurve, OutCurve); 00162 ADD_ENUM_VALUE(ctorFun, QEasingCurve, SineCurve); 00163 ADD_ENUM_VALUE(ctorFun, QEasingCurve, CosineCurve); 00164 00165 eng->setDefaultPrototype(qMetaTypeId<QEasingCurve>(), proto); 00166 eng->setDefaultPrototype(qMetaTypeId<QEasingCurve*>(), proto); 00167 00168 return ctorFun; 00169 } 00170 00171 } 00172
KDE 4.6 API Reference