2021-03-05 00:35:15 +01:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2018-04-28 14:27:12 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QFontMetrics>
|
|
|
|
#include <QPaintDevice>
|
|
|
|
#include <QPainter>
|
2020-06-05 23:34:00 +02:00
|
|
|
#include <QPainterPath>
|
2019-07-05 04:58:06 +02:00
|
|
|
#include <QtGlobal>
|
2018-04-28 14:27:12 +02:00
|
|
|
|
|
|
|
class Painter : public QPainter
|
|
|
|
{
|
|
|
|
public:
|
2021-09-18 00:22:33 +02:00
|
|
|
explicit Painter(QPaintDevice *device)
|
|
|
|
: QPainter(device)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void drawTextLeft(int x, int y, const QString &text)
|
|
|
|
{
|
|
|
|
QFontMetrics m(fontMetrics());
|
|
|
|
drawText(x, y + m.ascent(), text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawTextRight(int x, int y, int outerw, const QString &text, int textWidth = -1)
|
|
|
|
{
|
|
|
|
QFontMetrics m(fontMetrics());
|
|
|
|
if (textWidth < 0) {
|
|
|
|
textWidth = m.horizontalAdvance(text);
|
|
|
|
}
|
|
|
|
drawText((outerw - x - textWidth), y + m.ascent(), text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawPixmapLeft(int x, int y, const QPixmap &pix, const QRect &from)
|
|
|
|
{
|
|
|
|
drawPixmap(QPoint(x, y), pix, from);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawPixmapLeft(const QPoint &p, const QPixmap &pix, const QRect &from)
|
|
|
|
{
|
|
|
|
return drawPixmapLeft(p.x(), p.y(), pix, from);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawPixmapLeft(int x, int y, int w, int h, const QPixmap &pix, const QRect &from)
|
|
|
|
{
|
|
|
|
drawPixmap(QRect(x, y, w, h), pix, from);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawPixmapLeft(const QRect &r, const QPixmap &pix, const QRect &from)
|
|
|
|
{
|
|
|
|
return drawPixmapLeft(r.x(), r.y(), r.width(), r.height(), pix, from);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawPixmapLeft(int x, int y, int outerw, const QPixmap &pix)
|
|
|
|
{
|
|
|
|
Q_UNUSED(outerw);
|
|
|
|
drawPixmap(QPoint(x, y), pix);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawPixmapLeft(const QPoint &p, int outerw, const QPixmap &pix)
|
|
|
|
{
|
|
|
|
return drawPixmapLeft(p.x(), p.y(), outerw, pix);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawPixmapRight(int x, int y, int outerw, const QPixmap &pix, const QRect &from)
|
|
|
|
{
|
|
|
|
drawPixmap(QPoint((outerw - x - (from.width() / pix.devicePixelRatio())), y), pix, from);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawPixmapRight(const QPoint &p, int outerw, const QPixmap &pix, const QRect &from)
|
|
|
|
{
|
|
|
|
return drawPixmapRight(p.x(), p.y(), outerw, pix, from);
|
|
|
|
}
|
|
|
|
void
|
|
|
|
drawPixmapRight(int x, int y, int w, int h, int outerw, const QPixmap &pix, const QRect &from)
|
|
|
|
{
|
|
|
|
drawPixmap(QRect((outerw - x - w), y, w, h), pix, from);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawPixmapRight(const QRect &r, int outerw, const QPixmap &pix, const QRect &from)
|
|
|
|
{
|
|
|
|
return drawPixmapRight(r.x(), r.y(), r.width(), r.height(), outerw, pix, from);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawPixmapRight(int x, int y, int outerw, const QPixmap &pix)
|
|
|
|
{
|
|
|
|
drawPixmap(QPoint((outerw - x - (pix.width() / pix.devicePixelRatio())), y), pix);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawPixmapRight(const QPoint &p, int outerw, const QPixmap &pix)
|
|
|
|
{
|
|
|
|
return drawPixmapRight(p.x(), p.y(), outerw, pix);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawAvatar(const QPixmap &pix, int w, int h, int d)
|
|
|
|
{
|
|
|
|
QPainterPath pp;
|
|
|
|
pp.addEllipse((w - d) / 2, (h - d) / 2, d, d);
|
|
|
|
|
|
|
|
QRect region((w - d) / 2, (h - d) / 2, d, d);
|
|
|
|
|
|
|
|
setClipPath(pp);
|
|
|
|
drawPixmap(region, pix);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawLetterAvatar(const QString &c,
|
|
|
|
const QColor &penColor,
|
|
|
|
const QColor &brushColor,
|
|
|
|
int w,
|
|
|
|
int h,
|
|
|
|
int d)
|
|
|
|
{
|
|
|
|
QRect region((w - d) / 2, (h - d) / 2, d, d);
|
|
|
|
|
|
|
|
setPen(Qt::NoPen);
|
|
|
|
setBrush(brushColor);
|
|
|
|
|
|
|
|
drawEllipse(region.center(), d / 2, d / 2);
|
|
|
|
|
|
|
|
setBrush(Qt::NoBrush);
|
|
|
|
drawEllipse(region.center(), d / 2, d / 2);
|
|
|
|
|
|
|
|
setPen(penColor);
|
|
|
|
drawText(region.translated(0, -1), Qt::AlignCenter, c);
|
|
|
|
}
|
2018-04-28 14:27:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class PainterHighQualityEnabler
|
|
|
|
{
|
|
|
|
public:
|
2021-09-18 00:22:33 +02:00
|
|
|
PainterHighQualityEnabler(Painter &p)
|
|
|
|
: _painter(p)
|
|
|
|
{
|
|
|
|
hints_ =
|
|
|
|
QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing;
|
2018-04-28 14:27:12 +02:00
|
|
|
|
2021-09-18 00:22:33 +02:00
|
|
|
_painter.setRenderHints(hints_);
|
|
|
|
}
|
2018-04-28 14:27:12 +02:00
|
|
|
|
2021-09-18 00:22:33 +02:00
|
|
|
~PainterHighQualityEnabler()
|
|
|
|
{
|
|
|
|
if (hints_)
|
|
|
|
_painter.setRenderHints(hints_, false);
|
|
|
|
}
|
2018-04-28 14:27:12 +02:00
|
|
|
|
2021-09-18 00:22:33 +02:00
|
|
|
PainterHighQualityEnabler(const PainterHighQualityEnabler &other) = delete;
|
|
|
|
PainterHighQualityEnabler &operator=(const PainterHighQualityEnabler &other) = delete;
|
2018-04-28 14:27:12 +02:00
|
|
|
|
|
|
|
private:
|
2021-09-18 00:22:33 +02:00
|
|
|
Painter &_painter;
|
|
|
|
QPainter::RenderHints hints_ = {};
|
2018-04-28 14:27:12 +02:00
|
|
|
};
|