1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-19 23:39:42 +01:00

Self adjust item height in GridItemDelegate and GridView. We now only specify a width.

This commit is contained in:
Christian Muehlhaeuser 2014-11-04 15:26:27 +01:00
parent a0813f337f
commit 9100a8d5ff
4 changed files with 36 additions and 18 deletions

View File

@ -53,6 +53,7 @@ GridItemDelegate::GridItemDelegate( QAbstractItemView* parent, PlayableProxyMode
: QStyledItemDelegate( (QObject*)parent )
, m_view( parent )
, m_model( proxy )
, m_itemWidth( 0 )
, m_showPosition( false )
, m_margin( TomahawkUtils::DpiScaler::scaledY( parent, 32 ) )
{
@ -73,13 +74,29 @@ GridItemDelegate::GridItemDelegate( QAbstractItemView* parent, PlayableProxyMode
QSize
GridItemDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
if ( m_itemSize.isNull() )
if ( m_itemWidth == 0 )
{
QSize size = QStyledItemDelegate::sizeHint( option, index );
return size;
}
else
return m_itemSize;
{
PlayableItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) );
if ( !item || !index.isValid() )
return QStyledItemDelegate::sizeHint( option, index );
if ( item->artist() )
return QSize( m_itemWidth, m_itemWidth + option.fontMetrics.height() * 3 );
return QSize( m_itemWidth, m_itemWidth + option.fontMetrics.height() * 4.4 );
}
}
QSize
GridItemDelegate::itemSize() const
{
return sizeHint( QStyleOptionViewItem(), m_model->index( 0, 0 ) );
}
@ -217,9 +234,9 @@ GridItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option,
}
const QString fig = QString::number( index.row() + 1 );
painter->drawText( textRect, fig, QTextOption( Qt::AlignLeft | Qt::AlignTop ) );
painter->drawText( textRect, fig, QTextOption( Qt::AlignLeft | Qt::AlignVCenter ) );
textRect.adjust( painter->fontMetrics().boundingRect( textRect, Qt::AlignLeft | Qt::AlignTop, fig ).width() + m_margin / 4, 0, 0, 0 );
textRect.adjust( painter->fontMetrics().boundingRect( textRect, Qt::AlignLeft | Qt::AlignVCenter, fig ).width() + m_margin / 4, 0, 0, 0 );
painter->restore();
}

View File

@ -46,8 +46,8 @@ Q_OBJECT
public:
GridItemDelegate( QAbstractItemView* parent, PlayableProxyModel* proxy );
QSize itemSize() const { return m_itemSize; }
void setItemSize( const QSize& size ) { m_itemSize = size; }
QSize itemSize() const;
void setItemWidth( int width ) { m_itemWidth = width; }
public slots:
void resetHoverIndex();
@ -87,7 +87,7 @@ private:
QAbstractItemView* m_view;
PlayableProxyModel* m_model;
QSize m_itemSize;
int m_itemWidth;
bool m_showPosition;
mutable QHash< QPersistentModelIndex, QRect > m_artistNameRects;

View File

@ -59,6 +59,7 @@ GridView::GridView( QWidget* parent )
, m_overlay( new OverlayWidget( this ) )
, m_contextMenu( new ContextMenu( this ) )
, m_inited( false )
, m_itemWidth( 0 )
{
setFrameShape( QFrame::NoFrame );
setAttribute( Qt::WA_MacShowFocusRect, 0 );
@ -81,7 +82,7 @@ GridView::GridView( QWidget* parent )
setAutoFitItems( true );
setAutoResize( false );
setItemSize( TomahawkUtils::DpiScaler::scaled( this, QSize( 170, 170 + 56 ) ) );
setItemWidth( TomahawkUtils::DpiScaler::scaledX( this, 170 ) );
setProxyModel( new PlayableProxyModel( this ) );
m_timer.setInterval( SCROLL_TIMEOUT );
@ -129,7 +130,7 @@ GridView::setProxyModel( PlayableProxyModel* model )
m_delegate = new GridItemDelegate( this, m_proxyModel );
connect( m_delegate, SIGNAL( startedPlaying( QPersistentModelIndex ) ), this, SLOT( onDelegatePlaying( QPersistentModelIndex ) ) );
connect( m_delegate, SIGNAL( stoppedPlaying( QPersistentModelIndex ) ), this, SLOT( onDelegateStopped( QPersistentModelIndex ) ) );
m_delegate->setItemSize( m_itemSize );
m_delegate->setItemWidth( m_itemWidth );
setItemDelegate( m_delegate );
QListView::setModel( m_proxyModel );
@ -289,7 +290,7 @@ GridView::verifySize()
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 itemWidth = m_itemWidth + spacing();
const int itemsPerRow = qMax( 1, qFloor( rectWidth / itemWidth ) );
const int overlapRows = m_model->rowCount( QModelIndex() ) % itemsPerRow;
@ -322,14 +323,14 @@ GridView::layoutItems()
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 itemWidth = m_itemWidth + 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 + ( m_itemSize.height() - m_itemSize.width() ) ) );
m_delegate->setItemWidth( newItemWidth );
}
verifySize();
@ -496,11 +497,11 @@ GridView::setPlaylistInterface( const Tomahawk::playlistinterface_ptr& playlistI
void
GridView::setItemSize( const QSize& size )
GridView::setItemWidth( int width )
{
m_itemSize = size;
m_itemWidth = width;
if ( m_delegate )
m_delegate->setItemSize( m_itemSize );
m_delegate->setItemWidth( m_itemWidth );
layoutItems();
}

View File

@ -67,8 +67,8 @@ public:
void setEmptyTip( const QString& tip );
QSize itemSize() const { return m_itemSize; }
void setItemSize( const QSize& size );
int itemWidth() const { return m_itemWidth; }
void setItemWidth( int width );
virtual QWidget* widget() { return this; }
virtual Tomahawk::playlistinterface_ptr playlistInterface() const;
@ -135,7 +135,7 @@ private:
bool m_inited;
bool m_autoFitItems;
bool m_autoResize;
QSize m_itemSize;
int m_itemWidth;
QRect m_paintRect;
QTimer m_timer;