comparison view/ViewManager.cpp @ 127:89c625dda204

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