1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-05 21:57:41 +02:00

* Speed up collection removal in CollectionModel / TrackModel.

This commit is contained in:
Christian Muehlhaeuser
2010-10-23 08:49:32 +02:00
parent 0f9772e17a
commit 5ed57069d3
6 changed files with 93 additions and 35 deletions

View File

@@ -14,7 +14,7 @@ public:
PlaylistInterface() PlaylistInterface()
{ {
m_rootItem = new PlItem(); m_rootItem = 0;
} }
virtual ~PlaylistInterface() {} virtual ~PlaylistInterface() {}

View File

@@ -11,6 +11,7 @@ CollectionFlatModel::CollectionFlatModel( QObject* parent )
: TrackModel( parent ) : TrackModel( parent )
{ {
qDebug() << Q_FUNC_INFO; 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 ) ) ); this, SLOT( onTracksAddingFinished( Tomahawk::collection_ptr ) ) );
QList<PlItem*> plitems = m_collectionIndex.values( collection ); QList<PlItem*> plitems = m_collectionIndex.values( collection );
QList<int> 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 ); sortrows.append( rows.at( x ) );
int start = rows.first(); rows.removeAt( x );
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 );
} }
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 ); m_collectionIndex.remove( collection );
} }
@@ -108,7 +123,11 @@ void
CollectionFlatModel::onTracksAdded( const QList<QVariant>& tracks, const collection_ptr& collection ) CollectionFlatModel::onTracksAdded( const QList<QVariant>& tracks, const collection_ptr& collection )
{ {
int c = rowCount( QModelIndex() ); 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; PlItem* plitem;
foreach( const QVariant& v, tracks ) foreach( const QVariant& v, tracks )
@@ -132,6 +151,7 @@ CollectionFlatModel::onTracksAdded( const QList<QVariant>& tracks, const collect
m_collectionIndex.insertMulti( collection, plitem ); m_collectionIndex.insertMulti( collection, plitem );
} }
m_collectionRows.insertMulti( collection, crows );
emit endInsertRows(); emit endInsertRows();
emit trackCountChanged( rowCount( QModelIndex() ) ); emit trackCountChanged( rowCount( QModelIndex() ) );

View File

@@ -49,6 +49,7 @@ private slots:
private: private:
QMap< Tomahawk::collection_ptr, PlItem* > m_collectionIndex; QMap< Tomahawk::collection_ptr, PlItem* > m_collectionIndex;
QMap< Tomahawk::collection_ptr, QPair< int, int > > m_collectionRows;
}; };
#endif // COLLECTIONFLATMODEL_H #endif // COLLECTIONFLATMODEL_H

View File

@@ -11,6 +11,7 @@ PlaylistModel::PlaylistModel( QObject* parent )
: TrackModel( parent ) : TrackModel( parent )
{ {
qDebug() << Q_FUNC_INFO; 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 ); emit beginRemoveRows( QModelIndex(), 0, rowCount( QModelIndex() ) - 1 );
delete m_rootItem; delete m_rootItem;
m_rootItem = new PlItem(); m_rootItem = new PlItem( 0, this );
emit endRemoveRows(); emit endRemoveRows();
m_playlist = playlist; m_playlist = playlist;

View File

@@ -11,19 +11,24 @@ PlItem::~PlItem()
{ {
qDeleteAll( children ); qDeleteAll( children );
// Q_ASSERT( parent->children.at( m_parentPos ) == this );
if ( parent ) 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->parent = parent;
this->model = model;
childCount = 0; childCount = 0;
toberemoved = false;
if ( parent ) if ( parent )
{ {
parent->children.append( this ); 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->parent = parent;
this->caption = caption; this->caption = caption;
this->model = parent->model;
childCount = 0; childCount = 0;
m_isPlaying = false; m_isPlaying = false;
toberemoved = false;
if ( parent ) if ( parent )
{ {
parent->children.append( this ); 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 ) if ( parent )
{ {
parent->children.append( this ); 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; m_isPlaying = false;
toberemoved = false;
m_query = query; m_query = query;
if ( query->numResults() ) if ( query->numResults() )
onResultsAdded( query->results() ); onResultsAdded( query->results() );
@@ -83,3 +97,20 @@ PlItem::onResultsAdded( const QList<Tomahawk::result_ptr>& results )
emit dataChanged(); 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;
}
}
}
}

View File

@@ -2,7 +2,8 @@
#define PLITEM_H #define PLITEM_H
#include <QHash> #include <QHash>
#include <QPersistentModelIndex> #include <QModelIndex>
#include <QAbstractItemModel>
#include "tomahawk/query.h" #include "tomahawk/query.h"
#include "tomahawk/result.h" #include "tomahawk/result.h"
@@ -14,7 +15,7 @@ Q_OBJECT
public: public:
~PlItem(); ~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 QString& caption, PlItem* parent = 0 );
explicit PlItem( const Tomahawk::query_ptr& query, PlItem* parent = 0 ); explicit PlItem( const Tomahawk::query_ptr& query, PlItem* parent = 0 );
explicit PlItem( const Tomahawk::plentry_ptr& entry, PlItem* parent = 0 ); explicit PlItem( const Tomahawk::plentry_ptr& entry, PlItem* parent = 0 );
@@ -31,12 +32,15 @@ public:
QString caption; QString caption;
int childCount; int childCount;
QModelIndex index; QModelIndex index;
QAbstractItemModel* model;
bool toberemoved;
signals: signals:
void dataChanged(); void dataChanged();
private slots: private slots:
void onResultsAdded( const QList<Tomahawk::result_ptr>& result ); void onResultsAdded( const QList<Tomahawk::result_ptr>& result );
void onModelRowsRemoved( const QModelIndex& index, int start, int end );
private: private:
void setupItem( const Tomahawk::query_ptr& query, PlItem* parent ); void setupItem( const Tomahawk::query_ptr& query, PlItem* parent );
@@ -44,6 +48,7 @@ private:
Tomahawk::plentry_ptr m_entry; Tomahawk::plentry_ptr m_entry;
Tomahawk::query_ptr m_query; Tomahawk::query_ptr m_query;
bool m_isPlaying; bool m_isPlaying;
int m_parentPos;
}; };
#endif // PLITEM_H #endif // PLITEM_H