2017-09-29 14:06:44 +00:00
|
|
|
#include "qtmaterialsnackbar.h"
|
|
|
|
#include "qtmaterialsnackbar_p.h"
|
|
|
|
#include <QtWidgets/QApplication>
|
2017-09-29 13:58:00 +00:00
|
|
|
#include <QPainter>
|
2017-09-29 14:06:44 +00:00
|
|
|
#include "qtmaterialsnackbar_internal.h"
|
|
|
|
#include "lib/qtmaterialstyle.h"
|
|
|
|
#include "lib/qtmaterialstatetransition.h"
|
2017-09-29 13:58:00 +00:00
|
|
|
#include <QDebug>
|
2022-02-17 02:57:43 +00:00
|
|
|
namespace md
|
|
|
|
{
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \class QtMaterialSnackbarPrivate
|
|
|
|
* \internal
|
|
|
|
*/
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
SnackBarPrivate::SnackBarPrivate(SnackBar *q)
|
2017-09-29 13:58:00 +00:00
|
|
|
: q_ptr(q)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
SnackBarPrivate::~SnackBarPrivate()
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
void SnackBarPrivate::init()
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_Q(SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
stateMachine = new SnackBarStateMachine(q);
|
2017-09-29 13:58:00 +00:00
|
|
|
bgOpacity = 0.9;
|
|
|
|
duration = 3000;
|
|
|
|
boxWidth = 300;
|
|
|
|
clickDismiss = false;
|
|
|
|
useThemeColors = true;
|
|
|
|
|
|
|
|
q->setAttribute(Qt::WA_TransparentForMouseEvents);
|
|
|
|
|
2017-10-06 20:11:41 +00:00
|
|
|
QFont font("Roboto", 10, QFont::Medium);
|
2017-09-29 13:58:00 +00:00
|
|
|
font.setCapitalization(QFont::AllUppercase);
|
|
|
|
q->setFont(font);
|
|
|
|
|
|
|
|
stateMachine->start();
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \class QtMaterialSnackbar
|
|
|
|
*/
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
SnackBar::SnackBar(QWidget *parent)
|
|
|
|
: OverlayWidget(parent),
|
|
|
|
d_ptr(new SnackBarPrivate(this))
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
|
|
|
d_func()->init();
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
SnackBar::~SnackBar()
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
void SnackBar::setAutoHideDuration(int duration)
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_D(SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
d->duration = duration;
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
int SnackBar::autoHideDuration() const
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_D(const SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
return d->duration;
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
void SnackBar::setUseThemeColors(bool value)
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_D(SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
if (d->useThemeColors == value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->useThemeColors = value;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
bool SnackBar::useThemeColors() const
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_D(const SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
return d->useThemeColors;
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
void SnackBar::setBackgroundColor(const QColor &color)
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_D(SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
d->backgroundColor = color;
|
2017-09-30 00:20:12 +00:00
|
|
|
|
|
|
|
MATERIAL_DISABLE_THEME_COLORS
|
|
|
|
update();
|
2017-09-29 13:58:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
QColor SnackBar::backgroundColor() const
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_D(const SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
if (d->useThemeColors || !d->backgroundColor.isValid()) {
|
2022-02-17 02:57:43 +00:00
|
|
|
return Style::instance().themeColor("text");
|
2017-09-29 13:58:00 +00:00
|
|
|
} else {
|
|
|
|
return d->backgroundColor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
void SnackBar::setBackgroundOpacity(qreal opacity)
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_D(SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
d->bgOpacity = opacity;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
qreal SnackBar::backgroundOpacity() const
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_D(const SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
return d->bgOpacity;
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
void SnackBar::setTextColor(const QColor &color)
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_D(SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
d->textColor = color;
|
2017-09-30 00:20:12 +00:00
|
|
|
|
|
|
|
MATERIAL_DISABLE_THEME_COLORS
|
|
|
|
update();
|
2017-09-29 13:58:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
QColor SnackBar::textColor() const
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_D(const SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
if (d->useThemeColors || !d->textColor.isValid()) {
|
2022-02-17 02:57:43 +00:00
|
|
|
return Style::instance().themeColor("canvas");
|
2017-09-29 13:58:00 +00:00
|
|
|
} else {
|
|
|
|
return d->textColor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
void SnackBar::setFontSize(qreal size)
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
|
|
|
QFont f(font());
|
|
|
|
f.setPointSizeF(size);
|
|
|
|
setFont(f);
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
qreal SnackBar::fontSize() const
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
|
|
|
return font().pointSizeF();
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
void SnackBar::setBoxWidth(int width)
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_D(SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
d->boxWidth = width;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
int SnackBar::boxWidth() const
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_D(const SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
return d->boxWidth;
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
void SnackBar::setClickToDismissMode(bool value)
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_D(SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
d->clickDismiss = value;
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
bool SnackBar::clickToDismissMode() const
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_D(const SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
return d->clickDismiss;
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
void SnackBar::addMessage(const QString &message)
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_D(SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
d->messages.push_back(message);
|
2022-02-17 02:57:43 +00:00
|
|
|
d->stateMachine->postEvent(new StateTransitionEvent(SnackbarShowTransition));
|
2017-09-30 10:44:22 +00:00
|
|
|
raise();
|
2017-09-29 13:58:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
void SnackBar::addInstantMessage(const QString &message)
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_D(SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
if (d->messages.isEmpty()) {
|
|
|
|
d->messages.push_back(message);
|
|
|
|
} else {
|
|
|
|
d->messages.insert(1, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
d->stateMachine->progress();
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
void SnackBar::dequeue()
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_D(SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
if (d->messages.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->messages.removeFirst();
|
|
|
|
|
|
|
|
if (!d->messages.isEmpty()) {
|
2022-02-17 02:57:43 +00:00
|
|
|
d->stateMachine->postEvent(new StateTransitionEvent(SnackbarNextTransition));
|
2017-09-29 13:58:00 +00:00
|
|
|
} else {
|
2022-02-17 02:57:43 +00:00
|
|
|
d->stateMachine->postEvent(new StateTransitionEvent(SnackbarWaitTransition));
|
2017-09-29 13:58:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
void SnackBar::paintEvent(QPaintEvent *event)
|
2017-09-29 13:58:00 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(event)
|
|
|
|
|
2022-02-17 02:57:43 +00:00
|
|
|
Q_D(SnackBar);
|
2017-09-29 13:58:00 +00:00
|
|
|
|
|
|
|
if (d->messages.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString message = d->messages.first();
|
|
|
|
|
|
|
|
QPainter painter(this);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
|
|
|
|
QBrush brush;
|
|
|
|
brush.setStyle(Qt::SolidPattern);
|
|
|
|
brush.setColor(backgroundColor());
|
|
|
|
painter.setBrush(brush);
|
|
|
|
painter.setOpacity(d->bgOpacity);
|
|
|
|
|
|
|
|
QRect r(0, 0, d->boxWidth, 40);
|
|
|
|
|
|
|
|
painter.setPen(Qt::white);
|
|
|
|
QRect br = painter.boundingRect(r, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, message);
|
|
|
|
|
|
|
|
painter.setPen(Qt::NoPen);
|
|
|
|
r = br.united(r).adjusted(-10, -10, 10, 20);
|
|
|
|
|
|
|
|
const qreal s = 1-d->stateMachine->offset();
|
|
|
|
|
|
|
|
painter.translate((width()-(r.width()-20))/2,
|
|
|
|
height()+10-s*(r.height()));
|
|
|
|
|
|
|
|
br.moveCenter(r.center());
|
|
|
|
painter.drawRoundedRect(r.adjusted(0, 0, 0, 3), 3, 3);
|
|
|
|
painter.setPen(textColor());
|
|
|
|
painter.drawText(br, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, message);
|
|
|
|
}
|
2022-02-17 02:57:43 +00:00
|
|
|
}
|