/***************************************************************************
                          prefsdialogimpl.cpp  -  description
                             -------------------
    begin                : Tue Mar 6 2001
    copyright            : (C) 2001 by
    email                :
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "prefsdialogimpl.h"

#include <qcombobox.h>
#include <qradiobutton.h>
#include <qcheckbox.h>
#include <qmessagebox.h>
#include <qdialog.h>

#include <kcolorbutton.h>
#include <kdebug.h>
#include <klocale.h>

PrefsDialogImpl::PrefsDialogImpl( QWidget *parent ) : QDialog(parent)
{
	setupUi(this);
	m_minRefreshTime = 50;
	m_maxRefreshTime = 100000;
	m_minSmoothFactor = 1;
	m_maxSmoothFactor = 20;
	m_minScaling = 1000;
	m_maxScaling = 2000000000;
}

PrefsDialogImpl::~PrefsDialogImpl()
{
}

void PrefsDialogImpl::setRefreshTimeChoices( Q3MemArray< int > choices )
{
	for( uint i=0; i<choices.count(); i++ )
		general_refreshTimeComboBox->insertItem(
			QString::number(choices[i]), i );
}

void PrefsDialogImpl::setSmoothFactorChoices( Q3MemArray< int > choices )
{
	for( uint i=0; i<choices.count(); i++ )
		general_smoothFactorComboBox->insertItem(
			QString::number(choices[i]), i );
}

void PrefsDialogImpl::setDeviceChoices( QVector< QString > choices )
{
	for( uint i=0; i<choices.count(); i++ )
		diagram_deviceComboBox->insertItem((choices[i]), i );
}

void PrefsDialogImpl::setScalingChoices( Q3MemArray< long > choices )
{
	for( uint i=0; i<choices.count(); i++ )
		diagram_scalingComboBox->insertItem(
			QString::number(choices[i]), i );
}

void PrefsDialogImpl::setRefreshTimeRange( int min, int max )
{
	m_minRefreshTime = min;
	m_maxRefreshTime = max;
}

void PrefsDialogImpl::setSmoothFactorRange( int min, int max )
{
	m_minSmoothFactor = min;
	m_maxSmoothFactor = max;
}

void PrefsDialogImpl::setScalingRange( long min, long max )
{
	m_minScaling = min;
	m_maxScaling = max;
}

void PrefsDialogImpl::setRefreshTime( int value )
{
	int index = -1;
	int count = general_refreshTimeComboBox->count();
	for( int i=0; i<count; i++ )
		if( general_refreshTimeComboBox->text(i) == QString::number(value) )
		{
			index = i;
			break;
		}
	if( index != -1 )
	{
		general_refreshTimeComboBox->setCurrentItem(index);
	}
	else
	{
		general_refreshTimeComboBox->insertItem(
			QString::number(value), count );
		general_refreshTimeComboBox->setCurrentItem(count);
	}
}

void PrefsDialogImpl::setSmoothFactor( int value )
{
	int index = -1;
	int count = general_smoothFactorComboBox->count();
	for( int i=0; i<count; i++ )
		if( general_smoothFactorComboBox->text(i) == QString::number(value) )
		{
			index = i;
			break;
		}
	if( index != -1 )
	{
		general_smoothFactorComboBox->setCurrentItem(index);
	}
	else
	{
		general_smoothFactorComboBox->insertItem(
			QString::number(value), count );
		general_smoothFactorComboBox->setCurrentItem(count);
	}
}

void PrefsDialogImpl::setUnit( Common::Unit value )
{
	if(value == Common::UNIT_BIT)
		general_unit_bitsRadioButton->setChecked(true);
	else if(value == Common::UNIT_BYTE)
		general_unit_bytesRadioButton->setChecked(true);
}

void PrefsDialogImpl::setIncomingColor( const QColor& color )
{
	general_colors_incomingColorButton->setColor( color );
}

void PrefsDialogImpl::setOutgoingColor( const QColor& color )
{
	general_colors_outgoingColorButton->setColor( color );
}

void PrefsDialogImpl::setDevice( const QString& value )
{
	int index = -1;
	int count = diagram_deviceComboBox->count();
	for( int i=0; i<count; i++ )
		if( diagram_deviceComboBox->text(i) == value )
		{
			index = i;
			break;
		}
	if( index != -1 )
	{
		diagram_deviceComboBox->setCurrentItem(index);
	}
	else
	{
		diagram_deviceComboBox->insertItem(
			value, count );
		diagram_deviceComboBox->setCurrentItem(count);
	}
}

void PrefsDialogImpl::setScaling( long value )
{
	int index = -1;
	int count = diagram_scalingComboBox->count();
	for( int i=0; i<count; i++ )
		if( diagram_scalingComboBox->text(i) == QString::number(value) )
		{
			index = i;
			break;
		}
	if( index != -1 )
	{
		diagram_scalingComboBox->setCurrentItem(index);
	}
	else
	{
		diagram_scalingComboBox->insertItem(
			QString::number(value), count );
		diagram_scalingComboBox->setCurrentItem(count);
	}
}

void PrefsDialogImpl::setScalingMode( Common::ScalingMode value )
{
	if( value == Common::MODE_NORMAL )
		diagram_scalingMode_normalRadioButton->setChecked(true);
	else if( value == Common::MODE_CBRT )
		diagram_scalingMode_cbrtRadioButton->setChecked(true);
}

void PrefsDialogImpl::setGraphPosition( Common::GraphPosition value )
{
	if( value == Common::POS_TOP_BOTTOM )
		diagram_graphPosition_topBottomRadioButton->setChecked(true);
	else if( value == Common::POS_BOTTOM )
		diagram_graphPosition_bottomRadioButton->setChecked(true);
}

void PrefsDialogImpl::setGraphStyle( Common::GraphStyle value )
{
	if( value == Common::STYLE_SHADED )
		diagram_graphStyle_shadedRadioButton->setChecked(true);
	else if( value == Common::STYLE_SOLID )
		diagram_graphStyle_solidRadioButton->setChecked(true);
	else if( value == Common::STYLE_LINES )
		diagram_graphStyle_linesRadioButton->setChecked(true);
}

void PrefsDialogImpl::setShowGrid( bool value )
{
	diagram_showGridCheckBox->setChecked(value);
}

void PrefsDialogImpl::setMainFrameBgColor( const QColor& color )
{
	mainFrame_bgColorColorButton->setColor( color );
}

void PrefsDialogImpl::setMainFrameGridColor( const QColor& color )
{
	mainFrame_gridColorColorButton->setColor( color );
}

void PrefsDialogImpl::setMainFrameShowTooltips( bool value )
{
	mainFrame_showTooltipsCheckBox->setChecked( value );
}

void PrefsDialogImpl::setMainFrameShowStatsBar( bool value )
{
	mainFrame_showStatsBarCheckBox->setChecked( value );
}

void PrefsDialogImpl::setMainFrameTransparent( bool value )
{
	mainFrame_transparentCheckBox->setChecked( value );
}

void PrefsDialogImpl::setMainFrameShowDecorations( bool value )
{
	mainFrame_showDecorationsCheckBox->setChecked( value );	
}

void PrefsDialogImpl::setDockFrameBgColor( const QColor& color )
{
	dockFrame_bgColorColorButton->setColor( color );
}

void PrefsDialogImpl::setDockFrameGridColor( const QColor& color )
{
	dockFrame_gridColorColorButton->setColor( color );
}

void PrefsDialogImpl::setDockFrameShowText( bool value )
{
	dockFrame_showTextCheckBox->setChecked( value );
	if (dockFrame_showTextCheckBox->isChecked())
	    dockFrame_TextSizeSpinBox->setEnabled(true);
	 else
	    dockFrame_TextSizeSpinBox->setEnabled(false);
}

void PrefsDialogImpl::setDockFrameTextSize( int value )
{
	dockFrame_TextSizeSpinBox->setValue( value );
}

void PrefsDialogImpl::setStatsFrameBgColor( const QColor& color )
{
	statsFrame_bgColorColorButton->setColor( color );
}

//void PrefsDialogImpl::setPopupFrameBgColor( const QColor& color )
//{
//	popupFrame_bgColorColorButton->setColor( color );
//}
//
//void PrefsDialogImpl::setPopupFrameFgColor( const QColor& color )
//{
//	popupFrame_fgColorColorButton->setColor( color );
//}
//
int PrefsDialogImpl::getRefreshTime()
{
	return general_refreshTimeComboBox->currentText().toInt();
}

int PrefsDialogImpl::getSmoothFactor()
{
	return general_smoothFactorComboBox->currentText().toInt();
}

Common::Unit PrefsDialogImpl::getUnit()
{
	if( general_unit_bitsRadioButton->isChecked() )
		return Common::UNIT_BIT;
	else
		return Common::UNIT_BYTE;
}

QColor PrefsDialogImpl::getIncomingColor()
{
	return general_colors_incomingColorButton->color();
}

QColor PrefsDialogImpl::getOutgoingColor()
{
	return general_colors_outgoingColorButton->color();
}

QString PrefsDialogImpl::getDevice()
{
	return diagram_deviceComboBox->currentText();
}

long PrefsDialogImpl::getScaling()
{
	return diagram_scalingComboBox->currentText().toInt();
}

Common::ScalingMode PrefsDialogImpl::getScalingMode()
{
	if( diagram_scalingMode_normalRadioButton->isChecked() )
		return Common::MODE_NORMAL;
	else
		return Common::MODE_CBRT;
}

Common::GraphPosition PrefsDialogImpl::getGraphPosition()
{
	if( diagram_graphPosition_topBottomRadioButton->isChecked() )
		return Common::POS_TOP_BOTTOM;
	else
		return Common::POS_BOTTOM;
}

Common::GraphStyle PrefsDialogImpl::getGraphStyle()
{
	if( diagram_graphStyle_shadedRadioButton->isChecked() )
		return Common::STYLE_SHADED;
	else if( diagram_graphStyle_solidRadioButton->isChecked() )
		return Common::STYLE_SOLID;
	else
		return Common::STYLE_LINES;
}

bool PrefsDialogImpl::getShowGrid()
{
	return diagram_showGridCheckBox->isChecked();
}

QColor PrefsDialogImpl::getMainFrameBgColor()
{
	return mainFrame_bgColorColorButton->color();
}

QColor PrefsDialogImpl::getMainFrameGridColor()
{
	return mainFrame_gridColorColorButton->color();
}

bool PrefsDialogImpl::getMainFrameShowTooltips()
{
	return mainFrame_showTooltipsCheckBox->isChecked();
}

bool PrefsDialogImpl::getMainFrameShowStatsBar()
{
	return mainFrame_showStatsBarCheckBox->isChecked();
}

bool PrefsDialogImpl::getMainFrameTransparent()
{
	return mainFrame_transparentCheckBox->isChecked();
}

bool PrefsDialogImpl::getMainFrameShowDecorations()
{
	return mainFrame_showDecorationsCheckBox->isChecked();
}

QColor PrefsDialogImpl::getDockFrameBgColor()
{
	return dockFrame_bgColorColorButton->color();
}

QColor PrefsDialogImpl::getDockFrameGridColor()
{
	return dockFrame_gridColorColorButton->color();
}

bool PrefsDialogImpl::getDockFrameShowText()
{
	return dockFrame_showTextCheckBox->isChecked();
}

int PrefsDialogImpl::getDockFrameTextSize()
{
	return dockFrame_TextSizeSpinBox->value();
}



//QColor PrefsDialogImpl::getPopupFrameBgColor()
//{
//	return popupFrame_bgColorColorButton->color();
//}
//
//QColor PrefsDialogImpl::getPopupFrameFgColor()
//{
//	return popupFrame_fgColorColorButton->color();
//}
//

QColor PrefsDialogImpl::getStatsFrameBgColor()
{
	return statsFrame_bgColorColorButton->color();
}

void PrefsDialogImpl::slotCheck()
{
	QString errorMessage = QString::null;
	if( (getRefreshTime() < m_minRefreshTime) ||
		(getRefreshTime() > m_maxRefreshTime) )
	{
		errorMessage += i18n("Refresh time not legal.") + '\n';
	}
	if( (getSmoothFactor() < m_minSmoothFactor) ||
		(getSmoothFactor() > m_maxSmoothFactor) )
	{
		errorMessage += i18n("Smooth factor not legal.") + '\n';
	}
	if( (getScaling() < m_minScaling) ||
		(getScaling() > m_maxScaling) )
	{
		errorMessage += i18n("Scaling not legal.") + '\n';
	}

	if( errorMessage == QString::null )
	{
		accept();
	}
	else
	{
		QMessageBox::information(
			this, i18n("Error"), errorMessage, QMessageBox::Ok );
		return;
	}
}
