1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-22 16:59:58 +01: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 );
}
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 )
{
@ -160,7 +164,7 @@ AlbumItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option,
#endif
}
painter->drawPixmap( r, cover.scaled( r.size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation ) );
painter->drawPixmap( r, cover );
if ( m_hoverIndex == index )
{
@ -263,7 +267,7 @@ AlbumItemDelegate::onPlayClicked( const QPersistentModelIndex& index )
spinner->fadeIn();
spinner->move( pos );
m_subWidgets[ index ] = spinner;
m_spinner[ index ] = spinner;
AlbumItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) );
if ( item )
@ -301,9 +305,23 @@ AlbumItemDelegate::editorEvent( QEvent* event, QAbstractItemModel* model, const
event->type() != QEvent::Leave )
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 ( !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 )
button->deleteLater();
@ -317,80 +335,63 @@ AlbumItemDelegate::editorEvent( QEvent* event, QAbstractItemModel* model, const
button->setContentsMargins( 0, 0, 0, 0 );
button->show();
_detail::Closure* closure = NewClosure( button, SIGNAL( clicked( bool ) ),
const_cast<AlbumItemDelegate*>(this), SLOT( onPlayClicked( QPersistentModelIndex ) ), QPersistentModelIndex( index ) );
NewClosure( button, SIGNAL( clicked( bool ) ),
const_cast<AlbumItemDelegate*>(this), SLOT( onPlayClicked( QPersistentModelIndex ) ), QPersistentModelIndex( index ) );
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 )
{
emit updateIndex( m_hoverIndex );
m_hoverIndex = index;
emit updateIndex( index );
}
event->accept();
return true;
}
QMouseEvent* ev = static_cast< QMouseEvent* >( event );
if ( m_artistNameRects.contains( index ) )
if ( hoveringArtist )
{
QRect artistNameRect = m_artistNameRects[ index ];
if ( artistNameRect.contains( ev->pos() ) )
if ( event->type() == QEvent::MouseButtonRelease )
{
if ( event->type() == QEvent::MouseMove )
{
if ( m_hoveringOver != index )
{
QModelIndex old = m_hoveringOver;
m_hoveringOver = index;
emit updateIndex( old );
emit updateIndex( index );
}
AlbumItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) );
if ( !item )
return false;
event->accept();
return true;
}
else if ( event->type() == QEvent::MouseButtonRelease )
{
AlbumItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) );
if ( !item )
return false;
if ( !item->query().isNull() )
ViewManager::instance()->show( Tomahawk::Artist::get( item->query()->artist() ) );
else if ( !item->album().isNull() && !item->album()->artist().isNull() )
ViewManager::instance()->show( item->album()->artist() );
if ( !item->query().isNull() )
ViewManager::instance()->show( Tomahawk::Artist::get( item->query()->artist() ) );
else if ( !item->album().isNull() && !item->album()->artist().isNull() )
ViewManager::instance()->show( item->album()->artist() );
event->accept();
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
event->accept();
return true;
}
event->accept();
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
event->accept();
return true;
}
}
whitespaceMouseEvent();
return false;
}
void
AlbumItemDelegate::whitespaceMouseEvent()
{
if ( m_hoveringOver.isValid() )
{
QModelIndex old = m_hoveringOver;
m_hoveringOver = QPersistentModelIndex();
emit updateIndex( old );
}
}
void
AlbumItemDelegate::modelChanged()
{
@ -404,9 +405,9 @@ AlbumItemDelegate::modelChanged()
foreach ( ImageButton* button, m_pauseButton )
button->deleteLater();
m_pauseButton.clear();
foreach ( QWidget* widget, m_subWidgets )
foreach ( QWidget* widget, m_spinner )
widget->deleteLater();
m_subWidgets.clear();
m_spinner.clear();
if ( AlbumView* view = qobject_cast< AlbumView* >( m_view ) )
m_model = view->proxyModel();
@ -425,7 +426,7 @@ AlbumItemDelegate::doUpdateIndex( const QPersistentModelIndex& idx )
void
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 ) );
}
@ -488,15 +489,15 @@ AlbumItemDelegate::onPlaylistChanged( const QPersistentModelIndex& index )
void
AlbumItemDelegate::onPlaybackStarted( const QPersistentModelIndex& index )
{
if ( !m_subWidgets.contains( index ) )
if ( !m_spinner.contains( index ) )
return;
QPoint pos = m_subWidgets[ index ]->pos();
foreach ( QWidget* widget, m_subWidgets.values() )
QPoint pos = m_spinner[ index ]->pos();
foreach ( QWidget* widget, m_spinner.values() )
{
delete widget;
}
m_subWidgets.clear();
m_spinner.clear();
ImageButton* button = new ImageButton( m_view );
button->setPixmap( RESPATH "images/pause-rest.png" );

View File

@ -39,8 +39,6 @@ Q_OBJECT
public:
AlbumItemDelegate( QAbstractItemView* parent = 0, AlbumProxyModel* proxy = 0 );
void whitespaceMouseEvent();
protected:
void paint( QPainter* painter, 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, QSharedPointer< Tomahawk::PixmapDelegateFader > > m_covers;
QPersistentModelIndex m_hoveringOver;
QPersistentModelIndex m_hoverIndex;
QPersistentModelIndex m_hoveringOver;
mutable QRect m_playButtonRect;
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_pauseButton;
};

View File

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