]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistgroupheader.cpp
Group header improvements
[dolphin.git] / src / kitemviews / kitemlistgroupheader.cpp
index 5aac02c829d8c39673d755cde35691f0713d008c..8eff399011ff37a19b00be012ee701b0952c85f5 100644 (file)
 
 #include "kitemlistview.h"
 
+#include <QGraphicsSceneResizeEvent>
 #include <QPainter>
-
+#include <QStyleOptionGraphicsItem>
 #include <KDebug>
 
 KItemListGroupHeader::KItemListGroupHeader(QGraphicsWidget* parent) :
     QGraphicsWidget(parent, 0),
+    m_dirtyCache(true),
     m_role(),
-    m_data()
+    m_data(),
+    m_styleOption(),
+    m_scrollOrientation(Qt::Vertical),
+    m_roleColor(),
+    m_roleBounds()
 {
 }
 
@@ -69,22 +75,60 @@ QVariant KItemListGroupHeader::data() const
     return m_data;
 }
 
-QSizeF KItemListGroupHeader::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
+void KItemListGroupHeader::setStyleOption(const KItemListStyleOption& option)
+{
+    const KItemListStyleOption previous = m_styleOption;
+    m_styleOption = option;
+    m_dirtyCache = true;
+    styleOptionChanged(option, previous);
+}
+
+const KItemListStyleOption& KItemListGroupHeader::styleOption() const
+{
+    return m_styleOption;
+}
+
+void KItemListGroupHeader::setScrollOrientation(Qt::Orientation orientation)
 {
-    Q_UNUSED(which);
-    Q_UNUSED(constraint);
-    return QSizeF();
+    if (m_scrollOrientation != orientation) {
+        const Qt::Orientation previous = m_scrollOrientation;
+        m_scrollOrientation = orientation;
+        if (orientation == Qt::Vertical) {
+            m_dirtyCache = true;
+        }
+        scrollOrientationChanged(orientation, previous);
+    }
+}
+
+Qt::Orientation KItemListGroupHeader::scrollOrientation() const
+{
+    return m_scrollOrientation;
 }
 
 void KItemListGroupHeader::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
 {
     Q_UNUSED(option);
     Q_UNUSED(widget);
-    painter->setPen(Qt::darkGreen);
-    painter->setBrush(QColor(0, 255, 0, 50));
-    painter->drawRect(rect());
 
-    //painter->drawText(rect(), QString::number(m_index));
+    if (m_dirtyCache) {
+        updateCache();
+    }
+
+    if (m_scrollOrientation != Qt::Horizontal) {
+        painter->setPen(m_roleColor);
+        const qreal y = m_roleBounds.y() - m_styleOption.margin;
+        painter->drawLine(0, y, size().width() - 1, y);
+    }
+}
+
+QRectF KItemListGroupHeader::roleBounds() const
+{
+    return m_roleBounds;
+}
+
+QColor KItemListGroupHeader::roleColor() const
+{
+    return m_roleColor;
 }
 
 void KItemListGroupHeader::roleChanged(const QByteArray& current, const QByteArray& previous)
@@ -99,4 +143,50 @@ void KItemListGroupHeader::dataChanged(const QVariant& current, const QVariant&
     Q_UNUSED(previous);
 }
 
+void KItemListGroupHeader::styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous)
+{
+    Q_UNUSED(current);
+    Q_UNUSED(previous);
+}
+
+void KItemListGroupHeader::scrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
+{
+    Q_UNUSED(current);
+    Q_UNUSED(previous);
+}
+
+void KItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent* event)
+{
+    QGraphicsWidget::resizeEvent(event);
+    if (event->oldSize().height() != event->newSize().height()) {
+        m_dirtyCache = true;
+    }
+}
+
+void KItemListGroupHeader::updateCache()
+{
+    Q_ASSERT(m_dirtyCache);
+
+    // Calculate the outline color. No alphablending is used for
+    // performance reasons.
+    const QColor c1 = m_styleOption.palette.text().color();
+    const QColor c2 = m_styleOption.palette.background().color();
+    const int p1 = 35;
+    const int p2 = 100 - p1;
+    m_roleColor = QColor((c1.red()   * p1 + c2.red()   * p2) / 100,
+                         (c1.green() * p1 + c2.green() * p2) / 100,
+                         (c1.blue()  * p1 + c2.blue()  * p2) / 100);
+
+    const int margin = m_styleOption.margin;
+    const QFontMetrics fontMetrics(m_styleOption.font);
+    const qreal roleHeight = fontMetrics.height();
+
+    m_roleBounds = QRectF(margin,
+                          size().height() - roleHeight - margin,
+                          size().width() - 2 * margin,
+                          roleHeight);
+
+    m_dirtyCache = false;
+}
+
 #include "kitemlistgroupheader.moc"