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
|
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QPainterPath>
#include <QFontMetrics>
#include "GraphicsButtonObject.h"
QPixmap *GraphicsButtonObject::s_tmpGfx = 0;
int GraphicsButtonObject::s_ref = 0;
GraphicsButtonObject::GraphicsButtonObject(const QPixmap &releasedPixmap,
QGraphicsPixmapItem *parent,
QGraphicsScene *scene)
: QObject(0),
QGraphicsPixmapItem(releasedPixmap, parent, scene)
{
s_ref++;
m_releasedGfx = releasedPixmap;
}
GraphicsButtonObject::GraphicsButtonObject(QGraphicsPixmapItem *parent,
QGraphicsScene *scene)
: QObject(0),
QGraphicsPixmapItem(parent, scene)
{
s_ref++;
}
GraphicsButtonObject::GraphicsButtonObject(const QString &str,
QGraphicsPixmapItem *parent,
QGraphicsScene *scene)
: QObject(0),
QGraphicsPixmapItem(parent, scene)
{
s_ref++;
int pw = 280;
int ph = 60;
// Draw the gradients only once
if (!s_tmpGfx)
{
//DBG("generating button gradients");
s_tmpGfx = new QPixmap(pw, ph);
QPainter painter(s_tmpGfx);
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::white);
painter.drawRect(s_tmpGfx->rect());
QLinearGradient grad;
grad.setCoordinateMode(QGradient::ObjectBoundingMode);
grad.setStart(0, 0);
grad.setFinalStop(0, 1);
/*
grad.setColorAt(0, Qt::white);
grad.setColorAt(0.20, QColor(137, 175, 201));
grad.setColorAt(0.35, QColor(35, 136, 207));
grad.setColorAt(0.5, QColor(32, 98, 145));
grad.setColorAt(0.65, QColor(35, 136, 207));
grad.setColorAt(0.80, QColor(137, 175, 201));
grad.setColorAt(1, Qt::white);
*/
grad.setColorAt(0, Qt::red);
grad.setColorAt(0.20, QColor(255, 127, 0));
grad.setColorAt(0.35, QColor(255, 255, 0));
grad.setColorAt(0.5, QColor(0, 255, 0));
grad.setColorAt(0.65, QColor(0, 0, 255));
grad.setColorAt(0.80, QColor(111, 0, 255));
grad.setColorAt(1, QColor(143, 0, 255));
painter.setBrush(QBrush(grad));
painter.drawRect(4, 4, pw-8, ph-8);
}
QPixmap released = QPixmap(*s_tmpGfx);
QPainter painter(&released);
QFont font("Arial", 36);
font.setLetterSpacing(QFont::PercentageSpacing, 95);
QFontMetrics metrics(font);
QRect bbox = metrics.boundingRect(str);
QPainterPath path;
int x = (ph - metrics.height()) / 2;
path.addText(pw/2-bbox.width()/2, x + metrics.ascent(), font, str);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(QPen(Qt::black, 3));
painter.setBrush(Qt::white);
painter.drawPath(path);
QPixmap pressed(released);
QPainter painter2(&pressed);
painter2.setBrush(QColor(0, 0, 0, 127));
painter2.drawRect(pressed.rect());
m_releasedGfx = released;
m_pressedGfx = pressed;
setPixmap(released);
}
GraphicsButtonObject::~GraphicsButtonObject()
{
s_ref--;
if (s_tmpGfx && s_ref == 0)
{
delete s_tmpGfx;
s_tmpGfx = 0;
}
}
void GraphicsButtonObject::setPressedPixmap(const QPixmap &pixmap)
{
m_pressedGfx = pixmap;
}
void GraphicsButtonObject::setReleasedPixmap(const QPixmap &pixmap)
{
m_releasedGfx = pixmap;
}
void GraphicsButtonObject::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
setPixmap(m_releasedGfx);
emit clicked();
}
void GraphicsButtonObject::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
setPixmap(m_pressedGfx);
}
|