annotate view/ViewManager.cpp @ 286:7554ae119882

* Fix failure to save vertical scale setting from time-value layer
author Chris Cannam
date Wed, 11 Jul 2007 20:46:37 +0000
parents 1a49bd0d8375
children 24fc90078754
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@222 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@222 54 */
Chris@127 55 }
Chris@127 56
Chris@127 57 ViewManager::~ViewManager()
Chris@127 58 {
Chris@127 59 }
Chris@127 60
Chris@127 61 unsigned long
Chris@127 62 ViewManager::getGlobalCentreFrame() const
Chris@127 63 {
Chris@127 64 #ifdef DEBUG_VIEW_MANAGER
Chris@127 65 std::cout << "ViewManager::getGlobalCentreFrame: returning " << m_globalCentreFrame << std::endl;
Chris@127 66 #endif
Chris@127 67 return m_globalCentreFrame;
Chris@127 68 }
Chris@127 69
Chris@180 70 void
Chris@180 71 ViewManager::setGlobalCentreFrame(unsigned long f)
Chris@180 72 {
Chris@180 73 #ifdef DEBUG_VIEW_MANAGER
Chris@180 74 std::cout << "ViewManager::setGlobalCentreFrame to " << f << std::endl;
Chris@180 75 #endif
Chris@180 76 m_globalCentreFrame = f;
Chris@211 77 emit globalCentreFrameChanged(f);
Chris@180 78 }
Chris@180 79
Chris@127 80 unsigned long
Chris@127 81 ViewManager::getGlobalZoom() const
Chris@127 82 {
Chris@127 83 #ifdef DEBUG_VIEW_MANAGER
Chris@127 84 std::cout << "ViewManager::getGlobalZoom: returning " << m_globalZoom << std::endl;
Chris@127 85 #endif
Chris@127 86 return m_globalZoom;
Chris@127 87 }
Chris@127 88
Chris@127 89 unsigned long
Chris@127 90 ViewManager::getPlaybackFrame() const
Chris@127 91 {
Chris@127 92 if (m_playSource && m_playSource->isPlaying()) {
Chris@127 93 m_playbackFrame = m_playSource->getCurrentPlayingFrame();
Chris@127 94 }
Chris@127 95 return m_playbackFrame;
Chris@127 96 }
Chris@127 97
Chris@127 98 void
Chris@127 99 ViewManager::setPlaybackFrame(unsigned long f)
Chris@127 100 {
Chris@127 101 if (m_playbackFrame != f) {
Chris@127 102 m_playbackFrame = f;
Chris@127 103 emit playbackFrameChanged(f);
Chris@127 104 if (m_playSource && m_playSource->isPlaying()) {
Chris@127 105 m_playSource->play(f);
Chris@127 106 }
Chris@127 107 }
Chris@127 108 }
Chris@127 109
Chris@127 110 bool
Chris@127 111 ViewManager::haveInProgressSelection() const
Chris@127 112 {
Chris@127 113 return !m_inProgressSelection.isEmpty();
Chris@127 114 }
Chris@127 115
Chris@127 116 const Selection &
Chris@127 117 ViewManager::getInProgressSelection(bool &exclusive) const
Chris@127 118 {
Chris@127 119 exclusive = m_inProgressExclusive;
Chris@127 120 return m_inProgressSelection;
Chris@127 121 }
Chris@127 122
Chris@127 123 void
Chris@127 124 ViewManager::setInProgressSelection(const Selection &selection, bool exclusive)
Chris@127 125 {
Chris@127 126 m_inProgressExclusive = exclusive;
Chris@127 127 m_inProgressSelection = selection;
Chris@127 128 if (exclusive) clearSelections();
Chris@127 129 emit inProgressSelectionChanged();
Chris@127 130 }
Chris@127 131
Chris@127 132 void
Chris@127 133 ViewManager::clearInProgressSelection()
Chris@127 134 {
Chris@127 135 m_inProgressSelection = Selection();
Chris@127 136 emit inProgressSelectionChanged();
Chris@127 137 }
Chris@127 138
Chris@127 139 const MultiSelection &
Chris@127 140 ViewManager::getSelection() const
Chris@127 141 {
Chris@127 142 return m_selections;
Chris@127 143 }
Chris@127 144
Chris@127 145 const MultiSelection::SelectionList &
Chris@127 146 ViewManager::getSelections() const
Chris@127 147 {
Chris@127 148 return m_selections.getSelections();
Chris@127 149 }
Chris@127 150
Chris@127 151 void
Chris@127 152 ViewManager::setSelection(const Selection &selection)
Chris@127 153 {
Chris@127 154 MultiSelection ms(m_selections);
Chris@127 155 ms.setSelection(selection);
Chris@127 156 setSelections(ms);
Chris@127 157 }
Chris@127 158
Chris@127 159 void
Chris@127 160 ViewManager::addSelection(const Selection &selection)
Chris@127 161 {
Chris@127 162 MultiSelection ms(m_selections);
Chris@127 163 ms.addSelection(selection);
Chris@127 164 setSelections(ms);
Chris@127 165 }
Chris@127 166
Chris@127 167 void
Chris@127 168 ViewManager::removeSelection(const Selection &selection)
Chris@127 169 {
Chris@127 170 MultiSelection ms(m_selections);
Chris@127 171 ms.removeSelection(selection);
Chris@127 172 setSelections(ms);
Chris@127 173 }
Chris@127 174
Chris@127 175 void
Chris@127 176 ViewManager::clearSelections()
Chris@127 177 {
Chris@127 178 MultiSelection ms(m_selections);
Chris@127 179 ms.clearSelections();
Chris@127 180 setSelections(ms);
Chris@127 181 }
Chris@127 182
Chris@127 183 void
Chris@127 184 ViewManager::setSelections(const MultiSelection &ms)
Chris@127 185 {
Chris@127 186 if (m_selections.getSelections() == ms.getSelections()) return;
Chris@127 187 SetSelectionCommand *command = new SetSelectionCommand(this, ms);
Chris@127 188 CommandHistory::getInstance()->addCommand(command);
Chris@127 189 }
Chris@127 190
Chris@271 191 size_t
Chris@271 192 ViewManager::constrainFrameToSelection(size_t frame) const
Chris@271 193 {
Chris@271 194 MultiSelection::SelectionList sl = getSelections();
Chris@271 195 if (sl.empty()) return frame;
Chris@271 196
Chris@271 197 size_t selectionStartFrame = sl.begin()->getStartFrame();
Chris@271 198 if (frame < selectionStartFrame) {
Chris@271 199 frame = selectionStartFrame;
Chris@271 200 return frame;
Chris@271 201 }
Chris@271 202
Chris@271 203 MultiSelection::SelectionList::iterator i = sl.end();
Chris@271 204 --i;
Chris@271 205 size_t selectionEndFrame = i->getEndFrame();
Chris@271 206 if (frame > selectionEndFrame) frame = selectionEndFrame;
Chris@271 207
Chris@271 208 return frame;
Chris@271 209 }
Chris@271 210
Chris@127 211 void
Chris@127 212 ViewManager::signalSelectionChange()
Chris@127 213 {
Chris@127 214 emit selectionChanged();
Chris@127 215 }
Chris@127 216
Chris@127 217 ViewManager::SetSelectionCommand::SetSelectionCommand(ViewManager *vm,
Chris@127 218 const MultiSelection &ms) :
Chris@127 219 m_vm(vm),
Chris@127 220 m_oldSelection(vm->m_selections),
Chris@127 221 m_newSelection(ms)
Chris@127 222 {
Chris@127 223 }
Chris@127 224
Chris@127 225 ViewManager::SetSelectionCommand::~SetSelectionCommand() { }
Chris@127 226
Chris@127 227 void
Chris@127 228 ViewManager::SetSelectionCommand::execute()
Chris@127 229 {
Chris@127 230 m_vm->m_selections = m_newSelection;
Chris@127 231 m_vm->signalSelectionChange();
Chris@127 232 }
Chris@127 233
Chris@127 234 void
Chris@127 235 ViewManager::SetSelectionCommand::unexecute()
Chris@127 236 {
Chris@127 237 m_vm->m_selections = m_oldSelection;
Chris@127 238 m_vm->signalSelectionChange();
Chris@127 239 }
Chris@127 240
Chris@127 241 QString
Chris@127 242 ViewManager::SetSelectionCommand::getName() const
Chris@127 243 {
Chris@127 244 if (m_newSelection.getSelections().empty()) return tr("Clear Selection");
Chris@127 245 else return tr("Select");
Chris@127 246 }
Chris@127 247
Chris@127 248 Selection
Chris@127 249 ViewManager::getContainingSelection(size_t frame, bool defaultToFollowing) const
Chris@127 250 {
Chris@127 251 return m_selections.getContainingSelection(frame, defaultToFollowing);
Chris@127 252 }
Chris@127 253
Chris@127 254 void
Chris@127 255 ViewManager::setToolMode(ToolMode mode)
Chris@127 256 {
Chris@127 257 m_toolMode = mode;
Chris@127 258
Chris@127 259 emit toolModeChanged();
Chris@127 260 }
Chris@127 261
Chris@127 262 void
Chris@127 263 ViewManager::setPlayLoopMode(bool mode)
Chris@127 264 {
Chris@177 265 if (m_playLoopMode != mode) {
Chris@127 266
Chris@177 267 m_playLoopMode = mode;
Chris@177 268
Chris@177 269 emit playLoopModeChanged();
Chris@177 270 emit playLoopModeChanged(mode);
Chris@177 271 }
Chris@127 272 }
Chris@127 273
Chris@127 274 void
Chris@127 275 ViewManager::setPlaySelectionMode(bool mode)
Chris@127 276 {
Chris@177 277 if (m_playSelectionMode != mode) {
Chris@127 278
Chris@177 279 m_playSelectionMode = mode;
Chris@177 280
Chris@177 281 emit playSelectionModeChanged();
Chris@177 282 emit playSelectionModeChanged(mode);
Chris@177 283 }
Chris@127 284 }
Chris@127 285
Chris@224 286 size_t
Chris@224 287 ViewManager::getPlaybackSampleRate() const
Chris@224 288 {
Chris@224 289 if (m_playSource) {
Chris@224 290 return m_playSource->getSourceSampleRate();
Chris@224 291 }
Chris@224 292 return 0;
Chris@224 293 }
Chris@224 294
Chris@127 295 size_t
Chris@224 296 ViewManager::getOutputSampleRate() const
Chris@127 297 {
Chris@127 298 if (m_playSource) {
Chris@127 299 return m_playSource->getTargetSampleRate();
Chris@127 300 }
Chris@127 301 return 0;
Chris@127 302 }
Chris@127 303
Chris@127 304 void
Chris@127 305 ViewManager::setAudioPlaySource(AudioPlaySource *source)
Chris@127 306 {
Chris@127 307 if (!m_playSource) {
Chris@127 308 QTimer::singleShot(100, this, SLOT(checkPlayStatus()));
Chris@127 309 }
Chris@127 310 m_playSource = source;
Chris@127 311 }
Chris@127 312
Chris@127 313 void
Chris@248 314 ViewManager::playStatusChanged(bool /* playing */)
Chris@127 315 {
Chris@127 316 checkPlayStatus();
Chris@127 317 }
Chris@127 318
Chris@127 319 void
Chris@127 320 ViewManager::checkPlayStatus()
Chris@127 321 {
Chris@127 322 if (m_playSource && m_playSource->isPlaying()) {
Chris@127 323
Chris@127 324 float left = 0, right = 0;
Chris@127 325 if (m_playSource->getOutputLevels(left, right)) {
Chris@127 326 if (left != m_lastLeft || right != m_lastRight) {
Chris@127 327 emit outputLevelsChanged(left, right);
Chris@127 328 m_lastLeft = left;
Chris@127 329 m_lastRight = right;
Chris@127 330 }
Chris@127 331 }
Chris@127 332
Chris@127 333 m_playbackFrame = m_playSource->getCurrentPlayingFrame();
Chris@127 334
Chris@127 335 #ifdef DEBUG_VIEW_MANAGER
Chris@127 336 std::cout << "ViewManager::checkPlayStatus: Playing, frame " << m_playbackFrame << ", levels " << m_lastLeft << "," << m_lastRight << std::endl;
Chris@127 337 #endif
Chris@127 338
Chris@127 339 emit playbackFrameChanged(m_playbackFrame);
Chris@127 340
Chris@127 341 QTimer::singleShot(20, this, SLOT(checkPlayStatus()));
Chris@127 342
Chris@127 343 } else {
Chris@127 344
Chris@127 345 QTimer::singleShot(100, this, SLOT(checkPlayStatus()));
Chris@127 346
Chris@127 347 if (m_lastLeft != 0.0 || m_lastRight != 0.0) {
Chris@127 348 emit outputLevelsChanged(0.0, 0.0);
Chris@127 349 m_lastLeft = 0.0;
Chris@127 350 m_lastRight = 0.0;
Chris@127 351 }
Chris@127 352
Chris@127 353 #ifdef DEBUG_VIEW_MANAGER
Chris@127 354 // std::cout << "ViewManager::checkPlayStatus: Not playing" << std::endl;
Chris@127 355 #endif
Chris@127 356 }
Chris@127 357 }
Chris@127 358
Chris@127 359 bool
Chris@127 360 ViewManager::isPlaying() const
Chris@127 361 {
Chris@127 362 return m_playSource && m_playSource->isPlaying();
Chris@127 363 }
Chris@127 364
Chris@127 365 void
Chris@211 366 ViewManager::viewCentreFrameChanged(unsigned long f, bool locked,
Chris@211 367 PlaybackFollowMode mode)
Chris@127 368 {
Chris@211 369 View *v = dynamic_cast<View *>(sender());
Chris@211 370
Chris@127 371 if (locked) {
Chris@211 372 m_globalCentreFrame = f;
Chris@211 373 emit globalCentreFrameChanged(f);
Chris@211 374 } else {
Chris@211 375 if (v) emit viewCentreFrameChanged(v, f);
Chris@127 376 }
Chris@127 377
Chris@211 378 if (mode == PlaybackIgnore) {
Chris@211 379 return;
Chris@211 380 }
Chris@211 381
Chris@211 382 seek(f);
Chris@211 383 }
Chris@211 384
Chris@211 385 void
Chris@211 386 ViewManager::seek(unsigned long f)
Chris@211 387 {
Chris@127 388 #ifdef DEBUG_VIEW_MANAGER
Chris@211 389 std::cout << "ViewManager::seek(" << f << ")" << std::endl;
Chris@127 390 #endif
Chris@127 391
Chris@127 392 if (m_playSource && m_playSource->isPlaying()) {
Chris@127 393 unsigned long playFrame = m_playSource->getCurrentPlayingFrame();
Chris@127 394 unsigned long diff = std::max(f, playFrame) - std::min(f, playFrame);
Chris@127 395 if (diff > 20000) {
Chris@127 396 m_playbackFrame = f;
Chris@127 397 m_playSource->play(f);
Chris@127 398 #ifdef DEBUG_VIEW_MANAGER
Chris@127 399 std::cout << "ViewManager::considerSeek: reseeking from " << playFrame << " to " << f << std::endl;
Chris@127 400 #endif
Chris@211 401 emit playbackFrameChanged(f);
Chris@127 402 }
Chris@127 403 } else {
Chris@211 404 if (m_playbackFrame != f) {
Chris@211 405 m_playbackFrame = f;
Chris@211 406 emit playbackFrameChanged(f);
Chris@211 407 }
Chris@127 408 }
Chris@127 409 }
Chris@127 410
Chris@127 411 void
Chris@222 412 ViewManager::viewZoomLevelChanged(unsigned long z, bool locked)
Chris@127 413 {
Chris@222 414 View *v = dynamic_cast<View *>(sender());
Chris@222 415
Chris@222 416 if (!v) {
Chris@222 417 std::cerr << "ViewManager::viewZoomLevelChanged: WARNING: sender is not a view" << std::endl;
Chris@222 418 return;
Chris@222 419 }
Chris@222 420
Chris@222 421 //!!! emit zoomLevelChanged();
Chris@133 422
Chris@127 423 if (locked) {
Chris@127 424 m_globalZoom = z;
Chris@127 425 }
Chris@127 426
Chris@127 427 #ifdef DEBUG_VIEW_MANAGER
Chris@222 428 std::cout << "ViewManager::viewZoomLevelChanged(" << v << ", " << z << ", " << locked << ")" << std::endl;
Chris@127 429 #endif
Chris@222 430
Chris@222 431 emit viewZoomLevelChanged(v, z, locked);
Chris@127 432 }
Chris@127 433
Chris@127 434 void
Chris@127 435 ViewManager::setOverlayMode(OverlayMode mode)
Chris@127 436 {
Chris@127 437 if (m_overlayMode != mode) {
Chris@127 438 m_overlayMode = mode;
Chris@127 439 emit overlayModeChanged();
Chris@127 440 }
Chris@133 441
Chris@133 442 QSettings settings;
Chris@133 443 settings.beginGroup("MainWindow");
Chris@133 444 settings.setValue("overlay-mode", int(m_overlayMode));
Chris@133 445 settings.endGroup();
Chris@127 446 }
Chris@127 447
Chris@133 448 void
Chris@133 449 ViewManager::setZoomWheelsEnabled(bool enabled)
Chris@133 450 {
Chris@133 451 if (m_zoomWheelsEnabled != enabled) {
Chris@133 452 m_zoomWheelsEnabled = enabled;
Chris@133 453 emit zoomWheelsEnabledChanged();
Chris@133 454 }
Chris@127 455
Chris@133 456 QSettings settings;
Chris@133 457 settings.beginGroup("MainWindow");
Chris@133 458 settings.setValue("zoom-wheels-enabled", m_zoomWheelsEnabled);
Chris@133 459 settings.endGroup();
Chris@133 460 }
Chris@133 461