1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-07-31 11:20:22 +02:00

Cache models so we don't constantly re-fetch and re-resolve everything

This commit is contained in:
Leo Franchi
2011-10-15 19:30:04 -04:00
parent 7cc964dfc5
commit 6606bd4185
2 changed files with 58 additions and 55 deletions

View File

@@ -75,46 +75,28 @@ WhatsHotWidget::WhatsHotWidget( QWidget* parent )
connect( ui->breadCrumbLeft, SIGNAL( currentIndexChanged( QModelIndex ) ), SLOT( leftCrumbIndexChanged(QModelIndex) ) ); connect( ui->breadCrumbLeft, SIGNAL( currentIndexChanged( QModelIndex ) ), SLOT( leftCrumbIndexChanged(QModelIndex) ) );
m_albumsModel = new AlbumModel( ui->additionsView );
ui->additionsView->setAlbumModel( m_albumsModel );
/// Disable sorting, its a ranked list! /// Disable sorting, its a ranked list!
ui->additionsView->proxyModel()->sort( -1 ); ui->additionsView->proxyModel()->sort( -1 );
m_tracksModel = new PlaylistModel( ui->tracksViewLeft );
m_tracksModel->setStyle( TrackModel::ShortWithAvatars );
ui->tracksViewLeft->setFrameShape( QFrame::NoFrame ); ui->tracksViewLeft->setFrameShape( QFrame::NoFrame );
ui->tracksViewLeft->setAttribute( Qt::WA_MacShowFocusRect, 0 ); ui->tracksViewLeft->setAttribute( Qt::WA_MacShowFocusRect, 0 );
ui->tracksViewLeft->overlay()->setEnabled( false ); ui->tracksViewLeft->overlay()->setEnabled( false );
ui->tracksViewLeft->setTrackModel( m_tracksModel );
ui->tracksViewLeft->setHeaderHidden( true ); ui->tracksViewLeft->setHeaderHidden( true );
ui->tracksViewLeft->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); ui->tracksViewLeft->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
m_artistsModel = new TreeModel( this ); TreeProxyModel* artistsProxy = new TreeProxyModel( ui->artistsViewLeft );
m_artistsModel->setColumnStyle( TreeModel::TrackOnly ); artistsProxy->setFilterCaseSensitivity( Qt::CaseInsensitive );
artistsProxy->setDynamicSortFilter( true );
ui->artistsViewLeft->setProxyModel( artistsProxy );
m_artistsProxy = new TreeProxyModel( ui->artistsViewLeft );
m_artistsProxy->setFilterCaseSensitivity( Qt::CaseInsensitive );
m_artistsProxy->setDynamicSortFilter( true );
ui->artistsViewLeft->setProxyModel( m_artistsProxy );
ui->artistsViewLeft->setTreeModel( m_artistsModel );
ui->artistsViewLeft->setFrameShape( QFrame::NoFrame ); ui->artistsViewLeft->setFrameShape( QFrame::NoFrame );
ui->artistsViewLeft->setAttribute( Qt::WA_MacShowFocusRect, 0 ); ui->artistsViewLeft->setAttribute( Qt::WA_MacShowFocusRect, 0 );
artistsProxy->sort( -1 ); // disable sorting, must be called after artistsViewLeft->setTreeModel
m_artistsProxy->sort( -1 ); // disable sorting, must be called after artistsViewLeft->setTreeModel
ui->artistsViewLeft->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); ui->artistsViewLeft->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
ui->artistsViewLeft->header()->setVisible( false ); ui->artistsViewLeft->header()->setVisible( false );
m_timer = new QTimer( this );
connect( m_timer, SIGNAL( timeout() ), SLOT( checkQueries() ) );
connect( Tomahawk::InfoSystem::InfoSystem::instance(), connect( Tomahawk::InfoSystem::InfoSystem::instance(),
SIGNAL( info( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ), SIGNAL( info( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ),
SLOT( infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ) ); SLOT( infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ) );
@@ -147,15 +129,6 @@ WhatsHotWidget::fetchData()
tDebug( LOGVERBOSE ) << "WhatsHot: requested InfoChartCapabilities"; tDebug( LOGVERBOSE ) << "WhatsHot: requested InfoChartCapabilities";
} }
void
WhatsHotWidget::checkQueries()
{
m_timer->stop();
m_tracksModel->ensureResolved();
}
void void
WhatsHotWidget::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestData, QVariant output ) WhatsHotWidget::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestData, QVariant output )
{ {
@@ -205,21 +178,25 @@ WhatsHotWidget::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestDat
tDebug( LOGVERBOSE ) << "WhatsHot: got chart! " << type << " on " << side; tDebug( LOGVERBOSE ) << "WhatsHot: got chart! " << type << " on " << side;
if( type == "artists" ) if( type == "artists" )
{ {
setLeftViewArtists();
const QStringList artists = returnedData["artists"].toStringList(); const QStringList artists = returnedData["artists"].toStringList();
tDebug( LOGVERBOSE ) << "WhatsHot: got artists! " << artists.size(); tDebug( LOGVERBOSE ) << "WhatsHot: got artists! " << artists.size();
m_artistsModel->clear();
TreeModel* artistsModel = new TreeModel( ui->artistsViewLeft );
artistsModel->setColumnStyle( TreeModel::TrackOnly );
foreach ( const QString& artist, artists ) foreach ( const QString& artist, artists )
m_artistsModel->addArtists( Artist::get( artist ) ); artistsModel->addArtists( Artist::get( artist ) );
const QString chartId = requestData.input.value< Tomahawk::InfoSystem::InfoCriteriaHash >().value( "chart_id" );
m_artistModels[ chartId ] = artistsModel;
setLeftViewArtists( artistsModel );
} }
else if( type == "albums" ) else if( type == "albums" )
{ {
setLeftViewAlbums();
m_albumsModel->clear();
QList<album_ptr> al; QList<album_ptr> al;
const QList<Tomahawk::InfoSystem::ArtistAlbumPair> albums = returnedData["albums"].value<QList<Tomahawk::InfoSystem::ArtistAlbumPair> >(); const QList<Tomahawk::InfoSystem::ArtistAlbumPair> albums = returnedData["albums"].value<QList<Tomahawk::InfoSystem::ArtistAlbumPair> >();
tDebug( LOGVERBOSE ) << "WhatsHot: got albums! " << albums.size(); tDebug( LOGVERBOSE ) << "WhatsHot: got albums! " << albums.size();
AlbumModel* albumModel = new AlbumModel( ui->additionsView );
foreach ( const Tomahawk::InfoSystem::ArtistAlbumPair& album, albums ) foreach ( const Tomahawk::InfoSystem::ArtistAlbumPair& album, albums )
{ {
qDebug() << "Getting album" << album.album << "By" << album.artist; qDebug() << "Getting album" << album.album << "By" << album.artist;
@@ -230,20 +207,28 @@ WhatsHotWidget::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestDat
} }
qDebug() << "Adding albums to model"; qDebug() << "Adding albums to model";
m_albumsModel->addAlbums( al ); albumModel->addAlbums( al );
const QString chartId = requestData.input.value< Tomahawk::InfoSystem::InfoCriteriaHash >().value( "chart_id" );
m_albumModels[ chartId ] = albumModel;
setLeftViewAlbums( albumModel );
} }
else if( type == "tracks" ) else if( type == "tracks" )
{ {
setLeftViewTracks();
const QList<Tomahawk::InfoSystem::ArtistTrackPair> tracks = returnedData["tracks"].value<QList<Tomahawk::InfoSystem::ArtistTrackPair> >(); const QList<Tomahawk::InfoSystem::ArtistTrackPair> tracks = returnedData["tracks"].value<QList<Tomahawk::InfoSystem::ArtistTrackPair> >();
tDebug( LOGVERBOSE ) << "WhatsHot: got tracks! " << tracks.size(); tDebug( LOGVERBOSE ) << "WhatsHot: got tracks! " << tracks.size();
m_tracksModel->clear();
PlaylistModel* trackModel = new PlaylistModel( ui->tracksViewLeft );
trackModel->setStyle( TrackModel::Short );
foreach ( const Tomahawk::InfoSystem::ArtistTrackPair& track, tracks ) foreach ( const Tomahawk::InfoSystem::ArtistTrackPair& track, tracks )
{ {
query_ptr query = Query::get( track.artist, track.track, QString(), uuid() ); query_ptr query = Query::get( track.artist, track.track, QString(), uuid() );
m_tracksModel->append( query ); trackModel->append( query );
} }
const QString chartId = requestData.input.value< Tomahawk::InfoSystem::InfoCriteriaHash >().value( "chart_id" );
m_trackModels[ chartId ] = trackModel;
setLeftViewTracks( trackModel );
} }
else else
{ {
@@ -286,6 +271,22 @@ WhatsHotWidget::leftCrumbIndexChanged( QModelIndex index )
const QString chartId = item->data().toString(); const QString chartId = item->data().toString();
if ( m_artistModels.contains( chartId ) )
{
setLeftViewArtists( m_artistModels[ chartId ] );
return;
}
else if ( m_albumModels.contains( chartId ) )
{
setLeftViewAlbums( m_albumModels[ chartId ] );
return;
}
else if ( m_trackModels.contains( chartId ) )
{
setLeftViewTracks( m_trackModels[ chartId ] );
return;
}
Tomahawk::InfoSystem::InfoCriteriaHash criteria; Tomahawk::InfoSystem::InfoCriteriaHash criteria;
criteria.insert( "chart_id", chartId ); criteria.insert( "chart_id", chartId );
/// Remember to lower the source! /// Remember to lower the source!
@@ -369,21 +370,24 @@ WhatsHotWidget::parseNode( QStandardItem* parentItem, const QString &label, cons
void void
WhatsHotWidget::setLeftViewAlbums() WhatsHotWidget::setLeftViewAlbums( AlbumModel* model )
{ {
ui->stackLeft->setCurrentIndex(2); ui->additionsView->setAlbumModel( model );
ui->stackLeft->setCurrentIndex( 2 );
} }
void void
WhatsHotWidget::setLeftViewArtists() WhatsHotWidget::setLeftViewArtists( TreeModel* model )
{ {
ui->stackLeft->setCurrentIndex(1); ui->artistsViewLeft->setTreeModel( model );
ui->stackLeft->setCurrentIndex( 1 );
} }
void void
WhatsHotWidget::setLeftViewTracks() WhatsHotWidget::setLeftViewTracks( PlaylistModel* model )
{ {
ui->stackLeft->setCurrentIndex(0); ui->tracksViewLeft->setPlaylistModel( model );
ui->stackLeft->setCurrentIndex( 0 );
} }

View File

@@ -77,26 +77,25 @@ public slots:
private slots: private slots:
void fetchData(); void fetchData();
void checkQueries();
void infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestData, QVariant output ); void infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestData, QVariant output );
void infoSystemFinished( QString target ); void infoSystemFinished( QString target );
void leftCrumbIndexChanged( QModelIndex ); void leftCrumbIndexChanged( QModelIndex );
private: private:
void setLeftViewArtists(); void setLeftViewArtists( TreeModel* artistModel );
void setLeftViewAlbums(); void setLeftViewAlbums( AlbumModel* albumModel );
void setLeftViewTracks(); void setLeftViewTracks( PlaylistModel* trackModel );
QStandardItem* parseNode( QStandardItem* parentItem, const QString &label, const QVariant &data ); QStandardItem* parseNode( QStandardItem* parentItem, const QString &label, const QVariant &data );
Ui::WhatsHotWidget *ui; Ui::WhatsHotWidget *ui;
PlaylistModel* m_tracksModel;
TreeModel* m_artistsModel;
TreeProxyModel* m_artistsProxy;
QStandardItemModel* m_crumbModelLeft; QStandardItemModel* m_crumbModelLeft;
AlbumModel* m_albumsModel;
// Cache our model data
QHash< QString, AlbumModel* > m_albumModels;
QHash< QString, TreeModel* > m_artistModels;
QHash< QString, PlaylistModel* > m_trackModels;
QTimer* m_timer; QTimer* m_timer;
}; };