-
Notifications
You must be signed in to change notification settings - Fork 1
/
CairoWidget.cpp
80 lines (60 loc) · 1.73 KB
/
CairoWidget.cpp
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
#include <QPainter>
#include "cairo/cairo.h"
#include "CairoWidget.hpp"
struct CairoWidget::S
{
static inline std::size_t size_;
static inline unsigned char* data_;
};
//////////////////////////////////////////////////////////////////////////////
CairoWidget::CairoWidget(QWidget* const p, Qt::WindowFlags const wf):
QWidget(p, wf),
df_{[](auto, auto, auto) noexcept {}},
if_{[](auto const cr, auto, auto) noexcept
{
cairo_set_line_width(cr, 1.);
cairo_translate(cr, .5, .5);
}
}
{
}
//////////////////////////////////////////////////////////////////////////////
CairoWidget::~CairoWidget() { cairo_destroy(cr_); }
//////////////////////////////////////////////////////////////////////////////
void CairoWidget::paintEvent(QPaintEvent*)
{
auto d(S::data_);
auto const w(width()), h(height());
{
auto cr(cr_);
if ((w != w_) || (h != h_) || (d != d_))
{
w_ = w; h_ = h;
//
auto const stride(stride_ =
cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, w));
if (auto const size(h * std::size_t(stride)); S::size_ < size)
{
delete [] d; S::data_ = d = new unsigned char[S::size_ = size];
}
d_ = d; // multiple instances
//
cairo_destroy(cr);
auto const srf(cairo_image_surface_create_for_data(
d, CAIRO_FORMAT_ARGB32, w, h, stride));
cr_ = cr = cairo_create(srf);
cairo_surface_destroy(srf);
//
if_(cr, w, h);
}
//
cairo_save(cr);
df_(cr, w, h);
cairo_restore(cr);
}
//
QPainter painter(this);
painter.setCompositionMode(QPainter::CompositionMode_Source);
painter.drawPixmap(QPoint{}, QPixmap::fromImage(
{d, w, h, stride_, QImage::Format_ARGB32_Premultiplied}));
}