mirror of https://github.com/Nheko-Reborn/nheko
parent
67458dd2f8
commit
9a0e18dea7
@ -0,0 +1,47 @@ |
||||
#pragma once |
||||
|
||||
#include <QColor> |
||||
#include <QDateTime> |
||||
#include <QWidget> |
||||
|
||||
class InfoMessage : public QWidget |
||||
{ |
||||
Q_OBJECT |
||||
|
||||
Q_PROPERTY(QColor textColor WRITE setTextColor READ textColor) |
||||
Q_PROPERTY(QColor boxColor WRITE setBoxColor READ boxColor) |
||||
|
||||
public: |
||||
explicit InfoMessage(QWidget *parent = nullptr); |
||||
InfoMessage(QString msg, QWidget *parent = nullptr); |
||||
|
||||
void setTextColor(QColor color) { textColor_ = color; } |
||||
void setBoxColor(QColor color) { boxColor_ = color; } |
||||
void saveDatetime(QDateTime datetime) { datetime_ = datetime; } |
||||
|
||||
QColor textColor() const { return textColor_; } |
||||
QColor boxColor() const { return boxColor_; } |
||||
QDateTime datetime() const { return datetime_; } |
||||
|
||||
protected: |
||||
void paintEvent(QPaintEvent *event) override; |
||||
|
||||
int width_; |
||||
int height_; |
||||
|
||||
QString msg_; |
||||
QFont font_; |
||||
|
||||
QDateTime datetime_; |
||||
|
||||
QColor textColor_ = QColor("black"); |
||||
QColor boxColor_ = QColor("white"); |
||||
}; |
||||
|
||||
class DateSeparator : public InfoMessage |
||||
{ |
||||
Q_OBJECT |
||||
|
||||
public: |
||||
DateSeparator(QDateTime datetime, QWidget *parent = nullptr); |
||||
}; |
@ -0,0 +1,79 @@ |
||||
#include "Config.h" |
||||
#include "InfoMessage.hpp" |
||||
|
||||
#include <QDateTime> |
||||
#include <QPainter> |
||||
#include <QPen> |
||||
|
||||
constexpr int VPadding = 6; |
||||
constexpr int HPadding = 12; |
||||
constexpr int HMargin = 20; |
||||
|
||||
InfoMessage::InfoMessage(QWidget *parent) |
||||
: QWidget{parent} |
||||
{ |
||||
font_.setWeight(60); |
||||
font_.setPixelSize(conf::timeline::fonts::dateSeparator); |
||||
} |
||||
|
||||
InfoMessage::InfoMessage(QString msg, QWidget *parent) |
||||
: QWidget{parent} |
||||
, msg_{msg} |
||||
{ |
||||
font_.setWeight(60); |
||||
font_.setPixelSize(conf::timeline::fonts::dateSeparator); |
||||
|
||||
QFontMetrics fm{font_}; |
||||
width_ = fm.width(msg_) + HPadding * 2; |
||||
height_ = fm.ascent() + 2 * VPadding; |
||||
|
||||
setFixedHeight(height_ + 2 * HMargin); |
||||
} |
||||
|
||||
void |
||||
InfoMessage::paintEvent(QPaintEvent *) |
||||
{ |
||||
QPainter p(this); |
||||
p.setRenderHint(QPainter::Antialiasing); |
||||
p.setFont(font_); |
||||
|
||||
// Center the box horizontally & vertically.
|
||||
auto textRegion = QRectF(width() / 2 - width_ / 2, HMargin, width_, height_); |
||||
|
||||
QPainterPath ppath; |
||||
ppath.addRoundedRect(textRegion, height_ / 2, height_ / 2); |
||||
|
||||
p.setPen(Qt::NoPen); |
||||
p.fillPath(ppath, boxColor()); |
||||
p.drawPath(ppath); |
||||
|
||||
p.setPen(QPen(textColor())); |
||||
p.drawText(textRegion, Qt::AlignCenter, msg_); |
||||
} |
||||
|
||||
DateSeparator::DateSeparator(QDateTime datetime, QWidget *parent) |
||||
: InfoMessage{parent} |
||||
{ |
||||
auto now = QDateTime::currentDateTime(); |
||||
auto days = now.daysTo(datetime); |
||||
|
||||
QString fmt; |
||||
|
||||
if (now.date().year() != datetime.date().year()) |
||||
fmt = QString("ddd d MMMM yy"); |
||||
else |
||||
fmt = QString("ddd d MMMM"); |
||||
|
||||
if (days == 0) |
||||
msg_ = tr("Today"); |
||||
else if (std::abs(days) == 1) |
||||
msg_ = tr("Yesterday"); |
||||
else |
||||
msg_ = datetime.toString(fmt); |
||||
|
||||
QFontMetrics fm{font_}; |
||||
width_ = fm.width(msg_) + HPadding * 2; |
||||
height_ = fm.ascent() + 2 * VPadding; |
||||
|
||||
setFixedHeight(height_ + 2 * HMargin); |
||||
} |
Loading…
Reference in new issue