1 | /* |
2 | SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
5 | */ |
6 | #include "region.h" |
7 | #include "wayland_pointer_p.h" |
8 | // Qt |
9 | #include <QList> |
10 | #include <QRegion> |
11 | // Wayland |
12 | #include <wayland-client-protocol.h> |
13 | |
14 | namespace KWayland |
15 | { |
16 | namespace Client |
17 | { |
18 | class Q_DECL_HIDDEN Region::Private |
19 | { |
20 | public: |
21 | Private(const QRegion ®ion); |
22 | void installRegion(const QRect &rect); |
23 | void installRegion(const QRegion ®ion); |
24 | void uninstallRegion(const QRect &rect); |
25 | void uninstallRegion(const QRegion ®ion); |
26 | |
27 | WaylandPointer<wl_region, wl_region_destroy> region; |
28 | QRegion qtRegion; |
29 | }; |
30 | |
31 | Region::Private::Private(const QRegion ®ion) |
32 | : qtRegion(region) |
33 | { |
34 | } |
35 | |
36 | void Region::Private::installRegion(const QRect &rect) |
37 | { |
38 | if (!region.isValid()) { |
39 | return; |
40 | } |
41 | wl_region_add(wl_region: region, x: rect.x(), y: rect.y(), width: rect.width(), height: rect.height()); |
42 | } |
43 | |
44 | void Region::Private::installRegion(const QRegion ®ion) |
45 | { |
46 | for (const QRect &rect : region) { |
47 | installRegion(rect); |
48 | } |
49 | } |
50 | |
51 | void Region::Private::uninstallRegion(const QRect &rect) |
52 | { |
53 | if (!region.isValid()) { |
54 | return; |
55 | } |
56 | wl_region_subtract(wl_region: region, x: rect.x(), y: rect.y(), width: rect.width(), height: rect.height()); |
57 | } |
58 | |
59 | void Region::Private::uninstallRegion(const QRegion ®ion) |
60 | { |
61 | for (const QRect &rect : region) { |
62 | uninstallRegion(rect); |
63 | } |
64 | } |
65 | |
66 | Region::Region(const QRegion ®ion, QObject *parent) |
67 | : QObject(parent) |
68 | , d(new Private(region)) |
69 | { |
70 | } |
71 | |
72 | Region::~Region() |
73 | { |
74 | release(); |
75 | } |
76 | |
77 | void Region::release() |
78 | { |
79 | d->region.release(); |
80 | } |
81 | |
82 | void Region::destroy() |
83 | { |
84 | d->region.destroy(); |
85 | } |
86 | |
87 | void Region::setup(wl_region *region) |
88 | { |
89 | Q_ASSERT(region); |
90 | d->region.setup(pointer: region); |
91 | d->installRegion(region: d->qtRegion); |
92 | } |
93 | |
94 | bool Region::isValid() const |
95 | { |
96 | return d->region.isValid(); |
97 | } |
98 | |
99 | void Region::add(const QRect &rect) |
100 | { |
101 | d->qtRegion = d->qtRegion.united(r: rect); |
102 | d->installRegion(rect); |
103 | } |
104 | |
105 | void Region::add(const QRegion ®ion) |
106 | { |
107 | d->qtRegion = d->qtRegion.united(r: region); |
108 | d->installRegion(region); |
109 | } |
110 | |
111 | void Region::subtract(const QRect &rect) |
112 | { |
113 | d->qtRegion = d->qtRegion.subtracted(r: rect); |
114 | d->uninstallRegion(rect); |
115 | } |
116 | |
117 | void Region::subtract(const QRegion ®ion) |
118 | { |
119 | d->qtRegion = d->qtRegion.subtracted(r: region); |
120 | d->uninstallRegion(region); |
121 | } |
122 | |
123 | QRegion Region::region() const |
124 | { |
125 | return d->qtRegion; |
126 | } |
127 | |
128 | Region::operator wl_region *() const |
129 | { |
130 | return d->region; |
131 | } |
132 | |
133 | Region::operator wl_region *() |
134 | { |
135 | return d->region; |
136 | } |
137 | |
138 | } |
139 | } |
140 | |
141 | #include "moc_region.cpp" |
142 | |