aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/lua/generateqtbindings.cpp
blob: c7ed25dfd8eed0e848cca452e8d482d994b7664b (plain)
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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "lua_global.h"

#include "luaapiregistry.h"
#include "luaengine.h"

#include <utils/layoutbuilder.h>

#include <QAbstractButton>
#include <QAbstractSlider>
#include <QAbstractSpinBox>
#include <QCalendarWidget>
#include <QComboBox>
#include <QDialog>
#include <QDialogButtonBox>
#include <QDockWidget>
#include <QElapsedTimer>
#include <QFocusFrame>
#include <QFrame>
#include <QGroupBox>
#include <QKeySequenceEdit>
#include <QLineEdit>
#include <QMainWindow>
#include <QMdiSubWindow>
#include <QMenu>
#include <QMenuBar>
#include <QMetaProperty>
#include <QProgressBar>
#include <QRubberBand>
#include <QSizeGrip>
#include <QSplashScreen>
#include <QSplitterHandle>
#include <QStatusBar>
#include <QTabBar>
#include <QTabWidget>
#include <QToolBar>
#include <QWizardPage>

#include <fstream>
#include <iostream>
#include <valarray>

namespace Lua::Internal {

QStringList baseClasses(const QMetaObject *metaObject)
{
    QStringList bases;
    const QMetaObject *base = metaObject;
    while ((base = base->superClass())) {
        bases << QString::fromLocal8Bit(base->className());
    }
    return bases;
}

bool isBaseClassProperty(QString name, const QMetaObject *metaObject)
{
    const QMetaObject *base = metaObject;
    while ((base = base->superClass())) {
        for (int i = 0; i < base->propertyCount(); ++i) {
            QMetaProperty p = base->property(i);
            if (QString::fromLocal8Bit(p.name()) == name)
                return true;
        }
    }
    return false;
}

template<class T>
QString createQObjectRegisterCode()
{
    // Add new types here when you've added "sol_lua_check, sol_lua_get and sol_lua_push" for them.
    // clang-format off
    static const QStringList whiteListedTypes = {
        "bool", "int", "double", "float",
        "QString", "QRect"};
    // clang-format on

    auto &metaObject = T::staticMetaObject;
    auto className = QString::fromLocal8Bit(metaObject.className());

    QStringList parts = {};

    // properties
    for (int i = 0; i < metaObject.propertyCount(); ++i) {
        QMetaProperty p = metaObject.property(i);
        QMetaType t = p.metaType();
        QString typeName = QString::fromLocal8Bit(t.name());
        QString propName = QString::fromLocal8Bit(p.name());
        if (isBaseClassProperty(propName, &metaObject))
            continue;

        if (!p.isEnumType() && !whiteListedTypes.contains(typeName)) {
            qDebug() << "Skipping" << p.name() << "of type" << typeName
                     << "as it is not whitelisted";
            continue;
        }

        QString propTemplate
            = QString("    \"%1\",\nsol::property(").arg(QString::fromLocal8Bit(p.name()));

        if (p.isReadable()) {
            QString readTemplate;

            if (p.isEnumType()) {
                readTemplate = QString(R"([](const %1 *obj) -> const char * {
                    auto p = %1::staticMetaObject.property(%2);
                    int v = p.read(obj).toInt();
                    return p.enumerator().valueToKey(v);
            })")
                                   .arg(className)
                                   .arg(i);

            } else {
                readTemplate = QString(R"([](const %1 *obj) -> %2 {
    return qvariant_cast<%2>(
        %1::staticMetaObject.property(%3).read(obj));
    })")
                                   .arg(className)
                                   .arg(typeName)
                                   .arg(i);
            }

            propTemplate += readTemplate;
        }

