1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-04-13 20:41:58 +02:00

refactor the generator a bit to share code

update the data in the echonest control when changing type
This commit is contained in:
Leo Franchi 2011-01-25 18:55:15 -05:00
parent c55afebff0
commit f78874f671
5 changed files with 45 additions and 37 deletions

View File

@ -60,6 +60,8 @@ Tomahawk::EchonestControl::setSelectedType ( const QString& type )
Tomahawk::DynamicControl::setSelectedType ( type );
updateWidgets();
updateData();
qDebug() << "Setting new type, set data to:" << m_data.first << m_data.second;
}
}

View File

@ -76,6 +76,11 @@ void
EchonestGenerator::generate ( int number )
{
// convert to an echonest query, and fire it off
qDebug() << Q_FUNC_INFO;
qDebug() << "Generating playlist with " << m_controls.size();
foreach( const dyncontrol_ptr& ctrl, m_controls )
qDebug() << ctrl->selectedType() << ctrl->match() << ctrl->input();
try {
Echonest::DynamicPlaylist::PlaylistParams params = getParams();
@ -153,7 +158,7 @@ EchonestGenerator::getParams() const throw( std::runtime_error )
foreach( const dyncontrol_ptr& control, m_controls ) {
params.append( control.dynamicCast<EchonestControl>()->toENParam() );
}
params.append( Echonest::DynamicPlaylist::PlaylistParamData( Echonest::DynamicPlaylist::Type, determineRadioType() ) );
appendRadioType( params );
return params;
}
@ -193,50 +198,46 @@ EchonestGenerator::dynamicFetched()
}
}
bool
EchonestGenerator::onlyThisArtistType( Echonest::DynamicPlaylist::ArtistTypeEnum type ) const throw( std::runtime_error )
{
bool only = true;
bool some = false;
foreach( const dyncontrol_ptr& control, m_controls ) {
if( control->selectedType() == "Artist" && static_cast<Echonest::DynamicPlaylist::ArtistTypeEnum>( control->match().toInt() ) != type ) {
only = false;
} else if( control->selectedType() == "Artist" && static_cast<Echonest::DynamicPlaylist::ArtistTypeEnum>( control->match().toInt() ) == type ) {
some = true;
}
}
if( some && only ) {
return true;
} else if( some && !only ) {
throw std::runtime_error( "All artist match types must be the same" );
}
return false;
}
Echonest::DynamicPlaylist::ArtistTypeEnum
EchonestGenerator::determineRadioType() const throw( std::runtime_error )
void
EchonestGenerator::appendRadioType( Echonest::DynamicPlaylist::PlaylistParams& params ) const throw( std::runtime_error )
{
/**
* so we try to match the best type of echonest playlist, based on the controls
* the types are artist, artist-radio, artist-description, catalog, catalog-radio, song-radio. we don't care about the catalog ones.
*
* Fallback is artist-radio
*/
/// 1. artist: If all the artist controls are Limit-To. If some were but not all, error out.
bool artistOnly = true;
bool someArtist = false;
foreach( const dyncontrol_ptr& control, m_controls ) {
if( control->selectedType() == "Artist" && static_cast<Echonest::DynamicPlaylist::ArtistTypeEnum>( control->match().toInt() ) != Echonest::DynamicPlaylist::ArtistType ) {
artistOnly = false;
} else if( control->selectedType() == "Artist" && static_cast<Echonest::DynamicPlaylist::ArtistTypeEnum>( control->match().toInt() ) == Echonest::DynamicPlaylist::ArtistType ) {
someArtist = true;
}
}
if( someArtist && artistOnly ) {
return Echonest::DynamicPlaylist::ArtistType;
} else if( someArtist && !artistOnly ) {
throw std::runtime_error( "All artist match types must be the same" );
}
/// 2. artist-description: If all the artist entries are Description. If some were but not all, error out.
bool artistDescOnly = true;
bool someArtistDescFound = false;
foreach( const dyncontrol_ptr& control, m_controls ) {
if( control->selectedType() == "Artist" && static_cast<Echonest::DynamicPlaylist::ArtistTypeEnum>( control->match().toInt() ) == Echonest::DynamicPlaylist::ArtistDescriptionType ) {
someArtistDescFound = true;
} else if( control->selectedType() == "Artist" && static_cast<Echonest::DynamicPlaylist::ArtistTypeEnum>( control->match().toInt() ) != Echonest::DynamicPlaylist::ArtistDescriptionType ) {
artistDescOnly = false;
}
}
if( someArtistDescFound && artistDescOnly ) {
return Echonest::DynamicPlaylist::ArtistDescriptionType;
} else if( someArtistDescFound && !artistDescOnly ) // fail, must be all artist desc
throw std::runtime_error( "All artist match types must be the same" );
return Echonest::DynamicPlaylist::ArtistRadioType;
/// 3. Artist-Radio: If all the artist entries are Similar To. If some were but not all, error out.
if( onlyThisArtistType( Echonest::DynamicPlaylist::ArtistType ) )
params.append( Echonest::DynamicPlaylist::PlaylistParamData( Echonest::DynamicPlaylist::Type, Echonest::DynamicPlaylist::ArtistType ) );
else if( onlyThisArtistType( Echonest::DynamicPlaylist::ArtistDescriptionType ) )
params.append( Echonest::DynamicPlaylist::PlaylistParamData( Echonest::DynamicPlaylist::Type, Echonest::DynamicPlaylist::ArtistDescriptionType ) );
else if( onlyThisArtistType( Echonest::DynamicPlaylist::ArtistRadioType ) )
params.append( Echonest::DynamicPlaylist::PlaylistParamData( Echonest::DynamicPlaylist::Type, Echonest::DynamicPlaylist::ArtistRadioType ) );
}
query_ptr

View File

@ -58,7 +58,8 @@ private slots:
private:
Echonest::DynamicPlaylist::PlaylistParams getParams() const throw( std::runtime_error );
query_ptr queryFromSong( const Echonest::Song& song );
Echonest::DynamicPlaylist::ArtistTypeEnum determineRadioType() const throw( std::runtime_error );
void appendRadioType( Echonest::DynamicPlaylist::PlaylistParams& params ) const throw( std::runtime_error );
bool onlyThisArtistType( Echonest::DynamicPlaylist::ArtistTypeEnum type ) const throw( std::runtime_error );
Echonest::DynamicPlaylist* m_dynPlaylist;
QPixmap m_logo;

View File

@ -63,7 +63,7 @@ DynamicControlList::init()
setLayout( m_layout );
m_layout->setColumnStretch( 2, 1 );
m_layout->setMargin( 0 );
m_layout->setSpacing( 0 );
m_layout->setVerticalSpacing( 0 );
m_layout->setContentsMargins( 0, 0, 0, 0 );
m_layout->setSizeConstraint( QLayout::SetMinimumSize );

View File

@ -114,6 +114,10 @@ DynamicWidget::loadDynamicPlaylist( const Tomahawk::dynplaylist_ptr& playlist )
if( !m_playlist.isNull() && ( m_playlist.data() == playlist.data() ) // same playlist pointer
&& m_playlist->generator()->controls().size() == playlist->generator()->controls().size() ) {
// we can skip our work. just let the dynamiccontrollist show the difference
qDebug() << "SKIPPING SETTING:" << playlist->generator()->controls().size();
foreach( const dyncontrol_ptr& control, playlist->generator()->controls() ) {
qDebug() << "CONTROL:" << control->selectedType() << control->match() << control->input();
}
m_controls->setControls( m_playlist->generator(), m_playlist->generator()->controls(), m_playlist->author()->isLocal() );
m_playlist = playlist;