diff options
| author | Peter Penz <[email protected]> | 2011-11-13 20:55:51 +0100 |
|---|---|---|
| committer | Peter Penz <[email protected]> | 2011-11-13 20:58:43 +0100 |
| commit | e560a2f6462044c4cf3c66366b6995cf74dd8e2d (patch) | |
| tree | 40f953f9f848509e2b2a00ee120df3bb17776ef1 /src/kitemviews/kitemlistwidget.cpp | |
| parent | 2438b61d8e76f8ea016217150be711a1467c32d0 (diff) | |
Fix selection style issues
Don't use a custom drawing code for showing the hover-indication
or selection of the text.
- The default style for items is used.
- Merge icon-rectangle and text-rectangle if possible.
- Fix background and minor focus-issues
Diffstat (limited to 'src/kitemviews/kitemlistwidget.cpp')
| -rw-r--r-- | src/kitemviews/kitemlistwidget.cpp | 90 |
1 files changed, 47 insertions, 43 deletions
diff --git a/src/kitemviews/kitemlistwidget.cpp b/src/kitemviews/kitemlistwidget.cpp index 04438262f..cf8b54c0c 100644 --- a/src/kitemviews/kitemlistwidget.cpp +++ b/src/kitemviews/kitemlistwidget.cpp @@ -31,7 +31,6 @@ #include <QApplication> #include <QPainter> #include <QPropertyAnimation> -#include <QStyle> #include <QStyleOption> KItemListWidget::KItemListWidget(QGraphicsItem* parent) : @@ -111,16 +110,10 @@ void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* o painter->fillRect(backgroundRect, backgroundColor); } - const QRect iconBounds = iconRect().toRect(); if (m_selected) { - QStyleOptionViewItemV4 viewItemOption; - viewItemOption.initFrom(widget); - viewItemOption.rect = iconBounds; - viewItemOption.state = QStyle::State_Enabled | QStyle::State_Selected | QStyle::State_Item; - viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne; - widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget); - - drawTextBackground(painter); + drawItemStyleOption(painter, widget, QStyle::State_Enabled | + QStyle::State_Selected | + QStyle::State_Item); } if (isCurrent()) { @@ -132,32 +125,24 @@ void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* o style()->drawPrimitive(QStyle::PE_FrameFocusRect, &viewItemOption, painter, widget); } - if (m_hoverOpacity <= 0.0) { - return; - } - - if (!m_hoverCache) { - // Initialize the m_hoverCache pixmap to improve the drawing performance - // when fading the hover background - m_hoverCache = new QPixmap(iconBounds.size()); - m_hoverCache->fill(Qt::transparent); - - QPainter pixmapPainter(m_hoverCache); + if (m_hoverOpacity > 0.0) { + if (!m_hoverCache) { + // Initialize the m_hoverCache pixmap to improve the drawing performance + // when fading the hover background + m_hoverCache = new QPixmap(size().toSize()); + m_hoverCache->fill(Qt::transparent); - QStyleOptionViewItemV4 viewItemOption; - viewItemOption.initFrom(widget); - viewItemOption.rect = QRect(0, 0, iconBounds.width(), iconBounds.height()); - viewItemOption.state = QStyle::State_Enabled | QStyle::State_MouseOver | QStyle::State_Item; - viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne; + QPainter pixmapPainter(m_hoverCache); + drawItemStyleOption(&pixmapPainter, widget, QStyle::State_Enabled | + QStyle::State_MouseOver | + QStyle::State_Item); + } - widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, &pixmapPainter, widget); + const qreal opacity = painter->opacity(); + painter->setOpacity(m_hoverOpacity * opacity); + painter->drawPixmap(0, 0, *m_hoverCache); + painter->setOpacity(opacity); } - - const qreal opacity = painter->opacity(); - painter->setOpacity(m_hoverOpacity * opacity); - painter->drawPixmap(iconBounds.topLeft(), *m_hoverCache); - drawTextBackground(painter); - painter->setOpacity(opacity); } void KItemListWidget::setVisibleRoles(const QList<QByteArray>& roles) @@ -247,11 +232,14 @@ void KItemListWidget::setHovered(bool hovered) m_hoverAnimation->stop(); if (hovered) { + const qreal startValue = qMax(hoverOpacity(), qreal(0.1)); + m_hoverAnimation->setStartValue(startValue); m_hoverAnimation->setEndValue(1.0); if (m_enabledSelectionToggle && !(QApplication::mouseButtons() & Qt::LeftButton)) { initializeSelectionToggle(); } } else { + m_hoverAnimation->setStartValue(hoverOpacity()); m_hoverAnimation->setEndValue(0.0); } @@ -408,6 +396,12 @@ void KItemListWidget::setHoverOpacity(qreal opacity) if (m_selectionToggle) { m_selectionToggle->setOpacity(opacity); } + + if (m_hoverOpacity <= 0.0) { + delete m_hoverCache; + m_hoverCache = 0; + } + update(); } @@ -417,19 +411,29 @@ void KItemListWidget::clearHoverCache() m_hoverCache = 0; } -void KItemListWidget::drawTextBackground(QPainter* painter) +void KItemListWidget::drawItemStyleOption(QPainter* painter, QWidget* widget, QStyle::State styleState) { - const qreal opacity = painter->opacity(); + const QRect iconBounds = iconRect().toRect(); + const QRect textBounds = textRect().toRect(); + + QStyleOptionViewItemV4 viewItemOption; + viewItemOption.initFrom(widget); + viewItemOption.state = styleState; + viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne; - QRectF textBounds = textRect(); - const qreal marginDiff = m_styleOption.margin / 2; - textBounds.adjust(marginDiff, marginDiff, -marginDiff, -marginDiff); - painter->setOpacity(opacity * 0.1); - painter->setPen(Qt::NoPen); - painter->setBrush(m_styleOption.palette.text()); - painter->drawRoundedRect(textBounds, 4, 4); + const bool drawMerged = (iconBounds.top() == textBounds.top() && + iconBounds.bottom() == textBounds.bottom()); - painter->setOpacity(opacity); + if (drawMerged) { + viewItemOption.rect = iconBounds | textBounds; + widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget); + } else { + viewItemOption.rect = iconBounds; + widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget); + + viewItemOption.rect = textBounds.adjusted(2, 2, -2, -2); + widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget); + } } #include "kitemlistwidget.moc" |
