output is split over several files
[lqt.git] / old_bindings / script / test.cpp
blob53914bf31e0c84a0a8bd94adf6fdf478636f5113
1 /*
2 * Copyright (c) 2007 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.hpp"
29 #include <QApplication>
30 #include <QPushButton>
31 #include <QDebug>
32 #include <QSlider>
33 #include <QSpinBox>
34 #include <QTextEdit>
35 #include <QVBoxLayout>
36 #include <QMessageBox>
37 #include <QShortcut>
39 #include "luadummy.hpp"
41 static int my_slot (lua_State *L) {
42 qDebug() << "my_slot called with" << lua_gettop(L) << "arguments";
43 if ( lua_gettop(L)==0 ) {
44 lua_getglobal(L, "text_edit");
45 lua_getfield(L, 1, "plainText");
46 lua_remove(L, 1);
47 luaL_dostring(L, lua_tostring(L, 1));
48 lua_remove(L, 1);
50 QString mess("");
51 for (int i=1;i<=lua_gettop(L);i++) {
52 QVariant val = lua_toqvariant(L, i);
53 mess.append(QString::number(i));
54 mess.append(" => ");
55 mess.append(val.toString());
56 mess.append('\n');
58 QMessageBox::information(0, "Results", mess);
59 return 0;
62 static int lua_pushshortcut(lua_State *L) {
63 QWidget *parent = qobject_cast<QWidget*>(luaL_checkqobject(L, 1));
64 QShortcut *sc = new QShortcut(parent);
65 lua_pushqobject(L, sc);
66 return 1;
69 static int lua_slot(lua_State *L) {
70 lua_pushvalue(L, 1);
71 QObject *dummy = new LuaDummy(L);
72 lua_pushqobject(L, dummy);
73 return 1;
77 int main(int argc, char *argv[])
79 QApplication app(argc, argv);
80 int st = 0;
81 lua_State *L = lua_open();
82 luaL_openlibs(L);
84 lua_newtable(L);
85 lua_pushcfunction(L, lua_pushshortcut);
86 lua_setfield(L, -2, "new");
87 lua_setglobal(L, "QShortcut");
89 lua_pushcfunction(L, lua_slot);
90 lua_setglobal(L, "SLOT");
92 lua_newtable(L);
93 lua_pushcfunction(L, lqt_connect);
94 lua_setfield(L, -2, "connect");
95 lua_setglobal(L, "qt");
97 luaL_checkstack(L, 15, "shit");
99 // QSlider *hello2 = new QSlider;
100 // QSpinBox *hello = new QSpinBox;
101 // hello->resize(100, 30);
102 // hello->show();
103 // hello2->resize(100, 100);
104 // hello2->show();
106 // lua_pushqobject(L, hello);
107 // lua_pushqobject(L, hello);
109 // lua_getfield(L, -1, "setValue");
110 // lua_pushqobject(L, hello);
111 // lua_pushinteger(L, 99);
112 // lua_call(L, 2, 0);
114 // lua_pushqobject(L, hello);
115 // lua_pushstring(L, "suffix");
116 // lua_pushstring(L, "suffix");
117 // lua_settable(L, -3);
118 // lua_pushstring(L, "prefix");
119 // lua_pushstring(L, "prefix");
120 // lua_settable(L, -3);
121 // lua_getfield(L, -1, "cleanText");
122 // qDebug() << lua_tostring(L, -1);
124 // lua_pushcfunction(L, lqt_connect);
125 // lua_pushqobject(L, hello2);
126 // lua_pushstring(L, "valueChanged(int)");
127 // lua_pushqobject(L, hello);
128 // lua_pushstring(L, "setValue ( int ) ");
129 // lua_call(L, 4, 1);
130 // qDebug() << (bool)lua_toboolean(L, -1);
131 // lua_pop(L, 1);
132 // lua_pushcfunction(L, lqt_disconnect);
133 // lua_pushqobject(L, hello);
134 // lua_pushstring(L, "valueChanged(int)");
135 // lua_pushqobject(L, hello2);
136 // lua_pushstring(L, "setValue ( int ) ");
137 // lua_call(L, 4, 1);
138 // qDebug() << (bool)lua_toboolean(L, -1);
139 // lua_pop(L, 1);
142 // ???????
143 /* lua_pushcfunction(L, lqt_connect);
144 lua_pushqobject(L, &hello);
145 lua_pushstring(L, "destroyed()");
146 lua_pushqobject(L, &hello2);
147 lua_pushstring(L, "deleteLater()");
148 lua_call(L, 4, 1);
149 qDebug() << (bool)lua_toboolean(L, -1);
150 lua_pop(L, 1);
151 lua_pushcfunction(L, lqt_connect);
152 lua_pushqobject(L, &hello2);
153 lua_pushstring(L, "destroyed()");
154 lua_pushqobject(L, &hello);
155 lua_pushstring(L, "deleteLater()");
156 lua_call(L, 4, 1);
157 qDebug() << (bool)lua_toboolean(L, -1);
158 lua_pop(L, 1);
159 QObject::connect(&hello, SIGNAL(destroyed()), &hello2, SLOT(deleteLater()));*/
162 /* lua_pushqobject(L, b);
163 lua_pop(L, 3);
164 delete b;
165 delete a;*/
168 // lua_pushqobject(L, &hello);
169 // QVariant var = lua_toqvariant(L, -1);
170 // lua_pop(L, 1);
171 // qDebug() << var.typeName();
174 QPushButton *a = new QPushButton("Exec");
175 // lua_pushqobject(L, a);
176 // lua_getfield(L, -1, "close");
177 // lua_pushqobject(L, a);
178 // lua_call(L, 1, 1);
179 /* qDebug() << lua_toqvariant(L, -1);*/
180 // delete a;
181 // lua_getfield(L, -1, "text");
182 // qDebug() << lua_toqvariant(L, -1);
183 // lua_pop(L, 1);
184 // lua_pop(L, 1);
186 QWidget mainwin;
188 lua_pushqobject(L, &mainwin);
189 lua_setglobal(L, "mainwin");
191 QVBoxLayout *qvbl = new QVBoxLayout;
192 QTextEdit *qte = new QTextEdit;
193 QSpinBox *qsb = new QSpinBox;
194 qvbl->addWidget(qte);
195 // qvbl->addWidget(hello);
196 // qvbl->addWidget(hello2);
197 qvbl->addWidget(a);
198 qvbl->addWidget(qsb);
200 mainwin.setLayout(qvbl);
201 mainwin.show();
203 // lua_pushcfunction(L, methods_of);
204 lua_pushqobject(L, qte);
205 lua_setglobal(L, "text_edit");
206 // lua_call(L, 1, 0);
209 lua_pushcfunction(L, my_slot);
210 int t = lua_gettop(L);
213 // lua_pushcfunction(L, methods_of);
214 lua_pushvalue(L, t);
215 QObject * dummy2 = new LuaDummy(L);
216 // lua_call(L, 1, 0);
220 lua_pushcfunction(L, lqt_connect);
221 lua_pushqobject(L, a);
222 lua_pushstring(L, "pressed ()");
224 lua_pushvalue(L, t);
225 QObject * dummy1 = new LuaDummy(L);
226 lua_pushstring(L, "generic_slot");
227 lua_call(L, 4, 1);
228 qDebug() << (bool)lua_toboolean(L, -1);
229 lua_pop(L, 1);
232 lua_pushcfunction(L, lqt_connect);
233 lua_pushqobject(L, qsb);
234 lua_pushstring(L, "valueChanged");
236 lua_pushvalue(L, t);
237 QObject * dummy3 = new LuaDummy(L);
238 lua_pushstring(L, "generic_slot(int)");
239 lua_call(L, 4, 1);
240 qDebug() << (bool)lua_toboolean(L, -1);
241 lua_pop(L, 1);
243 QShortcut *sc = new QShortcut(&mainwin);
244 lua_pushcfunction(L, lqt_connect);
245 lua_pushqobject(L, sc);
246 lua_pushstring(L, "Ctrl+P");
247 lua_setfield(L, -2, "key");
248 lua_pushstring(L, "activated()");
249 lua_pushvalue(L, t);
250 QObject * dummy4 = new LuaDummy(L);
251 lua_pushstring(L, "generic_slot");
252 lua_call(L, 4, 1);
253 qDebug() << (bool)lua_toboolean(L, -1);
254 lua_pop(L, 2);
256 luaL_dostring(L, "return function (...) print(...) return true end");
257 QObject * dummy5 = new LuaDummy(L);
258 qsb->installEventFilter(dummy5);
262 // return 0;
263 // QMetaObject::invokeMethod(&hello, "setDisabled", Q_ARG(bool, true));
264 // QGenericArgument a(Q_ARG(bool, true));
265 // qDebug() << a.data() << a.name();
268 st = app.exec();
270 lua_close(L);
272 // QObject *pippo = new QObject::QObject;
274 return st;