Chris@172
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@172
|
2
|
Chris@172
|
3 /*
|
Chris@172
|
4 Sonic Visualiser
|
Chris@172
|
5 An audio file viewer and annotation editor.
|
Chris@172
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@182
|
7 This file copyright 2006 QMUL.
|
Chris@172
|
8
|
Chris@172
|
9 This program is free software; you can redistribute it and/or
|
Chris@172
|
10 modify it under the terms of the GNU General Public License as
|
Chris@172
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@172
|
12 License, or (at your option) any later version. See the file
|
Chris@172
|
13 COPYING included with this distribution for more information.
|
Chris@172
|
14 */
|
Chris@172
|
15
|
Chris@172
|
16 #include "Panner.h"
|
Chris@172
|
17
|
Chris@172
|
18 #include <QMouseEvent>
|
Chris@172
|
19 #include <QPaintEvent>
|
Chris@172
|
20 #include <QWheelEvent>
|
Chris@172
|
21 #include <QPainter>
|
Chris@172
|
22
|
Chris@172
|
23 #include <iostream>
|
Chris@181
|
24 #include <cmath>
|
Martin@175
|
25
|
Chris@172
|
26 Panner::Panner(QWidget *parent) :
|
Chris@172
|
27 QWidget(parent),
|
Chris@172
|
28 m_rectX(0),
|
Chris@172
|
29 m_rectY(0),
|
Chris@172
|
30 m_rectWidth(1),
|
Chris@173
|
31 m_rectHeight(1),
|
Chris@173
|
32 m_defaultCentreX(0),
|
Chris@173
|
33 m_defaultCentreY(0),
|
Chris@173
|
34 m_defaultsSet(false),
|
Chris@174
|
35 m_thumbColour(palette().highlightedText().color()),
|
Chris@174
|
36 m_backgroundAlpha(255),
|
Chris@174
|
37 m_thumbAlpha(255),
|
Chris@173
|
38 m_clicked(false)
|
Chris@172
|
39 {
|
Chris@172
|
40 }
|
Chris@172
|
41
|
Chris@172
|
42 Panner::~Panner()
|
Chris@172
|
43 {
|
Chris@172
|
44 }
|
Chris@172
|
45
|
Chris@172
|
46 void
|
Chris@174
|
47 Panner::setAlpha(int backgroundAlpha, int thumbAlpha)
|
Chris@174
|
48 {
|
Chris@174
|
49 m_backgroundAlpha = backgroundAlpha;
|
Chris@174
|
50 m_thumbAlpha = thumbAlpha;
|
Chris@174
|
51 }
|
Chris@174
|
52
|
Chris@174
|
53 void
|
Chris@172
|
54 Panner::mousePressEvent(QMouseEvent *e)
|
Chris@172
|
55 {
|
Chris@188
|
56 if (e->button() == Qt::MidButton ||
|
Chris@188
|
57 ((e->button() == Qt::LeftButton) &&
|
Chris@188
|
58 (e->modifiers() & Qt::ControlModifier))) {
|
Chris@188
|
59 resetToDefault();
|
Chris@188
|
60 } else if (e->button() == Qt::LeftButton) {
|
Chris@173
|
61 m_clicked = true;
|
Chris@173
|
62 m_clickPos = e->pos();
|
Chris@173
|
63 m_dragStartX = m_rectX;
|
Chris@173
|
64 m_dragStartY = m_rectY;
|
Chris@173
|
65 }
|
Chris@172
|
66 }
|
Chris@172
|
67
|
Chris@172
|
68 void
|
Chris@172
|
69 Panner::mouseDoubleClickEvent(QMouseEvent *e)
|
Chris@172
|
70 {
|
Chris@188
|
71 if (e->button() != Qt::LeftButton) {
|
Chris@188
|
72 return;
|
Chris@188
|
73 }
|
Chris@188
|
74
|
Chris@188
|
75 emit doubleClicked();
|
Chris@172
|
76 }
|
Chris@172
|
77
|
Chris@172
|
78 void
|
Chris@172
|
79 Panner::mouseMoveEvent(QMouseEvent *e)
|
Chris@172
|
80 {
|
Chris@173
|
81 if (!m_clicked) return;
|
Chris@173
|
82
|
Chris@173
|
83 float dx = float(e->pos().x() - m_clickPos.x()) / float(width());
|
Chris@173
|
84 float dy = float(e->pos().y() - m_clickPos.y()) / float(height());
|
Chris@173
|
85
|
Chris@173
|
86 m_rectX = m_dragStartX + dx;
|
Chris@173
|
87 m_rectY = m_dragStartY + dy;
|
Chris@173
|
88
|
Chris@173
|
89 normalise();
|
Chris@174
|
90 repaint();
|
Chris@174
|
91 emit rectExtentsChanged(m_rectX, m_rectY, m_rectWidth, m_rectHeight);
|
Chris@174
|
92 emit rectCentreMoved(centreX(), centreY());
|
Chris@172
|
93 }
|
Chris@172
|
94
|
Chris@172
|
95 void
|
Chris@172
|
96 Panner::mouseReleaseEvent(QMouseEvent *e)
|
Chris@172
|
97 {
|
Chris@173
|
98 if (!m_clicked) return;
|
Chris@173
|
99
|
Chris@173
|
100 mouseMoveEvent(e);
|
Chris@173
|
101 m_clicked = false;
|
Chris@172
|
102 }
|
Chris@172
|
103
|
Chris@172
|
104 void
|
Chris@172
|
105 Panner::wheelEvent(QWheelEvent *e)
|
Chris@172
|
106 {
|
Chris@173
|
107 if (e->delta() > 0) {
|
Chris@173
|
108 m_rectY += 0.1;
|
Chris@173
|
109 } else {
|
Chris@173
|
110 m_rectY -= 0.1;
|
Chris@173
|
111 }
|
Chris@173
|
112
|
Chris@173
|
113 normalise();
|
Chris@173
|
114 emitAndUpdate();
|
Chris@172
|
115 }
|
Chris@172
|
116
|
Chris@172
|
117 void
|
Chris@172
|
118 Panner::paintEvent(QPaintEvent *e)
|
Chris@172
|
119 {
|
Chris@172
|
120 QPainter paint(this);
|
Chris@172
|
121 paint.setRenderHint(QPainter::Antialiasing, false);
|
Chris@173
|
122
|
Chris@173
|
123 QColor bg(palette().background().color());
|
Chris@174
|
124 bg.setAlpha(m_backgroundAlpha);
|
Chris@173
|
125
|
Chris@172
|
126 paint.setPen(palette().dark().color());
|
Chris@173
|
127 paint.setBrush(bg);
|
Chris@173
|
128 paint.drawRect(0, 0, width(), height());
|
Chris@173
|
129
|
Chris@174
|
130 QColor hl(m_thumbColour);
|
Chris@174
|
131 hl.setAlpha(m_thumbAlpha);
|
Chris@173
|
132
|
Chris@173
|
133 paint.setBrush(hl);
|
Chris@173
|
134
|
Chris@173
|
135 paint.drawRect(lrintf(width() * m_rectX),
|
Chris@173
|
136 lrintf(height() * m_rectY),
|
Chris@173
|
137 lrintf(width() * m_rectWidth),
|
Chris@173
|
138 lrintf(height() * m_rectHeight));
|
Chris@172
|
139 }
|
Chris@172
|
140
|
Chris@172
|
141 void
|
Chris@172
|
142 Panner::normalise()
|
Chris@172
|
143 {
|
Chris@172
|
144 if (m_rectWidth > 1.0) m_rectWidth = 1.0;
|
Chris@172
|
145 if (m_rectHeight > 1.0) m_rectHeight = 1.0;
|
Chris@172
|
146 if (m_rectX + m_rectWidth > 1.0) m_rectX = 1.0 - m_rectWidth;
|
Chris@172
|
147 if (m_rectX < 0) m_rectX = 0;
|
Chris@172
|
148 if (m_rectY + m_rectHeight > 1.0) m_rectY = 1.0 - m_rectHeight;
|
Chris@172
|
149 if (m_rectY < 0) m_rectY = 0;
|
Chris@173
|
150
|
Chris@173
|
151 if (!m_defaultsSet) {
|
Chris@173
|
152 m_defaultCentreX = centreX();
|
Chris@173
|
153 m_defaultCentreY = centreY();
|
Chris@173
|
154 m_defaultsSet = true;
|
Chris@173
|
155 }
|
Chris@172
|
156 }
|
Chris@172
|
157
|
Chris@172
|
158 void
|
Chris@172
|
159 Panner::emitAndUpdate()
|
Chris@172
|
160 {
|
Chris@172
|
161 emit rectExtentsChanged(m_rectX, m_rectY, m_rectWidth, m_rectHeight);
|
Chris@173
|
162 emit rectCentreMoved(centreX(), centreY());
|
Chris@172
|
163 update();
|
Chris@172
|
164 }
|
Chris@172
|
165
|
Chris@172
|
166 void
|
Chris@188
|
167 Panner::getRectExtents(float &x0, float &y0, float &width, float &height)
|
Chris@188
|
168 {
|
Chris@188
|
169 x0 = m_rectX;
|
Chris@188
|
170 y0 = m_rectY;
|
Chris@188
|
171 width = m_rectWidth;
|
Chris@188
|
172 height = m_rectHeight;
|
Chris@188
|
173 }
|
Chris@188
|
174
|
Chris@188
|
175 void
|
Chris@172
|
176 Panner::setRectExtents(float x0, float y0, float width, float height)
|
Chris@172
|
177 {
|
Chris@174
|
178 std::cerr << "Panner::setRectExtents(" << x0 << ", " << y0 << ", "
|
Chris@174
|
179 << width << ", " << height << ")" << std::endl;
|
Chris@174
|
180
|
Chris@172
|
181 if (m_rectX == x0 &&
|
Chris@172
|
182 m_rectY == y0 &&
|
Chris@172
|
183 m_rectWidth == width &&
|
Chris@172
|
184 m_rectHeight == height) {
|
Chris@172
|
185 return;
|
Chris@172
|
186 }
|
Chris@173
|
187
|
Chris@172
|
188 m_rectX = x0;
|
Chris@172
|
189 m_rectY = y0;
|
Chris@172
|
190 m_rectWidth = width;
|
Chris@172
|
191 m_rectHeight = height;
|
Chris@173
|
192
|
Chris@172
|
193 normalise();
|
Chris@172
|
194 emitAndUpdate();
|
Chris@172
|
195 }
|
Chris@172
|
196
|
Chris@172
|
197 void
|
Chris@172
|
198 Panner::setRectWidth(float width)
|
Chris@172
|
199 {
|
Chris@172
|
200 if (m_rectWidth == width) return;
|
Chris@172
|
201 m_rectWidth = width;
|
Chris@172
|
202 normalise();
|
Chris@172
|
203 emitAndUpdate();
|
Chris@172
|
204 }
|
Chris@172
|
205
|
Chris@172
|
206 void
|
Chris@172
|
207 Panner::setRectHeight(float height)
|
Chris@172
|
208 {
|
Chris@172
|
209 if (m_rectHeight == height) return;
|
Chris@172
|
210 m_rectHeight = height;
|
Chris@172
|
211 normalise();
|
Chris@172
|
212 emitAndUpdate();
|
Chris@172
|
213 }
|
Chris@172
|
214
|
Chris@172
|
215 void
|
Chris@172
|
216 Panner::setRectCentreX(float x)
|
Chris@172
|
217 {
|
Chris@172
|
218 float x0 = x - m_rectWidth/2;
|
Chris@172
|
219 if (x0 == m_rectX) return;
|
Chris@172
|
220 m_rectX = x0;
|
Chris@172
|
221 normalise();
|
Chris@172
|
222 emitAndUpdate();
|
Chris@172
|
223 }
|
Chris@172
|
224
|
Chris@172
|
225 void
|
Chris@172
|
226 Panner::setRectCentreY(float y)
|
Chris@172
|
227 {
|
Chris@173
|
228 float y0 = y - m_rectHeight/2;
|
Chris@172
|
229 if (y0 == m_rectY) return;
|
Chris@172
|
230 m_rectY = y0;
|
Chris@172
|
231 normalise();
|
Chris@172
|
232 emitAndUpdate();
|
Chris@172
|
233 }
|
Chris@172
|
234
|
Chris@172
|
235 QSize
|
Chris@172
|
236 Panner::sizeHint() const
|
Chris@172
|
237 {
|
Chris@172
|
238 return QSize(30, 30);
|
Chris@172
|
239 }
|
Chris@172
|
240
|
Chris@173
|
241 void
|
Chris@173
|
242 Panner::setDefaultRectCentre(float cx, float cy)
|
Chris@173
|
243 {
|
Chris@173
|
244 m_defaultCentreX = cx;
|
Chris@173
|
245 m_defaultCentreY = cy;
|
Chris@173
|
246 m_defaultsSet = true;
|
Chris@173
|
247 }
|
Chris@172
|
248
|
Chris@173
|
249 void
|
Chris@173
|
250 Panner::resetToDefault()
|
Chris@173
|
251 {
|
Chris@173
|
252 float x0 = m_defaultCentreX - m_rectWidth/2;
|
Chris@173
|
253 float y0 = m_defaultCentreY - m_rectHeight/2;
|
Chris@173
|
254 if (x0 == m_rectX && y0 == m_rectY) return;
|
Chris@173
|
255 m_rectX = x0;
|
Chris@173
|
256 m_rectY = y0;
|
Chris@173
|
257 normalise();
|
Chris@173
|
258 emitAndUpdate();
|
Chris@173
|
259 }
|
Chris@172
|
260
|
Chris@173
|
261
|