File indexing completed on 2024-06-23 05:20:55

0001 /* Copyright (C) 2013 Thomas Lübking <thomas.luebking@gmail.com>
0002 
0003    This file is part of the Trojita Qt IMAP e-mail client,
0004    http://trojita.flaska.net/
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License or (at your option) version 3 or any later version
0010    accepted by the membership of KDE e.V. (or its successor approved
0011    by the membership of KDE e.V.), which shall act as a proxy
0012    defined in Section 14 of version 3 of the license.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    You should have received a copy of the GNU General Public License
0020    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #ifndef SPINNER_H
0024 #define SPINNER_H
0025 
0026 class QTimer;
0027 #include <QWidget>
0028 
0029 namespace Gui {
0030 /**
0031  * @short A generic Spinner, ie an overlay displaying some rotating stuff to indicate:
0032  * Hold on, slow action in process...
0033  * The Spinner fades in and out and automatically positions itself in the visible area of its
0034  * parent with 2/3 of the smaller dimension of the visible parent rect.
0035  */
0036 class Spinner : public QWidget {
0037     Q_OBJECT
0038 public:
0039     enum Type { Aperture = 0, Scythe, Elastic, Sun };
0040     enum Context { Overlay = 0, Throbber };
0041     /** Providing a @p parent that is NOT Null is mandatory! */
0042     explicit Spinner(QWidget *parent);
0043     /**
0044      * Set the context. Throbber suits the needs of a small icon with opaque foreground.
0045      * You might recall that browsers used to have such when loading webpages took minutes.
0046      * Throbbers have the optional text as tooltip.
0047      * The default context is "Overlay"
0048      */
0049     void setContext(const Context c);
0050     Context context() const;
0051     /** The optional text */
0052     QString text() const;
0053     /** Set a type to control the look a bit */
0054     void setType(const Type t);
0055     Type type() const;
0056 public slots:
0057     /**
0058      * Set an optional text in the center of the rotating stuff
0059      * the text of course does not rotate ;-)
0060      */
0061     void setText(const QString &text);
0062     /**
0063      * You can start the spinner as much as you want. If it's still visible, this will just keep it
0064      * alive (or do nothing)
0065      * The optional @p delay in milliseconds can be used if you've no idea when you'll stop the
0066      * spinner and want to avoid it if your stop condition triggers in eg. less then 250ms
0067      * Ie. if you call m_spinner->start(250); QTimer::singleShot(100, m_spinner, SLOT(stop()));
0068      * the spinner will never actually start (because the stop call arrives before the delay times
0069      * out)
0070      */
0071     void start(uint delay);
0072     void start();
0073     /** Stop the spinner, the spinner will fade out for around a second before it's really hidden */
0074     void stop();
0075 
0076 protected:
0077     bool event(QEvent *e);
0078     bool eventFilter(QObject *o, QEvent *e);
0079     void paintEvent(QPaintEvent *e);
0080     void timerEvent(QTimerEvent *e);
0081 private slots:
0082     void updateAncestors();
0083     void updateGeometry();
0084 private:
0085     int m_step;
0086     int m_fadeStep;
0087     int m_timer;
0088     QTimer *m_startTimer;
0089     QList<QWidget*> m_ancestors;
0090     QString m_text;
0091     int m_textCols;
0092     Type m_type;
0093     bool m_geometryDirty;
0094     Context m_context;
0095 };
0096 
0097 } // namespace
0098 
0099 #endif // SPINNER_H