        if (p.isWritable()) {
            QString writeTemplate = ",\n";

            if (p.isEnumType()) {
                writeTemplate += QString(R"([](%1 *obj, const char *v) {
                    auto p = %1::staticMetaObject.property(%2);
                    int i = p.enumerator().keyToValue(v);
                    p.write(obj, i);
            })")
                                     .arg(className)
                                     .arg(i);

            } else {
                writeTemplate += QString(R"([](%1 *obj, %2 v) {
    %1::staticMetaObject.property(%3).write(obj, QVariant::fromValue(v));
    })")
                                     .arg(className)
                                     .arg(typeName)
                                     .arg(i);
            }

            propTemplate += writeTemplate;
        }

        propTemplate += ")";

        parts << propTemplate;
    }

    // Methods

    /*for (int i = 0; i < metaObject.methodCount(); i++) {
        QMetaMethod m = metaObject.method(i);
        QString methodTemplate;
        if (m.methodType() == QMetaMethod::Signal) {
            QString name = QString::fromLocal8Bit(m.name());
            name[0] = name[0].toUpper();
            name = "on" + name;

            methodTemplate = QString("    \"%1\",\n").arg(name);



        } else if (m.methodType() == QMetaMethod::Slot) {
        } else if (m.methodType() == QMetaMethod::Method) {
        } else if (m.methodType() == QMetaMethod::Constructor) {
        } else {
            qDebug() << "Unknown method type" << m.methodType();
        }

        templateString += methodTemplate;
    }*/

    // Generate base classes
    if (metaObject.superClass()) {
        parts << QString("sol::base_classes,\nsol::bases<%1>()")
                     .arg(baseClasses(&metaObject).join(','));
    }

    const QString registerFunctionTemplate = QString(R"(
static void register%1Bindings(sol::state &lua) {
    lua.new_usertype<%1>("%1",
                         %2
    );
}
    )")
                                                 .arg(className)
                                                 .arg(parts.join(",\n"));

    return registerFunctionTemplate;
}

template<class... Classes>
QStringList createQObjectRegisterCodes()
{
    QStringList result;
    ((result << createQObjectRegisterCode<Classes>()), ...);
    return result;
}

template<class... Classes>
QStringList createRegisterCalls()
{
    QStringList result;
    ((result << QString("register%1Bindings(lua);")
                    .arg(QString::fromLocal8Bit(Classes::staticMetaObject.className()))),
     ...);
    return result;
}

template<class... Classes>
QStringList createIncludeCalls()
{
    QStringList result;
    ((result << QString("#include <%1>")
                    .arg(QString::fromLocal8Bit(Classes::staticMetaObject.className()))),
     ...);
    return result;
}

template<class... Classes>
QString createBindings()
{
    QStringList registerFunctions = createQObjectRegisterCodes<Classes...>();

    QString finalFunction = QString(R"(
void registerUiBindings() {
    auto &lua = LuaEngine::instance().lua();
    %1
}
    )")
                                .arg(createRegisterCalls<Classes...>().join('\n'));

    return QString(R"(
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "lua_global.h"
#include "luaengine.h"

#include <QMetaProperty>

%1

namespace Lua::Internal
{
    %2

    %3
}
    )")
        .arg(createIncludeCalls<Classes...>().join('\n'))
        .arg(registerFunctions.join('\n'))
        .arg(finalFunction);
}

void createWidgetBindings()
{
    std::ofstream out("/tmp/bindings.cpp", std::ios::out | std::ios::trunc);

    out << createBindings<QObject,
                          QWidget,
                          QDialog,
                          QAbstractButton,
                          QAbstractSlider,
                          QAbstractSpinBox,
                          QCalendarWidget,
                          QComboBox,
                          QDialogButtonBox,
                          QDockWidget,
                          QFocusFrame,
                          QFrame,
                          QGroupBox,
                          QKeySequenceEdit,
                          QLineEdit,
                          QMainWindow,
                          QMdiSubWindow,
                          QMenu,
                          QMenuBar,
                          QProgressBar,
                          QRubberBand,
                          QSizeGrip,
                          QSplashScreen,
                          QSplitterHandle,
                          QStatusBar,
                          QTabBar,
                          QTabWidget,
                          QToolBar,
                          QWizardPage>()
               .toStdString()
        << std::endl;

    qDebug() << "Done!";
}

