added some tests to noqt
[lqt.git] / old_bindings / lqt_qt_utils.cpp
bloba48ebe006f934f4e68fef088b1ebfb2aa529eb54
1 /*
2 * Copyright (c) 2007-2008 Mauro Iazzi
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
27 #include "lqt_qt_utils.hpp"
28 #include "lqt_common.hpp"
29 #include "lqt_function.hpp"
31 static int qt_slot_from_string (lua_State *L) {
32 lua_pushstring(L, "1");
33 lua_pushvalue(L, 1);
34 lua_concat(L, 2);
35 return 1;
38 static int qt_signal (lua_State *L) {
39 lua_pushstring(L, "2");
40 lua_pushvalue(L, 1);
41 lua_concat(L, 2);
42 return 1;
45 static int qt_derive (lua_State *L) {
46 if (!lua_isuserdata(L, 1) || !lua_getmetatable(L, 1)) {
47 lua_pushnil(L);
48 lua_pushstring(L, "no userdata or no metatable given");
49 return 2;
51 lua_getfield(L, -1, "__qtype");
52 if (!lua_isstring(L, -1)) {
53 lua_pushnil(L);
54 lua_pushstring(L, "not a Qt type");
55 return 2;
57 lua_insert(L, -2);
58 lua_newtable(L);
59 lua_insert(L, -3);
60 lua_settable(L, -3);
61 lua_newtable(L);
62 lua_insert(L, -2);
63 lua_setfield(L, -2, "__base");
64 lua_pushcfunction(L, lqtL_index);
65 lua_setfield(L, -2, "__index");
66 lua_pushcfunction(L, lqtL_newindex);
67 lua_setfield(L, -2, "__newindex");
68 lua_setmetatable(L, 1);
69 return 0;
72 static int qt_slot_from_function (lua_State *L) {
73 lua_pushvalue(L, 1);
74 LuaFunction *f = new LuaFunction(L);
75 f = 0;
76 lua_pushstring(L, "1function()");
77 return 2;
80 static int qt_slot (lua_State *L) {
81 int ret = 0;
82 if (lua_type(L, 1)==LUA_TSTRING) {
83 ret = qt_slot_from_string(L);
84 } else if (lua_type(L, 1)==LUA_TFUNCTION) {
85 ret = qt_slot_from_function(L);
87 return ret;
90 static int qt_pick (lua_State *L) {
91 int nargs = lua_gettop(L);
92 int i = 0;
93 while (i++<nargs) {
94 lqtL_manageudata(L, i);
96 return nargs;
99 static int qt_pass (lua_State *L) {
100 int nargs = lua_gettop(L);
101 int i = 0;
102 while (i++<nargs) {
103 lqtL_unmanageudata(L, i);
105 return nargs;
109 static luaL_Reg libqt[] = {
110 { "pick", qt_pick },
111 { "pass", qt_pass },
112 { "slot", qt_slot },
113 { "signal", qt_signal },
114 { "derive", qt_derive },
115 { 0, 0 },
118 int luaopen_qt (lua_State *L) {
119 luaL_register(L, "qt", libqt);
120 return 1;