1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-20 15:59:42 +01:00

Make artist names in album cover view clickable

This commit is contained in:
Leo Franchi 2011-10-30 17:49:09 -04:00
parent 70608158fe
commit 29fa5c4cd9
6 changed files with 89 additions and 4 deletions

View File

@ -31,6 +31,8 @@
#include "playlist/albumitem.h"
#include "playlist/albumproxymodel.h"
#include <QMouseEvent>
#include <viewmanager.h>
AlbumItemDelegate::AlbumItemDelegate( QAbstractItemView* parent, AlbumProxyModel* proxy )
@ -152,11 +154,75 @@ AlbumItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option,
text = painter->fontMetrics().elidedText( item->album()->name(), Qt::ElideRight, textRect.width() - 3 );
painter->drawText( textRect, text, to );
// If the user is hovering over an artist rect, draw a background so she knows it's clickable
QRect r = textRect;
r.setTop( r.bottom() - painter->fontMetrics().height() );
if ( m_hoveringOver == index )
TomahawkUtils::drawQueryBackground( painter, opt.palette, r, 1.5 );
painter->setPen( opt.palette.color( QPalette::Dark ) );
to.setAlignment( Qt::AlignHCenter | Qt::AlignBottom );
text = painter->fontMetrics().elidedText( item->album()->artist()->name(), Qt::ElideRight, textRect.width() - 3 );
painter->drawText( textRect, text, to );
// Calculate rect of artist on-hover button click area
m_artistNameRects[ index ] = r;
}
painter->restore();
}
bool
AlbumItemDelegate::editorEvent( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index )
{
Q_UNUSED( option );
if ( event->type() != QEvent::MouseButtonRelease &&
event->type() != QEvent::MouseMove &&
event->type() != QEvent::MouseButtonPress )
return false;
if ( m_artistNameRects.contains( index ) )
{
QMouseEvent* ev = static_cast< QMouseEvent* >( event );
QRect artistNameRect = m_artistNameRects[ index ];
if ( artistNameRect.contains( ev->pos() ) )
{
if ( event->type() == QEvent::MouseMove )
{
if ( m_hoveringOver != index )
{
QModelIndex old = m_hoveringOver;
m_hoveringOver = index;
emit updateIndex( old );
emit updateIndex( index );
}
return true;
}
else if ( event->type() == QEvent::MouseButtonRelease )
{
AlbumItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) );
if ( !item || item->album().isNull() || item->album()->artist().isNull() )
return false;
ViewManager::instance()->show( item->album()->artist() );
return true;
} else if ( event->type() == QEvent::MouseButtonPress )
{
// Stop the whole album from having a down click action as we just want the artist name to be clicked
return true;
}
}
}
if ( m_hoveringOver.isValid() )
{
QModelIndex old = m_hoveringOver;
m_hoveringOver = QPersistentModelIndex();
emit updateIndex( old );
}
return false;
}

View File

@ -23,6 +23,7 @@
#include "dllmacro.h"
class QEvent;
class AlbumProxyModel;
class DLLEXPORT AlbumItemDelegate : public QStyledItemDelegate
@ -36,13 +37,20 @@ protected:
void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const;
QSize sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const;
bool editorEvent( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index );
// QWidget* createEditor( QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const;
signals:
void updateIndex( const QModelIndex& idx );
private:
QAbstractItemView* m_view;
AlbumProxyModel* m_model;
mutable QHash< qint64, QPixmap > m_cache;
mutable QHash< QPersistentModelIndex, QRect > m_artistNameRects;
QPersistentModelIndex m_hoveringOver;
QPixmap m_shadowPixmap;
QPixmap m_defaultCover;
};

View File

@ -50,6 +50,7 @@ AlbumView::AlbumView( QWidget* parent )
setUniformItemSizes( true );
setSpacing( 16 );
setContentsMargins( 0, 0, 0, 0 );
setMouseTracking( true );
setResizeMode( Adjust );
setViewMode( IconMode );
@ -77,7 +78,9 @@ void
AlbumView::setProxyModel( AlbumProxyModel* model )
{
m_proxyModel = model;
setItemDelegate( new AlbumItemDelegate( this, m_proxyModel ) );
AlbumItemDelegate* del = new AlbumItemDelegate( this, m_proxyModel );
connect( del, SIGNAL( updateIndex( QModelIndex ) ), this, SLOT( update( QModelIndex ) ) );
setItemDelegate( del );
QListView::setModel( m_proxyModel );
}

View File

@ -434,6 +434,14 @@ drawBackgroundAndNumbers( QPainter* painter, const QString& text, const QRect& f
painter->drawText( figRect.adjusted( -5, 0, 6, 0 ), text, to );
}
void
drawQueryBackground( QPainter* p, const QPalette& palette, const QRect& r, qreal lightnessFactor )
{
p->setPen( palette.mid().color().lighter( lightnessFactor * 100 ) );
p->setBrush( palette.highlight().color().lighter( lightnessFactor * 100 ) );
p->drawRoundedRect( r, 4.0, 4.0 );
}
void
unmarginLayout( QLayout* layout )

View File

@ -26,6 +26,7 @@
#include <QtNetwork/QNetworkProxy>
#include <QtCore/QStringList>
#include <QtCore/QRect>
#include <QPalette>
#define RESPATH ":/data/"
@ -85,6 +86,7 @@ namespace TomahawkUtils
DLLEXPORT QPixmap createDragPixmap( MediaType type, int itemCount = 1 );
DLLEXPORT void drawBackgroundAndNumbers( QPainter* p, const QString& text, const QRect& rect );
DLLEXPORT void drawQueryBackground( QPainter* p, const QPalette& palette, const QRect& r, qreal lightnessFactor = 1 );
DLLEXPORT void unmarginLayout( QLayout* layout );

View File

@ -367,9 +367,7 @@ QueryLabel::paintEvent( QPaintEvent* event )
m_hoverType = Track;
}
p.setPen( palette().mid().color() );
p.setBrush( palette().highlight() );
p.drawRoundedRect( m_hoverArea, 4.0, 4.0 );
TomahawkUtils::drawQueryBackground( &p, palette(), m_hoverArea );
}
if ( elidedText != s || ( m_result.isNull() && m_query.isNull() ) )