From 22a5e87979d58eca50eaa8220cd0189f2092d44c Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Tue, 12 Aug 2014 19:52:15 +0200 Subject: [PATCH] * Added BackgroundWidget, a widget that can display a (blurred) background image and thoughtfully reacts to size changes. --- src/libtomahawk/widgets/BackgroundWidget.cpp | 99 ++++++++++++++++++++ src/libtomahawk/widgets/BackgroundWidget.h | 49 ++++++++++ 2 files changed, 148 insertions(+) create mode 100644 src/libtomahawk/widgets/BackgroundWidget.cpp create mode 100644 src/libtomahawk/widgets/BackgroundWidget.h diff --git a/src/libtomahawk/widgets/BackgroundWidget.cpp b/src/libtomahawk/widgets/BackgroundWidget.cpp new file mode 100644 index 000000000..d84185e36 --- /dev/null +++ b/src/libtomahawk/widgets/BackgroundWidget.cpp @@ -0,0 +1,99 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2014, Christian Muehlhaeuser + * + * 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 . + */ + +#include "BackgroundWidget.h" + +#include "utils/TomahawkUtilsGui.h" +#include "utils/Logger.h" + +#include +#include +#include +#include + +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 ); +} diff --git a/src/libtomahawk/widgets/BackgroundWidget.h b/src/libtomahawk/widgets/BackgroundWidget.h new file mode 100644 index 000000000..0a4281898 --- /dev/null +++ b/src/libtomahawk/widgets/BackgroundWidget.h @@ -0,0 +1,49 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2014, Christian Muehlhaeuser + * + * 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 . + */ + +#ifndef BACKGROUNDWIDGET_H +#define BACKGROUNDWIDGET_H + +#include + +#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