From 1c6cbed1f5e91d19a7fc0eb26202307b411f26fc Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Thu, 20 Dec 2012 11:27:31 +0100 Subject: [PATCH] * Add an auto-resize mode for TrackView. --- src/libtomahawk/playlist/TrackView.cpp | 54 ++++++++++++++++++++++++-- src/libtomahawk/playlist/TrackView.h | 7 ++++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/libtomahawk/playlist/TrackView.cpp b/src/libtomahawk/playlist/TrackView.cpp index 4edd4a890..09a0e9040 100644 --- a/src/libtomahawk/playlist/TrackView.cpp +++ b/src/libtomahawk/playlist/TrackView.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include "ViewHeader.h" #include "ViewManager.h" @@ -75,6 +76,7 @@ TrackView::TrackView( QWidget* parent ) setVerticalScrollMode( QAbstractItemView::ScrollPerPixel ); setRootIsDecorated( false ); setUniformRowHeights( true ); + setAutoResize( false ); setHeader( m_header ); setSortingEnabled( true ); @@ -144,8 +146,21 @@ TrackView::setGuid( const QString& newguid ) void TrackView::setProxyModel( PlayableProxyModel* model ) { + if ( m_proxyModel ) + { + disconnect( m_proxyModel, SIGNAL( filterChanged( QString ) ), this, SLOT( onFilterChanged( QString ) ) ); + disconnect( m_proxyModel, SIGNAL( rowsInserted( QModelIndex, int, int ) ), this, SLOT( onViewChanged() ) ); + disconnect( m_proxyModel, SIGNAL( rowsInserted( QModelIndex, int, int ) ), this, SLOT( verifySize() ) ); + disconnect( m_proxyModel, SIGNAL( rowsRemoved( QModelIndex, int, int ) ), this, SLOT( verifySize() ) ); + } + m_proxyModel = model; + connect( m_proxyModel, SIGNAL( filterChanged( QString ) ), SLOT( onFilterChanged( QString ) ) ); + connect( m_proxyModel, SIGNAL( rowsInserted( QModelIndex, int, int ) ), SLOT( onViewChanged() ) ); + connect( m_proxyModel, SIGNAL( rowsInserted( QModelIndex, int, int ) ), SLOT( verifySize() ) ); + connect( m_proxyModel, SIGNAL( rowsRemoved( QModelIndex, int, int ) ), SLOT( verifySize() ) ); + m_delegate = new PlaylistItemDelegate( this, m_proxyModel ); setItemDelegate( m_delegate ); @@ -162,19 +177,27 @@ TrackView::setModel( QAbstractItemModel* model ) } +void +TrackView::setPlaylistItemDelegate( PlaylistItemDelegate* delegate ) +{ + m_delegate = delegate; + setItemDelegate( delegate ); + connect( delegate, SIGNAL( updateIndex( QModelIndex ) ), SLOT( update( QModelIndex ) ) ); + + verifySize(); +} + + void TrackView::setPlayableModel( PlayableModel* model ) { - m_model = model; + m_model = model; if ( m_proxyModel ) { m_proxyModel->setSourcePlayableModel( m_model ); } - connect( m_proxyModel, SIGNAL( filterChanged( QString ) ), SLOT( onFilterChanged( QString ) ) ); - connect( m_proxyModel, SIGNAL( rowsInserted( QModelIndex, int, int ) ), SLOT( onViewChanged() ) ); - setAcceptDrops( true ); m_header->setDefaultColumnWeights( m_proxyModel->columnWeights() ); setGuid( m_proxyModel->guid() ); @@ -403,6 +426,8 @@ TrackView::resizeEvent( QResizeEvent* event ) { m_header->resizeSection( 0, event->size().width() ); } + + } @@ -807,3 +832,24 @@ TrackView::deleteSelectedItems() tDebug() << Q_FUNC_INFO << "Error: Model is read-only!"; } } + + +void +TrackView::verifySize() +{ + if ( !autoResize() || !m_proxyModel ) + return; + + if ( m_proxyModel->rowCount() > 0 ) + setFixedHeight( m_proxyModel->rowCount() * m_delegate->sizeHint( QStyleOptionViewItem(), m_proxyModel->index( 0, 0 ) ).height() + frameWidth() * 2 ); +} + + +void +TrackView::setAutoResize( bool b ) +{ + m_autoResize = b; + + if ( m_autoResize ) + setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); +} diff --git a/src/libtomahawk/playlist/TrackView.h b/src/libtomahawk/playlist/TrackView.h index b840c3576..7dc488e7a 100644 --- a/src/libtomahawk/playlist/TrackView.h +++ b/src/libtomahawk/playlist/TrackView.h @@ -48,6 +48,7 @@ public: virtual QString guid() const; virtual void setGuid( const QString& newguid ); + virtual void setPlaylistItemDelegate( PlaylistItemDelegate* delegate ); virtual void setPlayableModel( PlayableModel* model ); virtual void setModel( QAbstractItemModel* model ); void setProxyModel( PlayableProxyModel* model ); @@ -81,6 +82,9 @@ public: bool updatesContextView() const { return m_updateContextView; } void setUpdatesContextView( bool b ) { m_updateContextView = b; } + bool autoResize() const { return m_autoResize; } + void setAutoResize( bool b ); + // Starts playing from the beginning if resolved, or waits until a track is playable void startPlayingFromStart(); @@ -126,6 +130,8 @@ private slots: void autoPlayResolveFinished( const Tomahawk::query_ptr& query, int row ); + void verifySize(); + private: void startAutoPlay( const QModelIndex& index ); bool tryToPlayItem( const QModelIndex& index ); @@ -145,6 +151,7 @@ private: QRect m_dropRect; bool m_updateContextView; + bool m_autoResize; QModelIndex m_hoveredIndex; QModelIndex m_contextMenuIndex;