QDjango
QDjangoScript.h
00001 /*
00002  * Copyright (C) 2010-2012 Jeremy Lainé
00003  * Contact: http://code.google.com/p/qdjango/
00004  *
00005  * This file is part of the QDjango Library.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  */
00017 
00018 #ifndef QDJANGO_SCRIPT_H
00019 #define QDJANGO_SCRIPT_H
00020 
00021 #include <QtScript/QScriptValue>
00022 #include <QtScript/QScriptEngine>
00023 
00024 #include "QDjango.h"
00025 #include "QDjangoQuerySet.h"
00026 #include "QDjangoScript_p.h"
00027 
00028 Q_DECLARE_METATYPE(QDjangoWhere)
00029 
00030 
00037 class QDjangoScript
00038 {
00039 public:
00040     template <class T>
00041     static void registerModel(QScriptEngine *engine);
00042     static void registerWhere(QScriptEngine *engine);
00043 };
00044 
00049 template <class T>
00050 void QDjangoScript::registerModel(QScriptEngine *engine)
00051 {
00052     QDjango::registerModel<T>();
00053 
00054     QScriptValue querysetProto = engine->newObject();
00055     querysetProto.setProperty("all", engine->newFunction(QDjangoQuerySet_all<T>));
00056     querysetProto.setProperty("at", engine->newFunction(QDjangoQuerySet_at<T>));
00057     querysetProto.setProperty("count", engine->newFunction(QDjangoQuerySet_count<T>));
00058     querysetProto.setProperty("exclude", engine->newFunction(QDjangoQuerySet_exclude<T>));
00059     querysetProto.setProperty("filter", engine->newFunction(QDjangoQuerySet_filter<T>));
00060     querysetProto.setProperty("get", engine->newFunction(QDjangoQuerySet_get<T>));
00061     querysetProto.setProperty("limit", engine->newFunction(QDjangoQuerySet_limit<T>));
00062     querysetProto.setProperty("remove", engine->newFunction(QDjangoQuerySet_remove<T>));
00063     querysetProto.setProperty("size", engine->newFunction(QDjangoQuerySet_size<T>));
00064     querysetProto.setProperty("toString", engine->newFunction(QDjangoQuerySet_toString<T>));
00065     engine->setDefaultPrototype(qMetaTypeId< QDjangoQuerySet<T> >(), querysetProto);
00066 
00067     QDjangoQuerySet<T> qs;
00068     QScriptValue value = engine->newQMetaObject(&T::staticMetaObject, engine->newFunction(QDjangoModel_new<T>));
00069     value.setProperty("objects", engine->toScriptValue(qs));
00070     engine->globalObject().setProperty(T::staticMetaObject.className(), value);
00071 }
00072 
00073 #endif
 All Classes Functions Typedefs Enumerations Enumerator Properties