1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-20 07:49:42 +01:00

Set two default items for the charts

try listening to any changed and set default source

don't listen to indexchanged, do a manual set when setting default
This commit is contained in:
Leo Franchi 2011-10-22 23:45:21 -04:00
parent 4e8d0d46fe
commit ad739d0ce8
4 changed files with 88 additions and 6 deletions

View File

@ -279,6 +279,7 @@ ChartsPlugin::chartTypes()
// We'll populate charts with the data from the server
QVariantMap charts;
QString chartName;
QStringList defaultChain;
if ( source == "itunes" )
{
// Itunes has geographic-area based charts. So we build a breadcrumb of
@ -292,6 +293,7 @@ ChartsPlugin::chartTypes()
const QString geo = chart.value( "geo" ).toString();
QString name = chart.value( "name" ).toString();
const QString type = chart.value( "type" ).toString();
const bool isDefault = ( chart.contains( "default" ) && chart[ "default" ].toInt() == 1 );
QString country;
if ( !m_cachedCountries.contains( geo ) )
@ -321,10 +323,20 @@ ChartsPlugin::chartTypes()
c[ "id" ] = id;
c[ "label" ] = name;
c[ "type" ] = "album";
if ( isDefault )
c[ "default" ] = "true";
QList<InfoStringHash> countryTypeData = countries[ country ][ type ].value< QList< InfoStringHash > >();
countryTypeData.append( c );
countries[ country ].insert( type, QVariant::fromValue< QList< InfoStringHash > >( countryTypeData ) );
if ( isDefault )
{
defaultChain.clear();
defaultChain.append( country );
defaultChain.append( type );
defaultChain.append( name );
}
}
foreach( const QString& c, countries.keys() )
@ -344,9 +356,14 @@ ChartsPlugin::chartTypes()
{
const QVariantMap chart = chartObj.toMap();
const QString type = chart.value( "type" ).toString();
const bool isDefault = ( chart.contains( "default" ) && chart[ "default" ].toInt() == 1 );
InfoStringHash c;
c[ "id" ] = chart.value( "id" ).toString();
c[ "label" ] = chart.value( "name" ).toString();
if ( isDefault )
c[ "default" ] = "true";
if ( type == "Album" )
{
c[ "type" ] = "album";
@ -357,6 +374,13 @@ ChartsPlugin::chartTypes()
c[ "type" ] = "tracks";
trackCharts.append( c );
}
if ( isDefault )
{
defaultChain.clear();
defaultChain.append( type + "s" ); //UGLY but it's plural to the user, see below
defaultChain.append( c[ "label" ] );
}
}
charts.insert( tr( "Albums" ), QVariant::fromValue< QList< InfoStringHash > >( albumCharts ) );
charts.insert( tr( "Tracks" ), QVariant::fromValue< QList< InfoStringHash > >( trackCharts ) );
@ -369,6 +393,10 @@ ChartsPlugin::chartTypes()
/// Add the possible charts and its types to breadcrumb
// qDebug() << "ADDING CHART TYPE TO CHARTS:" << chartName;
QVariantMap defaultMap = m_allChartsMap.value( "defaults" ).value< QVariantMap >();
defaultMap[ source ] = defaultChain;
m_allChartsMap[ "defaults" ] = defaultMap;
m_allChartsMap[ "defaultSource" ] = "itunes";
m_allChartsMap.insert( chartName , QVariant::fromValue< QVariantMap >( charts ) );
}

View File

