annotate view/ViewManager.cpp @ 292:24fc90078754

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