]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistview.cpp
Prevent endless scrolling of list when dragging items
[dolphin.git] / src / kitemviews / kitemlistview.cpp
index 372726954ffc41fe1c86a2ce423d34b0fae7de8f..d7777b541380ed3c0c04df22b9fb15df4496e739 100644 (file)
@@ -156,9 +156,24 @@ void KItemListView::setItemSize(const QSizeF& itemSize)
     const bool animate = !changesItemGridLayout(m_layouter->size(),
                                                 itemSize,
                                                 m_layouter->itemMargin());
-    
+
+    const bool updateAlternateBackgrounds = (m_visibleRoles.count() > 1) &&
+                                            (( m_itemSize.isEmpty() && !itemSize.isEmpty()) ||
+                                             (!m_itemSize.isEmpty() && itemSize.isEmpty()));
+
     m_itemSize = itemSize;
 
+    if (updateAlternateBackgrounds) {
+        // For an empty item size alternate backgrounds are drawn if more than
+        // one role is shown. Assure that the backgrounds for visible items are
+        // updated when changing the size in this context.
+        QHashIterator<int, KItemListWidget*> it(m_visibleItems);
+        while (it.hasNext()) {
+            it.next();
+            updateAlternateBackgroundForWidget(it.value());
+        }
+    }
+
     if (itemSize.isEmpty()) {
         updateVisibleRolesSizes();
     } else {
@@ -238,12 +253,19 @@ void KItemListView::setVisibleRoles(const QList<QByteArray>& roles)
     const QList<QByteArray> previousRoles = m_visibleRoles;
     m_visibleRoles = roles;
 
+    const bool updateAlternateBackgrounds = m_itemSize.isEmpty() &&
+                                            ((roles.count() > 1 && previousRoles.count() <= 1) ||
+                                             (roles.count() <= 1 && previousRoles.count() > 1));
+
     QHashIterator<int, KItemListWidget*> it(m_visibleItems);
     while (it.hasNext()) {
         it.next();
         KItemListWidget* widget = it.value();
         widget->setVisibleRoles(roles);
         widget->setVisibleRolesSizes(m_stretchedVisibleRolesSizes);
+        if (updateAlternateBackgrounds) {
+            updateAlternateBackgroundForWidget(widget);
+        }
     }
 
     m_sizeHintResolver->clearCache();
@@ -277,7 +299,6 @@ void KItemListView::setAutoScroll(bool enabled)
         delete m_autoScrollTimer;
         m_autoScrollTimer = 0;
     }
-
 }
 
 bool KItemListView::autoScroll() const
@@ -338,7 +359,7 @@ void KItemListView::setStyleOption(const KItemListStyleOption& option)
     const KItemListStyleOption previousOption = m_styleOption;
     m_styleOption = option;
 
-    bool animate = true;    
+    bool animate = true;
     const QSizeF margin(option.horizontalMargin, option.verticalMargin);
     if (margin != m_layouter->itemMargin()) {
         // Skip animations when the number of rows or columns
@@ -360,7 +381,7 @@ void KItemListView::setStyleOption(const KItemListStyleOption& option)
         it.value()->setStyleOption(option);
     }
 
-    m_sizeHintResolver->clearCache();   
+    m_sizeHintResolver->clearCache();
     doLayout(animate ? Animation : NoAnimation);
 
     onStyleOptionChanged(option, previousOption);
@@ -386,10 +407,10 @@ void KItemListView::setGeometry(const QRectF& rect)
         // update of the visible-roles sizes, only the stretched sizes
         // need to be adjusted to the new size.
         updateStretchedVisibleRolesSizes();
-        
+
         if (m_useHeaderWidths) {
             QSizeF dynamicItemSize = m_layouter->itemSize();
-    
+
             if (m_itemSize.width() < 0) {
                 const qreal requiredWidth = visibleRolesSizesWidthSum();
                 if (newSize.width() > requiredWidth) {
@@ -398,7 +419,7 @@ void KItemListView::setGeometry(const QRectF& rect)
                 const qreal headerWidth = qMax(newSize.width(), requiredWidth);
                 m_header->resize(headerWidth, m_header->size().height());
             }
-    
+
             if (m_itemSize.height() < 0) {
                 const qreal requiredHeight = visibleRolesSizesHeightSum();
                 if (newSize.height() > requiredHeight) {
@@ -406,10 +427,10 @@ void KItemListView::setGeometry(const QRectF& rect)
                 }
                 // TODO: KItemListHeader is not prepared for vertical alignment
             }
-    
+
             m_layouter->setItemSize(dynamicItemSize);
         }
-        
+
         // Triggering a synchronous layout is fine from a performance point of view,
         // as with dynamic item sizes no moving animation must be done.
         m_layouter->setSize(newSize);
@@ -417,9 +438,9 @@ void KItemListView::setGeometry(const QRectF& rect)
     } else {
         const bool animate = !changesItemGridLayout(newSize,
                                                     m_layouter->itemSize(),
-                                                    m_layouter->itemMargin());        
+                                                    m_layouter->itemMargin());
         m_layouter->setSize(newSize);
