mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-06 14:16:32 +02:00
* Add spacing to GridView and fixed size calculations for non-wrapping views.
This commit is contained in:
@@ -66,7 +66,7 @@ GridView::GridView( QWidget* parent )
|
|||||||
setDropIndicatorShown( false );
|
setDropIndicatorShown( false );
|
||||||
setDragDropOverwriteMode( false );
|
setDragDropOverwriteMode( false );
|
||||||
setUniformItemSizes( true );
|
setUniformItemSizes( true );
|
||||||
setSpacing( 0 );
|
setSpacing( 16 );
|
||||||
setContentsMargins( 0, 0, 0, 0 );
|
setContentsMargins( 0, 0, 0, 0 );
|
||||||
setMouseTracking( true );
|
setMouseTracking( true );
|
||||||
setContextMenuPolicy( Qt::CustomContextMenu );
|
setContextMenuPolicy( Qt::CustomContextMenu );
|
||||||
@@ -75,10 +75,11 @@ GridView::GridView( QWidget* parent )
|
|||||||
setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
|
setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
|
||||||
setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
|
setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
|
||||||
|
|
||||||
setStyleSheet( "QListView { background-color: #272b2e; }" );
|
setStyleSheet( "QListView { background-color: #ffffff; }" );
|
||||||
|
|
||||||
setAutoFitItems( true );
|
setAutoFitItems( true );
|
||||||
setAutoResize( false );
|
setAutoResize( false );
|
||||||
|
setItemSize( QSize( 170, 170 + 48 ) );
|
||||||
setProxyModel( new PlayableProxyModel( this ) );
|
setProxyModel( new PlayableProxyModel( this ) );
|
||||||
|
|
||||||
connect( this, SIGNAL( doubleClicked( QModelIndex ) ), SLOT( onItemActivated( QModelIndex ) ) );
|
connect( this, SIGNAL( doubleClicked( QModelIndex ) ), SLOT( onItemActivated( QModelIndex ) ) );
|
||||||
@@ -115,6 +116,7 @@ GridView::setProxyModel( PlayableProxyModel* model )
|
|||||||
m_delegate = new GridItemDelegate( this, m_proxyModel );
|
m_delegate = new GridItemDelegate( this, m_proxyModel );
|
||||||
connect( m_delegate, SIGNAL( startedPlaying( QPersistentModelIndex ) ), this, SLOT( onDelegatePlaying( QPersistentModelIndex ) ) );
|
connect( m_delegate, SIGNAL( startedPlaying( QPersistentModelIndex ) ), this, SLOT( onDelegatePlaying( QPersistentModelIndex ) ) );
|
||||||
connect( m_delegate, SIGNAL( stoppedPlaying( QPersistentModelIndex ) ), this, SLOT( onDelegateStopped( QPersistentModelIndex ) ) );
|
connect( m_delegate, SIGNAL( stoppedPlaying( QPersistentModelIndex ) ), this, SLOT( onDelegateStopped( QPersistentModelIndex ) ) );
|
||||||
|
m_delegate->setItemSize( m_itemSize );
|
||||||
setItemDelegate( m_delegate );
|
setItemDelegate( m_delegate );
|
||||||
|
|
||||||
QListView::setModel( m_proxyModel );
|
QListView::setModel( m_proxyModel );
|
||||||
@@ -159,12 +161,10 @@ GridView::currentChanged( const QModelIndex& current, const QModelIndex& previou
|
|||||||
{
|
{
|
||||||
QListView::currentChanged( current, previous );
|
QListView::currentChanged( current, previous );
|
||||||
|
|
||||||
PlayableItem* item = m_model->itemFromIndex( m_proxyModel->mapToSource( current ) );
|
/* PlayableItem* item = m_model->itemFromIndex( m_proxyModel->mapToSource( current ) );
|
||||||
if ( item )
|
if ( item )
|
||||||
{
|
{
|
||||||
// if ( !item->album().isNull() )
|
}*/
|
||||||
// ViewManager::instance()->context()->setAlbum( item->album() );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -174,9 +174,6 @@ GridView::onItemActivated( const QModelIndex& index )
|
|||||||
PlayableItem* item = m_model->itemFromIndex( m_proxyModel->mapToSource( index ) );
|
PlayableItem* item = m_model->itemFromIndex( m_proxyModel->mapToSource( index ) );
|
||||||
if ( item )
|
if ( item )
|
||||||
{
|
{
|
||||||
// qDebug() << "Result activated:" << item->album()->tracks().first()->toString() << item->album()->tracks().first()->results().first()->url();
|
|
||||||
// APP->audioEngine()->playItem( item->album().data(), item->album()->tracks().first()->results().first() );
|
|
||||||
|
|
||||||
if ( !item->album().isNull() )
|
if ( !item->album().isNull() )
|
||||||
ViewManager::instance()->show( item->album() );
|
ViewManager::instance()->show( item->album() );
|
||||||
else if ( !item->artist().isNull() )
|
else if ( !item->artist().isNull() )
|
||||||
@@ -217,6 +214,7 @@ GridView::resizeEvent( QResizeEvent* event )
|
|||||||
{
|
{
|
||||||
QListView::resizeEvent( event );
|
QListView::resizeEvent( event );
|
||||||
layoutItems();
|
layoutItems();
|
||||||
|
verifySize();
|
||||||
|
|
||||||
emit resized();
|
emit resized();
|
||||||
}
|
}
|
||||||
@@ -246,18 +244,53 @@ GridView::verifySize()
|
|||||||
if ( rect().width() - contentsRect().width() > scrollbar ) //HACK: if the contentsRect includes the scrollbar
|
if ( rect().width() - contentsRect().width() > scrollbar ) //HACK: if the contentsRect includes the scrollbar
|
||||||
scrollbar = 0; //don't count it any more
|
scrollbar = 0; //don't count it any more
|
||||||
|
|
||||||
const int rectWidth = contentsRect().width() - scrollbar - 3;
|
const int rectWidth = contentsRect().width() - scrollbar - 3 - spacing();
|
||||||
const int itemWidth = 160;
|
const int itemWidth = m_itemSize.width() + spacing();
|
||||||
const int itemsPerRow = qMax( 1, qFloor( rectWidth / itemWidth ) );
|
const int itemsPerRow = qMax( 1, qFloor( rectWidth / itemWidth ) );
|
||||||
|
|
||||||
const int overlapRows = m_model->rowCount( QModelIndex() ) % itemsPerRow;
|
const int overlapRows = m_model->rowCount( QModelIndex() ) % itemsPerRow;
|
||||||
const int rows = qMax( 1.0, floor( (double)m_model->rowCount( QModelIndex() ) / (double)itemsPerRow ) );
|
const int rows = qMax( 1.0, floor( (double)m_model->rowCount( QModelIndex() ) / (double)itemsPerRow ) );
|
||||||
const int newHeight = rows * m_delegate->itemSize().height();
|
const int newHeight = rows * ( m_delegate->itemSize().height() + spacing() );
|
||||||
|
|
||||||
m_proxyModel->setMaxVisibleItems( m_model->rowCount( QModelIndex() ) - overlapRows );
|
if ( !isWrapping() )
|
||||||
|
{
|
||||||
|
m_proxyModel->setMaxVisibleItems( qMin( itemsPerRow, m_model->rowCount( QModelIndex() ) ) );
|
||||||
|
}
|
||||||
|
else if ( newHeight > 0 )
|
||||||
|
{
|
||||||
|
m_proxyModel->setMaxVisibleItems( m_model->rowCount( QModelIndex() ) - overlapRows );
|
||||||
|
setFixedHeight( newHeight + spacing() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( newHeight > 0 )
|
|
||||||
setFixedHeight( newHeight );
|
void
|
||||||
|
GridView::layoutItems()
|
||||||
|
{
|
||||||
|
if ( autoFitItems() && m_model )
|
||||||
|
{
|
||||||
|
int scrollbar = verticalScrollBar()->rect().width();
|
||||||
|
|
||||||
|
if ( rect().width() - contentsRect().width() >= scrollbar ) //HACK: if the contentsRect includes the scrollbar
|
||||||
|
scrollbar = 0; //don't count it any more
|
||||||
|
|
||||||
|
const int rectWidth = contentsRect().width() - scrollbar - 3 - spacing();
|
||||||
|
const int itemWidth = m_itemSize.width() + spacing();
|
||||||
|
const int itemsPerRow = qMax( 1, qFloor( rectWidth / itemWidth ) );
|
||||||
|
|
||||||
|
const int remSpace = rectWidth - ( itemsPerRow * itemWidth );
|
||||||
|
const int extraSpace = remSpace / itemsPerRow;
|
||||||
|
const int newItemWidth = itemWidth + extraSpace - spacing();
|
||||||
|
|
||||||
|
m_delegate->setItemSize( QSize( newItemWidth, newItemWidth + 32 ) );
|
||||||
|
verifySize();
|
||||||
|
|
||||||
|
if ( !m_inited )
|
||||||
|
{
|
||||||
|
m_inited = true;
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -276,36 +309,6 @@ GridView::onDelegateStopped( const QPersistentModelIndex& index )
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
GridView::layoutItems()
|
|
||||||
{
|
|
||||||
if ( autoFitItems() && m_model )
|
|
||||||
{
|
|
||||||
int scrollbar = verticalScrollBar()->rect().width();
|
|
||||||
|
|
||||||
if ( rect().width() - contentsRect().width() >= scrollbar ) //HACK: if the contentsRect includes the scrollbar
|
|
||||||
scrollbar = 0; //don't count it any more
|
|
||||||
|
|
||||||
const int rectWidth = contentsRect().width() - scrollbar - 3;
|
|
||||||
const int itemWidth = 160;
|
|
||||||
const int itemsPerRow = qMax( 1, qFloor( rectWidth / itemWidth ) );
|
|
||||||
|
|
||||||
const int remSpace = rectWidth - ( itemsPerRow * itemWidth );
|
|
||||||
const int extraSpace = remSpace / itemsPerRow;
|
|
||||||
const int newItemWidth = itemWidth + extraSpace;
|
|
||||||
|
|
||||||
m_delegate->setItemSize( QSize( newItemWidth, newItemWidth ) );
|
|
||||||
verifySize();
|
|
||||||
|
|
||||||
if ( !m_inited )
|
|
||||||
{
|
|
||||||
m_inited = true;
|
|
||||||
repaint();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
GridView::onFilterChanged( const QString& )
|
GridView::onFilterChanged( const QString& )
|
||||||
{
|
{
|
||||||
@@ -428,3 +431,14 @@ GridView::setPlaylistInterface( const Tomahawk::playlistinterface_ptr& playlistI
|
|||||||
{
|
{
|
||||||
proxyModel()->setPlaylistInterface( playlistInterface );
|
proxyModel()->setPlaylistInterface( playlistInterface );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
GridView::setItemSize( const QSize& size )
|
||||||
|
{
|
||||||
|
m_itemSize = size;
|
||||||
|
if ( m_delegate )
|
||||||
|
m_delegate->setItemSize( m_itemSize );
|
||||||
|
|
||||||
|
layoutItems();
|
||||||
|
}
|
||||||
|
@@ -64,6 +64,9 @@ public:
|
|||||||
|
|
||||||
void setEmptyTip( const QString& tip );
|
void setEmptyTip( const QString& tip );
|
||||||
|
|
||||||
|
QSize itemSize() const { return m_itemSize; }
|
||||||
|
void setItemSize( const QSize& size );
|
||||||
|
|
||||||
virtual QWidget* widget() { return this; }
|
virtual QWidget* widget() { return this; }
|
||||||
virtual Tomahawk::playlistinterface_ptr playlistInterface() const;
|
virtual Tomahawk::playlistinterface_ptr playlistInterface() const;
|
||||||
void setPlaylistInterface( const Tomahawk::playlistinterface_ptr& playlistInterface );
|
void setPlaylistInterface( const Tomahawk::playlistinterface_ptr& playlistInterface );
|
||||||
@@ -123,6 +126,7 @@ private:
|
|||||||
bool m_inited;
|
bool m_inited;
|
||||||
bool m_autoFitItems;
|
bool m_autoFitItems;
|
||||||
bool m_autoResize;
|
bool m_autoResize;
|
||||||
|
QSize m_itemSize;
|
||||||
|
|
||||||
QRect m_paintRect;
|
QRect m_paintRect;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user