00001 #include <qapplication.h>
00002 #include <qtimer.h>
00003 #include <qpainter.h>
00004
00005 #include "qbusybar.h"
00006
00007
00008
00009 QBusyBar::QBusyBar ( QWidget *parent, const char *name, int flags ) : QWidget ( parent, name, flags | WRepaintNoErase )
00010 {
00011 m_busy = 0;
00012
00013 m_div = 0;
00014 m_pos = 0;
00015 m_fade = 0;
00016 m_fadecols = 0;
00017 m_speed = 500;
00018
00019 m_timer = new QTimer ( this );
00020 connect ( m_timer, SIGNAL( timeout()), this, SLOT( slotTimeout()));
00021
00022 setParameters ( 12, 8, 200 );
00023 }
00024
00025 void QBusyBar::setParameters ( int d, int s, int v )
00026 {
00027 bool running = m_timer-> isActive ( );
00028
00029 if ( running )
00030 m_timer-> stop ( );
00031
00032 m_div = d;
00033 m_speed = v;
00034
00035 delete [] m_fadecols;
00036 m_fade = s;
00037 m_fadecols = new QColor [m_fade];
00038
00039 int rt, gt, bt;
00040 int rf, gf, bf;
00041
00042 colorGroup ( ). color ( QColorGroup::Highlight ). rgb ( &rf, &gf, &bf );
00043 colorGroup ( ). color ( QColorGroup::Background ). rgb ( &rt, >, &bt );
00044
00045 for ( int i = 0; i < s; i++ )
00046 m_fadecols [i]. setRgb ( rf + ( rt - rf ) * i / s, gf + ( gt - gf ) * i / s, bf + ( bt - bf ) * i / s );
00047
00048 if ( running ) {
00049 m_pos = 0;
00050 m_timer-> start ( m_speed );
00051 }
00052 }
00053
00054 QBusyBar::~QBusyBar ( )
00055 {
00056 delete [] m_fadecols;
00057 }
00058
00059 bool QBusyBar::isBusy ( ) const
00060 {
00061 return m_busy;
00062 }
00063
00064 void QBusyBar::beginBusy ( )
00065 {
00066 setBusy ( true );
00067 }
00068
00069 void QBusyBar::endBusy ( )
00070 {
00071 setBusy ( false );
00072 }
00073
00074 void QBusyBar::setBusy ( bool b )
00075 {
00076 int busy = m_busy + ( b ? 1 : -1 );
00077
00078 if ( busy < 0 )
00079 busy = 0;
00080
00081 if (( m_busy == 0 ) && ( busy > 0 )) {
00082 m_pos = 0;
00083 m_timer-> start ( m_speed );
00084 update ( );
00085 }
00086 else if (( m_busy > 0 ) && ( busy == 0 )) {
00087 m_timer-> stop ( );
00088 update ( );
00089 }
00090
00091 m_busy = busy;
00092 }
00093
00094 void QBusyBar::slotTimeout ( )
00095 {
00096 m_pos++;
00097 m_pos %= ( 2 * ( m_fade + m_div ));
00098
00099 update ( );
00100 }
00101
00102 void QBusyBar::paintEvent ( QPaintEvent *e )
00103 {
00104 QPainter p ( this );
00105
00106 QRect clip = e-> rect ( );
00107
00108 int x = 0;
00109 int dx = width ( ) / m_div;
00110 int y = clip. top ( );
00111 int dy = clip. height ( );
00112
00113 if ( m_busy ) {
00114 int dir = ( m_pos < ( m_fade + m_div )) ? 1 : -1;
00115 int pos = ( dir > 0 ) ? m_pos : ( 2 * ( m_div + m_fade )) - m_pos - m_fade - 1;
00116
00117 for ( int i = 0; i < m_div; i++ ) {
00118 int ind = ( pos - i ) * dir;
00119 if (( ind < 0 ) || ( ind >= m_fade ))
00120 ind = m_fade - 1;
00121
00122 if ((( x + dx ) > clip. left ( )) || ( x < clip. right ( )))
00123 p. fillRect ( x, y, ( i < ( m_div - 1 )) ? dx : width ( ) - x, dy, m_fadecols [ind] );
00124 x += dx;
00125 }
00126 }
00127 else {
00128 p. fillRect ( e-> rect ( ), m_fadecols [m_fade - 1] );
00129 }
00130 }
00131
00132
00133