File indexing completed on 2024-04-21 03:42:34

0001 /*
0002 
0003     This file is part of QProgressIndicator,
0004     an open-source recent files menu widget
0005 
0006     SPDX-FileCopyrightText: 2009-2010 Morgan Leborgne
0007     SPDX-License-Identifier: LGPL-3.0-or-later
0008 */
0009 
0010 #include "QProgressIndicator.h"
0011 
0012 #include <QPainter>
0013 
0014 QProgressIndicator::QProgressIndicator(QWidget *parent)
0015     : QWidget(parent)
0016 {
0017     setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
0018     setFocusPolicy(Qt::NoFocus);
0019 }
0020 
0021 bool QProgressIndicator::isAnimated() const
0022 {
0023     return (m_timerId != -1);
0024 }
0025 
0026 void QProgressIndicator::setDisplayedWhenStopped(bool state)
0027 {
0028     m_displayedWhenStopped = state;
0029 
0030     update();
0031 }
0032 
0033 bool QProgressIndicator::isDisplayedWhenStopped() const
0034 {
0035     return m_displayedWhenStopped;
0036 }
0037 
0038 void QProgressIndicator::startAnimation()
0039 {
0040     m_angle = 0;
0041 
0042     if (m_timerId == -1)
0043         m_timerId = startTimer(m_delay);
0044 }
0045 
0046 void QProgressIndicator::stopAnimation()
0047 {
0048     if (m_timerId != -1)
0049         killTimer(m_timerId);
0050 
0051     m_timerId = -1;
0052 
0053     update();
0054 }
0055 
0056 void QProgressIndicator::setAnimationDelay(int delay)
0057 {
0058     if (m_timerId != -1)
0059         killTimer(m_timerId);
0060 
0061     m_delay = delay;
0062 
0063     if (m_timerId != -1)
0064         m_timerId = startTimer(m_delay);
0065 }
0066 
0067 void QProgressIndicator::setColor(const QColor &color)
0068 {
0069     m_color = color;
0070 
0071     update();
0072 }
0073 
0074 QSize QProgressIndicator::sizeHint() const
0075 {
0076     return QSize(20, 20);
0077 }
0078 
0079 int QProgressIndicator::heightForWidth(int w) const
0080 {
0081     return w;
0082 }
0083 
0084 void QProgressIndicator::timerEvent(QTimerEvent * /*event*/)
0085 {
0086     m_angle = (m_angle + 30) % 360;
0087 
0088     update();
0089 }
0090 
0091 void QProgressIndicator::paintEvent(QPaintEvent * /*event*/)
0092 {
0093     if (!m_displayedWhenStopped && !isAnimated())
0094         return;
0095 
0096     int width = qMin(this->width(), this->height());
0097 
0098     QPainter p(this);
0099     p.setRenderHint(QPainter::Antialiasing);
0100 
0101     int outerRadius = (width - 1) * 0.5;
0102     int innerRadius = (width - 1) * 0.5 * 0.38;
0103 
0104     int capsuleHeight = outerRadius - innerRadius;
0105     int capsuleWidth  = (width > 32) ? capsuleHeight * .23 : capsuleHeight * .35;
0106     int capsuleRadius = capsuleWidth / 2;
0107 
0108     for (int i = 0; i < 12; i++)
0109     {
0110         QColor color = m_color;
0111         color.setAlphaF(1.0f - (i / 12.0f));
0112         p.setPen(Qt::NoPen);
0113         p.setBrush(color);
0114         p.save();
0115         p.translate(rect().center());
0116         p.rotate(m_angle - i * 30.0f);
0117         p.drawRoundedRect(-capsuleWidth * 0.5, -(innerRadius + capsuleHeight), capsuleWidth, capsuleHeight,
0118                           capsuleRadius, capsuleRadius);
0119         p.restore();
0120     }
0121 }