@ -27,6 +27,7 @@
#include <QPainter>
#include <QStyle>
#include <QStyleOption>
#include "whatshotwidget.h"
BreadcrumbButtonBase* SiblingCrumbButtonFactory::newButton(QModelIndex index, BreadcrumbBar *parent)
{
@ -129,10 +130,16 @@ void SiblingCrumbButton::fillCombo()
{
QStringList list;
int count = breadcrumbBar()->model()->rowCount(m_index.parent());
for(int i = 0; i < count; ++i) {
int defaultIndex = -1;
for ( int i = 0; i < count; ++i )
{
QModelIndex sibling = m_index.sibling(i,0);
if( sibling.isValid() )
if ( sibling.isValid() )
{
list << sibling.data().toString();
if ( sibling.data( WhatsHotWidget::DefaultRole ).toBool() )
defaultIndex = i;
}
}
if ( m_combo->count() && list.count() )
@ -148,7 +155,13 @@ void SiblingCrumbButton::fillCombo()
m_combo->clear();
m_combo->addItems(list);
m_combo->setCurrentIndex( m_combo->findText(text()));
if ( defaultIndex == -1 )
m_combo->setCurrentIndex( m_combo->findText(text()));
else
{
m_combo->setCurrentIndex( defaultIndex );
comboboxActivated( defaultIndex );
}
m_combo->adjustSize();
}

View File

@ -171,7 +171,12 @@ WhatsHotWidget::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestDat
QStandardItem *rootItem= m_crumbModelLeft->invisibleRootItem();
tDebug( LOGVERBOSE ) << "WhatsHot:: " << returnedData.keys();
foreach( const QString label, returnedData.keys() )
QVariantMap defaults;
if ( returnedData.contains( "defaults" ) )
defaults = returnedData.take( "defaults" ).toMap();
QString defaultSource = returnedData.take( "defaultSource" ).toString();
foreach ( const QString label, returnedData.keys() )
{
tDebug( LOGVERBOSE ) << "WhatsHot:: parsing " << label;
QStandardItem *childItem = parseNode( rootItem, label, returnedData[label] );
@ -179,9 +184,40 @@ WhatsHotWidget::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestDat
rootItem->appendRow(childItem);
}
// Set the default source
// Set the default chart for each source
for ( int i = 0; i < rootItem->rowCount(); i++ )
{
QStandardItem* source = rootItem->child( i, 0 );
if ( defaultSource.toLower() == source->text().toLower() )
{
source->setData( true, DefaultRole );
}
if ( defaults.contains( source->text().toLower() ) )
{
QStringList defaultIndices = defaults[ source->text().toLower() ].toStringList();
QStandardItem* cur = source;
foreach( const QString& index, defaultIndices )
{
// Go through the children of the current item, marking the default one as default
for ( int k = 0; k < cur->rowCount(); k++ )
{
if ( cur->child( k, 0 )->text() == index )
{
// tDebug() << "Found DEFAULT ITEM:" << index;
cur = cur->child( k, 0 ); // this is the default, drill down into the default to pick the next default
cur->setData( true, DefaultRole );
break;
}
}
}
}
}
KBreadcrumbSelectionModel *selectionModelLeft = new KBreadcrumbSelectionModel(new QItemSelectionModel(m_crumbModelLeft, this), this);
ui->breadCrumbLeft->setSelectionModel(selectionModelLeft);
//ui->breadCrumbRight->setSelectionModel(selectionModelLeft);
break;
}
@ -368,6 +404,8 @@ WhatsHotWidget::parseNode( QStandardItem* parentItem, const QString &label, cons
{
QStandardItem *childItem= new QStandardItem( chart[ "label" ] );
childItem->setData( chart[ "id" ] );
if ( chart.value( "default", "" ) == "true")
sourceItem->setData( WhatsHotWidget::DefaultRole, true );
sourceItem->appendRow( childItem );
}
}
@ -386,7 +424,6 @@ WhatsHotWidget::parseNode( QStandardItem* parentItem, const QString &label, cons
foreach ( const QVariant value, dataList )
{
qDebug() << "CREATED:" << value.toString();
QStandardItem *childItem= new QStandardItem(value.toString());
sourceItem->appendRow(childItem);
}

View File

@ -53,6 +53,10 @@ class DLLEXPORT WhatsHotWidget : public QWidget, public Tomahawk::ViewPage
Q_OBJECT
public:
enum ExtraRoles {
DefaultRole = Qt::UserRole + 10
};
WhatsHotWidget( QWidget* parent = 0 );
~WhatsHotWidget();