]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistwidget.cpp
Selection and current item fixes
[dolphin.git] / src / kitemviews / kitemlistwidget.cpp
index 025b8e883f6fab59842384c5f1b02076edb6d800..687367adb9fffa04917a78aa2eb83ce1d00a1031 100644 (file)
 
 #include "kitemlistwidget.h"
 
+#include "kitemlistselectiontoggle_p.h"
 #include "kitemlistview.h"
 #include "kitemmodelbase.h"
 
 #include <KDebug>
 
+#include <QApplication>
 #include <QPainter>
 #include <QPropertyAnimation>
-#include <QStyle>
 #include <QStyleOption>
 
 KItemListWidget::KItemListWidget(QGraphicsItem* parent) :
@@ -39,13 +40,15 @@ KItemListWidget::KItemListWidget(QGraphicsItem* parent) :
     m_current(false),
     m_hovered(false),
     m_alternatingBackgroundColors(false),
+    m_enabledSelectionToggle(false),
     m_data(),
     m_visibleRoles(),
     m_visibleRolesSizes(),
     m_styleOption(),
     m_hoverOpacity(0),
     m_hoverCache(0),
-    m_hoverAnimation(0)
+    m_hoverAnimation(0),
+    m_selectionToggle(0)
 {
 }
 
@@ -57,6 +60,9 @@ KItemListWidget::~KItemListWidget()
 void KItemListWidget::setIndex(int index)
 {
     if (m_index != index) {
+        delete m_selectionToggle;
+        m_selectionToggle = 0;
+
         if (m_hoverAnimation) {
             m_hoverAnimation->stop();
             m_hoverOpacity = 0;
@@ -104,53 +110,57 @@ void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* o
         painter->fillRect(backgroundRect, backgroundColor);
     }
 
-    const QRect iconBounds = iconBoundingRect().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);
+        const QStyle::State activeState(isActiveWindow() ? QStyle::State_Active : 0);
+        drawItemStyleOption(painter, widget, activeState |
+                                             QStyle::State_Enabled |
+                                             QStyle::State_Selected |
+                                             QStyle::State_Item);
     }
 
     if (isCurrent()) {
         QStyleOptionViewItemV4 viewItemOption;
         viewItemOption.initFrom(widget);
-        viewItemOption.rect = textBoundingRect().toRect();
+
+        const QRect iconBounds = iconRect().toRect();
+        const QRect textBounds = textRect().toRect();
+        if (iconBounds.bottom() >= textBounds.top()) {
+            viewItemOption.rect = textBounds;
+        } else {
+            // See KItemListWidget::drawItemStyleOption(): The selection rectangle
+            // gets decreased.
+            viewItemOption.rect = textBounds.adjusted(1, 1, -1, -1);
+        }
+
         viewItemOption.state = QStyle::State_Enabled | QStyle::State_Item;
+        if (m_selected) {
+            viewItemOption.state |= QStyle::State_Selected;
+        }
+
         viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
         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);
-
-        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;
+    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);
+
+            QPainter pixmapPainter(m_hoverCache);
+            const QStyle::State activeState(isActiveWindow() ? QStyle::State_Active : 0);
+            drawItemStyleOption(&pixmapPainter, widget, activeState |
+                                                        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)
@@ -195,6 +205,10 @@ void KItemListWidget::setSelected(bool selected)
 {
     if (m_selected != selected) {
         m_selected = selected;
+        if (m_selectionToggle) {
+            m_selectionToggle->setChecked(selected);
+        }
+
         selectedChanged(selected);
         update();
     }
@@ -209,6 +223,7 @@ void KItemListWidget::setCurrent(bool current)
 {
     if (m_current != current) {
         m_current = current;
+
         currentChanged(current);
         update();
     }
@@ -230,12 +245,19 @@ void KItemListWidget::setHovered(bool hovered)
     if (!m_hoverAnimation) {
         m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this);
         m_hoverAnimation->setDuration(200);
+        connect(m_hoverAnimation, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
     }
     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);
     }
 
@@ -265,14 +287,27 @@ bool KItemListWidget::alternatingBackgroundColors() const
     return m_alternatingBackgroundColors;
 }
 
+void KItemListWidget::setEnabledSelectionToggle(bool enable)
+{
+    if (m_enabledSelectionToggle != enable) {
+        m_enabledSelectionToggle = enable;
+        update();
+    }
+}
+
+bool KItemListWidget::enabledSelectionToggle() const
+{
+    return m_enabledSelectionToggle;
+}
+
 bool KItemListWidget::contains(const QPointF& point) const
 {
     if (!QGraphicsWidget::contains(point)) {
         return false;
     }
 
-    return iconBoundingRect().contains(point) ||
-           textBoundingRect().contains(point) ||
+    return iconRect().contains(point) ||
+           textRect().contains(point) ||
            expansionToggleRect().contains(point) ||
            selectionToggleRect().contains(point);
 }
@@ -350,9 +385,41 @@ qreal KItemListWidget::hoverOpacity() const
     return m_hoverOpacity;
 }
 
+void KItemListWidget::slotHoverAnimationFinished()
+{
+    if (!m_hovered) {
+        delete m_selectionToggle;
+        m_selectionToggle = 0;
+    }
+}
+
+void KItemListWidget::initializeSelectionToggle()
+{
+    Q_ASSERT(m_enabledSelectionToggle);
+
+    if (!m_selectionToggle) {
+        m_selectionToggle = new KItemListSelectionToggle(this);
+    }
+
+    const QRectF toggleRect = selectionToggleRect();
+    m_selectionToggle->setPos(toggleRect.topLeft());
+    m_selectionToggle->resize(toggleRect.size());
+
+    m_selectionToggle->setChecked(isSelected());
+}
+
 void KItemListWidget::setHoverOpacity(qreal opacity)
 {
     m_hoverOpacity = opacity;
+    if (m_selectionToggle) {
+        m_selectionToggle->setOpacity(opacity);
+    }
+
+    if (m_hoverOpacity <= 0.0) {
+        delete m_hoverCache;
+        m_hoverCache = 0;
+    }
+
     update();
 }
 
@@ -362,19 +429,26 @@ 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();
 
-    QRectF textBounds = textBoundingRect();
-    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);
+    QStyleOptionViewItemV4 viewItemOption;
+    viewItemOption.initFrom(widget);
+    viewItemOption.state = styleState;
+    viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
 
-    painter->setOpacity(opacity);
+    if (iconBounds.bottom() >= textBounds.top()) {
+        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(1, 1, -1, -1);
+        widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
+    }
 }
 
 #include "kitemlistwidget.moc"