Bladeren bron

Fix custom keymaps

Since the "multiple actions per pattern" change we also allowed
multiple sources to match. This broke custom keymaps since it
sent muliple actions, one for the built-in keymap and one for the
custom. We fixed this by adding a flag to CachedRegexMatcher that
makes it possible to overwrite an pattern entry.
Tobias Hieta 8 jaren geleden
bovenliggende
commit
e27fb3eacf
3 gewijzigde bestanden met toevoegingen van 15 en 3 verwijderingen
  1. 2 2
      src/input/InputMapping.cpp
  2. 10 0
      src/utils/CachedRegexMatcher.cpp
  3. 3 1
      src/utils/CachedRegexMatcher.h

+ 2 - 2
src/input/InputMapping.cpp

@@ -12,7 +12,7 @@
 #include "utils/Utils.h"
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-InputMapping::InputMapping(QObject *parent) : QObject(parent)
+InputMapping::InputMapping(QObject *parent) : QObject(parent), m_sourceMatcher(false)
 {
   m_watcher = new QFileSystemWatcher(this);
   connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, &InputMapping::dirChange);
@@ -142,7 +142,7 @@ bool InputMapping::loadMappingDirectory(const QString& path, bool copy)
         {
           // get the input map and add it to a new CachedMatcher
           QVariantMap inputMap = mapping.second.value("mapping").toMap();
-          auto  inputMatcher = new CachedRegexMatcher(this);
+          auto inputMatcher = new CachedRegexMatcher(true, this);
           for(const QString& pattern : inputMap.keys())
             inputMatcher->addMatcher("^" + pattern + "$", inputMap.value(pattern));
 

+ 10 - 0
src/utils/CachedRegexMatcher.cpp

@@ -15,6 +15,16 @@ bool CachedRegexMatcher::addMatcher(const QString& pattern, const QVariant& resu
     return false;
   }
 
+  // Remove older mapping if it exists.
+  if (!m_allowMultiplePatterns)
+  {
+    auto newEnd = std::remove_if(m_matcherList.begin(),m_matcherList.end(), [pattern](auto mp)
+    {
+      return mp.first.pattern() == pattern;
+    });
+    m_matcherList.erase(newEnd, m_matcherList.end());
+  }
+
   m_matcherList.push_back(qMakePair(matcher, result));
   return true;
 }

+ 3 - 1
src/utils/CachedRegexMatcher.h

@@ -16,7 +16,8 @@ typedef QList<MatcherValuePair> MatcherValueList;
 class CachedRegexMatcher : public QObject
 {
 public:
-  explicit CachedRegexMatcher(QObject* parent = nullptr) : QObject(parent) {}
+  explicit CachedRegexMatcher(bool allowMultiplePatterns = true, QObject* parent = nullptr)
+    : QObject(parent), m_allowMultiplePatterns(allowMultiplePatterns) {}
 
   bool addMatcher(const QString& pattern, const QVariant& result);
   QVariantList match(const QString& input);
@@ -25,6 +26,7 @@ public:
 private:
   MatcherValueList m_matcherList;
   QHash<QString, QVariantList> m_matcherCache;
+  bool m_allowMultiplePatterns;
 };
 
 #endif //KONVERGO_CACHEDREGEXMATCHER_H