Commit 344416ec authored by Rafaël Carré's avatar Rafaël Carré

QT4 Icon view: cache album art pixmap

The QPixmapCache default size is 10240kB on desktops, this leaves room
for 640 pictures 64x64 in rgba

Improves scrolling with a lot of items.

Also use art url from the first children with art for nodes without art

TODO: cache the full rendering (text + art), QPixmap is a QPaintDevice
subclass
parent 2cd90236
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include <QRect> #include <QRect>
#include <QStyleOptionViewItem> #include <QStyleOptionViewItem>
#include <QFontMetrics> #include <QFontMetrics>
#include <QPixmapCache>
#include "assert.h" #include "assert.h"
...@@ -39,6 +40,22 @@ ...@@ -39,6 +40,22 @@
#define ITEMS_SPACING 10 #define ITEMS_SPACING 10
#define ART_RADIUS 5 #define ART_RADIUS 5
static QPixmap find_art_pixmap( const QString& url )
{
QPixmap pix;
if( QPixmapCache::find( url, pix ) ) /* great, we found it */
return pix;
if( url.isEmpty() || !pix.load( url ) )
pix = QPixmap( ":/noart64" );
else
pix = pix.scaled( ART_SIZE, ART_SIZE, Qt::KeepAspectRatioByExpanding );
QPixmapCache::insert( url, pix ); /* save it for next time */
return pix;
}
void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{ {
painter->setRenderHints( QPainter::Antialiasing | QPainter::SmoothPixmapTransform ); painter->setRenderHints( QPainter::Antialiasing | QPainter::SmoothPixmapTransform );
...@@ -50,19 +67,25 @@ void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewIt ...@@ -50,19 +67,25 @@ void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewIt
PLItem *currentItem = static_cast<PLItem*>( index.internalPointer() ); PLItem *currentItem = static_cast<PLItem*>( index.internalPointer() );
assert( currentItem ); assert( currentItem );
QPixmap pix; QPixmap artpix;
QString url = InputManager::decodeArtURL( currentItem->inputItem() ); QString url = InputManager::decodeArtURL( currentItem->inputItem() );
if( !url.isEmpty() && pix.load( url ) ) /* look up through all children and use the first picture found */
if( url.isEmpty() )
{ {
pix = pix.scaled( ART_SIZE, ART_SIZE, Qt::KeepAspectRatioByExpanding ); int children = currentItem->childCount();
} for( int i = 0; i < children; i++ )
else
{ {
pix = QPixmap( ":/noart64" ); PLItem *child = currentItem->child( i );
url = InputManager::decodeArtURL( child->inputItem() );
if( !url.isEmpty() )
break;
}
} }
QRect artRect = option.rect.adjusted( OFFSET - 1, 0, - OFFSET, - OFFSET *2 ); QRect artRect = option.rect.adjusted( OFFSET - 1, 0, - OFFSET, - OFFSET *2 );
artpix = find_art_pixmap( url ); /* look in the QPixmapCache for art */
QPainterPath artRectPath; QPainterPath artRectPath;
artRectPath.addRoundedRect( artRect, ART_RADIUS, ART_RADIUS ); artRectPath.addRoundedRect( artRect, ART_RADIUS, ART_RADIUS );
...@@ -75,7 +98,7 @@ void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewIt ...@@ -75,7 +98,7 @@ void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewIt
// Draw the art pixmap // Draw the art pixmap
painter->setClipPath( artRectPath ); painter->setClipPath( artRectPath );
painter->drawPixmap( artRect, pix ); painter->drawPixmap( artRect, artpix );
painter->setClipping( false ); painter->setClipping( false );
QColor text = qApp->palette().text().color(); QColor text = qApp->palette().text().color();
......
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