1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-01-17 22:38:33 +01: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()
{
m_rootItem = new PlItem();
m_rootItem = 0;
}
virtual ~PlaylistInterface() {}

View File

@ -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<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 );
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<QVariant>& 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<QVariant>& tracks, const collect
m_collectionIndex.insertMulti( collection, plitem );
}
m_collectionRows.insertMulti( collection, crows );
emit endInsertRows();
emit trackCountChanged( rowCount( QModelIndex() ) );

View File

@ -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

View File

@ -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;

View File

@ -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<Tomahawk::result_ptr>& 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;
}
}
}
}

View File

@ -2,7 +2,8 @@
#define PLITEM_H
#include <QHash>
#include <QPersistentModelIndex>
#include <QModelIndex>
#include <QAbstractItemModel>
#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<Tomahawk::result_ptr>& 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