Added utility files lqt_qt_utils.?pp
[lqt.git] / lqt_qt_utils.cpp
blob74e75ec63d58e71d2ec4d93ab69e903b0e558cab
1 #include "lqt_qt_utils.hpp"
2 #include "lqt_function.hpp"
4 static int qt_slot_from_string (lua_State *L) {
5 lua_pushstring(L, "1");
6 lua_pushvalue(L, 1);
7 lua_concat(L, 2);
8 return 1;
11 static int qt_signal (lua_State *L) {
12 lua_pushstring(L, "2");
13 lua_pushvalue(L, 1);
14 lua_concat(L, 2);
15 return 1;
18 static int qt_derive (lua_State *L) {
19 if (!lua_isuserdata(L, 1) || !lua_getmetatable(L, 1)) {
20 lua_pushnil(L);
21 lua_pushstring(L, "no userdata or no metatable given");
22 return 2;
24 lua_getfield(L, -1, "__qtype");
25 if (!lua_isstring(L, -1)) {
26 lua_pushnil(L);
27 lua_pushstring(L, "not a Qt type");
28 return 2;
30 lua_insert(L, -2);
31 lua_newtable(L);
32 lua_insert(L, -3);
33 lua_settable(L, -3);
34 lua_newtable(L);
35 lua_insert(L, -2);
36 lua_setfield(L, -2, "__base");
37 lua_pushcfunction(L, lqtL_index);
38 lua_setfield(L, -2, "__index");
39 lua_pushcfunction(L, lqtL_newindex);
40 lua_setfield(L, -2, "__newindex");
41 lua_setmetatable(L, 1);
42 return 0;
45 static int qt_slot_from_function (lua_State *L) {
46 lua_pushvalue(L, 1);
47 LuaFunction *f = new LuaFunction(L);
48 f = 0;
49 lua_pushstring(L, "1function()");
50 return 2;
53 static int qt_slot (lua_State *L) {
54 int ret = 0;
55 if (lua_type(L, 1)==LUA_TSTRING) {
56 ret = qt_slot_from_string(L);
57 } else if (lua_type(L, 1)==LUA_TFUNCTION) {
58 ret = qt_slot_from_function(L);
60 return ret;
63 static luaL_Reg libqt[] = {
64 { "slot", qt_slot },
65 { "signal", qt_signal },
66 { "derive", qt_derive },
67 { 0, 0 },
70 int luaopen_qt (lua_State *L) {
71 luaL_register(L, "qt", libqt);
72 return 1;