/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. #include "ImageButton.h"
  20. #include "utils/Logger.h"
  21. #include <QPainter>
  22. #include <QPaintEvent>
  23. #include <QLayout>
  24. #include <QPixmap>
  25. #include <QIcon>
  26. #include <QString>
  27. ImageButton::ImageButton( QWidget* parent )
  28. : QAbstractButton( parent )
  29. {
  30. }
  31. ImageButton::ImageButton( const QPixmap& rest, QWidget* parent )
  32. : QAbstractButton( parent )
  33. {
  34. init( rest );
  35. }
  36. ImageButton::ImageButton( const QString& path, QWidget* parent )
  37. : QAbstractButton( parent )
  38. {
  39. init( QPixmap( path ) );
  40. }
  41. void
  42. ImageButton::init( const QPixmap& p )
  43. {
  44. setPixmap( p, QIcon::Off );
  45. m_sizeHint = p.size();
  46. updateGeometry();
  47. }
  48. void
  49. ImageButton::setPixmap( const QString& path )
  50. {
  51. init( QPixmap( path ) );
  52. }
  53. void
  54. ImageButton::setPixmap( const QPixmap& pixmap )
  55. {
  56. init( pixmap );
  57. }
  58. void
  59. ImageButton::clear()
  60. {
  61. setIcon( QIcon() );
  62. }
  63. void
  64. ImageButton::paintEvent( QPaintEvent* event )
  65. {
  66. QPainter p( this );
  67. p.setClipRect( event->rect() );
  68. QIcon::Mode mode = isDown()
  69. ? QIcon::Selected
  70. : QIcon::Normal;
  71. QIcon::State state = isChecked()
  72. ? QIcon::On
  73. : QIcon::Off;
  74. if ( !isEnabled() )
  75. p.setOpacity( 0.4 );
  76. icon().paint( &p, rect(), Qt::AlignCenter, mode, state );
  77. }
  78. void
  79. ImageButton::setPixmap( const QString& path, const QIcon::State state, const QIcon::Mode mode )
  80. {
  81. setPixmap( QPixmap( path ), state, mode );
  82. }
  83. void
  84. ImageButton::setPixmap( const QPixmap& p, const QIcon::State state, const QIcon::Mode mode )
  85. {
  86. QIcon i = icon();
  87. i.addPixmap( p, mode, state );
  88. setIcon( i );
  89. }