diff options
author | Eike Ziller <eike.ziller@qt.io> | 2018-06-12 08:13:33 +0200 |
---|---|---|
committer | Eike Ziller <eike.ziller@qt.io> | 2018-06-14 09:56:12 +0000 |
commit | 74eef6d9086baa9a991365229826b0c358b3fc92 (patch) | |
tree | 00c55a8bff05d06e897535fb305be7d5df814e8b /src/libs/utils | |
parent | 451e437afd55ed132eaed1f139a882b2c555a7d2 (diff) |
Fix painting of clear buttons in history completer popup
We did set our own delegate, but QCompleter overrode that with its own
when setting the popup widget. So we have to override it after setting
the popup widget.
Also fix the icon painting on HiDPI screens.
Task-number: QTCREATORBUG-16037
Change-Id: Ibd06e945c75f2bceb02c0b5a23a9810d83b4b5e1
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
Diffstat (limited to 'src/libs/utils')
-rw-r--r-- | src/libs/utils/historycompleter.cpp | 57 |
1 files changed, 42 insertions, 15 deletions
diff --git a/src/libs/utils/historycompleter.cpp b/src/libs/utils/historycompleter.cpp index 736f55a7405..6d0484b85a0 100644 --- a/src/libs/utils/historycompleter.cpp +++ b/src/libs/utils/historycompleter.cpp @@ -36,6 +36,7 @@ #include <QKeyEvent> #include <QListView> #include <QPainter> +#include <QWindow> namespace Utils { namespace Internal { @@ -62,19 +63,35 @@ public: class HistoryLineDelegate : public QItemDelegate { public: - HistoryLineDelegate(QObject *parent) + HistoryLineDelegate(QAbstractItemView *parent) : QItemDelegate(parent) - , pixmap(Icons::EDIT_CLEAR.pixmap()) + , view(parent) + , icon(Icons::EDIT_CLEAR.icon()) {} void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { + // from QHistoryCompleter + QStyleOptionViewItem optCopy = option; + optCopy.showDecorationSelected = true; + if (view->currentIndex() == index) + optCopy.state |= QStyle::State_HasFocus; QItemDelegate::paint(painter,option,index); - QRect r = QStyle::alignedRect(option.direction, Qt::AlignRight | Qt::AlignVCenter , pixmap.size(), option.rect); - painter->drawPixmap(r, pixmap); + // add remove button + QWindow *window = view->window()->windowHandle(); + const QPixmap iconPixmap = icon.pixmap(window, option.rect.size()); + QRect pixmapRect = QStyle::alignedRect(option.direction, + Qt::AlignRight | Qt::AlignVCenter, + iconPixmap.size() / window->devicePixelRatio(), + option.rect); + if (!clearIconSize.isValid()) + clearIconSize = pixmapRect.size(); + painter->drawPixmap(pixmapRect, iconPixmap); } - QPixmap pixmap; + QAbstractItemView *view; + QIcon icon; + mutable QSize clearIconSize; }; class HistoryLineView : public QListView @@ -83,26 +100,32 @@ public: HistoryLineView(HistoryCompleterPrivate *model_) : model(model_) { - HistoryLineDelegate *delegate = new HistoryLineDelegate(this); - pixmapWidth = delegate->pixmap.width(); + } + + void installDelegate() + { + delegate = new HistoryLineDelegate(this); setItemDelegate(delegate); } private: void mousePressEvent(QMouseEvent *event) { - int rr= event->x(); - if (layoutDirection() == Qt::LeftToRight) - rr = viewport()->width() - event->x(); - if (rr < pixmapWidth) { - model->removeRow(indexAt(event->pos()).row()); - return; + const QSize clearButtonSize = delegate->clearIconSize; + if (clearButtonSize.isValid()) { + int rr = event->x(); + if (layoutDirection() == Qt::LeftToRight) + rr = viewport()->width() - event->x(); + if (rr < clearButtonSize.width()) { + model->removeRow(indexAt(event->pos()).row()); + return; + } } QListView::mousePressEvent(event); } HistoryCompleterPrivate *model; - int pixmapWidth; + HistoryLineDelegate *delegate; }; } // namespace Internal @@ -178,7 +201,11 @@ HistoryCompleter::HistoryCompleter(const QString &historyKey, QObject *parent) d->isLastItemEmpty = theSettings->value(d->historyKeyIsLastItemEmpty, false).toBool(); setModel(d); - setPopup(new HistoryLineView(d)); + auto popup = new HistoryLineView(d); + setPopup(popup); + // setPopup unconditionally sets a delegate on the popup, + // so we need to set our delegate afterwards + popup->installDelegate(); } bool HistoryCompleter::removeHistoryItem(int index) |