aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/progressindicator.h
blob: 0105072f36bd768cf41d1393a141d6cbcd73de23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#pragma once

#include "utils_global.h"

#include "overlaywidget.h"

#include <QTimer>

#include <functional>
#include <memory>

namespace Utils {

namespace Internal { class ProgressIndicatorPrivate; }

enum class ProgressIndicatorSize
{
    Small,
    Medium,
    Large
};

class QTCREATOR_UTILS_EXPORT ProgressIndicatorPainter
{
public:
    using UpdateCallback = std::function<void()>;

    ProgressIndicatorPainter(ProgressIndicatorSize size);
    virtual ~ProgressIndicatorPainter() = default;

    void setIndicatorSize(ProgressIndicatorSize size);
    ProgressIndicatorSize indicatorSize() const;

    void setUpdateCallback(const UpdateCallback &cb);

    QSize size() const;

    void paint(QPainter &painter, const QRect &rect) const;

    void startAnimation();
    void stopAnimation();

protected:
    void nextAnimationStep();

private:
    ProgressIndicatorSize m_size = ProgressIndicatorSize::Small;
    int m_rotationStep = 45;
    int m_rotation = 0;
    QTimer m_timer;
    QPixmap m_pixmap;
    UpdateCallback m_callback;
};

class QTCREATOR_UTILS_EXPORT ProgressIndicator : public OverlayWidget
{
    Q_OBJECT
public:
    explicit ProgressIndicator(ProgressIndicatorSize size, QWidget *parent = nullptr);

    void setIndicatorSize(ProgressIndicatorSize size);

    QSize sizeHint() const final;

protected:
    void showEvent(QShowEvent *) final;
    void hideEvent(QHideEvent *) final;

private:
    ProgressIndicatorPainter m_paint;
};

} // Utils