From 6f0d29dde9abbe58ee619226883f0330e2f7445b Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sat, 17 Mar 2012 08:08:28 +0100 Subject: [PATCH] * Bold font decoration for usernames in the delegate. --- .../playlist/PlaylistLargeItemDelegate.cpp | 49 ++++++++++++++++--- .../playlist/PlaylistLargeItemDelegate.h | 2 + src/libtomahawk/query.cpp | 14 ++++-- src/libtomahawk/query.h | 5 +- 4 files changed, 57 insertions(+), 13 deletions(-) diff --git a/src/libtomahawk/playlist/PlaylistLargeItemDelegate.cpp b/src/libtomahawk/playlist/PlaylistLargeItemDelegate.cpp index b94c17ac2..f1c036456 100644 --- a/src/libtomahawk/playlist/PlaylistLargeItemDelegate.cpp +++ b/src/libtomahawk/playlist/PlaylistLargeItemDelegate.cpp @@ -20,6 +20,7 @@ #include #include +#include #include "query.h" #include "result.h" @@ -109,6 +110,29 @@ PlaylistLargeItemDelegate::prepareStyleOption( QStyleOptionViewItemV4* option, c } +void +PlaylistLargeItemDelegate::drawRichText( QPainter* painter, const QRect& rect, int flags, QTextDocument& text ) const +{ + text.setPageSize( QSize( rect.width(), QWIDGETSIZE_MAX ) ); + QAbstractTextDocumentLayout* layout = text.documentLayout(); + + const int height = qRound( layout->documentSize().height() ); + int y = rect.y(); + if ( flags & Qt::AlignBottom ) + y += ( rect.height() - height ); + else if ( flags & Qt::AlignVCenter ) + y += ( rect.height() - height ) / 2; + + QAbstractTextDocumentLayout::PaintContext context; + context.palette.setColor( QPalette::Text, painter->pen().color() ); + + painter->save(); + painter->translate( rect.x(), y ); + layout->draw( painter, context ); + painter->restore(); +} + + void PlaylistLargeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const { @@ -141,8 +165,8 @@ PlaylistLargeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& track = item->query()->track(); } - lowerText = item->query()->socialActionDescription( "Love" ); - + lowerText = item->query()->socialActionDescription( "Love", Query::Detailed ); + if ( source.isNull() ) { } @@ -201,9 +225,12 @@ PlaylistLargeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& smallBoldFont.setBold( true ); smallBoldFont.setWeight( 80 ); + QFont smallFont = opt.font; + smallFont.setPixelSize( 10 ); + r.adjust( pixmapRect.width() + 12, 1, -28 - avatar.width(), 0 ); - QRect leftRect = r.adjusted( 0, 0, -r.width() / 2 - 4, 0 ); - QRect rightRect = r.adjusted( r.width() / 2 + 4, 0, 0, 0 ); + QRect leftRect = r.adjusted( 0, 0, -48, 0 ); + QRect rightRect = r.adjusted( r.width() - 40, 0, 0, 0 ); painter->setFont( boldFont ); QString text = painter->fontMetrics().elidedText( artist, Qt::ElideRight, leftRect.width() ); @@ -213,9 +240,17 @@ PlaylistLargeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& text = painter->fontMetrics().elidedText( track, Qt::ElideRight, leftRect.width() ); painter->drawText( leftRect.adjusted( 0, 19, 0, 0 ), text, m_topOption ); - painter->setFont( opt.font ); - text = painter->fontMetrics().elidedText( lowerText, Qt::ElideRight, leftRect.width() ); - painter->drawText( leftRect, text, m_bottomOption ); + painter->setFont( smallFont ); + QTextDocument textDoc; + textDoc.setHtml( lowerText ); + textDoc.setDocumentMargin( 0 ); + textDoc.setDefaultFont( painter->font() ); + textDoc.setDefaultTextOption( m_bottomOption ); + + if ( textDoc.idealWidth() > leftRect.width() ) + textDoc.setHtml( item->query()->socialActionDescription( "Love", Query::Short ) ); + + drawRichText( painter, leftRect, Qt::AlignBottom, textDoc ); if ( duration > 0 ) { diff --git a/src/libtomahawk/playlist/PlaylistLargeItemDelegate.h b/src/libtomahawk/playlist/PlaylistLargeItemDelegate.h index 6bdae39aa..56ea64a57 100644 --- a/src/libtomahawk/playlist/PlaylistLargeItemDelegate.h +++ b/src/libtomahawk/playlist/PlaylistLargeItemDelegate.h @@ -20,6 +20,7 @@ #define PLAYLISTLARGEITEMDELEGATE_H #include +#include #include #include "dllmacro.h" @@ -43,6 +44,7 @@ protected: private: void prepareStyleOption( QStyleOptionViewItemV4* option, const QModelIndex& index, TrackModelItem* item ) const; + void drawRichText( QPainter* painter, const QRect& rect, int flags, QTextDocument& text ) const; QTextOption m_topOption; QTextOption m_centerRightOption; diff --git a/src/libtomahawk/query.cpp b/src/libtomahawk/query.cpp index e59248057..dd47eec9a 100644 --- a/src/libtomahawk/query.cpp +++ b/src/libtomahawk/query.cpp @@ -585,7 +585,7 @@ Query::setLoved( bool loved ) QString -Query::socialActionDescription( const QString& action ) const +Query::socialActionDescription( const QString& action, DescriptionMode mode ) const { QString desc; QList< Tomahawk::SocialAction > socialActions = allSocialActions(); @@ -626,18 +626,22 @@ Query::socialActionDescription( const QString& action ) const if ( sa.source->isLocal() ) { if ( loveCounter == 1 ) - desc += tr( "You" ); + desc += "" + tr( "You" ) + ""; else - desc += tr( "you" ); + desc += "" + tr( "you" ) + ""; } else - desc += sa.source->friendlyName(); + desc += "" + sa.source->friendlyName() + ""; } } if ( loveCounter > 0 ) { if ( loveCounter > 3 ) - desc += " " + tr( "and %1 others" ).arg( loveCounter - 3 ); + desc += " " + tr( "and %1%2 others%3" ).arg( "" ).arg( loveCounter - 3 ).arg( "" ); + + if ( mode == Short ) + desc = "" + tr( "%1 people" ).arg( loveCounter ) + ""; + desc += " " + tr( "loved this track" ); //FIXME: more action descs required } diff --git a/src/libtomahawk/query.h b/src/libtomahawk/query.h index 8629dcfec..f4e2c1b74 100644 --- a/src/libtomahawk/query.h +++ b/src/libtomahawk/query.h @@ -48,6 +48,9 @@ friend class ::DatabaseCommand_LoadPlaylistEntries; friend class Pipeline; public: + enum DescriptionMode + { Detailed = 0, Short = 1 }; + static query_ptr get( const QString& artist, const QString& track, const QString& album, const QID& qid = QString(), bool autoResolve = true ); static query_ptr get( const QString& query, const QID& qid ); @@ -116,7 +119,7 @@ public: void loadSocialActions(); QList< Tomahawk::SocialAction > allSocialActions() const; void setAllSocialActions( const QList< Tomahawk::SocialAction >& socialActions ); - QString socialActionDescription( const QString& action ) const; + QString socialActionDescription( const QString& action, DescriptionMode mode ) const; QWeakPointer< Tomahawk::Query > weakRef() { return m_ownRef; } void setWeakRef( QWeakPointer< Tomahawk::Query > weakRef ) { m_ownRef = weakRef; }