extragear/multimedia/amarok/src/context

Leo Franchi lfranchi at kde.org
Mon Apr 20 11:43:18 CEST 2009


SVN commit 956570 by lfranchi:

refactor code to eliminate a bunch of code copied around everywhere.
try with a flat no-gradient background with a thin border, going for
elegance over ritzyness. comments appreciated, here is what it looks
like here:

file:///home/leo/Desktop/amarok.cv.highlight.nogradient2.png

CCMAIL: amarok-devel at kde.org

 M  +58 -2     Applet.cpp  
 M  +5 -0      Applet.h  
 M  +4 -25     applets/albums/Albums.cpp  
 M  +1 -12     applets/currenttrack/CurrentTrack.cpp  
 M  +6 -39     applets/lyrics/LyricsApplet.cpp  
 M  +8 -34     applets/wikipedia/WikipediaApplet.cpp  


--- trunk/extragear/multimedia/amarok/src/context/Applet.cpp #956569:956570
@@ -13,11 +13,14 @@
 
 #include "Applet.h"
 
+#include "Amarok.h"
+
 #include <plasma/animator.h>
 
 #include <QGraphicsLayout>
 #include <QGraphicsScene>
 #include <QFontMetrics>
+#include <QPainter>
 
 namespace Context
 {
@@ -29,7 +32,8 @@
     , m_transient( 0 )
 {}
 
-QFont Context::Applet::shrinkTextSizeToFit( const QString& text, const QRectF& bounds )
+QFont
+Context::Applet::shrinkTextSizeToFit( const QString& text, const QRectF& bounds )
 {
     Q_UNUSED( text );
 
@@ -60,7 +64,8 @@
     return QFont( returnFont );
 }
 
-QString Context::Applet::truncateTextToFit( QString text, const QFont& font, const QRectF& bounds )
+QString
+Context::Applet::truncateTextToFit( QString text, const QFont& font, const QRectF& bounds )
 {
     QFontMetrics fm( font );
     return fm.elidedText ( text, Qt::ElideRight, (int)bounds.width() );
@@ -68,6 +73,57 @@
 }
 
 void
+Context::Applet::drawRoundedRectAroundText( QPainter* p, QGraphicsSimpleTextItem* t )
+{
+    p->save();
+    QColor topColor( 255, 255, 255, 120 );
+    QLinearGradient gradient2( t->boundingRect().topLeft(), t->boundingRect().bottomLeft() );
+    topColor.setAlpha( 120 );
+    gradient2.setColorAt( 0, topColor );
+    topColor.setAlpha( 200 );
+    gradient2.setColorAt( 1, topColor );
+    QPainterPath path = QPainterPath();
+    QRectF rect = t->boundingRect();
+    rect.moveTopLeft( t->pos() );
+    path.addRoundedRect( rect.adjusted( -5, -2, 5, 2 ), 5, 5 );
+    p->fillPath( path, gradient2 );
+    p->restore();
+}
+
+void
+Context::Applet::addGradientToAppletBackground( QPainter* p )
+{
+        // tint the whole applet
+/*    p->save();
+    QLinearGradient gradient( boundingRect().topLeft(), boundingRect().bottomLeft() );
+    QColor highlight = Amarok::highlightColor();
+    highlight.setAlpha( 80 );
+    gradient.setColorAt( 0, highlight );
+    highlight.setAlpha( 200 );
+    gradient.setColorAt( 1, highlight ); 
+    QPainterPath path;
+    path.addRoundedRect( boundingRect().adjusted( 1, 1, -1, -1 ), 3, 3 );
+    p->fillPath( path, gradient );
+    p->restore(); */
+
+    // draw non-gradient backround. going for elegance and style
+    p->save();
+    QPainterPath path;
+    path.addRoundedRect( boundingRect().adjusted( 0, 1, -1, -1 ), 3, 3 );
+    //p->fillPath( path, gradient );
+    QColor highlight = Amarok::highlightColor( 0.15, 1.05 );
+    highlight.setAlpha( 120 );
+    p->fillPath( path, highlight );
+    p->restore();
+
+    p->save();
+    QColor col = Amarok::highlightColor( 0.3, .8 );
+    p->setPen( col );
+    p->drawRoundedRect( boundingRect().adjusted( 2, 2, -2, -2 ), 3, 3 );
+    p->restore(); 
+}
+
+void
 Context::Applet::destroy()
 {
     if ( Plasma::Applet::immutability() != Plasma::Mutable || m_transient ) {
--- trunk/extragear/multimedia/amarok/src/context/Applet.h #956569:956570
@@ -22,6 +22,8 @@
 #include <QRectF>
 #include <QString>
 
+class QPainter;
+
 namespace Context
 {
 
@@ -34,6 +36,9 @@
         //helper functions
         QFont shrinkTextSizeToFit( const QString& text, const QRectF& bounds );
         QString truncateTextToFit( QString text, const QFont& font, const QRectF& bounds );
+
+        void drawRoundedRectAroundText( QPainter* p, QGraphicsSimpleTextItem* t );
+        void addGradientToAppletBackground( QPainter* p );
         
     public Q_SLOTS:
         virtual void destroy();
--- trunk/extragear/multimedia/amarok/src/context/applets/albums/Albums.cpp #956569:956570
@@ -215,32 +215,11 @@
 
     p->setRenderHint( QPainter::Antialiasing );
 
-    p->save();
-    QLinearGradient gradient( boundingRect().topLeft(), boundingRect().bottomLeft() );
-    QColor highlight = Amarok::highlightColor();
-    highlight.setAlpha( 80 );
-    gradient.setColorAt( 0, highlight );
-    highlight.setAlpha( 200 );
-    gradient.setColorAt( 1, highlight );
-    QPainterPath path;
-    path.addRoundedRect( boundingRect(), 3, 3 );
-    p->fillPath( path, gradient );
-    p->restore();
+    // tint the whole applet
+    addGradientToAppletBackground( p );
 
-         // draw rounded rect around title
-    p->save();
-    QColor topColor( 255, 255, 255, 120 );
-    QLinearGradient gradient2( m_headerText->boundingRect().topLeft(), m_headerText->boundingRect().bottomLeft() );
-    topColor.setAlpha( 120 );
-    gradient2.setColorAt( 0, topColor );
-    topColor.setAlpha( 200 );
-    gradient2.setColorAt( 1, topColor );
-    path = QPainterPath();
-    QRectF titleRect = m_headerText->boundingRect();
-    titleRect.moveTopLeft( m_headerText->pos() );
-    path.addRoundedRect( titleRect.adjusted( -3, 0, 3, 0 ), 5, 5 );
-    p->fillPath( path, gradient2 );
-    p->restore();
+    // draw rounded rect around title
+    drawRoundedRectAroundText( p, m_headerText );
 
 }
 
--- trunk/extragear/multimedia/amarok/src/context/applets/currenttrack/CurrentTrack.cpp #956569:956570
@@ -372,18 +372,7 @@
     p->setRenderHint( QPainter::Antialiasing );
 
     // tint the whole applet
-    p->save();
-    QLinearGradient gradient( boundingRect().topLeft(), boundingRect().bottomLeft() );
-    QColor highlight = Amarok::highlightColor(); 
-    highlight.setAlpha( 40 );
-    gradient.setColorAt( 0, highlight );
-    highlight.setAlpha( 200 );
-    gradient.setColorAt( 1, highlight );
-    QPainterPath path;
-    path.addRoundedRect( boundingRect(), 5, 5 );
-    p->fillPath( path, gradient );
-
-    p->restore();
+    addGradientToAppletBackground( p );
     
     //bail out if there is no room to paint. Prevents crashes and really there is no sense in painting if the
     //context view has been minimized completely
--- trunk/extragear/multimedia/amarok/src/context/applets/lyrics/LyricsApplet.cpp #956569:956570
@@ -158,13 +158,13 @@
     QRectF rect = boundingRect();
     rect.setWidth( rect.width() - 30 );
     m_titleLabel->setText( truncateTextToFit( m_titleText, m_titleLabel->font(), rect ) );
-    m_titleLabel->setPos( (size().width() - m_titleLabel->boundingRect().width() ) / 2, 6 );
+    m_titleLabel->setPos( (size().width() - m_titleLabel->boundingRect().width() ) / 2, 9 );
     
     m_reloadIcon->setPos( QPointF( size().width() - m_reloadIcon->size().width() - 10, 10 ) );
     m_reloadIcon->show();
     
     //m_lyricsProxy->setPos( 0, m_reloadIcon->size().height() );
-    m_lyricsProxy->setPos( 6, m_titleLabel->pos().y() + m_titleLabel->boundingRect().height() + 3 );
+    m_lyricsProxy->setPos( 6, m_titleLabel->pos().y() + m_titleLabel->boundingRect().height() + 6 );
     QSize lyricsSize( size().width() - 12, boundingRect().height() - m_lyricsProxy->pos().y() - 6 );
     m_lyricsProxy->setMinimumSize( lyricsSize );
     m_lyricsProxy->setMaximumSize( lyricsSize );
@@ -256,52 +256,19 @@
     p->setRenderHint( QPainter::Antialiasing );
 
     // tint the whole applet
-    p->save();
-    QLinearGradient gradient( boundingRect().topLeft(), boundingRect().bottomLeft() );
-    QColor highlight = Amarok::highlightColor();
-    highlight.setAlpha( 40 );
-    gradient.setColorAt( 0, highlight );
-    highlight.setAlpha( 180 );
-    gradient.setColorAt( 1, highlight );
-    QPainterPath path;
-    path.addRoundedRect( boundingRect(), 3, 3 );
-    p->fillPath( path, gradient );
+    addGradientToAppletBackground( p );
 
-    p->restore();
-
     // draw rounded rect around title
-    p->save();
-    QColor topColor( 255, 255, 255, 120 );
-    QLinearGradient gradient2( m_titleLabel->boundingRect().topLeft(), m_titleLabel->boundingRect().bottomLeft() );
-    topColor.setAlpha( 120 );
-    gradient2.setColorAt( 0, topColor );
-    topColor.setAlpha( 200 );
-    gradient2.setColorAt( 1, topColor );
-    path = QPainterPath();
-    QRectF titleRect = m_titleLabel->boundingRect();
-    titleRect.moveTopLeft( m_titleLabel->pos() );
-    path.addRoundedRect( titleRect.adjusted( -3, 0, 3, 0 ), 5, 5 );
-    p->fillPath( path, gradient2 );
-    p->restore();
+    drawRoundedRectAroundText( p, m_titleLabel );
 
-    p->save();
-    highlight.darker( 300 );
-    p->setPen( highlight );
-    p->drawPath( path );
-    p->restore();
-
     //draw background of lyrics text
     p->save();
-    highlight = KGlobalSettings::activeTitleColor();
-    qreal saturation = highlight.saturationF();
-    saturation *= 0.3;
-    qreal value = highlight.valueF();
-    value *= 1.1;
+    QColor highlight = QColor( App::instance()->palette().highlight().color() );
     highlight.setHsvF( highlight.hueF(), 0.05, 1, highlight.alphaF() );
 
     QRectF lyricsRect = m_lyricsProxy->boundingRect();
     lyricsRect.moveTopLeft( m_lyricsProxy->pos() );
-    path = QPainterPath();
+    QPainterPath path;
     path.addRoundedRect( lyricsRect, 5, 5 );
     p->fillPath( path , highlight );
     p->restore();
--- trunk/extragear/multimedia/amarok/src/context/applets/wikipedia/WikipediaApplet.cpp #956569:956570
@@ -134,9 +134,9 @@
     float textWidth = m_wikipediaLabel->boundingRect().width();
     float offsetX =  ( boundingRect().width() - textWidth ) / 2;
 
-    m_wikipediaLabel->setPos( offsetX, 6 );
+    m_wikipediaLabel->setPos( offsetX, 9 );
 
-    m_webView->setPos( 6, m_wikipediaLabel->pos().y() + m_wikipediaLabel->boundingRect().height() + 3 );
+    m_webView->setPos( 6, m_wikipediaLabel->pos().y() + m_wikipediaLabel->boundingRect().height() + 6 );
     m_webView->resize( boundingRect().width() - 12, boundingRect().height() - m_webView->pos().y() - 6 );
 
     m_reloadIcon->setPos( size().width() - m_reloadIcon->size().width() - MARGIN, MARGIN );
@@ -188,37 +188,16 @@
     Q_UNUSED( contentsRect )
     p->setRenderHint( QPainter::Antialiasing );
 
-    p->save();
-    QLinearGradient gradient( boundingRect().topLeft(), boundingRect().bottomLeft() );
-    QColor highlight = Amarok::highlightColor();
-    highlight.setAlpha( 80 );
-    gradient.setColorAt( 0, highlight );
-    highlight.setAlpha( 200 );
-    gradient.setColorAt( 1, highlight );
-    QPainterPath path;
-    path.addRoundedRect( boundingRect(), 3, 3 );
-    p->fillPath( path, gradient );
-    p->restore();
+    addGradientToAppletBackground( p );
 
-        // draw rounded rect around title
-    p->save();
-    QColor topColor( 255, 255, 255, 120 );
-    QLinearGradient gradient2( m_wikipediaLabel->boundingRect().topLeft(), m_wikipediaLabel->boundingRect().bottomLeft() );
-    topColor.setAlpha( 120 );
-    gradient2.setColorAt( 0, topColor );
-    topColor.setAlpha( 200 );
-    gradient2.setColorAt( 1, topColor );
-    path = QPainterPath();
-    QRectF titleRect = m_wikipediaLabel->boundingRect();
-    titleRect.moveTopLeft( m_wikipediaLabel->pos() );
-    path.addRoundedRect( titleRect.adjusted( -3, 0, 3, 0 ), 5, 5 );
-    p->fillPath( path, gradient2 );
-    p->restore();
+    // draw rounded rect around title
+    drawRoundedRectAroundText( p, m_wikipediaLabel );
 
+
     //draw background of wiki text
     // is overwritten when we ahve a page, but when we don't the large expanse of background is weird
     p->save();
-    highlight = QColor( App::instance()->palette().highlight().color() );
+    QColor highlight = QColor( App::instance()->palette().highlight().color() );
     qreal saturation = highlight.saturationF();
     saturation *= 0.3;
     qreal value = highlight.valueF();
@@ -261,12 +240,7 @@
     QFile file( KStandardDirs::locate("data", "amarok/data/WikipediaCustomStyle.css" ) );
     if( file.open(QIODevice::ReadOnly | QIODevice::Text) )
     {
-        QColor highlight = KGlobalSettings::activeTitleColor();
-        qreal saturation = highlight.saturationF();
-        saturation *= 0.3;
-        qreal value = highlight.valueF();
-        value *= 1.1;
-        debug() << "value:" << highlight.valueF();
+        QColor highlight = QColor( App::instance()->palette().highlight().color() );
         highlight.setHsvF( highlight.hueF(), 0.05, 1, highlight.alphaF() );
         
         QString contents = QString( file.readAll() );


More information about the Amarok-devel mailing list