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

Draw tiled background in FlexibleHeader as well as InfoBar

This commit is contained in:
Leo Franchi
2012-09-21 16:56:33 -04:00
parent cb6233bbfe
commit 0e9bb5ff8f
6 changed files with 79 additions and 45 deletions

View File

@@ -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;

View File

@@ -76,8 +76,6 @@ private slots:
void artistClicked();
private:
void createTile( int width = 2000 );
Ui::InfoBar* ui;
QPixmap m_bgTile;

View File

@@ -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() );
}

View File

@@ -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

View File

@@ -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

View File

@@ -25,6 +25,7 @@
#include <QColor>
#include <QRect>
#include <QTextOption>
#include <QImage>
#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