]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/CategorizedCombo.cpp
Use <cstdint> instead of <boost/cstdint.hpp>
[lyx.git] / src / frontends / qt4 / CategorizedCombo.cpp
index 4509e79b9566c28a5531ce748cbb5a1818645dc5..89b28cd3bd9eb84dfb31593ea45ae3c09494b8b3 100644 (file)
@@ -24,6 +24,7 @@
 #include "support/gettext.h"
 #include "support/lassert.h"
 #include "support/lstrings.h"
+#include "support/qstring_helpers.h"
 
 #include <QAbstractTextDocumentLayout>
 #include <QComboBox>
@@ -55,11 +56,11 @@ public:
        ///
        QSize sizeHint(QStyleOptionViewItem const & opt,
                QModelIndex const & index) const;
-       
+
 private:
        ///
        void drawCategoryHeader(QPainter * painter, QStyleOptionViewItem const & opt,
-               QString const & category) const;        
+               QString const & category) const;
        ///
        QString underlineFilter(QString const & s) const;
        ///
@@ -106,8 +107,8 @@ struct CategorizedCombo::Private
        void countCategories();
        ///
        CategorizedCombo * p;
-       
-       /** the layout model: 
+
+       /** the layout model:
         * 1st column: translated GUI name,
         * 2nd column: raw item name,
         * 3rd column: category,
@@ -162,7 +163,7 @@ void CCItemDelegate::paint(QPainter * painter, QStyleOptionViewItem const & opti
                opt.state = state;
 
                // draw category header
-               drawCategoryHeader(painter, opt, 
+               drawCategoryHeader(painter, opt,
                        categoryCC(*index.model(), index.row()));
 
                // move rect down below header
@@ -267,43 +268,13 @@ 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><b>\\1</b></u>");
        return r;
 }
 
 
-static QString charFilterRegExpCC(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;
-}
-
-
 void CategorizedCombo::Private::setFilter(QString const & s)
 {
        bool enabled = p->view()->updatesEnabled();
@@ -315,16 +286,16 @@ void CategorizedCombo::Private::setFilter(QString const & s)
                lastSel_ = filterModel_->mapToSource(filterModel_->index(sel, 0)).row();
 
        filter_ = s;
-       filterModel_->setFilterRegExp(charFilterRegExpCC(filter_));
+    filterModel_->setFilterRegExp(charFilterRegExp(filter_));
        countCategories();
-       
+
        // restore old selection
        if (lastSel_ != -1) {
                QModelIndex i = filterModel_->mapFromSource(model_->index(lastSel_, 0));
                if (i.isValid())
                        p->setCurrentIndex(i.row());
        }
-       
+
        // Workaround to resize to content size
        // FIXME: There must be a better way. The QComboBox::AdjustToContents)
        //        does not help.
@@ -339,7 +310,7 @@ void CategorizedCombo::Private::setFilter(QString const & s)
                p->QComboBox::showPopup();
                inShowPopup_ = false;
        }
-       
+
        p->view()->setUpdatesEnabled(enabled);
 }
 
@@ -356,7 +327,7 @@ CategorizedCombo::CategorizedCombo(QWidget * parent)
        // for the filtering we have to intercept characters
        view()->installEventFilter(this);
        view()->setItemDelegateForColumn(0, d->CCItemDelegate_);
-       
+
        updateCombo();
 }
 
@@ -373,7 +344,7 @@ void CategorizedCombo::Private::countCategories()
        if (n == 0)
                return;
 
-       QString prevCat = model_->index(0, 2).data().toString(); 
+       QString prevCat = model_->index(0, 2).data().toString();
 
        // count categories
        for (int i = 1; i < n; ++i) {
@@ -412,7 +383,7 @@ bool CategorizedCombo::eventFilter(QObject * o, QEvent * e)
        bool modified = (ke->modifiers() == Qt::ControlModifier)
                || (ke->modifiers() == Qt::AltModifier)
                || (ke->modifiers() == Qt::MetaModifier);
-       
+
        switch (ke->key()) {
        case Qt::Key_Escape:
                if (!modified && !d->filter_.isEmpty()) {
@@ -520,7 +491,7 @@ void CategorizedCombo::addItemSort(QString const & item, QString const & guiname
        if (sortedByCat) {
                // If sortCats == true, sort categories alphabetically, uncategorized at the end.
                while (i < end && d->model_->item(i, 2)->text() != qcat
-                      && (!sortCats 
+                      && (!sortCats
                           || (!uncategorized && d->model_->item(i, 2)->text().localeAwareCompare(qcat) < 0
                               && d->model_->item(i, 2)->text() != qt_("Uncategorized"))
                           || (uncategorized && d->model_->item(i, 2)->text() != qt_("Uncategorized"))))
@@ -571,7 +542,7 @@ void CategorizedCombo::reset()
 void CategorizedCombo::updateCombo()
 {
        d->countCategories();
-       
+
        // needed to recalculate size hint
        hide();
        setMinimumWidth(sizeHint().width());
@@ -584,8 +555,8 @@ QString const & CategorizedCombo::filter() const
        return d->filter_;
 }
 
-}  // namespace frontend
-}  // namespace lyx
+} // namespace frontend
+} // namespace lyx
 
 
 #include "moc_CategorizedCombo.cpp"