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
|
#include <cstring>
#include <QPainter>
#include "qmppianowidget.hpp"
qmpPianoWidget::qmpPianoWidget(QWidget *parent) : QWidget(parent)
{
memset(keystates, 0, sizeof(keystates));
QPalette p = palette();
p.setColor(QPalette::ColorRole::Highlight, 0xff66cc);
p.setColor(QPalette::ColorRole::Base, 0x66ccff);
setPalette(p);
}
void qmpPianoWidget::setKeyState(int key, bool state)
{
keystates[key] = state;
update();
}
void qmpPianoWidget::reset()
{
memset(keystates, 0, sizeof(keystates));
update();
}
QSize qmpPianoWidget::minimumSizeHint()const
{
return QSize(320, 22);
}
bool qmpPianoWidget::hasHeightForWidth()const
{
return true;
}
int qmpPianoWidget::heightForWidth(int w)const
{
return w * 22 / 320;
}
void qmpPianoWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
for (int i = 0; i < 128; ++i)
{
QRectF r = getKeyRect(i);
QColor activeColor = palette().color(QPalette::ColorRole::Highlight);
QColor inactiveColor = palette().color(QPalette::ColorRole::Base);
if (i / 12 % 2)
{
if (inactiveColor.valueF() > 0.5)
inactiveColor = inactiveColor.darker(112);
else
inactiveColor = inactiveColor.lighter(112);
}
paintKey(r, keystates[i] ? activeColor : inactiveColor);
}
}
QRectF qmpPianoWidget::getKeyRect(int key)
{
int octave = key / 12;
key %= 12;
bool is_black = (key < 5 && (key & 1)) || (key > 5 && ((key & 1) ^ 1));
double key_width = width() / 75.;
QRectF ret(0, 0, key_width, height() / 2.);
if (!is_black)
{
ret.moveTop(height() / 2.);
int shift = (key + (key >= 5)) >> 1;
ret.moveLeft((octave * 7 + shift)*key_width);
}
else
ret.moveLeft((octave * 7 + (key + (key >= 5)) / 2.)*key_width);
return ret;
}
void qmpPianoWidget::paintKey(QRectF keyrect, QColor keycolor)
{
QColor bordercolor(keycolor);
if (keycolor.valueF() > 0.5)
bordercolor = bordercolor.darker(150);
else
bordercolor = bordercolor.lighter(150);
QPainter *p = new QPainter(this);
p->setPen(bordercolor);
p->setBrush(QBrush(keycolor));
p->drawRect(keyrect.adjusted(1, 1, -1, -1));
delete p;
}
|