1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-05 21:57:41 +02:00

Properly sort

This commit is contained in:
Leo Franchi
2011-10-30 20:58:58 -04:00
parent ca0166d8fd
commit 65779b18eb
3 changed files with 11 additions and 14 deletions

View File

@@ -102,10 +102,6 @@ BreadcrumbButton::sizeHint() const
return m_combo->sizeHint() + QSize( padding, 0 ); return m_combo->sizeHint() + QSize( padding, 0 );
} }
bool caseInsensitiveLessThan( const QString &s1, const QString &s2 )
{
return s1.toLower() < s2.toLower();
}
void void
BreadcrumbButton::setParentIndex( const QModelIndex& idx ) BreadcrumbButton::setParentIndex( const QModelIndex& idx )
@@ -116,19 +112,12 @@ BreadcrumbButton::setParentIndex( const QModelIndex& idx )
QStringList list; QStringList list;
int count = m_model->rowCount( m_parentIndex ); int count = m_model->rowCount( m_parentIndex );
int defaultIndex = -1, userSelected = -1; int defaultIndex = -1, userSelected = -1;
// Two-pass so we can sort the list first
for ( int i = 0; i < count; ++i )
{
list << m_model->index( i, 0, m_parentIndex ).data().toString();
}
qSort( list.begin(), list.end(), caseInsensitiveLessThan );
for ( int i = 0; i < count; ++i ) for ( int i = 0; i < count; ++i )
{ {
QModelIndex idx = m_model->index( i, 0, m_parentIndex ); QModelIndex idx = m_model->index( i, 0, m_parentIndex );
if ( idx.isValid() ) if ( idx.isValid() )
{ {
list << idx.data().toString();
if ( idx.data( Breadcrumb::DefaultRole ).toBool() ) if ( idx.data( Breadcrumb::DefaultRole ).toBool() )
defaultIndex = i; defaultIndex = i;
if ( idx.data( Breadcrumb::UserSelectedRole ).toBool() ) if ( idx.data( Breadcrumb::UserSelectedRole ).toBool() )

View File

@@ -50,6 +50,7 @@ static QString s_whatsHotIdentifier = QString( "WhatsHotWidget" );
WhatsHotWidget::WhatsHotWidget( QWidget* parent ) WhatsHotWidget::WhatsHotWidget( QWidget* parent )
: QWidget( parent ) : QWidget( parent )
, ui( new Ui::WhatsHotWidget ) , ui( new Ui::WhatsHotWidget )
, m_sortedProxy( 0 )
{ {
ui->setupUi( this ); ui->setupUi( this );
@@ -64,6 +65,9 @@ WhatsHotWidget::WhatsHotWidget( QWidget* parent )
TomahawkUtils::unmarginLayout( ui->verticalLayout->layout() ); TomahawkUtils::unmarginLayout( ui->verticalLayout->layout() );
m_crumbModelLeft = new QStandardItemModel( this ); m_crumbModelLeft = new QStandardItemModel( this );
m_sortedProxy = new QSortFilterProxyModel( this );
m_sortedProxy->setDynamicSortFilter( true );
m_sortedProxy->setFilterCaseSensitivity( Qt::CaseInsensitive );
ui->breadCrumbLeft->setRootIcon( QPixmap( RESPATH "images/charts.png" ) ); ui->breadCrumbLeft->setRootIcon( QPixmap( RESPATH "images/charts.png" ) );
@@ -209,7 +213,9 @@ WhatsHotWidget::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestDat
} }
} }
ui->breadCrumbLeft->setModel( m_crumbModelLeft ); m_sortedProxy->setSourceModel( m_crumbModelLeft );
m_sortedProxy->sort( 0, Qt::AscendingOrder );
ui->breadCrumbLeft->setModel( m_sortedProxy );
break; break;
} }
case InfoSystem::InfoChart: case InfoSystem::InfoChart:
@@ -310,7 +316,7 @@ void
WhatsHotWidget::leftCrumbIndexChanged( QModelIndex index ) WhatsHotWidget::leftCrumbIndexChanged( QModelIndex index )
{ {
tDebug( LOGVERBOSE ) << "WhatsHot:: left crumb changed" << index.data(); tDebug( LOGVERBOSE ) << "WhatsHot:: left crumb changed" << index.data();
QStandardItem* item = m_crumbModelLeft->itemFromIndex( index ); QStandardItem* item = m_crumbModelLeft->itemFromIndex( m_sortedProxy->mapToSource( index ) );
if( !item ) if( !item )
return; return;
if( !item->data( Breadcrumb::ChartIdRole ).isValid() ) if( !item->data( Breadcrumb::ChartIdRole ).isValid() )

View File

@@ -31,6 +31,7 @@
#include "dllmacro.h" #include "dllmacro.h"
class QSortFilterProxyModel;
class QStandardItemModel; class QStandardItemModel;
class QStandardItem; class QStandardItem;
class TreeModel; class TreeModel;
@@ -91,6 +92,7 @@ private:
Ui::WhatsHotWidget *ui; Ui::WhatsHotWidget *ui;
QStandardItemModel* m_crumbModelLeft; QStandardItemModel* m_crumbModelLeft;
QSortFilterProxyModel* m_sortedProxy;
// Cache our model data // Cache our model data
QHash< QString, AlbumModel* > m_albumModels; QHash< QString, AlbumModel* > m_albumModels;