File indexing completed on 2024-04-21 14:44:21

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 #pragma once
0011 
0012 #include <QWidget>
0013 #include <QColor>
0014 
0015 /**
0016  * @class QProgressIndicator
0017  * @brief The QProgressIndicator class lets an application display a progress indicator to show that a long task is
0018  * under way.
0019  * Progress indicators are indeterminate and do nothing more than spin to show that the application is busy.
0020  *
0021  * @sa QProgressBar
0022  */
0023 class QProgressIndicator : public QWidget
0024 {
0025     Q_OBJECT
0026     Q_PROPERTY(int delay READ animationDelay WRITE setAnimationDelay)
0027     Q_PROPERTY(bool displayedWhenStopped READ isDisplayedWhenStopped WRITE setDisplayedWhenStopped)
0028     Q_PROPERTY(QColor color READ color WRITE setColor)
0029   public:
0030     explicit QProgressIndicator(QWidget *parent = nullptr);
0031 
0032     /**
0033      * Returns the delay between animation steps.
0034      * @return The number of milliseconds between animation steps. By default, the animation delay is set to
0035      * 40 milliseconds.
0036      *
0037      * @sa setAnimationDelay
0038      */
0039     int animationDelay() const { return m_delay; }
0040 
0041     /**
0042      * Returns a Boolean value indicating whether the component is currently animated.
0043      * @return Animation state.
0044      * @sa startAnimation stopAnimation
0045      */
0046     bool isAnimated() const;
0047 
0048     /**
0049      * Returns a Boolean value indicating whether the receiver shows itself even when it is not animating.
0050      * @return Return true if the progress indicator shows itself even when it is not animating. By default,
0051      * it returns false.
0052      * @sa setDisplayedWhenStopped
0053      */
0054     bool isDisplayedWhenStopped() const;
0055 
0056     /**
0057      * Returns the color of the component.
0058      * @sa setColor
0059      */
0060     const QColor &color() const { return m_color; }
0061 
0062     QSize sizeHint() const override;
0063     int heightForWidth(int w) const override;
0064   public slots:
0065     /**
0066      * Starts the spin animation.
0067      * @sa stopAnimation isAnimated
0068      */
0069     void startAnimation();
0070 
0071     /**
0072      * Stops the spin animation.
0073      * @sa startAnimation isAnimated
0074      */
0075     void stopAnimation();
0076 
0077     /**
0078      * Sets the delay between animation steps.
0079      * Setting the \a delay to a value larger than 40 slows the animation, while setting the @a delay to a
0080      * smaller value speeds it up.
0081      * @param delay The delay, in milliseconds.
0082      * @sa animationDelay
0083      */
0084     void setAnimationDelay(int delay);
0085 
0086     /**
0087      * Sets whether the component hides itself when it is not animating.
0088      * @param state The animation state. Set false to hide the progress indicator when it is not animating; otherwise true.
0089      * @sa isDisplayedWhenStopped
0090      */
0091     void setDisplayedWhenStopped(bool state);
0092 
0093     /**
0094      * Sets the color of the components to the given color.
0095      * @sa color
0096      */
0097     void setColor(const QColor &color);
0098 
0099   protected:
0100     void timerEvent(QTimerEvent *event) override;
0101     void paintEvent(QPaintEvent *event) override;
0102 
0103   private:
0104     int m_angle { 0 };
0105     int m_timerId { -1 };
0106     int m_delay { 40 };
0107     bool m_displayedWhenStopped { false };
0108     QColor m_color { Qt::black };
0109 };