mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-03-21 00:09:47 +01:00
* AlbumModel now inherits from PlayableModel.
This commit is contained in:
parent
811cd200d0
commit
bc982f0ca5
@ -35,8 +35,7 @@ using namespace Tomahawk;
|
||||
|
||||
|
||||
AlbumModel::AlbumModel( QObject* parent )
|
||||
: QAbstractItemModel( parent )
|
||||
, m_rootItem( new PlayableItem( 0, this ) )
|
||||
: PlayableModel( parent )
|
||||
, m_overwriteOnAdd( false )
|
||||
{
|
||||
}
|
||||
@ -44,197 +43,6 @@ AlbumModel::AlbumModel( QObject* parent )
|
||||
|
||||
AlbumModel::~AlbumModel()
|
||||
{
|
||||
delete m_rootItem;
|
||||
}
|
||||
|
||||
|
||||
QModelIndex
|
||||
AlbumModel::index( int row, int column, const QModelIndex& parent ) const
|
||||
{
|
||||
if ( !m_rootItem || row < 0 || column < 0 )
|
||||
return QModelIndex();
|
||||
|
||||
PlayableItem* parentItem = itemFromIndex( parent );
|
||||
PlayableItem* childItem = parentItem->children.value( row );
|
||||
if ( !childItem )
|
||||
return QModelIndex();
|
||||
|
||||
return createIndex( row, column, childItem );
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
AlbumModel::rowCount( const QModelIndex& parent ) const
|
||||
{
|
||||
if ( parent.column() > 0 )
|
||||
return 0;
|
||||
|
||||
PlayableItem* parentItem = itemFromIndex( parent );
|
||||
if ( !parentItem )
|
||||
return 0;
|
||||
|
||||
return parentItem->children.count();
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
AlbumModel::columnCount( const QModelIndex& parent ) const
|
||||
{
|
||||
Q_UNUSED( parent );
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
QModelIndex
|
||||
AlbumModel::parent( const QModelIndex& child ) const
|
||||
{
|
||||
PlayableItem* entry = itemFromIndex( child );
|
||||
if ( !entry )
|
||||
return QModelIndex();
|
||||
|
||||
PlayableItem* parentEntry = entry->parent();
|
||||
if ( !parentEntry )
|
||||
return QModelIndex();
|
||||
|
||||
PlayableItem* grandparentEntry = parentEntry->parent();
|
||||
if ( !grandparentEntry )
|
||||
return QModelIndex();
|
||||
|
||||
int row = grandparentEntry->children.indexOf( parentEntry );
|
||||
return createIndex( row, 0, parentEntry );
|
||||
}
|
||||
|
||||
|
||||
QVariant
|
||||
AlbumModel::data( const QModelIndex& index, int role ) const
|
||||
{
|
||||
if ( role == Qt::SizeHintRole )
|
||||
return m_itemSize;
|
||||
|
||||
PlayableItem* entry = itemFromIndex( index );
|
||||
if ( !entry )
|
||||
return QVariant();
|
||||
|
||||
if ( role != Qt::DisplayRole ) // && role != Qt::ToolTipRole )
|
||||
return QVariant();
|
||||
|
||||
QString name;
|
||||
if ( !entry->album().isNull() )
|
||||
name = entry->album()->name();
|
||||
else if ( !entry->artist().isNull() )
|
||||
name = entry->artist()->name();
|
||||
|
||||
switch( index.column() )
|
||||
{
|
||||
case 0:
|
||||
return name;
|
||||
break;
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
|
||||
QVariant
|
||||
AlbumModel::headerData( int section, Qt::Orientation orientation, int role ) const
|
||||
{
|
||||
QStringList headers;
|
||||
headers << tr( "Album" );
|
||||
if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= 0 )
|
||||
{
|
||||
return headers.at( section );
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
|
||||
Qt::ItemFlags
|
||||
AlbumModel::flags( const QModelIndex& index ) const
|
||||
{
|
||||
Qt::ItemFlags defaultFlags = QAbstractItemModel::flags( index );
|
||||
|
||||
if ( index.isValid() && index.column() == 0 )
|
||||
return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
|
||||
else
|
||||
return defaultFlags;
|
||||
}
|
||||
|
||||
|
||||
QStringList
|
||||
AlbumModel::mimeTypes() const
|
||||
{
|
||||
QStringList types;
|
||||
types << "application/tomahawk.query.list";
|
||||
return types;
|
||||
}
|
||||
|
||||
|
||||
QMimeData*
|
||||
AlbumModel::mimeData( const QModelIndexList &indexes ) const
|
||||
{
|
||||
QByteArray queryData;
|
||||
QDataStream queryStream( &queryData, QIODevice::WriteOnly );
|
||||
|
||||
bool isAlbumData = true;
|
||||
foreach ( const QModelIndex& i, indexes )
|
||||
{
|
||||
if ( i.column() > 0 )
|
||||
continue;
|
||||
|
||||
QModelIndex idx = index( i.row(), 0, i.parent() );
|
||||
PlayableItem* item = itemFromIndex( idx );
|
||||
if ( item && !item->album().isNull() )
|
||||
{
|
||||
const album_ptr& album = item->album();
|
||||
queryStream << album->artist()->name();
|
||||
queryStream << album->name();
|
||||
|
||||
isAlbumData = true;
|
||||
}
|
||||
else if ( item && !item->artist().isNull() )
|
||||
{
|
||||
const artist_ptr& artist = item->artist();
|
||||
queryStream << artist->name();
|
||||
|
||||
isAlbumData = false;
|
||||
}
|
||||
}
|
||||
|
||||
QMimeData* mimeData = new QMimeData;
|
||||
mimeData->setData( isAlbumData ? "application/tomahawk.metadata.album" : "application/tomahawk.metadata.artist", queryData );
|
||||
|
||||
return mimeData;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
AlbumModel::removeIndex( const QModelIndex& index )
|
||||
{
|
||||
qDebug() << Q_FUNC_INFO;
|
||||
|
||||
if ( index.column() > 0 )
|
||||
return;
|
||||
|
||||
PlayableItem* item = itemFromIndex( index );
|
||||
if ( item )
|
||||
{
|
||||
emit beginRemoveRows( index.parent(), index.row(), index.row() );
|
||||
delete item;
|
||||
emit endRemoveRows();
|
||||
}
|
||||
|
||||
emit itemCountChanged( rowCount( QModelIndex() ) );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
AlbumModel::removeIndexes( const QList<QModelIndex>& indexes )
|
||||
{
|
||||
foreach( const QModelIndex& idx, indexes )
|
||||
{
|
||||
removeIndex( idx );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -254,7 +62,7 @@ AlbumModel::addCollection( const collection_ptr& collection, bool overwrite )
|
||||
|
||||
Database::instance()->enqueue( QSharedPointer<DatabaseCommand>( cmd ) );
|
||||
|
||||
m_title = tr( "All albums from %1" ).arg( collection->source()->friendlyName() );
|
||||
setTitle( tr( "All albums from %1" ).arg( collection->source()->friendlyName() ) );
|
||||
|
||||
if ( collection.isNull() )
|
||||
{
|
||||
@ -296,9 +104,9 @@ AlbumModel::addFilteredCollection( const collection_ptr& collection, unsigned in
|
||||
Database::instance()->enqueue( QSharedPointer<DatabaseCommand>( cmd ) );
|
||||
|
||||
if ( !collection.isNull() )
|
||||
m_title = tr( "All albums from %1" ).arg( collection->source()->friendlyName() );
|
||||
setTitle( tr( "All albums from %1" ).arg( collection->source()->friendlyName() ) );
|
||||
else
|
||||
m_title = tr( "All albums" );
|
||||
setTitle( tr( "All albums" ) );
|
||||
|
||||
emit loadingStarted();
|
||||
}
|
||||
@ -339,8 +147,8 @@ AlbumModel::addAlbums( const QList<Tomahawk::album_ptr>& albums )
|
||||
PlayableItem* albumitem;
|
||||
foreach( const album_ptr& album, trimmedAlbums )
|
||||
{
|
||||
albumitem = new PlayableItem( album, m_rootItem );
|
||||
albumitem->index = createIndex( m_rootItem->children.count() - 1, 0, albumitem );
|
||||
albumitem = new PlayableItem( album, rootItem() );
|
||||
albumitem->index = createIndex( rootItem()->children.count() - 1, 0, albumitem );
|
||||
|
||||
connect( albumitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) );
|
||||
}
|
||||
@ -385,8 +193,8 @@ AlbumModel::addArtists( const QList<Tomahawk::artist_ptr>& artists )
|
||||
PlayableItem* albumitem;
|
||||
foreach ( const artist_ptr& artist, trimmedArtists )
|
||||
{
|
||||
albumitem = new PlayableItem( artist, m_rootItem );
|
||||
albumitem->index = createIndex( m_rootItem->children.count() - 1, 0, albumitem );
|
||||
albumitem = new PlayableItem( artist, rootItem() );
|
||||
albumitem->index = createIndex( rootItem()->children.count() - 1, 0, albumitem );
|
||||
|
||||
connect( albumitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) );
|
||||
}
|
||||
@ -414,8 +222,8 @@ AlbumModel::addQueries( const QList<Tomahawk::query_ptr>& queries )
|
||||
PlayableItem* albumitem;
|
||||
foreach ( const query_ptr& query, queries )
|
||||
{
|
||||
albumitem = new PlayableItem( query, m_rootItem );
|
||||
albumitem->index = createIndex( m_rootItem->children.count() - 1, 0, albumitem );
|
||||
albumitem = new PlayableItem( query, rootItem() );
|
||||
albumitem->index = createIndex( rootItem()->children.count() - 1, 0, albumitem );
|
||||
|
||||
connect( albumitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) );
|
||||
}
|
||||
@ -439,24 +247,6 @@ AlbumModel::onCollectionChanged()
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
AlbumModel::clear()
|
||||
{
|
||||
beginResetModel();
|
||||
delete m_rootItem;
|
||||
m_rootItem = new PlayableItem( 0, this );
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
AlbumModel::onDataChanged()
|
||||
{
|
||||
PlayableItem* p = (PlayableItem*)sender();
|
||||
emit dataChanged( p->index, p->index.sibling( p->index.row(), columnCount( QModelIndex() ) - 1 ) );
|
||||
}
|
||||
|
||||
|
||||
PlayableItem*
|
||||
AlbumModel::findItem( const artist_ptr& artist ) const
|
||||
{
|
||||
|
@ -20,19 +20,19 @@
|
||||
#ifndef ALBUMMODEL_H
|
||||
#define ALBUMMODEL_H
|
||||
|
||||
#include <QAbstractItemModel>
|
||||
#include <QPixmap>
|
||||
|
||||
#include "Album.h"
|
||||
#include "PlaylistInterface.h"
|
||||
#include "database/DatabaseCommand_AllAlbums.h"
|
||||
#include "PlayableModel.h"
|
||||
|
||||
#include "DllMacro.h"
|
||||
|
||||
class PlayableItem;
|
||||
class QMetaData;
|
||||
|
||||
class DLLEXPORT AlbumModel : public QAbstractItemModel
|
||||
class DLLEXPORT AlbumModel : public PlayableModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@ -40,87 +40,28 @@ public:
|
||||
explicit AlbumModel( QObject* parent = 0 );
|
||||
virtual ~AlbumModel();
|
||||
|
||||
virtual QModelIndex index( int row, int column, const QModelIndex& parent ) const;
|
||||
virtual QModelIndex parent( const QModelIndex& child ) const;
|
||||
|
||||
virtual bool isReadOnly() const { return true; }
|
||||
|
||||
virtual int trackCount() const { return rowCount( QModelIndex() ); }
|
||||
virtual int albumCount() const { return rowCount( QModelIndex() ); }
|
||||
|
||||
virtual int rowCount( const QModelIndex& parent ) const;
|
||||
virtual int columnCount( const QModelIndex& parent ) const;
|
||||
|
||||
virtual QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const;
|
||||
virtual QVariant headerData( int section, Qt::Orientation orientation, int role ) const;
|
||||
|
||||
virtual void removeIndex( const QModelIndex& index );
|
||||
virtual void removeIndexes( const QList<QModelIndex>& indexes );
|
||||
|
||||
virtual QMimeData* mimeData( const QModelIndexList& indexes ) const;
|
||||
virtual QStringList mimeTypes() const;
|
||||
virtual Qt::ItemFlags flags( const QModelIndex& index ) const;
|
||||
|
||||
Tomahawk::collection_ptr collection() const { return m_collection; }
|
||||
|
||||
void clear();
|
||||
void addCollection( const Tomahawk::collection_ptr& collection, bool overwrite = false );
|
||||
void addFilteredCollection( const Tomahawk::collection_ptr& collection, unsigned int amount, DatabaseCommand_AllAlbums::SortOrder order, bool overwrite = false );
|
||||
|
||||
virtual QString title() const { return m_title; }
|
||||
virtual QString description() const { return m_description; }
|
||||
virtual void setTitle( const QString& title ) { m_title = title; }
|
||||
virtual void setDescription( const QString& description ) { m_description = description; }
|
||||
|
||||
QSize itemSize() const { return m_itemSize; }
|
||||
void setItemSize( const QSize& size ) { m_itemSize = size; }
|
||||
|
||||
PlayableItem* findItem( const Tomahawk::artist_ptr& artist ) const;
|
||||
PlayableItem* findItem( const Tomahawk::album_ptr& album ) const;
|
||||
|
||||
PlayableItem* itemFromIndex( const QModelIndex& index ) const
|
||||
{
|
||||
if ( index.isValid() )
|
||||
return static_cast<PlayableItem*>( index.internalPointer() );
|
||||
else
|
||||
{
|
||||
return m_rootItem;
|
||||
}
|
||||
}
|
||||
|
||||
public slots:
|
||||
virtual void setRepeatMode( Tomahawk::PlaylistModes::RepeatMode /*mode*/ ) {}
|
||||
virtual void setShuffled( bool /*shuffled*/ ) {}
|
||||
|
||||
void addAlbums( const QList<Tomahawk::album_ptr>& albums );
|
||||
void addArtists( const QList<Tomahawk::artist_ptr>& artists );
|
||||
void addQueries( const QList<Tomahawk::query_ptr>& queries );
|
||||
|
||||
signals:
|
||||
void repeatModeChanged( Tomahawk::PlaylistModes::RepeatMode mode );
|
||||
void shuffleModeChanged( bool enabled );
|
||||
|
||||
void itemCountChanged( unsigned int items );
|
||||
|
||||
void loadingStarted();
|
||||
void loadingFinished();
|
||||
|
||||
private slots:
|
||||
void onDataChanged();
|
||||
|
||||
void onSourceAdded( const Tomahawk::source_ptr& source );
|
||||
void onCollectionChanged();
|
||||
|
||||
private:
|
||||
QPersistentModelIndex m_currentIndex;
|
||||
PlayableItem* m_rootItem;
|
||||
|
||||
QString m_title;
|
||||
QString m_description;
|
||||
bool m_overwriteOnAdd;
|
||||
|
||||
QSize m_itemSize;
|
||||
|
||||
Tomahawk::collection_ptr m_collection;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user