Selaa lähdekoodia

Log error if registerHostCommand() maps to an unknown method

The method name could simply not exist. Or what we had a lot in the
past, the Q_INVOKABLE marker on the method could have forgotten.

remapInput() is modified to avoid duplication of logic somewhat.
Vincent Lang 9 vuotta sitten
vanhempi
commit
c6b683b9be
2 muutettua tiedostoa jossa 20 lisäystä ja 4 poistoa
  1. 19 4
      src/input/InputComponent.cpp
  2. 1 0
      src/input/InputComponent.h

+ 19 - 4
src/input/InputComponent.cpp

@@ -104,16 +104,14 @@ void InputComponent::remapInput(const QString &source, const QString &keycode, f
         ReceiverSlot* recvSlot = m_hostCommands.value(hostCommand);
         if (recvSlot)
         {
-          QString slotWithArgs = QString("%1(QString)").arg(QString::fromLatin1(recvSlot->slot));
-          QLOG_DEBUG() << "Looking for method:" << slotWithArgs;
-          if (recvSlot->receiver->metaObject()->indexOfMethod(slotWithArgs.toLatin1().data()) != -1)
+          QLOG_DEBUG() << "Invoking slot" << qPrintable(recvSlot->slot.data());
+          if (recvSlot->hasArguments)
           {
             QMetaObject::invokeMethod(recvSlot->receiver, recvSlot->slot.data(),
                                       Qt::AutoConnection, Q_ARG(const QString&, hostArguments));
           }
           else
           {
-            QLOG_DEBUG() << "Method has no arguments:" << recvSlot->slot.data();
             QMetaObject::invokeMethod(recvSlot->receiver, recvSlot->slot.data(), Qt::AutoConnection);
           }
         }
@@ -141,8 +139,25 @@ void InputComponent::registerHostCommand(const QString& command, QObject* receiv
   ReceiverSlot* recvSlot = new ReceiverSlot;
   recvSlot->receiver = receiver;
   recvSlot->slot = QMetaObject::normalizedSignature(slot);
+  recvSlot->hasArguments = false;
 
   QLOG_DEBUG() << "Adding host command:" << qPrintable(command) << "mapped to" << qPrintable(QString(receiver->metaObject()->className()) + "::" + recvSlot->slot);
 
   m_hostCommands.insert(command, recvSlot);
+
+  auto slotWithArgs = QString("%1(QString)").arg(QString::fromLatin1(recvSlot->slot)).toLatin1().data();
+  auto slotWithoutArgs = QString("%1()").arg(QString::fromLatin1(recvSlot->slot)).toLatin1().data();
+  if (recvSlot->receiver->metaObject()->indexOfMethod(slotWithArgs) != -1)
+  {
+    QLOG_DEBUG() << "Host command maps to method with an argument.";
+    recvSlot->hasArguments = true;
+  }
+  else if (recvSlot->receiver->metaObject()->indexOfMethod(slotWithoutArgs) != -1)
+  {
+    QLOG_DEBUG() << "Host command maps to method without arguments.";
+  }
+  else
+  {
+    QLOG_ERROR() << "Slot for host command missing, or has incorrect signature!";
+  }
 }

+ 1 - 0
src/input/InputComponent.h

@@ -70,6 +70,7 @@ struct ReceiverSlot
 {
   QObject* receiver;
   QByteArray slot;
+  bool hasArguments;
 };
 
 class InputComponent : public ComponentBase