1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-16 11:04:01 +02:00

Paint a nicer playlist delegate, and number count backdrop

This commit is contained in:
Leo Franchi
2011-08-12 00:10:02 -04:00
parent fe15fcd1f5
commit 1fa1ed42e9
9 changed files with 96 additions and 32 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@@ -349,6 +349,42 @@ createDragPixmap( int itemCount )
return dragPixmap;
}
void
drawBackgroundAndNumbers( QPainter* painter, const QString& text, const QRect& figRectIn )
{
QRect figRect = figRectIn;
if ( text.length() == 1 )
figRect.adjust( -painter->fontMetrics().averageCharWidth(), 0, 0, 0 );
QPen origpen = painter->pen();
QPen pen = origpen;
pen.setWidth( 1.0 );
painter->setPen( pen );
painter->drawRect( figRect );
// circles look bad. make it an oval. (thanks, apple)
const int bulgeWidth = 8;
const int offset = 0; // number of pixels to begin, counting inwards from figRect.x() and figRect.width(). 0 means start at each end, negative means start inside the rect.
QPainterPath ppath;
ppath.moveTo( QPoint( figRect.x() + offset, figRect.y() + figRect.height() / 2 ) );
QRect leftArcRect( figRect.x() + offset - bulgeWidth, figRect.y(), 2*bulgeWidth, figRect.height() );
ppath.arcTo( leftArcRect, 90, 180 );
painter->drawPath( ppath );
ppath = QPainterPath();
ppath.moveTo( figRect.x() + figRect.width() - offset, figRect.y() + figRect.height() / 2 );
leftArcRect = QRect( figRect.x() + figRect.width() - offset - bulgeWidth, figRect.y(), 2*bulgeWidth, figRect.height() );
ppath.arcTo( leftArcRect, 270, 180 );
painter->drawPath( ppath );
painter->setPen( origpen );
QTextOption to( Qt::AlignCenter );
painter->setPen( Qt::white );
painter->drawText( figRect.adjusted( -5, 0, 6, 0 ), text, to );
}
void
unmarginLayout( QLayout* layout )

View File

@@ -25,9 +25,11 @@
#include <QThread>
#include <QNetworkProxy>
#include <QStringList>
#include <QRect>
#define RESPATH ":/data/"
class QPainter;
class QColor;
class QDir;
class QDateTime;
@@ -74,6 +76,8 @@ namespace TomahawkUtils
DLLEXPORT QColor alphaBlend( const QColor& colorFrom, const QColor& colorTo, float opacity );
DLLEXPORT QPixmap createDragPixmap( int itemCount = 1 );
DLLEXPORT void drawBackgroundAndNumbers( QPainter* p, const QString& text, const QRect& rect );
DLLEXPORT void unmarginLayout( QLayout* layout );
DLLEXPORT NetworkProxyFactory* proxyFactory();

View File

@@ -167,10 +167,27 @@ WelcomePlaylistModel::playlistRevisionLoaded()
void
WelcomePlaylistModel::onSourceAdded( const Tomahawk::source_ptr& source )
{
connect( source.data(), SIGNAL( online() ), this, SLOT( sourceOnline() ) );
connect( source->collection().data(), SIGNAL( playlistsAdded( QList<Tomahawk::playlist_ptr> ) ), SLOT( loadFromSettings() ) );
connect( source->collection().data(), SIGNAL( playlistsDeleted( QList<Tomahawk::playlist_ptr> ) ), SLOT( onPlaylistsRemoved( QList<Tomahawk::playlist_ptr> ) ) );
}
void
WelcomePlaylistModel::sourceOnline()
{
Source* s = qobject_cast< Source* >( sender() );
Q_ASSERT( s );
for ( int i = 0; i < m_recplaylists.size(); i++ )
{
if ( m_recplaylists[ i ]->author().data() == s )
{
QModelIndex idx = index( i, 0, QModelIndex() );
emit dataChanged( idx, idx );
}
}
}
void
WelcomePlaylistModel::onPlaylistsRemoved( QList< playlist_ptr > playlists )

View File

@@ -60,6 +60,8 @@ private:
unsigned int m_maxPlaylists;
bool m_waitingForSome;
public slots:
void sourceOnline();
};
#endif // WELCOMEPLAYLISTMODEL_H

View File

