2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2018-09-30 13:24:36 +03:00
|
|
|
#include "InfoMessage.h"
|
2018-09-30 14:33:54 +03:00
|
|
|
#include "Config.h"
|
2018-07-07 13:39:53 +03:00
|
|
|
|
|
|
|
#include <QDateTime>
|
2019-07-28 13:50:10 +03:00
|
|
|
#include <QLocale>
|
2018-07-07 13:39:53 +03:00
|
|
|
#include <QPainter>
|
2020-06-06 00:34:00 +03:00
|
|
|
#include <QPainterPath>
|
2018-07-07 13:39:53 +03:00
|
|
|
#include <QPen>
|
2019-07-05 05:58:06 +03:00
|
|
|
#include <QtGlobal>
|
2018-07-07 13:39:53 +03:00
|
|
|
|
|
|
|
constexpr int VPadding = 6;
|
|
|
|
constexpr int HPadding = 12;
|
|
|
|
constexpr int HMargin = 20;
|
|
|
|
|
|
|
|
InfoMessage::InfoMessage(QWidget *parent)
|
|
|
|
: QWidget{parent}
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
initFont();
|
2018-07-07 13:39:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
InfoMessage::InfoMessage(QString msg, QWidget *parent)
|
|
|
|
: QWidget{parent}
|
|
|
|
, msg_{msg}
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
initFont();
|
2018-07-07 13:39:53 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
QFontMetrics fm{font()};
|
|
|
|
width_ = fm.horizontalAdvance(msg_) + HPadding * 2;
|
|
|
|
height_ = fm.ascent() + 2 * VPadding;
|
2018-07-07 13:39:53 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
setFixedHeight(height_ + 2 * HMargin);
|
2018-07-07 13:39:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InfoMessage::paintEvent(QPaintEvent *)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
QPainter p(this);
|
|
|
|
p.setRenderHint(QPainter::Antialiasing);
|
|
|
|
p.setFont(font());
|
2018-07-07 13:39:53 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// Center the box horizontally & vertically.
|
|
|
|
auto textRegion = QRectF(width() / 2 - width_ / 2, HMargin, width_, height_);
|
2018-07-07 13:39:53 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
QPainterPath ppath;
|
|
|
|
ppath.addRoundedRect(textRegion, height_ / 2, height_ / 2);
|
2018-07-07 13:39:53 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.fillPath(ppath, boxColor());
|
|
|
|
p.drawPath(ppath);
|
2018-07-07 13:39:53 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
p.setPen(QPen(textColor()));
|
|
|
|
p.drawText(textRegion, Qt::AlignCenter, msg_);
|
2018-07-07 13:39:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
DateSeparator::DateSeparator(QDateTime datetime, QWidget *parent)
|
|
|
|
: InfoMessage{parent}
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto now = QDateTime::currentDateTime();
|
2018-07-07 13:39:53 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
QString fmt = QLocale::system().dateFormat(QLocale::LongFormat);
|
2018-07-07 13:39:53 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (now.date().year() == datetime.date().year()) {
|
|
|
|
QRegularExpression rx("[^a-zA-Z]*y+[^a-zA-Z]*");
|
|
|
|
fmt = fmt.remove(rx);
|
|
|
|
}
|
2018-07-07 13:39:53 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
msg_ = datetime.date().toString(fmt);
|
2018-07-07 13:39:53 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
QFontMetrics fm{font()};
|
|
|
|
width_ = fm.horizontalAdvance(msg_) + HPadding * 2;
|
|
|
|
height_ = fm.ascent() + 2 * VPadding;
|
2018-07-07 13:39:53 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
setFixedHeight(height_ + 2 * HMargin);
|
2018-07-07 13:39:53 +03:00
|
|
|
}
|