-        
+
         if (animate) {
             // Trigger an asynchronous relayout with m_layoutTimer to prevent
             // performance bottlenecks. If the timer is exceeded, an animated layout
@@ -502,6 +523,15 @@ QHash<QByteArray, QSizeF> KItemListView::visibleRolesSizes(const KItemRangeList&
     return QHash<QByteArray, QSizeF>();
 }
 
+void KItemListView::setSupportsItemExpanding(bool supportsExpanding)
+{
+    if (m_supportsItemExpanding != supportsExpanding) {
+        m_supportsItemExpanding = supportsExpanding;
+        updateSiblingsInformation();
+        onSupportsItemExpandingChanged(supportsExpanding);
+    }
+}
+
 bool KItemListView::supportsItemExpanding() const
 {
     return m_supportsItemExpanding;
@@ -597,6 +627,8 @@ void KItemListView::setHeaderShown(bool show)
 
         connect(m_header, SIGNAL(visibleRoleWidthChanged(QByteArray,qreal,qreal)),
                 this, SLOT(slotVisibleRoleWidthChanged(QByteArray,qreal,qreal)));
+        connect(m_header, SIGNAL(visibleRoleMoved(QByteArray,int,int)),
+                this, SLOT(slotVisibleRoleMoved(QByteArray,int,int)));
         connect(m_header, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)),
                 this, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)));
         connect(m_header, SIGNAL(sortRoleChanged(QByteArray,QByteArray)),
@@ -701,6 +733,11 @@ void KItemListView::onStyleOptionChanged(const KItemListStyleOption& current, co
     Q_UNUSED(previous);
 }
 
+void KItemListView::onSupportsItemExpandingChanged(bool supportsExpanding)
+{
+    Q_UNUSED(supportsExpanding);
+}
+
 void KItemListView::onTransactionBegin()
 {
 }
@@ -768,14 +805,6 @@ QList<KItemListWidget*> KItemListView::visibleItemListWidgets() const
     return m_visibleItems.values();
 }
 
-void KItemListView::setSupportsItemExpanding(bool supportsExpanding)
-{
-    if (m_supportsItemExpanding != supportsExpanding) {
-        m_supportsItemExpanding = supportsExpanding;
-        updateSiblingsInformation();
-    }
-}
-
 void KItemListView::slotItemsInserted(const KItemRangeList& itemRanges)
 {
     updateVisibleRolesSizes(itemRanges);
@@ -785,9 +814,6 @@ void KItemListView::slotItemsInserted(const KItemRangeList& itemRanges)
         beginTransaction();
     }
 
-    // Important: Don't read any m_layouter-property inside the for-loop in case if
-    // multiple ranges are given! m_layouter accesses m_sizeHintResolver which is
-    // updated in each loop-cycle and has only a consistent state after the loop.
     m_layouter->markAsDirty();
 
     int previouslyInsertedCount = 0;
@@ -823,11 +849,12 @@ void KItemListView::slotItemsInserted(const KItemRangeList& itemRanges)
         for (int i = itemsToMove.count() - 1; i >= 0; --i) {
             KItemListWidget* widget = m_visibleItems.value(itemsToMove[i]);
             Q_ASSERT(widget);
+            const int newIndex = widget->index() + count;
             if (hasMultipleRanges) {
-                setWidgetIndex(widget, widget->index() + count);
+                setWidgetIndex(widget, newIndex);
             } else {
                 // Try to animate the moving of the item
-                moveWidgetToIndex(widget, widget->index() + count);
+                moveWidgetToIndex(widget, newIndex);
             }
         }
 
