summaryrefslogtreecommitdiffstats
path: root/src/gui/editors/guitar/FingeringBox.cpp
blob: 7677b8585c2740dfe390fd6393d95dc9ddb03f39 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */

/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.

    This program is Copyright 2000-2008
        Guillaume Laurent   <glaurent@telegraph-road.org>,
        Chris Cannam        <cannam@all-day-breakfast.com>,
        Richard Bown        <richard.bown@ferventsoftware.com>

    The moral rights of Guillaume Laurent, Chris Cannam, and Richard
    Bown to claim authorship of this work have been asserted.

    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/


#include "FingeringBox.h"
#include "Fingering.h"

#include "misc/Debug.h"

namespace Rosegarden
{

FingeringBox::FingeringBox(unsigned int nbFrets, unsigned int nbStrings, bool editable, TQWidget *tqparent, const char* name)
    : TQFrame(tqparent, name),
    m_nbFretsDisplayed(nbFrets),
    m_startFret(1),
    m_nbStrings(nbStrings),
    m_transientFretNb(0),
    m_transientStringNb(0),
    m_editable(editable),
    m_noteSymbols(m_nbStrings, m_nbFretsDisplayed)
{
    init();    
}

FingeringBox::FingeringBox(bool editable, TQWidget *tqparent, const char* name)
    : TQFrame(tqparent, name),
    m_nbFretsDisplayed(DEFAULT_NB_DISPLAYED_FRETS),
    m_startFret(1),
    m_nbStrings(Guitar::Fingering::DEFAULT_NB_STRINGS),
    m_editable(editable),
    m_noteSymbols(m_nbStrings, m_nbFretsDisplayed)
{
    init();
}

void
FingeringBox::init()
{
    setFrameStyle(TQFrame::StyledPanel | TQFrame::Sunken);
    setFixedSize(IMG_WIDTH, IMG_HEIGHT);
    setBackgroundMode(PaletteBase);
    if (m_editable)
        setMouseTracking(true);
    
}

void
FingeringBox::drawContents(TQPainter* p)
{
//    NOTATION_DEBUG << "FingeringBox::drawContents()" << endl;
    
    // For all strings on guitar
    //   check state of string
    //     If pressed display note
    //     Else display muted or open symbol
    // For all bars
    //   display bar
    // Horizontal separator line

    // draw guitar chord fingering
    //
    m_noteSymbols.drawFretNumber(p, m_startFret);
    m_noteSymbols.drawFrets(p);
    m_noteSymbols.drawStrings(p);

    unsigned int stringNb = 0;
    
    // draw notes
    //
    for (Guitar::Fingering::const_iterator pos = m_fingering.begin();
         pos != m_fingering.end();
         ++pos, ++stringNb) {
                
        switch (*pos) {
        case Guitar::Fingering::OPEN:
//                NOTATION_DEBUG << "Fingering::drawContents - drawing Open symbol on string " << stringNb << endl;
                m_noteSymbols.drawOpenSymbol(p, stringNb);
                break;

        case Guitar::Fingering::MUTED:
//                NOTATION_DEBUG << "Fingering::drawContents - drawing Mute symbol on string" << stringNb << endl;
                m_noteSymbols.drawMuteSymbol(p, stringNb);
                break;

        default:
//                NOTATION_DEBUG << "Fingering::drawContents - drawing note symbol at " << *pos << " on string " << stringNb << endl;
                m_noteSymbols.drawNoteSymbol(p, stringNb, *pos - (m_startFret - 1), false);
                break;
        }
    }

    // TODO: detect barres and draw them in a special way ?
    
    // draw transient note (visual feedback for mouse move)
    //
    if (hasMouse() &&
        m_transientFretNb > 0 && m_transientFretNb <= m_nbFretsDisplayed &&
        m_transientStringNb >= 0 && m_transientStringNb <= m_nbStrings) {
        m_noteSymbols.drawNoteSymbol(p, m_transientStringNb, m_transientFretNb - (m_startFret - 1), true);
    }
    
    // DEBUG
//    p->save();
//    p->setPen(TQt::red);
//    unsigned int topBorderY = m_noteSymbols.getTopBorder(maximumHeight());
//    p->drawLine(0, topBorderY, 20, topBorderY);
//    p->drawRect(m_r1);
//    p->setPen(TQt::blue);
//    p->drawRect(m_r2);
//    p->restore();
}

void
FingeringBox::setFingering(const Guitar::Fingering& f) {
    m_fingering = f;
    m_startFret = m_fingering.getStartFret();
    update();
}

unsigned int
FingeringBox::getStringNumber(const TQPoint& pos)
{
    PositionPair result = m_noteSymbols.getStringNumber(maximumHeight(),
                                                        pos.x(),
                                                        m_nbStrings);
    unsigned int stringNum = -1;

    if(result.first){
        stringNum = result.second;
//        RG_DEBUG << "FingeringBox::getStringNumber : res = " << stringNum << endl; 
    }

    return stringNum;
}

unsigned int
FingeringBox::getFretNumber(const TQPoint& pos)
{
    unsigned int fretNum = 0;

    if(true || pos.y() > m_noteSymbols.getTopBorder(maximumHeight())) {
        // If fret position is below the top line of the guitar chord image.
        PositionPair result = m_noteSymbols.getFretNumber(maximumWidth(),
                                                          pos.y(),
                                                          m_nbFretsDisplayed);

        if(result.first) {
            fretNum = result.second + (m_startFret - 1);
//            RG_DEBUG << "FingeringBox::getFretNumber : res = " << fretNum << " startFret = " << m_startFret << endl; 
        } else {
//            RG_DEBUG << "FingeringBox::getFretNumber : no res\n";
        }
    }

    return fretNum;
}

void
FingeringBox::mousePressEvent(TQMouseEvent *event)
{
    if (!m_editable)
        return;
        
    if((event->button() == Qt::LeftButton) && m_editable) {

        // Find string position
        m_press_string_num = getStringNumber(event->pos());

        // Find fret position
        m_press_fret_num = getFretNumber(event->pos());
    }
}

void
FingeringBox::mouseReleaseEvent(TQMouseEvent *event)
{
    if(!m_editable)
        return ;

    unsigned int release_string_num = getStringNumber(event->pos());
    unsigned int release_fret_num = getFretNumber(event->pos());

    processMouseRelease(release_string_num, release_fret_num);
}

void
FingeringBox::processMouseRelease(unsigned int release_string_num,
                                   unsigned int release_fret_num)
{
    if(m_press_fret_num == release_fret_num) {
        // If press string & fret pos == release string & fret position, display chord
        if(m_press_string_num == release_string_num) {

            if(m_press_fret_num < (m_startFret + m_nbFretsDisplayed)) {

                unsigned int aVal = m_press_fret_num;
                
                if(m_press_fret_num == 0) {
                    
                    int stringtqStatus = m_fingering.getStringtqStatus(m_press_string_num);

                    if (stringtqStatus == Guitar::Fingering::OPEN)
                        aVal = Guitar::Fingering::MUTED;
                    else if (stringtqStatus > Guitar::Fingering::OPEN)
                        aVal = Guitar::Fingering::OPEN;

                }
                
                m_fingering.setStringtqStatus(m_press_string_num, aVal);
                
                update();
            }
        }
        // else if press fret pos == release fret pos & press string pos != release string pos, display bar
        else {
            if(((m_press_string_num > 0)&&(release_string_num > 0)) &&
                   (( m_press_string_num <= m_nbStrings)&&
                     (release_string_num <= m_nbStrings)) &&
                   (( m_press_fret_num <(m_startFret + m_nbFretsDisplayed)) &&
                     (release_fret_num <(m_startFret + m_nbFretsDisplayed)))) {

                // TODO deal with barre later on

            }
        }
    }
}


void
FingeringBox::mouseMoveEvent( TQMouseEvent *event )
{
    if (!m_editable)
        return;
        
    unsigned int transientStringNb = getStringNumber(event->pos());
    unsigned int transientFretNb   = getFretNumber(event->pos());
        
    if (transientStringNb != m_transientStringNb ||
        transientFretNb != m_transientFretNb) {

        TQRect r1 = m_noteSymbols.getTransientNoteSymbolRect(size(),
                                                            m_transientStringNb,
                                                            m_transientFretNb - (m_startFret - 1));
        m_transientStringNb = transientStringNb;
        m_transientFretNb   = transientFretNb;
        TQRect r2 = m_noteSymbols.getTransientNoteSymbolRect(size(),
                                                            m_transientStringNb,
                                                            m_transientFretNb - (m_startFret - 1));
    
        m_r1 = r1;
        m_r2 = r2;
        
//    RG_DEBUG << "Fingering::updateTransientPos r1 = " << r1 << " - r2 = " << r2 << endl;
     
//        TQRect updateRect = r1 | r2;
//        update(updateRect);

        update();
            
    }    
    
}

void
FingeringBox::leaveEvent(TQEvent*)
{
    update();
}

}