fixed old include in lqt_common.cpp
[lqt.git] / main.cpp
blob8e9dbafc1e2c46cf6bc7eab7d35e9be7430a1ed1
1 #include "common_bind.hpp"
2 #include "luafunction.hpp"
3 #include "lqt_bind_QLabel.hpp"
4 #include "lqt_bind_QCoreApplication.hpp"
5 #include "lqt_bind_QApplication.hpp"
6 #include "lqt_bind_QAbstractButton.hpp"
7 #include "lqt_bind_QPushButton.hpp"
8 #include "lqt_bind_QObject.hpp"
9 #include "lqt_bind_QFont.hpp"
10 #include "lqt_bind_QWidget.hpp"
11 #include "lqt_bind_QTextEdit.hpp"
12 #include "lqt_bind_QAbstractScrollArea.hpp"
13 #include "lqt_bind_QFrame.hpp"
14 #include "lqt_bind_QEvent.hpp"
15 #include "lqt_bind_QLayout.hpp"
16 #include "lqt_bind_QBoxLayout.hpp"
17 #include "lqt_bind_QVBoxLayout.hpp"
19 #define EXPORT(n) do { \
20 luaL_getmetatable(L,#n "*" );\
21 lua_setglobal(L, #n);\
22 } while (0)
25 #include <QApplication>
26 #include <QPushButton>
27 #include <QDebug>
28 #include <QSlider>
29 #include <QSpinBox>
30 #include <QTextEdit>
31 #include <QVBoxLayout>
32 #include <QMessageBox>
33 #include <QShortcut>
34 #include <QWidget>
35 // */
37 static int retriever (lua_State *L) {
38 lua_pushvalue(L, lua_upvalueindex(1));
39 return 1;
42 static int F2S (lua_State *L) {
43 lua_settop(L, 1);
44 if (!lua_isfunction(L, -1)) {
45 return 0;
47 LuaFunction *lf = new LuaFunction(L);
48 lua_pushstring(L, "1function()");
49 return 2;
52 int main(int argc, char *argv[])
54 QApplication *app = new QApplication(argc, argv);
56 int st = 0;
57 int oldtop = 0;
58 lua_State *L = lua_open();
59 luaL_openlibs(L);
60 luaopen_QEvent(L);
61 luaopen_QObject(L);
62 luaopen_QFont(L);
63 luaopen_QWidget(L);
64 luaopen_QLabel(L);
65 luaopen_QPushButton(L);
66 luaopen_QAbstractButton(L);
67 luaopen_QApplication(L);
68 luaopen_QCoreApplication(L);
69 luaopen_QTextEdit(L);
70 luaopen_QAbstractScrollArea(L);
71 luaopen_QFrame(L);
73 luaopen_QLayout(L);
74 luaopen_QBoxLayout(L);
75 luaopen_QVBoxLayout(L);
77 lua_pushcfunction(L, F2S);
78 lua_setglobal(L, "SLOT");
80 #if 1
81 EXPORT(QWidget);
82 EXPORT(QObject);
83 EXPORT(QLabel);
84 EXPORT(QFont);
85 EXPORT(QEvent);
86 EXPORT(QAbstractButton);
87 EXPORT(QPushButton);
88 EXPORT(QApplication);
89 EXPORT(QTextEdit);
90 EXPORT(QVBoxLayout);
92 lua_pushvalue(L, LUA_REGISTRYINDEX);
93 lua_setglobal(L, "_R");
94 luaL_dostring(L, "\
95 do\
96 local del = QEvent.delete\
97 local done = {}\
98 local search\
99 function search (k, v)\
100 if v == del then print ('here', v) return true end\
101 if type(v) == 'table' then\
102 if not done[v] then\
103 done[v]=true\
104 for i, e in pairs(v) do\
105 if search(i, e) then print (k) end\
106 end\
107 end\
108 end\
109 end\
110 search('_G', _G)\
111 end");
114 lqtL_pushudata(L, app, "QApplication*");
115 lua_pushcclosure(L, retriever, 1);
116 lua_setglobal(L, "app");
118 lua_getglobal(L, "QApplication");
119 lua_getglobal(L, "app");
120 lua_setfield(L, -2, "new");
122 lua_pushnil(L);
123 lua_setglobal(L, "app");
126 luaL_dofile(L, "tutorial.lua");
127 qDebug() << lua_tostring(L, -2) << lua_tostring(L, -1);
129 lua_getglobal(L, "PB");
131 #else
132 lua_checkstack(L, 15);
134 QWidget *mainwin = new LuaBinder<QWidget>(L, 0, 0);
136 lqtL_pushudata(L, app, "QApplication*");
137 lua_setglobal(L, "app");
139 QPushButton butt;
140 lqtL_pushudata(L, &butt, "QPushButton*");
141 lua_setglobal(L, "my_button");
142 luaL_dostring(L, "my_button:show();my_button:connect('2pressed()', my_button, '1close()')");
143 luaL_dostring(L, "my_button:setWindowTitle'lqt ROCKS!'");
144 luaL_dostring(L, "my_button:setText'Hello World'");
145 luaL_dostring(L, "my_button:setFixedSize(200, 100)");
146 qDebug() << lua_tostring(L, -1);
147 lua_settop(L, 0);
149 //QWidget *mainwin = new QWidget(0, 0);
151 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");
153 lqtL_pushudata(L, mainwin, "QWidget*");
154 lua_pushvalue(L, -1);
155 lua_setglobal(L, "mainwin");
156 luaL_dostring(L, "my_button:show();my_button:connect('2pressed()', mainwin, '1close()')");
158 oldtop = lua_gettop(L);
159 //luaL_dostring(L, "print(mainwin.isWidgetType)");
160 luaL_dostring(L, "print(mainwin:isWidgetType())");
161 luaL_dostring(L, "mainwin:setWindowTitle'TestTitle'");
162 qDebug() << lua_tostring(L, -1);
163 lua_settop(L, oldtop);
165 luaL_dostring(L, "override(mainwin, '__gc')");
166 //luaL_dostring(L, "override(mainwin, '__index')");
167 //luaL_dostring(L, "override(mainwin, '__newindex')");
168 //luaL_dostring(L, "mainwin.qt_metacast = nil");
169 //luaL_dostring(L, "print(mainwin.qt_metacast)");
171 luaL_dostring(L, "mainwin['~QWidget'] = function(obj) print('deleting', obj) end;");
172 //luaL_dostring(L, "local qtmc = mainwin.qt_metacast;"
173 //"mainwin.qt_metacast = function(...) print('overridden qt_metacast', ...) return qtmc(...) end;");
174 //luaL_dostring(L, "local hfw = mainwin.heightForWidth;"
175 //"mainwin.heightForWidth = function(...) print('overridden heightForWidth', ...) return hfw(...) end;");
176 //luaL_dostring(L, "print(mainwin.RenderFlag.IgnoreMask)");
178 QVBoxLayout *qvbl = new QVBoxLayout;
179 QTextEdit *qte = new QTextEdit;
180 QSpinBox *qsb = new QSpinBox;
181 qvbl->addWidget(qte);
182 qvbl->addWidget(qsb);
184 mainwin->setLayout(qvbl);
186 qDebug() << "==============";
188 lua_getfield(L, -1, "show");
189 lua_pushvalue(L, -2);
190 lua_call(L, 1, 0);
192 lua_getfield(L, -1, "width");
193 lua_pushvalue(L, -2);
194 lua_call(L, 1, 1);
195 qDebug() << lua_tonumber(L, -1) << mainwin->width();
196 lua_pop(L, 1);
198 lua_getfield(L, -1, "height");
199 lua_pushvalue(L, -2);
200 lua_call(L, 1, 1);
201 qDebug() << lua_tonumber(L, -1) << mainwin->height();
202 lua_pop(L, 1);
204 lua_getfield(L, -1, "isTopLevel");
205 lua_pushvalue(L, -2);
206 lua_call(L, 1, 1);
207 qDebug() << (bool)lua_toboolean(L, -1) << mainwin->isTopLevel();
208 lua_pop(L, 1);
210 qDebug() << lua_gettop(L) << lua_tostring(L, 2);
212 lua_getfield(L, -1, "heightForWidth");
213 lua_pushvalue(L, -2);
214 lua_pushinteger(L, 278);
215 lua_call(L, 2, 1);
216 qDebug() << lua_tonumber(L, -1) << mainwin->heightForWidth(278);
217 lua_pop(L, 1);
219 lua_getfield(L, -1, "new");
220 lua_call(L, 0, 1);
221 lua_getfield(L, -1, "show");
222 lua_pushvalue(L, -2);
223 lua_call(L, 1, 0);
224 lqtL_manageudata(L, -1);
225 lua_pop(L, 1);
227 //luaL_dostring(L, "child = mainwin.new();child:delete();");
228 lua_gc(L, LUA_GCCOLLECT, 0);
229 qDebug() << "=================";
230 luaL_dostring(L, "w = mainwin.new();w:show();w:move(400, 400)");
231 lua_gc(L, LUA_GCCOLLECT, 0);
232 qDebug() << "=================";
234 lua_getfield(L, -1, "move");
235 lua_pushvalue(L, -2);
236 lua_pushinteger(L, 100);
237 lua_pushinteger(L, 100);
238 lua_call(L, 3, 0);
240 //mainwin->show();
242 //delete mainwin;
244 //st = app->exec();
245 luaL_dostring(L, "app:exec()");
247 qDebug() << "I will now close Lua state";
248 #endif
249 lua_close(L);
251 return st;