1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-23 17:29:42 +01:00

* Moved style/view specific model stuff into PlayableProxyModel.

This commit is contained in:
Christian Muehlhaeuser 2012-07-02 23:08:07 +02:00
parent 27df8fd3dc
commit 1860d7732a
4 changed files with 143 additions and 119 deletions

View File

@ -41,7 +41,6 @@ PlayableModel::PlayableModel( QObject* parent, bool loading )
: QAbstractItemModel( parent )
, m_rootItem( new PlayableItem( 0, this ) )
, m_readOnly( true )
, m_style( Detailed )
, m_loading( loading )
{
connect( AudioEngine::instance(), SIGNAL( started( Tomahawk::result_ptr ) ), SLOT( onPlaybackStarted( Tomahawk::result_ptr ) ), Qt::DirectConnection );
@ -49,9 +48,6 @@ PlayableModel::PlayableModel( QObject* parent, bool loading )
m_header << tr( "Artist" ) << tr( "Title" ) << tr( "Composer" ) << tr( "Album" ) << tr( "Track" ) << tr( "Duration" )
<< tr( "Bitrate" ) << tr( "Age" ) << tr( "Year" ) << tr( "Size" ) << tr( "Origin" ) << tr( "Score" ) << tr( "Name" );
m_headerStyle[ Detailed ] << Artist << Track << Composer << Album << AlbumPos << Duration << Bitrate << Age << Year << Filesize << Origin << Score;
m_headerStyle[ Collection ] << Name << Composer << Duration << Bitrate << Age << Year << Filesize << Origin;
}
@ -94,23 +90,7 @@ PlayableModel::columnCount( const QModelIndex& parent ) const
{
Q_UNUSED( parent );
switch ( m_style )
{
case Short:
case ShortWithAvatars:
case Large:
return 1;
break;
case Collection:
return 8;
break;
case Detailed:
default:
return 12;
break;
}
return 12;
}
@ -183,10 +163,7 @@ PlayableModel::queryData( const query_ptr& query, int column, int role ) const
if ( role != Qt::DisplayRole ) // && role != Qt::ToolTipRole )
return QVariant();
if ( !m_headerStyle.contains( m_style ) )
return query->track();
switch( m_headerStyle[ m_style ].at( column ) )
switch ( column )
{
case Artist:
return query->artist();
@ -229,7 +206,7 @@ PlayableModel::queryData( const query_ptr& query, int column, int role ) const
}
if ( query->numResults() )
{
switch( m_headerStyle[ m_style ].at( column ) )
switch ( column )
{
case Bitrate:
if ( query->results().first()->bitrate() > 0 )
@ -283,16 +260,6 @@ PlayableModel::data( const QModelIndex& index, int role ) const
return QVariant( columnAlignment( index.column() ) );
}
if ( role == StyleRole )
{
return m_style;
}
if ( role == Qt::SizeHintRole && !m_itemSize.isEmpty() )
{
return m_itemSize;
}
if ( !entry->query().isNull() )
{
return queryData( entry->query()->displayQuery(), index.column(), role );
@ -317,10 +284,8 @@ PlayableModel::headerData( int section, Qt::Orientation orientation, int role )
if ( role == Qt::DisplayRole && section >= 0 )
{
if ( m_headerStyle.contains( m_style ) )
{
return m_header.at( m_headerStyle[ m_style ].at( section ) );
}
if ( section < m_header.count() )
return m_header.at( section );
else
return tr( "Name" );
}
@ -334,28 +299,6 @@ PlayableModel::headerData( int section, Qt::Orientation orientation, int role )
}
void
PlayableModel::updateDetailedInfo( const QModelIndex& index )
{
if ( style() != PlayableModel::Short && style() != PlayableModel::Large )
return;
PlayableItem* item = itemFromIndex( index );
if ( item->query().isNull() )
return;
if ( style() == PlayableModel::Short || style() == PlayableModel::Large )
{
item->query()->cover( QSize( 0, 0 ) );
}
if ( style() == PlayableModel::Large )
{
item->query()->loadSocialActions();
}
}
void
PlayableModel::setCurrentItem( const QModelIndex& index )
{
@ -717,47 +660,10 @@ PlayableModel::ensureResolved()
}
void
PlayableModel::setStyle( PlayableModel::PlayableItemStyle style )
{
m_style = style;
}
QList< double >
PlayableModel::columnWeights() const
{
QList< double > w;
switch ( m_style )
{
case Short:
case ShortWithAvatars:
case Large:
w << 1.0;
break;
case Collection:
w << 0.42 << 0.12 << 0.07 << 0.07 << 0.07 << 0.07 << 0.07; // << 0.11;
break;
case Detailed:
default:
w << 0.16 << 0.16 << 0.14 << 0.12 << 0.05 << 0.05 << 0.05 << 0.05 << 0.05 << 0.05 << 0.09; // << 0.03;
break;
}
return w;
}
Qt::Alignment
PlayableModel::columnAlignment( int column ) const
{
if ( !m_headerStyle.contains( m_style ) )
return Qt::AlignLeft;
switch( m_headerStyle[ m_style ].at( column ) )
switch ( column )
{
case Age:
case AlbumPos:

View File

@ -38,12 +38,6 @@ class DLLEXPORT PlayableModel : public QAbstractItemModel
Q_OBJECT
public:
enum PlayableItemStyle
{ Detailed = 0, Short = 1, ShortWithAvatars = 2, Large = 3, Collection = 4 };
enum PlayableModelRole
{ StyleRole = Qt::UserRole + 1 };
enum Columns {
Artist = 0,
Track = 1,
@ -63,9 +57,6 @@ public:
explicit PlayableModel( QObject* parent = 0, bool loading = true );
virtual ~PlayableModel();
PlayableModel::PlayableItemStyle style() const { return m_style; }
void setStyle( PlayableModel::PlayableItemStyle style );
virtual QModelIndex index( int row, int column, const QModelIndex& parent ) const;
virtual QModelIndex parent( const QModelIndex& child ) const;
@ -87,8 +78,6 @@ public:
virtual int columnCount( const QModelIndex& parent = QModelIndex() ) const;
virtual bool hasChildren( const QModelIndex& parent ) const;
QList< double > columnWeights() const;
virtual QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const;
virtual QVariant headerData( int section, Qt::Orientation orientation, int role ) const;
@ -113,11 +102,6 @@ public:
/// Returns a flat list of all tracks in this model
QList< Tomahawk::query_ptr > queries() const;
void updateDetailedInfo( const QModelIndex& index );
QSize itemSize() const { return m_itemSize; }
void setItemSize( const QSize& size ) { m_itemSize = size; }
void startLoading();
void finishLoading();
@ -176,7 +160,6 @@ private:
PlayableItem* m_rootItem;
QPersistentModelIndex m_currentIndex;
Tomahawk::QID m_currentUuid;
QSize m_itemSize;
bool m_readOnly;
@ -184,10 +167,8 @@ private:
QString m_description;
QPixmap m_icon;
QHash< PlayableItemStyle, QList<Columns> > m_headerStyle;
QStringList m_header;
PlayableItemStyle m_style;
bool m_loading;
};

View File

@ -36,12 +36,16 @@ PlayableProxyModel::PlayableProxyModel( QObject* parent )
, m_showOfflineResults( true )
, m_hideDupeItems( false )
, m_maxVisibleItems( -1 )
, m_style( Detailed )
{
setFilterCaseSensitivity( Qt::CaseInsensitive );
setSortCaseSensitivity( Qt::CaseInsensitive );
setDynamicSortFilter( true );
setSourcePlayableModel( 0 );
m_headerStyle[ Detailed ] << PlayableModel::Artist << PlayableModel::Track << PlayableModel::Composer << PlayableModel::Album << PlayableModel::AlbumPos << PlayableModel::Duration << PlayableModel::Bitrate << PlayableModel::Age << PlayableModel::Year << PlayableModel::Filesize << PlayableModel::Origin << PlayableModel::Score;
m_headerStyle[ Collection ] << PlayableModel::Name << PlayableModel::Composer << PlayableModel::Duration << PlayableModel::Bitrate << PlayableModel::Age << PlayableModel::Year << PlayableModel::Filesize << PlayableModel::Origin;
}
@ -461,3 +465,116 @@ PlayableProxyModel::playlistInterface()
return m_playlistInterface;
}
int
PlayableProxyModel::columnCount( const QModelIndex& parent ) const
{
Q_UNUSED( parent );
switch ( m_style )
{
case Short:
case ShortWithAvatars:
case Large:
return 1;
break;
case Collection:
return 8;
break;
case Detailed:
default:
return 12;
break;
}
}
QVariant
PlayableProxyModel::data( const QModelIndex& index, int role ) const
{
if ( role == StyleRole )
{
return m_style;
}
if ( !sourceModel() )
return QVariant();
if ( !m_headerStyle.contains( m_style ) )
return QVariant();
PlayableModel::Columns col = m_headerStyle[ m_style ].at( index.column() );
QModelIndex sourceIdx = mapToSource( index );
QModelIndex idx = sourceModel()->index( sourceIdx.row(), (int)col, sourceIdx.parent() );
return idx.data( role );
}
QVariant
PlayableProxyModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
if ( !sourceModel() )
return QVariant();
if ( !m_headerStyle.contains( m_style ) )
return QVariant();
if ( section < m_headerStyle[ m_style ].count() )
{
PlayableModel::Columns col = m_headerStyle[ m_style ].at( section );
return sourceModel()->headerData( (int)col, orientation, role );
}
else
return sourceModel()->headerData( 255, orientation, role );
}
QList< double >
PlayableProxyModel::columnWeights() const
{
QList< double > w;
switch ( m_style )
{
case Short:
case ShortWithAvatars:
case Large:
w << 1.0;
break;
case Collection:
w << 0.42 << 0.12 << 0.07 << 0.07 << 0.07 << 0.07 << 0.07; // << 0.11;
break;
case Detailed:
default:
w << 0.16 << 0.16 << 0.14 << 0.12 << 0.05 << 0.05 << 0.05 << 0.05 << 0.05 << 0.05 << 0.09; // << 0.03;
break;
}
return w;
}
void
PlayableProxyModel::updateDetailedInfo( const QModelIndex& index )
{
if ( style() != PlayableProxyModel::Short && style() != PlayableProxyModel::Large )
return;
PlayableItem* item = itemFromIndex( mapToSource( index ) );
if ( item->query().isNull() )
return;
if ( style() == PlayableProxyModel::Short || style() == PlayableProxyModel::Large )
{
item->query()->cover( QSize( 0, 0 ) );
}
if ( style() == PlayableProxyModel::Large )
{
item->query()->loadSocialActions();
}
}

View File

@ -32,6 +32,12 @@ class DLLEXPORT PlayableProxyModel : public QSortFilterProxyModel
Q_OBJECT
public:
enum PlayableItemStyle
{ Detailed = 0, Short = 1, ShortWithAvatars = 2, Large = 3, Collection = 4 };
enum PlayableProxyModelRole
{ StyleRole = Qt::UserRole + 1 };
explicit PlayableProxyModel ( QObject* parent = 0 );
virtual ~PlayableProxyModel() {}
@ -41,6 +47,9 @@ public:
virtual bool isLoading() const;
PlayableProxyModel::PlayableItemStyle style() const { return m_style; }
void setStyle( PlayableProxyModel::PlayableItemStyle style ) { m_style = style; }
virtual QPersistentModelIndex currentIndex() const { return mapFromSource( m_model->currentItem() ); }
virtual void setCurrentIndex( const QModelIndex& index ) { m_model->setCurrentItem( mapToSource( index ) ); }
@ -63,6 +72,14 @@ public:
virtual Tomahawk::playlistinterface_ptr playlistInterface();
QList< double > columnWeights() const;
virtual int columnCount( const QModelIndex& parent = QModelIndex() ) const;
virtual QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const;
virtual QVariant headerData( int section, Qt::Orientation orientation, int role ) const;
void updateDetailedInfo( const QModelIndex& index );
signals:
void filterChanged( const QString& filter );
@ -86,6 +103,9 @@ private:
bool m_showOfflineResults;
bool m_hideDupeItems;
int m_maxVisibleItems;
QHash< PlayableItemStyle, QList<PlayableModel::Columns> > m_headerStyle;
PlayableItemStyle m_style;
};
#endif // TRACKPROXYMODEL_H