From 0e9bb5ff8f292fe1321683291562bef96dfc138a Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Fri, 21 Sep 2012 16:56:33 -0400 Subject: [PATCH] Draw tiled background in FlexibleHeader as well as InfoBar --- src/libtomahawk/infobar/InfoBar.cpp | 45 +-------------------- src/libtomahawk/infobar/InfoBar.h | 2 - src/libtomahawk/playlist/FlexibleHeader.cpp | 25 ++++++++++++ src/libtomahawk/playlist/FlexibleHeader.h | 4 ++ src/libtomahawk/utils/TomahawkUtilsGui.cpp | 45 +++++++++++++++++++++ src/libtomahawk/utils/TomahawkUtilsGui.h | 3 ++ 6 files changed, 79 insertions(+), 45 deletions(-) diff --git a/src/libtomahawk/infobar/InfoBar.cpp b/src/libtomahawk/infobar/InfoBar.cpp index cbfe0de66..9fb8234fa 100644 --- a/src/libtomahawk/infobar/InfoBar.cpp +++ b/src/libtomahawk/infobar/InfoBar.cpp @@ -98,7 +98,7 @@ InfoBar::InfoBar( QWidget* parent ) setAutoFillBackground( true ); setFixedHeight( 80 ); - createTile(); + m_bgTile = TomahawkUtils::createTiledPixmap( 2000, height(), QImage( RESPATH "images/playlist-header-tiled.png" ) ); connect( ViewManager::instance(), SIGNAL( filterAvailable( bool ) ), SLOT( setFilterAvailable( bool ) ) ); } @@ -282,54 +282,13 @@ InfoBar::onFilterEdited() emit filterTextChanged( m_searchWidget->text() ); } - -void -InfoBar::createTile( int w ) -{ - QImage tile = QImage( RESPATH "images/playlist-header-tiled.png" ); - - if ( tile.isNull() ) - return; - - if ( tile.height() < height() ) - { - // image must be at least as tall as we are - QImage taller( tile.width(), height(), QImage::Format_ARGB32_Premultiplied ); - QPainter p( &taller ); - int curY = 0; - while ( curY < taller.height() ) - { - const int thisHeight = (curY + tile.height() > height()) ? height() - curY : tile.height(); - p.drawImage( QRect( 0, curY, tile.width(), thisHeight ), tile, QRect( 0, 0, tile.width(), thisHeight ) ); - curY += tile.height(); - } - tile = taller; - } - - m_bgTile = QPixmap( w, height() ); - m_bgTile.fill( Qt::transparent ); - - int curWidth = 0; - QPainter p( &m_bgTile ); - while ( curWidth < w ) - { - const int thisWidth = (curWidth + tile.width() > w) ? w - curWidth : tile.width(); - - const QRect source( 0, 0, thisWidth, m_bgTile.height() ); - const QRect dest( curWidth, 0, thisWidth, m_bgTile.height() ); - p.drawImage( dest, tile, source ); - curWidth += thisWidth; - } -} - - void InfoBar::paintEvent( QPaintEvent* e ) { Q_UNUSED( e ); if ( m_bgTile.isNull() || width() > m_bgTile.width() ) - createTile( width() ); + m_bgTile = TomahawkUtils::createTiledPixmap( width(), height(), QImage( RESPATH "images/playlist-header-tiled.png" ) ); if ( m_bgTile.isNull() ) return; diff --git a/src/libtomahawk/infobar/InfoBar.h b/src/libtomahawk/infobar/InfoBar.h index b69aed592..80ef6dbfa 100644 --- a/src/libtomahawk/infobar/InfoBar.h +++ b/src/libtomahawk/infobar/InfoBar.h @@ -76,8 +76,6 @@ private slots: void artistClicked(); private: - void createTile( int width = 2000 ); - Ui::InfoBar* ui; QPixmap m_bgTile; diff --git a/src/libtomahawk/playlist/FlexibleHeader.cpp b/src/libtomahawk/playlist/FlexibleHeader.cpp index c42f0399c..ff02994fd 100644 --- a/src/libtomahawk/playlist/FlexibleHeader.cpp +++ b/src/libtomahawk/playlist/FlexibleHeader.cpp @@ -36,6 +36,7 @@ using namespace Tomahawk; +QPixmap* FlexibleHeader::s_tiledHeader = 0; FlexibleHeader::FlexibleHeader( FlexibleView* parent ) : QWidget( parent ) @@ -78,6 +79,9 @@ FlexibleHeader::FlexibleHeader( FlexibleView* parent ) setPalette( pal ); setAutoFillBackground( true ); + if ( !s_tiledHeader ) + s_tiledHeader = new QPixmap( TomahawkUtils::createTiledPixmap( 2000, height(), QImage( RESPATH "images/playlist-header-tiled.png" ) ) ); + connect( &m_filterTimer, SIGNAL( timeout() ), SLOT( applyFilter() ) ); connect( ui->filter, SIGNAL( textChanged( QString ) ), SLOT( onFilterEdited() ) ); @@ -89,6 +93,8 @@ FlexibleHeader::FlexibleHeader( FlexibleView* parent ) FlexibleHeader::~FlexibleHeader() { + delete s_tiledHeader; + s_tiledHeader = 0; delete ui; } @@ -154,3 +160,22 @@ FlexibleHeader::changeEvent( QEvent* e ) break; } } + + +void +FlexibleHeader::paintEvent( QPaintEvent* ) +{ + if ( !s_tiledHeader || s_tiledHeader->isNull() || width() > s_tiledHeader->width() ) + { + delete s_tiledHeader; + s_tiledHeader = new QPixmap( TomahawkUtils::createTiledPixmap( width(), height(), QImage( RESPATH "images/playlist-header-tiled.png" ) ) ); + } + + if ( !s_tiledHeader || s_tiledHeader->isNull() ) + return; + + QPainter p( this ); + + // Truncate bg pixmap and paint into bg + p.drawPixmap( rect(), *s_tiledHeader, rect() ); +} diff --git a/src/libtomahawk/playlist/FlexibleHeader.h b/src/libtomahawk/playlist/FlexibleHeader.h index 623293cec..f538be364 100644 --- a/src/libtomahawk/playlist/FlexibleHeader.h +++ b/src/libtomahawk/playlist/FlexibleHeader.h @@ -25,6 +25,7 @@ #include "DllMacro.h" #include "Artist.h" +class QPaintEvent; class FlexibleView; namespace Ui @@ -52,6 +53,7 @@ signals: protected: void changeEvent( QEvent* e ); + void paintEvent( QPaintEvent* e ); private slots: void onFilterEdited(); @@ -63,6 +65,8 @@ private: QString m_filter; QTimer m_filterTimer; + + static QPixmap* s_tiledHeader; }; #endif diff --git a/src/libtomahawk/utils/TomahawkUtilsGui.cpp b/src/libtomahawk/utils/TomahawkUtilsGui.cpp index 8b7b62bc9..8f5ffb128 100644 --- a/src/libtomahawk/utils/TomahawkUtilsGui.cpp +++ b/src/libtomahawk/utils/TomahawkUtilsGui.cpp @@ -543,4 +543,49 @@ styleScrollBar( QScrollBar* scrollBar ) "border: 0px; width: 0px; height: 0px; background: none; background-color: transparent; }" ); } + +QPixmap +createTiledPixmap( int width, int height, const QImage& inputTile ) +{ + if ( inputTile.isNull() ) + return QPixmap(); + + + QImage localTile = inputTile; + + if ( localTile.height() < height ) + { + // image must be at least as tall as we are + QImage taller( localTile.width(), height, QImage::Format_ARGB32_Premultiplied ); + QPainter p( &taller ); + int curY = 0; + while ( curY < taller.height() ) + { + const int thisHeight = (curY + localTile.height() > height) ? height - curY : localTile.height(); + p.drawImage( QRect( 0, curY, localTile.width(), thisHeight ), localTile, QRect( 0, 0, localTile.width(), thisHeight ) ); + curY += localTile.height(); + } + localTile = taller; + } + + + QPixmap tiledImage = QPixmap( width, height ); + tiledImage.fill( Qt::transparent ); + + int curWidth = 0; + QPainter p( &tiledImage ); + while ( curWidth < width ) + { + const int thisWidth = (curWidth + localTile.width() > width) ? width - curWidth : localTile.width(); + + const QRect source( 0, 0, thisWidth, tiledImage.height() ); + const QRect dest( curWidth, 0, thisWidth, tiledImage.height() ); + p.drawImage( dest, localTile, source ); + curWidth += thisWidth; + } + + return tiledImage; +} + + } // ns diff --git a/src/libtomahawk/utils/TomahawkUtilsGui.h b/src/libtomahawk/utils/TomahawkUtilsGui.h index f66f14aff..2813ca68a 100644 --- a/src/libtomahawk/utils/TomahawkUtilsGui.h +++ b/src/libtomahawk/utils/TomahawkUtilsGui.h @@ -25,6 +25,7 @@ #include #include #include +#include #include "TomahawkUtils.h" #include "DllMacro.h" @@ -68,6 +69,8 @@ namespace TomahawkUtils DLLEXPORT void drawRoundedButton( QPainter* painter, const QRect& btnRect, const QColor& color, const QColor &gradient1bottom = QColor(), const QColor& gradient2top = QColor(), const QColor& gradient2bottom = QColor() ); DLLEXPORT void styleScrollBar( QScrollBar* scrollBar ); + + DLLEXPORT QPixmap createTiledPixmap( int width, int height, const QImage& src ); } #endif // TOMAHAWKUTILSGUI_H