summaryrefslogtreecommitdiff
path: root/noncore/multimedia/tonleiter/fretboard.cpp
blob: 58bdb865979eb1e5439c4eddd4a76b9a1c8fa17a (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "fretboard.h"

/* OPIE */
#include <opie2/odebug.h>
using namespace Opie::Core;

/* QT */
#include <qpainter.h>

Graph::FretBoard::FretBoard(TonleiterData* data,QWidget* parent,const char* name,WFlags f)
:QWidget(parent,name,f),data(data)
{
    setBackgroundColor(QColor(0,0,0));
    fretpen=QPen(QColor(155,155,155),0);
    markerbrush=QBrush(QColor(155,155,155));
    stringpen=QPen(QColor(255,255,255),3);
    connect(data,SIGNAL(dataChange()),this,SLOT(dataChange()));
}
//****************************************************************************
Graph::FretBoard::~FretBoard()
{
}
//****************************************************************************
void Graph::FretBoard::dataChange()
{
    repaint(true);
}
//****************************************************************************
void Graph::FretBoard::paintEvent(QPaintEvent* pe)
{
    Q_UNUSED(pe);

    int instid=data->getCurrentInstrumentID();
    inst=data->getInstrument(instid);
    //odebug << "inst " << instid << "  is " << inst.instName().data() << "" << oendl; 

    QRect mysize=rect();

    xmargin=(int)(mysize.width()*0.05);
    xmin=2*xmargin;
    xmax=mysize.width()-xmargin;
    xwidth=xmax-xmin;

    ymargin=(int)(mysize.height()*0.1);
    ymin=ymargin;
    ymax=mysize.height()-2*ymargin;
    yheight=ymax-ymin;

    QPainter p(this);
    paintBoard(&p);
    paintFrets(&p);
    paintStrings(&p);
    paintScale(&p);
}
//****************************************************************************
void Graph::FretBoard::resizeEvent(QResizeEvent* re)
{
    Q_UNUSED(re);
}
//****************************************************************************
void Graph::FretBoard::mouseReleaseEvent(QMouseEvent* me)
{
    Q_UNUSED(me);
    emit pressed();
}
//****************************************************************************
void Graph::FretBoard::paintBoard(QPainter* p)
{
    //debug
    QColor bgc=QColor(142,138,120);
    p->setPen(QPen(bgc,5));
    p->setBrush(QBrush(bgc));
    p->drawRect(xmin,ymin,xwidth,yheight);

}
//****************************************************************************
void Graph::FretBoard::paintFrets(QPainter* p)
{
    //draw frets
    p->setPen(fretpen);
    p->setBrush(markerbrush);
    fretdist=(double)(xwidth)/(double)(inst.noOfFrets());

    int markerwidth = (int)(fretdist/4.0);
    if(markerwidth>5)
        markerwidth=5;
    else if(markerwidth<2)
        markerwidth=2;

    for(int f=0;f<=inst.noOfFrets();f++)
    {
        int fretpos=(int)(fretdist*f);
        p->drawLine(fretpos+xmin,ymin,fretpos+xmin,ymax);

        int above_octave=f%12;
        int octave=(f-above_octave)/12;
        int marker=f-12*octave;


        if(marker==5 || marker==7 || marker==9)
        {
            p->drawEllipse(fretpos+xmin,ymax+ymargin,markerwidth,markerwidth);
        }
        else if(marker==0 && f!=0)
        {
            p->drawEllipse(fretpos+xmin,ymax+ymargin+2*markerwidth,markerwidth,markerwidth);
            p->drawEllipse(fretpos+xmin,ymax+ymargin-2*markerwidth,markerwidth,markerwidth);
        }

    }
}
//****************************************************************************
void Graph::FretBoard::paintStrings(QPainter* p)
{
    //draw strings
    p->setPen(stringpen);

    stringdist=(double)(yheight)/(double)(inst.noOfStrings()-1);
    for(int s=0;s<inst.noOfStrings();s++)
    {
        int stringpos=(int)(stringdist*s);
        p->drawLine(xmin,stringpos+ymin,xmax,stringpos+ymin);

        int stinglabelid=inst.noOfStrings()-(s+1);
        QString label=Note::getNameOfNote(inst.string(stinglabelid));
        p->drawText(xmargin/2,stringpos+ymin,label);
    }
}
//****************************************************************************
void Graph::FretBoard::paintScale(QPainter* p)
{
    int dotsize=10;
    int scaleid=data->getCurrentScaleID();
    Scale scale=data->getScale(scaleid);
    int baseoctave=Note::octaveOfBaseNote(data->getCurrentBaseNote(),inst.lowestNote());

    for(int s=0;s<inst.noOfStrings();s++)
    {
        int y=(int)(stringdist*s)+ymin;
        int stingid=inst.noOfStrings()-(s+1);
        for(int f=0;f<=inst.noOfFrets();f++)
        {
            int note=inst.string(stingid)+f;
            if(scale.noteInScale(data->getCurrentBaseNote(),note))
            {
                int x=(int)(fretdist*f)+xmin;

                //no more than six octaves can be visualised (there is a zero octave)
                int octave=Note::octaveOfBaseNote(data->getCurrentBaseNote(),note)-baseoctave;
                if(octave<0)
                    odebug << "" << octave << "," << baseoctave << "" << oendl; 
                if(octave>5)
                {
                    odebug << "octave out of range" << oendl; 
                    octave=5;
                }

                p->setPen(QColor(255,0,0));
                int c= ( (note-12*baseoctave) - (12*octave+data->getCurrentBaseNote()) )*15;
                if(c<0 || c>255)
                    odebug << "" << c << " = " << note << " - ( " << 12*octave << " + " << data->getCurrentBaseNote() << ")" << oendl; 
                QColor dotcolor(255,255,255);

                if(octave==0)
                    dotcolor=QColor(c,c,255);
                else if(octave==1)
                    dotcolor=QColor(c,255,c);
                else if(octave==2)
                    dotcolor=QColor(255,c,c);
                else if(octave==3)
                    dotcolor=QColor(255,255,c);
                else if(octave==4)
                    dotcolor=QColor(255,c,255);
                else
                    dotcolor=QColor(c,255,255);

                p->setBrush(dotcolor);
                p->drawEllipse(x-dotsize/2,y-dotsize/2,dotsize,dotsize);

                if(data->isDrawNames())
                {
                    p->setPen(QColor(255,155,0));
                    p->drawText(x-dotsize/2,y-dotsize/2,Note::getNameOfNote(note));
                }

            }
        }
    }
}
//****************************************************************************
//****************************************************************************