fixed argument test code
[lqt.git] / main.cpp
blobfcee14a46a71742781d09eb9077a47a30e37b04d
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 "common_bind.hpp"
28 #include "luafunction.hpp"
29 #include "lqt_bind_QLabel.hpp"
30 #include "lqt_bind_QCoreApplication.hpp"
31 #include "lqt_bind_QApplication.hpp"
32 #include "lqt_bind_QAbstractButton.hpp"
33 #include "lqt_bind_QPushButton.hpp"
34 #include "lqt_bind_QObject.hpp"
35 #include "lqt_bind_QFont.hpp"
36 #include "lqt_bind_QWidget.hpp"
37 #include "lqt_bind_QTextEdit.hpp"
38 #include "lqt_bind_QAbstractScrollArea.hpp"
39 #include "lqt_bind_QFrame.hpp"
40 #include "lqt_bind_QEvent.hpp"
41 #include "lqt_bind_QLayout.hpp"
42 #include "lqt_bind_QBoxLayout.hpp"
43 #include "lqt_bind_QVBoxLayout.hpp"
45 #define EXPORT(n) do { \
46 luaL_getmetatable(L,#n "*" );\
47 lua_setglobal(L, #n);\
48 } while (0)
51 #include <QApplication>
52 #include <QPushButton>
53 #include <QDebug>
54 #include <QSlider>
55 #include <QSpinBox>
56 #include <QTextEdit>
57 #include <QVBoxLayout>
58 #include <QMessageBox>
59 #include <QShortcut>
60 #include <QWidget>
61 // */
63 static int retriever (lua_State *L) {
64 lua_pushvalue(L, lua_upvalueindex(1));
65 return 1;
68 static int F2S (lua_State *L) {
69 lua_settop(L, 1);
70 if (!lua_isfunction(L, -1)) {
71 return 0;
73 LuaFunction *lf = new LuaFunction(L);
74 lua_pushstring(L, "1function()");
75 return 2;
78 int main(int argc, char *argv[])
80 QApplication *app = new QApplication(argc, argv);
82 int st = 0;
83 int oldtop = 0;
84 lua_State *L = lua_open();
85 luaL_openlibs(L);
86 luaopen_QEvent(L);
87 luaopen_QObject(L);
88 luaopen_QFont(L);
89 luaopen_QWidget(L);
90 luaopen_QLabel(L);
91 luaopen_QPushButton(L);
92 luaopen_QAbstractButton(L);
93 luaopen_QApplication(L);
94 luaopen_QCoreApplication(L);
95 luaopen_QTextEdit(L);
96 luaopen_QAbstractScrollArea(L);
97 luaopen_QFrame(L);
99 luaopen_QLayout(L);
100 luaopen_QBoxLayout(L);
101 luaopen_QVBoxLayout(L);
103 lua_pushcfunction(L, F2S);
104 lua_setglobal(L, "SLOT");
106 #if 1
107 EXPORT(QWidget);
108 EXPORT(QObject);
109 EXPORT(QLabel);
110 EXPORT(QFont);
111 EXPORT(QEvent);
112 EXPORT(QAbstractButton);
113 EXPORT(QPushButton);
114 EXPORT(QApplication);
115 EXPORT(QTextEdit);
116 EXPORT(QVBoxLayout);
118 lua_pushvalue(L, LUA_REGISTRYINDEX);
119 lua_setglobal(L, "_R");
120 luaL_dostring(L, "\
122 local del = QEvent.delete\
123 local done = {}\
124 local search\
125 function search (k, v)\
126 if v == del then print ('here', v) return true end\
127 if type(v) == 'table' then\
128 if not done[v] then\
129 done[v]=true\
130 for i, e in pairs(v) do\
131 if search(i, e) then print (k) end\
132 end\
133 end\
134 end\
135 end\
136 search('_G', _G)\
137 end");
140 lqtL_pushudata(L, app, "QApplication*");
141 lua_pushcclosure(L, retriever, 1);
142 lua_setglobal(L, "app");
144 lua_getglobal(L, "QApplication");
145 lua_getglobal(L, "app");
146 lua_setfield(L, -2, "new");
148 lua_pushnil(L);
149 lua_setglobal(L, "app");
152 luaL_dofile(L, "tutorial.lua");
153 qDebug() << lua_tostring(L, -2) << lua_tostring(L, -1);
155 lua_getglobal(L, "PB");
157 #else
158 lua_checkstack(L, 15);
160 QWidget *mainwin = new LuaBinder<QWidget>(L, 0, 0);
162 lqtL_pushudata(L, app, "QApplication*");
163 lua_setglobal(L, "app");
165 QPushButton butt;
166 lqtL_pushudata(L, &butt, "QPushButton*");
167 lua_setglobal(L, "my_button");
168 luaL_dostring(L, "my_button:show();my_button:connect('2pressed()', my_button, '1close()')");
169 luaL_dostring(L, "my_button:setWindowTitle'lqt ROCKS!'");
170 luaL_dostring(L, "my_button:setText'Hello World'");
171 luaL_dostring(L, "my_button:setFixedSize(200, 100)");
172 qDebug() << lua_tostring(L, -1);
173 lua_settop(L, 0);
175 //QWidget *mainwin = new QWidget(0, 0);
177 luaL_dostring(L, "function override(t, k) local old_t = t[k] if type(old_t)~='function' then return end t[k] = function(...) print('overridden ', k, 'on', ...) return old_t(...) end end");
179 lqtL_pushudata(L, mainwin, "QWidget*");
180 lua_pushvalue(L, -1);
181 lua_setglobal(L, "mainwin");
182 luaL_dostring(L, "my_button:show();my_button:connect('2pressed()', mainwin, '1close()')");
184 oldtop = lua_gettop(L);
185 //luaL_dostring(L, "print(mainwin.isWidgetType)");
186 luaL_dostring(L, "print(mainwin:isWidgetType())");
187 luaL_dostring(L, "mainwin:setWindowTitle'TestTitle'");
188 qDebug() << lua_tostring(L, -1);
189 lua_settop(L, oldtop);
191 luaL_dostring(L, "override(mainwin, '__gc')");
192 //luaL_dostring(L, "override(mainwin, '__index')");
193 //luaL_dostring(L, "override(mainwin, '__newindex')");
194 //luaL_dostring(L, "mainwin.qt_metacast = nil");
195 //luaL_dostring(L, "print(mainwin.qt_metacast)");
197 luaL_dostring(L, "mainwin['~QWidget'] = function(obj) print('deleting', obj) end;");
198 //luaL_dostring(L, "local qtmc = mainwin.qt_metacast;"
199 //"mainwin.qt_metacast = function(...) print('overridden qt_metacast', ...) return qtmc(...) end;");
200 //luaL_dostring(L, "local hfw = mainwin.heightForWidth;"
201 //"mainwin.heightForWidth = function(...) print('overridden heightForWidth', ...) return hfw(...) end;");
202 //luaL_dostring(L, "print(mainwin.RenderFlag.IgnoreMask)");
204 QVBoxLayout *qvbl = new QVBoxLayout;
205 QTextEdit *qte = new QTextEdit;
206 QSpinBox *qsb = new QSpinBox;
207 qvbl->addWidget(qte);
208 qvbl->addWidget(qsb);
210 mainwin->setLayout(qvbl);
212 qDebug() << "==============";
214 lua_getfield(L, -1, "show");
215 lua_pushvalue(L, -2);
216 lua_call(L, 1, 0);
218 lua_getfield(L, -1, "width");
219 lua_pushvalue(L, -2);
220 lua_call(L, 1, 1);
221 qDebug() << lua_tonumber(L, -1) << mainwin->width();
222 lua_pop(L, 1);
224 lua_getfield(L, -1, "height");
225 lua_pushvalue(L, -2);
226 lua_call(L, 1, 1);
227 qDebug() << lua_tonumber(L, -1) << mainwin->height();
228 lua_pop(L, 1);
230 lua_getfield(L, -1, "isTopLevel");
231 lua_pushvalue(L, -2);
232 lua_call(L, 1, 1);
233 qDebug() << (bool)lua_toboolean(L, -1) << mainwin->isTopLevel();
234 lua_pop(L, 1);
236 qDebug() << lua_gettop(L) << lua_tostring(L, 2);
238 lua_getfield(L, -1, "heightForWidth");
239 lua_pushvalue(L, -2);
240 lua_pushinteger(L, 278);
241 lua_call(L, 2, 1);
242 qDebug() << lua_tonumber(L, -1) << mainwin->heightForWidth(278);
243 lua_pop(L, 1);
245 lua_getfield(L, -1, "new");
246 lua_call(L, 0, 1);
247 lua_getfield(L, -1, "show");
248 lua_pushvalue(L, -2);
249 lua_call(L, 1, 0);
250 lqtL_manageudata(L, -1);
251 lua_pop(L, 1);
253 //luaL_dostring(L, "child = mainwin.new();child:delete();");
254 lua_gc(L, LUA_GCCOLLECT, 0);
255 qDebug() << "=================";
256 luaL_dostring(L, "w = mainwin.new();w:show();w:move(400, 400)");
257 lua_gc(L, LUA_GCCOLLECT, 0);
258 qDebug() << "=================";
260 lua_getfield(L, -1, "move");
261 lua_pushvalue(L, -2);
262 lua_pushinteger(L, 100);
263 lua_pushinteger(L, 100);
264 lua_call(L, 3, 0);
266 //mainwin->show();
268 //delete mainwin;
270 //st = app->exec();
271 luaL_dostring(L, "app:exec()");
273 qDebug() << "I will now close Lua state";
274 #endif
275 lua_close(L);
277 return st;