Chris@127: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@127: Chris@127: /* Chris@127: Sonic Visualiser Chris@127: An audio file viewer and annotation editor. Chris@127: Centre for Digital Music, Queen Mary, University of London. Chris@127: This file copyright 2006 Chris Cannam. Chris@127: Chris@127: This program is free software; you can redistribute it and/or Chris@127: modify it under the terms of the GNU General Public License as Chris@127: published by the Free Software Foundation; either version 2 of the Chris@127: License, or (at your option) any later version. See the file Chris@127: COPYING included with this distribution for more information. Chris@127: */ Chris@127: Chris@127: #include "ViewManager.h" Chris@127: #include "AudioPlaySource.h" Chris@127: #include "Model.h" Chris@127: #include "CommandHistory.h" Chris@127: Chris@127: #include Chris@127: Chris@127: // #define DEBUG_VIEW_MANAGER 1 Chris@127: Chris@127: ViewManager::ViewManager() : Chris@127: m_playSource(0), Chris@127: m_globalCentreFrame(0), Chris@127: m_globalZoom(1024), Chris@127: m_playbackFrame(0), Chris@127: m_mainModelSampleRate(0), Chris@127: m_lastLeft(0), Chris@127: m_lastRight(0), Chris@127: m_inProgressExclusive(true), Chris@127: m_toolMode(NavigateMode), Chris@127: m_playLoopMode(false), Chris@127: m_playSelectionMode(false), Chris@127: m_overlayMode(BasicOverlays) Chris@127: { Chris@127: connect(this, Chris@127: SIGNAL(centreFrameChanged(void *, unsigned long, bool)), Chris@127: SLOT(considerSeek(void *, unsigned long, bool))); Chris@127: Chris@127: connect(this, Chris@127: SIGNAL(zoomLevelChanged(void *, unsigned long, bool)), Chris@127: SLOT(considerZoomChange(void *, unsigned long, bool))); Chris@127: } Chris@127: Chris@127: ViewManager::~ViewManager() Chris@127: { Chris@127: } Chris@127: Chris@127: unsigned long Chris@127: ViewManager::getGlobalCentreFrame() const Chris@127: { Chris@127: #ifdef DEBUG_VIEW_MANAGER Chris@127: std::cout << "ViewManager::getGlobalCentreFrame: returning " << m_globalCentreFrame << std::endl; Chris@127: #endif Chris@127: return m_globalCentreFrame; Chris@127: } Chris@127: Chris@127: unsigned long Chris@127: ViewManager::getGlobalZoom() const Chris@127: { Chris@127: #ifdef DEBUG_VIEW_MANAGER Chris@127: std::cout << "ViewManager::getGlobalZoom: returning " << m_globalZoom << std::endl; Chris@127: #endif Chris@127: return m_globalZoom; Chris@127: } Chris@127: Chris@127: unsigned long Chris@127: ViewManager::getPlaybackFrame() const Chris@127: { Chris@127: if (m_playSource && m_playSource->isPlaying()) { Chris@127: m_playbackFrame = m_playSource->getCurrentPlayingFrame(); Chris@127: } Chris@127: return m_playbackFrame; Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::setPlaybackFrame(unsigned long f) Chris@127: { Chris@127: if (m_playbackFrame != f) { Chris@127: m_playbackFrame = f; Chris@127: emit playbackFrameChanged(f); Chris@127: if (m_playSource && m_playSource->isPlaying()) { Chris@127: m_playSource->play(f); Chris@127: } Chris@127: } Chris@127: } Chris@127: Chris@127: bool Chris@127: ViewManager::haveInProgressSelection() const Chris@127: { Chris@127: return !m_inProgressSelection.isEmpty(); Chris@127: } Chris@127: Chris@127: const Selection & Chris@127: ViewManager::getInProgressSelection(bool &exclusive) const Chris@127: { Chris@127: exclusive = m_inProgressExclusive; Chris@127: return m_inProgressSelection; Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::setInProgressSelection(const Selection &selection, bool exclusive) Chris@127: { Chris@127: m_inProgressExclusive = exclusive; Chris@127: m_inProgressSelection = selection; Chris@127: if (exclusive) clearSelections(); Chris@127: emit inProgressSelectionChanged(); Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::clearInProgressSelection() Chris@127: { Chris@127: m_inProgressSelection = Selection(); Chris@127: emit inProgressSelectionChanged(); Chris@127: } Chris@127: Chris@127: const MultiSelection & Chris@127: ViewManager::getSelection() const Chris@127: { Chris@127: return m_selections; Chris@127: } Chris@127: Chris@127: const MultiSelection::SelectionList & Chris@127: ViewManager::getSelections() const Chris@127: { Chris@127: return m_selections.getSelections(); Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::setSelection(const Selection &selection) Chris@127: { Chris@127: MultiSelection ms(m_selections); Chris@127: ms.setSelection(selection); Chris@127: setSelections(ms); Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::addSelection(const Selection &selection) Chris@127: { Chris@127: MultiSelection ms(m_selections); Chris@127: ms.addSelection(selection); Chris@127: setSelections(ms); Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::removeSelection(const Selection &selection) Chris@127: { Chris@127: MultiSelection ms(m_selections); Chris@127: ms.removeSelection(selection); Chris@127: setSelections(ms); Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::clearSelections() Chris@127: { Chris@127: MultiSelection ms(m_selections); Chris@127: ms.clearSelections(); Chris@127: setSelections(ms); Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::setSelections(const MultiSelection &ms) Chris@127: { Chris@127: if (m_selections.getSelections() == ms.getSelections()) return; Chris@127: SetSelectionCommand *command = new SetSelectionCommand(this, ms); Chris@127: CommandHistory::getInstance()->addCommand(command); Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::signalSelectionChange() Chris@127: { Chris@127: emit selectionChanged(); Chris@127: } Chris@127: Chris@127: ViewManager::SetSelectionCommand::SetSelectionCommand(ViewManager *vm, Chris@127: const MultiSelection &ms) : Chris@127: m_vm(vm), Chris@127: m_oldSelection(vm->m_selections), Chris@127: m_newSelection(ms) Chris@127: { Chris@127: } Chris@127: Chris@127: ViewManager::SetSelectionCommand::~SetSelectionCommand() { } Chris@127: Chris@127: void Chris@127: ViewManager::SetSelectionCommand::execute() Chris@127: { Chris@127: m_vm->m_selections = m_newSelection; Chris@127: m_vm->signalSelectionChange(); Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::SetSelectionCommand::unexecute() Chris@127: { Chris@127: m_vm->m_selections = m_oldSelection; Chris@127: m_vm->signalSelectionChange(); Chris@127: } Chris@127: Chris@127: QString Chris@127: ViewManager::SetSelectionCommand::getName() const Chris@127: { Chris@127: if (m_newSelection.getSelections().empty()) return tr("Clear Selection"); Chris@127: else return tr("Select"); Chris@127: } Chris@127: Chris@127: Selection Chris@127: ViewManager::getContainingSelection(size_t frame, bool defaultToFollowing) const Chris@127: { Chris@127: return m_selections.getContainingSelection(frame, defaultToFollowing); Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::setToolMode(ToolMode mode) Chris@127: { Chris@127: m_toolMode = mode; Chris@127: Chris@127: emit toolModeChanged(); Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::setPlayLoopMode(bool mode) Chris@127: { Chris@127: m_playLoopMode = mode; Chris@127: Chris@127: emit playLoopModeChanged(); Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::setPlaySelectionMode(bool mode) Chris@127: { Chris@127: m_playSelectionMode = mode; Chris@127: Chris@127: emit playSelectionModeChanged(); Chris@127: } Chris@127: Chris@127: size_t Chris@127: ViewManager::getPlaybackSampleRate() const Chris@127: { Chris@127: if (m_playSource) { Chris@127: return m_playSource->getTargetSampleRate(); Chris@127: } Chris@127: return 0; Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::setAudioPlaySource(AudioPlaySource *source) Chris@127: { Chris@127: if (!m_playSource) { Chris@127: QTimer::singleShot(100, this, SLOT(checkPlayStatus())); Chris@127: } Chris@127: m_playSource = source; Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::playStatusChanged(bool playing) Chris@127: { Chris@127: checkPlayStatus(); Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::checkPlayStatus() Chris@127: { Chris@127: if (m_playSource && m_playSource->isPlaying()) { Chris@127: Chris@127: float left = 0, right = 0; Chris@127: if (m_playSource->getOutputLevels(left, right)) { Chris@127: if (left != m_lastLeft || right != m_lastRight) { Chris@127: emit outputLevelsChanged(left, right); Chris@127: m_lastLeft = left; Chris@127: m_lastRight = right; Chris@127: } Chris@127: } Chris@127: Chris@127: m_playbackFrame = m_playSource->getCurrentPlayingFrame(); Chris@127: Chris@127: #ifdef DEBUG_VIEW_MANAGER Chris@127: std::cout << "ViewManager::checkPlayStatus: Playing, frame " << m_playbackFrame << ", levels " << m_lastLeft << "," << m_lastRight << std::endl; Chris@127: #endif Chris@127: Chris@127: emit playbackFrameChanged(m_playbackFrame); Chris@127: Chris@127: QTimer::singleShot(20, this, SLOT(checkPlayStatus())); Chris@127: Chris@127: } else { Chris@127: Chris@127: QTimer::singleShot(100, this, SLOT(checkPlayStatus())); Chris@127: Chris@127: if (m_lastLeft != 0.0 || m_lastRight != 0.0) { Chris@127: emit outputLevelsChanged(0.0, 0.0); Chris@127: m_lastLeft = 0.0; Chris@127: m_lastRight = 0.0; Chris@127: } Chris@127: Chris@127: #ifdef DEBUG_VIEW_MANAGER Chris@127: // std::cout << "ViewManager::checkPlayStatus: Not playing" << std::endl; Chris@127: #endif Chris@127: } Chris@127: } Chris@127: Chris@127: bool Chris@127: ViewManager::isPlaying() const Chris@127: { Chris@127: return m_playSource && m_playSource->isPlaying(); Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::considerSeek(void *p, unsigned long f, bool locked) Chris@127: { Chris@127: if (locked) { Chris@127: m_globalCentreFrame = f; Chris@127: } Chris@127: Chris@127: #ifdef DEBUG_VIEW_MANAGER Chris@127: std::cout << "ViewManager::considerSeek(" << p << ", " << f << ", " << locked << ")" << std::endl; Chris@127: #endif Chris@127: Chris@127: if (p == this || !locked) return; Chris@127: Chris@127: if (m_playSource && m_playSource->isPlaying()) { Chris@127: unsigned long playFrame = m_playSource->getCurrentPlayingFrame(); Chris@127: unsigned long diff = std::max(f, playFrame) - std::min(f, playFrame); Chris@127: if (diff > 20000) { Chris@127: m_playbackFrame = f; Chris@127: m_playSource->play(f); Chris@127: #ifdef DEBUG_VIEW_MANAGER Chris@127: std::cout << "ViewManager::considerSeek: reseeking from " << playFrame << " to " << f << std::endl; Chris@127: #endif Chris@127: } Chris@127: } else { Chris@127: m_playbackFrame = f; //!!! only if that view is in scroll mode? Chris@127: } Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::considerZoomChange(void *p, unsigned long z, bool locked) Chris@127: { Chris@127: if (locked) { Chris@127: m_globalZoom = z; Chris@127: } Chris@127: Chris@127: #ifdef DEBUG_VIEW_MANAGER Chris@127: std::cout << "ViewManager::considerZoomChange(" << p << ", " << z << ", " << locked << ")" << std::endl; Chris@127: #endif Chris@127: } Chris@127: Chris@127: void Chris@127: ViewManager::setOverlayMode(OverlayMode mode) Chris@127: { Chris@127: if (m_overlayMode != mode) { Chris@127: m_overlayMode = mode; Chris@127: emit overlayModeChanged(); Chris@127: } Chris@127: } Chris@127: Chris@127: #ifdef INCLUDE_MOCFILES Chris@127: #include "ViewManager.moc.cpp" Chris@127: #endif Chris@127: