/*    
    This file is part of kenvy24 KDE multimedia utility
    Copyright (c) 2007  Valentin RUSU kenvy24@rusu.info
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License or (at
    your option) version 3 or, at the discretion of KDE e.V. (which shall
    act as a proxy as in section 14 of the GPLv3), any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
*/

#include "volumeimpl.h"

#include <qbuttongroup.h>
#include <qtoolbutton.h>
#include <qdial.h>
#include <qslider.h>
#include <kconfiggroup.h>


VolumeImpl::VolumeImpl( QWidget* parent, const char * name ) : QWidget( parent ) {
    setupUi( this );
    volume->setInvertedAppearance(true);
}

void VolumeImpl::setCaption( const QString& title ) {
  buttonGroup->setTitle( title );
}

void VolumeImpl::mixerUpdateMuteSwitch( int index, LeftRight channel, bool state ) {
    if ( index == this->index && channel == this->channel) {
        ExclusiveFlag inSlot( inSlotFlag );
        if ( !inEventFlag )
            mute->setChecked( state );
    }
}

void VolumeImpl::mixerUpdatePlaybackVolume( int index, LeftRight channel, MixerAdjustement adj ) {
    if ( index == this->index && channel == this->channel) {
        ExclusiveFlag inSlot( inSlotFlag );
        if ( !inEventFlag )
            setAdjustment( adj );
    }
}

void VolumeImpl::setPan( int pan ) {
    this->pan->setValue( pan );
}

void VolumeImpl::setVolume( int vol ) {
    this->volume->setValue( vol );
}

void VolumeImpl::setMute( bool muteOn ) {
    mute->setChecked( muteOn );
}

void VolumeImpl::setAdjustment( MixerAdjustement adj) {
    setPan( adj.pan );
    setVolume( 96-adj.volume );
}

void VolumeImpl::muteToggled(bool muteState) {
    if ( !inSlotFlag ) {
        ExclusiveFlag inEvent( inEventFlag );
        emit muted( index, channel, muteState );
    }
}

void VolumeImpl::volumeChanged(int value) {
    if ( !inSlotFlag ) {
        ExclusiveFlag inEvent( inEventFlag );
        emit adjusted( index, channel, 96-value, pan->value() );
    }
}

void VolumeImpl::panChanged(int value) {
    if ( !inSlotFlag ) {
        ExclusiveFlag inEvent( inEventFlag );
        emit adjusted( index, channel, 96-volume->value(), value );
    }
}

void VolumeImpl::saveToConfig( KConfigGroup& config ) {
    QString keyBase = QString( "%1-%2-%3" ).arg( objectName() ).arg( index ).arg( channel );
    config.writeEntry( QString("%1-vol").arg( keyBase ), this->volume->value() );
    config.writeEntry( QString("%1-pan").arg( keyBase ), this->pan->value() );
    config.writeEntry( QString("%1-mute").arg( keyBase ), mute->isChecked() );
}

void VolumeImpl::loadFromConfig( KConfigGroup& config ) {
    QString keyBase = QString( "%1-%2-%3" ).arg( objectName() ).arg( index ).arg( channel );
    
    int panValue = 0;
    config.readEntry( QString("%1-pan").arg( keyBase ), panValue );
    this->pan->setValue( panValue );
    
    int volValue = 0;
    config.readEntry( QString("%1-vol").arg( keyBase ), volValue );
    this->volume->setValue( volValue );
    
    bool muteValue = true;
    config.readEntry( QString("%1-mute").arg( keyBase ), muteValue );
    mute->setChecked( muteValue );
}


#include "volumeimpl.moc"
