]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/CategorizedCombo.cpp
Use QFontMetrics information for underlines (and friends) width and position
[lyx.git] / src / frontends / qt4 / CategorizedCombo.cpp
index 7a9508b1af10df388673ab914921a6e2946cd2e8..b52172d95099fbfed4d1942c2c761556adf71446 100644 (file)
 #include <QStandardItemModel>
 #include <QTextFrame>
 
-using namespace std;
-using namespace lyx;
 using namespace lyx::support;
 
+namespace lyx {
+namespace frontend {
+
 
 class CCItemDelegate : public QItemDelegate {
 public:
@@ -128,13 +129,13 @@ struct CategorizedCombo::Private
 };
 
 
-static QString category(QAbstractItemModel const & model, int row)
+static QString categoryCC(QAbstractItemModel const & model, int row)
 {
        return model.data(model.index(row, 2), Qt::DisplayRole).toString();
 }
 
 
-static int headerHeight(QStyleOptionViewItem const & opt)
+static int headerHeightCC(QStyleOptionViewItem const & opt)
 {
        return opt.fontMetrics.height();
 }
@@ -148,10 +149,10 @@ void CCItemDelegate::paint(QPainter * painter, QStyleOptionViewItem const & opti
        // default background
        painter->fillRect(opt.rect, opt.palette.color(QPalette::Base));
 
-       QString cat = category(*index.model(), index.row());
+       QString cat = categoryCC(*index.model(), index.row());
 
        // not the same as in the previous line?
-       if (index.row() == 0 || cat != category(*index.model(), index.row() - 1)) {
+       if (index.row() == 0 || cat != categoryCC(*index.model(), index.row() - 1)) {
                painter->save();
 
                // draw unselected background
@@ -162,10 +163,10 @@ void CCItemDelegate::paint(QPainter * painter, QStyleOptionViewItem const & opti
 
                // draw category header
                drawCategoryHeader(painter, opt, 
-                       category(*index.model(), index.row()));
+                       categoryCC(*index.model(), index.row()));
 
                // move rect down below header
-               opt.rect.setTop(opt.rect.top() + headerHeight(opt));
+               opt.rect.setTop(opt.rect.top() + headerHeightCC(opt));
 
                painter->restore();
        }
@@ -216,9 +217,9 @@ QSize CCItemDelegate::sizeHint(QStyleOptionViewItem const & opt,
        /// So we tweak this value accordingly. It's not nice, but the
        /// only possible way it seems.
        // Add space for the category headers here
-       QString cat = category(*index.model(), index.row());
-       if (index.row() == 0 || cat != category(*index.model(), index.row() - 1)) {
-               size.setHeight(size.height() + headerHeight(opt));
+       QString cat = categoryCC(*index.model(), index.row());
+       if (index.row() == 0 || cat != categoryCC(*index.model(), index.row() - 1)) {
+               size.setHeight(size.height() + headerHeightCC(opt));
        }
 
        return size;
@@ -244,7 +245,7 @@ void CCItemDelegate::drawCategoryHeader(QPainter * painter, QStyleOptionViewItem
        QFontMetrics fm(font);
        int w = fm.width(category);
        int x = opt.rect.x() + (opt.rect.width() - w) / 2;
-       int y = opt.rect.y() + 1.5 * fm.ascent();
+       int y = opt.rect.y() + 3 * fm.ascent() / 2;
        int left = x;
        int right = x + w;
        painter->drawText(x, y, category);
@@ -289,7 +290,7 @@ QString CCItemDelegate::underlineFilter(QString const & s) const
 }
 
 
-static QString charFilterRegExp(QString const & filter)
+static QString charFilterRegExpCC(QString const & filter)
 {
        QString re;
        for (int i = 0; i < filter.length(); ++i) {
@@ -314,7 +315,7 @@ void CategorizedCombo::Private::setFilter(QString const & s)
                lastSel_ = filterModel_->mapToSource(filterModel_->index(sel, 0)).row();
 
        filter_ = s;
-       filterModel_->setFilterRegExp(charFilterRegExp(filter_));
+       filterModel_->setFilterRegExp(charFilterRegExpCC(filter_));
        countCategories();
        
        // restore old selection
@@ -454,7 +455,7 @@ bool CategorizedCombo::eventFilter(QObject * o, QEvent * e)
 
 void CategorizedCombo::setIconSize(QSize size)
 {
-#ifdef Q_WS_MACX
+#ifdef Q_OS_MAC
        bool small = size.height() < 20;
        setAttribute(Qt::WA_MacSmallSize, small);
        setAttribute(Qt::WA_MacNormalSize, !small);
@@ -493,7 +494,9 @@ void CategorizedCombo::addItemSort(QString const & item, QString const & guiname
                                   bool sorted, bool sortedByCat, bool sortCats,
                                   bool available)
 {
-       QString titem = available ? guiname : qt_("Unavailable: %1").arg(guiname);
+       QString titem = available ? guiname
+                                 : toqstr(bformat(_("Unavailable: %1$s"),
+                                                  qstring_to_ucs4(guiname)));
        bool const uncategorized = category.isEmpty();
        QString qcat = uncategorized ? qt_("Uncategorized") : category;
 
@@ -516,9 +519,12 @@ void CategorizedCombo::addItemSort(QString const & item, QString const & guiname
        // find category
        int i = 0;
        if (sortedByCat) {
+               // If sortCats == true, sort categories alphabetically, uncategorized at the end.
                while (i < end && d->model_->item(i, 2)->text() != qcat
-                      && (!sortCats || (d->model_->item(i, 2)->text().localeAwareCompare(qcat) < 0)
-                          || (uncategorized && d->model_->item(i, 2)->text() == qt_("Uncategorized"))))
+                      && (!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"))))
                        ++i;
        }
 
@@ -551,7 +557,8 @@ void CategorizedCombo::addItemSort(QString const & item, QString const & guiname
 
 QString CategorizedCombo::getData(int row) const
 {
-       return d->model_->data(d->model_->index(row, 1), Qt::DisplayRole).toString();
+       int srow = d->filterModel_->mapToSource(d->filterModel_->index(row, 1)).row();
+       return d->model_->data(d->model_->index(srow, 1), Qt::DisplayRole).toString();
 }
 
 
@@ -578,5 +585,8 @@ QString const & CategorizedCombo::filter() const
        return d->filter_;
 }
 
+}  // namespace frontend
+}  // namespace lyx
+
 
 #include "moc_CategorizedCombo.cpp"