template<typename... T>
std::valarray<QString> createTypeBinding(QString typeName, QString fieldTypeName, T... fields)
{
    QString cppTemplateStr = R"(
// %1
bool sol_lua_check(sol::types<%1>,
                   lua_State *L,
                   int index,
                   std::function<sol::check_handler_type> handler,
                   sol::stack::record &tracking)
{ return sol::stack::check<sol::table>(L, index, handler, tracking); }
%1 sol_lua_get(sol::types<%1>, lua_State *L, int index, sol::stack::record &tracking)
{
    sol::state_view lua(L);
    sol::table table = sol::stack::get<sol::table>(L, index, tracking);
    return %2;
}
int sol_lua_push(sol::types<%1>, lua_State *L, const %1 &value)
{
    sol::state_view lua(L);
    sol::table table = lua.create_table();
    %3;
    return sol::stack::push(L, table);
}
)";

    QString headerTemplateStr = R"(SOL_CONVERSION_FUNCTIONS(%1)
)";

    auto createField = [&](const std::pair<QString, QString> &field) {
        return QString("table.get_or<%1, const char *, %1>(\"%2\", %3)")
            .arg(fieldTypeName)
            .arg(field.first)
            .arg(field.second);
    };
    QString createTypeFromTable = QString("%1(%2)").arg(typeName).arg(
        QStringList{createField(fields)...}.join(','));

    QString createTableFromType
        = QString("table.set(%1)")
              .arg(QStringList{QString(R"("%1", value.%1())").arg(fields.first)...}.join(','));

    return {cppTemplateStr.arg(typeName).arg(createTypeFromTable).arg(createTableFromType),
            headerTemplateStr.arg(typeName)};
}

void createTypeBindings()
{
    std::valarray<QString> code = {"", R"(
#define SOL_CONVERSION_FUNCTIONS(TYPE) \
    bool LUA_EXPORT sol_lua_check(sol::types<TYPE>, \
                                  lua_State *L, \
                                  int index, \
                                  std::function<sol::check_handler_type> handler, \
                                  sol::stack::record &tracking); \
    TYPE LUA_EXPORT sol_lua_get(sol::types<TYPE>, \
                                lua_State *L, \
                                int index, \
                                sol::stack::record &tracking); \
    int LUA_EXPORT sol_lua_push(sol::types<TYPE>, lua_State *L, const TYPE &rect);

    SOL_CONVERSION_FUNCTIONS(QString)

)"};

    using T = std::pair<QString, QString>;

    code += createTypeBinding("QRect",
                              "int",
                              T{"x", "0"},
                              T{"y", "0"},
                              T{"width", "0"},
                              T{"height", "0"});
    code += createTypeBinding("QSize", "int", T{"width", "0"}, T{"height", "0"});
    code += createTypeBinding("QPoint", "int", T{"x", "0"}, T{"y", "0"});

    code += createTypeBinding("QRectF",
                              "qreal",
                              T{"x", "0.0"},
                              T{"y", "0.0"},
                              T{"width", "0.0"},
                              T{"height", "0.0"});
    code += createTypeBinding("QSizeF", "qreal", T{"width", "0.0"}, T{"height", "0.0"});
    code += createTypeBinding("QPointF", "qreal", T{"x", "0.0"}, T{"y", "0.0"});

    code += createTypeBinding("QColor",
                              "int",
                              T{"red", "0"},
                              T{"green", "0"},
                              T{"blue", "0"},
                              T{"alpha", "255"});

    code[1] += "\n#undef SOL_CONVERSION_FUNCTIONS\n";

    qDebug().noquote() << code[0];
    qDebug().noquote() << code[1];
}

} // namespace Lua::Internal