]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistwidget.cpp
Allow custom sorting of details-view columns
[dolphin.git] / src / kitemviews / kitemlistwidget.cpp
index cf8b54c0cfec7bce7771d5886c9327d1f0a92b30..951fb396c54faa540771d32bc1fc10fd981f16f6 100644 (file)
@@ -28,6 +28,7 @@
 
 #include <KDebug>
 
+#include <KGlobalSettings>
 #include <QApplication>
 #include <QPainter>
 #include <QPropertyAnimation>
@@ -39,12 +40,13 @@ KItemListWidget::KItemListWidget(QGraphicsItem* parent) :
     m_selected(false),
     m_current(false),
     m_hovered(false),
-    m_alternatingBackgroundColors(false),
+    m_alternateBackground(false),
     m_enabledSelectionToggle(false),
     m_data(),
     m_visibleRoles(),
     m_visibleRolesSizes(),
     m_styleOption(),
+    m_siblingsInfo(),
     m_hoverOpacity(0),
     m_hoverCache(0),
     m_hoverAnimation(0),
@@ -91,6 +93,7 @@ void KItemListWidget::setData(const QHash<QByteArray, QVariant>& data,
         }
         dataChanged(m_data, roles);
     }
+    update();
 }
 
 QHash<QByteArray, QVariant> KItemListWidget::data() const
