[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [amarok] /: Fix Collection Browser auto-expand after search expanding too little
From:       Matěj_Laitl <matej () laitl ! cz>
Date:       2016-10-16 22:09:10
Message-ID: E1bvtcE-0000ak-BD () code ! kde ! org
[Download RAW message or body]

Git commit 9fb00e5a97574af3f0cb22d740da7e0cd0761d78 by Matěj Laitl.
Committed on 16/10/2016 at 22:03.
Pushed by laitl into branch 'master'.

Fix Collection Browser auto-expand after search expanding too little

Underlying model is asynchronous, we need to cope with that. Do it
using a RAII self-destructing object AutoExpander.

As a side-effect, Amarok no longer expands collection after start in
split collection mode. If this is desired, we may expand them
explicitly upon start.

BUG: 335217
FIXED-IN: 2.9
CCBUG: 300557

M  +1    -0    ChangeLog
M  +68   -20   src/browsers/CollectionTreeView.cpp

http://commits.kde.org/amarok/9fb00e5a97574af3f0cb22d740da7e0cd0761d78

diff --git a/ChangeLog b/ChangeLog
index f427e97..2f1f907 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -17,6 +17,7 @@ VERSION 2.9.0
      thanks to Stefano Pettini. (BR 226144)
    * Fix MPRIS2 DesktopEntry name, makes media controls in Plasma 5.7 taskbar work
      again; thanks to Antonio Rojas, Rex Dieter. (BR 365275)
+   * Auto-expand after search in Collection Browser works correctly again. (BR 335217)
 
 
 VERSION 2.8.90
diff --git a/src/browsers/CollectionTreeView.cpp b/src/browsers/CollectionTreeView.cpp
index 816d2e1..4b96543 100644
--- a/src/browsers/CollectionTreeView.cpp
+++ b/src/browsers/CollectionTreeView.cpp
@@ -110,6 +110,72 @@ class DelayedScroller : public QObject
         int m_topOffset;
 };
 
+/**
+ * RAII class to auto-expand collection tree entries after filtering.
+ * AutoExpander auto-deletes itself once its job is over (or if it finds
+ * it is useless).
+ */
+class AutoExpander : public QObject
+{
+    Q_OBJECT
+
+    public:
+        AutoExpander( CollectionTreeView *treeView,
+                      CollectionTreeItemModelBase *treeModel,
+                      QAbstractItemModel *filterModel)
+            : QObject( treeView )
+            , m_treeView( treeView )
+            , m_filterModel( filterModel )
+        {
+            connect( filterModel, SIGNAL(destroyed(QObject*)), SLOT(deleteLater()) );
+            connect( treeModel, SIGNAL(allQueriesFinished(bool)), SLOT(slotExpandMore()) );
+
+            // start with the root index
+            m_indicesToCheck.enqueue( QModelIndex() );
+            slotExpandMore();
+        }
+
+    private slots:
+        void slotExpandMore()
+        {
+            const int maxChildrenToExpand = 3;
+
+            QQueue<QModelIndex> pendingIndices;
+            while( !m_indicesToCheck.isEmpty() )
+            {
+                QModelIndex current = m_indicesToCheck.dequeue();
+
+                if( m_filterModel->canFetchMore( current ) )
+                {
+                    m_filterModel->fetchMore( current );
+                    pendingIndices.enqueue( current );
+                    continue;
+                }
+
+                if( m_filterModel->rowCount( current ) <= maxChildrenToExpand )
+                {
+                    m_treeView->expand( current );
+                    for( int i = 0; i < m_filterModel->rowCount( current ); i++ )
+                        m_indicesToCheck.enqueue( m_filterModel->index( i, 0, current ) );
+                }
+            }
+
+            if( pendingIndices.isEmpty() )
+                // nothing left to do
+                deleteLater();
+            else
+                // process pending indices when queries finish
+                m_indicesToCheck.swap( pendingIndices );
+        }
+
+    private:
+        Q_DISABLE_COPY(AutoExpander)
+
+        CollectionTreeView *m_treeView;
+        QAbstractItemModel *m_filterModel;
+        QQueue<QModelIndex> m_indicesToCheck;
+};
+
 CollectionTreeView::CollectionTreeView( QWidget *parent)
     : Amarok::PrettyTreeView( parent )
     , m_filterModel( 0 )
@@ -708,26 +774,8 @@ CollectionTreeView::slotCheckAutoExpand( bool reallyExpand )
     if( !m_filterModel || !reallyExpand )
         return;
 
-    // Cases where root is not collections but
-    // for example magnatunes's plethora of genres, don't expand by default
-    if( m_filterModel->rowCount() > 6 )
-        return;
-
-    QModelIndexList indicesToCheck;
-    for( int i = 0; i < m_filterModel->rowCount(); i++ ) //need something to start for'ing with
-        indicesToCheck += m_filterModel->index( i, 0 ); //lowest level is fine for that
-
-    QModelIndex current;
-    for( int j = 0; j < indicesToCheck.size(); j++)
-    {
-        current = indicesToCheck.at( j );
-        if( m_filterModel->rowCount( current ) < 4 )
-        { //don't expand if many results
-            expand( current );
-            for( int i = 0; i < m_filterModel->rowCount( current ); i++ )
-                indicesToCheck += m_filterModel->index( i, 0, current );
-        }
-    }
+    // auto-deletes itself:
+    new AutoExpander( this, m_treeModel, m_filterModel );
 }
 
 void
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic