home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / progressbar / progressbar.cpp < prev    next >
C/C++ Source or Header  |  2001-10-11  |  5KB  |  167 lines

  1. /****************************************************************************
  2. ** $Id:  qt/progressbar.cpp   3.0.0   edited Jun 22 13:24 $
  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 "progressbar.h"
  12.  
  13. #include <qradiobutton.h>
  14. #include <qpushbutton.h>
  15. #include <qprogressbar.h>
  16. #include <qlayout.h>
  17.  
  18. #include <qmotifstyle.h>
  19.  
  20. /*
  21.  * Constructor
  22.  *
  23.  * Creates child widgets of the ProgressBar widget
  24.  */
  25.  
  26. ProgressBar::ProgressBar( QWidget *parent, const char *name )
  27.     : QButtonGroup( 0, Horizontal, "Progress Bar", parent, name ), timer()
  28. {
  29.     setMargin( 10 );
  30.  
  31.     QGridLayout* toplayout = new QGridLayout( layout(), 2, 2, 5);
  32.  
  33.     setRadioButtonExclusive( TRUE );
  34.  
  35.     // insert three radiobuttons which the user can use
  36.     // to set the speed of the progress and two pushbuttons
  37.     // to start/pause/continue and reset the progress
  38.     slow = new QRadioButton( "&Slow", this );
  39.     normal = new QRadioButton( "&Normal", this );
  40.     fast = new QRadioButton( "&Fast", this );
  41.     QVBoxLayout* vb1 = new QVBoxLayout;
  42.     toplayout->addLayout( vb1, 0, 0 );
  43.     vb1->addWidget( slow );
  44.     vb1->addWidget( normal );
  45.     vb1->addWidget( fast );
  46.  
  47.     // two push buttons, one for start, for for reset.
  48.     start = new QPushButton( "&Start", this );
  49.     reset = new QPushButton( "&Reset", this );
  50.     QVBoxLayout* vb2 = new QVBoxLayout;
  51.     toplayout->addLayout( vb2, 0, 1 );
  52.     vb2->addWidget( start );
  53.     vb2->addWidget( reset );
  54.  
  55.     // Create the progressbar
  56.     progress = new QProgressBar( 100, this );
  57.     //    progress->setStyle( new QMotifStyle() );
  58.     toplayout->addMultiCellWidget( progress, 1, 1, 0, 1 );
  59.  
  60.     // connect the clicked() SIGNALs of the pushbuttons to SLOTs
  61.     connect( start, SIGNAL( clicked() ), this, SLOT( slotStart() ) );
  62.     connect( reset, SIGNAL( clicked() ), this, SLOT( slotReset() ) );
  63.  
  64.     // connect the timeout() SIGNAL of the progress-timer to a SLOT
  65.     connect( &timer, SIGNAL( timeout() ), this, SLOT( slotTimeout() ) );
  66.  
  67.     // Let's start with normal speed...
  68.     normal->setChecked( TRUE );
  69.  
  70.  
  71.     // some contraints
  72.     start->setFixedWidth( 80 );
  73.     setMinimumWidth( 300 );
  74. }
  75.  
  76. /*
  77.  * SLOT slotStart
  78.  *
  79.  * This SLOT is called if the user clicks start/pause/continue
  80.  * button
  81.  */
  82.  
  83. void ProgressBar::slotStart()
  84. {
  85.     // If the progress bar is at the beginning...
  86.     if ( progress->progress() == -1 ) {
  87.         // ...set according to the checked speed-radiobutton
  88.         // the number of steps which are needed to complete the process
  89.         if ( slow->isChecked() )
  90.             progress->setTotalSteps( 10000 );
  91.         else if ( normal->isChecked() )
  92.             progress->setTotalSteps( 1000 );
  93.         else
  94.             progress->setTotalSteps( 50 );
  95.  
  96.         // disable the speed-radiobuttons
  97.         slow->setEnabled( FALSE );
  98.         normal->setEnabled( FALSE );
  99.         fast->setEnabled( FALSE );
  100.     }
  101.  
  102.     // If the progress is not running...
  103.     if ( !timer.isActive() ) {
  104.         // ...start the timer (and so the progress) with a interval of 1 ms...
  105.         timer.start( 1 );
  106.         // ...and rename the start/pause/continue button to Pause
  107.         start->setText( "&Pause" );
  108.     } else { // if the prgress is running...
  109.         // ...stop the timer (and so the prgress)...
  110.         timer.stop();
  111.         // ...and rename the start/pause/continue button to Continue
  112.         start->setText( "&Continue" );
  113.     }
  114. }
  115.  
  116. /*
  117.  * SLOT slotReset
  118.  *
  119.  * This SLOT is called when the user clicks the reset button
  120.  */
  121.  
  122. void ProgressBar::slotReset()
  123. {
  124.     // stop the timer and progress
  125.     timer.stop();
  126.  
  127.     // rename the start/pause/continue button to Start...
  128.     start->setText( "&Start" );
  129.     // ...and enable this button
  130.     start->setEnabled( TRUE );
  131.  
  132.     // enable the speed-radiobuttons
  133.     slow->setEnabled( TRUE );
  134.     normal->setEnabled( TRUE );
  135.     fast->setEnabled( TRUE );
  136.  
  137.     // reset the progressbar
  138.     progress->reset();
  139. }
  140.  
  141. /*
  142.  * SLOT slotTimeout
  143.  *
  144.  * This SLOT is called each ms when the timer is
  145.  * active (== progress is running)
  146.  */
  147.  
  148. void ProgressBar::slotTimeout()
  149. {
  150.     int p = progress->progress();
  151.  
  152. #if 1
  153.     // If the progress is complete...
  154.     if ( p == progress->totalSteps() )  {
  155.         // ...rename the start/pause/continue button to Start...
  156.         start->setText( "&Start" );
  157.         // ...and disable it...
  158.         start->setEnabled( FALSE );
  159.         // ...and return
  160.         return;
  161.     }
  162. #endif
  163.  
  164.     // If the process is not complete increase it
  165.     progress->setProgress( ++p );
  166. }
  167.