#include "customprogressindicator.h" #include #include 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); } void CustomProgressIndicator::timerEvent(QTimerEvent * /*event*/) { angle_ = (angle_+30)%360; update(); } void CustomProgressIndicator::paintEvent(QPaintEvent * /*event*/) { QPainter p(this); // if(m_iFlagFinishedSuccess==1) // { // drawUnderCircle(&p); // } // else { drawJuHua(&p); } } 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(); } } //没有使用的画圆环函数 void CustomProgressIndicator::drawUnderCircle(QPainter *painter) { int bigradius = 50; painter->save(); painter->setPen(color_); painter->setBrush(color_); QPainterPath bigCircle; bigCircle.addEllipse(-width()/2, -height()/2, bigradius*2, bigradius*2); int smallradius = 40; QPainterPath smallCircle; smallCircle.addEllipse(-width()/2, -height()/2, smallradius*2, smallradius*2); QPainterPath path = bigCircle - smallCircle; painter->drawPath(path); painter->setPen(color_); painter->drawText(rect(), Qt::AlignCenter, m_strTip); painter->restore(); /* painter->save(); painter->setPen(color_); painter->setBrush(color_); int m_outerRadius=width()>height()?(qreal)height()/2-4:(qreal)width()/2-4;//求最小的那个值。自己可以测试一下减4个像素与不减的区别 QPointF TopLeft(rect().center().x() - m_outerRadius,rect().center().y() - m_outerRadius); QPointF BottomRight(rect().center().x() + m_outerRadius,rect().center().y() + m_outerRadius); QRectF CircleRect(TopLeft,BottomRight);//大圆矩形 //painter->setPen(Qt::NoPen);//大家可以注释掉这行看一下,有什么差别? painter->drawEllipse(CircleRect);//画椭圆,其实就是画圆。特殊椭圆而已 painter->setPen(Qt::white); painter->drawText(rect(), Qt::AlignCenter, m_strTip); painter->restore();*/ }