PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 10ms RepoModel.GetById 30ms app.codeStats 1ms

/src/libtomahawk/playlist/topbar/clearbutton.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 92 lines | 49 code | 12 blank | 31 comment | 4 complexity | d42227a5721f2fc18153bfb9faaa0aac MD5 | raw file
 1/**
 2 * Copyright (c) 2009, Benjamin C. Meyer  <ben@meyerhome.net>
 3 *
 4 * Redistribution and use in source and binary forms, with or without
 5 * modification, are permitted provided that the following conditions
 6 * are met:
 7 * 1. Redistributions of source code must retain the above copyright
 8 *    notice, this list of conditions and the following disclaimer.
 9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the Benjamin Meyer nor the names of its contributors
13 *    may be used to endorse or promote products derived from this software
14 *    without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include "clearbutton.h"
30
31#include <qpainter.h>
32
33ClearButton::ClearButton(QWidget *parent)
34    : QAbstractButton(parent)
35{
36    setCursor(Qt::ArrowCursor);
37    setFocusPolicy(Qt::NoFocus);
38    setToolTip(tr("Clear"));
39    setMinimumSize(22, 22);
40    setVisible(false);
41
42#if QT_VERSION >= 0x040600
43    // First check for a style icon, current KDE provides one
44    if (m_styleImage.isNull()) {
45        QLatin1String iconName = (layoutDirection() == Qt::RightToLeft)
46            ? QLatin1String("edit-clear-locationbar-ltr")
47            : QLatin1String("edit-clear-locationbar-rtl");
48        QIcon icon = QIcon::fromTheme(iconName);
49        if (!icon.isNull())
50            m_styleImage = icon.pixmap(16, 16).toImage();
51    }
52#endif
53}
54
55void ClearButton::textChanged(const QString &text)
56{
57    setVisible(!text.isEmpty());
58}
59
60void ClearButton::paintEvent(QPaintEvent *event)
61{
62    Q_UNUSED(event);
63    QPainter painter(this);
64
65    if (!m_styleImage.isNull()) {
66        int x = (width() - m_styleImage.width()) / 2 - 1;
67        int y = (height() - m_styleImage.height()) / 2 - 1;
68        painter.drawImage(x, y, m_styleImage);
69        return;
70    }
71
72    // Fall back to boring circle X
73    painter.setRenderHint(QPainter::Antialiasing, true);
74
75    QPalette p = palette();
76    QColor circleColor = isDown() ? p.color(QPalette::Dark) : p.color(QPalette::Mid);
77    QColor xColor = p.color(QPalette::Window);
78
79    // draw circle
80    painter.setBrush(circleColor);
81    painter.setPen(circleColor);
82    int padding = width() / 5;
83    int circleRadius = width() - (padding * 2);
84    painter.drawEllipse(padding, padding, circleRadius, circleRadius);
85
86    // draw X
87    painter.setPen(xColor);
88    padding = padding * 2;
89    painter.drawLine(padding, padding,            width() - padding, width() - padding);
90    painter.drawLine(padding, height() - padding, width() - padding, padding);
91}
92