22 #include "../core/manager.h"
23 #include "../core/object.h"
33 #include <QVBoxLayout>
35 #include <QAbstractItemView>
36 #include <QAbstractItemModel>
37 #include <QStringListModel>
38 #include <QScriptClass>
39 #include <QScriptContext>
40 #include <QScriptValueIterator>
46 using namespace Kross;
57 explicit EcmaObject(QScriptEngine* engine,
const QScriptValue&
object = QScriptValue())
61 virtual QScriptValue
prototype()
const {
return m_object; }
65 QScriptValue
function = m_object.property(name);
66 if( !
function.isFunction() ) {
68 if( QScriptContext* context = engine()->currentContext() )
69 context->throwError(QScriptContext::ReferenceError,
i18n(
"No such function \"%1\"", name));
72 QScriptValueList arguments;
74 arguments << engine()->toScriptValue(v);
75 QScriptValue result =
function.call(m_object, arguments);
76 return result.toVariant();
82 QScriptValueIterator it( m_object );
83 while( it.hasNext() ) {
85 if( it.value().isFunction() )
92 QScriptValue m_object;
95 QScriptValue
toByteArray(QScriptEngine *e,
const QByteArray &ba) {
96 return ba.isNull() ? e->nullValue() : e->newVariant(ba);
99 ba = v.isNull() ? QByteArray() : v.toString().toUtf8();
103 return e->newVariant( url.toString() );
106 url.setUrl( v.toString() );
110 return e->newVariant( url.
url() );
113 url.setUrl( v.toString() );
117 return c.isValid() ? e->newVariant(c.name()) : e->nullValue();
120 c.setNamedColor( v.isNull() ?
QString() : v.toString() );
124 return e->toScriptValue( QVariantList() << r.x() << r.y() << r.width() << r.height() );
127 r = v.isArray() ?
QRect(v.property(0).toInt32(),v.property(1).toInt32(),v.property(2).toInt32(),v.property(3).toInt32()) :
QRect();
129 QScriptValue
toRectF(QScriptEngine *e,
const QRectF &r) {
130 return e->toScriptValue( QVariantList() << r.x() << r.y() << r.width() << r.height() );
133 r = v.isArray() ? QRectF(v.property(0).toNumber(),v.property(1).toNumber(),v.property(2).toNumber(),v.property(3).toNumber()) : QRectF();
137 return e->toScriptValue( QVariantList() << p.x() << p.y() );
140 p = v.isArray() ?
QPoint(v.property(0).toInt32(),v.property(1).toInt32()) :
QPoint();
142 QScriptValue
toPointF(QScriptEngine *e,
const QPointF &p) {
143 return e->toScriptValue( QVariantList() << p.x() << p.y() );
146 p = v.isArray() ? QPointF(v.property(0).toNumber(),v.property(1).toNumber()) : QPointF();
150 return e->toScriptValue( QVariantList() << s.width() << s.height() );
153 s = v.isArray() ?
QSize(v.property(0).toInt32(),v.property(1).toInt32()) :
QSize();
155 QScriptValue
toSizeF(QScriptEngine *e,
const QSizeF &s) {
156 return e->toScriptValue( QVariantList() << s.width() << s.height() );
159 s = v.isArray() ? QSizeF(v.property(0).toNumber(),v.property(1).toNumber()) : QSizeF();
180 return obj ? obj->
prototype() : e->nullValue();
186 QScriptValue
createWidget(QScriptContext *context, QScriptEngine *engine) {
187 const QString widgetname = context->callee().prototype().property(
"className").toString();
188 Q_ASSERT( ! widgetname.isEmpty() );
189 QWidget *parent = qscriptvalue_cast<
QWidget*>(context->argument(0));
191 QWidget *widget = loader.createWidget(widgetname, parent);
193 return context->throwError(QScriptContext::TypeError,
QString(
"No such QWidget \"%1\"").arg(widgetname));
194 if( parent && parent->layout() )
195 parent->layout()->addWidget(widget);
196 QScriptEngine::ValueOwnership owner = parent ? QScriptEngine::QtOwnership : QScriptEngine::ScriptOwnership;
197 QScriptValue result = engine->newQObject(widget, owner);
203 if( QLayout *layout = dynamic_cast<QLayout*>( qscriptvalue_cast<QObject*>(c->thisObject()) ) ) {
204 QGridLayout *gridLayout =
dynamic_cast<QGridLayout*
>(layout);
206 if(
QWidget *w = dynamic_cast<QWidget*>(obj) ) {
208 gridLayout->addWidget(w, c->argument(1).toInt32(), c->argument(2).toInt32(), (Qt::Alignment)c->argument(3).toInt32());
210 layout->addWidget(w);
212 else if( QLayout *l = dynamic_cast<QLayout*>( qscriptvalue_cast<QObject*>(c->argument(0)) ) ) {
214 gridLayout->addLayout(l, c->argument(1).toInt32(), c->argument(2).toInt32(), (Qt::Alignment)c->argument(3).toInt32());
215 else if( QBoxLayout *bl = dynamic_cast<QBoxLayout*>(layout) )
219 return engine->nullValue();
221 QScriptValue
createLayout(QScriptContext *context, QScriptEngine *engine, QLayout *layout) {
222 QObject *parent = qscriptvalue_cast<
QObject*>(context->argument(0));
223 if(
QWidget *parentWidget = dynamic_cast<QWidget*>(parent) )
224 parentWidget->setLayout(layout);
225 else if( QBoxLayout *parentLayout = dynamic_cast<QBoxLayout*>(parent) )
226 parentLayout->addLayout(layout);
227 QScriptValue obj = engine->newQObject(layout);
233 return createLayout(context, engine,
new QVBoxLayout());
236 return createLayout(context, engine,
new QHBoxLayout());
239 return createLayout(context, engine,
new QGridLayout());
242 if (context->argumentCount() < 1)
243 return engine->nullValue();
244 return engine->importExtension(context->argument(0).toString());
252 QScriptValue global = engine->globalObject();
255 if( ! global.property(
"println").isValid() )
256 global.setProperty(
"println", global.property(
"print"));
260 qScriptRegisterMetaType< QUrl >(engine,
toUrl,
fromUrl);
286 QScriptValue global = engine->globalObject();
290 foreach(
const QString &widgetname, loader.availableWidgets()) {
291 QScriptValue proto = engine->newObject();
292 proto.setProperty(
"className", QScriptValue(engine, widgetname));
294 func.setPrototype(proto);
295 global.setProperty(widgetname, func);
QScriptValue createGridLayout(QScriptContext *context, QScriptEngine *engine)
QScriptValue toKUrl(QScriptEngine *e, const KUrl &url)
QString i18n(const char *text)
QScriptValue includeFunction(QScriptContext *context, QScriptEngine *engine)
QScriptValue createWidget(QScriptContext *context, QScriptEngine *engine)
void fromUrl(const QScriptValue &v, QUrl &url)
QScriptValue createVBoxLayout(QScriptContext *context, QScriptEngine *engine)
The EcmaObject implements QScriptClass and Kross::Object to provide custom behavior to QtScript objec...
void fromByteArray(const QScriptValue &v, QByteArray &ba)
void fromPointF(const QScriptValue &v, QPointF &p)
void fromSizeF(const QScriptValue &v, QSizeF &s)
void fromObjPtr(const QScriptValue &v, Kross::Object::Ptr &ptr)
void initializeCore(QScriptEngine *engine)
Initialize some core functionality like common used types we like to use within scripts.
virtual QString name() const
void fromKUrl(const QScriptValue &v, KUrl &url)
void fromRectF(const QScriptValue &v, QRectF &r)
QScriptValue toSizeF(QScriptEngine *e, const QSizeF &s)
QScriptValue toColor(QScriptEngine *e, const QColor &c)
QScriptValue toRectF(QScriptEngine *e, const QRectF &r)
QScriptValue toObjPtr(QScriptEngine *e, const Kross::Object::Ptr &ptr)
QScriptValue addWidgetLayout(QScriptContext *c, QScriptEngine *engine)
void krosswarning(const QString &s)
Warning function.
void fromRect(const QScriptValue &v, QRect &r)
virtual QVariant callMethod(const QString &name, const QVariantList &args=QVariantList())
Pass a call to the object and evaluated it.
void fromColor(const QScriptValue &v, QColor &c)
virtual QStringList methodNames()
Return a list of supported callable objects.
QScriptValue toByteArray(QScriptEngine *e, const QByteArray &ba)
void initializeGui(QScriptEngine *engine)
Initialize GUI functionality like widgets the QUiLoader provides and some layout-managers.
QScriptValue toSize(QScriptEngine *e, const QSize &s)
QScriptValue toRect(QScriptEngine *e, const QRect &r)
void fromPoint(const QScriptValue &v, QPoint &p)
QScriptValue createLayout(QScriptContext *context, QScriptEngine *engine, QLayout *layout)
QScriptValue toPointF(QScriptEngine *e, const QPointF &p)
The class Object does provide us scripting objects like class instances to the C++ world...
EcmaObject(QScriptEngine *engine, const QScriptValue &object=QScriptValue())
QString url(AdjustPathOption trailing=LeaveTrailingSlash) const
QScriptValue toUrl(QScriptEngine *e, const QUrl &url)
virtual QScriptValue prototype() const
void fromSize(const QScriptValue &v, QSize &s)
QScriptValue toPoint(QScriptEngine *e, const QPoint &p)
QScriptValue createHBoxLayout(QScriptContext *context, QScriptEngine *engine)