@@ -221,8 +221,6 @@ PlaylistDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option,
QFont font = opt.font;
QFont boldFont = opt.font;
boldFont.setBold( true );
QFont italicFont = opt.font;
italicFont.setItalic( true );
QPixmap icon;
WelcomePlaylistModel::PlaylistTypes type = (WelcomePlaylistModel::PlaylistTypes)index.data( WelcomePlaylistModel::PlaylistTypeRole ).toInt();
@@ -246,16 +244,40 @@ PlaylistDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option,
{
descText = index.data( WelcomePlaylistModel::ArtistRole ).toString();
}
painter->drawText( option.rect.adjusted( 56, 26, -100, -8 ), descText );
QColor c = painter->pen().color();
painter->setPen( QColor( Qt::gray ).darker() );
painter->drawText( option.rect.adjusted( 66, 19, -100, -8 ), descText );
painter->setPen( c );
QString trackCount = tr( "%1 tracks" ).arg( index.data( WelcomePlaylistModel::TrackCountRole ).toString() );
painter->drawText( option.rect.adjusted( option.rect.width() - 96, 12, 0, -2 - opt.rect.height() / 2 ), trackCount, to );
if ( type != WelcomePlaylistModel::Station )
{
painter->save();
QString tracks = index.data( WelcomePlaylistModel::TrackCountRole ).toString();
int width = painter->fontMetrics().width( tracks );
// int bottomEdge = pixmapRect
// right edge 10px past right edge of pixmapRect
// bottom edge flush with bottom of pixmap
QRect rect( pixmapRect.right() - width, 0, width - 8, 0 );
rect.adjust( 0, 0, -1, 0 );
rect.setTop( pixmapRect.bottom() - painter->fontMetrics().height() - 1 );
rect.setBottom( pixmapRect.bottom() + 1 );
QString author = index.data( WelcomePlaylistModel::PlaylistRole ).value< Tomahawk::playlist_ptr >()->author()->friendlyName();
QRect r = option.rect.adjusted( option.rect.width() - 96, 2 + opt.rect.height() / 2, 0, -12);
painter->setFont( italicFont );
author = painter->fontMetrics().elidedText( author, Qt::ElideRight, r.width() );
painter->drawText( r, author, to );
QColor figColor( 191, 191, 191 );
painter->setPen( figColor );
painter->setBrush( figColor );
painter->setFont( boldFont );
TomahawkUtils::drawBackgroundAndNumbers( painter, tracks, rect );
painter->restore();
}
QPixmap avatar = index.data( WelcomePlaylistModel::PlaylistRole ).value< Tomahawk::playlist_ptr >()->author()->avatar();
if ( avatar.isNull() )
avatar = m_defaultAvatar;
avatar = TomahawkUtils::createAvatarFrame( avatar );
QRect r( option.rect.width() - avatar.width() - 10, option.rect.top() + option.rect.height()/2 - avatar.height()/2, avatar.width(), avatar.height() );
painter->drawPixmap( r, avatar );
painter->setFont( boldFont );
painter->drawText( option.rect.adjusted( 56, 6, -100, -option.rect.height() + 20 ), index.data().toString() );

View File

@@ -52,6 +52,7 @@ public:
m_playlistIcon = QPixmap( RESPATH "images/playlist-icon.png" );
m_autoIcon = QPixmap( RESPATH "images/automatic-playlist.png" );
m_stationIcon = QPixmap( RESPATH "images/station.png" );
m_defaultAvatar = QPixmap( RESPATH "images/user-avatar.png" );
}
protected:
@@ -59,7 +60,7 @@ protected:
QSize sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const;
private:
QPixmap m_playlistIcon, m_autoIcon, m_stationIcon;
QPixmap m_playlistIcon, m_autoIcon, m_stationIcon, m_defaultAvatar;
};
class DLLEXPORT PlaylistWidget : public QListView

View File

@@ -673,34 +673,16 @@ SourceDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, co
{
painter->setRenderHint( QPainter::Antialiasing );
QRect figRect = o.rect.adjusted( o.rect.width() - figWidth - 18, 0, -10, -o.rect.height() + 16 );
QRect figRect = o.rect.adjusted( o.rect.width() - figWidth - 6, 0, -15, -o.rect.height() + 16 );
int hd = ( option.rect.height() - figRect.height() ) / 2;
figRect.adjust( 0, hd, 0, hd );
painter->setFont( bold );
QColor figColor( 167, 183, 211 );
painter->setPen( figColor );
painter->setBrush( figColor );
QPen origpen = painter->pen();
QPen pen = origpen;
pen.setWidth( 1.0 );
painter->setPen( pen );
painter->drawRect( figRect );
QPainterPath ppath;
ppath.moveTo( QPoint( figRect.x(), figRect.y() ) );
ppath.quadTo( QPoint( figRect.x() - 8, figRect.y() + figRect.height() / 2 ), QPoint( figRect.x(), figRect.y() + figRect.height() ) );
painter->drawPath( ppath );
ppath.moveTo( QPoint( figRect.x() + figRect.width(), figRect.y() ) );
ppath.quadTo( QPoint( figRect.x() + figRect.width() + 8, figRect.y() + figRect.height() / 2 ), QPoint( figRect.x() + figRect.width(), figRect.y() + figRect.height() ) );
painter->drawPath( ppath );
painter->setPen( origpen );
QTextOption to( Qt::AlignCenter );
painter->setFont( bold );
painter->setPen( Qt::white );
painter->drawText( figRect, tracks, to );
TomahawkUtils::drawBackgroundAndNumbers( painter, tracks, figRect );
}
painter->restore();