| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: progress.cpp 2 2005-11-16 15:49:26Z dmik $
|
|---|
| 3 | **
|
|---|
| 4 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of an example program for Qt. This example
|
|---|
| 7 | ** program may be used, distributed and modified without limitation.
|
|---|
| 8 | **
|
|---|
| 9 | *****************************************************************************/
|
|---|
| 10 |
|
|---|
| 11 | #include <qprogressdialog.h>
|
|---|
| 12 | #include <qapplication.h>
|
|---|
| 13 | #include <qmenubar.h>
|
|---|
| 14 | #include <qpopupmenu.h>
|
|---|
| 15 | #include <qpainter.h>
|
|---|
| 16 | #include <stdlib.h>
|
|---|
| 17 |
|
|---|
| 18 | class AnimatedThingy : public QLabel {
|
|---|
| 19 | public:
|
|---|
| 20 | AnimatedThingy( QWidget* parent, const QString& s ) :
|
|---|
| 21 | QLabel(parent),
|
|---|
| 22 | label(s),
|
|---|
| 23 | step(0)
|
|---|
| 24 | {
|
|---|
| 25 | setBackgroundColor(white);
|
|---|
| 26 | label+="\n... and wasting CPU\nwith this animation!\n";
|
|---|
| 27 |
|
|---|
| 28 | for (int i=0; i<nqix; i++)
|
|---|
| 29 | ox[0][i] = oy[0][i] = ox[1][i] = oy[1][i] = 0;
|
|---|
| 30 | x0 = y0 = x1 = y1 = 0;
|
|---|
| 31 | dx0 = rand()%8+2;
|
|---|
| 32 | dy0 = rand()%8+2;
|
|---|
| 33 | dx1 = rand()%8+2;
|
|---|
| 34 | dy1 = rand()%8+2;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | void show()
|
|---|
| 38 | {
|
|---|
| 39 | if (!isVisible()) startTimer(100);
|
|---|
| 40 | QWidget::show();
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | void hide()
|
|---|
| 44 | {
|
|---|
| 45 | QWidget::hide();
|
|---|
| 46 | killTimers();
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | QSize sizeHint() const
|
|---|
| 50 | {
|
|---|
| 51 | return QSize(120,100);
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | protected:
|
|---|
| 55 | void timerEvent(QTimerEvent*)
|
|---|
| 56 | {
|
|---|
| 57 | QPainter p(this);
|
|---|
| 58 | QPen pn=p.pen();
|
|---|
| 59 | pn.setWidth(2);
|
|---|
| 60 | pn.setColor(backgroundColor());
|
|---|
| 61 | p.setPen(pn);
|
|---|
| 62 |
|
|---|
| 63 | step = (step + 1) % nqix;
|
|---|
| 64 |
|
|---|
| 65 | p.drawLine(ox[0][step], oy[0][step], ox[1][step], oy[1][step]);
|
|---|
| 66 |
|
|---|
| 67 | inc(x0, dx0, width());
|
|---|
| 68 | inc(y0, dy0, height());
|
|---|
| 69 | inc(x1, dx1, width());
|
|---|
| 70 | inc(y1, dy1, height());
|
|---|
| 71 | ox[0][step] = x0;
|
|---|
| 72 | oy[0][step] = y0;
|
|---|
| 73 | ox[1][step] = x1;
|
|---|
| 74 | oy[1][step] = y1;
|
|---|
| 75 |
|
|---|
| 76 | QColor c;
|
|---|
| 77 | c.setHsv( (step*255)/nqix, 255, 255 ); // rainbow effect
|
|---|
| 78 | pn.setColor(c);
|
|---|
| 79 | p.setPen(pn);
|
|---|
| 80 | p.drawLine(ox[0][step], oy[0][step], ox[1][step], oy[1][step]);
|
|---|
| 81 | p.setPen(colorGroup().text());
|
|---|
| 82 | p.drawText(rect(), AlignCenter, label);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | void paintEvent(QPaintEvent* event)
|
|---|
| 86 | {
|
|---|
| 87 | QPainter p(this);
|
|---|
| 88 | QPen pn=p.pen();
|
|---|
| 89 | pn.setWidth(2);
|
|---|
| 90 | p.setPen(pn);
|
|---|
| 91 | p.setClipRect(event->rect());
|
|---|
| 92 | for (int i=0; i<nqix; i++) {
|
|---|
| 93 | QColor c;
|
|---|
| 94 | c.setHsv( (i*255)/nqix, 255, 255 ); // rainbow effect
|
|---|
| 95 | pn.setColor(c);
|
|---|
| 96 | p.setPen(pn);
|
|---|
| 97 | p.drawLine(ox[0][i], oy[0][i], ox[1][i], oy[1][i]);
|
|---|
| 98 | }
|
|---|
| 99 | p.setPen(colorGroup().text());
|
|---|
| 100 | p.drawText(rect(), AlignCenter, label);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | private:
|
|---|
| 104 | void inc(int& x, int& dx, int b)
|
|---|
| 105 | {
|
|---|
| 106 | x+=dx;
|
|---|
| 107 | if (x<0) { x=0; dx=rand()%8+2; }
|
|---|
| 108 | else if (x>=b) { x=b-1; dx=-(rand()%8+2); }
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | enum {nqix=10};
|
|---|
| 112 | int ox[2][nqix];
|
|---|
| 113 | int oy[2][nqix];
|
|---|
| 114 | int x0,y0,x1,y1;
|
|---|
| 115 | int dx0,dy0,dx1,dy1;
|
|---|
| 116 | QString label;
|
|---|
| 117 | int step;
|
|---|
| 118 | };
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 | class CPUWaster : public QWidget
|
|---|
| 122 | {
|
|---|
| 123 | Q_OBJECT
|
|---|
| 124 |
|
|---|
| 125 | enum { first_draw_item = 1000, last_draw_item = 1006 };
|
|---|
| 126 |
|
|---|
| 127 | int drawItemRects(int id)
|
|---|
| 128 | {
|
|---|
| 129 | int n = id - first_draw_item;
|
|---|
| 130 | int r = 100;
|
|---|
| 131 | while (n--) r*=(n%3 ? 5 : 4);
|
|---|
| 132 | return r;
|
|---|
| 133 | }
|
|---|
| 134 | QString drawItemText(int id)
|
|---|
| 135 | {
|
|---|
| 136 | QString str;
|
|---|
| 137 | str.sprintf("%d Rectangles", drawItemRects(id));
|
|---|
| 138 | return str;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | public:
|
|---|
| 142 | CPUWaster() :
|
|---|
| 143 | pb(0)
|
|---|
| 144 | {
|
|---|
| 145 | menubar = new QMenuBar( this, "menu" );
|
|---|
| 146 | Q_CHECK_PTR( menubar );
|
|---|
| 147 |
|
|---|
| 148 | QPopupMenu* file = new QPopupMenu();
|
|---|
| 149 | Q_CHECK_PTR( file );
|
|---|
| 150 | menubar->insertItem( "&File", file );
|
|---|
| 151 | for (int i=first_draw_item; i<=last_draw_item; i++)
|
|---|
| 152 | file->insertItem( drawItemText(i), i );
|
|---|
| 153 | connect( menubar, SIGNAL(activated(int)), this, SLOT(doMenuItem(int)) );
|
|---|
| 154 | file->insertSeparator();
|
|---|
| 155 | file->insertItem( "Quit", qApp, SLOT(quit()) );
|
|---|
| 156 |
|
|---|
| 157 | options = new QPopupMenu();
|
|---|
| 158 | Q_CHECK_PTR( options );
|
|---|
| 159 | menubar->insertItem( "&Options", options );
|
|---|
| 160 | td_id = options->insertItem( "Timer driven", this, SLOT(timerDriven()) );
|
|---|
| 161 | ld_id = options->insertItem( "Loop driven", this, SLOT(loopDriven()) );
|
|---|
| 162 | options->insertSeparator();
|
|---|
| 163 | dl_id = options->insertItem( "Default label", this, SLOT(defaultLabel()) );
|
|---|
| 164 | cl_id = options->insertItem( "Custom label", this, SLOT(customLabel()) );
|
|---|
| 165 | options->insertSeparator();
|
|---|
| 166 | md_id = options->insertItem( "No minimum duration", this, SLOT(toggleMinimumDuration()) );
|
|---|
| 167 | options->setCheckable( TRUE );
|
|---|
| 168 | loopDriven();
|
|---|
| 169 | defaultLabel();
|
|---|
| 170 |
|
|---|
| 171 | setFixedSize( 400, 300 );
|
|---|
| 172 |
|
|---|
| 173 | setBackgroundColor( black );
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | public slots:
|
|---|
| 177 | void doMenuItem(int id)
|
|---|
| 178 | {
|
|---|
| 179 | if (id >= first_draw_item && id <= last_draw_item)
|
|---|
| 180 | draw(drawItemRects(id));
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | void stopDrawing() { got_stop = TRUE; }
|
|---|
| 184 |
|
|---|
| 185 | void timerDriven()
|
|---|
| 186 | {
|
|---|
| 187 | timer_driven = TRUE;
|
|---|
| 188 | options->setItemChecked( td_id, TRUE );
|
|---|
| 189 | options->setItemChecked( ld_id, FALSE );
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | void loopDriven()
|
|---|
| 193 | {
|
|---|
| 194 | timer_driven = FALSE;
|
|---|
| 195 | options->setItemChecked( ld_id, TRUE );
|
|---|
| 196 | options->setItemChecked( td_id, FALSE );
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | void defaultLabel()
|
|---|
| 200 | {
|
|---|
| 201 | default_label = TRUE;
|
|---|
| 202 | options->setItemChecked( dl_id, TRUE );
|
|---|
| 203 | options->setItemChecked( cl_id, FALSE );
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | void customLabel()
|
|---|
| 207 | {
|
|---|
| 208 | default_label = FALSE;
|
|---|
| 209 | options->setItemChecked( dl_id, FALSE );
|
|---|
| 210 | options->setItemChecked( cl_id, TRUE );
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | void toggleMinimumDuration()
|
|---|
| 214 | {
|
|---|
| 215 | options->setItemChecked( md_id,
|
|---|
| 216 | !options->isItemChecked( md_id ) );
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | private:
|
|---|
| 220 | void timerEvent( QTimerEvent* )
|
|---|
| 221 | {
|
|---|
| 222 | if (!got_stop)
|
|---|
| 223 | pb->setProgress( pb->totalSteps() - rects );
|
|---|
| 224 | rects--;
|
|---|
| 225 |
|
|---|
| 226 | {
|
|---|
| 227 | QPainter p(this);
|
|---|
| 228 |
|
|---|
| 229 | int ww = width();
|
|---|
| 230 | int wh = height();
|
|---|
| 231 |
|
|---|
| 232 | if ( ww > 8 && wh > 8 ) {
|
|---|
| 233 | QColor c(rand()%255, rand()%255, rand()%255);
|
|---|
| 234 | int x = rand() % (ww-8);
|
|---|
| 235 | int y = rand() % (wh-8);
|
|---|
| 236 | int w = rand() % (ww-x);
|
|---|
| 237 | int h = rand() % (wh-y);
|
|---|
| 238 | p.fillRect( x, y, w, h, c );
|
|---|
| 239 | }
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | if (!rects || got_stop) {
|
|---|
| 243 | if (!got_stop)
|
|---|
| 244 | pb->setProgress( pb->totalSteps() );
|
|---|
| 245 | QPainter p(this);
|
|---|
| 246 | p.fillRect(0, 0, width(), height(), backgroundColor());
|
|---|
| 247 | enableDrawingItems(TRUE);
|
|---|
| 248 | killTimers();
|
|---|
| 249 | delete pb;
|
|---|
| 250 | pb = 0;
|
|---|
| 251 | }
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | QProgressDialog* newProgressDialog( const char* label, int steps, bool modal )
|
|---|
| 255 | {
|
|---|
| 256 | QProgressDialog *d = new QProgressDialog(label, "Cancel", steps, this,
|
|---|
| 257 | "progress", modal);
|
|---|
| 258 | if ( options->isItemChecked( md_id ) )
|
|---|
| 259 | d->setMinimumDuration(0);
|
|---|
| 260 | if ( !default_label )
|
|---|
| 261 | d->setLabel( new AnimatedThingy(d,label) );
|
|---|
| 262 | return d;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | void enableDrawingItems(bool yes)
|
|---|
| 266 | {
|
|---|
| 267 | for (int i=first_draw_item; i<=last_draw_item; i++) {
|
|---|
| 268 | menubar->setItemEnabled(i, yes);
|
|---|
| 269 | }
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | void draw(int n)
|
|---|
| 273 | {
|
|---|
| 274 | if ( timer_driven ) {
|
|---|
| 275 | if ( pb ) {
|
|---|
| 276 | qWarning("This cannot happen!");
|
|---|
| 277 | return;
|
|---|
| 278 | }
|
|---|
| 279 | rects = n;
|
|---|
| 280 | pb = newProgressDialog("Drawing rectangles.\n"
|
|---|
| 281 | "Using timer event.", n, FALSE);
|
|---|
| 282 | pb->setCaption("Please Wait");
|
|---|
| 283 | connect(pb, SIGNAL(cancelled()), this, SLOT(stopDrawing()));
|
|---|
| 284 | enableDrawingItems(FALSE);
|
|---|
| 285 | startTimer(0);
|
|---|
| 286 | got_stop = FALSE;
|
|---|
| 287 | } else {
|
|---|
| 288 | QProgressDialog* lpb = newProgressDialog(
|
|---|
| 289 | "Drawing rectangles.\nUsing loop.", n, TRUE);
|
|---|
| 290 | lpb->setCaption("Please Wait");
|
|---|
| 291 |
|
|---|
| 292 | QPainter p(this);
|
|---|
| 293 | for (int i=0; i<n; i++) {
|
|---|
| 294 | lpb->setProgress(i);
|
|---|
| 295 | if ( lpb->wasCancelled() )
|
|---|
| 296 | break;
|
|---|
| 297 |
|
|---|
| 298 | QColor c(rand()%255, rand()%255, rand()%255);
|
|---|
| 299 | int x = rand()%(width()-8);
|
|---|
| 300 | int y = rand()%(height()-8);
|
|---|
| 301 | int w = rand()%(width()-x);
|
|---|
| 302 | int h = rand()%(height()-y);
|
|---|
| 303 | p.fillRect(x,y,w,h,c);
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | p.fillRect(0, 0, width(), height(), backgroundColor());
|
|---|
| 307 |
|
|---|
| 308 | delete lpb;
|
|---|
| 309 | }
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | QMenuBar* menubar;
|
|---|
| 313 | QProgressDialog* pb;
|
|---|
| 314 | QPopupMenu* options;
|
|---|
| 315 | int td_id, ld_id;
|
|---|
| 316 | int dl_id, cl_id;
|
|---|
| 317 | int md_id;
|
|---|
| 318 | int rects;
|
|---|
| 319 | bool timer_driven;
|
|---|
| 320 | bool default_label;
|
|---|
| 321 | bool got_stop;
|
|---|
| 322 | };
|
|---|
| 323 |
|
|---|
| 324 | int main( int argc, char **argv )
|
|---|
| 325 | {
|
|---|
| 326 | QApplication a( argc, argv );
|
|---|
| 327 |
|
|---|
| 328 | int wincount = argc > 1 ? atoi(argv[1]) : 1;
|
|---|
| 329 |
|
|---|
| 330 | for ( int i=0; i<wincount; i++ ) {
|
|---|
| 331 | CPUWaster* cpuw = new CPUWaster;
|
|---|
| 332 | if ( i == 0 ) a.setMainWidget(cpuw);
|
|---|
| 333 | cpuw->show();
|
|---|
| 334 | }
|
|---|
| 335 | return a.exec();
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | #include "progress.moc"
|
|---|