summaryrefslogtreecommitdiffstats
path: root/doc/html/progress-example.html
blob: 009b4dab0f1db596571a4ffa2aa2a623c1739ba0 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/progress/progress.doc:4 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Progress Bar and Dialog Example</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Progress Bar and Dialog Example</h1>

   
<p> 
This example displays either a simple (text-only) or a
custom-labelled (user-supplied widget) progress dialog.  It also
demonstrates simple use of menus.
<hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** $Id: qt/progress.cpp   3.3.8   edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include &lt;<a href="qprogressdialog-h.html">ntqprogressdialog.h</a>&gt;
#include &lt;<a href="qapplication-h.html">ntqapplication.h</a>&gt;
#include &lt;<a href="qmenubar-h.html">ntqmenubar.h</a>&gt;
#include &lt;<a href="qpopupmenu-h.html">ntqpopupmenu.h</a>&gt;
#include &lt;<a href="qpainter-h.html">ntqpainter.h</a>&gt;
#include &lt;stdlib.h&gt;

class AnimatedThingy : public <a href="ntqlabel.html">TQLabel</a> {
public:
    AnimatedThingy( <a href="ntqwidget.html">TQWidget</a>* parent, const <a href="ntqstring.html">TQString</a>&amp; s ) :
        <a href="ntqlabel.html">TQLabel</a>(parent),
        label(s),
        step(0)
    {
        setBackgroundColor(white);
        label+="\n... and wasting CPU\nwith this animation!\n";

        for (int i=0; i&lt;nqix; i++)
            ox[0][i] = oy[0][i] = ox[1][i] = oy[1][i] = 0;
        x0 = y0 = x1 = y1 = 0;
        dx0 = rand()%8+2;
        dy0 = rand()%8+2;
        dx1 = rand()%8+2;
        dy1 = rand()%8+2;
    }

    void show()
    {
        if (!isVisible()) startTimer(100);
        TQWidget::<a href="ntqwidget.html#show">show</a>();
    }

    void hide()
    {
<a name="x89"></a>        TQWidget::<a href="ntqwidget.html#hide">hide</a>();
        killTimers();
    }

    <a href="ntqsize.html">TQSize</a> sizeHint() const
    {
        return TQSize(120,100);
    }

protected:
    void timerEvent(TQTimerEvent*)
    {
        <a href="ntqpainter.html">TQPainter</a> p(this);
<a name="x76"></a>        <a href="ntqpen.html">TQPen</a> pn=p.<a href="ntqpainter.html#pen">pen</a>();
<a name="x80"></a>        pn.<a href="ntqpen.html#setWidth">setWidth</a>(2);
<a name="x79"></a>        pn.<a href="ntqpen.html#setColor">setColor</a>(backgroundColor());
<a name="x78"></a>        p.<a href="ntqpainter.html#setPen">setPen</a>(pn);

        step = (step + 1) % nqix;

<a name="x73"></a>        p.<a href="ntqpainter.html#drawLine">drawLine</a>(ox[0][step], oy[0][step], ox[1][step], oy[1][step]);

        inc(x0, dx0, width());
        inc(y0, dy0, height());
        inc(x1, dx1, width());
        inc(y1, dy1, height());
        ox[0][step] = x0;
        oy[0][step] = y0;
        ox[1][step] = x1;
        oy[1][step] = y1;

        <a href="ntqcolor.html">TQColor</a> c;
<a name="x66"></a>        c.<a href="ntqcolor.html#setHsv">setHsv</a>( (step*255)/nqix, 255, 255 ); // rainbow effect
        pn.<a href="ntqpen.html#setColor">setColor</a>(c);
        p.<a href="ntqpainter.html#setPen">setPen</a>(pn);
        p.<a href="ntqpainter.html#drawLine">drawLine</a>(ox[0][step], oy[0][step], ox[1][step], oy[1][step]);
        p.<a href="ntqpainter.html#setPen">setPen</a>(colorGroup().text());
<a name="x74"></a>        p.<a href="ntqpainter.html#drawText">drawText</a>(rect(), AlignCenter, label);
    }

