/*
    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 "mixerpartmonovumeter.h"
#include "envystructs.h"

#include <assert.h>
#include <QBrush>
#include <QPainter>

#include <kdebug.h>

#define TIMER_TICK_LEVEL_DROP  255 / (1000 / UPDATE_TIMER_INTERVAL)
#define PEAK_TICK_COUNT (500 / UPDATE_TIMER_INTERVAL)

MixerPartMonoVuMeter::MixerPartMonoVuMeter( QWidget* parent ) :
    QFrame( parent ),
    currentLevel( 0 ), peakLevel( 0 ), peakTickCount( PEAK_TICK_COUNT ),
    vuBrush(0),
    peakLevelDecaySpeed(0)
{
}

MixerPartMonoVuMeter::~MixerPartMonoVuMeter()
{
    if (vuBrush)
        delete vuBrush, vuBrush = 0;
}

void MixerPartMonoVuMeter::levelUpdated( int level )
{
    if ( level >=0 )
        currentLevel = level;
    else
        currentLevel = 0;
    if ( currentLevel > peakLevel )
    {
        peakLevel = currentLevel;
        // allow for a slight hold of peak indicators by setting
        // the time longer that normal updated interval
        peakTickCount = PEAK_TICK_COUNT * 3;
        peakLevelDecaySpeed = 0;
    }
    update();
}

void MixerPartMonoVuMeter::timerTick()
{
    if ( currentLevel >=0 )
    {
        levelUpdated( currentLevel - TIMER_TICK_LEVEL_DROP );
    }
    if ( peakTickCount == 0 )
    {
        if (peakLevel >0) 
        {
            peakTickCount = PEAK_TICK_COUNT;
            peakLevel -= TIMER_TICK_LEVEL_DROP * (1+peakLevelDecaySpeed);
            peakLevelDecaySpeed += 1;
            if ( peakLevel < 0)
                peakLevel = 0;
        }
    }
    else
        peakTickCount--;
}

void MixerPartMonoVuMeter::resizeEvent( QResizeEvent* resizeEvent )
{
    QFrame::resizeEvent( resizeEvent );

    QLinearGradient vuGradient( QPoint(0,0), QPoint(0, size().height()) );
    vuGradient.setColorAt(1.0, Qt::green);
    vuGradient.setColorAt(0.0, Qt::red);
    if (vuBrush)
        delete vuBrush, vuBrush = 0;
    vuBrush = new QBrush( vuGradient );
}

void MixerPartMonoVuMeter::paintEvent( QPaintEvent* )
{
    assert( vuBrush != 0);

    QPainter painter( this );

    // draw level rectangle
    int levelHeight = (currentLevel * size().height()) / MAX_LEVEL;
    painter.setBrush( *vuBrush );
    painter.drawRect( 0, size().height() - levelHeight, size().width(), levelHeight );

    // draw remainder rectangle (upper rectangle)
    painter.setBrush( QBrush( Qt::black) );
    painter.eraseRect( 0, 0, size().width(), size().height() - levelHeight - 1 );

    // draw peak indicator
    int peakHeight = (peakLevel * size().height()) / MAX_LEVEL;
    painter.setBrush( *vuBrush );
    painter.drawRect( 0, size().height() - peakHeight, size().width(), 2);
}

#include "mixerpartmonovumeter.moc"
// kate: space-indent on; indent-width 4; replace-tabs on;
