1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-08 07:07:05 +02:00

* Cleaned up AlbumItemDelegate and fixed a few smaller issues.

This commit is contained in:
Christian Muehlhaeuser
2012-05-20 12:30:21 +02:00
parent 4fdd6ddc45
commit 46fde05aa9
3 changed files with 65 additions and 69 deletions

View File

@@ -140,7 +140,11 @@ AlbumItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option,
closure->setAutoDelete( false ); closure->setAutoDelete( false );
} }
const QPixmap cover = m_covers[ index ]->currentPixmap(); QSharedPointer< Tomahawk::PixmapDelegateFader > fader = m_covers[ index ];
if ( fader->size() != r.size() )
fader->setSize( r.size() );
const QPixmap cover = fader->currentPixmap();
if ( false && option.state & QStyle::State_Selected ) if ( false && option.state & QStyle::State_Selected )
{ {
@@ -160,7 +164,7 @@ AlbumItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option,
#endif #endif
} }
painter->drawPixmap( r, cover.scaled( r.size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation ) ); painter->drawPixmap( r, cover );
if ( m_hoverIndex == index ) if ( m_hoverIndex == index )
{ {
@@ -263,7 +267,7 @@ AlbumItemDelegate::onPlayClicked( const QPersistentModelIndex& index )
spinner->fadeIn(); spinner->fadeIn();
spinner->move( pos ); spinner->move( pos );
m_subWidgets[ index ] = spinner; m_spinner[ index ] = spinner;
AlbumItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) ); AlbumItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) );
if ( item ) if ( item )
@@ -301,9 +305,23 @@ AlbumItemDelegate::editorEvent( QEvent* event, QAbstractItemModel* model, const
event->type() != QEvent::Leave ) event->type() != QEvent::Leave )
return false; return false;
bool hoveringArtist = false;
if ( m_artistNameRects.contains( index ) )
{
QRect artistNameRect = m_artistNameRects[ index ];
QMouseEvent* ev = static_cast< QMouseEvent* >( event );
hoveringArtist = artistNameRect.contains( ev->pos() );
}
if ( event->type() == QEvent::MouseMove ) if ( event->type() == QEvent::MouseMove )
{ {
if ( !m_playButton.contains( index ) && !m_subWidgets.contains( index ) && !m_pauseButton.contains( index ) ) foreach ( const QModelIndex& idx, m_playButton.keys() )
{
if ( index != idx )
m_playButton.take( idx )->deleteLater();
}
if ( !m_playButton.contains( index ) && !m_spinner.contains( index ) && !m_pauseButton.contains( index ) )
{ {
foreach ( ImageButton* button, m_playButton ) foreach ( ImageButton* button, m_playButton )
button->deleteLater(); button->deleteLater();
@@ -317,40 +335,38 @@ AlbumItemDelegate::editorEvent( QEvent* event, QAbstractItemModel* model, const
button->setContentsMargins( 0, 0, 0, 0 ); button->setContentsMargins( 0, 0, 0, 0 );
button->show(); button->show();
_detail::Closure* closure = NewClosure( button, SIGNAL( clicked( bool ) ), NewClosure( button, SIGNAL( clicked( bool ) ),
const_cast<AlbumItemDelegate*>(this), SLOT( onPlayClicked( QPersistentModelIndex ) ), QPersistentModelIndex( index ) ); const_cast<AlbumItemDelegate*>(this), SLOT( onPlayClicked( QPersistentModelIndex ) ), QPersistentModelIndex( index ) );
m_playButton[ index ] = button; m_playButton[ index ] = button;
} }
if ( m_hoveringOver != index || ( !hoveringArtist && m_hoveringOver.isValid() ) )
{
emit updateIndex( m_hoveringOver );
if ( hoveringArtist )
m_hoveringOver = index;
else
m_hoveringOver = QPersistentModelIndex();
emit updateIndex( index );
}
if ( m_hoverIndex != index ) if ( m_hoverIndex != index )
{ {
emit updateIndex( m_hoverIndex ); emit updateIndex( m_hoverIndex );
m_hoverIndex = index; m_hoverIndex = index;
emit updateIndex( index ); emit updateIndex( index );
} }
}
QMouseEvent* ev = static_cast< QMouseEvent* >( event );
if ( m_artistNameRects.contains( index ) )
{
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 );
}
event->accept(); event->accept();
return true; return true;
} }
else if ( event->type() == QEvent::MouseButtonRelease )
if ( hoveringArtist )
{
if ( event->type() == QEvent::MouseButtonRelease )
{ {
AlbumItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) ); AlbumItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) );
if ( !item ) if ( !item )
@@ -371,26 +387,11 @@ AlbumItemDelegate::editorEvent( QEvent* event, QAbstractItemModel* model, const
return true; return true;
} }
} }
}
whitespaceMouseEvent();
return false; return false;
} }
void
AlbumItemDelegate::whitespaceMouseEvent()
{
if ( m_hoveringOver.isValid() )
{
QModelIndex old = m_hoveringOver;
m_hoveringOver = QPersistentModelIndex();
emit updateIndex( old );
}
}
void void
AlbumItemDelegate::modelChanged() AlbumItemDelegate::modelChanged()
{ {
@@ -404,9 +405,9 @@ AlbumItemDelegate::modelChanged()
foreach ( ImageButton* button, m_pauseButton ) foreach ( ImageButton* button, m_pauseButton )
button->deleteLater(); button->deleteLater();
m_pauseButton.clear(); m_pauseButton.clear();
foreach ( QWidget* widget, m_subWidgets ) foreach ( QWidget* widget, m_spinner )
widget->deleteLater(); widget->deleteLater();
m_subWidgets.clear(); m_spinner.clear();
if ( AlbumView* view = qobject_cast< AlbumView* >( m_view ) ) if ( AlbumView* view = qobject_cast< AlbumView* >( m_view ) )
m_model = view->proxyModel(); m_model = view->proxyModel();
@@ -425,7 +426,7 @@ AlbumItemDelegate::doUpdateIndex( const QPersistentModelIndex& idx )
void void
AlbumItemDelegate::onScrolled( int dx, int dy ) AlbumItemDelegate::onScrolled( int dx, int dy )
{ {
foreach ( QWidget* widget, m_subWidgets.values() ) foreach ( QWidget* widget, m_spinner.values() )
{ {
widget->move( widget->pos() + QPoint( dx, dy ) ); widget->move( widget->pos() + QPoint( dx, dy ) );
} }
@@ -488,15 +489,15 @@ AlbumItemDelegate::onPlaylistChanged( const QPersistentModelIndex& index )
void void
AlbumItemDelegate::onPlaybackStarted( const QPersistentModelIndex& index ) AlbumItemDelegate::onPlaybackStarted( const QPersistentModelIndex& index )
{ {
if ( !m_subWidgets.contains( index ) ) if ( !m_spinner.contains( index ) )
return; return;
QPoint pos = m_subWidgets[ index ]->pos(); QPoint pos = m_spinner[ index ]->pos();
foreach ( QWidget* widget, m_subWidgets.values() ) foreach ( QWidget* widget, m_spinner.values() )
{ {
delete widget; delete widget;
} }
m_subWidgets.clear(); m_spinner.clear();
ImageButton* button = new ImageButton( m_view ); ImageButton* button = new ImageButton( m_view );
button->setPixmap( RESPATH "images/pause-rest.png" ); button->setPixmap( RESPATH "images/pause-rest.png" );

View File

@@ -39,8 +39,6 @@ Q_OBJECT
public: public:
AlbumItemDelegate( QAbstractItemView* parent = 0, AlbumProxyModel* proxy = 0 ); AlbumItemDelegate( QAbstractItemView* parent = 0, AlbumProxyModel* proxy = 0 );
void whitespaceMouseEvent();
protected: protected:
void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const; void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const;
QSize sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const; QSize sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const;
@@ -68,12 +66,12 @@ private:
mutable QHash< QPersistentModelIndex, QRect > m_artistNameRects; mutable QHash< QPersistentModelIndex, QRect > m_artistNameRects;
mutable QHash< QPersistentModelIndex, QSharedPointer< Tomahawk::PixmapDelegateFader > > m_covers; mutable QHash< QPersistentModelIndex, QSharedPointer< Tomahawk::PixmapDelegateFader > > m_covers;
QPersistentModelIndex m_hoveringOver;
QPersistentModelIndex m_hoverIndex; QPersistentModelIndex m_hoverIndex;
QPersistentModelIndex m_hoveringOver;
mutable QRect m_playButtonRect; mutable QRect m_playButtonRect;
QPixmap m_shadowPixmap; QPixmap m_shadowPixmap;
mutable QHash< QPersistentModelIndex, QWidget* > m_subWidgets; mutable QHash< QPersistentModelIndex, QWidget* > m_spinner;
mutable QHash< QPersistentModelIndex, ImageButton* > m_playButton; mutable QHash< QPersistentModelIndex, ImageButton* > m_playButton;
mutable QHash< QPersistentModelIndex, ImageButton* > m_pauseButton; mutable QHash< QPersistentModelIndex, ImageButton* > m_pauseButton;
}; };

View File

@@ -109,9 +109,7 @@ QVariant
AlbumModel::data( const QModelIndex& index, int role ) const AlbumModel::data( const QModelIndex& index, int role ) const
{ {
if ( role == Qt::SizeHintRole ) if ( role == Qt::SizeHintRole )
{
return m_itemSize; return m_itemSize;
}
AlbumItem* entry = itemFromIndex( index ); AlbumItem* entry = itemFromIndex( index );
if ( !entry ) if ( !entry )
@@ -131,7 +129,6 @@ AlbumModel::data( const QModelIndex& index, int role ) const
case 0: case 0:
return name; return name;
break; break;
} }
return QVariant(); return QVariant();