Chris@49: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@28: 
Chris@28: /*
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@28:     
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@28: */
Chris@28: 
Chris@28: #include "PlayParameterRepository.h"
Chris@28: #include "PlayParameters.h"
Chris@391: #include "Playable.h"
Chris@30: 
Chris@29: #include <iostream>
Chris@29: 
Chris@28: PlayParameterRepository *
Chris@28: PlayParameterRepository::m_instance = new PlayParameterRepository;
Chris@28: 
Chris@28: PlayParameterRepository *
Chris@145: PlayParameterRepository::getInstance()
Chris@28: {
Chris@28:     return m_instance;
Chris@28: }
Chris@28: 
Chris@28: PlayParameterRepository::~PlayParameterRepository()
Chris@28: {
Chris@28: }
Chris@28: 
Chris@30: void
Chris@391: PlayParameterRepository::addPlayable(const Playable *playable)
Chris@30: {
Chris@391: //    std::cerr << "PlayParameterRepository:addPlayable " << playable <<  std::endl;
Chris@82: 
Chris@391:     if (!getPlayParameters(playable)) {
Chris@30: 
Chris@391: 	// Give all playables the same type of play parameters for the
Chris@391: 	// moment
Chris@30: 
Chris@391: //	    std::cerr << "PlayParameterRepository: Adding play parameters for " << playable << std::endl;
Chris@30: 
Chris@391:         PlayParameters *params = new PlayParameters;
Chris@391:         m_playParameters[playable] = params;
Chris@30: 
Chris@391:         params->setPlayPluginId
Chris@391:             (playable->getDefaultPlayPluginId());
Chris@391:         
Chris@391:         params->setPlayPluginConfiguration
Chris@391:             (playable->getDefaultPlayPluginConfiguration());
Chris@391:         
Chris@391:         connect(params, SIGNAL(playParametersChanged()),
Chris@391:                 this, SLOT(playParametersChanged()));
Chris@391:         
Chris@391:         connect(params, SIGNAL(playPluginIdChanged(QString)),
Chris@391:                 this, SLOT(playPluginIdChanged(QString)));
Chris@30: 
Chris@391:         connect(params, SIGNAL(playPluginConfigurationChanged(QString)),
Chris@391:                 this, SLOT(playPluginConfigurationChanged(QString)));
Chris@391:         
Chris@391: //            std::cerr << "Connected play parameters " << params << " for playable "
Chris@391: //                      << playable << " to this " << this << std::endl;
Chris@57: 
Chris@30:     }
Chris@30: }    
Chris@30: 
Chris@30: void
Chris@391: PlayParameterRepository::removePlayable(const Playable *playable)
Chris@30: {
Chris@391:     if (m_playParameters.find(playable) == m_playParameters.end()) {
Chris@391:         std::cerr << "WARNING: PlayParameterRepository::removePlayable: unknown playable " << playable << std::endl;
Chris@391:         return;
Chris@391:     }
Chris@391:     delete m_playParameters[playable];
Chris@391:     m_playParameters.erase(playable);
Chris@30: }
Chris@30: 
Chris@284: void
Chris@391: PlayParameterRepository::copyParameters(const Playable *from, const Playable *to)
Chris@284: {
Chris@284:     if (!getPlayParameters(from)) {
Chris@391:         std::cerr << "ERROR: PlayParameterRepository::copyParameters: source playable unknown" << std::endl;
Chris@284:         return;
Chris@284:     }
Chris@284:     if (!getPlayParameters(to)) {
Chris@391:         std::cerr << "WARNING: PlayParameterRepository::copyParameters: target playable unknown, adding it now" << std::endl;
Chris@391:         addPlayable(to);
Chris@284:     }
Chris@284:     getPlayParameters(to)->copyFrom(getPlayParameters(from));
Chris@284: }
Chris@284: 
Chris@28: PlayParameters *
Chris@391: PlayParameterRepository::getPlayParameters(const Playable *playable) 
Chris@28: {
Chris@391:     if (m_playParameters.find(playable) == m_playParameters.end()) return 0;
Chris@391:     return m_playParameters.find(playable)->second;
Chris@28: }
Chris@28: 
Chris@28: void
Chris@29: PlayParameterRepository::playParametersChanged()
Chris@29: {
Chris@57:     PlayParameters *params = dynamic_cast<PlayParameters *>(sender());
Chris@57:     emit playParametersChanged(params);
Chris@57: }
Chris@57: 
Chris@57: void
Chris@57: PlayParameterRepository::playPluginIdChanged(QString id)
Chris@57: {
Chris@57:     PlayParameters *params = dynamic_cast<PlayParameters *>(sender());
Chris@391:     for (PlayableParameterMap::iterator i = m_playParameters.begin();
Chris@57:          i != m_playParameters.end(); ++i) {
Chris@57:         if (i->second == params) {
Chris@57:             emit playPluginIdChanged(i->first, id);
Chris@57:             return;
Chris@57:         }
Chris@57:     }
Chris@57: }
Chris@57: 
Chris@57: void
Chris@57: PlayParameterRepository::playPluginConfigurationChanged(QString config)
Chris@57: {
Chris@57:     PlayParameters *params = dynamic_cast<PlayParameters *>(sender());
Chris@117: //    std::cerr << "PlayParameterRepository::playPluginConfigurationChanged" << std::endl;
Chris@391:     for (PlayableParameterMap::iterator i = m_playParameters.begin();
Chris@57:          i != m_playParameters.end(); ++i) {
Chris@57:         if (i->second == params) {
Chris@57:             emit playPluginConfigurationChanged(i->first, config);
Chris@57:             return;
Chris@57:         }
Chris@57:     }
Chris@29: }
Chris@29: 
Chris@29: void
Chris@28: PlayParameterRepository::clear()
Chris@28: {
Chris@117: //    std::cerr << "PlayParameterRepository: PlayParameterRepository::clear" << std::endl;
Chris@28:     while (!m_playParameters.empty()) {
Chris@28: 	delete m_playParameters.begin()->second;
Chris@28: 	m_playParameters.erase(m_playParameters.begin());
Chris@28:     }
Chris@28: }
Chris@28: 
Chris@391: PlayParameterRepository::EditCommand::EditCommand(PlayParameters *params) :
Chris@391:     m_params(params)
Chris@391: {
Chris@391:     m_from.copyFrom(m_params);
Chris@391:     m_to.copyFrom(m_params);
Chris@391: }
Chris@391: 
Chris@391: void
Chris@391: PlayParameterRepository::EditCommand::setPlayMuted(bool muted)
Chris@391: {
Chris@391:     m_to.setPlayMuted(muted);
Chris@391: }
Chris@391: 
Chris@391: void
Chris@391: PlayParameterRepository::EditCommand::setPlayAudible(bool audible)
Chris@391: {
Chris@391:     m_to.setPlayAudible(audible);
Chris@391: }
Chris@391: 
Chris@391: void
Chris@391: PlayParameterRepository::EditCommand::setPlayPan(float pan)
Chris@391: {
Chris@391:     m_to.setPlayPan(pan);
Chris@391: }
Chris@391: 
Chris@391: void
Chris@391: PlayParameterRepository::EditCommand::setPlayGain(float gain)
Chris@391: {
Chris@391:     m_to.setPlayGain(gain);
Chris@391: }
Chris@391: 
Chris@391: void
Chris@391: PlayParameterRepository::EditCommand::setPlayPluginId(QString id)
Chris@391: {
Chris@391:     m_to.setPlayPluginId(id);
Chris@391: }
Chris@391: 
Chris@391: void
Chris@391: PlayParameterRepository::EditCommand::setPlayPluginConfiguration(QString conf)
Chris@391: {
Chris@391:     m_to.setPlayPluginConfiguration(conf);
Chris@391: }
Chris@391: 
Chris@391: void
Chris@391: PlayParameterRepository::EditCommand::execute()
Chris@391: {
Chris@391:     m_params->copyFrom(&m_to);
Chris@391: }
Chris@391: 
Chris@391: void
Chris@391: PlayParameterRepository::EditCommand::unexecute()
Chris@391: {
Chris@391:     m_params->copyFrom(&m_from);
Chris@391: }
Chris@391:     
Chris@391: QString
Chris@391: PlayParameterRepository::EditCommand::getName() const
Chris@391: {
Chris@391:     QString name;
Chris@391:     QString multiname = tr("Adjust Playback Parameters");
Chris@391: 
Chris@391:     int changed = 0;
Chris@391: 
Chris@391:     if (m_to.isPlayAudible() != m_from.isPlayAudible()) {
Chris@391:         name = tr("Change Playback Mute State");
Chris@391:         if (++changed > 1) return multiname;
Chris@391:     }
Chris@391: 
Chris@391:     if (m_to.getPlayGain() != m_from.getPlayGain()) {
Chris@391:         name = tr("Change Playback Gain");
Chris@391:         if (++changed > 1) return multiname;
Chris@391:     }
Chris@391: 
Chris@391:     if (m_to.getPlayPan() != m_from.getPlayPan()) {
Chris@391:         name = tr("Change Playback Pan");
Chris@391:         if (++changed > 1) return multiname;
Chris@391:     }
Chris@391: 
Chris@391:     if (m_to.getPlayPluginId() != m_from.getPlayPluginId()) {
Chris@391:         name = tr("Change Playback Plugin");
Chris@391:         if (++changed > 1) return multiname;
Chris@391:     }
Chris@391: 
Chris@391:     if (m_to.getPlayPluginConfiguration() != m_from.getPlayPluginConfiguration()) {
Chris@391:         name = tr("Configure Playback Plugin");
Chris@391:         if (++changed > 1) return multiname;
Chris@391:     }
Chris@391: 
Chris@391:     if (name == "") return multiname;
Chris@391:     return name;
Chris@391: }
Chris@391: