]> git.lyx.org Git - features.git/commitdiff
Simplify the code that adds underlining to the layout combo.
authorRichard Kimberly Heck <rikiheck@lyx.org>
Fri, 19 Oct 2018 01:41:20 +0000 (21:41 -0400)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Thu, 18 Jun 2020 12:39:50 +0000 (14:39 +0200)
src/frontends/qt4/CategorizedCombo.cpp
src/frontends/qt4/LayoutBox.cpp
src/support/qstring_helpers.cpp
src/support/qstring_helpers.h

index 291a4021bcfaef1ae83d8e774614778c52c2233c..200628be3209679ac0d2f9a9b71ef65edbb3c415 100644 (file)
@@ -268,25 +268,9 @@ QString CCItemDelegate::underlineFilter(QString const & s) const
        QString const & f = cc_->filter();
        if (f.isEmpty())
                return s;
-
-       // step through data item and put "(x)" for every matching character
-       QString r;
-       int lastp = -1;
-       for (int i = 0; i < f.length(); ++i) {
-               int p = s.indexOf(f[i], lastp + 1, Qt::CaseInsensitive);
-               if (p < 0)
-                       continue;
-               if (lastp == p - 1 && lastp != -1) {
-                       // remove ")" and append "x)"
-                       r = r.left(r.length() - 4) + s[p] + "</u>";
-               } else {
-                       // append "(x)"
-                       r += s.mid(lastp + 1, p - lastp - 1);
-                       r += QString("<u>") + s[p] + "</u>";
-               }
-               lastp = p;
-       }
-       r += s.mid(lastp + 1);
+       QString r(s);
+       QRegExp pattern(charFilterRegExpC(f));
+       r.replace(pattern, "<u>\\1</u>");
        return r;
 }
 
index 2bc871470f33e9afd97beab7655cb0cdbd045c64..e82be6e3fa57da67471f8534fe698dd4508b4724 100644 (file)
@@ -43,6 +43,7 @@
 #include <QHeaderView>
 #include <QItemDelegate>
 #include <QPainter>
+#include <QRegExp>
 #include <QSortFilterProxyModel>
 #include <QStandardItemModel>
 #include <QTextFrame>
@@ -298,25 +299,9 @@ QString LayoutItemDelegate::underlineFilter(QString const & s) const
        QString const & f = layout_->filter();
        if (f.isEmpty())
                return s;
-
-       // step through data item and put "(x)" for every matching character
-       QString r;
-       int lastp = -1;
-       for (int i = 0; i < f.length(); ++i) {
-               int p = s.indexOf(f[i], lastp + 1, Qt::CaseInsensitive);
-               if (p < 0)
-                       continue;
-               if (lastp == p - 1 && lastp != -1) {
-                       // remove ")" and append "x)"
-                       r = r.left(r.length() - 4) + s[p] + "</u>";
-               } else {
-                       // append "(x)"
-                       r += s.mid(lastp + 1, p - lastp - 1);
-                       r += QString("<u>") + s[p] + "</u>";
-               }
-               lastp = p;
-       }
-       r += s.mid(lastp + 1);
+       QString r(s);
+       QRegExp pattern(charFilterRegExpC(f));
+       r.replace(pattern, "<u>\\1</u>");
        return r;
 }
 
index ada61250987bc047ee814eb47df6a4d9cfb20eb1..4a09c1b6655a37f1848115f235c738c184c28d4d 100644 (file)
@@ -81,4 +81,19 @@ QString charFilterRegExp(QString const & filter)
     return re;
 }
 
+QString charFilterRegExpC(QString const & filter)
+{
+       QString re = "(";
+       for (int i = 0; i < filter.length(); ++i) {
+               QChar c = filter[i];
+               if (c.isLower())
+                       re +=  "["+ QRegExp::escape(c) + QRegExp::escape(c.toUpper()) + "]";
+               else
+                       re +=  QRegExp::escape(c);
+       }
+       return re + ")";
+}
+
+
+
 } // namespace lyx
index a3fafb43439a5742bfa0bb6daede9117b391a5e0..45e0434f9a08aad37e9b31eae762eb0861ea1df1 100644 (file)
@@ -82,9 +82,16 @@ std::string fromqstr(QString const & str);
 
 /**
  * constructs a regex to filter on consecutive characters
+ * matches lower- and uppercase on lowercase characters,
+ * and just uppercase for uppercase
  */
 QString charFilterRegExp(QString const & filter);
 
+/**
+ * as above, but constructs a capturing regex for a sequence of characters
+ */
+QString charFilterRegExpC(QString const & filter);
+
 } // namespace lyx
 
 #endif // QSTRING_HELPERS_H