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