Commit 0823125e authored by Francois Cartegnie's avatar Francois Cartegnie

Qt: VLCModel/TreeView: delegate cover rendering (fix #10206)

parent a1f1c15f
...@@ -381,7 +381,7 @@ QVariant PLModel::data( const QModelIndex &index, const int role ) const ...@@ -381,7 +381,7 @@ QVariant PLModel::data( const QModelIndex &index, const int role ) const
break; break;
} }
} }
return QVariant( artUrl ); return artUrl;
} }
else else
{ {
...@@ -391,51 +391,25 @@ QVariant PLModel::data( const QModelIndex &index, const int role ) const ...@@ -391,51 +391,25 @@ QVariant PLModel::data( const QModelIndex &index, const int role ) const
} }
return QVariant( returninfo ); return QVariant( returninfo );
} }
else if( role == Qt::DecorationRole && index.column() == 0 ) else if( role == Qt::DecorationRole )
{ {
/* Used to segfault here because i_type wasn't always initialized */ switch( columnToMeta(index.column()) )
return QVariant( icons[item->inputItem()->i_type] ); {
case COLUMN_TITLE:
/* Used to segfault here because i_type wasn't always initialized */
return QVariant( icons[item->inputItem()->i_type] );
case COLUMN_COVER:
/* !warn: changes tree item line height. Otherwise, override
* delegate's sizehint */
return getArtPixmap( index, QSize(16,16) );
default:
return QVariant();
}
} }
else if( role == Qt::FontRole ) else if( role == Qt::FontRole )
{ {
return QVariant( QFont() ); return QVariant( QFont() );
} }
else if( role == Qt::ToolTipRole )
{
int i_art_policy = var_GetInteger( p_playlist, "album-art" );
QString artUrl;
/* FIXME: Skip, as we don't want the pixmap and do not know the cached art file */
if ( i_art_policy == ALBUM_ART_ALL )
artUrl = getArtUrl( index );
if ( artUrl.isEmpty() ) artUrl = ":/noart";
QString duration = qtr( "unknown" );
QString name;
PL_LOCK;
input_item_t *p_item = item->inputItem();
if ( !p_item )
{
PL_UNLOCK;
return QVariant();
}
if ( p_item->i_duration > 0 )
{
char *psz = psz_column_meta( item->inputItem(), COLUMN_DURATION );
duration = qfu( psz );
free( psz );
}
name = qfu( p_item->psz_name );
PL_UNLOCK;
QPixmap image = getArtPixmap( index, QSize( 128, 128 ) );
QByteArray bytes;
QBuffer buffer( &bytes );
buffer.open( QIODevice::WriteOnly );
image.save(&buffer, "BMP"); /* uncompressed, see qpixmap#reading-and-writing-image-files */
return QVariant( QString("<img width=\"128\" height=\"128\" align=\"left\" src=\"data:image/bmp;base64,%1\"/><div><b>%2</b><br/>%3</div>")
.arg( bytes.toBase64().constData() )
.arg( name )
.arg( qtr("Duration") + ": " + duration )
);
}
else if( role == Qt::BackgroundRole && isCurrent( index ) ) else if( role == Qt::BackgroundRole && isCurrent( index ) )
{ {
return QVariant( QBrush( Qt::gray ) ); return QVariant( QBrush( Qt::gray ) );
......
...@@ -61,7 +61,7 @@ static inline const char * psz_column_title( uint32_t i_column ) ...@@ -61,7 +61,7 @@ static inline const char * psz_column_title( uint32_t i_column )
case COLUMN_DESCRIPTION: return VLC_META_DESCRIPTION; case COLUMN_DESCRIPTION: return VLC_META_DESCRIPTION;
case COLUMN_URI: return _("URI"); case COLUMN_URI: return _("URI");
case COLUMN_RATING: return VLC_META_RATING; case COLUMN_RATING: return VLC_META_RATING;
case COLUMN_COVER: return VLC_META_ART_URL; case COLUMN_COVER: return _("Cover");
default: abort(); default: abort();
} }
} }
......
...@@ -288,6 +288,13 @@ void PlTreeViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewIt ...@@ -288,6 +288,13 @@ void PlTreeViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewIt
} }
} }
void CellPixmapDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
QPixmap pixmap = index.data( Qt::DecorationRole ).value<QPixmap>();
painter->drawPixmap( option.rect.topLeft(),
pixmap.scaled( option.rect.size(), Qt::KeepAspectRatio ) );
}
static inline void plViewStartDrag( QAbstractItemView *view, const Qt::DropActions & supportedActions ) static inline void plViewStartDrag( QAbstractItemView *view, const Qt::DropActions & supportedActions )
{ {
QDrag *drag = new QDrag( view ); QDrag *drag = new QDrag( view );
...@@ -415,7 +422,8 @@ bool PlListView::viewportEvent ( QEvent * event ) ...@@ -415,7 +422,8 @@ bool PlListView::viewportEvent ( QEvent * event )
PlTreeView::PlTreeView( QAbstractItemModel *, QWidget *parent ) : QTreeView( parent ) PlTreeView::PlTreeView( QAbstractItemModel *, QWidget *parent ) : QTreeView( parent )
{ {
setItemDelegate( new PlTreeViewItemDelegate( this ) ); setItemDelegate( new PlTreeViewItemDelegate( this ) );
setItemDelegateForColumn( VLCModel::metaToColumn(COLUMN_COVER),
new CellPixmapDelegate( this ) );
setIconSize( QSize( 20, 20 ) ); setIconSize( QSize( 20, 20 ) );
setAlternatingRowColors( true ); setAlternatingRowColors( true );
setAnimated( true ); setAnimated( true );
......
...@@ -78,6 +78,15 @@ public: ...@@ -78,6 +78,15 @@ public:
virtual void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const; virtual void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
}; };
class CellPixmapDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
CellPixmapDelegate(QWidget *parent = 0) : QStyledItemDelegate(parent) {}
virtual void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
};
class PlIconView : public QListView class PlIconView : public QListView
{ {
Q_OBJECT Q_OBJECT
......
...@@ -76,18 +76,10 @@ QString VLCModel::getMeta( const QModelIndex & index, int meta ) ...@@ -76,18 +76,10 @@ QString VLCModel::getMeta( const QModelIndex & index, int meta )
data().toString(); data().toString();
} }
QString VLCModel::getArtUrl( const QModelIndex & index )
{
return index.model()->index( index.row(),
columnFromMeta( COLUMN_COVER ),
index.parent() )
.data().toString();
}
QPixmap VLCModel::getArtPixmap( const QModelIndex & index, const QSize & size ) QPixmap VLCModel::getArtPixmap( const QModelIndex & index, const QSize & size )
{ {
QString artUrl = VLCModel::getArtUrl( index ) ; QString artUrl = index.sibling( index.row(),
VLCModel::columnFromMeta(COLUMN_COVER) ).data().toString();
QPixmap artPix; QPixmap artPix;
QString key = artUrl + QString("%1%2").arg(size.width()).arg(size.height()); QString key = artUrl + QString("%1%2").arg(size.width()).arg(size.height());
...@@ -141,6 +133,21 @@ int VLCModel::columnToMeta( int _column ) ...@@ -141,6 +133,21 @@ int VLCModel::columnToMeta( int _column )
return meta; return meta;
} }
int VLCModel::metaToColumn( int _meta )
{
int meta = 1, column = 0;
while( meta != COLUMN_END )
{
if ( meta & _meta )
break;
meta <<= 1;
column++;
}
return column;
}
int VLCModel::itemId( const QModelIndex &index, int type ) const int VLCModel::itemId( const QModelIndex &index, int type ) const
{ {
AbstractPLItem *item = getItem( index ); AbstractPLItem *item = getItem( index );
...@@ -198,8 +205,8 @@ void VLCModel::ensureArtRequested( const QModelIndex &index ) ...@@ -198,8 +205,8 @@ void VLCModel::ensureArtRequested( const QModelIndex &index )
QModelIndex child; QModelIndex child;
for( int row = 0 ; row < nbnodes ; row++ ) for( int row = 0 ; row < nbnodes ; row++ )
{ {
child = index.child( row, 0 ); child = index.child( row, COLUMN_COVER );
if ( child.isValid() && getArtUrl( child ).isEmpty() ) if ( child.isValid() && child.data().toString().isEmpty() )
THEMIM->getIM()->requestArtUpdate( getInputItem( child ) ); THEMIM->getIM()->requestArtUpdate( getInputItem( child ) );
} }
} }
......
...@@ -163,9 +163,9 @@ public: ...@@ -163,9 +163,9 @@ public:
/* Custom */ /* Custom */
static int columnToMeta( int _column ); static int columnToMeta( int _column );
static int metaToColumn( int meta );
static QString getMeta( const QModelIndex & index, int meta ); static QString getMeta( const QModelIndex & index, int meta );
static QPixmap getArtPixmap( const QModelIndex & index, const QSize & size ); static QPixmap getArtPixmap( const QModelIndex & index, const QSize & size );
static QString getArtUrl( const QModelIndex & index );
protected: protected:
/* Custom methods / helpers */ /* Custom methods / helpers */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment