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@20
|
36 m_playSelectionMode(false)
|
Chris@0
|
37 {
|
Chris@0
|
38 connect(this,
|
Chris@0
|
39 SIGNAL(centreFrameChanged(void *, unsigned long, bool)),
|
Chris@0
|
40 SLOT(considerSeek(void *, unsigned long, bool)));
|
Chris@0
|
41
|
Chris@0
|
42 connect(this,
|
Chris@0
|
43 SIGNAL(zoomLevelChanged(void *, unsigned long, bool)),
|
Chris@0
|
44 SLOT(considerZoomChange(void *, unsigned long, bool)));
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 unsigned long
|
Chris@0
|
48 ViewManager::getGlobalCentreFrame() const
|
Chris@0
|
49 {
|
Chris@0
|
50 #ifdef DEBUG_VIEW_MANAGER
|
Chris@0
|
51 std::cout << "ViewManager::getGlobalCentreFrame: returning " << m_globalCentreFrame << std::endl;
|
Chris@0
|
52 #endif
|
Chris@0
|
53 return m_globalCentreFrame;
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 unsigned long
|
Chris@0
|
57 ViewManager::getGlobalZoom() const
|
Chris@0
|
58 {
|
Chris@0
|
59 #ifdef DEBUG_VIEW_MANAGER
|
Chris@0
|
60 std::cout << "ViewManager::getGlobalZoom: returning " << m_globalZoom << std::endl;
|
Chris@0
|
61 #endif
|
Chris@0
|
62 return m_globalZoom;
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@24
|
65 unsigned long
|
Chris@24
|
66 ViewManager::getPlaybackFrame() const
|
Chris@24
|
67 {
|
Chris@24
|
68 if (m_playSource && m_playSource->isPlaying()) {
|
Chris@24
|
69 m_playbackFrame = m_playSource->getCurrentPlayingFrame();
|
Chris@24
|
70 }
|
Chris@24
|
71 return m_playbackFrame;
|
Chris@24
|
72 }
|
Chris@24
|
73
|
Chris@24
|
74 void
|
Chris@24
|
75 ViewManager::setPlaybackFrame(unsigned long f)
|
Chris@24
|
76 {
|
Chris@24
|
77 if (m_playbackFrame != f) {
|
Chris@24
|
78 m_playbackFrame = f;
|
Chris@24
|
79 if (m_playSource && m_playSource->isPlaying()) {
|
Chris@24
|
80 m_playSource->play(f);
|
Chris@24
|
81 }
|
Chris@24
|
82 emit playbackFrameChanged(f);
|
Chris@24
|
83 }
|
Chris@24
|
84 }
|
Chris@24
|
85
|
Chris@8
|
86 bool
|
Chris@8
|
87 ViewManager::haveInProgressSelection() const
|
Chris@8
|
88 {
|
Chris@8
|
89 return !m_inProgressSelection.isEmpty();
|
Chris@8
|
90 }
|
Chris@8
|
91
|
Chris@8
|
92 const Selection &
|
Chris@8
|
93 ViewManager::getInProgressSelection(bool &exclusive) const
|
Chris@8
|
94 {
|
Chris@8
|
95 exclusive = m_inProgressExclusive;
|
Chris@8
|
96 return m_inProgressSelection;
|
Chris@8
|
97 }
|
Chris@8
|
98
|
Chris@8
|
99 void
|
Chris@8
|
100 ViewManager::setInProgressSelection(const Selection &selection, bool exclusive)
|
Chris@8
|
101 {
|
Chris@8
|
102 m_inProgressExclusive = exclusive;
|
Chris@8
|
103 m_inProgressSelection = selection;
|
Chris@8
|
104 if (exclusive) clearSelections();
|
Chris@9
|
105 emit inProgressSelectionChanged();
|
Chris@8
|
106 }
|
Chris@8
|
107
|
Chris@8
|
108 void
|
Chris@8
|
109 ViewManager::clearInProgressSelection()
|
Chris@8
|
110 {
|
Chris@8
|
111 m_inProgressSelection = Selection();
|
Chris@9
|
112 emit inProgressSelectionChanged();
|
Chris@8
|
113 }
|
Chris@8
|
114
|
Chris@34
|
115 const MultiSelection &
|
Chris@34
|
116 ViewManager::getSelection() const
|
Chris@34
|
117 {
|
Chris@34
|
118 return m_selections;
|
Chris@34
|
119 }
|
Chris@34
|
120
|
Chris@24
|
121 const MultiSelection::SelectionList &
|
Chris@8
|
122 ViewManager::getSelections() const
|
Chris@8
|
123 {
|
Chris@24
|
124 return m_selections.getSelections();
|
Chris@8
|
125 }
|
Chris@8
|
126
|
Chris@8
|
127 void
|
Chris@8
|
128 ViewManager::setSelection(const Selection &selection)
|
Chris@8
|
129 {
|
Chris@45
|
130 MultiSelection ms(m_selections);
|
Chris@45
|
131 ms.setSelection(selection);
|
Chris@45
|
132 setSelections(ms);
|
Chris@8
|
133 }
|
Chris@8
|
134
|
Chris@8
|
135 void
|
Chris@8
|
136 ViewManager::addSelection(const Selection &selection)
|
Chris@8
|
137 {
|
Chris@45
|
138 MultiSelection ms(m_selections);
|
Chris@45
|
139 ms.addSelection(selection);
|
Chris@45
|
140 setSelections(ms);
|
Chris@8
|
141 }
|
Chris@8
|
142
|
Chris@8
|
143 void
|
Chris@8
|
144 ViewManager::removeSelection(const Selection &selection)
|
Chris@8
|
145 {
|
Chris@45
|
146 MultiSelection ms(m_selections);
|
Chris@45
|
147 ms.removeSelection(selection);
|
Chris@45
|
148 setSelections(ms);
|
Chris@8
|
149 }
|
Chris@8
|
150
|
Chris@8
|
151 void
|
Chris@8
|
152 ViewManager::clearSelections()
|
Chris@8
|
153 {
|
Chris@45
|
154 MultiSelection ms(m_selections);
|
Chris@45
|
155 ms.clearSelections();
|
Chris@45
|
156 setSelections(ms);
|
Chris@45
|
157 }
|
Chris@45
|
158
|
Chris@45
|
159 void
|
Chris@45
|
160 ViewManager::setSelections(const MultiSelection &ms)
|
Chris@45
|
161 {
|
Chris@45
|
162 if (m_selections.getSelections() == ms.getSelections()) return;
|
Chris@45
|
163 SetSelectionCommand *command = new SetSelectionCommand(this, ms);
|
Chris@45
|
164 CommandHistory::getInstance()->addCommand(command);
|
Chris@45
|
165 }
|
Chris@45
|
166
|
Chris@45
|
167 void
|
Chris@45
|
168 ViewManager::signalSelectionChange()
|
Chris@45
|
169 {
|
Chris@24
|
170 emit selectionChanged();
|
Chris@8
|
171 }
|
Chris@8
|
172
|
Chris@45
|
173 ViewManager::SetSelectionCommand::SetSelectionCommand(ViewManager *vm,
|
Chris@45
|
174 const MultiSelection &ms) :
|
Chris@45
|
175 m_vm(vm),
|
Chris@45
|
176 m_oldSelection(vm->m_selections),
|
Chris@45
|
177 m_newSelection(ms)
|
Chris@45
|
178 {
|
Chris@45
|
179 }
|
Chris@45
|
180
|
Chris@45
|
181 ViewManager::SetSelectionCommand::~SetSelectionCommand() { }
|
Chris@45
|
182
|
Chris@45
|
183 void
|
Chris@45
|
184 ViewManager::SetSelectionCommand::execute()
|
Chris@45
|
185 {
|
Chris@45
|
186 m_vm->m_selections = m_newSelection;
|
Chris@45
|
187 m_vm->signalSelectionChange();
|
Chris@45
|
188 }
|
Chris@45
|
189
|
Chris@45
|
190 void
|
Chris@45
|
191 ViewManager::SetSelectionCommand::unexecute()
|
Chris@45
|
192 {
|
Chris@45
|
193 m_vm->m_selections = m_oldSelection;
|
Chris@45
|
194 m_vm->signalSelectionChange();
|
Chris@45
|
195 }
|
Chris@45
|
196
|
Chris@45
|
197 QString
|
Chris@45
|
198 ViewManager::SetSelectionCommand::getName() const
|
Chris@45
|
199 {
|
Chris@45
|
200 if (m_newSelection.getSelections().empty()) return tr("Clear Selection");
|
Chris@45
|
201 else return tr("Select");
|
Chris@45
|
202 }
|
Chris@45
|
203
|
Chris@9
|
204 Selection
|
Chris@36
|
205 ViewManager::getContainingSelection(size_t frame, bool defaultToFollowing) const
|
Chris@9
|
206 {
|
Chris@24
|
207 return m_selections.getContainingSelection(frame, defaultToFollowing);
|
Chris@9
|
208 }
|
Chris@9
|
209
|
Chris@8
|
210 void
|
Chris@8
|
211 ViewManager::setToolMode(ToolMode mode)
|
Chris@8
|
212 {
|
Chris@8
|
213 m_toolMode = mode;
|
Chris@8
|
214
|
Chris@8
|
215 emit toolModeChanged();
|
Chris@8
|
216 }
|
Chris@8
|
217
|
Chris@0
|
218 void
|
Chris@9
|
219 ViewManager::setPlayLoopMode(bool mode)
|
Chris@9
|
220 {
|
Chris@9
|
221 m_playLoopMode = mode;
|
Chris@9
|
222
|
Chris@9
|
223 emit playLoopModeChanged();
|
Chris@9
|
224 }
|
Chris@9
|
225
|
Chris@9
|
226 void
|
Chris@9
|
227 ViewManager::setPlaySelectionMode(bool mode)
|
Chris@9
|
228 {
|
Chris@9
|
229 m_playSelectionMode = mode;
|
Chris@9
|
230
|
Chris@9
|
231 emit playSelectionModeChanged();
|
Chris@9
|
232 }
|
Chris@9
|
233
|
Chris@40
|
234 size_t
|
Chris@40
|
235 ViewManager::getPlaybackSampleRate() const
|
Chris@40
|
236 {
|
Chris@40
|
237 if (m_playSource) {
|
Chris@40
|
238 return m_playSource->getTargetSampleRate();
|
Chris@40
|
239 }
|
Chris@40
|
240 return 0;
|
Chris@40
|
241 }
|
Chris@40
|
242
|
Chris@9
|
243 void
|
Chris@0
|
244 ViewManager::setAudioPlaySource(AudioPlaySource *source)
|
Chris@0
|
245 {
|
Chris@0
|
246 if (!m_playSource) {
|
Chris@0
|
247 QTimer::singleShot(100, this, SLOT(checkPlayStatus()));
|
Chris@0
|
248 }
|
Chris@0
|
249 m_playSource = source;
|
Chris@0
|
250 }
|
Chris@0
|
251
|
Chris@0
|
252 void
|
Chris@10
|
253 ViewManager::playStatusChanged(bool playing)
|
Chris@10
|
254 {
|
Chris@10
|
255 checkPlayStatus();
|
Chris@10
|
256 }
|
Chris@10
|
257
|
Chris@10
|
258 void
|
Chris@0
|
259 ViewManager::checkPlayStatus()
|
Chris@0
|
260 {
|
Chris@0
|
261 if (m_playSource && m_playSource->isPlaying()) {
|
Chris@0
|
262
|
Chris@0
|
263 float left = 0, right = 0;
|
Chris@0
|
264 if (m_playSource->getOutputLevels(left, right)) {
|
Chris@0
|
265 if (left != m_lastLeft || right != m_lastRight) {
|
Chris@0
|
266 emit outputLevelsChanged(left, right);
|
Chris@0
|
267 m_lastLeft = left;
|
Chris@0
|
268 m_lastRight = right;
|
Chris@0
|
269 }
|
Chris@0
|
270 }
|
Chris@0
|
271
|
Chris@24
|
272 m_playbackFrame = m_playSource->getCurrentPlayingFrame();
|
Chris@0
|
273
|
Chris@0
|
274 #ifdef DEBUG_VIEW_MANAGER
|
Chris@24
|
275 std::cout << "ViewManager::checkPlayStatus: Playing, frame " << m_playbackFrame << ", levels " << m_lastLeft << "," << m_lastRight << std::endl;
|
Chris@0
|
276 #endif
|
Chris@0
|
277
|
Chris@24
|
278 emit playbackFrameChanged(m_playbackFrame);
|
Chris@0
|
279
|
Chris@0
|
280 QTimer::singleShot(20, this, SLOT(checkPlayStatus()));
|
Chris@0
|
281
|
Chris@0
|
282 } else {
|
Chris@0
|
283
|
Chris@0
|
284 QTimer::singleShot(100, this, SLOT(checkPlayStatus()));
|
Chris@0
|
285
|
Chris@0
|
286 if (m_lastLeft != 0.0 || m_lastRight != 0.0) {
|
Chris@0
|
287 emit outputLevelsChanged(0.0, 0.0);
|
Chris@0
|
288 m_lastLeft = 0.0;
|
Chris@0
|
289 m_lastRight = 0.0;
|
Chris@0
|
290 }
|
Chris@0
|
291
|
Chris@0
|
292 #ifdef DEBUG_VIEW_MANAGER
|
Chris@0
|
293 // std::cout << "ViewManager::checkPlayStatus: Not playing" << std::endl;
|
Chris@0
|
294 #endif
|
Chris@0
|
295 }
|
Chris@0
|
296 }
|
Chris@0
|
297
|
Chris@8
|
298 bool
|
Chris@8
|
299 ViewManager::isPlaying() const
|
Chris@8
|
300 {
|
Chris@8
|
301 return m_playSource && m_playSource->isPlaying();
|
Chris@8
|
302 }
|
Chris@8
|
303
|
Chris@0
|
304 void
|
Chris@0
|
305 ViewManager::considerSeek(void *p, unsigned long f, bool locked)
|
Chris@0
|
306 {
|
Chris@0
|
307 if (locked) {
|
Chris@0
|
308 m_globalCentreFrame = f;
|
Chris@0
|
309 }
|
Chris@0
|
310
|
Chris@0
|
311 #ifdef DEBUG_VIEW_MANAGER
|
Chris@0
|
312 std::cout << "ViewManager::considerSeek(" << p << ", " << f << ", " << locked << ")" << std::endl;
|
Chris@0
|
313 #endif
|
Chris@0
|
314
|
Chris@0
|
315 if (p == this || !locked) return;
|
Chris@0
|
316
|
Chris@0
|
317 if (m_playSource && m_playSource->isPlaying()) {
|
Chris@0
|
318 unsigned long playFrame = m_playSource->getCurrentPlayingFrame();
|
Chris@0
|
319 unsigned long diff = std::max(f, playFrame) - std::min(f, playFrame);
|
Chris@0
|
320 if (diff > 20000) {
|
Chris@24
|
321 m_playbackFrame = f;
|
Chris@0
|
322 m_playSource->play(f);
|
Chris@0
|
323 #ifdef DEBUG_VIEW_MANAGER
|
Chris@0
|
324 std::cout << "ViewManager::considerSeek: reseeking from " << playFrame << " to " << f << std::endl;
|
Chris@0
|
325 #endif
|
Chris@0
|
326 }
|
Chris@24
|
327 } else {
|
Chris@24
|
328 m_playbackFrame = f; //!!! only if that view is in scroll mode?
|
Chris@0
|
329 }
|
Chris@0
|
330 }
|
Chris@0
|
331
|
Chris@0
|
332 void
|
Chris@0
|
333 ViewManager::considerZoomChange(void *p, unsigned long z, bool locked)
|
Chris@0
|
334 {
|
Chris@0
|
335 if (locked) {
|
Chris@0
|
336 m_globalZoom = z;
|
Chris@0
|
337 }
|
Chris@0
|
338
|
Chris@0
|
339 #ifdef DEBUG_VIEW_MANAGER
|
Chris@0
|
340 std::cout << "ViewManager::considerZoomChange(" << p << ", " << z << ", " << locked << ")" << std::endl;
|
Chris@0
|
341 #endif
|
Chris@0
|
342 }
|
Chris@0
|
343
|
Chris@0
|
344 #ifdef INCLUDE_MOCFILES
|
Chris@0
|
345 #include "ViewManager.moc.cpp"
|
Chris@0
|
346 #endif
|
Chris@0
|
347
|