summaryrefslogtreecommitdiffstats
path: root/src/gui/rulers/PercussionPitchRuler.cpp
blob: 16b9558d55391279a60192b8f286de09146f6584 (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
/* -*- 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 "PercussionPitchRuler.h"

#include "misc/Debug.h"
#include "misc/Strings.h"
#include "base/MidiProgram.h"
#include "gui/editors/matrix/MatrixStaff.h"
#include "gui/editors/matrix/MatrixView.h"
#include "gui/general/MidiPitchLabel.h"
#include "PitchRuler.h"
#include <tqcolor.h>
#include <tqevent.h>
#include <tqfont.h>
#include <tqfontmetrics.h>
#include <tqpainter.h>
#include <tqsize.h>
#include <tqwidget.h>


namespace Rosegarden
{

PercussionPitchRuler::PercussionPitchRuler(TQWidget *parent,
        const MidiKeyMapping *mapping,
        int lineSpacing) :
        PitchRuler(parent),
        m_mapping(mapping),
        m_lineSpacing(lineSpacing),
        m_mouseDown(false),
        m_lastHoverHighlight( -1)
{
    m_font = new TQFont();
    m_font->setPixelSize(9);
    m_fontMetrics = new TQFontMetrics(*m_font);
    m_width = m_fontMetrics->width("  A#2   Acoustic Bass Drum  ");

    setPaletteBackgroundColor(TQColor(238, 238, 224));

    setMouseTracking(true);
}

TQSize PercussionPitchRuler::tqsizeHint() const
{
    return TQSize(m_width,
                 (m_lineSpacing + 1) * m_mapping->getPitchExtent());
}

TQSize PercussionPitchRuler::tqminimumSizeHint() const
{
    return TQSize(m_width, m_lineSpacing + 1);
}

void PercussionPitchRuler::paintEvent(TQPaintEvent*)
{
    TQPainter paint(this);

    paint.setFont(*m_font);

    int minPitch = m_mapping->getPitchForOffset(0);
    int extent = m_mapping->getPitchExtent();

    for (int i = 0; i < extent; ++i) {
        paint.drawLine(0, i * (m_lineSpacing + 1),
                       m_width, i * (m_lineSpacing + 1));
    }

    int lw = m_fontMetrics->width("A#2");

    for (int i = 0; i < extent; ++i) {

        MidiPitchLabel label(minPitch + i);
        std::string key = m_mapping->getMapForKeyName(minPitch + i);

        RG_DEBUG << i << ": " << label.getTQString() << ": " << key << endl;

        paint.drawText
        (2, (extent - i - 1) * (m_lineSpacing + 1) +
         m_fontMetrics->ascent() + 1,
         label.getTQString());

        paint.drawText
        (9 + lw, (extent - i - 1) * (m_lineSpacing + 1) +
         m_fontMetrics->ascent() + 1,
         strtoqstr(key));
    }
}

void PercussionPitchRuler::enterEvent(TQEvent *)
{}

void PercussionPitchRuler::leaveEvent(TQEvent*)
{
    //    m_hoverHighlight->hide();
}

void PercussionPitchRuler::drawHoverNote(int evPitch)
{
    TQPainter paint(this);
    paint.setFont(*m_font);

    if (m_lastHoverHighlight != evPitch) {

        int minPitch = m_mapping->getPitchForOffset(0);
        int extent = m_mapping->getPitchExtent();

        int lw = m_fontMetrics->width("A#2");

        if (m_lastHoverHighlight >= 0) {

            int y = (extent - (m_lastHoverHighlight - minPitch) - 1) * (m_lineSpacing + 1);
            paint.setBrush(TQColor(238, 238, 224));
            paint.setPen(TQColor(238, 238, 224));
            paint.drawRect(lw + 7, y + 1, m_width - lw, m_lineSpacing);
            std::string key = m_mapping->getMapForKeyName(m_lastHoverHighlight);
            paint.setPen(TQt::black);
            paint.drawText
            (9 + lw, y + m_fontMetrics->ascent() + 1,
             strtoqstr(key));
        }

        int y = (extent - (evPitch - minPitch) - 1) * (m_lineSpacing + 1);
        m_lastHoverHighlight = evPitch;
        paint.setBrush(paint.pen().color());
        paint.drawRect(lw + 7, y, m_width - lw, m_lineSpacing + 1);
        paint.setPen(TQColor(238, 238, 224));

        std::string key = m_mapping->getMapForKeyName(evPitch);
        paint.drawText
        (9 + lw, y + m_fontMetrics->ascent() + 1,
         strtoqstr(key));
    }
}

void PercussionPitchRuler::mouseMoveEvent(TQMouseEvent* e)
{
    // ugh

    MatrixView *matrixView = dynamic_cast<MatrixView*>(tqtopLevelWidget());
    if (matrixView) {
        MatrixStaff *staff = matrixView->getStaff(0);
        if (staff) {
            drawHoverNote(staff->getHeightAtCanvasCoords(e->x(), e->y()));
        }
    }

    if (m_mouseDown)
        if (m_selecting)
            emit keySelected(e->y(), true);
        else
            emit keyPressed(e->y(), true); // we're swooshing
    else
        emit hoveredOverKeyChanged(e->y());
}

void PercussionPitchRuler::mousePressEvent(TQMouseEvent *e)
{
    TQt::ButtonState bs = e->state();

    if (e->button() == Qt::LeftButton) {

        m_mouseDown = true;
        m_selecting = (bs & TQt::ShiftButton);

        if (m_selecting)
            emit keySelected(e->y(), false);
        else
            emit keyPressed(e->y(), false);
    }
}

void PercussionPitchRuler::mouseReleaseEvent(TQMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        m_mouseDown = false;
        m_selecting = false;
    }
}

}
#include "PercussionPitchRuler.moc"