Ver Fonte

Map input to several actions

This allows multiple actions for one button to be sent to the web-client. Necessary for getting search to work properly

Fixes #214
Tobias Hieta há 8 anos atrás
pai
commit
a86ca1d4fa

+ 3 - 0
resources/inputmaps/keyboard.json

@@ -17,6 +17,9 @@
     // map X and Shift+X to X. This allows normal number input
     "(?:Shift\\+)?([0-9])": "%1",
 
+    // map X and Shift+X to X for letters. This allows normal text input
+    "(?:Shift\\+)?([A-Z])": "%1",
+
     // map some other normal buttons that might be useful
     "(?:Shift\\+)?(\\.|\\:|\\_)": "%1",
 

+ 16 - 13
src/input/InputComponent.cpp

@@ -167,28 +167,31 @@ void InputComponent::remapInput(const QString &source, const QString &keycode, b
   // hide mouse if it's visible.
   SystemComponent::Get().setCursorVisibility(false);
 
-  QVariant action = m_mappings->mapToAction(source, keycode);
-  if (action.isNull())
+  auto actions = m_mappings->mapToAction(source, keycode);
+  if (actions.isEmpty())
   {
     QLOG_WARN() << "Could not map:" << source << keycode << "to any useful action";
     return;
   }
 
-  if (action.type() == QVariant::String)
+  for (auto action : actions)
   {
-    handleAction(action.toString());
-  }
-  else if (action.type() == QVariant::Map)
-  {
-    QVariantMap map = action.toMap();
-    if (map.contains("long"))
+    if (action.type() == QVariant::String)
     {
-      m_longHoldTimer.start();
-      m_currentLongPressAction = map;
+      handleAction(action.toString());
     }
-    else if (map.contains("short"))
+    else if (action.type() == QVariant::Map)
     {
-      handleAction(map.value("short").toString());
+      QVariantMap map = action.toMap();
+      if (map.contains("long"))
+      {
+        m_longHoldTimer.start();
+        m_currentLongPressAction = map;
+      }
+      else if (map.contains("short"))
+      {
+        handleAction(map.value("short").toString());
+      }
     }
   }
 }

+ 8 - 10
src/input/InputMapping.cpp

@@ -52,21 +52,19 @@ bool InputMapping::loadMappings()
 }
 
 /////////////////////////////////////////////////////////////////////////////////////////
-QVariant InputMapping::mapToAction(const QString& source, const QString& keycode)
+QVariantList InputMapping::mapToAction(const QString& source, const QString& keycode)
 {
   // if the source is direct we will just use the keycode as the action
   if (source == "direct")
-    return keycode;
+    return { QVariant(keycode) };
+
+  QVariantList strActions;
 
   // first we need to match the source
-  QVariant sourceName = m_sourceMatcher.match(source);
-  if (sourceName.isValid())
-  {
-    QVariant action = m_inputMatcher.value(sourceName.toString())->match(keycode);
-    if (action.isValid())
-      return action;
-  }
-  return QString();
+  for (auto src : m_sourceMatcher.match(source))
+    strActions << m_inputMatcher.value(src.toString())->match(keycode);
+
+  return strActions;
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////

+ 1 - 1
src/input/InputMapping.h

@@ -16,7 +16,7 @@ class InputMapping : public QObject
 public:
   explicit InputMapping(QObject *parent = nullptr);
   bool loadMappings();
-  QVariant mapToAction(const QString& source, const QString& keycode);
+  QVariantList mapToAction(const QString& source, const QString& keycode);
 
 private Q_SLOTS:
   void dirChange();

+ 11 - 7
src/utils/CachedRegexMatcher.cpp

@@ -20,12 +20,14 @@ bool CachedRegexMatcher::addMatcher(const QString& pattern, const QVariant& resu
 }
 
 /////////////////////////////////////////////////////////////////////////////////////////
-QVariant CachedRegexMatcher::match(const QString& input)
+QVariantList CachedRegexMatcher::match(const QString& input)
 {
   // first we check if this match has already happened before
   if (m_matcherCache.contains(input))
     return m_matcherCache.value(input);
 
+  QVariantList matches;
+
   // otherwise try to iterate our list and find a match
   for(const MatcherValuePair& matcher : m_matcherList)
   {
@@ -49,17 +51,19 @@ QVariant CachedRegexMatcher::match(const QString& input)
         returnValue = QVariant(value);
       }
 
-      // now cache the match and the final value
-      m_matcherCache.insert(input, returnValue);
-
-      return returnValue;
+      matches << returnValue;
     }
   }
 
   QLOG_DEBUG() << "No match for:" << input;
 
-  // no match at all
-  return QVariant();
+  if (!matches.isEmpty())
+  {
+    m_matcherCache.insert(input, matches);
+    return matches;
+  }
+
+  return QVariantList();
 }
 
 /////////////////////////////////////////////////////////////////////////////////////////

+ 2 - 2
src/utils/CachedRegexMatcher.h

@@ -19,12 +19,12 @@ public:
   explicit CachedRegexMatcher(QObject* parent = nullptr) : QObject(parent) {}
 
   bool addMatcher(const QString& pattern, const QVariant& result);
-  QVariant match(const QString& input);
+  QVariantList match(const QString& input);
   void clear();
 
 private:
   MatcherValueList m_matcherList;
-  QHash<QString, QVariant> m_matcherCache;
+  QHash<QString, QVariantList> m_matcherCache;
 };
 
 #endif //KONVERGO_CACHEDREGEXMATCHER_H