From f5219525b4c60784f3bb24730279101d745fd4cf Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Thu, 14 Apr 2011 05:00:27 +0200 Subject: [PATCH] * Added alphaBlend method to TomahawkUtils. DRY. --- .../playlist/playlistitemdelegate.cpp | 18 +++++------------ src/libtomahawk/playlist/treeitemdelegate.cpp | 16 +++++++++++++-- src/libtomahawk/utils/tomahawkutils.cpp | 20 ++++++++++++++++--- src/libtomahawk/utils/tomahawkutils.h | 8 +++++--- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/libtomahawk/playlist/playlistitemdelegate.cpp b/src/libtomahawk/playlist/playlistitemdelegate.cpp index 174001fc7..e06bdb9c4 100644 --- a/src/libtomahawk/playlist/playlistitemdelegate.cpp +++ b/src/libtomahawk/playlist/playlistitemdelegate.cpp @@ -76,24 +76,16 @@ PlaylistItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& opti return; float opacity = 0.0; - painter->save(); if ( item->query()->results().count() ) opacity = item->query()->results().first()->score(); - QColor textcol, bgcol; - textcol = option.palette.color( QPalette::Foreground ); - bgcol = option.palette.color( QPalette::Background ); - opacity = qMax( (float)0.3, opacity ); - int r = textcol.red(), g = textcol.green(), b = textcol.blue(); - r = opacity * r + ( 1 - opacity ) * bgcol.red(); - g = opacity * g + ( 1 - opacity ) * bgcol.green(); - b = opacity * b + ( 1 - opacity ) * bgcol.blue(); - textcol = QColor( r, g, b ); + QColor textColor = TomahawkUtils::alphaBlend( option.palette.color( QPalette::Foreground ), option.palette.color( QPalette::Background ), opacity ); if ( item->isPlaying() ) { // painter->setRenderHint( QPainter::Antialiasing ); + painter->save(); { QRect r = option.rect.adjusted( 3, 0, 0, 0 ); @@ -120,18 +112,18 @@ PlaylistItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& opti painter->setPen( pen ); painter->drawRoundedRect( r, 3.0, 3.0 ); } + + painter->restore(); } else { if ( const QStyleOptionViewItem *vioption = qstyleoption_cast(&option)) { QStyleOptionViewItemV4 o( *vioption ); - o.palette.setColor( QPalette::Text, textcol ); + o.palette.setColor( QPalette::Text, textColor ); QStyledItemDelegate::paint( painter, o, index ); } else QStyledItemDelegate::paint( painter, option, index ); } - - painter->restore(); } diff --git a/src/libtomahawk/playlist/treeitemdelegate.cpp b/src/libtomahawk/playlist/treeitemdelegate.cpp index fa080c980..fcb453f73 100644 --- a/src/libtomahawk/playlist/treeitemdelegate.cpp +++ b/src/libtomahawk/playlist/treeitemdelegate.cpp @@ -66,7 +66,19 @@ TreeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, } else if ( !item->result().isNull() ) { - return QStyledItemDelegate::paint( painter, option, index ); + float opacity = item->result()->score(); + + opacity = qMax( (float)0.3, opacity ); + QColor textColor = TomahawkUtils::alphaBlend( option.palette.color( QPalette::Foreground ), option.palette.color( QPalette::Background ), opacity ); + + if ( const QStyleOptionViewItem *vioption = qstyleoption_cast(&option)) + { + QStyleOptionViewItemV4 o( *vioption ); + o.palette.setColor( QPalette::Text, textColor ); + return QStyledItemDelegate::paint( painter, o, index ); + } + else + return QStyledItemDelegate::paint( painter, option, index ); } QStyleOptionViewItemV4 opt = option; @@ -99,7 +111,7 @@ TreeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, text = painter->fontMetrics().elidedText( text, Qt::ElideRight, r.width() ); painter->drawText( r, text, to ); - painter->setFont( boldFont ); +// painter->setFont( boldFont ); painter->restore(); } diff --git a/src/libtomahawk/utils/tomahawkutils.cpp b/src/libtomahawk/utils/tomahawkutils.cpp index a551e39c6..e652f5e43 100644 --- a/src/libtomahawk/utils/tomahawkutils.cpp +++ b/src/libtomahawk/utils/tomahawkutils.cpp @@ -1,5 +1,5 @@ /* === This file is part of Tomahawk Player - === - * + * * Copyright 2010-2011, Christian Muehlhaeuser * * Tomahawk is free software: you can redistribute it and/or modify @@ -19,6 +19,7 @@ #include "tomahawkutils.h" #include +#include #include #include #include @@ -265,6 +266,19 @@ extensionToMimetype( const QString& extension ) } +QColor +alphaBlend( const QColor& colorFrom, const QColor& colorTo, float opacity ) +{ + opacity = qMax( (float)0.3, opacity ); + int r = colorFrom.red(), g = colorFrom.green(), b = colorFrom.blue(); + r = opacity * r + ( 1 - opacity ) * colorTo.red(); + g = opacity * g + ( 1 - opacity ) * colorTo.green(); + b = opacity * b + ( 1 - opacity ) * colorTo.blue(); + + return QColor( r, g, b ); +} + + QPixmap createDragPixmap( int itemCount ) { @@ -366,7 +380,7 @@ dnsResolver() { if( !s_dnsResolver ) s_dnsResolver = new DNSResolver(); - + return s_dnsResolver; } @@ -387,7 +401,7 @@ DNSResolver::resolve( QString &host, QString type ) { // For the moment, assume we are looking for XMPP... QString fullHost( "_xmpp-client._tcp." + host ); - + qDebug() << "Looking up SRV record for " << fullHost.toUtf8(); m_dnsSharedRequest->query( fullHost.toUtf8(), QJDns::Srv ); diff --git a/src/libtomahawk/utils/tomahawkutils.h b/src/libtomahawk/utils/tomahawkutils.h index 103e379ce..8490829f4 100644 --- a/src/libtomahawk/utils/tomahawkutils.h +++ b/src/libtomahawk/utils/tomahawkutils.h @@ -1,5 +1,5 @@ /* === This file is part of Tomahawk Player - === - * + * * Copyright 2010-2011, Christian Muehlhaeuser * * Tomahawk is free software: you can redistribute it and/or modify @@ -25,6 +25,7 @@ #define RESPATH ":/data/" +class QColor; class QDir; class QDateTime; class QString; @@ -70,11 +71,11 @@ namespace TomahawkUtils { QThread::sleep( secs ); } - static void msleep( unsigned long msecs ) + static void msleep( unsigned long msecs ) { QThread::msleep( msecs ); } - static void usleep( unsigned long usecs ) + static void usleep( unsigned long usecs ) { QThread::usleep( usecs ); } @@ -89,6 +90,7 @@ namespace TomahawkUtils DLLEXPORT QString filesizeToString( unsigned int size ); DLLEXPORT QString extensionToMimetype( const QString& extension ); + DLLEXPORT QColor alphaBlend( const QColor& colorFrom, const QColor& colorTo, float opacity ); DLLEXPORT QPixmap createDragPixmap( int itemCount = 1 ); DLLEXPORT QNetworkAccessManager* nam();