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

* Calculate margin once and cache it.

This commit is contained in:
Christian Muehlhaeuser
2014-09-01 04:02:32 +02:00
parent 76d44aeb4a
commit 1ac4236007
2 changed files with 29 additions and 39 deletions

View File

@@ -58,6 +58,7 @@ SourceDelegate::SourceDelegate( QAbstractItemView* parent )
: QStyledItemDelegate( parent )
, m_parent( parent )
, m_lastClicked( -1 )
, m_margin( TomahawkUtils::DpiScaler::scaledY( m_parent, 32 ) )
{
m_dropMimeData = new QMimeData();
}
@@ -114,8 +115,7 @@ SourceDelegate::paintStandardItem( QPainter* painter, const QStyleOptionViewItem
if ( !enabled )
iconMode = QIcon::Disabled;
const int margin = TomahawkUtils::DpiScaler::scaledY( m_parent, 32 );
QRect iconRect = opt.rect.adjusted( margin / 2, margin / 6, 0, -margin / 6 );
QRect iconRect = opt.rect.adjusted( m_margin / 2, m_margin / 6, 0, -m_margin / 6 );
iconRect.setWidth( iconRect.height() );
painter->drawPixmap( iconRect, opt.icon.pixmap( iconRect.size(), iconMode ) );
@@ -130,11 +130,11 @@ SourceDelegate::paintStandardItem( QPainter* painter, const QStyleOptionViewItem
if ( !count.isEmpty() )
{
figWidth = QFontMetrics( painter->font() ).width( count );
const QRect figRect = option.rect.adjusted( option.rect.width() - figWidth - margin / 2, 0, -margin / 2, 0 );
const QRect figRect = option.rect.adjusted( option.rect.width() - figWidth - m_margin / 2, 0, -m_margin / 2, 0 );
painter->drawText( figRect, count, QTextOption( Qt::AlignVCenter | Qt::AlignRight ) );
}
QRect textRect = opt.rect.adjusted( iconRect.width() + margin / 2 + margin / 4, 0, -margin - figWidth, 0 );
QRect textRect = opt.rect.adjusted( iconRect.width() + m_margin / 2 + m_margin / 4, 0, -m_margin - figWidth, 0 );
const QString text = painter->fontMetrics().elidedText( upperCase ? opt.text.toUpper() : opt.text, Qt::ElideRight, textRect.width() );
{
QTextOption to( Qt::AlignVCenter );
@@ -168,9 +168,8 @@ SourceDelegate::paintDecorations( QPainter* painter, const QStyleOptionViewItem&
if ( playable && playing && item->isBeingPlayed() )
{
const int margin = TomahawkUtils::DpiScaler::scaledY( m_parent, 32 );
const int iconW = option.rect.height() - margin / 4;
const QRect iconRect( margin / 4, option.rect.y() + margin / 8, iconW, iconW );
const int iconW = option.rect.height() - m_margin / 4;
const QRect iconRect( m_margin / 4, option.rect.y() + m_margin / 8, iconW, iconW );
const QPixmap speaker = TomahawkUtils::defaultPixmap( TomahawkUtils::NowPlayingSpeakerDark, TomahawkUtils::Original, iconRect.size() );
painter->drawPixmap( iconRect, speaker );
@@ -187,9 +186,8 @@ SourceDelegate::paintSource( QPainter* painter, const QStyleOptionViewItem& opti
SourceTreeItem* item = index.data( SourcesModel::SourceTreeItemRole ).value< SourceTreeItem* >();
SourcesModel::RowType type = static_cast< SourcesModel::RowType >( index.data( SourcesModel::SourceTreeItemTypeRole ).toInt() );
const int margin = TomahawkUtils::DpiScaler::scaledY( m_parent, 32 );
const int iconRectVertMargin = margin / 4;
const QRect iconRect = option.rect.adjusted( margin / 2 + margin / 4, iconRectVertMargin, -option.rect.width() + option.rect.height() + margin / 4, -iconRectVertMargin );
const int iconRectVertMargin = m_margin / 4;
const QRect iconRect = option.rect.adjusted( m_margin / 2 + m_margin / 4, iconRectVertMargin, -option.rect.width() + option.rect.height() + m_margin / 4, -iconRectVertMargin );
QString name = index.data().toString();
QPixmap avatar;
int figWidth = 0;
@@ -252,7 +250,7 @@ SourceDelegate::paintSource( QPainter* painter, const QStyleOptionViewItem& opti
painter->setOpacity( 1.0 );
painter->drawPixmap( iconRect, avatar );
QRect textRect = option.rect.adjusted( iconRect.width() + margin, margin / 4, -figWidth - ( figWidth ? margin : 0 ), 0 );
QRect textRect = option.rect.adjusted( iconRect.width() + m_margin, m_margin / 4, -figWidth - ( figWidth ? m_margin : 0 ), 0 );
QString text = painter->fontMetrics().elidedText( name, Qt::ElideRight, textRect.width() );
{
QTextOption to;
@@ -261,7 +259,7 @@ SourceDelegate::paintSource( QPainter* painter, const QStyleOptionViewItem& opti
painter->drawText( textRect, text, to );
}
textRect = option.rect.adjusted( iconRect.width() + margin, option.rect.height() / 2, -figWidth - ( figWidth ? margin : 0 ), -margin / 4 );
textRect = option.rect.adjusted( iconRect.width() + m_margin, option.rect.height() / 2, -figWidth - ( figWidth ? m_margin : 0 ), -m_margin / 4 );
if ( type == SourcesModel::Source )
{
@@ -274,7 +272,7 @@ SourceDelegate::paintSource( QPainter* painter, const QStyleOptionViewItem& opti
QRect pmRect = textRect;
pmRect.setRight( pmRect.left() + pmRect.height() );
ActionCollection::instance()->getAction( "togglePrivacy" )->icon().paint( painter, pmRect );
textRect.adjust( pmRect.width() + margin / 8, 0, 0, 0 );
textRect.adjust( pmRect.width() + m_margin / 8, 0, 0, 0 );
}
if ( ( isPlaying || ( !colItem->source().isNull() && colItem->source()->isLocal() ) ) && !shouldDrawDropHint )
{
@@ -302,7 +300,7 @@ SourceDelegate::paintSource( QPainter* painter, const QStyleOptionViewItem& opti
QRect pmRect = textRect;
pmRect.setRight( pmRect.left() + pmRect.height() );
painter->drawPixmap( pmRect, TomahawkUtils::defaultPixmap( listenAlongPixmap, TomahawkUtils::Original, pmRect.size() ) );
textRect.adjust( pmRect.width() + margin / 8, 0, 0, 0 );
textRect.adjust( pmRect.width() + m_margin / 8, 0, 0, 0 );
m_headphoneRects[ index ] = pmRect;
}
@@ -314,7 +312,7 @@ SourceDelegate::paintSource( QPainter* painter, const QStyleOptionViewItem& opti
QRect pmRect = textRect;
pmRect.setRight( pmRect.left() + pmRect.height() );
painter->drawPixmap( pmRect, TomahawkUtils::defaultPixmap( realtimeListeningAlongPixmap, TomahawkUtils::Original, pmRect.size() ) );
textRect.adjust( pmRect.width() + margin / 8, 0, 0, 0 );
textRect.adjust( pmRect.width() + m_margin / 8, 0, 0, 0 );
m_lockRects[ index ] = pmRect;
}
@@ -330,14 +328,14 @@ SourceDelegate::paintSource( QPainter* painter, const QStyleOptionViewItem& opti
font.setUnderline( true );
painter->setFont( font );
}
textRect.adjust( 0, 0, 0, margin / 16 );
textRect.adjust( 0, 0, 0, m_margin / 16 );
if ( shouldDrawDropHint )
{
desc = tr( "Drop to send tracks" );
}
text = painter->fontMetrics().elidedText( desc, Qt::ElideRight, textRect.width() - margin / 4 );
text = painter->fontMetrics().elidedText( desc, Qt::ElideRight, textRect.width() - m_margin / 4 );
{
QTextOption to( Qt::AlignVCenter );
to.setWrapMode( QTextOption::NoWrap );
@@ -373,7 +371,7 @@ SourceDelegate::paintSource( QPainter* painter, const QStyleOptionViewItem& opti
}
else
{
const QRect figRect = option.rect.adjusted( option.rect.width() - figWidth - margin / 2, 0, -margin / 2, 0 );
const QRect figRect = option.rect.adjusted( option.rect.width() - figWidth - m_margin / 2, 0, -m_margin / 2, 0 );
painter->drawText( figRect, tracks, QTextOption( Qt::AlignVCenter | Qt::AlignRight ) );
}
}
@@ -387,15 +385,13 @@ SourceDelegate::paintCategory( QPainter* painter, const QStyleOptionViewItem& op
{
painter->save();
const int margin = TomahawkUtils::DpiScaler::scaledY( m_parent, 32 );
QFont font = painter->font();
font.setPointSize( TomahawkUtils::defaultFontSize() - 1 );
painter->setFont( font );
painter->setPen( Qt::black );
painter->setOpacity( 0.5 );
painter->drawText( option.rect.translated( margin / 2, 0 ), index.data().toString().toUpper(), QTextOption( Qt::AlignVCenter ) );
painter->drawText( option.rect.translated( m_margin / 2, 0 ), index.data().toString().toUpper(), QTextOption( Qt::AlignVCenter ) );
if ( option.state & QStyle::State_MouseOver )
{
@@ -407,7 +403,7 @@ SourceDelegate::paintCategory( QPainter* painter, const QStyleOptionViewItem& op
// draw close icon
painter->setPen( TomahawkStyle::GROUP_HEADER );
painter->drawText( option.rect.translated( -margin / 8, 0 ), text, QTextOption( Qt::AlignVCenter | Qt::AlignRight ) );
painter->drawText( option.rect.translated( -m_margin / 8, 0 ), text, QTextOption( Qt::AlignVCenter | Qt::AlignRight ) );
}
painter->restore();
@@ -419,15 +415,13 @@ SourceDelegate::paintGroup( QPainter* painter, const QStyleOptionViewItem& optio
{
painter->save();
const int margin = TomahawkUtils::DpiScaler::scaledY( m_parent, 32 );
QFont font = painter->font();
font.setPointSize( TomahawkUtils::defaultFontSize() - 1 );
painter->setFont( font );
painter->setPen( Qt::black );
painter->setOpacity( 0.5 );
painter->drawText( option.rect.adjusted( margin, 0, -margin, -margin / 4 ), index.data().toString().toUpper(), QTextOption( Qt::AlignBottom ) );
painter->drawText( option.rect.adjusted( m_margin, 0, -m_margin, -m_margin / 4 ), index.data().toString().toUpper(), QTextOption( Qt::AlignBottom ) );
if ( option.state & QStyle::State_MouseOver )
{
@@ -439,7 +433,7 @@ SourceDelegate::paintGroup( QPainter* painter, const QStyleOptionViewItem& optio
// draw close icon
painter->setPen( TomahawkStyle::GROUP_HEADER );
painter->drawText( option.rect.translated( -margin / 8, -margin / 8 ), text, QTextOption( Qt::AlignBottom | Qt::AlignRight ) );
painter->drawText( option.rect.translated( -m_margin / 8, -m_margin / 8 ), text, QTextOption( Qt::AlignBottom | Qt::AlignRight ) );
}
painter->restore();
@@ -452,8 +446,6 @@ SourceDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, co
QStyleOptionViewItemV4 optIndentation = option;
QStyleOptionViewItemV4 opt = option;
const int margin = TomahawkUtils::DpiScaler::scaledY( m_parent, 32 );
painter->save();
painter->setRenderHint( QPainter::TextAntialiasing );
painter->setRenderHint( QPainter::SmoothPixmapTransform );
@@ -501,7 +493,7 @@ SourceDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, co
}
else if ( type == SourcesModel::Divider )
{
const QRect middle = optIndentation.rect.adjusted( 0, margin / 16, 0, -margin / 16 );
const QRect middle = optIndentation.rect.adjusted( 0, m_margin / 16, 0, -m_margin / 16 );
const QColor bgcolor = opt.palette.color( QPalette::Base );
painter->setPen( bgcolor.darker( 120 ) );
@@ -513,7 +505,7 @@ SourceDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, co
{
optIndentation.state &= ~QStyle::State_MouseOver;
if ( !index.parent().parent().isValid() )
optIndentation.rect.adjust( margin / 4, 0, 0, 0 );
optIndentation.rect.adjust( m_margin / 4, 0, 0, 0 );
if ( type == SourcesModel::Inbox || type == SourcesModel::Queue || type == SourcesModel::Collection )
{
@@ -547,7 +539,7 @@ SourceDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, co
paintStandardItem( painter, optIndentation, index );
// draw close icon
const QRect r( opt.rect.right() - margin / 8 - m_iconHeight, opt.rect.y() + ( opt.rect.height() - m_iconHeight ) / 2, m_iconHeight, m_iconHeight );
const QRect r( opt.rect.right() - m_margin / 8 - m_iconHeight, opt.rect.y() + ( opt.rect.height() - m_iconHeight ) / 2, m_iconHeight, m_iconHeight );
painter->drawPixmap( r, TomahawkUtils::defaultPixmap( TomahawkUtils::ListRemove, TomahawkUtils::Original, r.size() ) );
}
else
@@ -562,7 +554,7 @@ SourceDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, co
{
const int imgWidth = optIndentation.rect.height() / 2;
const QPixmap icon = plItem->subscribedIcon().scaled( imgWidth, imgWidth, Qt::KeepAspectRatio, Qt::SmoothTransformation );
const QRect subRect( optIndentation.rect.right() - margin / 2 - imgWidth, optIndentation.rect.top() + ( optIndentation.rect.height() - imgWidth ) / 2, imgWidth, imgWidth );
const QRect subRect( optIndentation.rect.right() - m_margin / 2 - imgWidth, optIndentation.rect.top() + ( optIndentation.rect.height() - imgWidth ) / 2, imgWidth, imgWidth );
painter->drawPixmap( subRect, icon );
}
@@ -587,14 +579,12 @@ SourceDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, co
void
SourceDelegate::updateEditorGeometry( QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
const int margin = TomahawkUtils::DpiScaler::scaledY( m_parent, 32 );
SourcesModel::RowType type = static_cast< SourcesModel::RowType >( index.data( SourcesModel::SourceTreeItemTypeRole ).toInt() );
if ( type == SourcesModel::StaticPlaylist ||
type == SourcesModel::AutomaticPlaylist ||
type == SourcesModel::Station )
{
QRect newGeometry = option.rect.adjusted( margin / 2 + margin / 4, 0, 0, 0 ); //room for the icon
QRect newGeometry = option.rect.adjusted( m_margin / 2 + m_margin / 4, 0, 0, 0 ); //room for the icon
#ifdef Q_OS_MAC
newGeometry.adjust( 3 * TREEVIEW_INDENT_ADD + 5, 0, 0, 0 ); //compensate for osx indentation
@@ -612,8 +602,6 @@ SourceDelegate::updateEditorGeometry( QWidget* editor, const QStyleOptionViewIte
bool
SourceDelegate::editorEvent( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index )
{
const int margin = TomahawkUtils::DpiScaler::scaledY( m_parent, 32 );
QMouseEvent* mEvent = 0;
switch ( event->type() )
{
@@ -680,7 +668,7 @@ SourceDelegate::editorEvent( QEvent* event, QAbstractItemModel* model, const QSt
QStyleOptionViewItemV4 o = option;
initStyleOption( &o, index );
const int padding = margin / 8;
const int padding = m_margin / 8;
const QRect r( o.rect.right() - padding - m_iconHeight, padding + o.rect.y(), m_iconHeight, m_iconHeight );
if ( r.contains( mEvent->pos() ) )
@@ -746,7 +734,7 @@ SourceDelegate::editorEvent( QEvent* event, QAbstractItemModel* model, const QSt
if ( plItem->canSubscribe() && !plItem->subscribedIcon().isNull() )
{
const int padding = margin / 16;
const int padding = m_margin / 16;
const int imgWidth = option.rect.height() - 2 * padding;
const QRect subRect( option.rect.right() - padding - imgWidth, option.rect.top() + padding, imgWidth, imgWidth );

View File

@@ -70,6 +70,8 @@ private:
mutable QHash< QPersistentModelIndex, QRect > m_trackRects;
mutable QHash< QPersistentModelIndex, QRect > m_headphoneRects;
mutable QHash< QPersistentModelIndex, QRect > m_lockRects;
const int m_margin;
};
#endif // SOURCEDELEGATE_H