-
Notifications
You must be signed in to change notification settings - Fork 1
/
FastCairoWidget.cpp
64 lines (49 loc) · 1.29 KB
/
FastCairoWidget.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
#include <QBackingStore>
#include "cairo/cairo.h"
#include "CairoWidget.hpp"
//a fast but unreliable alternative implementation////////////////////////////
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);
}
}
{
setAttribute(Qt::WA_DontCreateNativeAncestors);
setAttribute(Qt::WA_NativeWindow);
}
//////////////////////////////////////////////////////////////////////////////
CairoWidget::~CairoWidget() { cairo_destroy(cr_); }
//////////////////////////////////////////////////////////////////////////////
void CairoWidget::paintEvent(QPaintEvent*)
{
auto cr(cr_);
auto const w(width()), h(height());
if ((w != w_) || (h != h_))
{
w_ = w; h_ = h;
//
cairo_destroy(cr);
auto& img(*static_cast<QImage*>(backingStore()->paintDevice()));
auto const srf(
cairo_image_surface_create_for_data(
img.bits(),
CAIRO_FORMAT_ARGB32,
w,
h,
img.bytesPerLine()
)
);
cr_ = cr = cairo_create(srf);
cairo_surface_destroy(srf);
//
if_(cr, w, h);
}
//
cairo_save(cr);
df_(cr, w, h);
cairo_restore(cr);
}