]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Provide alternating background colors for the details-view
authorPeter Penz <peter.penz19@gmail.com>
Thu, 6 Oct 2011 21:02:43 +0000 (23:02 +0200)
committerPeter Penz <peter.penz19@gmail.com>
Thu, 6 Oct 2011 21:03:23 +0000 (23:03 +0200)
src/kitemviews/kfileitemlistview.cpp
src/kitemviews/kitemlistview.cpp
src/kitemviews/kitemlistwidget.cpp
src/kitemviews/kitemlistwidget.h

index d3de2c8c99c72ef041617fa2c703eb80e9978cfb..a77ede50a91293cab22d9d224f1d35c766870817 100644 (file)
@@ -245,6 +245,8 @@ void KFileItemListView::initializeItemListWidget(KItemListWidget* item)
     case DetailsLayout: fileItemListWidget->setLayout(KFileItemListWidget::DetailsLayout); break;
     default:            Q_ASSERT(false); break;
     }
+
+    fileItemListWidget->setAlternatingBackgroundColors(m_itemLayout == DetailsLayout);
 }
 
 bool KFileItemListView::itemSizeHintUpdateRequired(const QSet<QByteArray>& changedRoles) const
index 81cfc1eedefe2c5a82e4778a2031bc74ecb0c7c7..bafbb47a4b9ccbec4c63ce56a85ae7a9bbde0724 100644 (file)
@@ -1394,8 +1394,8 @@ void KItemListView::updateWidgetProperties(KItemListWidget* widget, int index)
     const KItemListSelectionManager* selectionManager = m_controller->selectionManager();
     widget->setCurrent(index == selectionManager->currentItem());
     widget->setSelected(selectionManager->isSelected(index));
-
     widget->setHovered(false);
+    widget->setAlternatingBackgroundColors(false);
     widget->setIndex(index);
     widget->setData(m_model->data(index));
 }
index 476031777f1b29c379cb75ccae6b7c6f23a78b8f..5aa6e1baa7762c5d26917588aa56fc57ce3c99a5 100644 (file)
@@ -38,6 +38,7 @@ KItemListWidget::KItemListWidget(QGraphicsItem* parent) :
     m_selected(false),
     m_current(false),
     m_hovered(false),
+    m_alternatingBackgroundColors(false),
     m_data(),
     m_visibleRoles(),
     m_visibleRolesSizes(),
@@ -50,7 +51,7 @@ KItemListWidget::KItemListWidget(QGraphicsItem* parent) :
 
 KItemListWidget::~KItemListWidget()
 {
-    clearCache();
+    clearHoverCache();
 }
 
 void KItemListWidget::setIndex(int index)
@@ -60,7 +61,7 @@ void KItemListWidget::setIndex(int index)
             m_hoverAnimation->stop();
             m_hoverOpacity = 0;
         }
-        clearCache();
+        clearHoverCache();
 
         m_index = index;
     }
@@ -74,7 +75,7 @@ int KItemListWidget::index() const
 void KItemListWidget::setData(const QHash<QByteArray, QVariant>& data,
                               const QSet<QByteArray>& roles)
 {
-    clearCache();
+    clearHoverCache();
     if (roles.isEmpty()) {
         m_data = data;
         dataChanged(m_data);
@@ -97,6 +98,12 @@ void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* o
 
     painter->setRenderHint(QPainter::Antialiasing);
 
+    if (m_alternatingBackgroundColors && (m_index & 0x1)) {
+        const QColor backgroundColor = m_styleOption.palette.color(QPalette::AlternateBase);
+        const QRectF backgroundRect(0, 0, size().width(), size().height());
+        painter->fillRect(backgroundRect, backgroundColor);
+    }
+
     const QRect iconBounds = iconBoundingRect().toRect();
     if (m_selected) {
         QStyleOptionViewItemV4 viewItemOption;
@@ -168,7 +175,7 @@ QHash<QByteArray, QSizeF> KItemListWidget::visibleRolesSizes() const
 void KItemListWidget::setStyleOption(const KItemListStyleOption& option)
 {
     const KItemListStyleOption previous = m_styleOption;
-    clearCache();
+    clearHoverCache();
     m_styleOption = option;
 
     styleOptionChanged(option, previous);
@@ -239,6 +246,20 @@ bool KItemListWidget::isHovered() const
     return m_hovered;
 }
 
+void KItemListWidget::setAlternatingBackgroundColors(bool enable)
+{
+    if (m_alternatingBackgroundColors != enable) {
+        m_alternatingBackgroundColors = enable;
+        alternatingBackgroundColorsChanged(enable);
+        update();
+    }
+}
+
+bool KItemListWidget::alternatingBackgroundColors() const
+{
+    return m_alternatingBackgroundColors;
+}
+
 bool KItemListWidget::contains(const QPointF& point) const
 {
     if (!QGraphicsWidget::contains(point)) {
@@ -308,10 +329,15 @@ void KItemListWidget::hoveredChanged(bool hovered)
     Q_UNUSED(hovered);
 }
 
+void KItemListWidget::alternatingBackgroundColorsChanged(bool enabled)
+{
+    Q_UNUSED(enabled);
+}
+
 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
 {
     QGraphicsWidget::resizeEvent(event);
-    clearCache();
+    clearHoverCache();
 }
 
 qreal KItemListWidget::hoverOpacity() const
@@ -325,7 +351,7 @@ void KItemListWidget::setHoverOpacity(qreal opacity)
     update();
 }
 
-void KItemListWidget::clearCache()
+void KItemListWidget::clearHoverCache()
 {
     delete m_hoverCache;
     m_hoverCache = 0;
index 2229948a3704a814242395a898df9e4386865ade..b1df7bff4264704f9d8509c136344337f20fadc8 100644 (file)
@@ -77,6 +77,9 @@ public:
     void setHovered(bool hovered);
     bool isHovered() const;
 
+    void setAlternatingBackgroundColors(bool enable);
+    bool alternatingBackgroundColors() const;
+
     /**
      * @return True if \a point is inside KItemListWidget::hoverBoundingRect(),
      *         KItemListWidget::textBoundingRect(), KItemListWidget::selectionToggleRect()
@@ -117,7 +120,8 @@ protected:
     virtual void currentChanged(bool current);
     virtual void selectedChanged(bool selected);
     virtual void hoveredChanged(bool hovered);
-    virtual void resizeEvent(QGraphicsSceneResizeEvent* event);
+    virtual void alternatingBackgroundColorsChanged(bool enabled);
+    virtual void resizeEvent(QGraphicsSceneResizeEvent* event);    
 
     /**
      * @return The current opacity of the hover-animation. When implementing a custom painting-code for a hover-state
@@ -127,7 +131,7 @@ protected:
 
 private:
     void setHoverOpacity(qreal opacity);
-    void clearCache();
+    void clearHoverCache();
     void drawFocusIndicator(QPainter* painter);
     void drawTextBackground(QPainter* painter);
 
@@ -138,6 +142,7 @@ private:
     bool m_selected;
     bool m_current;
     bool m_hovered;
+    bool m_alternatingBackgroundColors;
     QHash<QByteArray, QVariant> m_data;
     QList<QByteArray> m_visibleRoles;
     QHash<QByteArray, QSizeF> m_visibleRolesSizes;