comparison view/ViewManager.cpp @ 0:fc9323a41f5a

start base : Sonic Visualiser sv1-1.0rc1
author lbajardsilogic
date Fri, 11 May 2007 09:08:14 +0000
parents
children be6d31baecb9
comparison
equal deleted inserted replaced
-1:000000000000 0:fc9323a41f5a
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam and QMUL.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "ViewManager.h"
17 #include "base/AudioPlaySource.h"
18 #include "data/model/Model.h"
19 #include "base/CommandHistory.h"
20 #include "View.h"
21 #include "system/System.h"
22
23 #include <QSettings>
24
25 #include <iostream>
26
27 //#define DEBUG_VIEW_MANAGER 1
28
29 ViewManager::ViewManager() :
30 m_playSource(0),
31 m_globalCentreFrame(0),
32 m_globalZoom(1024),
33 m_playbackFrame(0),
34 m_mainModelSampleRate(0),
35 m_lastLeft(0),
36 m_lastRight(0),
37 m_inProgressExclusive(true),
38 m_toolMode(NavigateMode),
39 m_playLoopMode(false),
40 m_playSelectionMode(false),
41 m_overlayMode(StandardOverlays),
42 m_zoomWheelsEnabled(true)
43 {
44 QSettings settings;
45 settings.beginGroup("MainWindow");
46 m_overlayMode = OverlayMode
47 (settings.value("overlay-mode", int(m_overlayMode)).toInt());
48 m_zoomWheelsEnabled =
49 settings.value("zoom-wheels-enabled", m_zoomWheelsEnabled).toBool();
50 settings.endGroup();
51 /*!!!
52 connect(this,
53 SIGNAL(zoomLevelChanged(void *, unsigned long, bool)),
54 SLOT(considerZoomChange(void *, unsigned long, bool)));
55 */
56 }
57
58 ViewManager::~ViewManager()
59 {
60 }
61
62 unsigned long
63 ViewManager::getGlobalCentreFrame() const
64 {
65 #ifdef DEBUG_VIEW_MANAGER
66 std::cout << "ViewManager::getGlobalCentreFrame: returning " << m_globalCentreFrame << std::endl;
67 #endif
68 return m_globalCentreFrame;
69 }
70
71 void
72 ViewManager::setGlobalCentreFrame(unsigned long f)
73 {
74 #ifdef DEBUG_VIEW_MANAGER
75 std::cout << "ViewManager::setGlobalCentreFrame to " << f << std::endl;
76 #endif
77 m_globalCentreFrame = f;
78 emit globalCentreFrameChanged(f);
79 }
80
81 unsigned long
82 ViewManager::getGlobalZoom() const
83 {
84 #ifdef DEBUG_VIEW_MANAGER
85 std::cout << "ViewManager::getGlobalZoom: returning " << m_globalZoom << std::endl;
86 #endif
87 return m_globalZoom;
88 }
89
90 unsigned long
91 ViewManager::getPlaybackFrame() const
92 {
93 if (m_playSource && m_playSource->isPlaying()) {
94 m_playbackFrame = m_playSource->getCurrentPlayingFrame();
95 }
96 return m_playbackFrame;
97 }
98
99 void
100 ViewManager::setPlaybackFrame(unsigned long f)
101 {
102 if (m_playbackFrame != f) {
103 m_playbackFrame = f;
104 emit playbackFrameChanged(f);
105 if (m_playSource && m_playSource->isPlaying()) {
106 m_playSource->play(f);
107 }
108 }
109 }
110
111 bool
112 ViewManager::haveInProgressSelection() const
113 {
114 return !m_inProgressSelection.isEmpty();
115 }
116
117 const Selection &
118 ViewManager::getInProgressSelection(bool &exclusive) const
119 {
120 exclusive = m_inProgressExclusive;
121 return m_inProgressSelection;
122 }
123
124 void
125 ViewManager::setInProgressSelection(const Selection &selection, bool exclusive)
126 {
127 m_inProgressExclusive = exclusive;
128 m_inProgressSelection = selection;
129 if (exclusive) clearSelections();
130 emit inProgressSelectionChanged();
131 }
132
133 void
134 ViewManager::clearInProgressSelection()
135 {
136 m_inProgressSelection = Selection();
137 emit inProgressSelectionChanged();
138 }
139
140 const MultiSelection &
141 ViewManager::getSelection() const
142 {
143 return m_selections;
144 }
145
146 const MultiSelection::SelectionList &
147 ViewManager::getSelections() const
148 {
149 return m_selections.getSelections();
150 }
151
152 void
153 ViewManager::setSelection(const Selection &selection)
154 {
155 MultiSelection ms(m_selections);
156 ms.setSelection(selection);
157 setSelections(ms);
158 }
159
160 void
161 ViewManager::addSelection(const Selection &selection)
162 {
163 MultiSelection ms(m_selections);
164 ms.addSelection(selection);
165 setSelections(ms);
166 }
167
168 void
169 ViewManager::removeSelection(const Selection &selection)
170 {
171 MultiSelection ms(m_selections);
172 ms.removeSelection(selection);
173 setSelections(ms);
174 }
175
176 void
177 ViewManager::clearSelections()
178 {
179 MultiSelection ms(m_selections);
180 ms.clearSelections();
181 setSelections(ms);
182 }
183
184 void
185 ViewManager::setSelections(const MultiSelection &ms)
186 {
187 if (m_selections.getSelections() == ms.getSelections()) return;
188 SetSelectionCommand *command = new SetSelectionCommand(this, ms);
189 CommandHistory::getInstance()->addCommand(command);
190 }
191
192 void
193 ViewManager::signalSelectionChange()
194 {
195 emit selectionChanged();
196 }
197
198 ViewManager::SetSelectionCommand::SetSelectionCommand(ViewManager *vm,
199 const MultiSelection &ms) :
200 m_vm(vm),
201 m_oldSelection(vm->m_selections),
202 m_newSelection(ms)
203 {
204 }
205
206 ViewManager::SetSelectionCommand::~SetSelectionCommand() { }
207
208 void
209 ViewManager::SetSelectionCommand::execute()
210 {
211 m_vm->m_selections = m_newSelection;
212 m_vm->signalSelectionChange();
213 }
214
215 void
216 ViewManager::SetSelectionCommand::unexecute()
217 {
218 m_vm->m_selections = m_oldSelection;
219 m_vm->signalSelectionChange();
220 }
221
222 QString
223 ViewManager::SetSelectionCommand::getName() const
224 {
225 if (m_newSelection.getSelections().empty()) return tr("Clear Selection");
226 else return tr("Select");
227 }
228
229 Selection
230 ViewManager::getContainingSelection(size_t frame, bool defaultToFollowing) const
231 {
232 return m_selections.getContainingSelection(frame, defaultToFollowing);
233 }
234
235 void
236 ViewManager::setToolMode(ToolMode mode)
237 {
238 m_toolMode = mode;
239
240 emit toolModeChanged();
241 }
242
243 void
244 ViewManager::setPlayLoopMode(bool mode)
245 {
246 if (m_playLoopMode != mode) {
247
248 m_playLoopMode = mode;
249
250 emit playLoopModeChanged();
251 emit playLoopModeChanged(mode);
252 }
253 }
254
255 void
256 ViewManager::setPlaySelectionMode(bool mode)
257 {
258 if (m_playSelectionMode != mode) {
259
260 m_playSelectionMode = mode;
261
262 emit playSelectionModeChanged();
263 emit playSelectionModeChanged(mode);
264 }
265 }
266
267 size_t
268 ViewManager::getPlaybackSampleRate() const
269 {
270 if (m_playSource) {
271 return m_playSource->getSourceSampleRate();
272 }
273 return 0;
274 }
275
276 size_t
277 ViewManager::getOutputSampleRate() const
278 {
279 if (m_playSource) {
280 return m_playSource->getTargetSampleRate();
281 }
282 return 0;
283 }
284
285 void
286 ViewManager::setAudioPlaySource(AudioPlaySource *source)
287 {
288 if (!m_playSource) {
289 QTimer::singleShot(100, this, SLOT(checkPlayStatus()));
290 }
291 m_playSource = source;
292 }
293
294 void
295 ViewManager::playStatusChanged(bool /* playing */)
296 {
297 checkPlayStatus();
298 }
299
300 void
301 ViewManager::checkPlayStatus()
302 {
303 if (m_playSource && m_playSource->isPlaying()) {
304
305 float left = 0, right = 0;
306 if (m_playSource->getOutputLevels(left, right)) {
307 if (left != m_lastLeft || right != m_lastRight) {
308 emit outputLevelsChanged(left, right);
309 m_lastLeft = left;
310 m_lastRight = right;
311 }
312 }
313
314 m_playbackFrame = m_playSource->getCurrentPlayingFrame();
315
316 #ifdef DEBUG_VIEW_MANAGER
317 std::cout << "ViewManager::checkPlayStatus: Playing, frame " << m_playbackFrame << ", levels " << m_lastLeft << "," << m_lastRight << std::endl;
318 #endif
319
320 emit playbackFrameChanged(m_playbackFrame);
321
322 QTimer::singleShot(20, this, SLOT(checkPlayStatus()));
323
324 } else {
325
326 QTimer::singleShot(100, this, SLOT(checkPlayStatus()));
327
328 if (m_lastLeft != 0.0 || m_lastRight != 0.0) {
329 emit outputLevelsChanged(0.0, 0.0);
330 m_lastLeft = 0.0;
331 m_lastRight = 0.0;
332 }
333
334 #ifdef DEBUG_VIEW_MANAGER
335 // std::cout << "ViewManager::checkPlayStatus: Not playing" << std::endl;
336 #endif
337 }
338 }
339
340 bool
341 ViewManager::isPlaying() const
342 {
343 return m_playSource && m_playSource->isPlaying();
344 }
345
346 void
347 ViewManager::viewCentreFrameChanged(unsigned long f, bool locked,
348 PlaybackFollowMode mode)
349 {
350 View *v = dynamic_cast<View *>(sender());
351
352 if (locked) {
353 m_globalCentreFrame = f;
354 emit globalCentreFrameChanged(f);
355 } else {
356 if (v) emit viewCentreFrameChanged(v, f);
357 }
358
359 if (mode == PlaybackIgnore) {
360 return;
361 }
362
363 seek(f);
364 }
365
366 void
367 ViewManager::seek(unsigned long f)
368 {
369 #ifdef DEBUG_VIEW_MANAGER
370 std::cout << "ViewManager::seek(" << f << ")" << std::endl;
371 #endif
372
373 if (m_playSource && m_playSource->isPlaying()) {
374 unsigned long playFrame = m_playSource->getCurrentPlayingFrame();
375 unsigned long diff = max(f, playFrame) - min(f, playFrame);
376 if (diff > 20000) {
377 m_playbackFrame = f;
378 m_playSource->play(f);
379 #ifdef DEBUG_VIEW_MANAGER
380 std::cout << "ViewManager::considerSeek: reseeking from " << playFrame << " to " << f << std::endl;
381 #endif
382 emit playbackFrameChanged(f);
383 }
384 } else {
385 if (m_playbackFrame != f) {
386 m_playbackFrame = f;
387 emit playbackFrameChanged(f);
388 }
389 }
390 }
391
392 void
393 ViewManager::viewZoomLevelChanged(unsigned long z, bool locked)
394 {
395 View *v = dynamic_cast<View *>(sender());
396
397 if (!v) {
398 std::cerr << "ViewManager::viewZoomLevelChanged: WARNING: sender is not a view" << std::endl;
399 return;
400 }
401
402 //!!! emit zoomLevelChanged();
403
404 if (locked) {
405 m_globalZoom = z;
406 }
407
408 #ifdef DEBUG_VIEW_MANAGER
409 std::cout << "ViewManager::viewZoomLevelChanged(" << v << ", " << z << ", " << locked << ")" << std::endl;
410 #endif
411
412 emit viewZoomLevelChanged(v, z, locked);
413 }
414
415 void
416 ViewManager::setOverlayMode(OverlayMode mode)
417 {
418 if (m_overlayMode != mode) {
419 m_overlayMode = mode;
420 emit overlayModeChanged();
421 }
422
423 QSettings settings;
424 settings.beginGroup("MainWindow");
425 settings.setValue("overlay-mode", int(m_overlayMode));
426 settings.endGroup();
427 }
428
429 void
430 ViewManager::setZoomWheelsEnabled(bool enabled)
431 {
432 if (m_zoomWheelsEnabled != enabled) {
433 m_zoomWheelsEnabled = enabled;
434 emit zoomWheelsEnabledChanged();
435 }
436
437 QSettings settings;
438 settings.beginGroup("MainWindow");
439 settings.setValue("zoom-wheels-enabled", m_zoomWheelsEnabled);
440 settings.endGroup();
441 }
442