#include "progress_indicator.h"
QProgressIndicator::QProgressIndicator(QWidget* parent)
: QWidget(parent)
{
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
setFocusPolicy(Qt::NoFocus);
angle = 0,
timer_id = -1,
delay_time = 40,
is_display_when_stop = false,
color = Qt::black;
}
bool QProgressIndicator::isAnimated () const
{
return (timer_id != -1);
}
int QProgressIndicator::animationDelay() const
{
return delay_time;
}
void QProgressIndicator::setColor(const QColor &color)
{
this->color = color;
update();
}
QColor & QProgressIndicator::getColor()
{
return color;
}
void QProgressIndicator::setDisplayWhenStop(bool is_display_when_stop)
{
this->is_display_when_stop = is_display_when_stop;
update();
}
bool QProgressIndicator::isDisplayWhenStop() const
{
return is_display_when_stop;
}
void QProgressIndicator::startAnimation()
{
angle = 0;
if(timer_id == -1)
{
timer_id = startTimer(delay_time);
}
}
void QProgressIndicator::stopAnimation()
{
if(timer_id != -1)
{
killTimer(timer_id);
}
timer_id = -1;
update();
}
void QProgressIndicator::setAnimationDelay(int delay_time)
{
if(timer_id != -1)
{
killTimer(timer_id);
}
this->delay_time = delay_time;
if(timer_id != -1)
{
timer_id = startTimer(delay_time);
}
}
void QProgressIndicator::timerEvent(QTimerEvent *)
{
angle = (angle + 30) % 360;
update();
}
void QProgressIndicator::paintEvent(QPaintEvent *)
{
if(!is_display_when_stop && !isAnimated())
return;
int width = qMin(this->width(), this->height());
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
int outer_radius = (width - 1) * 0.5;
int inner_radius = (width - 1) * 0.5 * 0.4;
int capsule_height = outer_radius - inner_radius;
int capsule_width = (width > 32 ) ? capsule_height *.30 : capsule_height *.35;
int capsule_radius = capsule_width/2;
for(int i=0; i<12; i++)
{
QColor color = color;
color.setAlphaF(1.0f - (i/12.0f));
p.setPen(Qt::NoPen);
p.setBrush(color);
p.save();
p.translate(rect().center());
p.rotate(angle - i*30.0f);
p.drawRoundedRect(-capsule_width*0.5, -(inner_radius+capsule_height), capsule_width, capsule_height, capsule_radius, capsule_radius);
p.restore();
}
}
效果图: