/***************************************************************************
                          AudioEstimatePnl.cpp -  description
                             -------------------
    begin                : Fri Jun 1 2001
    copyright            : (C) 2001 by Alexy Podolsky
    email                : alexpod@users.sourceforge.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include "AudioEstimatePnl.h"
#include "AudioEstimatePnl.moc"

#include <qvariant.h>
#include <qcombobox.h>
#include <qframe.h>
#include <qgroupbox.h>
#include <qlabel.h>
#include <qlcdnumber.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>

#include <klocale.h>
#include <kconfig.h>
#include <kmessagebox.h>


/* 
 *  Constructs a AudioEstimatePnl which is a child of 'parent', with the 
 *  name 'name' and widget flags set to 'f'.
 */
AudioEstimatePnl::AudioEstimatePnl( QWidget* parent,  const char* name, WFlags fl )
    : QWidget( parent, name, fl )
{
    if ( !name )
	setName( "AudioEstimatePnl" );
    resize( 209, 300 ); 
    setMinimumSize( QSize( 200, 300 ) );
    setMaximumSize( QSize( 230, 32767 ) );
    setCaption( i18n( "Form1" ) );
    AudioEstimatePnlLayout = new QVBoxLayout( this, 0, 0, "AudioEstimatePnlLayout"); 

    estFrm = new QGroupBox( this, "estFrm" );
    estFrm->setMinimumSize( QSize( 200, 300 ) );
    estFrm->setMaximumSize( QSize( 230, 32767 ) );
    estFrm->setMargin( 1 );
    estFrm->setTitle( i18n( "Estimate" ) );
    estFrm->setColumnLayout(0, Qt::Vertical );
    estFrm->layout()->setSpacing( 3 );
    estFrm->layout()->setMargin( 9 );
    estFrmLayout = new QGridLayout( estFrm->layout() );
    estFrmLayout->setAlignment( Qt::AlignTop );
    QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
    estFrmLayout->addItem( spacer, 3, 0 );
    QSpacerItem* spacer_2 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
    estFrmLayout->addItem( spacer_2, 3, 2 );

    recalcEstBtn = new QPushButton( estFrm, "recalcEstBtn" );
    recalcEstBtn->setMinimumSize( QSize( 120, 30 ) );
    recalcEstBtn->setMaximumSize( QSize( 120, 30 ) );
    QFont recalcEstBtn_font(  recalcEstBtn->font() );
    recalcEstBtn_font.setBold( TRUE );
    recalcEstBtn->setFont( recalcEstBtn_font ); 
    recalcEstBtn->setText( i18n( "Recalculate" ) );
    QToolTip::add( recalcEstBtn, i18n( "Recalculate estimate." ) );

    estFrmLayout->addWidget( recalcEstBtn, 3, 1 );
    QSpacerItem* spacer_3 = new QSpacerItem( 20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding );
    estFrmLayout->addItem( spacer_3, 2, 1 );

    GroupBox5 = new QGroupBox( estFrm, "GroupBox5" );
    GroupBox5->setMaximumSize( QSize( 210, 190 ) );
    GroupBox5->setTitle( i18n( "CD-R Capacity" ) );
    GroupBox5->setColumnLayout(0, Qt::Vertical );
    GroupBox5->layout()->setSpacing( 6 );
    GroupBox5->layout()->setMargin( 11 );
    GroupBox5Layout = new QHBoxLayout( GroupBox5->layout() );
    GroupBox5Layout->setAlignment( Qt::AlignTop );

    capacityCmb = new QComboBox( FALSE, GroupBox5, "capacityCmb" );
    capacityCmb->insertItem( i18n( "74 min" ) );
    capacityCmb->insertItem( i18n( "80 min" ) );
    capacityCmb->insertItem( i18n( "100 min" ) );
    capacityCmb->setMinimumSize( QSize( 85, 25 ) );
    capacityCmb->setMaximumSize( QSize( 90, 25 ) );
    QToolTip::add( capacityCmb, i18n( "Choose CD-R(W) capacity" ) );
    GroupBox5Layout->addWidget( capacityCmb );

    capacityLbl = new QLabel( GroupBox5, "capacityLbl" );
    capacityLbl->setMinimumSize( QSize( 70, 20 ) );
    capacityLbl->setMaximumSize( QSize( 70, 25 ) );
    QFont capacityLbl_font(  capacityLbl->font() );
    capacityLbl_font.setBold( TRUE );
    capacityLbl->setFont( capacityLbl_font ); 
    capacityLbl->setFrameShape( QLabel::Box );
    capacityLbl->setFrameShadow( QLabel::Raised );
    capacityLbl->setText( i18n( "650 Mb" ) );
    QToolTip::add( capacityLbl, i18n( "CD-R(W) Capacity" ) );
    GroupBox5Layout->addWidget( capacityLbl );

    estFrmLayout->addMultiCellWidget( GroupBox5, 0, 0, 0, 2 );

    GroupBox2_2_2 = new QGroupBox( estFrm, "GroupBox2_2_2" );
    GroupBox2_2_2->setMaximumSize( QSize( 210, 32767 ) );
    GroupBox2_2_2->setTitle( i18n( "Statistics" ) );
    GroupBox2_2_2->setColumnLayout(0, Qt::Vertical );
    GroupBox2_2_2->layout()->setSpacing( 6 );
    GroupBox2_2_2->layout()->setMargin( 11 );
    GroupBox2_2_2Layout = new QGridLayout( GroupBox2_2_2->layout() );
    GroupBox2_2_2Layout->setAlignment( Qt::AlignTop );

    TextLabel1_2 = new QLabel( GroupBox2_2_2, "TextLabel1_2" );
    TextLabel1_2->setText( i18n( "Min Wasted:" ) );

    GroupBox2_2_2Layout->addMultiCellWidget( TextLabel1_2, 1, 1, 0, 2 );

    numMp3LCD = new QLCDNumber( GroupBox2_2_2, "numMp3LCD" );
    numMp3LCD->setMinimumSize( QSize( 0, 30 ) );
    numMp3LCD->setMaximumSize( QSize( 30, 40 ) );
    numMp3LCD->setLineWidth( 1 );
    numMp3LCD->setMargin( 0 );
    numMp3LCD->setMidLineWidth( 0 );
    numMp3LCD->setNumDigits( 2 );
    numMp3LCD->setMode( QLCDNumber::DEC );
    numMp3LCD->setSegmentStyle( QLCDNumber::Filled );
    numMp3LCD->setProperty( "intValue", 0 );
    QToolTip::add( numMp3LCD, i18n( "Number of songs on Compilation list" ) );

    GroupBox2_2_2Layout->addWidget( numMp3LCD, 4, 1 );

    usedLCD = new QLCDNumber( GroupBox2_2_2, "usedLCD" );
    usedLCD->setMinimumSize( QSize( 60, 30 ) );
    usedLCD->setMaximumSize( QSize( 70, 35 ) );
    usedLCD->setMargin( 0 );
    usedLCD->setSmallDecimalPoint( true );
    usedLCD->setNumDigits( 4 );
    usedLCD->setMode( QLCDNumber::DEC );
    usedLCD->setSegmentStyle( QLCDNumber::Filled );
    usedLCD->setProperty( "intValue", 6656 );
    QToolTip::add( usedLCD, i18n( "Total time used" ) );

    GroupBox2_2_2Layout->addMultiCellWidget( usedLCD, 0, 0, 3, 4 );

    TextLabel1 = new QLabel( GroupBox2_2_2, "TextLabel1" );
    TextLabel1->setText( i18n( "Min Used:" ) );

    GroupBox2_2_2Layout->addMultiCellWidget( TextLabel1, 0, 0, 0, 2 );

    TextLabel2 = new QLabel( GroupBox2_2_2, "TextLabel2" );
    TextLabel2->setText( i18n( "Ogg:" ) );
    TextLabel2->setAlignment( int( QLabel::AlignVCenter | QLabel::AlignRight ) );

    GroupBox2_2_2Layout->addMultiCellWidget( TextLabel2, 4, 4, 2, 3 );

    numOfSongsLCD = new QLCDNumber( GroupBox2_2_2, "numOfSongsLCD" );
    numOfSongsLCD->setMinimumSize( QSize( 0, 30 ) );
    numOfSongsLCD->setMaximumSize( QSize( 30, 40 ) );
    numOfSongsLCD->setLineWidth( 1 );
    numOfSongsLCD->setMargin( 0 );
    numOfSongsLCD->setMidLineWidth( 0 );
    numOfSongsLCD->setNumDigits( 2 );
    numOfSongsLCD->setMode( QLCDNumber::DEC );
    numOfSongsLCD->setSegmentStyle( QLCDNumber::Filled );
    numOfSongsLCD->setProperty( "intValue", 0 );
    QToolTip::add( numOfSongsLCD, i18n( "Number of songs on Compilation list" ) );

    GroupBox2_2_2Layout->addWidget( numOfSongsLCD, 2, 4 );

    numOggLCD = new QLCDNumber( GroupBox2_2_2, "numOggLCD" );
    numOggLCD->setMinimumSize( QSize( 0, 5 ) );
    numOggLCD->setMaximumSize( QSize( 30, 40 ) );
    numOggLCD->setLineWidth( 1 );
    numOggLCD->setMargin( 0 );
    numOggLCD->setMidLineWidth( 0 );
    numOggLCD->setNumDigits( 2 );
    numOggLCD->setMode( QLCDNumber::DEC );
    numOggLCD->setSegmentStyle( QLCDNumber::Filled );
    numOggLCD->setProperty( "intValue", 0 );
    QToolTip::add( numOggLCD, i18n( "Number of songs on Compilation list" ) );

    GroupBox2_2_2Layout->addWidget( numOggLCD, 4, 4 );

    wastedLCD = new QLCDNumber( GroupBox2_2_2, "wastedLCD" );
    wastedLCD->setMinimumSize( QSize( 60, 30 ) );
    wastedLCD->setMaximumSize( QSize( 70, 35 ) );
    wastedLCD->setMargin( 0 );
    wastedLCD->setSmallDecimalPoint( true );
    wastedLCD->setNumDigits( 4 );
    wastedLCD->setMode( QLCDNumber::DEC );
    wastedLCD->setSegmentStyle( QLCDNumber::Filled );
    wastedLCD->setProperty( "intValue", 1020 );
    QToolTip::add( wastedLCD, i18n( "Total time wasted" ) );

    GroupBox2_2_2Layout->addMultiCellWidget( wastedLCD, 1, 1, 3, 4 );

    TextLabel2_2 = new QLabel( GroupBox2_2_2, "TextLabel2_2" );
    TextLabel2_2->setText( i18n( "MP3:" ) );

    GroupBox2_2_2Layout->addWidget( TextLabel2_2, 4, 0 );

    Line2 = new QFrame( GroupBox2_2_2, "Line2" );
    Line2->setProperty( "frameShape", (int)QFrame::HLine );
    Line2->setFrameShadow( QFrame::Sunken );
    Line2->setFrameShape( QFrame::HLine );

    GroupBox2_2_2Layout->addMultiCellWidget( Line2, 3, 3, 0, 4 );

    TextLabel1_2_2 = new QLabel( GroupBox2_2_2, "TextLabel1_2_2" );
    TextLabel1_2_2->setMaximumSize( QSize( 800, 32767 ) );
    TextLabel1_2_2->setText( i18n( "Total Tracks:" ) );

    GroupBox2_2_2Layout->addMultiCellWidget( TextLabel1_2_2, 2, 2, 0, 3 );

    estFrmLayout->addMultiCellWidget( GroupBox2_2_2, 1, 1, 0, 2 );
    AudioEstimatePnlLayout->addWidget( estFrm );

    // signals and slots connections
    connect( capacityCmb, SIGNAL( activated(int) ), this, SLOT( capacityChangedSlot(int) ) );


	usedSec = 0;
	wastedSec = 0;
	readOptions();
	synchUp();

}

