1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-20 12:52:30 +02:00

Word-wrapping setting for GridItemDelegate so we can accurately determine the size before content got loaded.

This commit is contained in:
Christian Muehlhaeuser
2014-11-04 16:52:11 +01:00
parent 9e92cc9f7b
commit 056ff3b8ce
2 changed files with 25 additions and 14 deletions

View File

@@ -55,11 +55,17 @@ GridItemDelegate::GridItemDelegate( QAbstractItemView* parent, PlayableProxyMode
, m_model( proxy ) , m_model( proxy )
, m_itemWidth( 0 ) , m_itemWidth( 0 )
, m_showPosition( false ) , m_showPosition( false )
, m_wordWrapping( false )
, m_margin( TomahawkUtils::DpiScaler::scaledY( parent, 32 ) ) , m_margin( TomahawkUtils::DpiScaler::scaledY( parent, 32 ) )
{ {
if ( m_view && m_view->metaObject()->indexOfSignal( "modelChanged()" ) > -1 ) if ( m_view && m_view->metaObject()->indexOfSignal( "modelChanged()" ) > -1 )
connect( m_view, SIGNAL( modelChanged() ), this, SLOT( modelChanged() ) ); connect( m_view, SIGNAL( modelChanged() ), this, SLOT( modelChanged() ) );
QFont m_font = m_view->font();
QFont m_smallFont = m_font;
m_font.setPointSize( TomahawkUtils::defaultFontSize() + 2 );
m_smallFont.setPointSize( TomahawkUtils::defaultFontSize() );
connect( this, SIGNAL( updateIndex( QModelIndex ) ), parent, SLOT( update( QModelIndex ) ) ); connect( this, SIGNAL( updateIndex( QModelIndex ) ), parent, SLOT( update( QModelIndex ) ) );
connect( proxy, SIGNAL( rowsAboutToBeInserted( QModelIndex, int, int ) ), SLOT( modelChanged() ) ); connect( proxy, SIGNAL( rowsAboutToBeInserted( QModelIndex, int, int ) ), SLOT( modelChanged() ) );
@@ -81,14 +87,13 @@ GridItemDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelInde
} }
else else
{ {
PlayableItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) ); const QFontMetrics fm( m_font );
if ( !item || !index.isValid() ) const QFontMetrics fms( m_smallFont );
return QStyledItemDelegate::sizeHint( option, index );
if ( item->artist() ) if ( !m_wordWrapping )
return QSize( m_itemWidth, m_itemWidth + option.fontMetrics.height() * 3 ); return QSize( m_itemWidth, m_itemWidth + fm.height() + m_margin );
return QSize( m_itemWidth, m_itemWidth + option.fontMetrics.height() * 4.4 ); return QSize( m_itemWidth, m_itemWidth + fm.height() + fms.height() + m_margin );
} }
} }
@@ -107,6 +112,13 @@ GridItemDelegate::setShowPosition( bool enabled )
} }
void
GridItemDelegate::setWordWrapping( bool enabled )
{
m_wordWrapping = enabled;
}
void void
GridItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const GridItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{ {
@@ -207,18 +219,13 @@ GridItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option,
to.setWrapMode( QTextOption::NoWrap ); to.setWrapMode( QTextOption::NoWrap );
QString text; QString text;
QFont font = opt.font;
font.setPointSize( TomahawkUtils::defaultFontSize() + 2 );
QFont smallFont = font;
smallFont.setPointSize( TomahawkUtils::defaultFontSize() );
QRect textRect = option.rect.adjusted( 0, r.height() + m_margin / 4, 0, -m_margin / 2 + m_margin / 8 ); QRect textRect = option.rect.adjusted( 0, r.height() + m_margin / 4, 0, -m_margin / 2 + m_margin / 8 );
bool oneLiner = false; bool oneLiner = false;
if ( bottom.isEmpty() ) if ( bottom.isEmpty() )
oneLiner = true; oneLiner = true;
painter->setPen( TomahawkStyle::SELECTION_FOREGROUND ); painter->setPen( TomahawkStyle::SELECTION_FOREGROUND );
painter->setFont( font ); painter->setFont( m_font );
painter->setPen( Qt::black ); painter->setPen( Qt::black );
painter->setOpacity( 0.8 ); painter->setOpacity( 0.8 );
@@ -228,7 +235,7 @@ GridItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option,
if ( !oneLiner ) if ( !oneLiner )
{ {
QFont figFont = font; QFont figFont = m_font;
figFont.setPixelSize( textRect.height() - m_margin / 8 ); figFont.setPixelSize( textRect.height() - m_margin / 8 );
painter->setFont( figFont ); painter->setFont( figFont );
} }
@@ -280,7 +287,7 @@ GridItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option,
painter->restore(); painter->restore();
painter->setOpacity( 0.6 ); painter->setOpacity( 0.6 );
painter->setFont( smallFont ); painter->setFont( m_smallFont );
// If the user is hovering over an artist rect, underline the artist name // If the user is hovering over an artist rect, underline the artist name
if ( m_hoveringOverArtist == index ) if ( m_hoveringOverArtist == index )

View File

@@ -52,6 +52,7 @@ public:
public slots: public slots:
void resetHoverIndex(); void resetHoverIndex();
void setShowPosition( bool enabled ); void setShowPosition( bool enabled );
void setWordWrapping( bool enabled );
protected: protected:
void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const; void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const;
@@ -89,6 +90,7 @@ private:
PlayableProxyModel* m_model; PlayableProxyModel* m_model;
int m_itemWidth; int m_itemWidth;
bool m_showPosition; bool m_showPosition;
bool m_wordWrapping;
mutable QHash< QPersistentModelIndex, QRect > m_artistNameRects; mutable QHash< QPersistentModelIndex, QRect > m_artistNameRects;
mutable QHash< QPersistentModelIndex, QRect > m_albumNameRects; mutable QHash< QPersistentModelIndex, QRect > m_albumNameRects;
@@ -102,6 +104,8 @@ private:
mutable QHash< QPersistentModelIndex, HoverControls* > m_hoverControls; mutable QHash< QPersistentModelIndex, HoverControls* > m_hoverControls;
mutable QHash< QPersistentModelIndex, QTimeLine* > m_hoverFaders; mutable QHash< QPersistentModelIndex, QTimeLine* > m_hoverFaders;
QFont m_font;
QFont m_smallFont;
const int m_margin; const int m_margin;
}; };