]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/CategorizedCombo.cpp
QDialogButtonBox for the remaining dialogs.
[lyx.git] / src / frontends / qt4 / CategorizedCombo.cpp
index 7a9508b1af10df388673ab914921a6e2946cd2e8..757196377a67441aad0b68e5f1921d62691d7b26 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:
@@ -54,11 +55,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;
        ///
@@ -105,8 +106,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,
@@ -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
@@ -161,11 +162,11 @@ void CCItemDelegate::paint(QPainter * painter, QStyleOptionViewItem const & opti
                opt.state = state;
 
                // draw category header
-               drawCategoryHeader(painter, opt, 
-                       category(*index.model(), index.row()));
+               drawCategoryHeader(painter, opt,
+                       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);
@@ -270,10 +271,10 @@ QString CCItemDelegate::underlineFilter(QString const & s) const
        // 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 (p < 0)
+                       continue;
                if (lastp == p - 1 && lastp != -1) {
                        // remove ")" and append "x)"
                        r = r.left(r.length() - 4) + s[p] + "</u>";
@@ -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,16 +315,16 @@ 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
        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.
@@ -338,7 +339,7 @@ void CategorizedCombo::Private::setFilter(QString const & s)
                p->QComboBox::showPopup();
                inShowPopup_ = false;
        }
-       
+
        p->view()->setUpdatesEnabled(enabled);
 }
 
@@ -347,7 +348,6 @@ CategorizedCombo::CategorizedCombo(QWidget * parent)
        : QComboBox(parent), d(new Private(this))
 {
        setSizeAdjustPolicy(QComboBox::AdjustToContents);
-       setFocusPolicy(Qt::ClickFocus);
        setMinimumWidth(sizeHint().width());
        setMaxVisibleItems(100);
 
@@ -356,7 +356,7 @@ CategorizedCombo::CategorizedCombo(QWidget * parent)
        // for the filtering we have to intercept characters
        view()->installEventFilter(this);
        view()->setItemDelegateForColumn(0, d->CCItemDelegate_);
-       
+
        updateCombo();
 }
 
@@ -373,7 +373,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 +412,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()) {
@@ -454,7 +454,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 +493,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 +518,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 +556,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();
 }
 
 
@@ -565,7 +571,7 @@ void CategorizedCombo::reset()
 void CategorizedCombo::updateCombo()
 {
        d->countCategories();
-       
+
        // needed to recalculate size hint
        hide();
        setMinimumWidth(sizeHint().width());
@@ -578,5 +584,8 @@ QString const & CategorizedCombo::filter() const
        return d->filter_;
 }
 
+} // namespace frontend
+} // namespace lyx
+
 
 #include "moc_CategorizedCombo.cpp"