/*  
 *  Destroys the object and frees any allocated resources
 */
AudioEstimatePnl::~AudioEstimatePnl(){
    // no need to delete child widgets, Qt does it all for us
}
//---------------------------------------------------------------------------------------------
void AudioEstimatePnl::readOptions(){
    KConfig *config = new KConfig("cdbakeovenrc");
    	config->setGroup("Audio Estimate");		
	int  iBuff = config->readNumEntry("Capacity Type", 0);
     capacityCmb->setCurrentItem(iBuff);
	capacityChangedSlot(iBuff);
	delete config;
}
//=======================================
void AudioEstimatePnl::saveOptions(){
	KConfig *config = new KConfig("cdbakeovenrc");
	config->setGroup("Audio Estimate");
	config->writeEntry("Capacity Type", capacityCmb->currentItem());
	
	config->sync();
	delete config;
}
//=======================================
void AudioEstimatePnl::capacityChangedSlot(int index){
   int buff = max;
	switch(index){
    		case 0: capacityLbl->setText( i18n( "650 MB" ) ); max = 4440;
				break;
    		case 1: capacityLbl->setText( i18n( "700 MB" ) ); max = 4800;
				break;
    		case 2: capacityLbl->setText( i18n( "875 MB" ) ); max = 6000;
				break;
	}
	if(usedSec > max){
		max = buff;
		if(max == 4800){
			capacityCmb->setCurrentItem(1);
			capacityLbl->setText( i18n( "700 MB" ) );
		}
		else{
			capacityCmb->setCurrentItem(2);
			capacityLbl->setText( i18n( "875 MB" ) );
		}
		KMessageBox::sorry(this,
				i18n("Unable to change capacity. This will put the compilation list over the size limit. Please remove some tracks off the list first."));
		return;
	}
	wastedSec = max - usedSec;
	
	synchUp();
	
}
//=======================================
bool AudioEstimatePnl::add(int length, int type){
	if(usedSec + length > max)
		return false;

	int buff = numOfSongsLCD->intValue();
	numOfSongsLCD->setProperty( "intValue", buff + 1 );
	if(type == mp3Track){
		buff = numMp3LCD->intValue();
		numMp3LCD->setProperty( "intValue", buff + 1 );
	}
	if(type == oggTrack){
		buff = numOggLCD->intValue();
		numOggLCD->setProperty( "intValue", buff + 1 );
	}
	usedSec += length;
	wastedSec -= length;
	synchUp();
	
	return true;
}
//=======================================
void AudioEstimatePnl::subtract(int length, int type){
	int buff = numOfSongsLCD->intValue();
	numOfSongsLCD->setProperty( "intValue", buff - 1 );
	if(type == mp3Track){
		buff = numMp3LCD->intValue();
		numMp3LCD->setProperty( "intValue", buff - 1 );
	}
	if(type == oggTrack){
		buff = numOggLCD->intValue();
		numOggLCD->setProperty( "intValue", buff - 1 );
	}
	usedSec -= length;
	wastedSec += length;
	synchUp();
}
//===============================
void AudioEstimatePnl::synchUp(){
	QString num;
	int min = wastedSec / 60;
	int sec = wastedSec % 60;
	if(sec < 10)
		sec = sec * 10;
	if(sec != 0)
		num = QString::number(min) + "." + QString::number(sec);
	else
		num = QString::number(min) + ".00";
	wastedLCD->display(num);
	
	min = usedSec / 60;
	sec = usedSec % 60;
	if(sec < 10)
		sec = sec * 10;
	if(sec != 0)
		num = QString::number(min) + "." + QString::number(sec);
	else
		num = QString::number(min) + ".00";
	usedLCD->display(num);

}
//=======================================
void AudioEstimatePnl::reset(){
	usedSec = 0;
	wastedSec = max;

	numOfSongsLCD->setProperty( "intValue", 0 );
	numMp3LCD->setProperty( "intValue", 0 );
	numOggLCD->setProperty( "intValue", 0 );
	synchUp();
}


