summary refs log tree commit diff
path: root/src/ui/Painter.h
blob: 5a7dae3e0b53d1faa38b206ff6f4d9fbe966cd95 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// SPDX-FileCopyrightText: 2021 Nheko Contributors
// SPDX-FileCopyrightText: 2022 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#pragma once

#include <QFontMetrics>
#include <QPaintDevice>
#include <QPainter>
#include <QPainterPath>
#include <QtGlobal>

class Painter : public QPainter
{
public:
    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);
    }
};

class PainterHighQualityEnabler
{
public:
    PainterHighQualityEnabler(Painter &p)
      : _painter(p)
    {
        hints_ =
          QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing;

        _painter.setRenderHints(hints_);
    }

    ~PainterHighQualityEnabler()
    {
        if (hints_)
            _painter.setRenderHints(hints_, false);
    }

    PainterHighQualityEnabler(const PainterHighQualityEnabler &other) = delete;
    PainterHighQualityEnabler &operator=(const PainterHighQualityEnabler &other) = delete;

private:
    Painter &_painter;
    QPainter::RenderHints hints_ = {};
};