@@ -861,6 +888,12 @@ void KItemListView::slotItemsInserted(const KItemRangeList& itemRanges)
     }
 
     if (hasMultipleRanges) {
+#ifndef QT_NO_DEBUG
+        // Important: Don't read any m_layouter-property inside the for-loop in case if
+        // multiple ranges are given! m_layouter accesses m_sizeHintResolver which is
+        // updated in each loop-cycle and has only a consistent state after the loop.
+        Q_ASSERT(m_layouter->isDirty());
+#endif
         m_endTransactionAnimationHint = NoAnimation;
         endTransaction();
         updateSiblingsInformation();
@@ -876,9 +909,6 @@ void KItemListView::slotItemsRemoved(const KItemRangeList& itemRanges)
         beginTransaction();
     }
 
-    // Important: Don't read any m_layouter-property inside the for-loop in case if
-    // multiple ranges are given! m_layouter accesses m_sizeHintResolver which is
-    // updated in each loop-cycle and has only a consistent state after the loop.
     m_layouter->markAsDirty();
 
     for (int i = itemRanges.count() - 1; i >= 0; --i) {
@@ -959,6 +989,12 @@ void KItemListView::slotItemsRemoved(const KItemRangeList& itemRanges)
     }
 
     if (hasMultipleRanges) {
+#ifndef QT_NO_DEBUG
+        // Important: Don't read any m_layouter-property inside the for-loop in case if
+        // multiple ranges are given! m_layouter accesses m_sizeHintResolver which is
+        // updated in each loop-cycle and has only a consistent state after the loop.
+        Q_ASSERT(m_layouter->isDirty());
+#endif
         m_endTransactionAnimationHint = NoAnimation;
         endTransaction();
         updateSiblingsInformation();
@@ -981,9 +1017,6 @@ void KItemListView::slotItemsMoved(const KItemRange& itemRange, const QList<int>
         KItemListWidget* widget = m_visibleItems.value(index);
         if (widget) {
             updateWidgetProperties(widget, index);
-            if (m_grouped) {
-                updateGroupHeaderForWidget(widget);
-            }
             initializeItemListWidget(widget);
         }
     }
@@ -1048,6 +1081,18 @@ void KItemListView::slotGroupedSortingChanged(bool current)
         Q_ASSERT(m_visibleGroups.isEmpty());
     }
 
+    if (useAlternateBackgrounds()) {
+        // Changing the group mode requires to update the alternate backgrounds
+        // as with the enabled group mode the altering is done on base of the first
+        // group item.
+        QHashIterator<int, KItemListWidget*> it(m_visibleItems);
+        while (it.hasNext()) {
+            it.next();
+            updateAlternateBackgroundForWidget(it.value());
+        }
+    }
+
+    updateSiblingsInformation();
     doLayout(NoAnimation);
 }
 
@@ -1077,13 +1122,11 @@ void KItemListView::slotCurrentChanged(int current, int previous)
 
     KItemListWidget* previousWidget = m_visibleItems.value(previous, 0);
     if (previousWidget) {
-        Q_ASSERT(previousWidget->isCurrent());
         previousWidget->setCurrent(false);
     }
 
     KItemListWidget* currentWidget = m_visibleItems.value(current, 0);
     if (currentWidget) {
-        Q_ASSERT(!currentWidget->isCurrent());
         currentWidget->setCurrent(true);
     }
 }
@@ -1201,6 +1244,23 @@ void KItemListView::slotVisibleRoleWidthChanged(const QByteArray& role,
     }
 }
 
