]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistview.cpp
Fixed empty files being erroneously grouped together with folders in "By size" grouping
[dolphin.git] / src / kitemviews / kitemlistview.cpp
index 659e59f0f40e7fb9794aebd1f491f042110f0e2c..45f5851bf5d6c2e06bd592c09350058fea35a2a8 100644 (file)
@@ -402,6 +402,10 @@ qreal KItemListView::verticalPageStep() const
 
 std::optional<int> KItemListView::itemAt(const QPointF &pos) const
 {
+    if (headerBoundaries().contains(pos)) {
+        return std::nullopt;
+    }
+
     QHashIterator<int, KItemListWidget *> it(m_visibleItems);
     while (it.hasNext()) {
         it.next();
@@ -537,7 +541,7 @@ bool KItemListView::isElided(int index) const
     return m_sizeHintResolver->isElided(index);
 }
 
-void KItemListView::scrollToItem(int index)
+void KItemListView::scrollToItem(int index, ViewItemPosition viewItemPosition)
 {
     QRectF viewGeometry = geometry();
     if (m_headerWidget->isVisible()) {
@@ -546,29 +550,89 @@ void KItemListView::scrollToItem(int index)
     }
     QRectF currentRect = itemRect(index);
 
-    // Fix for Bug 311099 - View the underscore when using Ctrl + PagDown
+    if (layoutDirection() == Qt::RightToLeft && scrollOrientation() == Qt::Horizontal) {
+        currentRect.moveLeft(m_layouter->size().width() - currentRect.right());
+    }
+
+    // Fix for Bug 311099 - View the underscore when using Ctrl + PageDown
     currentRect.adjust(-m_styleOption.horizontalMargin, -m_styleOption.verticalMargin, m_styleOption.horizontalMargin, m_styleOption.verticalMargin);
 
-    if (!viewGeometry.contains(currentRect)) {
-        qreal newOffset = scrollOffset();
-        if (scrollOrientation() == Qt::Vertical) {
-            if (currentRect.top() < viewGeometry.top()) {
-                newOffset += currentRect.top() - viewGeometry.top();
-            } else if (currentRect.bottom() > viewGeometry.bottom()) {
-                newOffset += currentRect.bottom() - viewGeometry.bottom();
+    qreal offset = 0;
+    switch (scrollOrientation()) {
+    case Qt::Vertical:
+        if (currentRect.top() < viewGeometry.top() || currentRect.bottom() > viewGeometry.bottom()) {
+            switch (viewItemPosition) {
+            case Beginning:
+                offset = currentRect.top() - viewGeometry.top();
+                break;
+            case Middle:
+                offset = 0.5 * (currentRect.top() + currentRect.bottom() - (viewGeometry.top() + viewGeometry.bottom()));
+                break;
+            case End:
+                offset = currentRect.bottom() - viewGeometry.bottom();
+                break;
+            case Nearest:
+                if (currentRect.top() < viewGeometry.top()) {
+                    offset = currentRect.top() - viewGeometry.top();
+                }
+                if (currentRect.bottom() > viewGeometry.bottom() + offset) {
+                    offset += currentRect.bottom() - viewGeometry.bottom() - offset;
+                }
+                break;
+            default:
+                Q_UNREACHABLE();
             }
-        } else {
-            if (currentRect.left() < viewGeometry.left()) {
-                newOffset += currentRect.left() - viewGeometry.left();
-            } else if (currentRect.right() > viewGeometry.right()) {
-                newOffset += currentRect.right() - viewGeometry.right();
+        }
+        break;
+    case Qt::Horizontal:
+        if (currentRect.left() < viewGeometry.left() || currentRect.right() > viewGeometry.right()) {
+            switch (viewItemPosition) {
+            case Beginning:
+                if (layoutDirection() == Qt::RightToLeft) {
+                    offset = currentRect.right() - viewGeometry.right();
+                } else {
+                    offset = currentRect.left() - viewGeometry.left();
+                }
+                break;
+            case Middle:
+                offset = 0.5 * (currentRect.left() + currentRect.right() - (viewGeometry.left() + viewGeometry.right()));
+                break;
+            case End:
+                if (layoutDirection() == Qt::RightToLeft) {
+                    offset = currentRect.left() - viewGeometry.left();
+                } else {
+                    offset = currentRect.right() - viewGeometry.right();
+                }
+                break;
+            case Nearest:
+                if (layoutDirection() == Qt::RightToLeft) {
+                    if (currentRect.left() < viewGeometry.left()) {
+                        offset = currentRect.left() - viewGeometry.left();
+                    }
+                    if (currentRect.right() > viewGeometry.right() + offset) {
+                        offset += currentRect.right() - viewGeometry.right() - offset;
+                    }
+                } else {
+                    if (currentRect.right() > viewGeometry.right()) {
+                        offset = currentRect.right() - viewGeometry.right();
+                    }
+                    if (currentRect.left() < viewGeometry.left() + offset) {
+                        offset += currentRect.left() - viewGeometry.left() - offset;
+                    }
+                }
+                break;
+            default:
+                Q_UNREACHABLE();
             }
         }
+        break;
+    default:
+        Q_UNREACHABLE();
+    }
 
-        if (newOffset != scrollOffset()) {
-            Q_EMIT scrollTo(newOffset);
-            return;
-        }
+    if (!qFuzzyIsNull(offset)) {
+        Q_EMIT scrollTo(scrollOffset() + offset);
+        return;
     }
 
     Q_EMIT scrollingStopped();
@@ -1417,6 +1481,26 @@ void KItemListView::slotSortRoleChanged(const QByteArray &current, const QByteAr
     }
 }
 
+void KItemListView::slotGroupOrderChanged(Qt::SortOrder current, Qt::SortOrder previous)
+{
+    Q_UNUSED(current)
+    Q_UNUSED(previous)
+    if (m_grouped) {
+        updateVisibleGroupHeaders();
+        doLayout(NoAnimation);
+    }
+}
+
+void KItemListView::slotGroupRoleChanged(const QByteArray &current, const QByteArray &previous)
+{
+    Q_UNUSED(current)
+    Q_UNUSED(previous)
+    if (m_grouped) {
+        updateVisibleGroupHeaders();
+        doLayout(NoAnimation);
+    }
+}
+
 void KItemListView::slotCurrentChanged(int current, int previous)
 {
     Q_UNUSED(previous)
@@ -1679,6 +1763,8 @@ void KItemListView::setModel(KItemModelBase *model)
         disconnect(m_model, &KItemModelBase::groupedSortingChanged, this, &KItemListView::slotGroupedSortingChanged);
         disconnect(m_model, &KItemModelBase::sortOrderChanged, this, &KItemListView::slotSortOrderChanged);
         disconnect(m_model, &KItemModelBase::sortRoleChanged, this, &KItemListView::slotSortRoleChanged);
+        disconnect(m_model, &KItemModelBase::groupOrderChanged, this, &KItemListView::slotGroupOrderChanged);
+        disconnect(m_model, &KItemModelBase::groupRoleChanged, this, &KItemListView::slotGroupRoleChanged);
 
         m_sizeHintResolver->itemsRemoved(KItemRangeList() << KItemRange(0, m_model->count()));
     }
@@ -2121,7 +2207,7 @@ void KItemListView::updateGroupHeaderForWidget(KItemListWidget *widget)
     const int groupIndex = groupIndexForItem(index);
     Q_ASSERT(groupIndex >= 0);
     groupHeader->setData(groups.at(groupIndex).second);
-    groupHeader->setRole(model()->sortRole());
+    groupHeader->setRole(model()->groupRole());
     groupHeader->setStyleOption(m_styleOption);
     groupHeader->setScrollOrientation(scrollOrientation());
     groupHeader->setItemIndex(index);