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