]> cloud.milkyroute.net Git - dolphin.git/commitdiff
KItemListView: add view position in scrollToItem()
authorAmol Godbole <amolagodbole@gmail.com>
Sat, 14 Oct 2023 02:08:35 +0000 (21:08 -0500)
committerMéven Car <meven.car@kdemail.net>
Tue, 17 Oct 2023 15:47:34 +0000 (15:47 +0000)
An item, on being scrolled to, is always located at the nearest edge of
the view. This is not always convenient. Allow specifying where the item
should be positioned with respect to the view in scrollToItem().

BUG: 423884

src/kitemviews/kitemlistcontroller.cpp
src/kitemviews/kitemlistview.cpp
src/kitemviews/kitemlistview.h
src/views/dolphinview.cpp

index be7a63e0993b43b16e0b5e8564fdb568999034d7..0016bb22aeb4fdf27f4c999ff3d6b7719a287ec1 100644 (file)
@@ -539,7 +539,7 @@ void KItemListController::slotChangeCurrentItem(const QString &text, bool search
             m_selectionManager->beginAnchoredSelection(index);
         }
 
-        m_view->scrollToItem(index);
+        m_view->scrollToItem(index, KItemListView::ViewItemPosition::Beginning);
     }
 }
 
index 457c02ec5f5253f70f504ee50b76384aa7b8ec52..be22b91cce91a4af301520c8d79359135f1dabed 100644 (file)
@@ -537,7 +537,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 +546,65 @@ void KItemListView::scrollToItem(int index)
     }
     QRectF currentRect = itemRect(index);
 
-    // Fix for Bug 311099 - View the underscore when using Ctrl + PagDown
+    // 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) {
+    qreal newOffset = scrollOffset();
+    if (scrollOrientation() == Qt::Vertical && (currentRect.top() < viewGeometry.top() || currentRect.bottom() > viewGeometry.bottom())) {
+        switch (viewItemPosition) {
+        case Beginning:
+            newOffset += currentRect.top() - viewGeometry.top();
+            break;
+        case Middle:
+            newOffset += 0.5 * (currentRect.top() + currentRect.bottom() - (viewGeometry.top() + viewGeometry.bottom()));
+            break;
+        case End:
+            newOffset += currentRect.bottom() - viewGeometry.bottom();
+            break;
+        case Nearest:
             if (currentRect.top() < viewGeometry.top()) {
                 newOffset += currentRect.top() - viewGeometry.top();
-            } else if (currentRect.bottom() > viewGeometry.bottom()) {
+            } else {
                 newOffset += currentRect.bottom() - viewGeometry.bottom();
             }
-        } else {
+            break;
+        default:
+            Q_UNREACHABLE();
+        }
+    } else if (scrollOrientation() == Qt::Horizontal && (currentRect.left() < viewGeometry.left() || currentRect.right() > viewGeometry.right())) {
+        switch (viewItemPosition) {
+        case Beginning:
+            if (layoutDirection() == Qt::RightToLeft) {
+                newOffset += currentRect.right() - viewGeometry.right();
+            } else {
+                newOffset += currentRect.left() - viewGeometry.left();
+            }
+            break;
+        case Middle:
+            newOffset += 0.5 * (currentRect.left() + currentRect.right() - (viewGeometry.left() + viewGeometry.right()));
+            break;
+        case End:
+            if (layoutDirection() == Qt::RightToLeft) {
+                newOffset += currentRect.left() - viewGeometry.left();
+            } else {
+                newOffset += currentRect.right() - viewGeometry.right();
+            }
+            break;
+        case Nearest:
             if (currentRect.left() < viewGeometry.left()) {
                 newOffset += currentRect.left() - viewGeometry.left();
-            } else if (currentRect.right() > viewGeometry.right()) {
+            } else {
                 newOffset += currentRect.right() - viewGeometry.right();
             }
+            break;
+        default:
+            Q_UNREACHABLE();
         }
+    }
 
-        if (newOffset != scrollOffset()) {
-            Q_EMIT scrollTo(newOffset);
-            return;
-        }
+    if (newOffset != scrollOffset()) {
+        Q_EMIT scrollTo(newOffset);
+        return;
     }
 
     Q_EMIT scrollingStopped();
index 7bcaec704dee9f8316852e7457a66a6f4bcec827..8812eb8cc45d3b9fdb07649daece70f4b83d79f9 100644 (file)
@@ -59,6 +59,9 @@ class DOLPHIN_EXPORT KItemListView : public QGraphicsWidget
     Q_PROPERTY(qreal itemOffset READ itemOffset WRITE setItemOffset NOTIFY itemOffsetChanged)
 
 public:
+    /** The position in the view to which an item should be scrolled to. */
+    enum ViewItemPosition { Beginning, Middle, End, Nearest };
+
     explicit KItemListView(QGraphicsWidget *parent = nullptr);
     ~KItemListView() override;
 
@@ -251,9 +254,10 @@ public:
 
     /**
      * Scrolls to the item with the index \a index so that the item
-     * will be fully visible.
+     * will be fully visible. The item is positioned within the view
+     * as specified by \a viewItemPosition.
      */
-    void scrollToItem(int index);
+    void scrollToItem(int index, ViewItemPosition viewItemPosition = ViewItemPosition::Nearest);
 
     /**
      * If several properties of KItemListView are changed synchronously, it is
index ace763b153167949f3b61733701c09a7528441d6..0a1d70bf11e334fc3d644861f4209c5546f95fc1 100644 (file)
@@ -1761,7 +1761,7 @@ void DolphinView::updateViewState()
 
                 // scroll to current item and reset the state
                 if (m_scrollToCurrentItem) {
-                    m_view->scrollToItem(currentIndex);
+                    m_view->scrollToItem(currentIndex, KItemListView::ViewItemPosition::Middle);
                     m_scrollToCurrentItem = false;
                 }
                 m_currentItemUrl = QUrl();