summaryrefslogtreecommitdiffstats
path: root/src/progressindicator.cpp
blob: ebc69a652b858113b680285f0dfca4956f91ff4b (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 <layout.h>
#include <tqlabel.h>
#include <tqprogressbar.h>
#include <tqtooltip.h>

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


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

    time = processedTime = 0;

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

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

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

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

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

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

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

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

    TQToolTip::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" );
    TQToolTip::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;
                TQString 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 ) {
            TQString actSpeed;
            actSpeed.sprintf( "%.1fx", speed );
            lSpeed->setText( actSpeed );
        }
    }

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