/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. #include "ToggleButton.h"
  20. #include "widgets/HeaderLabel.h"
  21. #include "utils/TomahawkStyle.h"
  22. #include "utils/TomahawkUtilsGui.h"
  23. #include <QStylePainter>
  24. #include <QStyleOptionButton>
  25. ToggleButton::ToggleButton( QWidget* parent )
  26. : QLabel( parent )
  27. , m_checked( false )
  28. {
  29. QFont f( font() );
  30. f.setBold( true );
  31. f.setPointSize( TomahawkUtils::defaultFontSize() + 1 );
  32. setFont( f );
  33. setFixedHeight( QLabel::sizeHint().height() + 8 );
  34. setCursor( Qt::PointingHandCursor );
  35. }
  36. ToggleButton::~ToggleButton()
  37. {
  38. }
  39. void
  40. ToggleButton::setText( const QString& s )
  41. {
  42. QLabel::setText( s );
  43. setFixedWidth( fontMetrics().width( text() ) + 32 );
  44. }
  45. void
  46. ToggleButton::mouseReleaseEvent( QMouseEvent* event )
  47. {
  48. QFrame::mouseReleaseEvent( event );
  49. m_checked ^= true;
  50. update();
  51. emit clicked();
  52. }
  53. void
  54. ToggleButton::paintEvent( QPaintEvent* event )
  55. {
  56. Q_UNUSED( event );
  57. QPainter p( this );
  58. p.save();
  59. QRect r = contentsRect();
  60. TomahawkStyle::horizontalHeader( &p, r );
  61. p.restore();
  62. p.save();
  63. p.setRenderHint( QPainter::Antialiasing );
  64. p.setPen( Qt::white );
  65. {
  66. QRect highlightRect( r );
  67. highlightRect.adjust( 0, 2, 0, -3 );
  68. if ( isChecked() )
  69. {
  70. p.setBrush( TomahawkStyle::TOGGLEBUTTON_HIGHLIGHT );
  71. }
  72. else
  73. {
  74. p.setBrush( TomahawkStyle::TOGGLEBUTTON_BACKGROUND );
  75. }
  76. p.drawRoundedRect( highlightRect, 4.0, 4.0 );
  77. }
  78. QTextOption to( Qt::AlignCenter );
  79. r.adjust( 8, 0, -8, 0 );
  80. p.setBrush( TomahawkStyle::TOGGLEBUTTON_TEXT );
  81. p.drawText( r, text(), to );
  82. p.restore();
  83. }