/src/libtomahawk/widgets/ComboBox.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 93 lines · 60 code · 16 blank · 17 comment · 2 complexity · 65cf2f00f9a4d4d4dd148f75f968bbe2 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2011, Casey Link <unnamedrambler@gmail.com>
  4. *
  5. * Tomahawk is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Tomahawk is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "ComboBox.h"
  19. #include "utils/TomahawkStyle.h"
  20. #include "utils/TomahawkUtilsGui.h"
  21. #include "utils/Logger.h"
  22. #include <QStyle>
  23. #include <QTextOption>
  24. #include <QStylePainter>
  25. #include <QStyleOptionComboBox>
  26. ComboBox::ComboBox( QWidget* parent )
  27. : QComboBox( parent )
  28. {
  29. }
  30. ComboBox::~ComboBox()
  31. {
  32. }
  33. QSize
  34. ComboBox::sizeHint() const
  35. {
  36. return QComboBox::sizeHint() + QSize( 8, 0 );
  37. }
  38. void
  39. ComboBox::paintEvent( QPaintEvent* )
  40. {
  41. QStylePainter p( this );
  42. p.setPen( palette().color( QPalette::Text ) );
  43. QStyleOptionComboBox cb;
  44. initStyleOption( &cb );
  45. QRect r = cb.rect;
  46. r.setHeight( TomahawkUtils::defaultFontHeight() + 8 );
  47. TomahawkStyle::horizontalHeader( &p, r );
  48. if ( cb.state & QStyle::State_MouseOver )
  49. {
  50. p.save();
  51. QRect highlightRect( r );
  52. QSize shrink( 3, 4 );
  53. QSize hS( highlightRect.size() );
  54. hS -= shrink;
  55. highlightRect.setSize( hS );
  56. highlightRect.translate( 0, 2 );
  57. p.setRenderHint( QPainter::Antialiasing );
  58. p.setBrush( TomahawkStyle::HEADER_HIGHLIGHT );
  59. p.drawRoundedRect( highlightRect, 10.0, 10.0 );
  60. p.restore();
  61. }
  62. p.save();
  63. QTextOption to( Qt::AlignVCenter );
  64. r.adjust( 8, 0, -8, 0 );
  65. p.setPen( Qt::white );
  66. p.setBrush( TomahawkStyle::HEADER_TEXT );
  67. p.drawText( r, cb.currentText, to );
  68. bool reverse = cb.direction == Qt::RightToLeft;
  69. int menuButtonWidth = 12;
  70. int left = !reverse ? r.right() - menuButtonWidth : r.left();
  71. int right = !reverse ? r.right() : r.left() + menuButtonWidth;
  72. QRect arrowRect( ( left + right ) / 2 + ( reverse ? 6 : -6 ), r.center().y() - 3, 9, 9 ); //FIXME: no consts please
  73. QStyleOption arrowOpt = cb;
  74. arrowOpt.rect = arrowRect;
  75. TomahawkStyle::drawArrow( QStyle::PE_IndicatorArrowDown, &p, &arrowOpt );
  76. p.restore();
  77. }