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
break;
}
}
return QVariant( artUrl );
return artUrl;
}
else
{
......@@ -391,51 +391,25 @@ QVariant PLModel::data( const QModelIndex &index, const int role ) const
}
return QVariant( returninfo );
}
else if( role == Qt::DecorationRole && index.column() == 0 )
else if( role == Qt::DecorationRole )
{
switch( columnToMeta(index.column()) )
{
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 )
{
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 ) )
{
return QVariant( QBrush( Qt::gray ) );
......
......@@ -61,7 +61,7 @@ static inline const char * psz_column_title( uint32_t i_column )
case COLUMN_DESCRIPTION: return VLC_META_DESCRIPTION;
case COLUMN_URI: return _("URI");
case COLUMN_RATING: return VLC_META_RATING;
case COLUMN_COVER: return VLC_META_ART_URL;
case COLUMN_COVER: return _("Cover");
default: abort();
}
}
......
......@@ -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 )
{
QDrag *drag = new QDrag( view );
......@@ -415,7 +422,8 @@ bool PlListView::viewportEvent ( QEvent * event )
PlTreeView::PlTreeView( QAbstractItemModel *, QWidget *parent ) : QTreeView( parent )
{
setItemDelegate( new PlTreeViewItemDelegate( this ) );
setItemDelegateForColumn( VLCModel::metaToColumn(COLUMN_COVER),
new CellPixmapDelegate( this ) );
setIconSize( QSize( 20, 20 ) );
setAlternatingRowColors( true );
setAnimated( true );
......
......@@ -78,6 +78,15 @@ public:
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
{
Q_OBJECT
......
......@@ -76,18 +76,10 @@ QString VLCModel::getMeta( const QModelIndex & index, int meta )
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 )
{
QString artUrl = VLCModel::getArtUrl( index ) ;
QString artUrl = index.sibling( index.row(),
VLCModel::columnFromMeta(COLUMN_COVER) ).data().toString();
QPixmap artPix;
QString key = artUrl + QString("%1%2").arg(size.width()).arg(size.height());
......@@ -141,6 +133,21 @@ int VLCModel::columnToMeta( int _column )
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
{
AbstractPLItem *item = getItem( index );
......@@ -198,8 +205,8 @@ void VLCModel::ensureArtRequested( const QModelIndex &index )
QModelIndex child;
for( int row = 0 ; row < nbnodes ; row++ )
{
child = index.child( row, 0 );
if ( child.isValid() && getArtUrl( child ).isEmpty() )
child = index.child( row, COLUMN_COVER );
if ( child.isValid() && child.data().toString().isEmpty() )
THEMIM->getIM()->requestArtUpdate( getInputItem( child ) );
}
}
......
......@@ -163,9 +163,9 @@ public:
/* Custom */
static int columnToMeta( int _column );
static int metaToColumn( int meta );
static QString getMeta( const QModelIndex & index, int meta );
static QPixmap getArtPixmap( const QModelIndex & index, const QSize & size );
static QString getArtUrl( const QModelIndex & index );
protected:
/* 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