File: PixmapButton.cpp

package info (click to toggle)
fotowall 0.9-8
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,280 kB
  • sloc: cpp: 23,275; makefile: 51; sh: 25
file content (176 lines) | stat: -rw-r--r-- 4,877 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/***************************************************************************
 *                                                                         *
 *   This file is part of the Fotowall project,                            *
 *       http://www.enricoros.com/opensource/fotowall                      *
 *                                                                         *
 *   Copyright (C) 2009 by Enrico Ros <enrico.ros@gmail.com>               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "PixmapButton.h"

#include <QApplication>
#include <QPaintEvent>
#include <QPainter>
#include <QStyle>
#include <QStyleOptionFocusRect>

PixmapButton::PixmapButton(QWidget * parent)
    : QAbstractButton(parent)
    , m_fadeInactive(false)
    , m_hovering(false)
{
    m_hoverFont = QApplication::font();
    m_hoverFont.setPixelSize(10);
    setFixedSize(32, 32);
}

void PixmapButton::setPixmap(const QPixmap & pixmap)
{
    m_fixedPixmap = pixmap;
    update();
}

QPixmap PixmapButton::pixmap() const
{
    return m_fixedPixmap;
}

void PixmapButton::setHoverText(const QString &text)
{
    m_hoverText = text;
    update();
}

QString PixmapButton::hoverText() const
{
    return m_hoverText;
}

void PixmapButton::setHoverPixmap(const QPixmap & pixmap)
{
    m_hoverPixmap = pixmap;
    update();
}

QPixmap PixmapButton::hoverPixmap() const
{
    return m_hoverPixmap;
}

void PixmapButton::setFadeInactive(bool fade)
{
    if (m_fadeInactive != fade) {
        m_fadeInactive = fade;
        update();
    }
}

bool PixmapButton::fadeInactive() const
{
    return m_fadeInactive;
}

void PixmapButton::setFixedSize(const QSize & size)
{
    m_fixedSize = size;
    QAbstractButton::setFixedSize(m_fixedSize);
}

void PixmapButton::setFixedSize(int w, int h)
{
    setFixedSize(QSize(w, h));
}

QSize PixmapButton::fixedSize() const
{
    return m_fixedSize;
}

void PixmapButton::enterEvent(QEvent *)
{
    m_hovering = true;
    update();
}

void PixmapButton::leaveEvent(QEvent *)
{
    m_hovering = false;
    update();
}

void PixmapButton::paintEvent(QPaintEvent *)
{
    QPainter p(this);

    // draw background if checked
    if (isChecked()) {
#if 0
        p.fillRect(rect(), palette().color(QPalette::Button));
#else
        QStyleOption opt(0);
        opt.palette = palette();
        opt.state = QStyle::State_Enabled | QStyle::State_Sunken;
        if (m_hovering)
            opt.state |= QStyle::State_MouseOver;
        opt.rect = rect();
        style()->drawPrimitive(QStyle::PE_PanelButtonTool, &opt, &p, this);
#endif
    }
    // draw background if hovering
    else if (m_hovering)
        p.fillRect(rect(), palette().color(QPalette::Highlight));

    // draw pixmap, or placeholder
    if (m_fixedPixmap.isNull()) {
#if 0
        p.fillRect(rect().adjusted(2, 2, -2, -2), palette().color(QPalette::Button));
#endif
    } else {
        bool fade = m_fadeInactive && !isDown() && !m_hovering && !hasFocus();
        if (fade)
            p.setOpacity(0.4);
        int offset = (isDown() | isChecked()) ? 1 : 0;
        p.drawPixmap( offset + (width() - m_fixedPixmap.width()) / 2,
                      offset + (height() - m_fixedPixmap.height()) / 2,
                      m_fixedPixmap);
        if (fade)
            p.setOpacity(1.0);
    }

    // draw hover text, if any
    if (!m_hoverText.isEmpty()) {
#if 1
        const int h = 13;
        const int w = h * m_hoverText.size();
        p.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing, true);
        p.setPen(Qt::NoPen);
        p.setBrush(palette().color(QPalette::Highlight));
        QRectF rect(0, 0, w, h);
        p.drawEllipse(rect.adjusted(0.5, 0.5, -0.5, -0.5));
        p.setPen(palette().color(QPalette::HighlightedText));
        p.setFont(m_hoverFont);
#ifdef Q_WS_WIN
        p.drawText(rect.toRect().adjusted(0, -1, 0, -1), Qt::AlignCenter, m_hoverText);
#else
        p.drawText(rect.toRect(), Qt::AlignCenter, m_hoverText);
#endif
#endif
    }

    // draw hover pixmap, if hovering
    if (m_hovering && !m_hoverPixmap.isNull())
        p.drawPixmap(width() - m_hoverPixmap.width(), height() - m_hoverPixmap.height(), m_hoverPixmap);

    // draw focus, if any
    if (hasFocus()) {
        QStyleOptionFocusRect opt;
        opt.initFrom(this);
        style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, &p, this);
    }
}