    void paintEvent(TQPaintEvent* event)
    {
        <a href="ntqpainter.html">TQPainter</a> p(this);
        <a href="ntqpen.html">TQPen</a> pn=p.<a href="ntqpainter.html#pen">pen</a>();
        pn.<a href="ntqpen.html#setWidth">setWidth</a>(2);
        p.<a href="ntqpainter.html#setPen">setPen</a>(pn);
<a name="x77"></a>        p.<a href="ntqpainter.html#setClipRect">setClipRect</a>(event-&gt;rect());
        for (int i=0; i&lt;nqix; i++) {
            <a href="ntqcolor.html">TQColor</a> c;
            c.<a href="ntqcolor.html#setHsv">setHsv</a>( (i*255)/nqix, 255, 255 ); // rainbow effect
            pn.<a href="ntqpen.html#setColor">setColor</a>(c);
            p.<a href="ntqpainter.html#setPen">setPen</a>(pn);
            p.<a href="ntqpainter.html#drawLine">drawLine</a>(ox[0][i], oy[0][i], ox[1][i], oy[1][i]);
        }
        p.<a href="ntqpainter.html#setPen">setPen</a>(colorGroup().text());
        p.<a href="ntqpainter.html#drawText">drawText</a>(rect(), AlignCenter, label);
    }

private:
    void inc(int&amp; x, int&amp; dx, int b)
    {
        x+=dx;
        if (x&lt;0) { x=0; dx=rand()%8+2; }
        else if (x&gt;=b) { x=b-1; dx=-(rand()%8+2); }
    }

    enum {nqix=10};
    int ox[2][nqix];
    int oy[2][nqix];
    int x0,y0,x1,y1;
    int dx0,dy0,dx1,dy1;
    <a href="ntqstring.html">TQString</a> label;
    int step;
};


class CPUWaster : public <a href="ntqwidget.html">TQWidget</a>
{
    <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>

    enum { first_draw_item = 1000, last_draw_item = 1006 };

    int drawItemRects(int id)
    {
        int n = id - first_draw_item;
        int r = 100;
        while (n--) r*=(n%3 ? 5 : 4);
        return r;
    }
    <a href="ntqstring.html">TQString</a> drawItemText(int id)
    {
        <a href="ntqstring.html">TQString</a> str;
        str.<a href="ntqstring.html#sprintf">sprintf</a>("%d Rectangles", drawItemRects(id));
        return str;
    }

public:
    CPUWaster() :
        pb(0)
    {
        menubar = new <a href="ntqmenubar.html">TQMenuBar</a>( this, "menu" );
        <a href="ntqapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( menubar );

        <a href="ntqpopupmenu.html">TQPopupMenu</a>* file = new <a href="ntqpopupmenu.html">TQPopupMenu</a>();
        <a href="ntqapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( file );
        menubar-&gt;<a href="ntqmenudata.html#insertItem">insertItem</a>( "&amp;File", file );
        for (int i=first_draw_item; i&lt;=last_draw_item; i++)
            file-&gt;<a href="ntqmenudata.html#insertItem">insertItem</a>( drawItemText(i), i );
<a name="x67"></a>        connect( menubar, SIGNAL(<a href="ntqmenubar.html#activated">activated</a>(int)), this, SLOT(doMenuItem(int)) );
<a name="x69"></a>        file-&gt;<a href="ntqmenudata.html#insertSeparator">insertSeparator</a>();
        file-&gt;<a href="ntqmenudata.html#insertItem">insertItem</a>( "Quit", qApp,  SLOT(<a href="ntqapplication.html#quit">quit</a>()) );

        options = new <a href="ntqpopupmenu.html">TQPopupMenu</a>();
        <a href="ntqapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( options );
        menubar-&gt;<a href="ntqmenudata.html#insertItem">insertItem</a>( "&amp;Options", options );
        td_id = options-&gt;<a href="ntqmenudata.html#insertItem">insertItem</a>( "Timer driven", this, SLOT(timerDriven()) );
        ld_id = options-&gt;<a href="ntqmenudata.html#insertItem">insertItem</a>( "Loop driven", this, SLOT(loopDriven()) );
        options-&gt;<a href="ntqmenudata.html#insertSeparator">insertSeparator</a>();
        dl_id = options-&gt;<a href="ntqmenudata.html#insertItem">insertItem</a>( "Default label", this, SLOT(defaultLabel()) );
        cl_id = options-&gt;<a href="ntqmenudata.html#insertItem">insertItem</a>( "Custom label", this, SLOT(customLabel()) );
        options-&gt;<a href="ntqmenudata.html#insertSeparator">insertSeparator</a>();
        md_id = options-&gt;<a href="ntqmenudata.html#insertItem">insertItem</a>( "No minimum duration", this, SLOT(toggleMinimumDuration()) );
<a name="x81"></a>        options-&gt;<a href="ntqpopupmenu.html#setCheckable">setCheckable</a>( TRUE );
        loopDriven();
        defaultLabel();

        setFixedSize( 400, 300 );

        setBackgroundColor( black );
    }

public slots:
    void doMenuItem(int id)
    {
        if (id &gt;= first_draw_item &amp;&amp; id &lt;= last_draw_item)
            draw(drawItemRects(id));
    }

