Diffstat (limited to 'noncore/multimedia/tonleiter/fretboard.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/multimedia/tonleiter/fretboard.cpp | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/noncore/multimedia/tonleiter/fretboard.cpp b/noncore/multimedia/tonleiter/fretboard.cpp new file mode 100644 index 0000000..d8e48d3 --- a/dev/null +++ b/noncore/multimedia/tonleiter/fretboard.cpp | |||
@@ -0,0 +1,159 @@ | |||
1 | #include "fretboard.h" | ||
2 | |||
3 | #include <qpainter.h> | ||
4 | |||
5 | FretBoard::FretBoard(TonleiterData* data,QWidget* parent,const char* name,WFlags f) | ||
6 | :QWidget(parent,name,f),data(data) | ||
7 | { | ||
8 | setBackgroundColor(QColor(0,0,0)); | ||
9 | fretpen=QPen(QColor(155,155,155),0); | ||
10 | markerbrush=QBrush(QColor(155,155,155)); | ||
11 | stringpen=QPen(QColor(255,255,255),3); | ||
12 | connect(data,SIGNAL(dataChange()),this,SLOT(dataChange())); | ||
13 | } | ||
14 | //**************************************************************************** | ||
15 | FretBoard::~FretBoard() | ||
16 | { | ||
17 | } | ||
18 | //**************************************************************************** | ||
19 | void FretBoard::dataChange() | ||
20 | { | ||
21 | repaint(true); | ||
22 | } | ||
23 | //**************************************************************************** | ||
24 | void FretBoard::paintEvent(QPaintEvent* pe) | ||
25 | { | ||
26 | Q_UNUSED(pe); | ||
27 | |||
28 | int instid=data->getCurrentInstrumentID(); | ||
29 | inst=data->getInstrument(instid); | ||
30 | //qDebug("inst %d is %s",instid,inst.instName().data()); | ||
31 | |||
32 | QRect mysize=rect(); | ||
33 | |||
34 | xmargin=(int)(mysize.width()*0.05); | ||
35 | xmin=2*xmargin; | ||
36 | xmax=mysize.width()-xmargin; | ||
37 | xwidth=xmax-xmin; | ||
38 | |||
39 | ymargin=(int)(mysize.height()*0.1); | ||
40 | ymin=ymargin; | ||
41 | ymax=mysize.height()-2*ymargin; | ||
42 | yheight=ymax-ymin; | ||
43 | |||
44 | QPainter p(this); | ||
45 | paintBoard(&p); | ||
46 | paintFrets(&p); | ||
47 | paintStrings(&p); | ||
48 | paintScale(&p); | ||
49 | } | ||
50 | //**************************************************************************** | ||
51 | void FretBoard::resizeEvent(QResizeEvent* re) | ||
52 | { | ||
53 | Q_UNUSED(re); | ||
54 | } | ||
55 | //**************************************************************************** | ||
56 | void FretBoard::paintBoard(QPainter* p) | ||
57 | { | ||
58 | //debug | ||
59 | QColor bgc=QColor(142,138,120); | ||
60 | p->setPen(QPen(bgc,5)); | ||
61 | p->setBrush(QBrush(bgc)); | ||
62 | p->drawRect(xmin,ymin,xwidth,yheight); | ||
63 | |||
64 | } | ||
65 | //**************************************************************************** | ||
66 | void FretBoard::paintFrets(QPainter* p) | ||
67 | { | ||
68 | //draw frets | ||
69 | p->setPen(fretpen); | ||
70 | p->setBrush(markerbrush); | ||
71 | fretdist=(double)(xwidth)/(double)(inst.noOfFrets()); | ||
72 | |||
73 | int markerwidth = (int)(fretdist/4.0); | ||
74 | if(markerwidth>5) | ||
75 | markerwidth=5; | ||
76 | else if(markerwidth<2) | ||
77 | markerwidth=2; | ||
78 | |||
79 | for(int f=0;f<=inst.noOfFrets();f++) | ||
80 | { | ||
81 | int fretpos=(int)(fretdist*f); | ||
82 | p->drawLine(fretpos+xmin,ymin,fretpos+xmin,ymax); | ||
83 | |||
84 | int above_octave=f%12; | ||
85 | int octave=(f-above_octave)/12; | ||
86 | int marker=f-12*octave; | ||
87 | |||
88 | |||
89 | if(marker==5 || marker==7 || marker==9) | ||
90 | { | ||
91 | p->drawEllipse(fretpos+xmin,ymax+ymargin,markerwidth,markerwidth); | ||
92 | } | ||
93 | else if(marker==0 && f!=0) | ||
94 | { | ||
95 | p->drawEllipse(fretpos+xmin,ymax+ymargin+2*markerwidth,markerwidth,markerwidth); | ||
96 | p->drawEllipse(fretpos+xmin,ymax+ymargin-2*markerwidth,markerwidth,markerwidth); | ||
97 | } | ||
98 | |||
99 | } | ||
100 | } | ||
101 | //**************************************************************************** | ||
102 | void FretBoard::paintStrings(QPainter* p) | ||
103 | { | ||
104 | //draw strings | ||
105 | p->setPen(stringpen); | ||
106 | |||
107 | stringdist=(double)(yheight)/(double)(inst.noOfStrings()-1); | ||
108 | for(int s=0;s<inst.noOfStrings();s++) | ||
109 | { | ||
110 | int stringpos=(int)(stringdist*s); | ||
111 | p->drawLine(xmin,stringpos+ymin,xmax,stringpos+ymin); | ||
112 | |||
113 | int stinglabelid=inst.noOfStrings()-(s+1); | ||
114 | QString label=Note::getNameOfNote(inst.string(stinglabelid)); | ||
115 | p->drawText(xmargin/2,stringpos+ymin,label); | ||
116 | } | ||
117 | } | ||
118 | //**************************************************************************** | ||
119 | void FretBoard::paintScale(QPainter* p) | ||
120 | { | ||
121 | int dotsize=10; | ||
122 | int scaleid=data->getCurrentScaleID(); | ||
123 | Scale scale=data->getScale(scaleid); | ||
124 | |||
125 | int colorstart=255%inst.noOfOctaves(); | ||
126 | int colorintervall=(255-colorstart)/inst.noOfOctaves(); | ||
127 | |||
128 | for(int s=0;s<inst.noOfStrings();s++) | ||
129 | { | ||
130 | int y=(int)(stringdist*s)+ymin; | ||
131 | int stingid=inst.noOfStrings()-(s+1); | ||
132 | for(int f=0;f<=inst.noOfFrets();f++) | ||
133 | { | ||
134 | int note=inst.string(stingid)+f; | ||
135 | if(scale.noteInScale(data->getCurrentBaseNote(),note)) | ||
136 | { | ||
137 | int x=(int)(fretdist*f)+xmin; | ||
138 | |||
139 | int octave=Note::octaveOfBaseNote(data->getCurrentBaseNote(),note); | ||
140 | if(octave>5) octave=5; | ||
141 | p->setPen(QColor(255,0,0)); | ||
142 | int c=colorstart+octave*colorintervall; | ||
143 | QColor dotcolor(c,c,255); | ||
144 | p->setBrush(dotcolor); | ||
145 | |||
146 | p->drawEllipse(x-dotsize/2,y-dotsize/2,dotsize,dotsize); | ||
147 | |||
148 | if(data->isDrawNames()) | ||
149 | { | ||
150 | p->setPen(QColor(0,255,0)); | ||
151 | p->drawText(x-dotsize/2,y-dotsize/2,Note::getNameOfNote(note)); | ||
152 | } | ||
153 | |||
154 | } | ||
155 | } | ||
156 | } | ||
157 | } | ||
158 | //**************************************************************************** | ||
159 | //**************************************************************************** | ||