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
|
#include <cstring>
#include <QPainter>
#include "qmppianowidget.hpp"
qmpPianoWidget::qmpPianoWidget(QWidget *parent) : QWidget(parent)
{
memset(keystates,0,sizeof(keystates));
}
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);
}
void qmpPianoWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
for(int i=0;i<128;++i)
{
QRectF r=getKeyRect(i);
paintKey(r,QColor(keystates[i]?0xff66cc:0x66ccff));
}
}
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;
}
|