#include <klocale.h>
/****************************************************************************
** Form implementation generated from reading ui file '/Linux/Programming/kdsl/src/statusDlg.ui'
**
** Created: Sat Aug 21 23:09:56 2004
**      by: The User Interface Compiler ($Id: qt/main.cpp   3.3.2   edited Nov 24 13:47 $)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/

#include "statusDlg.h"

#include <qvariant.h>
#include <qpushbutton.h>
#include <qframe.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qpainter.h>
#include <kiconloader.h>
#include <kapplication.h>
#include <klocale.h>
#include <kmessagebox.h>
#include "pppstats.h"
#include "docking.h"
#include "configuration.h"
#include "docking.h"

/*
 *  Constructs a kdslStats as a child of 'parent', with the
 *  name 'name' and widget flags set to 'f'.
 */
 
 extern Configuration gpppdata;
 extern PPPStats* stats;
 extern DockWidget* dock_widget;
   
kdslStats::kdslStats( QWidget* parent, const char* name, QWidget *, PPPStats *st )
    : QWidget( parent, name ),
      stats(st)
{
//   int i;
  max = 1024;

    if ( !name )
	setName( "kdslStats" );
    kdslStatsLayout = new QGridLayout( this, 1, 1, 11, 6, "kdslStatsLayout"); 

    frame16 = new QFrame( this, "frame16" );
    frame16->setFrameShape( QFrame::StyledPanel );
    frame16->setFrameShadow( QFrame::Sunken );
    frame16Layout = new QGridLayout( frame16, 1, 1, 11, 6, "frame16Layout"); 

    layout40 = new QGridLayout( 0, 1, 1, 0, 6, "layout40"); 

    layout39 = new QGridLayout( 0, 1, 1, 0, 6, "layout39"); 

    layout34 = new QHBoxLayout( 0, 0, 6, "layout34"); 

    textLabel6 = new QLabel( frame16, "textLabel6" );
    layout34->addWidget( textLabel6 );

    timeLabel = new QLabel( frame16, "timeLabel" );
    layout34->addWidget( timeLabel );

    layout39->addLayout( layout34, 1, 0 );

    layout33 = new QHBoxLayout( 0, 0, 6, "layout33"); 

    textLabel2 = new QLabel( frame16, "textLabel2" );
    layout33->addWidget( textLabel2 );

    localIPLabel = new QLabel( frame16, "localIPLabel" );
    layout33->addWidget( localIPLabel );

    layout39->addLayout( layout33, 2, 0 );

    layout35 = new QHBoxLayout( 0, 0, 6, "layout35"); 

    textLabel1 = new QLabel( frame16, "textLabel1" );
    layout35->addWidget( textLabel1 );

    connectionLabel = new QLabel( frame16, "connectionLabel" );
    layout35->addWidget( connectionLabel );

    layout39->addLayout( layout35, 0, 0 );

    layout38 = new QHBoxLayout( 0, 0, 6, "layout38"); 

    receivedLabel = new QLabel( frame16, "receivedLabel" );
    layout38->addWidget( receivedLabel );

    sentLabel = new QLabel( frame16, "sentLabel" );
    layout38->addWidget( sentLabel );

    layout39->addLayout( layout38, 4, 0 );

    layout32 = new QHBoxLayout( 0, 0, 6, "layout32"); 

    textLabel3 = new QLabel( frame16, "textLabel3" );
    layout32->addWidget( textLabel3 );

    remoteIPLabel = new QLabel( frame16, "remoteIPLabel" );
    layout32->addWidget( remoteIPLabel );

    layout39->addLayout( layout32, 3, 0 );

    layout40->addLayout( layout39, 0, 0 );

    graph = new QFrame( frame16, 0 );
    graph->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)5, 0, 80, graph->sizePolicy().hasHeightForWidth() ) );

    graph->setBackgroundColor( QColor(0, 0, 0 ) );
    graph->setFrameShape( QFrame::GroupBoxPanel );
    graph->setFrameShadow( QFrame::Sunken );
    graph->setFixedHeight(76+4);
    graph->setMinimumWidth(300);    
    layout40->addWidget( graph, 1, 0 );

    frame16Layout->addLayout( layout40, 0, 0 );

    kdslStatsLayout->addWidget( frame16, 0, 0 );
    languageChange();
    resize( QSize(346, 241).expandedTo(minimumSizeHint()) );
//    clearWState( WState_Polished );

  
      graphTimer = new QTimer(this);
      connect(graphTimer, SIGNAL(timeout()), SLOT(updateGraph()));

      int p_x, p_y;     
     gpppdata.winPosStatWin(p_x, p_y);      
     move(p_x, p_y);

    connect(stats, SIGNAL( interfaceDown() ), this, SLOT(interfaceDown()));
    connect(stats, SIGNAL( lostConnection() ), this, SLOT(interfaceDown()));

}

/*
 *  Destroys the object and frees any allocated resources
 */
kdslStats::~kdslStats()
{
    // no need to delete child widgets, Qt does it all for us
}

void kdslStats::interfaceDown()
{
     timeLabel->setText("Disconnected");      

}

