File: Panner.cpp

package info (click to toggle)
sonic-visualiser 5.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,744 kB
  • sloc: cpp: 158,888; ansic: 11,920; sh: 1,785; makefile: 517; xml: 64; perl: 31
file content (315 lines) | stat: -rw-r--r-- 6,475 bytes parent folder | download | duplicates (2)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.
    Centre for Digital Music, Queen Mary, University of London.
    This file copyright 2006 QMUL.
    
    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 "Panner.h"

#include <QMouseEvent>
#include <QPaintEvent>
#include <QWheelEvent>
#include <QPainter>

#include "WidgetScale.h"

#include <iostream>
#include <cmath>

namespace sv {

Panner::Panner(QWidget *parent) :
    QWidget(parent),
    m_rectX(0),
    m_rectY(0),
    m_rectWidth(1),
    m_rectHeight(1),
    m_scrollUnit(0),
    m_defaultCentreX(0),
    m_defaultCentreY(0),
    m_defaultsSet(false),
    m_thumbColour(palette().highlightedText().color()),
    m_backgroundAlpha(255),
    m_thumbAlpha(255),
    m_clicked(false),
    m_dragStartX(0),
    m_dragStartY(0)
{
}

Panner::~Panner()
{
}

void
Panner::setAlpha(int backgroundAlpha, int thumbAlpha)
{
    m_backgroundAlpha = backgroundAlpha;
    m_thumbAlpha = thumbAlpha;
}

void
Panner::setScrollUnit(float unit)
{
    m_scrollUnit = unit;
}

void
Panner::scroll(bool up)
{
    scroll(up, 1);
}

void
Panner::scroll(bool up, int count)
{
    float unit = m_scrollUnit;
    if (unit == 0.f) {
        unit = float(m_rectHeight) / (6 * float(height()));
        if (unit < 0.01f) unit = 0.01f;
    }

    if (!up) {
        m_rectY += unit * float(count);
    } else {
        m_rectY -= unit * float(count);
    }

    normalise();
    emitAndUpdate();
}

void
Panner::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::MiddleButton ||
        ((e->button() == Qt::LeftButton) &&
         (e->modifiers() & Qt::ControlModifier))) {
        resetToDefault();
    } else if (e->button() == Qt::LeftButton) {
        m_clicked = true;
        m_clickPos = e->pos();
        m_dragStartX = m_rectX;
        m_dragStartY = m_rectY;
    }
}

void
Panner::mouseDoubleClickEvent(QMouseEvent *e)
{
    if (e->button() != Qt::LeftButton) {
        return;
    }

    emit doubleClicked();
}

void
Panner::mouseMoveEvent(QMouseEvent *e)
{
    if (!m_clicked) return;

    float dx = float(e->pos().x() - m_clickPos.x()) / float(width());
    float dy = float(e->pos().y() - m_clickPos.y()) / float(height());
    
    m_rectX = m_dragStartX + dx;
    m_rectY = m_dragStartY + dy;
    
    normalise();
    repaint();
    emit rectExtentsChanged(m_rectX, m_rectY, m_rectWidth, m_rectHeight);
    emit rectCentreMoved(centreX(), centreY());
}

void
Panner::mouseReleaseEvent(QMouseEvent *e)
{
    if (!m_clicked) return;

    mouseMoveEvent(e);
    m_clicked = false;
}

void
Panner::wheelEvent(QWheelEvent *e)
{
    int delta = m_wheelCounter.count(e);
    scroll(delta > 0, abs(delta));
}

void
Panner::enterEvent(QEnterEvent *)
{
    emit mouseEntered();
}

void
Panner::leaveEvent(QEvent *)
{
    emit mouseLeft();
}

