Chris@173
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@173
|
2
|
Chris@173
|
3 /*
|
Chris@173
|
4 Sonic Visualiser
|
Chris@173
|
5 An audio file viewer and annotation editor.
|
Chris@173
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@182
|
7 This file copyright 2006 Chris Cannam and QMUL.
|
Chris@173
|
8
|
Chris@173
|
9 This program is free software; you can redistribute it and/or
|
Chris@173
|
10 modify it under the terms of the GNU General Public License as
|
Chris@173
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@173
|
12 License, or (at your option) any later version. See the file
|
Chris@173
|
13 COPYING included with this distribution for more information.
|
Chris@173
|
14 */
|
Chris@173
|
15
|
Chris@173
|
16 #include "Overview.h"
|
Chris@173
|
17 #include "layer/Layer.h"
|
Chris@173
|
18 #include "data/model/Model.h"
|
Chris@173
|
19 #include "base/ZoomConstraint.h"
|
Chris@173
|
20
|
Chris@173
|
21 #include <QPaintEvent>
|
Chris@173
|
22 #include <QPainter>
|
Chris@173
|
23 #include <iostream>
|
Chris@173
|
24
|
Chris@173
|
25 using std::cerr;
|
Chris@173
|
26 using std::endl;
|
Chris@173
|
27
|
Chris@173
|
28 Overview::Overview(QWidget *w) :
|
Chris@173
|
29 View(w, false),
|
Chris@173
|
30 m_clickedInRange(false)
|
Chris@173
|
31 {
|
Chris@173
|
32 setObjectName(tr("Overview"));
|
Chris@173
|
33 m_followPan = false;
|
Chris@173
|
34 m_followZoom = false;
|
Chris@211
|
35 setPlaybackFollow(PlaybackIgnore);
|
Chris@173
|
36 }
|
Chris@173
|
37
|
Chris@173
|
38 void
|
Chris@173
|
39 Overview::modelChanged(size_t startFrame, size_t endFrame)
|
Chris@173
|
40 {
|
Chris@173
|
41 View::modelChanged(startFrame, endFrame);
|
Chris@173
|
42 }
|
Chris@173
|
43
|
Chris@173
|
44 void
|
Chris@173
|
45 Overview::modelReplaced()
|
Chris@173
|
46 {
|
Chris@173
|
47 View::modelReplaced();
|
Chris@173
|
48 }
|
Chris@173
|
49
|
Chris@173
|
50 void
|
Chris@211
|
51 Overview::registerView(View *view)
|
Chris@173
|
52 {
|
Chris@211
|
53 m_views.insert(view);
|
Chris@173
|
54 update();
|
Chris@173
|
55 }
|
Chris@173
|
56
|
Chris@173
|
57 void
|
Chris@211
|
58 Overview::unregisterView(View *view)
|
Chris@173
|
59 {
|
Chris@211
|
60 m_views.erase(view);
|
Chris@173
|
61 update();
|
Chris@173
|
62 }
|
Chris@173
|
63
|
Chris@173
|
64 void
|
Chris@248
|
65 Overview::globalCentreFrameChanged(unsigned long)
|
Chris@173
|
66 {
|
Chris@211
|
67 update();
|
Chris@211
|
68 }
|
Chris@173
|
69
|
Chris@211
|
70 void
|
Chris@248
|
71 Overview::viewCentreFrameChanged(View *v, unsigned long)
|
Chris@211
|
72 {
|
Chris@211
|
73 if (m_views.find(v) != m_views.end()) {
|
Chris@173
|
74 update();
|
Chris@173
|
75 }
|
Chris@211
|
76 }
|
Chris@173
|
77
|
Chris@173
|
78 void
|
Chris@248
|
79 Overview::viewZoomLevelChanged(View *v, unsigned long, bool)
|
Chris@173
|
80 {
|
Chris@222
|
81 if (v == this) return;
|
Chris@211
|
82 if (m_views.find(v) != m_views.end()) {
|
Chris@173
|
83 update();
|
Chris@173
|
84 }
|
Chris@173
|
85 }
|
Chris@173
|
86
|
Chris@173
|
87 void
|
Chris@173
|
88 Overview::viewManagerPlaybackFrameChanged(unsigned long f)
|
Chris@173
|
89 {
|
Chris@173
|
90 bool changed = false;
|
Chris@173
|
91
|
Chris@173
|
92 if (getXForFrame(m_playPointerFrame) != getXForFrame(f)) changed = true;
|
Chris@173
|
93 m_playPointerFrame = f;
|
Chris@173
|
94
|
Chris@173
|
95 if (changed) update();
|
Chris@173
|
96 }
|
Chris@173
|
97
|
Chris@173
|
98 void
|
Chris@173
|
99 Overview::paintEvent(QPaintEvent *e)
|
Chris@173
|
100 {
|
Chris@173
|
101 // Recalculate zoom in case the size of the widget has changed.
|
Chris@173
|
102
|
Chris@214
|
103 // std::cerr << "Overview::paintEvent: width is " << width() << ", centre frame " << m_centreFrame << std::endl;
|
Chris@214
|
104
|
Chris@173
|
105 size_t startFrame = getModelsStartFrame();
|
Chris@173
|
106 size_t frameCount = getModelsEndFrame() - getModelsStartFrame();
|
Chris@173
|
107 int zoomLevel = frameCount / width();
|
Chris@173
|
108 if (zoomLevel < 1) zoomLevel = 1;
|
Chris@173
|
109 zoomLevel = getZoomConstraintBlockSize(zoomLevel,
|
Chris@173
|
110 ZoomConstraint::RoundUp);
|
Chris@173
|
111 if (zoomLevel != m_zoomLevel) {
|
Chris@173
|
112 m_zoomLevel = zoomLevel;
|
Chris@222
|
113 emit zoomLevelChanged(m_zoomLevel, m_followZoom);
|
Chris@173
|
114 }
|
Chris@173
|
115 size_t centreFrame = startFrame + m_zoomLevel * (width() / 2);
|
Chris@173
|
116 if (centreFrame > (startFrame + getModelsEndFrame())/2) {
|
Chris@173
|
117 centreFrame = (startFrame + getModelsEndFrame())/2;
|
Chris@173
|
118 }
|
Chris@173
|
119 if (centreFrame != m_centreFrame) {
|
Chris@214
|
120 // std::cerr << "Overview::paintEvent: Centre frame changed from "
|
Chris@214
|
121 // << m_centreFrame << " to " << centreFrame << " and thus start frame from " << getStartFrame();
|
Chris@173
|
122 m_centreFrame = centreFrame;
|
Chris@214
|
123 // std::cerr << " to " << getStartFrame() << std::endl;
|
Chris@211
|
124 emit centreFrameChanged(m_centreFrame, false, PlaybackIgnore);
|
Chris@173
|
125 }
|
Chris@173
|
126
|
Chris@173
|
127 View::paintEvent(e);
|
Chris@173
|
128
|
Chris@173
|
129 QPainter paint;
|
Chris@173
|
130 paint.begin(this);
|
Chris@173
|
131
|
Chris@173
|
132 QRect r(rect());
|
Chris@173
|
133
|
Chris@173
|
134 if (e) {
|
Chris@173
|
135 r = e->rect();
|
Chris@173
|
136 paint.setClipRect(r);
|
Chris@173
|
137 }
|
Chris@173
|
138
|
Chris@173
|
139 paint.setPen(Qt::black);
|
Chris@173
|
140
|
Chris@173
|
141 int y = 0;
|
Chris@173
|
142
|
Chris@173
|
143 int prevx0 = -10;
|
Chris@173
|
144 int prevx1 = -10;
|
Chris@173
|
145
|
Chris@211
|
146 for (ViewSet::iterator i = m_views.begin(); i != m_views.end(); ++i) {
|
Chris@173
|
147 if (!*i) continue;
|
Chris@173
|
148
|
Chris@173
|
149 View *w = (View *)*i;
|
Chris@173
|
150
|
Chris@173
|
151 long f0 = w->getFrameForX(0);
|
Chris@173
|
152 long f1 = w->getFrameForX(w->width());
|
Chris@173
|
153
|
Chris@173
|
154 int x0 = getXForFrame(f0);
|
Chris@173
|
155 int x1 = getXForFrame(f1);
|
Chris@173
|
156
|
Chris@173
|
157 if (x0 != prevx0 || x1 != prevx1) {
|
Chris@173
|
158 y += height() / 10 + 1;
|
Chris@173
|
159 prevx0 = x0;
|
Chris@173
|
160 prevx1 = x1;
|
Chris@173
|
161 }
|
Chris@173
|
162
|
Chris@173
|
163 if (x1 <= x0) x1 = x0 + 1;
|
Chris@173
|
164
|
Chris@173
|
165 paint.drawRect(x0, y, x1 - x0, height() - 2 * y);
|
Chris@173
|
166 }
|
Chris@173
|
167
|
Chris@173
|
168 paint.end();
|
Chris@173
|
169 }
|
Chris@173
|
170
|
Chris@173
|
171 void
|
Chris@173
|
172 Overview::mousePressEvent(QMouseEvent *e)
|
Chris@173
|
173 {
|
Chris@173
|
174 m_clickPos = e->pos();
|
Chris@211
|
175 for (ViewSet::iterator i = m_views.begin(); i != m_views.end(); ++i) {
|
Chris@173
|
176 if (*i) {
|
Chris@173
|
177 m_clickedInRange = true;
|
Chris@173
|
178 m_dragCentreFrame = ((View *)*i)->getCentreFrame();
|
Chris@173
|
179 break;
|
Chris@173
|
180 }
|
Chris@173
|
181 }
|
Chris@173
|
182 }
|
Chris@173
|
183
|
Chris@173
|
184 void
|
Chris@173
|
185 Overview::mouseReleaseEvent(QMouseEvent *e)
|
Chris@173
|
186 {
|
Chris@173
|
187 if (m_clickedInRange) {
|
Chris@173
|
188 mouseMoveEvent(e);
|
Chris@173
|
189 }
|
Chris@173
|
190 m_clickedInRange = false;
|
Chris@173
|
191 }
|
Chris@173
|
192
|
Chris@173
|
193 void
|
Chris@173
|
194 Overview::mouseMoveEvent(QMouseEvent *e)
|
Chris@173
|
195 {
|
Chris@173
|
196 if (!m_clickedInRange) return;
|
Chris@173
|
197
|
Chris@173
|
198 long xoff = int(e->x()) - int(m_clickPos.x());
|
Chris@173
|
199 long frameOff = xoff * m_zoomLevel;
|
Chris@173
|
200
|
Chris@173
|
201 size_t newCentreFrame = m_dragCentreFrame;
|
Chris@173
|
202 if (frameOff > 0) {
|
Chris@173
|
203 newCentreFrame += frameOff;
|
Chris@173
|
204 } else if (newCentreFrame >= size_t(-frameOff)) {
|
Chris@173
|
205 newCentreFrame += frameOff;
|
Chris@173
|
206 } else {
|
Chris@173
|
207 newCentreFrame = 0;
|
Chris@173
|
208 }
|
Chris@173
|
209
|
Chris@173
|
210 if (newCentreFrame >= getModelsEndFrame()) {
|
Chris@173
|
211 newCentreFrame = getModelsEndFrame();
|
Chris@173
|
212 if (newCentreFrame > 0) --newCentreFrame;
|
Chris@173
|
213 }
|
Chris@173
|
214
|
Chris@173
|
215 if (std::max(m_centreFrame, newCentreFrame) -
|
Chris@173
|
216 std::min(m_centreFrame, newCentreFrame) > size_t(m_zoomLevel)) {
|
Chris@213
|
217 emit centreFrameChanged(newCentreFrame, true, PlaybackScrollContinuous);
|
Chris@173
|
218 }
|
Chris@173
|
219 }
|
Chris@173
|
220
|
Chris@189
|
221 void
|
Chris@189
|
222 Overview::mouseDoubleClickEvent(QMouseEvent *e)
|
Chris@189
|
223 {
|
Chris@189
|
224 long frame = getFrameForX(e->x());
|
Chris@213
|
225 emit centreFrameChanged(frame, true, PlaybackScrollContinuous);
|
Chris@189
|
226 }
|
Chris@173
|
227
|
Chris@189
|
228 void
|
Chris@189
|
229 Overview::enterEvent(QEvent *)
|
Chris@189
|
230 {
|
Chris@189
|
231 emit contextHelpChanged(tr("Click and drag to navigate; double-click to jump"));
|
Chris@189
|
232 }
|
Chris@189
|
233
|
Chris@189
|
234 void
|
Chris@189
|
235 Overview::leaveEvent(QEvent *)
|
Chris@189
|
236 {
|
Chris@189
|
237 emit contextHelpChanged("");
|
Chris@189
|
238 }
|
Chris@189
|
239
|
Chris@189
|
240
|