Chris@127
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@127
|
2
|
Chris@127
|
3 /*
|
Chris@127
|
4 Sonic Visualiser
|
Chris@127
|
5 An audio file viewer and annotation editor.
|
Chris@127
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@127
|
7 This file copyright 2006 Chris Cannam.
|
Chris@127
|
8
|
Chris@127
|
9 This program is free software; you can redistribute it and/or
|
Chris@127
|
10 modify it under the terms of the GNU General Public License as
|
Chris@127
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@127
|
12 License, or (at your option) any later version. See the file
|
Chris@127
|
13 COPYING included with this distribution for more information.
|
Chris@127
|
14 */
|
Chris@127
|
15
|
Chris@127
|
16 #include "ViewManager.h"
|
Chris@127
|
17 #include "AudioPlaySource.h"
|
Chris@127
|
18 #include "Model.h"
|
Chris@127
|
19 #include "CommandHistory.h"
|
Chris@127
|
20
|
Chris@127
|
21 #include <iostream>
|
Chris@127
|
22
|
Chris@127
|
23 // #define DEBUG_VIEW_MANAGER 1
|
Chris@127
|
24
|
Chris@127
|
25 ViewManager::ViewManager() :
|
Chris@127
|
26 m_playSource(0),
|
Chris@127
|
27 m_globalCentreFrame(0),
|
Chris@127
|
28 m_globalZoom(1024),
|
Chris@127
|
29 m_playbackFrame(0),
|
Chris@127
|
30 m_mainModelSampleRate(0),
|
Chris@127
|
31 m_lastLeft(0),
|
Chris@127
|
32 m_lastRight(0),
|
Chris@127
|
33 m_inProgressExclusive(true),
|
Chris@127
|
34 m_toolMode(NavigateMode),
|
Chris@127
|
35 m_playLoopMode(false),
|
Chris@127
|
36 m_playSelectionMode(false),
|
Chris@127
|
37 m_overlayMode(BasicOverlays)
|
Chris@127
|
38 {
|
Chris@127
|
39 connect(this,
|
Chris@127
|
40 SIGNAL(centreFrameChanged(void *, unsigned long, bool)),
|
Chris@127
|
41 SLOT(considerSeek(void *, unsigned long, bool)));
|
Chris@127
|
42
|
Chris@127
|
43 connect(this,
|
Chris@127
|
44 SIGNAL(zoomLevelChanged(void *, unsigned long, bool)),
|
Chris@127
|
45 SLOT(considerZoomChange(void *, unsigned long, bool)));
|
Chris@127
|
46 }
|
Chris@127
|
47
|
Chris@127
|
48 ViewManager::~ViewManager()
|
Chris@127
|
49 {
|
Chris@127
|
50 }
|
Chris@127
|
51
|
Chris@127
|
52 unsigned long
|
Chris@127
|
53 ViewManager::getGlobalCentreFrame() const
|
Chris@127
|
54 {
|
Chris@127
|
55 #ifdef DEBUG_VIEW_MANAGER
|
Chris@127
|
56 std::cout << "ViewManager::getGlobalCentreFrame: returning " << m_globalCentreFrame << std::endl;
|
Chris@127
|
57 #endif
|
Chris@127
|
58 return m_globalCentreFrame;
|
Chris@127
|
59 }
|
Chris@127
|
60
|
Chris@127
|
61 unsigned long
|
Chris@127
|
62 ViewManager::getGlobalZoom() const
|
Chris@127
|
63 {
|
Chris@127
|
64 #ifdef DEBUG_VIEW_MANAGER
|
Chris@127
|
65 std::cout << "ViewManager::getGlobalZoom: returning " << m_globalZoom << std::endl;
|
Chris@127
|
66 #endif
|
Chris@127
|
67 return m_globalZoom;
|
Chris@127
|
68 }
|
Chris@127
|
69
|
Chris@127
|
70 unsigned long
|
Chris@127
|
71 ViewManager::getPlaybackFrame() const
|
Chris@127
|
72 {
|
Chris@127
|
73 if (m_playSource && m_playSource->isPlaying()) {
|
Chris@127
|
74 m_playbackFrame = m_playSource->getCurrentPlayingFrame();
|
Chris@127
|
75 }
|
Chris@127
|
76 return m_playbackFrame;
|
Chris@127
|
77 }
|
Chris@127
|
78
|
Chris@127
|
79 void
|
Chris@127
|
80 ViewManager::setPlaybackFrame(unsigned long f)
|
Chris@127
|
81 {
|
Chris@127
|
82 if (m_playbackFrame != f) {
|
Chris@127
|
83 m_playbackFrame = f;
|
Chris@127
|
84 emit playbackFrameChanged(f);
|
Chris@127
|
85 if (m_playSource && m_playSource->isPlaying()) {
|
Chris@127
|
86 m_playSource->play(f);
|
Chris@127
|
87 }
|
Chris@127
|
88 }
|
Chris@127
|
89 }
|
Chris@127
|
90
|
Chris@127
|
91 bool
|
Chris@127
|
92 ViewManager::haveInProgressSelection() const
|
Chris@127
|
93 {
|
Chris@127
|
94 return !m_inProgressSelection.isEmpty();
|
Chris@127
|
95 }
|
Chris@127
|
96
|
Chris@127
|
97 const Selection &
|
Chris@127
|
98 ViewManager::getInProgressSelection(bool &exclusive) const
|
Chris@127
|
99 {
|
Chris@127
|
100 exclusive = m_inProgressExclusive;
|
Chris@127
|
101 return m_inProgressSelection;
|
Chris@127
|
102 }
|
Chris@127
|
103
|
Chris@127
|
104 void
|
Chris@127
|
105 ViewManager::setInProgressSelection(const Selection &selection, bool exclusive)
|
Chris@127
|
106 {
|
Chris@127
|
107 m_inProgressExclusive = exclusive;
|
Chris@127
|
108 m_inProgressSelection = selection;
|
Chris@127
|
109 if (exclusive) clearSelections();
|
Chris@127
|
110 emit inProgressSelectionChanged();
|
Chris@127
|
111 }
|
Chris@127
|
112
|
Chris@127
|
113 void
|
Chris@127
|
114 ViewManager::clearInProgressSelection()
|
Chris@127
|
115 {
|
Chris@127
|
116 m_inProgressSelection = Selection();
|
Chris@127
|
117 emit inProgressSelectionChanged();
|
Chris@127
|
118 }
|
Chris@127
|
119
|
Chris@127
|
120 const MultiSelection &
|
Chris@127
|
121 ViewManager::getSelection() const
|
Chris@127
|
122 {
|
Chris@127
|
123 return m_selections;
|
Chris@127
|
124 }
|
Chris@127
|
125
|
Chris@127
|
126 const MultiSelection::SelectionList &
|
Chris@127
|
127 ViewManager::getSelections() const
|
Chris@127
|
128 {
|
Chris@127
|
129 return m_selections.getSelections();
|
Chris@127
|
130 }
|
Chris@127
|
131
|
Chris@127
|
132 void
|
Chris@127
|
133 ViewManager::setSelection(const Selection &selection)
|
Chris@127
|
134 {
|
Chris@127
|
135 MultiSelection ms(m_selections);
|
Chris@127
|
136 ms.setSelection(selection);
|
Chris@127
|
137 setSelections(ms);
|
Chris@127
|
138 }
|
Chris@127
|
139
|
Chris@127
|
140 void
|
Chris@127
|
141 ViewManager::addSelection(const Selection &selection)
|
Chris@127
|
142 {
|
Chris@127
|
143 MultiSelection ms(m_selections);
|
Chris@127
|
144 ms.addSelection(selection);
|
Chris@127
|
145 setSelections(ms);
|
Chris@127
|
146 }
|
Chris@127
|
147
|
Chris@127
|
148 void
|
Chris@127
|
149 ViewManager::removeSelection(const Selection &selection)
|
Chris@127
|
150 {
|
Chris@127
|
151 MultiSelection ms(m_selections);
|
Chris@127
|
152 ms.removeSelection(selection);
|
Chris@127
|
153 setSelections(ms);
|
Chris@127
|
154 }
|
Chris@127
|
155
|
Chris@127
|
156 void
|
Chris@127
|
157 ViewManager::clearSelections()
|
Chris@127
|
158 {
|
Chris@127
|
159 MultiSelection ms(m_selections);
|
Chris@127
|
160 ms.clearSelections();
|
Chris@127
|
161 setSelections(ms);
|
Chris@127
|
162 }
|
Chris@127
|
163
|
Chris@127
|
164 void
|
Chris@127
|
165 ViewManager::setSelections(const MultiSelection &ms)
|
Chris@127
|
166 {
|
Chris@127
|
167 if (m_selections.getSelections() == ms.getSelections()) return;
|
Chris@127
|
168 SetSelectionCommand *command = new SetSelectionCommand(this, ms);
|
Chris@127
|
169 CommandHistory::getInstance()->addCommand(command);
|
Chris@127
|
170 }
|
Chris@127
|
171
|
Chris@127
|
172 void
|
Chris@127
|
173 ViewManager::signalSelectionChange()
|
Chris@127
|
174 {
|
Chris@127
|
175 emit selectionChanged();
|
Chris@127
|
176 }
|
Chris@127
|
177
|
Chris@127
|
178 ViewManager::SetSelectionCommand::SetSelectionCommand(ViewManager *vm,
|
Chris@127
|
179 const MultiSelection &ms) :
|
Chris@127
|
180 m_vm(vm),
|
Chris@127
|
181 m_oldSelection(vm->m_selections),
|
Chris@127
|
182 m_newSelection(ms)
|
Chris@127
|
183 {
|
Chris@127
|
184 }
|
Chris@127
|
185
|
Chris@127
|
186 ViewManager::SetSelectionCommand::~SetSelectionCommand() { }
|
Chris@127
|
187
|
Chris@127
|
188 void
|
Chris@127
|
189 ViewManager::SetSelectionCommand::execute()
|
Chris@127
|
190 {
|
Chris@127
|
191 m_vm->m_selections = m_newSelection;
|
Chris@127
|
192 m_vm->signalSelectionChange();
|
Chris@127
|
193 }
|
Chris@127
|
194
|
Chris@127
|
195 void
|
Chris@127
|
196 ViewManager::SetSelectionCommand::unexecute()
|
Chris@127
|
197 {
|
Chris@127
|
198 m_vm->m_selections = m_oldSelection;
|
Chris@127
|
199 m_vm->signalSelectionChange();
|
Chris@127
|
200 }
|
Chris@127
|
201
|
Chris@127
|
202 QString
|
Chris@127
|
203 ViewManager::SetSelectionCommand::getName() const
|
Chris@127
|
204 {
|
Chris@127
|
205 if (m_newSelection.getSelections().empty()) return tr("Clear Selection");
|
Chris@127
|
206 else return tr("Select");
|
Chris@127
|
207 }
|
Chris@127
|
208
|
Chris@127
|
209 Selection
|
Chris@127
|
210 ViewManager::getContainingSelection(size_t frame, bool defaultToFollowing) const
|
Chris@127
|
211 {
|
Chris@127
|
212 return m_selections.getContainingSelection(frame, defaultToFollowing);
|
Chris@127
|
213 }
|
Chris@127
|
214
|
Chris@127
|
215 void
|
Chris@127
|
216 ViewManager::setToolMode(ToolMode mode)
|
Chris@127
|
217 {
|
Chris@127
|
218 m_toolMode = mode;
|
Chris@127
|
219
|
Chris@127
|
220 emit toolModeChanged();
|
Chris@127
|
221 }
|
Chris@127
|
222
|
Chris@127
|
223 void
|
Chris@127
|
224 ViewManager::setPlayLoopMode(bool mode)
|
Chris@127
|
225 {
|
Chris@127
|
226 m_playLoopMode = mode;
|
Chris@127
|
227
|
Chris@127
|
228 emit playLoopModeChanged();
|
Chris@127
|
229 }
|
Chris@127
|
230
|
Chris@127
|
231 void
|
Chris@127
|
232 ViewManager::setPlaySelectionMode(bool mode)
|
Chris@127
|
233 {
|
Chris@127
|
234 m_playSelectionMode = mode;
|
Chris@127
|
235
|
Chris@127
|
236 emit playSelectionModeChanged();
|
Chris@127
|
237 }
|
Chris@127
|
238
|
Chris@127
|
239 size_t
|
Chris@127
|
240 ViewManager::getPlaybackSampleRate() const
|
Chris@127
|
241 {
|
Chris@127
|
242 if (m_playSource) {
|
Chris@127
|
243 return m_playSource->getTargetSampleRate();
|
Chris@127
|
244 }
|
Chris@127
|
245 return 0;
|
Chris@127
|
246 }
|
Chris@127
|
247
|
Chris@127
|
248 void
|
Chris@127
|
249 ViewManager::setAudioPlaySource(AudioPlaySource *source)
|
Chris@127
|
250 {
|
Chris@127
|
251 if (!m_playSource) {
|
Chris@127
|
252 QTimer::singleShot(100, this, SLOT(checkPlayStatus()));
|
Chris@127
|
253 }
|
Chris@127
|
254 m_playSource = source;
|
Chris@127
|
255 }
|
Chris@127
|
256
|
Chris@127
|
257 void
|
Chris@127
|
258 ViewManager::playStatusChanged(bool playing)
|
Chris@127
|
259 {
|
Chris@127
|
260 checkPlayStatus();
|
Chris@127
|
261 }
|
Chris@127
|
262
|
Chris@127
|
263 void
|
Chris@127
|
264 ViewManager::checkPlayStatus()
|
Chris@127
|
265 {
|
Chris@127
|
266 if (m_playSource && m_playSource->isPlaying()) {
|
Chris@127
|
267
|
Chris@127
|
268 float left = 0, right = 0;
|
Chris@127
|
269 if (m_playSource->getOutputLevels(left, right)) {
|
Chris@127
|
270 if (left != m_lastLeft || right != m_lastRight) {
|
Chris@127
|
271 emit outputLevelsChanged(left, right);
|
Chris@127
|
272 m_lastLeft = left;
|
Chris@127
|
273 m_lastRight = right;
|
Chris@127
|
274 }
|
Chris@127
|
275 }
|
Chris@127
|
276
|
Chris@127
|
277 m_playbackFrame = m_playSource->getCurrentPlayingFrame();
|
Chris@127
|
278
|
Chris@127
|
279 #ifdef DEBUG_VIEW_MANAGER
|
Chris@127
|
280 std::cout << "ViewManager::checkPlayStatus: Playing, frame " << m_playbackFrame << ", levels " << m_lastLeft << "," << m_lastRight << std::endl;
|
Chris@127
|
281 #endif
|
Chris@127
|
282
|
Chris@127
|
283 emit playbackFrameChanged(m_playbackFrame);
|
Chris@127
|
284
|
Chris@127
|
285 QTimer::singleShot(20, this, SLOT(checkPlayStatus()));
|
Chris@127
|
286
|
Chris@127
|
287 } else {
|
Chris@127
|
288
|
Chris@127
|
289 QTimer::singleShot(100, this, SLOT(checkPlayStatus()));
|
Chris@127
|
290
|
Chris@127
|
291 if (m_lastLeft != 0.0 || m_lastRight != 0.0) {
|
Chris@127
|
292 emit outputLevelsChanged(0.0, 0.0);
|
Chris@127
|
293 m_lastLeft = 0.0;
|
Chris@127
|
294 m_lastRight = 0.0;
|
Chris@127
|
295 }
|
Chris@127
|
296
|
Chris@127
|
297 #ifdef DEBUG_VIEW_MANAGER
|
Chris@127
|
298 // std::cout << "ViewManager::checkPlayStatus: Not playing" << std::endl;
|
Chris@127
|
299 #endif
|
Chris@127
|
300 }
|
Chris@127
|
301 }
|
Chris@127
|
302
|
Chris@127
|
303 bool
|
Chris@127
|
304 ViewManager::isPlaying() const
|
Chris@127
|
305 {
|
Chris@127
|
306 return m_playSource && m_playSource->isPlaying();
|
Chris@127
|
307 }
|
Chris@127
|
308
|
Chris@127
|
309 void
|
Chris@127
|
310 ViewManager::considerSeek(void *p, unsigned long f, bool locked)
|
Chris@127
|
311 {
|
Chris@127
|
312 if (locked) {
|
Chris@127
|
313 m_globalCentreFrame = f;
|
Chris@127
|
314 }
|
Chris@127
|
315
|
Chris@127
|
316 #ifdef DEBUG_VIEW_MANAGER
|
Chris@127
|
317 std::cout << "ViewManager::considerSeek(" << p << ", " << f << ", " << locked << ")" << std::endl;
|
Chris@127
|
318 #endif
|
Chris@127
|
319
|
Chris@127
|
320 if (p == this || !locked) return;
|
Chris@127
|
321
|
Chris@127
|
322 if (m_playSource && m_playSource->isPlaying()) {
|
Chris@127
|
323 unsigned long playFrame = m_playSource->getCurrentPlayingFrame();
|
Chris@127
|
324 unsigned long diff = std::max(f, playFrame) - std::min(f, playFrame);
|
Chris@127
|
325 if (diff > 20000) {
|
Chris@127
|
326 m_playbackFrame = f;
|
Chris@127
|
327 m_playSource->play(f);
|
Chris@127
|
328 #ifdef DEBUG_VIEW_MANAGER
|
Chris@127
|
329 std::cout << "ViewManager::considerSeek: reseeking from " << playFrame << " to " << f << std::endl;
|
Chris@127
|
330 #endif
|
Chris@127
|
331 }
|
Chris@127
|
332 } else {
|
Chris@127
|
333 m_playbackFrame = f; //!!! only if that view is in scroll mode?
|
Chris@127
|
334 }
|
Chris@127
|
335 }
|
Chris@127
|
336
|
Chris@127
|
337 void
|
Chris@127
|
338 ViewManager::considerZoomChange(void *p, unsigned long z, bool locked)
|
Chris@127
|
339 {
|
Chris@127
|
340 if (locked) {
|
Chris@127
|
341 m_globalZoom = z;
|
Chris@127
|
342 }
|
Chris@127
|
343
|
Chris@127
|
344 #ifdef DEBUG_VIEW_MANAGER
|
Chris@127
|
345 std::cout << "ViewManager::considerZoomChange(" << p << ", " << z << ", " << locked << ")" << std::endl;
|
Chris@127
|
346 #endif
|
Chris@127
|
347 }
|
Chris@127
|
348
|
Chris@127
|
349 void
|
Chris@127
|
350 ViewManager::setOverlayMode(OverlayMode mode)
|
Chris@127
|
351 {
|
Chris@127
|
352 if (m_overlayMode != mode) {
|
Chris@127
|
353 m_overlayMode = mode;
|
Chris@127
|
354 emit overlayModeChanged();
|
Chris@127
|
355 }
|
Chris@127
|
356 }
|
Chris@127
|
357
|
Chris@127
|
358 #ifdef INCLUDE_MOCFILES
|
Chris@127
|
359 #include "ViewManager.moc.cpp"
|
Chris@127
|
360 #endif
|
Chris@127
|
361
|