PageRenderTime 142ms CodeModel.GetById 60ms app.highlight 11ms RepoModel.GetById 69ms app.codeStats 0ms

/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
 20#include "searchbutton.h"
 21
 22#include <qcompleter.h>
 23#include <qevent.h>
 24#include <qlineedit.h>
 25#include <qpainter.h>
 26
 27SearchButton::SearchButton(QWidget *parent)
 28    : QAbstractButton(parent)
 29    , m_showMenuTriangle(false)
 30{
 31    setFocusPolicy(Qt::NoFocus);
 32    setCursor(Qt::ArrowCursor);
 33    setMinimumSize(sizeHint());
 34}
 35
 36QSize SearchButton::sizeHint() const
 37{
 38    if (!m_cache.isNull())
 39        return m_cache.size();
 40    if (m_showMenuTriangle)
 41        return QSize(16, 16);
 42    return QSize(12, 16);
 43}
 44
 45QImage SearchButton::generateSearchImage(bool dropDown)
 46{
 47    QImage image(dropDown ? 16 : 12, 16, QImage::Format_ARGB32);
 48    image.fill(qRgba(0, 0, 0, 0));
 49    QPainterPath path;
 50
 51    // draw magnify glass circle
 52    int radius = image.height() / 2;
 53    QRect circle(1, 1, radius, radius);
 54    path.addEllipse(circle);
 55
 56    // draw handle
 57    path.arcMoveTo(circle, 300);
 58    QPointF currentPosition = path.currentPosition();
 59    path.moveTo(currentPosition.x() + 1, currentPosition.y() + 1);
 60    if (dropDown)
 61        path.lineTo(image.width()-6, image.height()-4);
 62    else
 63        path.lineTo(image.width()-2, image.height()-4);
 64
 65    QPainter painter(&image);
 66    painter.setRenderHint(QPainter::Antialiasing, true);
 67    painter.setPen(QPen(Qt::darkGray, 2));
 68    painter.drawPath(path);
 69
 70    if (dropDown) {
 71        // draw the dropdown triangle
 72        QPainterPath dropPath;
 73        dropPath.arcMoveTo(circle, 320);
 74        QPointF currentPosition = dropPath.currentPosition();
 75        currentPosition = QPointF(currentPosition.x() + 2, currentPosition.y() + 0.5);
 76        dropPath.moveTo(currentPosition);
 77        dropPath.lineTo(currentPosition.x() + 4, currentPosition.y());
 78        dropPath.lineTo(currentPosition.x() + 2, currentPosition.y() + 2);
 79        dropPath.closeSubpath();
 80        painter.setPen(Qt::darkGray);
 81        painter.setBrush(Qt::darkGray);
 82        painter.setRenderHint(QPainter::Antialiasing, false);
 83        painter.drawPath(dropPath);
 84    }
 85    painter.end();
 86    return image;
 87}
 88
 89void SearchButton::setImage(const QImage &image)
 90{
 91    m_cache = image;
 92    setMinimumSize(sizeHint());
 93    update();
 94}
 95
 96void SearchButton::setShowMenuTriangle(bool show)
 97{
 98    m_showMenuTriangle = show;
 99    setMinimumSize(sizeHint());
100}
101
102bool SearchButton::showMenuTriangle() const
103{
104    return m_showMenuTriangle;
105}
106
107void SearchButton::paintEvent(QPaintEvent *event)
108{
109    Q_UNUSED(event);
110    if (m_cache.isNull())
111        m_cache = generateSearchImage(m_showMenuTriangle);
112    QPainter painter(this);
113    painter.setRenderHint(QPainter::SmoothPixmapTransform);
114    painter.drawImage(QPoint(0, 0), m_cache);
115}
116