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
14namespace KWayland
15{
16namespace Client
17{
18class Q_DECL_HIDDEN Region::Private
19{
20public:
21 Private(const QRegion &region);
22 void installRegion(const QRect &rect);
23 void installRegion(const QRegion &region);
24 void uninstallRegion(const QRect &rect);
25 void uninstallRegion(const QRegion &region);
26
27 WaylandPointer<wl_region, wl_region_destroy> region;
28 QRegion qtRegion;
29};
30
31Region::Private::Private(const QRegion &region)
32 : qtRegion(region)
33{
34}
35
36void 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
44void Region::Private::installRegion(const QRegion &region)
45{
46 for (const QRect &rect : region) {
47 installRegion(rect);
48 }
49}
50
51void 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
59void Region::Private::uninstallRegion(const QRegion &region)
60{
61 for (const QRect &rect : region) {
62 uninstallRegion(rect);
63 }
64}
65
66Region::Region(const QRegion &region, QObject *parent)
67 : QObject(parent)
68 , d(new Private(region))
69{
70}
71
72Region::~Region()
73{
74 release();
75}
76
77void Region::release()
78{
79 d->region.release();
80}
81
82void Region::destroy()
83{
84 d->region.destroy();
85}
86
87void Region::setup(wl_region *region)
88{
89 Q_ASSERT(region);
90 d->region.setup(pointer: region);
91 d->installRegion(region: d->qtRegion);
92}
93
94bool Region::isValid() const
95{
96 return d->region.isValid();
97}
98
99void Region::add(const QRect &rect)
100{
101 d->qtRegion = d->qtRegion.united(r: rect);
102 d->installRegion(rect);
103}
104
105void Region::add(const QRegion &region)
106{
107 d->qtRegion = d->qtRegion.united(r: region);
108 d->installRegion(region);
109}
110
111void Region::subtract(const QRect &rect)
112{
113 d->qtRegion = d->qtRegion.subtracted(r: rect);
114 d->uninstallRegion(rect);
115}
116
117void Region::subtract(const QRegion &region)
118{
119 d->qtRegion = d->qtRegion.subtracted(r: region);
120 d->uninstallRegion(region);
121}
122
123QRegion Region::region() const
124{
125 return d->qtRegion;
126}
127
128Region::operator wl_region *() const
129{
130 return d->region;
131}
132
133Region::operator wl_region *()
134{
135 return d->region;
136}
137
138}
139}
140
141#include "moc_region.cpp"
142

source code of kwayland/src/client/region.cpp