+void KItemListView::slotVisibleRoleMoved(const QByteArray& role,
+                                         int currentIndex,
+                                         int previousIndex)
+{
+    Q_ASSERT(m_visibleRoles[previousIndex] == role);
+
+    const QList<QByteArray> previous = m_visibleRoles;
+
+    QList<QByteArray> current = m_visibleRoles;
+    current.removeAt(previousIndex);
+    current.insert(currentIndex, role);
+
+    setVisibleRoles(current);
+
+    emit visibleRolesChanged(current, previous);
+}
+
 void KItemListView::triggerAutoScrolling()
 {
     if (!m_autoScrollTimer) {
@@ -1317,7 +1377,7 @@ void KItemListView::setModel(KItemModelBase* model)
     }
 
     m_model = model;
-    m_layouter->setModel(model);   
+    m_layouter->setModel(model);
     m_grouped = model->groupedSorting();
 
     if (m_model) {
@@ -1406,10 +1466,8 @@ void KItemListView::doLayout(LayoutAnimationHint hint, int changedIndex, int cha
                 const int oldIndex = reusableItems.takeLast();
                 widget = m_visibleItems.value(oldIndex);
                 setWidgetIndex(widget, i);
-
-                if (m_grouped) {
-                    updateGroupHeaderForWidget(widget);
-                }
+                updateWidgetProperties(widget, i);
+                initializeItemListWidget(widget);
             } else {
                 // No reusable KItemListWidget instance is available, create a new one
                 widget = createWidget(i);
@@ -1551,7 +1609,7 @@ QList<int> KItemListView::recycleInvisibleItems(int firstVisibleIndex,
             } else {
                 widget->setVisible(false);
                 items.append(index);
-    
+
                 if (m_grouped) {
                     recycleGroupHeaderForWidget(widget);
                 }
@@ -1567,7 +1625,7 @@ bool KItemListView::moveWidget(KItemListWidget* widget,const QPointF& newPos)
     if (widget->pos() == newPos) {
         return false;
     }
-    
+
     bool startMovingAnim = false;
 
     // When having a grid the moving-animation should only be started, if it is done within
@@ -1626,14 +1684,9 @@ KItemListWidget* KItemListView::createWidget(int index)
     KItemListWidget* widget = m_widgetCreator->create(this);
     widget->setFlag(QGraphicsItem::ItemStacksBehindParent);
 
-    updateWidgetProperties(widget, index);
     m_visibleItems.insert(index, widget);
     m_visibleCells.insert(index, Cell());
-
-    if (m_grouped) {
-        updateGroupHeaderForWidget(widget);
-    }
-
+    updateWidgetProperties(widget, index);
     initializeItemListWidget(widget);
     return widget;
 }
@@ -1654,16 +1707,13 @@ void KItemListView::recycleWidget(KItemListWidget* widget)
 void KItemListView::setWidgetIndex(KItemListWidget* widget, int index)
 {
     const int oldIndex = widget->index();
-
     m_visibleItems.remove(oldIndex);
     m_visibleCells.remove(oldIndex);
 
-    updateWidgetProperties(widget, index);
-
     m_visibleItems.insert(index, widget);
     m_visibleCells.insert(index, Cell());
 
-    initializeItemListWidget(widget);
+    widget->setIndex(index);
 }
 
 void KItemListView::moveWidgetToIndex(KItemListWidget* widget, int index)
@@ -1701,11 +1751,15 @@ void KItemListView::updateWidgetProperties(KItemListWidget* widget, int index)
     widget->setCurrent(index == selectionManager->currentItem());
     widget->setSelected(selectionManager->isSelected(index));
     widget->setHovered(false);
-    widget->setAlternatingBackgroundColors(false);
     widget->setEnabledSelectionToggle(enabledSelectionToggles());
     widget->setIndex(index);
     widget->setData(m_model->data(index));
     widget->setSiblingsInformation(QBitArray());
+    updateAlternateBackgroundForWidget(widget);
+
+    if (m_grouped) {
+        updateGroupHeaderForWidget(widget);
+    }
 }
 
 void KItemListView::updateGroupHeaderForWidget(KItemListWidget* widget)
@@ -1734,21 +1788,9 @@ void KItemListView::updateGroupHeaderForWidget(KItemListWidget* widget)
     }
     Q_ASSERT(groupHeader->parentItem() == widget);
 
-    // Determine the shown data for the header by doing a binary
-    // search in the groups-list
-    int min = 0;
-    int max = groups.count() - 1;
-    int mid = 0;
-    do {
-        mid = (min + max) / 2;
-        if (index > groups.at(mid).first) {
-            min = mid + 1;
-        } else {
-            max = mid - 1;
-        }
-    } while (groups.at(mid).first != index && min <= max);
-
-    groupHeader->setData(groups.at(mid).second);
+    const int groupIndex = groupIndexForItem(index);
+    Q_ASSERT(groupIndex >= 0);
+    groupHeader->setData(groups.at(groupIndex).second);
     groupHeader->setRole(model()->sortRole());
     groupHeader->setStyleOption(m_styleOption);
     groupHeader->setScrollOrientation(scrollOrientation());
@@ -1803,6 +1845,60 @@ void KItemListView::updateVisibleGroupHeaders()
     }
 }
 