    void stopDrawing() { got_stop = TRUE; }

    void timerDriven()
    {
        timer_driven = TRUE;
<a name="x71"></a>        options-&gt;<a href="ntqmenudata.html#setItemChecked">setItemChecked</a>( td_id, TRUE );
        options-&gt;<a href="ntqmenudata.html#setItemChecked">setItemChecked</a>( ld_id, FALSE );
    }

    void loopDriven()
    {
        timer_driven = FALSE;
        options-&gt;<a href="ntqmenudata.html#setItemChecked">setItemChecked</a>( ld_id, TRUE );
        options-&gt;<a href="ntqmenudata.html#setItemChecked">setItemChecked</a>( td_id, FALSE );
    }

    void defaultLabel()
    {
        default_label = TRUE;
        options-&gt;<a href="ntqmenudata.html#setItemChecked">setItemChecked</a>( dl_id, TRUE );
        options-&gt;<a href="ntqmenudata.html#setItemChecked">setItemChecked</a>( cl_id, FALSE );
    }

    void customLabel()
    {
        default_label = FALSE;
        options-&gt;<a href="ntqmenudata.html#setItemChecked">setItemChecked</a>( dl_id, FALSE );
        options-&gt;<a href="ntqmenudata.html#setItemChecked">setItemChecked</a>( cl_id, TRUE );
    }

    void toggleMinimumDuration()
    {
        options-&gt;<a href="ntqmenudata.html#setItemChecked">setItemChecked</a>( md_id,
<a name="x70"></a>           !options-&gt;<a href="ntqmenudata.html#isItemChecked">isItemChecked</a>( md_id ) );
    }

private:
    void timerEvent( <a href="qtimerevent.html">TQTimerEvent</a>* )
    {
        if (!got_stop)
<a name="x86"></a><a name="x85"></a>            pb-&gt;<a href="ntqprogressdialog.html#setProgress">setProgress</a>( pb-&gt;<a href="ntqprogressdialog.html#totalSteps">totalSteps</a>() - rects );
        rects--;

        {
            <a href="ntqpainter.html">TQPainter</a> p(this);

            int ww = width();
            int wh = height();

            if ( ww &gt; 8 &amp;&amp; wh &gt; 8 ) {
                <a href="ntqcolor.html">TQColor</a> c(rand()%255, rand()%255, rand()%255);
                int x = rand() % (ww-8);
                int y = rand() % (wh-8);
                int w = rand() % (ww-x);
                int h = rand() % (wh-y);
<a name="x75"></a>                p.<a href="ntqpainter.html#fillRect">fillRect</a>( x, y, w, h, c );
            }
        }

        if (!rects || got_stop) {
            if (!got_stop)
                pb-&gt;<a href="ntqprogressdialog.html#setProgress">setProgress</a>( pb-&gt;<a href="ntqprogressdialog.html#totalSteps">totalSteps</a>() );
            <a href="ntqpainter.html">TQPainter</a> p(this);
            p.<a href="ntqpainter.html#fillRect">fillRect</a>(0, 0, width(), height(), backgroundColor());
            enableDrawingItems(TRUE);
            killTimers();
            delete pb;
            pb = 0;
        }
    }

