]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/LayoutBox.cpp
Use <cstdint> instead of <boost/cstdint.hpp>
[lyx.git] / src / frontends / qt4 / LayoutBox.cpp
index 85df1694b813e2a91cb4698d5502f50c4fb83854..7d0cfd666f3f1696cf80ae5c8e03ebd8dff6dcc2 100644 (file)
@@ -43,6 +43,7 @@
 #include <QHeaderView>
 #include <QItemDelegate>
 #include <QPainter>
+#include <QRegExp>
 #include <QSortFilterProxyModel>
 #include <QStandardItemModel>
 #include <QTextFrame>
@@ -118,7 +119,7 @@ public:
                filterModel_(new GuiLayoutFilterModel(p)),
                lastSel_(-1),
                layoutItemDelegate_(new LayoutItemDelegate(parent)),
-               visibleCategories_(0), inShowPopup_(false)
+               visibleCategories_(0)
        {
                filterModel_->setSourceModel(model_);
        }
@@ -149,8 +150,6 @@ public:
        LayoutItemDelegate * layoutItemDelegate_;
        ///
        unsigned visibleCategories_;
-       ///
-       bool inShowPopup_;
 };
 
 
@@ -239,38 +238,22 @@ void LayoutItemDelegate::drawDisplay(QPainter * painter, QStyleOptionViewItem co
 QSize LayoutItemDelegate::sizeHint(QStyleOptionViewItem const & opt,
                                                                   QModelIndex const & index) const
 {
-       QSortFilterProxyModel const * model =
-               static_cast<QSortFilterProxyModel const *>(index.model());
        QSize size = QItemDelegate::sizeHint(opt, index);
+       if (!lyxrc.group_layouts)
+               return size;
 
-       /// QComboBox uses the first row height to estimate the
-       /// complete popup height during QComboBox::showPopup().
-       /// To avoid scrolling we have to sneak in space for the headers.
-       /// So we tweak this value accordingly. It's not nice, but the
-       /// only possible way it seems.
-       if (lyxrc.group_layouts && index.row() == 0 && layout_->d->inShowPopup_) {
-               int itemHeight = size.height();
-
-               // we have to show \c cats many headers:
-               unsigned cats = layout_->d->visibleCategories_;
-
-               // and we have \c n items to distribute the needed space over
-               unsigned n = layout_->model()->rowCount();
+       // Add space for the category headers.
+       QSortFilterProxyModel const * const model =
+               static_cast<QSortFilterProxyModel const *>(index.model());
+       QString const stdCat = category(*model->sourceModel(), 0);
+       QString const cat = category(*index.model(), index.row());
 
-               // so the needed average height (rounded upwards) is:
-               size.setHeight((headerHeight(opt) * cats + itemHeight * n + n - 1) / n);
+       // There is no header for the stuff at the top.
+       if (stdCat == cat)
                return size;
-       }
 
-       // Add space for the category headers here?
-       // Not for the standard layout though.
-       QString stdCat = category(*model->sourceModel(), 0);
-       QString cat = category(*index.model(), index.row());
-       if (lyxrc.group_layouts && stdCat != cat
-               && (index.row() == 0 || cat != category(*index.model(), index.row() - 1))) {
+       if (index.row() == 0 || cat != category(*index.model(), index.row() - 1))
                size.setHeight(size.height() + headerHeight(opt));
-       }
-
        return size;
 }
 
@@ -316,43 +299,13 @@ 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><b>\\1</b></u>");
        return r;
 }
 
 
-static QString charFilterRegExp(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 LayoutBox::Private::setFilter(QString const & s)
 {
        // exit early if nothing has to be done
@@ -378,23 +331,8 @@ void LayoutBox::Private::setFilter(QString const & s)
                        p->setCurrentIndex(i.row());
        }
 
-       // Workaround to resize to content size
-       // FIXME: There must be a better way. The QComboBox::AdjustToContents)
-       //        does not help.
        if (p->view()->isVisible()) {
-               // call QComboBox::showPopup. But set the inShowPopup_ flag to switch on
-               // the hack in the item delegate to make space for the headers.
-               // We do not call our implementation of showPopup because that
-               // would reset the filter again. This is only needed if the user clicks
-               // on the QComboBox.
-               LATTEST(!inShowPopup_);
-               inShowPopup_ = true;
                p->QComboBox::showPopup();
-               inShowPopup_ = false;
-
-               // The item delegate hack is off again. So trigger a relayout of the popup.
-               filterModel_->triggerLayoutChange();
-
                if (!s.isEmpty())
                        owner_.message(bformat(_("Filtering layouts with \"%1$s\". "
                                                 "Press ESC to remove filter."),
@@ -459,19 +397,8 @@ void LayoutBox::showPopup()
 
        bool enabled = view()->updatesEnabled();
        view()->setUpdatesEnabled(false);
-
        d->resetFilter();
-
-       // call QComboBox::showPopup. But set the inShowPopup_ flag to switch on
-       // the hack in the item delegate to make space for the headers.
-       LATTEST(!d->inShowPopup_);
-       d->inShowPopup_ = true;
        QComboBox::showPopup();
-       d->inShowPopup_ = false;
-
-       // The item delegate hack is off again. So trigger a relayout of the popup.
-       d->filterModel_->triggerLayoutChange();
-
        view()->setUpdatesEnabled(enabled);
 }