/src/libtomahawk/playlist/topbar/searchbutton.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 116 lines · 81 code · 14 blank · 21 comment · 5 complexity · 2bb0d30baf8dd032ae8503c5802f8fbb MD5 · raw file

  1. /*
  2. * Copyright 2009 Benjamin C. Meyer <ben@meyerhome.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301 USA
  18. */
  19. #include "searchbutton.h"
  20. #include <qcompleter.h>
  21. #include <qevent.h>
  22. #include <qlineedit.h>
  23. #include <qpainter.h>
  24. SearchButton::SearchButton(QWidget *parent)
  25. : QAbstractButton(parent)
  26. , m_showMenuTriangle(false)
  27. {
  28. setFocusPolicy(Qt::NoFocus);
  29. setCursor(Qt::ArrowCursor);
  30. setMinimumSize(sizeHint());
  31. }
  32. QSize SearchButton::sizeHint() const
  33. {
  34. if (!m_cache.isNull())
  35. return m_cache.size();
  36. if (m_showMenuTriangle)
  37. return QSize(16, 16);
  38. return QSize(12, 16);
  39. }
  40. QImage SearchButton::generateSearchImage(bool dropDown)
  41. {
  42. QImage image(dropDown ? 16 : 12, 16, QImage::Format_ARGB32);
  43. image.fill(qRgba(0, 0, 0, 0));
  44. QPainterPath path;
  45. // draw magnify glass circle
  46. int radius = image.height() / 2;
  47. QRect circle(1, 1, radius, radius);
  48. path.addEllipse(circle);
  49. // draw handle
  50. path.arcMoveTo(circle, 300);
  51. QPointF currentPosition = path.currentPosition();
  52. path.moveTo(currentPosition.x() + 1, currentPosition.y() + 1);
  53. if (dropDown)
  54. path.lineTo(image.width()-6, image.height()-4);
  55. else
  56. path.lineTo(image.width()-2, image.height()-4);
  57. QPainter painter(&image);
  58. painter.setRenderHint(QPainter::Antialiasing, true);
  59. painter.setPen(QPen(Qt::darkGray, 2));
  60. painter.drawPath(path);
  61. if (dropDown) {
  62. // draw the dropdown triangle
  63. QPainterPath dropPath;
  64. dropPath.arcMoveTo(circle, 320);
  65. QPointF currentPosition = dropPath.currentPosition();
  66. currentPosition = QPointF(currentPosition.x() + 2, currentPosition.y() + 0.5);
  67. dropPath.moveTo(currentPosition);
  68. dropPath.lineTo(currentPosition.x() + 4, currentPosition.y());
  69. dropPath.lineTo(currentPosition.x() + 2, currentPosition.y() + 2);
  70. dropPath.closeSubpath();
  71. painter.setPen(Qt::darkGray);
  72. painter.setBrush(Qt::darkGray);
  73. painter.setRenderHint(QPainter::Antialiasing, false);
  74. painter.drawPath(dropPath);
  75. }
  76. painter.end();
  77. return image;
  78. }
  79. void SearchButton::setImage(const QImage &image)
  80. {
  81. m_cache = image;
  82. setMinimumSize(sizeHint());
  83. update();
  84. }
  85. void SearchButton::setShowMenuTriangle(bool show)
  86. {
  87. m_showMenuTriangle = show;
  88. setMinimumSize(sizeHint());
  89. }
  90. bool SearchButton::showMenuTriangle() const
  91. {
  92. return m_showMenuTriangle;
  93. }
  94. void SearchButton::paintEvent(QPaintEvent *event)
  95. {
  96. Q_UNUSED(event);
  97. if (m_cache.isNull())
  98. m_cache = generateSearchImage(m_showMenuTriangle);
  99. QPainter painter(this);
  100. painter.setRenderHint(QPainter::SmoothPixmapTransform);
  101. painter.drawImage(QPoint(0, 0), m_cache);
  102. }