PageRenderTime 102ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 52ms app.codeStats 0ms

/src/libtomahawk/widgets/ImageButton.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 115 lines | 71 code | 26 blank | 18 comment | 1 complexity | eda6cba8c6aab17a9a8246fcb8bbda11 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2014, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *   Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
  5 *
  6 *   Tomahawk is free software: you can redistribute it and/or modify
  7 *   it under the terms of the GNU General Public License as published by
  8 *   the Free Software Foundation, either version 3 of the License, or
  9 *   (at your option) any later version.
 10 *
 11 *   Tomahawk is distributed in the hope that it will be useful,
 12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14 *   GNU General Public License for more details.
 15 *
 16 *   You should have received a copy of the GNU General Public License
 17 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20#include "ImageButton.h"
 21
 22#include "utils/Logger.h"
 23
 24#include <QPainter>
 25#include <QPaintEvent>
 26#include <QLayout>
 27#include <QPixmap>
 28#include <QIcon>
 29#include <QString>
 30
 31ImageButton::ImageButton( QWidget* parent )
 32    : QAbstractButton( parent )
 33{
 34}
 35
 36
 37ImageButton::ImageButton( const QPixmap& rest, QWidget* parent )
 38    : QAbstractButton( parent )
 39{
 40    init( rest );
 41}
 42
 43
 44ImageButton::ImageButton( const QString& path, QWidget* parent )
 45    : QAbstractButton( parent )
 46{
 47    init( QPixmap( path ) );
 48}
 49
 50
 51void
 52ImageButton::init( const QPixmap& p )
 53{
 54    setPixmap( p, QIcon::Off );
 55    m_sizeHint = p.size();
 56    updateGeometry();
 57}
 58
 59
 60void
 61ImageButton::setPixmap( const QString& path )
 62{
 63    init( QPixmap( path ) );
 64}
 65
 66
 67void
 68ImageButton::setPixmap( const QPixmap& pixmap )
 69{
 70    init( pixmap );
 71}
 72
 73
 74void
 75ImageButton::clear()
 76{
 77    setIcon( QIcon() );
 78}
 79
 80
 81void
 82ImageButton::paintEvent( QPaintEvent* event )
 83{
 84    QPainter p( this );
 85    p.setClipRect( event->rect() );
 86
 87    QIcon::Mode mode = isDown()
 88        ? QIcon::Selected
 89        : QIcon::Normal;
 90
 91    QIcon::State state = isChecked()
 92        ? QIcon::On
 93        : QIcon::Off;
 94
 95    if ( !isEnabled() )
 96        p.setOpacity( 0.4 );
 97
 98    icon().paint( &p, rect(), Qt::AlignCenter, mode, state );
 99}
100
101
102void
103ImageButton::setPixmap( const QString& path, const QIcon::State state, const QIcon::Mode mode )
104{
105    setPixmap( QPixmap( path ), state, mode );
106}
107
108
109void
110ImageButton::setPixmap( const QPixmap& p, const QIcon::State state, const QIcon::Mode mode )
111{
112    QIcon i = icon();
113    i.addPixmap( p, mode, state );
114    setIcon( i );
115}