@@ -102,27 +105,40 @@ void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* o
 {
     Q_UNUSED(option);
 
-    painter->setRenderHint(QPainter::Antialiasing);
-
-    if (m_alternatingBackgroundColors && (m_index & 0x1)) {
+    if (m_alternateBackground) {
         const QColor backgroundColor = m_styleOption.palette.color(QPalette::AlternateBase);
         const QRectF backgroundRect(0, 0, size().width(), size().height());
         painter->fillRect(backgroundRect, backgroundColor);
     }
 
     if (m_selected) {
-        drawItemStyleOption(painter, widget, QStyle::State_Enabled |
+        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 = textRect().toRect();
-        viewItemOption.state = QStyle::State_Enabled | QStyle::State_Item;
-        viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
-        style()->drawPrimitive(QStyle::PE_FrameFocusRect, &viewItemOption, painter, widget);
+        QStyleOptionFocusRect focusRectOption;
+        focusRectOption.initFrom(widget);
+
+        const QRect iconBounds = iconRect().toRect();
+        const QRect textBounds = textRect().toRect();
+        if (iconBounds.bottom() > textBounds.top()) {
+            focusRectOption.rect = textBounds;
+        } else {
+            // See KItemListWidget::drawItemStyleOption(): The selection rectangle
+            // gets decreased.
+            focusRectOption.rect = textBounds.adjusted(1, 1, -1, -1);
+        }
+
+        focusRectOption.state = QStyle::State_Enabled | QStyle::State_Item | QStyle::State_KeyboardFocusChange;
+        if (m_selected) {
+            focusRectOption.state |= QStyle::State_Selected;
+        }
+
+        style()->drawPrimitive(QStyle::PE_FrameFocusRect, &focusRectOption, painter, widget);
     }
 
     if (m_hoverOpacity > 0.0) {
@@ -133,7 +149,9 @@ void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* o
             m_hoverCache->fill(Qt::transparent);
 
             QPainter pixmapPainter(m_hoverCache);
-            drawItemStyleOption(&pixmapPainter, widget, QStyle::State_Enabled |
+            const QStyle::State activeState(isActiveWindow() ? QStyle::State_Active : 0);
+            drawItemStyleOption(&pixmapPainter, widget, activeState |
+                                                        QStyle::State_Enabled |
                                                         QStyle::State_MouseOver |
                                                         QStyle::State_Item);
         }
@@ -149,7 +167,9 @@ void KItemListWidget::setVisibleRoles(const QList<QByteArray>& roles)
 {
     const QList<QByteArray> previousRoles = m_visibleRoles;
     m_visibleRoles = roles;
+
     visibleRolesChanged(roles, previousRoles);
+    update();
 }
 
 QList<QByteArray> KItemListWidget::visibleRoles() const
@@ -161,7 +181,9 @@ void KItemListWidget::setVisibleRolesSizes(const QHash<QByteArray, QSizeF> roles
 {
     const QHash<QByteArray, QSizeF> previousRolesSizes = m_visibleRolesSizes;
     m_visibleRolesSizes = rolesSizes;
+
     visibleRolesSizesChanged(rolesSizes, previousRolesSizes);
+    update();
 }
 
 QHash<QByteArray, QSizeF> KItemListWidget::visibleRolesSizes() const
@@ -176,6 +198,7 @@ void KItemListWidget::setStyleOption(const KItemListStyleOption& option)
     m_styleOption = option;
 
     styleOptionChanged(option, previous);
+    update();
 }
 
 const KItemListStyleOption& KItemListWidget::styleOption() const
@@ -190,7 +213,6 @@ void KItemListWidget::setSelected(bool selected)
         if (m_selectionToggle) {
             m_selectionToggle->setChecked(selected);
         }
-
         selectedChanged(selected);
         update();
     }
@@ -205,7 +227,6 @@ void KItemListWidget::setCurrent(bool current)
 {
     if (m_current != current) {
         m_current = current;
-
         currentChanged(current);
         update();
     }
@@ -226,7 +247,8 @@ void KItemListWidget::setHovered(bool hovered)
 
     if (!m_hoverAnimation) {
         m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this);
-        m_hoverAnimation->setDuration(200);
+        const int duration = (KGlobalSettings::graphicEffectsLevel() == KGlobalSettings::NoEffects) ? 1 : 200;
+        m_hoverAnimation->setDuration(duration);
         connect(m_hoverAnimation, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
     }
     m_hoverAnimation->stop();
@@ -246,7 +268,6 @@ void KItemListWidget::setHovered(bool hovered)
     m_hoverAnimation->start();
 
     hoveredChanged(hovered);
-
     update();
 }
 
@@ -255,18 +276,18 @@ bool KItemListWidget::isHovered() const
     return m_hovered;
 }
 
-void KItemListWidget::setAlternatingBackgroundColors(bool enable)
+void KItemListWidget::setAlternateBackground(bool enable)
 {
-    if (m_alternatingBackgroundColors != enable) {
-        m_alternatingBackgroundColors = enable;
-        alternatingBackgroundColorsChanged(enable);
+    if (m_alternateBackground != enable) {
+        m_alternateBackground = enable;
+        alternateBackgroundChanged(enable);
         update();
     }
 }
 
-bool KItemListWidget::alternatingBackgroundColors() const
+bool KItemListWidget::alternateBackground() const
 {
-    return m_alternatingBackgroundColors;
+    return m_alternateBackground;
 }
 
 void KItemListWidget::setEnabledSelectionToggle(bool enable)
@@ -282,6 +303,19 @@ bool KItemListWidget::enabledSelectionToggle() const
     return m_enabledSelectionToggle;
 }
 
+void KItemListWidget::setSiblingsInformation(const QBitArray& siblings)
+{
+    const QBitArray previous = m_siblingsInfo;
+    m_siblingsInfo = siblings;
+    siblingsInformationChanged(m_siblingsInfo, previous);
+    update();
+}
+
+QBitArray KItemListWidget::siblingsInformation() const
+{
+    return m_siblingsInfo;
+}
+
 bool KItemListWidget::contains(const QPointF& point) const
 {
     if (!QGraphicsWidget::contains(point)) {
@@ -309,7 +343,6 @@ void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
 {
     Q_UNUSED(current);
     Q_UNUSED(roles);
-    update();
 }
 
 void KItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
@@ -317,7 +350,6 @@ void KItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
 {
     Q_UNUSED(current);
     Q_UNUSED(previous);
-    update();
 }
 
 void KItemListWidget::visibleRolesSizesChanged(const QHash<QByteArray, QSizeF>& current,
@@ -325,7 +357,6 @@ void KItemListWidget::visibleRolesSizesChanged(const QHash<QByteArray, QSizeF>&
 {
     Q_UNUSED(current);
     Q_UNUSED(previous);
-    update();
 }
 
 void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
@@ -333,7 +364,6 @@ void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
 {
     Q_UNUSED(current);
     Q_UNUSED(previous);
-    update();
 }
 
 void KItemListWidget::currentChanged(bool current)
@@ -351,11 +381,17 @@ void KItemListWidget::hoveredChanged(bool hovered)
     Q_UNUSED(hovered);
 }
 
-void KItemListWidget::alternatingBackgroundColorsChanged(bool enabled)
+void KItemListWidget::alternateBackgroundChanged(bool enabled)
 {
     Q_UNUSED(enabled);
 }
 
+void KItemListWidget::siblingsInformationChanged(const QBitArray& current, const QBitArray& previous)
+{
+    Q_UNUSED(current);
+    Q_UNUSED(previous);
+}
+
 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
 {
     QGraphicsWidget::resizeEvent(event);
@@ -413,27 +449,15 @@ void KItemListWidget::clearHoverCache()
 
 void KItemListWidget::drawItemStyleOption(QPainter* painter, QWidget* widget, QStyle::State styleState)
 {
-    const QRect iconBounds = iconRect().toRect();
     const QRect textBounds = textRect().toRect();
 
     QStyleOptionViewItemV4 viewItemOption;
     viewItemOption.initFrom(widget);
     viewItemOption.state = styleState;
     viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
-
-    const bool drawMerged = (iconBounds.top()    == textBounds.top() &&
-                             iconBounds.bottom() == textBounds.bottom());
-
-    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);
-    }
+    viewItemOption.showDecorationSelected = true;
+    viewItemOption.rect = textBounds.adjusted(1, 0, -1, 0);
+    widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
 }
 
 #include "kitemlistwidget.moc"