annotate base/ViewManager.cpp @ 117:c30728d5625c sv1-v0.9rc1

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