void
Panner::paintEvent(QPaintEvent *)
{
    QPainter paint(this);
    paint.setRenderHint(QPainter::Antialiasing, false);

    QColor bg(palette().window().color());
    bg.setAlpha(m_backgroundAlpha);

    int penWidth = WidgetScale::scalePixelSize(1);
    if (penWidth < 1) penWidth = 1;
    paint.setPen(QPen(palette().dark().color(), penWidth));
    
    paint.setBrush(bg);
    paint.drawRect(penWidth/2, penWidth/2,
                   width()-penWidth/2-1, height()-penWidth/2-1);

    QColor hl(m_thumbColour);
    hl.setAlpha(m_thumbAlpha);

    paint.setBrush(hl);

    int rw = int(lrintf(float(width() - 1) * m_rectWidth));
    int rh = int(lrintf(float(height() - 1) * m_rectHeight));
    if (rw < 2) rw = 2;
    if (rh < 2) rh = 2;

    paint.drawRect(int(lrintf(float(width()) * m_rectX)),
                   int(lrintf(float(height()) * m_rectY)),
                   rw, rh);
}

void
Panner::normalise()
{
    if (m_rectWidth > 1.f) m_rectWidth = 1.f;
    if (m_rectHeight > 1.f) m_rectHeight = 1.f;
    if (m_rectX + m_rectWidth > 1.f) m_rectX = 1.f - m_rectWidth;
    if (m_rectX < 0) m_rectX = 0;
    if (m_rectY + m_rectHeight > 1.f) m_rectY = 1.f - m_rectHeight;
    if (m_rectY < 0) m_rectY = 0;

    if (!m_defaultsSet) {
        m_defaultCentreX = centreX();
        m_defaultCentreY = centreY();
        m_defaultsSet = true;
    }
}

void
Panner::emitAndUpdate()
{
    emit rectExtentsChanged(m_rectX, m_rectY, m_rectWidth, m_rectHeight);
    emit rectCentreMoved(centreX(), centreY());
    update();
}  

void
Panner::getRectExtents(float &x0, float &y0, float &width, float &height)
{
    x0 = m_rectX;
    y0 = m_rectY;
    width = m_rectWidth;
    height = m_rectHeight;
}

void
Panner::setRectExtents(float x0, float y0, float width, float height)
{
//    SVDEBUG << "Panner::setRectExtents(" << x0 << ", " << y0 << ", "
//              << width << ", " << height << ")" << endl;

    if (m_rectX == x0 &&
        m_rectY == y0 &&
        m_rectWidth == width &&
        m_rectHeight == height) {
        return;
    }

    m_rectX = x0;
    m_rectY = y0;
    m_rectWidth = width;
    m_rectHeight = height;

    normalise();
    emitAndUpdate();
}

void
Panner::setRectWidth(float width)
{
    if (m_rectWidth == width) return;
    m_rectWidth = width;
    normalise();
    emitAndUpdate();
}

void
Panner::setRectHeight(float height)
{
    if (m_rectHeight == height) return;
    m_rectHeight = height;
    normalise();
    emitAndUpdate();
}

void
Panner::setRectCentreX(float x)
{
    float x0 = x - m_rectWidth/2;
    if (x0 == m_rectX) return;
    m_rectX = x0;
    normalise();
    emitAndUpdate();
}

void
Panner::setRectCentreY(float y)
{
    float y0 = y - m_rectHeight/2;
    if (y0 == m_rectY) return;
    m_rectY = y0;
    normalise();
    emitAndUpdate();
}

QSize
Panner::sizeHint() const
{
    return QSize(30, 30);
}

void
Panner::setDefaultRectCentre(float cx, float cy)
{
    m_defaultCentreX = cx;
    m_defaultCentreY = cy;
    m_defaultsSet = true;
}

void
Panner::resetToDefault()
{
    float x0 = m_defaultCentreX - m_rectWidth/2;
    float y0 = m_defaultCentreY - m_rectHeight/2;
    if (x0 == m_rectX && y0 == m_rectY) return;
    m_rectX = x0;
    m_rectY = y0;
    normalise();
    emitAndUpdate();
}


} // end namespace sv