summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/examples/progressbar/ProgressBar.java
blob: 0737e270576347c9812833d596c0c3d17d06a802 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/***************************************************************************
* $Id$
**
* Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
* This file is part of an example program for Qt.  This example
* program may be used, distributed and modified without limitation.
**
****************************************************************************/

import org.kde.qt.*;


class ProgressBar extends TQButtonGroup
{

protected TQRadioButton slow, normal, fast;
protected TQPushButton start, pause, reset;
protected TQProgressBar progress;
protected TQTimer timer;


/*
  Constructor
 *
  Creates child widgets of the ProgressBar widget
 */

ProgressBar( )
{
	this(null, null);
}

ProgressBar( TQWidget parent, String name )
{
    super( 0, Horizontal, "Progress Bar", parent, name );
	timer = new TQTimer();
    setMargin( 10 );

    TQGridLayout toplayout = new TQGridLayout( layout(), 2, 2, 5);

    setRadioButtonExclusive( true );

    // insert three radiobuttons which the user can use
    // to set the speed of the progress and two pushbuttons
    // to start/pause/continue and reset the progress
    slow = new TQRadioButton( "&Slow", this );
    normal = new TQRadioButton( "&Normal", this );
    fast = new TQRadioButton( "&Fast", this );
    TQVBoxLayout vb1 = new TQVBoxLayout();
    toplayout.addLayout( vb1, 0, 0 );
    vb1.addWidget( slow );
    vb1.addWidget( normal );
    vb1.addWidget( fast );

    // two push buttons, one for start, for for reset.
    start = new TQPushButton( "&Start", this );
    reset = new TQPushButton( "&Reset", this );
    TQVBoxLayout vb2 = new TQVBoxLayout();
    toplayout.addLayout( vb2, 0, 1 );
    vb2.addWidget( start );
    vb2.addWidget( reset );

    // Create the progressbar
    progress = new TQProgressBar( 100, this );
    //    progress.setStyle( new TQMotifStyle() );
    toplayout.addMultiCellWidget( progress, 1, 1, 0, 1 );

    // connect the clicked() SIGNALs of the pushbuttons to SLOTs
    connect( start, SIGNAL(" clicked()"), this, SLOT(" slotStart()") );
    connect( reset, SIGNAL(" clicked()"), this, SLOT(" slotReset()") );

    // connect the timeout() SIGNAL of the progress-timer to a SLOT
    connect( timer, SIGNAL(" timeout()"), this, SLOT(" slotTimeout()") );

    // Let's start with normal speed...
    normal.setChecked( true );


    // some contraints
    start.setFixedWidth( 80 );
    setMinimumWidth( 300 );
}

/*
  SLOT slotStart
 *
  This SLOT is called if the user clicks start/pause/continue
  button
 */

void slotStart()
{
    // If the progress bar is at the beginning...
    if ( progress.progress() == -1 ) {
        // ...set according to the checked speed-radiobutton
        // the number of steps which are needed to complete the process
        if ( slow.isChecked() )
            progress.setTotalSteps( 10000 );
        else if ( normal.isChecked() )
            progress.setTotalSteps( 1000 );
        else
            progress.setTotalSteps( 50 );

        // disable the speed-radiobuttons
        slow.setEnabled( false );
        normal.setEnabled( false );
        fast.setEnabled( false );
    }

    // If the progress is not running...
    if ( !timer.isActive() ) {
        // ...start the timer (and so the progress) with a interval of 1 ms...
        timer.start( 1 );
        // ...and rename the start/pause/continue button to Pause
        start.setText( "&Pause" );
    } else { // if the prgress is running...
        // ...stop the timer (and so the prgress)...
        timer.stop();
        // ...and rename the start/pause/continue button to Continue
        start.setText( "&Continue" );
    }
}

/*
  SLOT slotReset
 *
  This SLOT is called when the user clicks the reset button
 */

void slotReset()
{
    // stop the timer and progress
    timer.stop();

    // rename the start/pause/continue button to Start...
    start.setText( "&Start" );
    // ...and enable this button
    start.setEnabled( true );

    // enable the speed-radiobuttons
    slow.setEnabled( true );
    normal.setEnabled( true );
    fast.setEnabled( true );

    // reset the progressbar
    progress.reset();
}

/*
  SLOT slotTimeout
 *
  This SLOT is called each ms when the timer is
  active (== progress is running)
 */

void slotTimeout()
{
    int p = progress.progress();

    // If the progress is complete...
    if ( p == progress.totalSteps() )  {
        // ...rename the start/pause/continue button to Start...
        start.setText( "&Start" );
        // ...and disable it...
        start.setEnabled( false );
        // ...and return
        return;
    }

    // If the process is not complete increase it
    progress.setProgress( ++p );
}
}