Meta::TrackList APG::ConstraintSolver::sample( Meta::TrackList domain, const int sampleSize ) const { std::random_shuffle( domain.begin(), domain.end() ); return domain.mid( 0, sampleSize ); }
void Albums::dataUpdated( const QString &name, const Plasma::DataEngine::Data &data ) { if( name != QLatin1String("albums") ) return; Meta::AlbumList albums = data[ "albums" ].value<Meta::AlbumList>(); Meta::TrackPtr track = data[ "currentTrack" ].value<Meta::TrackPtr>(); QString headerText = data[ "headerText" ].toString(); setHeaderText( headerText.isEmpty() ? i18n("Albums") : headerText ); //Don't keep showing the albums for the artist of the last track that had album in the collection if( (m_currentTrack == track) && (m_albums == albums) ) return; if( albums.isEmpty() ) { debug() << "received albums is empty"; setCollapseOn(); m_albums.clear(); m_albumsView->clear(); return; } setCollapseOff(); m_albums = albums; m_currentTrack = track; m_albumsView->clear(); m_albumsView->setMode( track ? AlbumsProxyModel::SortByYear : AlbumsProxyModel::SortByCreateDate ); QStandardItem *currentItem( 0 ); foreach( Meta::AlbumPtr albumPtr, albums ) { // do not show all tracks without an album from the collection, this takes ages // TODO: show all tracks from this artist that are not part of an album if( albumPtr->name().isEmpty() ) continue; Meta::TrackList tracks = albumPtr->tracks(); if( tracks.isEmpty() ) continue; AlbumItem *albumItem = new AlbumItem(); albumItem->setIconSize( 50 ); albumItem->setAlbum( albumPtr ); albumItem->setShowArtist( !m_currentTrack ); int numberOfDiscs = 0; int childRow = 0; qStableSort( tracks.begin(), tracks.end(), Meta::Track::lessThan ); QMultiHash< int, TrackItem* > trackItems; // hash of tracks items for each disc foreach( Meta::TrackPtr trackPtr, tracks ) { if( numberOfDiscs < trackPtr->discNumber() ) numberOfDiscs = trackPtr->discNumber(); TrackItem *trackItem = new TrackItem(); trackItem->setTrack( trackPtr ); // bold the current track to make it more visible if( m_currentTrack && *m_currentTrack == *trackPtr ) { currentItem = trackItem; trackItem->bold(); } // If compilation and same artist, then highlight, but only if there's a current track if( m_currentTrack && m_currentTrack->artist() && trackPtr->artist() && (*m_currentTrack->artist() == *trackPtr->artist()) && albumPtr->isCompilation() ) { trackItem->italicise(); } trackItems.insert( trackPtr->discNumber(), trackItem ); } for( int i = 0; i <= numberOfDiscs; ++i ) { QList<TrackItem*> items = trackItems.values( i ); if( !items.isEmpty() ) { const TrackItem *item = items.first(); QStandardItem *discItem( 0 ); if( numberOfDiscs > 1 ) { discItem = new QStandardItem( i18n("Disc %1", item->track()->discNumber()) ); albumItem->setChild( childRow++, discItem ); int discChildRow = 0; foreach( TrackItem *trackItem, items ) discItem->setChild( discChildRow++, trackItem ); } else { foreach( TrackItem *trackItem, items ) albumItem->setChild( childRow++, trackItem ); } } } m_albumsView->appendAlbum( albumItem ); } m_albumsView->sort(); if( currentItem ) { m_albumsView->setRecursiveExpanded( currentItem, true ); m_albumsView->scrollTo( currentItem ); } updateConstraints(); }