-
Notifications
You must be signed in to change notification settings - Fork 2
/
reflexive.cpp
408 lines (356 loc) · 13.7 KB
/
reflexive.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
pqConsole : interfacing SWI-Prolog and Qt
Author : Carlo Capelli
E-mail : cc.carlo.cap@gmail.com
Copyright (C): 2013,2014 Carlo Capelli
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define PROLOG_MODULE "pqConsole"
#include "PREDICATE.h"
#include "pqConsole.h"
#include <QStack>
#include <QDebug>
#include <QMetaObject>
#include <QMetaType>
#include <QVarLengthArray>
#include <QDate>
#include <QTime>
#include <QDateTime>
#include <QPixmap>
//! collect all registered Qt types
PREDICATE(types, 1) {
PlTail l(PL_A1);
for (int t = 0; t < (1 << 20); ++t)
if (QMetaType::isRegistered(t)) {
const char* n = QMetaType::typeName(t);
if (n && *n)
l.append(n);
}
l.close();
return TRUE;
}
structure2(pqObj)
//! create a meta-instantiable Qt object
PREDICATE(create, 2) {
VP obj = 0;
QString name = t2w(PL_A1);
long type = QMetaType::type(name.toUtf8());
if (type)
pqConsole::gui_run([&](){
obj = QMetaType::create(type); // calls default constructor here
});
if (obj)
return PL_A2 = pqObj(type, obj);
throw PlException(A(QString("create '%1' failed").arg(name)));
}
static QVariant T2V(PlTerm pl)
{
switch (pl.type()) {
case PL_INTEGER:
return QVariant(qlonglong(long(pl)));
case PL_FLOAT:
return QVariant(double(pl));
case PL_ATOM:
return QVariant(t2w(pl));
case PL_TERM: {
int ty = QMetaType::type(pl.name()), ar = pl.arity();
switch (ty) {
case QMetaType::QSize:
return QSize(pl[1], pl[2]);
case QMetaType::QSizeF:
return QSizeF(pl[1], pl[2]);
case QMetaType::QDate:
return QDate(pl[1], pl[2], pl[3]);
case QMetaType::QTime:
return ar == 2 ? QTime(pl[1], pl[2]) :
ar == 3 ? QTime(pl[1], pl[2], pl[3]) :
QTime(pl[1], pl[2], pl[3], pl[4]);
case QMetaType::QDateTime:
return QDateTime(T2V(pl[1]).toDate(), T2V(pl[2]).toTime());
case QMetaType::QUrl: // TBD
return QUrl(T2V(pl[1]).toString());
// TBD QLocale();
case QMetaType::QRect:
if (ar == 2) {
QVariant a[2] = { T2V(pl[1]), T2V(pl[2]) };
if (a[0].type() == QVariant::Point && a[1].type() == QVariant::Point)
return QRect(a[0].toPoint(), a[1].toPoint());
if (a[0].type() == QVariant::Point && a[1].type() == QVariant::Size)
return QRect(a[0].toPoint(), a[1].toSize());
break;
}
return QRect(pl[1], pl[2], pl[3], pl[4]);
case QMetaType::QRectF:
if (ar == 2) {
QVariant a[2] = { T2V(pl[1]), T2V(pl[2]) };
if (a[0].type() == QVariant::PointF && a[1].type() == QVariant::PointF)
return QRectF(a[0].toPointF(), a[1].toPointF());
if (a[0].type() == QVariant::PointF && a[1].type() == QVariant::SizeF)
return QRectF(a[0].toPointF(), a[1].toSizeF());
break;
}
return QRectF(pl[1], pl[2], pl[3], pl[4]);
case QMetaType::QLine:
return ar == 2 ? QLine(T2V(pl[1]).toPoint(), T2V(pl[2]).toPoint()) :
QLine(pl[1], pl[2], pl[3], pl[4]);
case QMetaType::QLineF:
return ar == 2 ? QLineF(T2V(pl[1]).toPointF(), T2V(pl[2]).toPointF()) :
QLineF(pl[1], pl[2], pl[3], pl[4]);
case QMetaType::QPoint:
return QPoint(pl[1], pl[2]);
case QMetaType::QPointF:
return QPointF(pl[1], pl[2]);
/*
case QMetaType::QBrush:
return QBrush();
*/
}
} }
throw PlException("cannot convert PlTerm to QVariant");
}
/** invoke(Object, Member, Args, Retv)
* note: pointers should be registered to safely exchange them
*/
PREDICATE(invoke, 4) {
T ttype, tptr;
PL_A1 = pqObj(ttype, tptr);
QObject *obj = pq_cast<QObject>(tptr);
if (obj) {
QString Member = t2w(PL_A2);
// iterate superClasses
for (const QMetaObject *meta = obj->metaObject(); meta; meta = meta->superClass()) {
for (int i = 0; i < meta->methodCount(); ++i) {
QMetaMethod m = meta->method(i);
if (m.methodType() == m.Method && m.access() == m.Public) {
QString sig = m.methodSignature();
if (sig.left(sig.indexOf('(')) == Member) {
QVariantList vl;
QList<QGenericArgument> va;
// scan the argument list
L Args(PL_A3); T Arg;
int ipar = 0;
for ( ; Args.next(Arg); ++ipar) {
if (ipar == m.parameterTypes().size())
throw PlException("argument list count mismatch (too much arguments)");
// match variant type
int vt = QMetaType::type(m.parameterTypes()[ipar]);
switch (Arg.type()) {
case PL_ATOM:
switch (vt) {
case QVariant::String:
vl.append(t2w(Arg));
break;
default:
throw PlException("invalid type");
}
break;
case PL_INTEGER:
if (vt == QVariant::Int) {
qlonglong l = long(Arg);
vl.append(l);
} else if (vt == QMetaType::VoidStar ||
vt == QMetaType::QObjectStar ||
vt == qMetaTypeId<QWidget*>() ||
vt >= QMetaType::User) {
VP p = Arg;
vl.append(QVariant(vt, &p));
}
else
throw PlException("invalid type");
break;
case PL_FLOAT:
if (vt == QVariant::Double)
vl.append(double(Arg));
else
throw PlException("invalid type");
break;
default:
throw PlException("unknown type conversion");
}
va.append(QGenericArgument(vl.back().typeName(), vl.back().data()));
}
if (ipar < m.parameterTypes().size())
throw PlException("argument list count mismatch (miss arguments)");
if (m.parameterTypes().size() > 3)
throw PlException("unsupported call (max 3 arguments)");
// optional return value
int trv = QMetaType::type(m.typeName());
if (trv == QMetaType::Void)
trv = 0; // was 0 in Qt 4
QVariant rv(trv, static_cast<void*>(NULL));
QGenericReturnArgument ra(m.typeName(), rv.data());
bool rc = false;
pqConsole::gui_run([&]() {
switch (m.parameterTypes().size()) {
case 0:
rc = trv ? m.invoke(obj, ra) : m.invoke(obj);
break;
case 1:
rc = trv ? m.invoke(obj, ra, va[0]) : m.invoke(obj, va[0]);
break;
case 2:
rc = trv ? m.invoke(obj, ra, va[0], va[1]) : m.invoke(obj, va[0], va[1]);
break;
case 3:
rc = trv ? m.invoke(obj, ra, va[0], va[1], va[2]) : m.invoke(obj, va[0], va[1], va[2]);
break;
}
});
if (rc && trv) {
// TBD unify return value
switch (trv) {
case QMetaType::Int:
PL_A4 = rv.toInt();
break;
case QMetaType::UInt:
PL_A4 = static_cast<int>(rv.toUInt());
break;
case QMetaType::LongLong:
PL_A4 = static_cast<int>(rv.toLongLong());
break;
case QMetaType::ULongLong:
PL_A4 = static_cast<int>(rv.toULongLong());
break;
default:
throw PlException("unsupported return type");
}
}
return rc;
}
}
}
}
}
throw PlException("pq_method failed");
}
/** property(Object, Property, Value)
* read/write a property by name
*/
PREDICATE(property, 3) {
T ttype, tptr;
PL_A1 = pqObj(ttype, tptr);
QObject *obj = pq_cast<QObject>(tptr);
if (obj) {
// from actual class up to QObject
for (const QMetaObject *meta = obj->metaObject(); meta; meta = meta->superClass()) {
int ip = meta->indexOfProperty(t2w(PL_A2).toUtf8());
if (ip >= 0) {
QMetaProperty p = meta->property(ip);
QVariant v;
bool isvar = PL_A3.type() == PL_VARIABLE, rc = false;
if (!isvar)
v = T2V(PL_A3);
pqConsole::gui_run([&]() {
if (isvar) {
v = p.read(obj);
rc = true;
}
else {
rc = p.write(obj, v);
}
});
return rc;
}
}
}
throw PlException("pq_property failed");
}
/** unify a property of QObject:
* allows read/write of simple atomic values
*/
QString pqConsole::unify(const QMetaProperty& p, QObject *o, PlTerm v) {
#define OK return QString()
QVariant V;
switch (v.type()) {
case PL_VARIABLE: {
gui_run([&]() { V = p.read(o); });
switch (p.type()) {
case QVariant::Bool:
v = V.toBool() ? A("true") : A("false");
OK;
case QVariant::Int:
if (p.isEnumType()) {
Q_ASSERT(!p.isFlagType()); // TBD
QMetaEnum e = p.enumerator();
if (CCP key = e.valueToKey(V.toInt())) {
v = A(key);
OK;
}
}
v = long(V.toInt());
OK;
case QVariant::UInt:
v = long(V.toUInt());
OK;
case QVariant::String:
v = A(V.toString());
OK;
default:
break;
}
} break;
case PL_INTEGER:
switch (p.type()) {
case QVariant::Int:
case QVariant::UInt:
V = qint32(v);
break;
default:
break;
}
break;
case PL_ATOM:
switch (p.type()) {
case QVariant::String:
V = t2w(v);
break;
case QVariant::Int:
if (p.isEnumType()) {
Q_ASSERT(!p.isFlagType()); // TBD
int i = p.enumerator().keyToValue(v);
if (i != -1)
V = i;
}
default:
break;
}
break;
case PL_FLOAT:
switch (p.type()) {
case QVariant::Double:
V = double(v);
break;
default:
break;
}
break;
default:
break;
}
if (V.isValid()) {
bool retok = false;
gui_run([&]() { if (p.write(o, V)) retok = true; });
if (retok)
OK;
}
return o->tr("property %1: type mismatch").arg(p.name());
}
/** unify a property of QObject, seek by name:
* allows read/write of basic atomic values (note: enums are symbolics)
*/
QString pqConsole::unify(CCP name, QObject *o, PlTerm v) {
int pid = o->metaObject()->indexOfProperty(name);
if (pid >= 0)
return unify(o->metaObject()->property(pid), o, v);
return o->tr("property %1: not found").arg(name);
}