+int KItemListView::groupIndexForItem(int index) const
+{
+    Q_ASSERT(m_grouped);
+
+    const QList<QPair<int, QVariant> > groups = model()->groups();
+    if (groups.isEmpty()) {
+        return -1;
+    }
+
+    int min = 0;
+    int max = groups.count() - 1;
+    int mid = 0;
+    do {
+        mid = (min + max) / 2;
+        if (index > groups[mid].first) {
+            min = mid + 1;
+        } else {
+            max = mid - 1;
+        }
+    } while (groups[mid].first != index && min <= max);
+
+    if (min > max) {
+        while (groups[mid].first > index && mid > 0) {
+            --mid;
+        }
+    }
+
+    return mid;
+}
+
+void KItemListView::updateAlternateBackgroundForWidget(KItemListWidget* widget)
+{
+    bool enabled = useAlternateBackgrounds();
+    if (enabled) {
+        const int index = widget->index();
+        enabled = (index & 0x1) > 0;
+        if (m_grouped) {
+            const int groupIndex = groupIndexForItem(index);
+            if (groupIndex >= 0) {
+                const QList<QPair<int, QVariant> > groups = model()->groups();
+                const int indexOfFirstGroupItem = groups[groupIndex].first;
+                const int relativeIndex = index - indexOfFirstGroupItem;
+                enabled = (relativeIndex & 0x1) > 0;
+            }
+        }
+    }
+    widget->setAlternateBackground(enabled);
+}
+
+bool KItemListView::useAlternateBackgrounds() const
+{
+    return m_itemSize.isEmpty() && m_visibleRoles.count() > 1;
+}
+
 QHash<QByteArray, qreal> KItemListView::headerRolesWidths() const
 {
     QHash<QByteArray, qreal> rolesWidths;
@@ -1969,11 +2065,11 @@ QRectF KItemListView::headerBoundaries() const
 bool KItemListView::changesItemGridLayout(const QSizeF& newGridSize,
                                           const QSizeF& newItemSize,
                                           const QSizeF& newItemMargin) const
-{  
+{
     if (newItemSize.isEmpty() || newGridSize.isEmpty()) {
         return false;
     }
-    
+
     if (m_layouter->scrollOrientation() == Qt::Vertical) {
         const qreal itemWidth = m_layouter->itemSize().width();
         if (itemWidth > 0) {
@@ -1986,7 +2082,7 @@ bool KItemListView::changesItemGridLayout(const QSizeF& newGridSize,
                                                         m_layouter->itemMargin().width());
                 return oldColumnCount != newColumnCount;
             }
-        }       
+        }
     } else {
         const qreal itemHeight = m_layouter->itemSize().height();
         if (itemHeight > 0) {
@@ -1999,9 +2095,9 @@ bool KItemListView::changesItemGridLayout(const QSizeF& newGridSize,
                                                      m_layouter->itemMargin().height());
                 return oldRowCount != newRowCount;
             }
-        }               
+        }
     }
-    
+
     return false;
 }
 
@@ -2010,7 +2106,7 @@ bool KItemListView::animateChangedItemCount(int changedItemCount) const
     if (m_layouter->size().isEmpty() || m_layouter->itemSize().isEmpty()) {
         return false;
     }
-    
+
     const int maximum = (scrollOrientation() == Qt::Vertical)
                         ? m_layouter->size().width()  / m_layouter->itemSize().width()
                         : m_layouter->size().height() / m_layouter->itemSize().height();
@@ -2035,7 +2131,7 @@ void KItemListView::updateGroupHeaderHeight()
 {
     qreal groupHeaderHeight = m_styleOption.fontMetrics.height();
     qreal groupHeaderMargin = 0;
-    
+
     if (scrollOrientation() == Qt::Horizontal) {
         // The vertical margin above and below the header should be
         // equal to the horizontal margin, not the vertical margin
@@ -2043,10 +2139,10 @@ void KItemListView::updateGroupHeaderHeight()
         groupHeaderHeight += 2 * m_styleOption.horizontalMargin;
         groupHeaderMargin = m_styleOption.horizontalMargin;
     } else if (m_itemSize.isEmpty()){
-        groupHeaderHeight += 2 * m_styleOption.padding;
+        groupHeaderHeight += 4 * m_styleOption.padding;
         groupHeaderMargin = m_styleOption.iconSize / 2;
     } else {
-        groupHeaderHeight += 2 * m_styleOption.padding;
+        groupHeaderHeight += 2 * m_styleOption.padding + m_styleOption.verticalMargin;
         groupHeaderMargin = m_styleOption.iconSize / 4;
     }
     m_layouter->setGroupHeaderHeight(groupHeaderHeight);
@@ -2057,7 +2153,7 @@ void KItemListView::updateGroupHeaderHeight()
 
 void KItemListView::updateSiblingsInformation(int firstIndex, int lastIndex)
 {
-    if (!supportsItemExpanding()) {
+    if (!supportsItemExpanding() || !m_model) {
         return;
     }