summary refs log tree commit diff
path: root/src/ui/SnackBar.h
blob: eed59c871c8718ba09a48b6538eabbf21d699cbb (plain) (blame)
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
77
78
79
#pragma once

#include <QCoreApplication>
#include <QPaintEvent>
#include <QTimer>
#include <deque>

#include "OverlayWidget.h"

enum class SnackBarPosition
{
        Bottom,
        Top,
};

class SnackBar : public OverlayWidget
{
        Q_OBJECT

public:
        explicit SnackBar(QWidget *parent);

        inline void setBackgroundColor(const QColor &color);
        inline void setTextColor(const QColor &color);
        inline void setPosition(SnackBarPosition pos);

public slots:
        void showMessage(const QString &msg);

protected:
        void paintEvent(QPaintEvent *event) override;
        void mousePressEvent(QMouseEvent *event) override;

private slots:
        void hideMessage();

private:
        void stopTimers();
        void start();

        QColor bgColor_;
        QColor textColor_;

        qreal bgOpacity_;
        qreal offset_;

        std::deque<QString> messages_;

        QTimer showTimer_;
        QTimer hideTimer_;

        int duration_;
        int boxWidth_;
        int boxHeight_;
        int boxPadding_;

        SnackBarPosition position_;
};

inline void
SnackBar::setPosition(SnackBarPosition pos)
{
        position_ = pos;
        update();
}

inline void
SnackBar::setBackgroundColor(const QColor &color)
{
        bgColor_ = color;
        update();
}

inline void
SnackBar::setTextColor(const QColor &color)
{
        textColor_ = color;
        update();
}