diff --git a/src/libtomahawk/playlist/albumitemdelegate.cpp b/src/libtomahawk/playlist/albumitemdelegate.cpp index c81722e0e..e1d025c3b 100644 --- a/src/libtomahawk/playlist/albumitemdelegate.cpp +++ b/src/libtomahawk/playlist/albumitemdelegate.cpp @@ -31,6 +31,8 @@ #include "playlist/albumitem.h" #include "playlist/albumproxymodel.h" +#include +#include 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; +} diff --git a/src/libtomahawk/playlist/albumitemdelegate.h b/src/libtomahawk/playlist/albumitemdelegate.h index a5bb3a27a..37a7226da 100644 --- a/src/libtomahawk/playlist/albumitemdelegate.h +++ b/src/libtomahawk/playlist/albumitemdelegate.h @@ -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; }; diff --git a/src/libtomahawk/playlist/albumview.cpp b/src/libtomahawk/playlist/albumview.cpp index 16c1fc132..a077ffa4a 100644 --- a/src/libtomahawk/playlist/albumview.cpp +++ b/src/libtomahawk/playlist/albumview.cpp @@ -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 ); } diff --git a/src/libtomahawk/utils/tomahawkutils.cpp b/src/libtomahawk/utils/tomahawkutils.cpp index 31cd625f8..b4e64fedc 100644 --- a/src/libtomahawk/utils/tomahawkutils.cpp +++ b/src/libtomahawk/utils/tomahawkutils.cpp @@ -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 ) diff --git a/src/libtomahawk/utils/tomahawkutils.h b/src/libtomahawk/utils/tomahawkutils.h index 543a0907e..e8bcd23ce 100644 --- a/src/libtomahawk/utils/tomahawkutils.h +++ b/src/libtomahawk/utils/tomahawkutils.h @@ -26,6 +26,7 @@ #include #include #include +#include #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 ); diff --git a/src/libtomahawk/widgets/querylabel.cpp b/src/libtomahawk/widgets/querylabel.cpp index eed695ce0..78a95c3f2 100644 --- a/src/libtomahawk/widgets/querylabel.cpp +++ b/src/libtomahawk/widgets/querylabel.cpp @@ -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() ) )