From 5ed57069d37a49448efd7cf36199552e3ec896be Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sat, 23 Oct 2010 08:49:32 +0200 Subject: [PATCH] * Speed up collection removal in CollectionModel / TrackModel. --- include/tomahawk/playlistinterface.h | 2 +- src/playlist/collectionflatmodel.cpp | 78 +++++++++++++++++----------- src/playlist/collectionflatmodel.h | 1 + src/playlist/playlistmodel.cpp | 3 +- src/playlist/plitem.cpp | 35 ++++++++++++- src/playlist/plitem.h | 9 +++- 6 files changed, 93 insertions(+), 35 deletions(-) diff --git a/include/tomahawk/playlistinterface.h b/include/tomahawk/playlistinterface.h index 061a459d2..d8b08330d 100644 --- a/include/tomahawk/playlistinterface.h +++ b/include/tomahawk/playlistinterface.h @@ -14,7 +14,7 @@ public: PlaylistInterface() { - m_rootItem = new PlItem(); + m_rootItem = 0; } virtual ~PlaylistInterface() {} diff --git a/src/playlist/collectionflatmodel.cpp b/src/playlist/collectionflatmodel.cpp index ae21885f8..16361de1f 100644 --- a/src/playlist/collectionflatmodel.cpp +++ b/src/playlist/collectionflatmodel.cpp @@ -11,6 +11,7 @@ CollectionFlatModel::CollectionFlatModel( QObject* parent ) : TrackModel( parent ) { qDebug() << Q_FUNC_INFO; + m_rootItem = new PlItem( 0, this ); } @@ -65,41 +66,55 @@ CollectionFlatModel::removeCollection( const collection_ptr& collection ) this, SLOT( onTracksAddingFinished( Tomahawk::collection_ptr ) ) ); QList plitems = m_collectionIndex.values( collection ); - QList rows; + QList< QPair< int, int > > rows; + QList< QPair< int, int > > sortrows; + QPair< int, int > row; + QPair< int, int > rowf; + rows = m_collectionRows.values( collection ); - if ( plitems.count() ) + while ( rows.count() ) { - foreach( PlItem* item, plitems ) + int x = -1; + int j = 0; + foreach( row, rows ) { - rows << item->index.row(); + if ( x < 0 || row.first > rows.at( x ).first ) + x = j; + + j++; } - qSort( rows ); - int start = rows.first(); - int end = rows.first(); - - foreach( int i, rows ) - { - if ( i > end + 1 ) - { - qDebug() << start << end; - emit beginRemoveRows( plitems.first()->parent->index, start, end ); - emit endRemoveRows(); - - start = i; - } - - end = i; - } - - qDebug() << start << end; - - emit beginRemoveRows( plitems.first()->parent->index, start, end ); - emit endRemoveRows(); - - qDeleteAll( plitems ); + sortrows.append( rows.at( x ) ); + rows.removeAt( x ); } + foreach( row, sortrows ) + { + QMap< Tomahawk::collection_ptr, QPair< int, int > > newrows; + foreach ( const collection_ptr& col, m_collectionRows.uniqueKeys() ) + { + if ( col.data() == collection.data() ) + continue; + + foreach ( rowf, m_collectionRows.values( col ) ) + { + if ( rowf.first > row.first ) + { + rowf.first -= ( row.second - row.first ) + 1; + rowf.second -= ( row.second - row.first ) + 1; + } + newrows.insertMulti( col, rowf ); + } + } + + m_collectionRows = newrows; + + qDebug() << "Removing rows:" << row.first << row.second; + emit beginRemoveRows( QModelIndex(), row.first, row.second ); + emit endRemoveRows(); + } + + qDeleteAll( plitems ); m_collectionIndex.remove( collection ); } @@ -108,7 +123,11 @@ void CollectionFlatModel::onTracksAdded( const QList& tracks, const collection_ptr& collection ) { int c = rowCount( QModelIndex() ); - emit beginInsertRows( QModelIndex(), c, c + tracks.count() - 1 ); + QPair< int, int > crows; + crows.first = c; + crows.second = c + tracks.count() - 1; + + emit beginInsertRows( QModelIndex(), crows.first, crows.second ); PlItem* plitem; foreach( const QVariant& v, tracks ) @@ -132,6 +151,7 @@ CollectionFlatModel::onTracksAdded( const QList& tracks, const collect m_collectionIndex.insertMulti( collection, plitem ); } + m_collectionRows.insertMulti( collection, crows ); emit endInsertRows(); emit trackCountChanged( rowCount( QModelIndex() ) ); diff --git a/src/playlist/collectionflatmodel.h b/src/playlist/collectionflatmodel.h index a12bc91e9..02647f34b 100644 --- a/src/playlist/collectionflatmodel.h +++ b/src/playlist/collectionflatmodel.h @@ -49,6 +49,7 @@ private slots: private: QMap< Tomahawk::collection_ptr, PlItem* > m_collectionIndex; + QMap< Tomahawk::collection_ptr, QPair< int, int > > m_collectionRows; }; #endif // COLLECTIONFLATMODEL_H diff --git a/src/playlist/playlistmodel.cpp b/src/playlist/playlistmodel.cpp index 2a0a5082c..87abe994c 100644 --- a/src/playlist/playlistmodel.cpp +++ b/src/playlist/playlistmodel.cpp @@ -11,6 +11,7 @@ PlaylistModel::PlaylistModel( QObject* parent ) : TrackModel( parent ) { qDebug() << Q_FUNC_INFO; + m_rootItem = new PlItem( 0, this ); } @@ -48,7 +49,7 @@ PlaylistModel::loadPlaylist( const Tomahawk::playlist_ptr& playlist ) emit beginRemoveRows( QModelIndex(), 0, rowCount( QModelIndex() ) - 1 ); delete m_rootItem; - m_rootItem = new PlItem(); + m_rootItem = new PlItem( 0, this ); emit endRemoveRows(); m_playlist = playlist; diff --git a/src/playlist/plitem.cpp b/src/playlist/plitem.cpp index 0e98432ad..3f77c1bcb 100644 --- a/src/playlist/plitem.cpp +++ b/src/playlist/plitem.cpp @@ -11,19 +11,24 @@ PlItem::~PlItem() { qDeleteAll( children ); +// Q_ASSERT( parent->children.at( m_parentPos ) == this ); + if ( parent ) - parent->children.removeOne( this ); + parent->children.removeAt( m_parentPos ); } -PlItem::PlItem( PlItem* parent ) +PlItem::PlItem( PlItem* parent, QAbstractItemModel* model ) { this->parent = parent; + this->model = model; childCount = 0; + toberemoved = false; if ( parent ) { parent->children.append( this ); + m_parentPos = parent->children.count() - 1; } } @@ -32,12 +37,15 @@ PlItem::PlItem( const QString& caption, PlItem* parent ) { this->parent = parent; this->caption = caption; + this->model = parent->model; childCount = 0; m_isPlaying = false; + toberemoved = false; if ( parent ) { parent->children.append( this ); + m_parentPos = parent->children.count() - 1; } } @@ -64,9 +72,15 @@ PlItem::setupItem( const Tomahawk::query_ptr& query, PlItem* parent ) if ( parent ) { parent->children.append( this ); + m_parentPos = parent->children.count() - 1; + this->model = parent->model; + + connect( model, SIGNAL( rowsRemoved( QModelIndex, int, int ) ), + SLOT( onModelRowsRemoved( QModelIndex, int, int ) ) ); } m_isPlaying = false; + toberemoved = false; m_query = query; if ( query->numResults() ) onResultsAdded( query->results() ); @@ -83,3 +97,20 @@ PlItem::onResultsAdded( const QList& results ) emit dataChanged(); } + +void +PlItem::onModelRowsRemoved( const QModelIndex& index, int start, int end ) +{ + if ( !toberemoved && this->parent->index == index ) + { + if ( ( start <= m_parentPos ) && ( m_parentPos <= end ) ) + toberemoved = true; + else + { + if ( start < m_parentPos ) + { + m_parentPos -= ( end - start ) + 1; + } + } + } +} diff --git a/src/playlist/plitem.h b/src/playlist/plitem.h index 19a64d430..222077835 100644 --- a/src/playlist/plitem.h +++ b/src/playlist/plitem.h @@ -2,7 +2,8 @@ #define PLITEM_H #include -#include +#include +#include #include "tomahawk/query.h" #include "tomahawk/result.h" @@ -14,7 +15,7 @@ Q_OBJECT public: ~PlItem(); - explicit PlItem( PlItem* parent = 0 ); + explicit PlItem( PlItem* parent = 0, QAbstractItemModel* model = 0 ); explicit PlItem( const QString& caption, PlItem* parent = 0 ); explicit PlItem( const Tomahawk::query_ptr& query, PlItem* parent = 0 ); explicit PlItem( const Tomahawk::plentry_ptr& entry, PlItem* parent = 0 ); @@ -31,12 +32,15 @@ public: QString caption; int childCount; QModelIndex index; + QAbstractItemModel* model; + bool toberemoved; signals: void dataChanged(); private slots: void onResultsAdded( const QList& result ); + void onModelRowsRemoved( const QModelIndex& index, int start, int end ); private: void setupItem( const Tomahawk::query_ptr& query, PlItem* parent ); @@ -44,6 +48,7 @@ private: Tomahawk::plentry_ptr m_entry; Tomahawk::query_ptr m_query; bool m_isPlaying; + int m_parentPos; }; #endif // PLITEM_H