QDjango
 All Classes Functions Typedefs Enumerations Enumerator Properties Groups Pages
QDjangoScript.h
1 /*
2  * Copyright (C) 2010-2012 Jeremy LainĂ©
3  * Contact: http://code.google.com/p/qdjango/
4  *
5  * This file is part of the QDjango Library.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  */
17 
18 #ifndef QDJANGO_SCRIPT_H
19 #define QDJANGO_SCRIPT_H
20 
21 #include <QtScript/QScriptValue>
22 #include <QtScript/QScriptEngine>
23 
24 #include "QDjango.h"
25 #include "QDjangoQuerySet.h"
26 #include "QDjangoScript_p.h"
27 
28 Q_DECLARE_METATYPE(QDjangoWhere)
29 
30 
37 class QDJANGO_EXPORT QDjangoScript
38 {
39 public:
40  template <class T>
41  static void registerModel(QScriptEngine *engine);
42  static void registerWhere(QScriptEngine *engine);
43 };
44 
49 template <class T>
50 void QDjangoScript::registerModel(QScriptEngine *engine)
51 {
52  QDjango::registerModel<T>();
53 
54  QScriptValue querysetProto = engine->newObject();
55  querysetProto.setProperty("all", engine->newFunction(QDjangoQuerySet_all<T>));
56  querysetProto.setProperty("at", engine->newFunction(QDjangoQuerySet_at<T>));
57  querysetProto.setProperty("count", engine->newFunction(QDjangoQuerySet_count<T>));
58  querysetProto.setProperty("exclude", engine->newFunction(QDjangoQuerySet_exclude<T>));
59  querysetProto.setProperty("filter", engine->newFunction(QDjangoQuerySet_filter<T>));
60  querysetProto.setProperty("get", engine->newFunction(QDjangoQuerySet_get<T>));
61  querysetProto.setProperty("limit", engine->newFunction(QDjangoQuerySet_limit<T>));
62  querysetProto.setProperty("remove", engine->newFunction(QDjangoQuerySet_remove<T>));
63  querysetProto.setProperty("size", engine->newFunction(QDjangoQuerySet_size<T>));
64  querysetProto.setProperty("toString", engine->newFunction(QDjangoQuerySet_toString<T>));
65  engine->setDefaultPrototype(qMetaTypeId< QDjangoQuerySet<T> >(), querysetProto);
66 
68  QScriptValue value = engine->newQMetaObject(&T::staticMetaObject, engine->newFunction(QDjangoModel_new<T>));
69  value.setProperty("objects", engine->toScriptValue(qs));
70  engine->globalObject().setProperty(T::staticMetaObject.className(), value);
71 }
72 
73 #endif