/*
 *  Sets the strings of the subwidgets using the current
 *  language.
 */
void kdslStats::languageChange()
{
    setCaption( tr2i18n( "Kdsl Statistics" ) );
    textLabel6->setText( tr2i18n( "Connected For:" ) );
    timeLabel->setText( tr2i18n( "0:00:00" ) );
    textLabel2->setText( tr2i18n( "Local IP Address" ) );
    localIPLabel->setText( QString::null );
    textLabel1->setText( tr2i18n( "Connected To:" ) );
    connectionLabel->setText( QString::null );
    receivedLabel->setText( QString::null );
    sentLabel->setText( QString::null );
    textLabel3->setText( tr2i18n( "Remote IP Address" ) );
    remoteIPLabel->setText( QString::null );
}

void kdslStats::take_stats() {
//  stats->initStats();
  bin_last = stats->ibytes;
  bout_last = stats->obytes;
  ringIdx = 0;
  for(int i = 0; i < MAX_GRAPH_WIDTH; i++) {
    bin[i] = -1;
    bout[i] = -1;
  }

  stats->start();
 // if(gpppdata.graphingEnabled())
connectionLabel->show();
localIPLabel->show();
remoteIPLabel->show();
connectionLabel->setText(gpppdata.accname());
//timeLabel->setText(m_kdsl->time_string2);
localIPLabel->setText(stats->local_ip_address);
remoteIPLabel->setText(stats->remote_ip_address); 
    graphTimer->start(GRAPH_UPDATE_TIME);
}

void kdslStats::stop_stats() {
  stats->stop();
    graphTimer->stop();
      localIPLabel->hide();
     remoteIPLabel->hide();
     connectionLabel->hide();
     timeLabel->setText("Disconnected");     
}

bool kdslStats::event(QEvent *e) {
  if (e->type() == QEvent::Hide)
  {
    gpppdata.setWinPosStatWin(x(), y());
    return true;
  }
  else 
    return QWidget::event(e);
}

void kdslStats::updateGraph()
{
  bin[ringIdx] = stats->ibytes - bin_last;
  bout[ringIdx] = stats->obytes - bout_last;
  if(bin[ringIdx] > max)
    max = ((bin[ringIdx] / 1024) + 1) * 1024;

 if(bout[ringIdx] > max)
    max = ((bout[ringIdx] / 1024) + 1) * 1024;

  bin_last = stats->ibytes;
  bout_last = stats->obytes;
  ringIdx = (ringIdx + 1) % MAX_GRAPH_WIDTH;
  paintGraph();
}

void kdslStats::paintEvent (QPaintEvent *) {
 // paintIcon(PPPStats::BytesNone); // correct ?
    paintGraph();
}


void kdslStats::startClock()
{
    qWarning( "kdslStats::startClock(): Not implemented yet" );
}


void kdslStats::paintGraph()
{

  if(!isVisible())
    return;

  QPixmap pm(graph->width() - 4, graph->height() - 4);
  QPainter p;
  pm.fill(QColor(0,0,0));
  p.begin(&pm);
 
  int x;
  int idx = ringIdx - pm.width() + 1;
  if(idx < 0)
    idx += MAX_GRAPH_WIDTH;

  // find good scaling factor
  int last_h_in =
    pm.height() - (int)((float)bin[idx]/max * (pm.height() - 8))-1;
  int last_h_out =
    pm.height() - (int)((float)bout[idx]/max * (pm.height() - 8))-1;

  // plot data
  int last_idx = 0;
  for(x = 1; x < pm.width(); x++) {
    int h_in, h_out;

    h_in = pm.height() - (int)((float)bin[idx]/max * (pm.height() - 8))-1;
    h_out = pm.height() - (int)((float)bout[idx]/max * (pm.height() - 8))-1;

    p.setPen(QPen(blue, 2, QPen::DotLine));

    if(bout[idx]!=-1)
      p.drawLine(x-1, last_h_out, x, h_out);
    p.setPen(QPen(yellow, 2, QPen::DotLine));

    if(bin[idx]!=-1)
      p.drawLine(x-1, last_h_in, x, h_in);
    last_h_in = h_in;
    last_h_out = h_out;

    last_idx = idx;
    idx = (idx + 1) % MAX_GRAPH_WIDTH;
  }

  // take last value
  int last_max = bin[last_idx]>bout[last_idx] ? bin[last_idx] : bout[last_idx];

  // plot scale line
  p.setPen(QPen(cyan, 2, QPen::SolidLine));
  p.setFont(QFont("fixed", 12));

  QRect r;
  QString s = i18n("%1 (max. %2) kb/sec")
		.arg(KGlobal::locale()->formatNumber((float)last_max / 1024.0, 1))
		.arg(KGlobal::locale()->formatNumber((float)max / 1024.0, 1));
  p.drawText(0, 0, pm.width(), 2*8, AlignRight|AlignVCenter, s, -1, &r);
  p.drawLine(0, 8, r.left() - 8, 8);

  p.end();
  bitBlt(graph, 2, 2, &pm, 0, 0, pm.width(), pm.height(), CopyROP);
}

#include "statusDlg.moc"
