annotate view/ViewManager.cpp @ 214:8520b7918104

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