1/*
2 * This file Copyright (C) Mnemosyne LLC
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
7 *
8 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
9 *
10 * $Id: stats-dialog.cc 11092 2010-08-01 20:36:13Z charles $
11 */
12
13#include <QDialogButtonBox>
14#include <QLabel>
15#include <QTimer>
16#include <QVBoxLayout>
17
18#include "formatter.h"
19#include "hig.h"
20#include "session.h"
21#include "stats-dialog.h"
22
23enum
24{
25    REFRESH_INTERVAL_MSEC = (15*1000)
26};
27
28StatsDialog :: StatsDialog( Session & session, QWidget * parent ):
29    QDialog( parent, Qt::Dialog ),
30    mySession( session ),
31    myTimer( new QTimer( this ) )
32{
33    myTimer->setSingleShot( false );
34    connect( myTimer, SIGNAL(timeout()), this, SLOT(onTimer()) );
35    setWindowTitle( tr( "Statistics" ) );
36
37    HIG * hig = new HIG( );
38    hig->addSectionTitle( tr( "Current Session" ) );
39    hig->addRow( tr( "Uploaded:" ), myCurrentUp = new QLabel(  ) );
40    hig->addRow( tr( "Downloaded:" ), myCurrentDown = new QLabel( ) );
41    hig->addRow( tr( "Ratio:" ), myCurrentRatio = new QLabel( ) );
42    hig->addRow( tr( "Duration:" ), myCurrentDuration = new QLabel( ) );
43    hig->addSectionDivider( );
44    hig->addSectionTitle( tr( "Total" ) );
45    hig->addRow( myStartCount = new QLabel( tr( "Started %n time(s)", 0, 1 ) ), 0 );
46    hig->addRow( tr( "Uploaded:" ), myTotalUp = new QLabel( ) );
47    hig->addRow( tr( "Downloaded:" ), myTotalDown = new QLabel( ) );
48    hig->addRow( tr( "Ratio:" ), myTotalRatio = new QLabel( ) );
49    hig->addRow( tr( "Duration:" ), myTotalDuration = new QLabel( ) );
50    hig->finish( );
51
52    QLayout * layout = new QVBoxLayout( this );
53    layout->addWidget( hig );
54    QDialogButtonBox * buttons = new QDialogButtonBox( QDialogButtonBox::Close, Qt::Horizontal, this );
55    connect( buttons, SIGNAL(rejected()), this, SLOT(hide()) ); // "close" triggers rejected
56    layout->addWidget( buttons );
57
58    connect( &mySession, SIGNAL(statsUpdated()), this, SLOT(updateStats()) );
59    updateStats( );
60    mySession.refreshSessionStats( );
61}
62
63StatsDialog :: ~StatsDialog( )
64{
65}
66
67void
68StatsDialog :: setVisible( bool visible )
69{
70    myTimer->stop( );
71    if( visible )
72        myTimer->start( REFRESH_INTERVAL_MSEC );
73    QDialog::setVisible( visible );
74}
75
76void
77StatsDialog :: onTimer( )
78{
79    mySession.refreshSessionStats( );
80}
81
82void
83StatsDialog :: updateStats( )
84{
85    const struct tr_session_stats& current( mySession.getStats( ) );
86    const struct tr_session_stats& total( mySession.getCumulativeStats( ) );
87
88    myCurrentUp->setText( Formatter::sizeToString( current.uploadedBytes ) );
89    myCurrentDown->setText( Formatter::sizeToString( current.downloadedBytes ) );
90    myCurrentRatio->setText( Formatter::ratioToString( current.ratio ) );
91    myCurrentDuration->setText( Formatter::timeToString( current.secondsActive ) );
92
93    myTotalUp->setText( Formatter::sizeToString( total.uploadedBytes ) );
94    myTotalDown->setText( Formatter::sizeToString( total.downloadedBytes ) );
95    myTotalRatio->setText( Formatter::ratioToString( total.ratio ) );
96    myTotalDuration->setText( Formatter::timeToString( total.secondsActive ) );
97
98    myStartCount->setText( tr( "Started %n time(s)", 0, total.sessionCount ) );
99}
100