PageRenderTime 36ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 12ms app.codeStats 0ms

/src/libtomahawk/widgets/ToggleButton.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 108 lines | 65 code | 25 blank | 18 comment | 1 complexity | 788afcf347c30456173037b836448eba MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *   Copyright 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 "ToggleButton.h"
 21
 22#include "widgets/HeaderLabel.h"
 23#include "utils/TomahawkStyle.h"
 24#include "utils/TomahawkUtilsGui.h"
 25
 26#include <QStylePainter>
 27#include <QStyleOptionButton>
 28
 29
 30ToggleButton::ToggleButton( QWidget* parent )
 31    : QLabel( parent )
 32    , m_checked( false )
 33{
 34    QFont f( font() );
 35    f.setBold( true );
 36    f.setPointSize( TomahawkUtils::defaultFontSize() + 1 );
 37
 38    setFont( f );
 39    setFixedHeight( QLabel::sizeHint().height() + 8 );
 40
 41    setCursor( Qt::PointingHandCursor );
 42}
 43
 44
 45ToggleButton::~ToggleButton()
 46{
 47}
 48
 49
 50void
 51ToggleButton::setText( const QString& s )
 52{
 53    QLabel::setText( s );
 54    setFixedWidth( fontMetrics().width( text() ) + 32 );
 55}
 56
 57
 58void
 59ToggleButton::mouseReleaseEvent( QMouseEvent* event )
 60{
 61    QFrame::mouseReleaseEvent( event );
 62
 63    m_checked ^= true;
 64    update();
 65
 66    emit clicked();
 67}
 68
 69
 70void
 71ToggleButton::paintEvent( QPaintEvent* event )
 72{
 73    Q_UNUSED( event );
 74
 75    QPainter p( this );
 76
 77    p.save();
 78    QRect r = contentsRect();
 79    TomahawkStyle::horizontalHeader( &p, r );
 80    p.restore();
 81
 82    p.save();
 83    p.setRenderHint( QPainter::Antialiasing );
 84    p.setPen( Qt::white );
 85
 86    {
 87        QRect highlightRect( r );
 88        highlightRect.adjust( 0, 2, 0, -3 );
 89
 90        if ( isChecked() )
 91        {
 92            p.setBrush( TomahawkStyle::TOGGLEBUTTON_HIGHLIGHT );
 93        }
 94        else
 95        {
 96            p.setBrush( TomahawkStyle::TOGGLEBUTTON_BACKGROUND );
 97        }
 98
 99        p.drawRoundedRect( highlightRect, 4.0, 4.0 );
100    }
101
102    QTextOption to( Qt::AlignCenter );
103    r.adjust( 8, 0, -8, 0 );
104    p.setBrush( TomahawkStyle::TOGGLEBUTTON_TEXT );
105    p.drawText( r, text(), to );
106
107    p.restore();
108}