summaryrefslogtreecommitdiffstats
path: root/src/progressindicator.cpp
blob: 91a40a22d33eeec80992213eb8278987913966b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

#include "progressindicator.h"

#include <qlayout.h>
#include <qlabel.h>
#include <qprogressbar.h>
#include <qtooltip.h>

#include <klocale.h>
#include <ksystemtray.h>
//#include <kiconloader.h>
//#include <kmessagebox.h>
// #include <kdebug.h>


ProgressIndicator::ProgressIndicator( KSystemTray* _systemTray, QWidget* parent, const char* name )
    : QWidget( parent, name )
{
    systemTray = _systemTray;

    time = processedTime = 0;

    QGridLayout *grid = new QGridLayout( this, 1, 1, 0, 6, "grid" );

    QHBoxLayout *box = new QHBoxLayout( );
    grid->addLayout( box, 0, 0 );

    pBar = new QProgressBar( this, "pBar" );
    box->addWidget( pBar );

    QGridLayout *statusChildGrid = new QGridLayout( box, 2, 2, 6, "statusChildGrid" );

    QLabel *lSpeedText = new QLabel( i18n("Speed")+":", this, "lSpeedText" );
    statusChildGrid->addWidget( lSpeedText, 0, 0, Qt::AlignVCenter );

    lSpeed = new QLabel( "0.0x", this, "lSpeed" );
    lSpeed->setFont( QFont( "Courier" ) );
    statusChildGrid->addWidget( lSpeed, 0, 1, Qt::AlignVCenter | Qt::AlignRight );
    speedTime.setHMS( 24, 0, 0 );

    QLabel *lTimeText = new QLabel( i18n("Time remaining")+":", this, "lTimeText" );
    statusChildGrid->addWidget( lTimeText, 1, 0, Qt::AlignVCenter );

    lTime = new QLabel( "00:00", this, "lTime" );
    lTime->setFont( QFont( "Courier" ) );
    statusChildGrid->addWidget( lTime, 1, 1, Qt::AlignVCenter | Qt::AlignRight );
    elapsedTime.setHMS( 24, 0, 0 );

    QToolTip::add( systemTray, i18n("Waiting") );
}

ProgressIndicator::~ProgressIndicator()
{}

void ProgressIndicator::increaseTime( float t )
{
    time += t;
//     kdDebug() << "increaseTime: " << time << " (" << t  << ")" <<  endl;
}

void ProgressIndicator::decreaseTime( float t )
{
    time -= t;
//     kdDebug() << "decreaseTime: " << time << " (" << t  << ")" <<  endl;
}

void ProgressIndicator::countTime( float t )
{
    processedTime += t;
//     kdDebug() << "countTime: " << processedTime << " (" << t  << ")" <<  endl;
}

void ProgressIndicator::uncountTime( float t )
{
    processedTime -= t;
//     kdDebug() << "uncountTime: " << processedTime << " (" << t  << ")" <<  endl;
}

void ProgressIndicator::setTime( float t )
{
    time = t;
//     kdDebug() << "setTime: " << time << " (" << t  << ")" <<  endl;
}

void ProgressIndicator::finished( float t )
{
    time = t;
//     kdDebug() << "finished: " << time << " (" << t  << ")" <<  endl;
    processedTime = 0;
    pBar->setProgress( 100, 100 );
    elapsedTime.setHMS( 24, 0, 0 );
    lTime->setText( "00:00" );
    speedTime.setHMS( 24, 0, 0 );
    lSpeed->setText( "0.0x" );
    QToolTip::add( systemTray, i18n("Finished") );
    emit setTitle( i18n("Finished") );
}

void ProgressIndicator::update( float t )
{
//     kdDebug() << "update: " << (processedTime + t) << " / " << time  << endl;
    pBar->setProgress( int(processedTime + t), int(time) );
    float fPercent;
//     if( pBar->totalSteps() > 0 ) fPercent = pBar->progress() * 100 / pBar->totalSteps();
    if( pBar->totalSteps() > 0 ) fPercent = (processedTime+t)*100.0f / time;
    else fPercent = 0.1f;

    if( !elapsedTime.isValid() ) elapsedTime.start();
    if( !speedTime.isValid() ) speedTime.start();
    if( speedTime.elapsed() > 1000 ) {
        if( elapsedTime.elapsed() > 10000 ) {
//             int tim = (int)( 1 + ((100/fPercent)-1) * (elapsedTime.elapsed()/1000) );
            int tim = (int)(( 1.0f + ((100.0f/fPercent)-1.0f) * (elapsedTime.elapsed()/1000.0f) ));
            //float fPercent = pBar->progress() / pBar->totalSteps();
            //int tim = (int)( 1 + (elapsedTime.elapsed()/fPercent - elapsedTime.elapsed()) / 1000 );
            if( tim >= 0 && tim < 1000000 ) {
                int sec = tim % 60;
                int min = ( tim / 60 ) % 60;
                int hou = tim / 3600;
                QString timeLeft;
                if( hou > 0 )
                    timeLeft.sprintf( "%d:%02d:%02d", hou, min, sec );
                else
                    timeLeft.sprintf( "%02d:%02d", min, sec );
                lTime->setText( timeLeft );
            }
        }

        int tim = speedTime.restart() / 1000;
        float speed = ( processedTime + t - speedProcessedTime ) / tim;
        speedProcessedTime = processedTime + t;
        if( speed >= 0.0f && speed < 100000.0f ) {
            QString actSpeed;
            actSpeed.sprintf( "%.1fx", speed );
            lSpeed->setText( actSpeed );
        }
    }

    QString percent;
    percent.sprintf( "%i%%", (int)fPercent );
    emit setTitle( percent );
    QToolTip::add( systemTray, percent );
}