matrixion/src/ui/SnackBar.h

93 lines
1.8 KiB
C
Raw Normal View History

2021-03-05 02:35:15 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
2017-10-08 16:49:56 +03:00
#pragma once
#include <QCoreApplication>
#include <QPaintEvent>
2021-03-05 16:04:11 +03:00
#include <QPropertyAnimation>
2017-10-08 16:49:56 +03:00
#include <QTimer>
#include <deque>
2017-10-08 16:49:56 +03:00
#include "OverlayWidget.h"
2017-10-15 22:08:51 +03:00
enum class SnackBarPosition
{
2021-09-18 01:22:33 +03:00
Bottom,
Top,
2017-10-08 16:49:56 +03:00
};
class SnackBar : public OverlayWidget
{
2021-09-18 01:22:33 +03:00
Q_OBJECT
2017-10-08 16:49:56 +03:00
2021-09-18 01:22:33 +03:00
Q_PROPERTY(QColor bgColor READ backgroundColor WRITE setBackgroundColor)
Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor)
Q_PROPERTY(double offset READ offset WRITE setOffset NOTIFY offsetChanged)
2018-07-25 23:07:56 +03:00
2017-10-08 16:49:56 +03:00
public:
2021-09-18 01:22:33 +03:00
explicit SnackBar(QWidget *parent);
QColor backgroundColor() const { return bgColor_; }
void setBackgroundColor(const QColor &color)
{
bgColor_ = color;
update();
}
QColor textColor() const { return textColor_; }
void setTextColor(const QColor &color)
{
textColor_ = color;
update();
}
void setPosition(SnackBarPosition pos)
{
position_ = pos;
update();
}
double offset() { return offset_; }
void setOffset(double offset)
{
if (offset != offset_) {
offset_ = offset;
emit offsetChanged();
2021-03-05 16:04:11 +03:00
}
2021-09-18 01:22:33 +03:00
}
2021-03-05 16:04:11 +03:00
2017-10-08 16:49:56 +03:00
public slots:
2021-09-18 01:22:33 +03:00
void showMessage(const QString &msg);
2017-10-08 16:49:56 +03:00
2021-03-05 16:04:11 +03:00
signals:
2021-09-18 01:22:33 +03:00
void offsetChanged();
2021-03-05 16:04:11 +03:00
2017-10-08 16:49:56 +03:00
protected:
2021-09-18 01:22:33 +03:00
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
2017-10-08 16:49:56 +03:00
private slots:
2021-09-18 01:22:33 +03:00
void hideMessage();
2017-10-08 16:49:56 +03:00
private:
2021-09-18 01:22:33 +03:00
void stopTimers();
void start();
2017-10-08 16:49:56 +03:00
2021-09-18 01:22:33 +03:00
QColor bgColor_;
QColor textColor_;
2017-10-08 16:49:56 +03:00
2021-09-18 01:22:33 +03:00
qreal bgOpacity_;
qreal offset_;
2017-10-08 16:49:56 +03:00
2021-09-18 01:22:33 +03:00
std::deque<QString> messages_;
2017-10-08 16:49:56 +03:00
2021-09-18 01:22:33 +03:00
QTimer hideTimer_;
2017-10-08 16:49:56 +03:00
2021-09-18 01:22:33 +03:00
double boxHeight_;
2017-10-08 16:49:56 +03:00
2021-09-18 01:22:33 +03:00
QPropertyAnimation offset_anim;
2021-03-05 16:04:11 +03:00
2021-09-18 01:22:33 +03:00
SnackBarPosition position_;
2017-10-08 16:49:56 +03:00
};