2 * Copyright (c) 2007-2008 Mauro Iazzi
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
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);\
51 #include <QApplication>
52 #include <QPushButton>
57 #include <QVBoxLayout>
58 #include <QMessageBox>
63 static int retriever (lua_State
*L
) {
64 lua_pushvalue(L
, lua_upvalueindex(1));
68 static int F2S (lua_State
*L
) {
70 if (!lua_isfunction(L
, -1)) {
73 LuaFunction
*lf
= new LuaFunction(L
);
74 lua_pushstring(L
, "1function()");
78 int main(int argc
, char *argv
[])
80 QApplication
*app
= new QApplication(argc
, argv
);
84 lua_State
*L
= lua_open();
91 luaopen_QPushButton(L
);
92 luaopen_QAbstractButton(L
);
93 luaopen_QApplication(L
);
94 luaopen_QCoreApplication(L
);
96 luaopen_QAbstractScrollArea(L
);
100 luaopen_QBoxLayout(L
);
101 luaopen_QVBoxLayout(L
);
103 lua_pushcfunction(L
, F2S
);
104 lua_setglobal(L
, "SLOT");
112 EXPORT(QAbstractButton
);
114 EXPORT(QApplication
);
118 lua_pushvalue(L
, LUA_REGISTRYINDEX
);
119 lua_setglobal(L
, "_R");
122 local del = QEvent.delete\
125 function search (k, v)\
126 if v == del then print ('here', v) return true end\
127 if type(v) == 'table' then\
130 for i, e in pairs(v) do\
131 if search(i, e) then print (k) 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");
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");
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");
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);
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);
218 lua_getfield(L
, -1, "width");
219 lua_pushvalue(L
, -2);
221 qDebug() << lua_tonumber(L
, -1) << mainwin
->width();
224 lua_getfield(L
, -1, "height");
225 lua_pushvalue(L
, -2);
227 qDebug() << lua_tonumber(L
, -1) << mainwin
->height();
230 lua_getfield(L
, -1, "isTopLevel");
231 lua_pushvalue(L
, -2);
233 qDebug() << (bool)lua_toboolean(L
, -1) << mainwin
->isTopLevel();
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);
242 qDebug() << lua_tonumber(L
, -1) << mainwin
->heightForWidth(278);
245 lua_getfield(L
, -1, "new");
247 lua_getfield(L
, -1, "show");
248 lua_pushvalue(L
, -2);
250 lqtL_manageudata(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);
271 luaL_dostring(L
, "app:exec()");
273 qDebug() << "I will now close Lua state";