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