void
TestSingleCollectionTreeItemModel::testRemoveArtist()
{
    Collections::CollectionTestImpl *coll = new Collections::CollectionTestImpl( "test" );
    addMockTrack( coll, "track1", "artist1", "album1" );
    addMockTrack( coll, "track2", "artist2", "album2" );

    QList<CategoryId::CatMenuId> levels;
    levels<< CategoryId::Artist << CategoryId::Album;

    SingleCollectionTreeItemModel *model = new SingleCollectionTreeItemModel( coll, levels );

    loadChildren( model, QModelIndex() );

    QCOMPARE( model->rowCount( QModelIndex() ), 2 );

    {
        QSet<QString> artists;

        QModelIndex idx1 = model->index( 0, 0, QModelIndex() );
        artists << model->data( idx1, Qt::DisplayRole ).toString();

        QModelIndex idx2 = model->index( 1, 0, QModelIndex() );
        artists << model->data( idx2, Qt::DisplayRole ).toString();

        QSet<QString> expected;
        expected << "artist1" << "artist2";
        QCOMPARE( artists, expected );
    }

    ArtistMap map = coll->mc->artistMap();
    map.remove( "artist2" );  //album and track are still part of the collection
    coll->mc->setArtistMap( map );

    model->slotFilter();

    loadChildren( model, QModelIndex() );

    QCOMPARE( model->rowCount( QModelIndex() ), 1 );

    {
        QModelIndex artist1Index = model->index( 0, 0, QModelIndex() );
        QCOMPARE( model->data( artist1Index, Qt::DisplayRole ).toString(), QString( "artist1" ) );
    }

    delete model;
    delete coll;
}
示例#2
0
void
MediaDeviceTrack::setArtist( const QString &newArtist )
{
    if( !m_collection )
        return;

    MediaDeviceArtistPtr artistPtr;
    MediaDeviceTrackPtr track( this );
    ArtistMap artistMap = m_collection.data()->memoryCollection()->artistMap();

    // do cleanup of soon to be previous artist

    artistPtr = m_artist;
    // remove track from previous artist's tracklist
    if ( !artistPtr.isNull() )
    {
        artistPtr->remTrack( track );
        // if artist's tracklist is empty, remove artist from artistmap
        if( artistPtr->tracks().isEmpty() )
            artistMap.remove( artistPtr->name() );
    }

    // change to a new artist

    // check for the existence of the artist to be set to,
    // if artist exists, reuse, else create

    if ( artistMap.contains( newArtist ) )
    {
        artistPtr = MediaDeviceArtistPtr::staticCast( artistMap.value( newArtist ) );
    }
    else
    {
        artistPtr = MediaDeviceArtistPtr( new MediaDeviceArtist( newArtist ) );
        artistMap.insert( newArtist, ArtistPtr::staticCast( artistPtr ) );
    }

    // add track to artist's tracklist
    artistPtr->addTrack( track );
    // set track's artist to the new artist
    setArtist( artistPtr );

    m_collection.data()->memoryCollection()->acquireWriteLock();
    m_collection.data()->memoryCollection()->setArtistMap( artistMap );
    m_collection.data()->memoryCollection()->releaseLock();
}