From e70efa5fd0812c0cf498a66838672da4b10f07a4 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Mon, 10 Jun 2013 10:37:46 +0200 Subject: [PATCH] * Delegates use the base-class impls for painting / handling. --- .../playlist/AlbumItemDelegate.cpp | 77 +++---------- src/libtomahawk/playlist/AlbumItemDelegate.h | 9 +- src/libtomahawk/playlist/GridItemDelegate.cpp | 14 +++ src/libtomahawk/playlist/GridItemDelegate.h | 3 + .../playlist/PlaylistLargeItemDelegate.cpp | 106 ++++-------------- .../playlist/PlaylistLargeItemDelegate.h | 15 +-- 6 files changed, 62 insertions(+), 162 deletions(-) diff --git a/src/libtomahawk/playlist/AlbumItemDelegate.cpp b/src/libtomahawk/playlist/AlbumItemDelegate.cpp index f938be75f..746f95226 100644 --- a/src/libtomahawk/playlist/AlbumItemDelegate.cpp +++ b/src/libtomahawk/playlist/AlbumItemDelegate.cpp @@ -48,14 +48,6 @@ AlbumItemDelegate::AlbumItemDelegate( TrackView* parent, PlayableProxyModel* pro , m_view( parent ) , m_model( proxy ) { - m_centerOption = QTextOption( Qt::AlignVCenter ); - m_centerOption.setWrapMode( QTextOption::NoWrap ); - - m_centerRightOption = QTextOption( Qt::AlignVCenter | Qt::AlignRight ); - m_centerRightOption.setWrapMode( QTextOption::NoWrap ); - - connect( proxy, SIGNAL( modelReset() ), this, SLOT( modelChanged() ) ); - connect( parent, SIGNAL( modelChanged() ), this, SLOT( modelChanged() ) ); } @@ -82,11 +74,10 @@ AlbumItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, opt.text.clear(); qApp->style()->drawControl( QStyle::CE_ItemViewItem, &opt, painter ); - if ( m_view->header()->visualIndex( index.column() ) > 0 ) return; - const track_ptr track = item->query()->track(); + const track_ptr& track = item->query()->track(); QString lowerText; painter->save(); @@ -102,73 +93,41 @@ AlbumItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, r.adjust( pixHeight, 0, 0, 0 ); } - QFont boldFont = opt.font; - boldFont.setPointSize( TomahawkUtils::defaultFontSize() + 2 ); - boldFont.setWeight( 99 ); - QFontMetrics boldFontMetrics( boldFont ); - - QFont smallBoldFont = opt.font; - smallBoldFont.setPointSize( TomahawkUtils::defaultFontSize() - 1 ); - smallBoldFont.setBold( true ); - smallBoldFont.setWeight( 60 ); - QFontMetrics smallBoldFontMetrics( smallBoldFont ); - - painter->setFont( boldFont ); + painter->setFont( m_bigBoldFont ); painter->setPen( option.palette.text().color().lighter( 450 ) ); + QRect leftRect = r; QRect figureRect = r.adjusted( 4, 0, 0, 0 ); figureRect.setWidth( QFontMetrics( painter->font() ).width( "888" ) ); - painter->drawText( figureRect, QString::number( index.row() + 1 ), QTextOption( Qt::AlignCenter ) ); - - r.adjust( figureRect.width() + 12, 0, 0, 0 ); - QRect rightRect = r.adjusted( r.width() - smallBoldFontMetrics.width( TomahawkUtils::timeToString( track->duration() ) ), 0, 0, 0 ); - QRect leftRect = r.adjusted( 0, 0, -( rightRect.width() + 8 ), 0 ); - - const int sourceIconSize = r.height(); if ( hoveringOver() == index && index.column() == 0 ) { - const QPixmap infoIcon = TomahawkUtils::defaultPixmap( TomahawkUtils::InfoIcon, TomahawkUtils::Original, QSize( sourceIconSize, sourceIconSize ) ); - QRect arrowRect = QRect( rightRect.right() - sourceIconSize, r.center().y() - sourceIconSize / 2, infoIcon.width(), infoIcon.height() ); - painter->drawPixmap( arrowRect, infoIcon ); - - setInfoButtonRect( index, arrowRect ); - rightRect.moveLeft( rightRect.left() - infoIcon.width() - 8 ); - leftRect.adjust( 0, 0, -( infoIcon.width() + 8 ), 0 ); + drawInfoButton( painter, figureRect.adjusted( 1, 0, 0, 0 ), index, 1.0 ); } - else if ( item->query()->numResults() && !item->query()->results().first()->sourceIcon( TomahawkUtils::RoundedCorners, QSize( sourceIconSize, sourceIconSize ) ).isNull() ) + else { - const QPixmap sourceIcon = item->query()->results().first()->sourceIcon( TomahawkUtils::RoundedCorners, QSize( sourceIconSize, sourceIconSize ) ); - painter->setOpacity( 0.8 ); - painter->drawPixmap( QRect( rightRect.right() - sourceIconSize, r.center().y() - sourceIconSize / 2, sourceIcon.width(), sourceIcon.height() ), sourceIcon ); - painter->setOpacity( 1.0 ); - rightRect.moveLeft( rightRect.left() - sourceIcon.width() - 8 ); + painter->drawText( figureRect, QString::number( index.row() + 1 ), QTextOption( Qt::AlignCenter ) ); } - QString text = painter->fontMetrics().elidedText( track->track(), Qt::ElideRight, leftRect.width() ); + leftRect = r; + leftRect.adjust( figureRect.width() + 12, 0, 0, 0 ); + QRect rightRect = r.adjusted( r.width() - m_smallBoldFontMetrics.width( TomahawkUtils::timeToString( track->duration() ) ), 0, 0, 0 ); + { + const QRect leftRectBefore = leftRect; + leftRect = drawSourceIcon( painter, leftRect, item, 1.0 ); + rightRect.moveLeft( rightRect.left() - ( leftRectBefore.width() - leftRect.width() ) ); + leftRect.setWidth( leftRect.width() - rightRect.width() ); + } + + const QString text = painter->fontMetrics().elidedText( track->track(), Qt::ElideRight, leftRect.width() ); painter->setPen( opt.palette.text().color() ); painter->drawText( leftRect, text, m_centerOption ); if ( track->duration() > 0 ) { - painter->setPen( opt.palette.text().color() ); - painter->setFont( smallBoldFont ); + painter->setFont( m_smallBoldFont ); painter->drawText( rightRect, TomahawkUtils::timeToString( track->duration() ), m_centerRightOption ); } } painter->restore(); } - - -void -AlbumItemDelegate::doUpdateIndex( const QPersistentModelIndex& idx ) -{ - if ( idx.isValid() ) - emit updateIndex( idx ); -} - - -void -AlbumItemDelegate::modelChanged() -{ -} diff --git a/src/libtomahawk/playlist/AlbumItemDelegate.h b/src/libtomahawk/playlist/AlbumItemDelegate.h index 040a20099..dccdbc5b8 100644 --- a/src/libtomahawk/playlist/AlbumItemDelegate.h +++ b/src/libtomahawk/playlist/AlbumItemDelegate.h @@ -43,18 +43,11 @@ public: AlbumItemDelegate( TrackView* parent = 0, PlayableProxyModel* proxy = 0 ); virtual QSize sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const; - + protected: void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const; -private slots: - void doUpdateIndex( const QPersistentModelIndex& idx ); - void modelChanged(); - private: - QTextOption m_centerOption; - QTextOption m_centerRightOption; - TrackView* m_view; PlayableProxyModel* m_model; }; diff --git a/src/libtomahawk/playlist/GridItemDelegate.cpp b/src/libtomahawk/playlist/GridItemDelegate.cpp index c25c368b2..544db7935 100644 --- a/src/libtomahawk/playlist/GridItemDelegate.cpp +++ b/src/libtomahawk/playlist/GridItemDelegate.cpp @@ -543,6 +543,20 @@ GridItemDelegate::onCurrentIndexChanged() } +void +GridItemDelegate::resetHoverIndex() +{ + foreach ( ImageButton* button, m_playButton ) + button->deleteLater(); + m_playButton.clear(); + + QModelIndex idx = m_hoveringOver; + m_hoveringOver = QPersistentModelIndex(); + m_hoverIndex = QPersistentModelIndex(); + doUpdateIndex( idx ); +} + + void GridItemDelegate::createPauseButton( const QPersistentModelIndex& index ) { diff --git a/src/libtomahawk/playlist/GridItemDelegate.h b/src/libtomahawk/playlist/GridItemDelegate.h index 6b48e3a19..002daa589 100644 --- a/src/libtomahawk/playlist/GridItemDelegate.h +++ b/src/libtomahawk/playlist/GridItemDelegate.h @@ -48,6 +48,9 @@ public: QSize itemSize() const { return m_itemSize; } void setItemSize( const QSize& size ) { m_itemSize = size; } +public slots: + void resetHoverIndex(); + protected: void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const; QSize sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const; diff --git a/src/libtomahawk/playlist/PlaylistLargeItemDelegate.cpp b/src/libtomahawk/playlist/PlaylistLargeItemDelegate.cpp index d2ef227d0..e6b36b079 100644 --- a/src/libtomahawk/playlist/PlaylistLargeItemDelegate.cpp +++ b/src/libtomahawk/playlist/PlaylistLargeItemDelegate.cpp @@ -37,8 +37,6 @@ #include "utils/TomahawkUtilsGui.h" #include "utils/Logger.h" -#include -#include using namespace Tomahawk; @@ -49,17 +47,6 @@ PlaylistLargeItemDelegate::PlaylistLargeItemDelegate( DisplayMode mode, TrackVie , m_model( proxy ) , m_mode( mode ) { - m_topOption = QTextOption( Qt::AlignTop ); - m_topOption.setWrapMode( QTextOption::NoWrap ); - - m_centerRightOption = QTextOption( Qt::AlignVCenter | Qt::AlignRight ); - m_centerRightOption.setWrapMode( QTextOption::NoWrap ); - - m_bottomOption = QTextOption( Qt::AlignBottom ); - m_bottomOption.setWrapMode( QTextOption::NoWrap ); - - connect( proxy, SIGNAL( modelReset() ), SLOT( modelChanged() ) ); - connect( parent, SIGNAL( modelChanged() ), SLOT( modelChanged() ) ); } @@ -91,7 +78,6 @@ PlaylistLargeItemDelegate::drawRichText( QPainter* painter, const QStyleOptionVi y += ( rect.height() - height ) / 2; QAbstractTextDocumentLayout::PaintContext context; - context.palette.setColor( QPalette::Text, painter->pen().color() ); painter->save(); @@ -111,10 +97,10 @@ PlaylistLargeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& prepareStyleOption( &opt, index, item ); bool isUnlistened = true; - if( m_mode == Inbox ) + if ( m_mode == Inbox ) { QList< Tomahawk::SocialAction > socialActions = item->query()->queryTrack()->allSocialActions(); - foreach( const Tomahawk::SocialAction& sa, socialActions ) + foreach ( const Tomahawk::SocialAction& sa, socialActions ) { if ( sa.action.toString() == "Inbox" && sa.value.toBool() == false ) { @@ -132,8 +118,8 @@ PlaylistLargeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& const track_ptr track = item->query()->track(); QString lowerText; - QSize avatarSize( 32, 32 ); + if ( m_mode == RecentlyPlayed && item->playbackLog().source ) { QString playtime = TomahawkUtils::ageToString( QDateTime::fromTime_t( item->playbackLog().timestamp ), true ); @@ -147,7 +133,6 @@ PlaylistLargeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& if ( m_mode == LatestAdditions && item->query()->numResults() ) { QString playtime = TomahawkUtils::ageToString( QDateTime::fromTime_t( item->query()->results().first()->modificationTime() ), true ); - lowerText = QString( tr( "added %1", "e.g. added 3 hours ago" ) ).arg( playtime ); } @@ -170,78 +155,43 @@ PlaylistLargeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& if ( item->isPlaying() ) { painter->drawPixmap( npr, TomahawkUtils::defaultPixmap( TomahawkUtils::NowPlayingSpeaker, TomahawkUtils::Original, npr.size() ) ); - r.adjust( pixHeight + 2*pixMargin, 0, 0, 0 ); + r.adjust( pixHeight + 2 * pixMargin, 0, 0, 0 ); } else { npr = npr.adjusted( 0, npr.height() / 4, -npr.width() / 2, -npr.height() / 4 ); painter->drawPixmap( npr, TomahawkUtils::defaultPixmap( TomahawkUtils::InboxNewItem, TomahawkUtils::Original, npr.size() ) ); - r.adjust( npr.width() + 2*pixMargin, 0, 0, 0 ); + r.adjust( npr.width() + 2 * pixMargin, 0, 0, 0 ); } } painter->setPen( opt.palette.text().color() ); - QRect pixmapRect = r.adjusted( 6, 0, -option.rect.width() + option.rect.height() - 6 + r.left(), 0 ); - QRect avatarRect = r.adjusted( option.rect.width() - r.left() - 12 - avatarSize.width(), ( option.rect.height() - avatarSize.height() ) / 2 - 5, 0, 0 ); - avatarRect.setSize( avatarSize ); - - if ( !m_pixmaps.contains( index ) ) - { - m_pixmaps.insert( index, QSharedPointer< Tomahawk::PixmapDelegateFader >( new Tomahawk::PixmapDelegateFader( item->query(), pixmapRect.size(), TomahawkUtils::RoundedCorners, false ) ) ); - _detail::Closure* closure = NewClosure( m_pixmaps[ index ], SIGNAL( repaintRequest() ), const_cast(this), SLOT( doUpdateIndex( const QPersistentModelIndex& ) ), QPersistentModelIndex( index ) ); - closure->setAutoDelete( false ); - } - - const QPixmap pixmap = m_pixmaps[ index ]->currentPixmap(); - painter->drawPixmap( pixmapRect, pixmap ); - - QFont boldFont = opt.font; - boldFont.setPointSize( TomahawkUtils::defaultFontSize() + 2 ); - boldFont.setWeight( 99 ); - QFontMetrics boldFontMetrics( boldFont ); - - QFont smallBoldFont = opt.font; - smallBoldFont.setPointSize( TomahawkUtils::defaultFontSize() - 1 ); - smallBoldFont.setBold( true ); - smallBoldFont.setWeight( 60 ); - QFontMetrics smallBoldFontMetrics( smallBoldFont ); - - QFont smallFont = opt.font; - smallFont.setPointSize( TomahawkUtils::defaultFontSize() - 1 ); - - r.adjust( pixmapRect.width() + 12, 1, - 16, 0 ); - QRect rightRect = r.adjusted( r.width() - smallBoldFontMetrics.width( TomahawkUtils::timeToString( track->duration() ) ), 0, 0, 0 ); - QRect leftRect = r.adjusted( 0, 0, -( rightRect.width() + 8 ), 0 ); - - const int sourceIconSize = avatarRect.width() - 6; + r.adjust( 4, 0, -16, 0 ); + QRect leftRect; if ( hoveringOver() == index && !index.data().toString().isEmpty() && index.column() == 0 ) { - const QPixmap infoIcon = TomahawkUtils::defaultPixmap( TomahawkUtils::InfoIcon, TomahawkUtils::Original, QSize( sourceIconSize, sourceIconSize ) ); - QRect arrowRect = QRect( rightRect.right() - sourceIconSize, r.center().y() - sourceIconSize / 2, infoIcon.width(), infoIcon.height() ); - painter->drawPixmap( arrowRect, infoIcon ); - - setInfoButtonRect( index, arrowRect ); - rightRect.moveLeft( rightRect.left() - infoIcon.width() - 8 ); - leftRect.adjust( 0, 0, -( infoIcon.width() + 8 ), 0 ); + leftRect = drawInfoButton( painter, r, index, 0.9 ); } - else if ( item->query()->numResults() && !item->query()->results().first()->sourceIcon( TomahawkUtils::RoundedCorners, QSize( sourceIconSize, sourceIconSize ) ).isNull() ) + else { - const QPixmap sourceIcon = item->query()->results().first()->sourceIcon( TomahawkUtils::RoundedCorners, QSize( sourceIconSize, sourceIconSize ) ); - painter->setOpacity( 0.8 ); - painter->drawPixmap( QRect( rightRect.right() - sourceIconSize, r.center().y() - sourceIconSize / 2, sourceIcon.width(), sourceIcon.height() ), sourceIcon ); - painter->setOpacity( 1.0 ); - - rightRect.moveLeft( rightRect.left() - sourceIcon.width() - 8 ); - leftRect.adjust( 0, 0, -( sourceIcon.width() + 8 ), 0 ); + leftRect = drawCover( painter, r, item, index ); } - painter->setFont( boldFont ); - QString text = painter->fontMetrics().elidedText( track->track(), Qt::ElideRight, leftRect.width() ); + leftRect.setX( leftRect.left() + 8 ); + QRect rightRect = r.adjusted( r.width() - m_smallBoldFontMetrics.width( TomahawkUtils::timeToString( track->duration() ) ), 0, 0, 0 ); + { + const QRect leftRectBefore = leftRect; + leftRect = drawSourceIcon( painter, leftRect, item, 0.5 ); + rightRect.moveLeft( rightRect.left() - ( leftRectBefore.width() - leftRect.width() ) ); + } + + painter->setFont( m_bigBoldFont ); + const QString text = painter->fontMetrics().elidedText( track->track(), Qt::ElideRight, leftRect.width() ); painter->drawText( leftRect, text, m_topOption ); - painter->setFont( smallFont ); + painter->setFont( m_smallFont ); QTextDocument textDoc; if ( track->album().isEmpty() ) textDoc.setHtml( tr( "by %1", "e.g. by SomeArtist" ).arg( track->artist() ) ); @@ -252,7 +202,7 @@ PlaylistLargeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& textDoc.setDefaultTextOption( m_topOption ); if ( textDoc.idealWidth() <= leftRect.width() ) - drawRichText( painter, opt, leftRect.adjusted( 0, boldFontMetrics.height() + 1, 0, 0 ), Qt::AlignTop, textDoc ); + drawRichText( painter, opt, leftRect.adjusted( 0, m_bigBoldFontMetrics.height() + 1, 0, 0 ), Qt::AlignTop, textDoc ); if ( !( option.state & QStyle::State_Selected || item->isPlaying() ) ) painter->setPen( opt.palette.text().color().darker() ); @@ -270,7 +220,7 @@ PlaylistLargeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& if ( track->duration() > 0 ) { painter->setPen( opt.palette.text().color() ); - painter->setFont( smallBoldFont ); + painter->setFont( m_smallBoldFont ); painter->drawText( rightRect, TomahawkUtils::timeToString( track->duration() ), m_centerRightOption ); } } @@ -278,16 +228,8 @@ PlaylistLargeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& } -void -PlaylistLargeItemDelegate::doUpdateIndex( const QPersistentModelIndex& idx ) -{ - if ( idx.isValid() ) - emit updateIndex( idx ); -} - - void PlaylistLargeItemDelegate::modelChanged() { - m_pixmaps.clear(); + PlaylistItemDelegate::modelChanged(); } diff --git a/src/libtomahawk/playlist/PlaylistLargeItemDelegate.h b/src/libtomahawk/playlist/PlaylistLargeItemDelegate.h index 2812225c8..d67f047a7 100644 --- a/src/libtomahawk/playlist/PlaylistLargeItemDelegate.h +++ b/src/libtomahawk/playlist/PlaylistLargeItemDelegate.h @@ -27,10 +27,6 @@ #include "DllMacro.h" #include "Typedefs.h" -namespace Tomahawk { -class PixmapDelegateFader; -} - class PlayableItem; class PlayableProxyModel; class TrackView; @@ -50,19 +46,12 @@ public: protected: void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const; -private slots: - void doUpdateIndex( const QPersistentModelIndex& idx ); - void modelChanged(); +protected slots: + virtual void modelChanged(); private: void drawRichText( QPainter* painter, const QStyleOptionViewItem& option, const QRect& rect, int flags, QTextDocument& text ) const; - QTextOption m_topOption; - QTextOption m_centerRightOption; - QTextOption m_bottomOption; - - mutable QHash< QPersistentModelIndex, QSharedPointer< Tomahawk::PixmapDelegateFader > > m_pixmaps; - TrackView* m_view; PlayableProxyModel* m_model; DisplayMode m_mode;