summaryrefslogtreecommitdiff
path: root/noncore/unsupported/qpdf/qbusybar.cpp
blob: e942f06b38a2edf95542842003108d91a6ee41cd (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
#include <qapplication.h>
#include <qtimer.h>
#include <qpainter.h>

#include "qbusybar.h"



QBusyBar::QBusyBar ( QWidget *parent, const char *name, int flags ) : QWidget ( parent, name, flags | WRepaintNoErase )
{
	m_busy = 0;

	m_div = 0;
	m_pos = 0;
	m_fade = 0;
	m_fadecols = 0;
	m_speed = 500;
	
	m_timer = new QTimer ( this );	
	connect ( m_timer, SIGNAL( timeout()), this, SLOT( slotTimeout()));	

	setParameters ( 12, 8, 200 );
}

void QBusyBar::setParameters ( int d, int s, int v )	
{	
	bool running = m_timer-> isActive ( );
	
	if ( running )
		m_timer-> stop ( );

	m_div = d;
	m_speed = v;

	delete [] m_fadecols;
	m_fade = s;
	m_fadecols = new QColor [m_fade];

	int rt, gt, bt;
	int rf, gf, bf;

	colorGroup ( ). color ( QColorGroup::Highlight ). rgb ( &rf, &gf, &bf );
	colorGroup ( ). color ( QColorGroup::Background ). rgb ( &rt, &gt, &bt );
	
	for ( int i = 0; i < s; i++ ) 
		m_fadecols [i]. setRgb ( rf + ( rt - rf ) * i / s, gf + ( gt - gf ) * i / s, bf + ( bt - bf ) * i / s );
		
	if ( running ) {
		m_pos = 0;
		m_timer-> start ( m_speed );
	}
}

QBusyBar::~QBusyBar ( )
{
	delete [] m_fadecols;
}

bool QBusyBar::isBusy ( ) const
{
	return m_busy;
}

void QBusyBar::beginBusy ( )
{
	setBusy ( true );
}

void QBusyBar::endBusy ( )
{
	setBusy ( false );
}

void QBusyBar::setBusy ( bool b )
{
	int busy = m_busy + ( b ? 1 : -1 );
	
	if ( busy < 0 )
		busy = 0;
	
	if (( m_busy == 0 ) && ( busy > 0 )) { // Changed state	to on
		m_pos = 0;
		m_timer-> start ( m_speed );
		update ( );		
	}
	else if (( m_busy > 0 ) && ( busy == 0 )) { // Changed state to off
		m_timer-> stop ( );
		update ( );	
	}
	
	m_busy = busy;
}

void QBusyBar::slotTimeout ( )
{
	m_pos++;
	m_pos %= ( 2 * ( m_fade + m_div ));

	update ( );
}

void QBusyBar::paintEvent ( QPaintEvent *e )
{
	QPainter p ( this );

	QRect clip = e-> rect ( );

	int x  = 0;
	int dx = width ( ) / m_div;
	int y  = clip. top ( ); 
	int dy = clip. height ( );

	if ( m_busy ) {
		int dir = ( m_pos < ( m_fade + m_div )) ? 1 : -1;
		int pos = ( dir > 0 ) ? m_pos : ( 2 * ( m_div + m_fade )) - m_pos - m_fade - 1;

		for ( int i = 0; i < m_div; i++ ) {
			int ind = ( pos - i ) * dir;
			if (( ind < 0 ) || ( ind >= m_fade ))
				ind = m_fade - 1;
			
			if ((( x + dx ) > clip. left ( )) || ( x < clip. right ( )))
				p. fillRect ( x, y, ( i < ( m_div - 1 )) ? dx : width ( ) - x, dy, m_fadecols [ind] );	
			x += dx;
		}
	}
	else {
		p. fillRect ( e-> rect ( ), m_fadecols [m_fade - 1] );
	}		
}