| 36 | } |
| 37 | |
| 38 | void BridgeForm::connect(QObject* sender, const QString& signalName, const QJSValue& handler) |
| 39 | { |
| 40 | if (!sender || !handler.isCallable()) { |
| 41 | Q_EMIT scriptError("connect -> Invalid sender or handler"); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | const QMetaObject* meta = sender->metaObject(); |
| 46 | for (int i = 0; i < meta->methodCount(); ++i) { |
| 47 | QMetaMethod method = meta->method(i); |
| 48 | if (method.methodType() != QMetaMethod::Signal) |
| 49 | continue; |
| 50 | |
| 51 | QString name = QString::fromLatin1(method.methodSignature()); |
| 52 | if (!name.startsWith(signalName)) |
| 53 | continue; |
| 54 | |
| 55 | int paramCount = method.parameterCount(); |
| 56 | auto* proxy = new SignalProxy(scriptEngine->engine(), handler, sender); |
| 57 | |
| 58 | bool connected = false; |
| 59 | |
| 60 | if (paramCount == 0) { |
| 61 | connected = QObject::connect(sender, method, proxy, proxy->metaObject()->method(proxy->metaObject()->indexOfSlot("call()"))); |
| 62 | } |
| 63 | else if (paramCount == 1) { |
| 64 | QByteArray typeName = method.parameterTypes().value(0); |
| 65 | if (typeName == "QString") |
| 66 | connected = QObject::connect(sender, method, proxy, proxy->metaObject()->method(proxy->metaObject()->indexOfSlot("callWithArg(QString)"))); |
| 67 | else if (typeName == "int") |
| 68 | connected = QObject::connect(sender, method, proxy, proxy->metaObject()->method(proxy->metaObject()->indexOfSlot("callWithArg(int)"))); |
| 69 | else if (typeName == "bool") |
| 70 | connected = QObject::connect(sender, method, proxy, proxy->metaObject()->method(proxy->metaObject()->indexOfSlot("callWithArg(bool)"))); |
| 71 | } |
| 72 | else if (paramCount == 2) { |
| 73 | QByteArray typeName1 = method.parameterTypes().value(0); |
| 74 | QByteArray typeName2 = method.parameterTypes().value(1); |
| 75 | if (typeName1 == "int" && typeName2 == "int") |
| 76 | connected = QObject::connect(sender, method, proxy, proxy->metaObject()->method(proxy->metaObject()->indexOfSlot("callWithArgs(int,int)"))); |
| 77 | } |
| 78 | else { |
| 79 | Q_EMIT scriptError("connect -> Signal " + signalName + " has too many parameters (not supported)"); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | if (!connected) |
| 84 | Q_EMIT scriptError("connect -> Failed to connect signal " + method.methodSignature()); |
| 85 | |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | Q_EMIT scriptError("connect -> Signal " + signalName + " not found"); |
| 90 | } |
| 91 | |
| 92 | /// Elements |
| 93 |
no test coverage detected