qt/LedOK/LoUIClass/customprogressindicator.cpp

138 lines
3.5 KiB
C++
Raw Normal View History

2022-01-04 18:11:48 +08:00
#include "customprogressindicator.h"
#include <QPainter>
#include <QPainterPath>
CustomProgressIndicator::CustomProgressIndicator(QWidget* parent)
: QWidget(parent),
angle_(0),
timerId_(-1),
delay_(20),
displayedWhenStopped_(false),
color_(Qt::green) {
setAttribute(Qt::WA_DeleteOnClose);
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
setFocusPolicy(Qt::NoFocus);
setWindowFlags(Qt::FramelessWindowHint);//无窗体
setAttribute(Qt::WA_TranslucentBackground);//背景透明
}
bool CustomProgressIndicator::isAnimated () const {
return (timerId_ != -1);
}
void CustomProgressIndicator::setDisplayedWhenStopped(bool state) {
displayedWhenStopped_ = state;
update();
}
bool CustomProgressIndicator::isDisplayedWhenStopped() const {
return displayedWhenStopped_;
}
void CustomProgressIndicator::setDisplayModel(int iFlag)
{
m_iLoopBackFlag=iFlag;
}
void CustomProgressIndicator::startAnimation() {
angle_ = 0;
if (timerId_ == -1) {
timerId_ = startTimer(delay_);
}
}
void CustomProgressIndicator::setDisplayStringInfo(QString strTip,QString strTiping) {
m_strTip=strTip;
m_strTiping=strTiping;
update();
}
void CustomProgressIndicator::stopAnimation() {
if (timerId_ != -1) {
killTimer(timerId_);
}
timerId_ = -1;
update();
}
void CustomProgressIndicator::setAnimationDelay(int delay) {
if (timerId_ != -1){
killTimer(timerId_);
}
delay_ = delay;
if (timerId_ != -1){
timerId_ = startTimer(delay_);
}
}
void CustomProgressIndicator::setColor(const QColor & color) {
color_ = color;
update();
}
QSize CustomProgressIndicator::sizeHint() const {
return QSize(80,80);
}
2022-08-25 18:37:24 +08:00
void CustomProgressIndicator::timerEvent(QTimerEvent *) {
2022-01-04 18:11:48 +08:00
angle_ = (angle_+30)%360;
update();
}
2022-08-25 18:37:24 +08:00
void CustomProgressIndicator::paintEvent(QPaintEvent *) {
2022-01-04 18:11:48 +08:00
QPainter p(this);
2022-08-25 18:37:24 +08:00
drawJuHua(&p);
2022-01-04 18:11:48 +08:00
}
void CustomProgressIndicator::drawJuHua(QPainter *painter)
{
painter->setRenderHint(QPainter::Antialiasing);
if (displayedWhenStopped_ && !isAnimated()) //如果displayedWhenStopped_==flash并且动画已经停止则不绘制
{
painter->setPen(color_);
painter->drawPixmap(rect(),currentPix_);
painter->drawText(rect(), Qt::AlignCenter, m_strTip);
return;
}
int width = qMin(this->width(), this->height());
int outerRadius = (width-1) >> 1;
int innerRadius = int(((width-1) >> 1)*0.38);
int capsuleHeight = outerRadius - innerRadius;
int capsuleWidth = (width > 32 ) ? (int)(capsuleHeight *0.23) : (int)(capsuleHeight *0.35);
int capsuleRadius = capsuleWidth >> 1;
if(m_iLoopBackFlag==1)
{
painter->setPen(color_);
painter->drawText(rect(), Qt::AlignCenter, m_strTiping);
}
else {
/* 撰写进度 */
if (progress_ > 0 && progress_ < 100) {
painter->setPen(color_);
painter->drawText(rect(), Qt::AlignCenter, QString("%1%").arg(progress_));
}
else if (progress_ == 100) {
stopAnimation();
}
}
for (int i=0; i<12; ++i) {
QColor color = color_;
color.setAlphaF(1.0f - (i/12.0f));
painter->setPen(Qt::NoPen);
painter->setBrush(color);
painter->save();
painter->translate(rect().center());
painter->rotate(angle_ - i*30.0f);
painter->drawRoundedRect(((-capsuleWidth) >> 1), -(innerRadius+capsuleHeight), capsuleWidth, capsuleHeight, capsuleRadius, capsuleRadius);
painter->restore();
}
}