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

* Add an auto-resize mode for TrackView.

This commit is contained in:
Christian Muehlhaeuser 2012-12-20 11:27:31 +01:00
parent 0dca8c2abd
commit 1c6cbed1f5
2 changed files with 57 additions and 4 deletions

View File

@ -22,6 +22,7 @@
#include <QKeyEvent>
#include <QPainter>
#include <QScrollBar>
#include <QStyleOptionViewItem>
#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 );
}

View File

@ -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;