1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-23 09:19:41 +01:00

* Added BackgroundWidget, a widget that can display a (blurred) background image and thoughtfully reacts to size changes.

This commit is contained in:
Christian Muehlhaeuser 2014-08-12 19:52:15 +02:00
parent 16928602e0
commit 22a5e87979
2 changed files with 148 additions and 0 deletions

View File

@ -0,0 +1,99 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2014, Christian Muehlhaeuser <muesli@tomahawk-player.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#include "BackgroundWidget.h"
#include "utils/TomahawkUtilsGui.h"
#include "utils/Logger.h"
#include <QWidget>
#include <QPixmap>
#include <QPaintEvent>
#include <QPainter>
using namespace Tomahawk;
BackgroundWidget::BackgroundWidget( QWidget* parent )
: QWidget( parent )
{
setAutoFillBackground( false );
}
BackgroundWidget::~BackgroundWidget()
{
}
void
BackgroundWidget::setBackground( const QPixmap& p, bool blurred, bool blackWhite )
{
if ( blurred )
{
m_background = QPixmap::fromImage( TomahawkUtils::blurred( p.toImage(), p.rect(), 10, false, blackWhite ) );
}
else
{
m_background = p;
}
m_backgroundSlice = QPixmap();
repaint();
}
void
BackgroundWidget::setBackgroundColor( const QColor& c )
{
m_backgroundColor = c;
}
void
BackgroundWidget::resizeEvent( QResizeEvent* event )
{
QWidget::resizeEvent( event );
m_backgroundSlice = QPixmap();
}
void
BackgroundWidget::paintEvent( QPaintEvent* event )
{
QPainter painter( this );
painter.save();
painter.setPen( m_backgroundColor );
painter.setBrush( m_backgroundColor );
painter.drawRect( event->rect() );
painter.restore();
if ( m_backgroundSlice.isNull() && !m_background.isNull() )
{
m_backgroundSlice = m_background.scaledToWidth( contentsRect().width(), Qt::SmoothTransformation );
m_backgroundSlice = m_backgroundSlice.copy( 0, m_backgroundSlice.height() / 2 - contentsRect().height() / 2, m_backgroundSlice.width(), contentsRect().height() );
}
if ( !m_backgroundSlice.isNull() )
{
painter.drawPixmap( event->rect(), m_backgroundSlice.copy( event->rect() ) );
}
QWidget::paintEvent( event );
}

View File

@ -0,0 +1,49 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2014, Christian Muehlhaeuser <muesli@tomahawk-player.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BACKGROUNDWIDGET_H
#define BACKGROUNDWIDGET_H
#include <QWidget>
#include "DllMacro.h"
class QPaintEvent;
class DLLEXPORT BackgroundWidget : public QWidget
{
Q_OBJECT
public:
explicit BackgroundWidget( QWidget* parent = 0 );
virtual ~BackgroundWidget();
public slots:
virtual void setBackground( const QPixmap& p, bool blurred = true, bool blackWhite = false );
virtual void setBackgroundColor( const QColor& c );
protected:
virtual void resizeEvent( QResizeEvent* event );
virtual void paintEvent( QPaintEvent* event );
private:
QColor m_backgroundColor;
QPixmap m_background;
QPixmap m_backgroundSlice;
};
#endif // BACKGROUNDWIDGET_H