#include "support/gettext.h"
#include "support/lassert.h"
#include "support/lstrings.h"
+#include "support/qstring_helpers.h"
#include <QAbstractTextDocumentLayout>
#include <QComboBox>
///
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;
///
void countCategories();
///
CategorizedCombo * p;
-
- /** the layout model:
+
+ /** the layout model:
* 1st column: translated GUI name,
* 2nd column: raw item name,
* 3rd column: category,
opt.state = state;
// draw category header
- drawCategoryHeader(painter, opt,
+ drawCategoryHeader(painter, opt,
categoryCC(*index.model(), index.row()));
// move rect down below header
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;
- cc_->filter();
- for (int i = 0; i < f.length(); ++i) {
- int p = s.indexOf(f[i], lastp + 1, Qt::CaseInsensitive);
- LASSERT(p != -1, /**/);
- 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();
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.
p->QComboBox::showPopup();
inShowPopup_ = false;
}
-
+
p->view()->setUpdatesEnabled(enabled);
}
: QComboBox(parent), d(new Private(this))
{
setSizeAdjustPolicy(QComboBox::AdjustToContents);
- setFocusPolicy(Qt::ClickFocus);
setMinimumWidth(sizeHint().width());
setMaxVisibleItems(100);
// for the filtering we have to intercept characters
view()->installEventFilter(this);
view()->setItemDelegateForColumn(0, d->CCItemDelegate_);
-
+
updateCombo();
}
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) {
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()) {
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"))))
void CategorizedCombo::updateCombo()
{
d->countCategories();
-
+
// needed to recalculate size hint
hide();
setMinimumWidth(sizeHint().width());
return d->filter_;
}
-} // namespace frontend
-} // namespace lyx
+} // namespace frontend
+} // namespace lyx
#include "moc_CategorizedCombo.cpp"