    <a href="ntqprogressdialog.html">TQProgressDialog</a>* newProgressDialog( const char* label, int steps, bool modal )
    {
        <a href="ntqprogressdialog.html">TQProgressDialog</a> *d = new <a href="ntqprogressdialog.html">TQProgressDialog</a>(label, "Cancel", steps, this,
                                                 "progress", modal);
        if ( options-&gt;<a href="ntqmenudata.html#isItemChecked">isItemChecked</a>( md_id ) )
<a name="x84"></a>            d-&gt;<a href="ntqprogressdialog.html#setMinimumDuration">setMinimumDuration</a>(0);
        if ( !default_label )
<a name="x83"></a>            d-&gt;<a href="ntqprogressdialog.html#setLabel">setLabel</a>( new AnimatedThingy(d,label) );
        return d;
    }

    void enableDrawingItems(bool yes)
    {
        for (int i=first_draw_item; i&lt;=last_draw_item; i++) {
<a name="x72"></a>            menubar-&gt;<a href="ntqmenudata.html#setItemEnabled">setItemEnabled</a>(i, yes);
        }
    }

    void draw(int n)
    {
        if ( timer_driven ) {
            if ( pb ) {
                <a href="ntqapplication.html#qWarning">qWarning</a>("This cannot happen!");
                return;
            }
            rects = n;
            pb = newProgressDialog("Drawing rectangles.\n"
                                   "Using timer event.", n, FALSE);
            pb-&gt;<a href="ntqwidget.html#setCaption">setCaption</a>("Please Wait");
<a name="x82"></a>            connect(pb, SIGNAL(<a href="ntqprogressdialog.html#cancelled">cancelled</a>()), this, SLOT(stopDrawing()));
            enableDrawingItems(FALSE);
            startTimer(0);
            got_stop = FALSE;
        } else {
            <a href="ntqprogressdialog.html">TQProgressDialog</a>* lpb = newProgressDialog(
                        "Drawing rectangles.\nUsing loop.", n, TRUE);
            lpb-&gt;<a href="ntqwidget.html#setCaption">setCaption</a>("Please Wait");

            <a href="ntqpainter.html">TQPainter</a> p(this);
            for (int i=0; i&lt;n; i++) {
                lpb-&gt;<a href="ntqprogressdialog.html#setProgress">setProgress</a>(i);
<a name="x87"></a>                if ( lpb-&gt;<a href="ntqprogressdialog.html#wasCancelled">wasCancelled</a>() )
                    break;

                <a href="ntqcolor.html">TQColor</a> c(rand()%255, rand()%255, rand()%255);
                int x = rand()%(width()-8);
                int y = rand()%(height()-8);
                int w = rand()%(width()-x);
                int h = rand()%(height()-y);
                p.<a href="ntqpainter.html#fillRect">fillRect</a>(x,y,w,h,c);
            }

            p.<a href="ntqpainter.html#fillRect">fillRect</a>(0, 0, width(), height(), backgroundColor());

            delete lpb;
        }
    }

    <a href="ntqmenubar.html">TQMenuBar</a>* menubar;
    <a href="ntqprogressdialog.html">TQProgressDialog</a>* pb;
    <a href="ntqpopupmenu.html">TQPopupMenu</a>* options;
    int td_id, ld_id;
    int dl_id, cl_id;
    int md_id;
    int rects;
    bool timer_driven;
    bool default_label;
    bool got_stop;
};

int main( int argc, char **argv )
{
    <a href="ntqapplication.html">TQApplication</a> a( argc, argv );

    int wincount = argc &gt; 1 ? atoi(argv[1]) : 1;

    for ( int i=0; i&lt;wincount; i++ ) {
        CPUWaster* cpuw = new CPUWaster;
        if ( i == 0 ) a.<a href="ntqapplication.html#setMainWidget">setMainWidget</a>(cpuw);
        cpuw-&gt;<a href="ntqwidget.html#show">show</a>();
    }
    return a.<a href="ntqapplication.html#exec">exec</a>();
}

#include "progress.moc"
</pre>

<p>See also <a href="examples.html">Examples</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>TQt 3.3.8</div>
</table></div></address></body>
</html>