commit 1410eeb10e4ce1549ce381ab7ae85847c442c41f
Author: Richard Kimberly Heck <rikih...@lyx.org>
Date:   Thu Oct 18 21:41:20 2018 -0400

    Simplify the code that adds underlining to the layout combo.
---
 src/frontends/qt4/CategorizedCombo.cpp |   22 +++-------------------
 src/frontends/qt4/LayoutBox.cpp        |   23 ++++-------------------
 src/support/qstring_helpers.cpp        |   15 +++++++++++++++
 src/support/qstring_helpers.h          |    7 +++++++
 4 files changed, 29 insertions(+), 38 deletions(-)

diff --git a/src/frontends/qt4/CategorizedCombo.cpp 
b/src/frontends/qt4/CategorizedCombo.cpp
index 291a402..200628b 100644
--- a/src/frontends/qt4/CategorizedCombo.cpp
+++ b/src/frontends/qt4/CategorizedCombo.cpp
@@ -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;
 }
 
diff --git a/src/frontends/qt4/LayoutBox.cpp b/src/frontends/qt4/LayoutBox.cpp
index 2bc8714..e82be6e 100644
--- a/src/frontends/qt4/LayoutBox.cpp
+++ b/src/frontends/qt4/LayoutBox.cpp
@@ -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;
 }
 
diff --git a/src/support/qstring_helpers.cpp b/src/support/qstring_helpers.cpp
index ada6125..4a09c1b 100644
--- a/src/support/qstring_helpers.cpp
+++ b/src/support/qstring_helpers.cpp
@@ -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
diff --git a/src/support/qstring_helpers.h b/src/support/qstring_helpers.h
index a3fafb4..45e0434 100644
--- a/src/support/qstring_helpers.h
+++ b/src/support/qstring_helpers.h
@@ -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

Reply via email to