Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qgeoshape.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qgeoshape.h"
5#include "qgeoshape_p.h"
7#include "qgeocircle.h"
8#include "qgeopath.h"
9#include "qgeopolygon.h"
10
11
12#ifndef QT_NO_DEBUG_STREAM
13#include <QtCore/QDebug>
14#endif
15
16#ifndef QT_NO_DATASTREAM
17#include <QtCore/QDataStream>
18#endif
19
20QT_BEGIN_NAMESPACE
21
22QT_IMPL_METATYPE_EXTERN(QGeoShape)
23
24QGeoShapePrivate::QGeoShapePrivate(QGeoShape::ShapeType type)
25: type(type)
26{
27}
28
29QGeoShapePrivate::~QGeoShapePrivate()
30{
31}
32
33bool QGeoShapePrivate::operator==(const QGeoShapePrivate &other) const
34{
35 return type == other.type;
36}
37
38/*!
39 \class QGeoShape
40 \inmodule QtPositioning
41 \ingroup QtPositioning-positioning
42 \since 5.2
43
44 \brief The QGeoShape class defines a geographic area.
45
46 This class is the base class for classes which specify a geographic
47 area.
48
49 For the sake of consistency, subclasses should describe the specific
50 details of the associated areas in terms of QGeoCoordinate instances
51 and distances in meters.
52
53 This class is a \l Q_GADGET since Qt 5.5. It can be
54 \l{Cpp_value_integration_positioning}{directly used from C++ and QML}.
55*/
56
57/*!
58 \enum QGeoShape::ShapeType
59
60 Describes the type of the shape.
61
62 \value UnknownType A shape of unknown type
63 \value RectangleType A rectangular shape
64 \value CircleType A circular shape
65 \value PathType A path type
66 \value PolygonType A polygon type
67*/
68
69/*!
70 \property QGeoShape::type
71 \brief This property holds the type of this geo shape.
72
73 While this property is introduced in Qt 5.5, the related accessor functions
74 exist since the first version of this class.
75
76 \since 5.5
77*/
78
79/*!
80 \property QGeoShape::isValid
81 \brief This property holds the validity of the geo shape.
82
83 A geo shape is considered to be invalid if some of the data that is required to
84 unambiguously describe the geo shape has not been set or has been set to an
85 unsuitable value depending on the subclass of this object. The default constructed
86 objects of this type are invalid.
87
88 While this property is introduced in Qt 5.5, the related accessor functions
89 exist since the first version of this class.
90
91 \since 5.5
92*/
93
94/*!
95 \property QGeoShape::isEmpty
96 \brief This property defines whether this geo shape is empty.
97
98 An empty geo shape is a region which has a geometrical area of 0.
99
100 While this property is introduced in Qt 5.5, the related accessor functions
101 exist since the first version of this class.
102
103 \since 5.5
104*/
105inline QGeoShapePrivate *QGeoShape::d_func()
106{
107 return static_cast<QGeoShapePrivate *>(d_ptr.data());
108}
109
110inline const QGeoShapePrivate *QGeoShape::d_func() const
111{
112 return static_cast<const QGeoShapePrivate *>(d_ptr.constData());
113}
114
115/*!
116 Constructs a new invalid geo shape of \l UnknownType.
117*/
118QGeoShape::QGeoShape()
119{
120}
121
122/*!
123 Constructs a new geo shape which is a copy of \a other.
124*/
125QGeoShape::QGeoShape(const QGeoShape &other)
126: d_ptr(other.d_ptr)
127{
128}
129
130/*!
131 \internal
132*/
133QGeoShape::QGeoShape(QGeoShapePrivate *d)
134: d_ptr(d)
135{
136}
137
138bool QGeoShape::equals(const QGeoShape &lhs, const QGeoShape &rhs)
139{
140 if (lhs.d_func() == rhs.d_func())
141 return true;
142
143 if (!lhs.d_func() || !rhs.d_func())
144 return false;
145
146 return *lhs.d_func() == *rhs.d_func();
147}
148
149/*!
150 Destroys this geo shape.
151*/
152QGeoShape::~QGeoShape()
153{
154}
155
156/*!
157 Returns the type of this geo shape.
158*/
159QGeoShape::ShapeType QGeoShape::type() const
160{
161 Q_D(const QGeoShape);
162
163 if (d)
164 return d->type;
165 else
166 return UnknownType;
167}
168
169/*!
170 Returns whether this geo shape is valid.
171
172*/
173bool QGeoShape::isValid() const
174{
175 Q_D(const QGeoShape);
176
177 if (d)
178 return d->isValid();
179 else
180 return false;
181}
182
183/*!
184 Returns whether this geo shape is empty.
185
186 An empty geo shape is a region which has a geometrical area of 0.
187*/
188bool QGeoShape::isEmpty() const
189{
190 Q_D(const QGeoShape);
191
192 if (d)
193 return d->isEmpty();
194 else
195 return true;
196}
197
198/*!
199 Returns whether the coordinate \a coordinate is contained within this geo shape.
200*/
201bool QGeoShape::contains(const QGeoCoordinate &coordinate) const
202{
203 Q_D(const QGeoShape);
204
205 if (d)
206 return d->contains(coordinate);
207 else
208 return false;
209}
210
211/*!
212 Returns a QGeoRectangle representing the geographical bounding rectangle of the
213 geo shape, that defines the latitudinal/longitudinal bounds of the geo shape.
214
215 \since 5.9
216*/
217QGeoRectangle QGeoShape::boundingGeoRectangle() const
218{
219 Q_D(const QGeoShape);
220
221 if (d)
222 return d->boundingGeoRectangle();
223 else
224 return QGeoRectangle();
225}
226
227/*!
228 \property QGeoShape::center
229 \brief the coordinate at the geometric center of the shape.
230*/
231
232/*!
233 Returns the coordinate located at the geometric center of the geo shape.
234
235 \since 5.5
236*/
237QGeoCoordinate QGeoShape::center() const
238{
239 Q_D(const QGeoShape);
240
241 if (d)
242 return d->center();
243 else
244 return QGeoCoordinate();
245}
246
247/*!
248 \fn bool QGeoShape::operator==(const QGeoShape &lhs, const QGeoShape &rhs)
249
250 Returns \c true if the \a lhs geo shape is equivalent to the \a rhs geo
251 shape, otherwise returns \c false.
252*/
253
254/*!
255 \fn bool QGeoShape::operator!=(const QGeoShape &lhs, const QGeoShape &rhs)
256
257 Returns \c true if the \a lhs geo shape is not equivalent to the \a rhs geo
258 shape, otherwise returns \c false.
259*/
260
261/*!
262 Assigns \a other to this geo shape and returns a reference to this geo shape.
263*/
264QGeoShape &QGeoShape::operator=(const QGeoShape &other)
265{
266 if (this == &other)
267 return *this;
268
269 d_ptr = other.d_ptr;
270 return *this;
271}
272
273/*!
274 Returns a string representation of this geo shape.
275
276 \since 5.5
277*/
278QString QGeoShape::toString() const
279{
280 return QStringLiteral("QGeoShape(%1)").arg(type());
281}
282
283#ifndef QT_NO_DEBUG_STREAM
284QDebug QGeoShape::debugStreaming(QDebug dbg, const QGeoShape &shape)
285{
286 QDebugStateSaver saver(dbg);
287 dbg.nospace() << "QGeoShape(";
288 switch (shape.type()) {
289 case QGeoShape::UnknownType:
290 dbg << "Unknown";
291 break;
292 case QGeoShape::RectangleType:
293 dbg << "Rectangle";
294 break;
295 case QGeoShape::PathType:
296 dbg << "Path";
297 break;
298 case QGeoShape::PolygonType:
299 dbg << "Polygon";
300 break;
301 case QGeoShape::CircleType:
302 dbg << "Circle";
303 }
304
305 dbg << ')';
306
307 return dbg;
308}
309#endif
310
311#ifndef QT_NO_DATASTREAM
312QDataStream &QGeoShape::dataStreamOut(QDataStream &stream, const QGeoShape &shape)
313{
314 stream << quint32(shape.type());
315 switch (shape.type()) {
316 case QGeoShape::UnknownType:
317 break;
318 case QGeoShape::RectangleType: {
319 QGeoRectangle r = shape;
320 stream << r.topLeft() << r.bottomRight();
321 break;
322 }
323 case QGeoShape::CircleType: {
324 QGeoCircle c = shape;
325 stream << c.center() << c.radius();
326 break;
327 }
328 case QGeoShape::PathType: {
329 QGeoPath p = shape;
330 stream << p.width();
331 stream << p.path().size();
332 for (const auto &c: p.path())
333 stream << c;
334 break;
335 }
336 case QGeoShape::PolygonType: {
337 QGeoPolygon p = shape;
338 stream << p.perimeter().size();
339 for (const auto &c: p.perimeter())
340 stream << c;
341 if (stream.version() >= QDataStream::Qt_6_10) {
342 // serialize holes
343 const qsizetype holesCount = p.holesCount();
344 stream << holesCount;
345 for (qsizetype i = 0; i < holesCount; ++i)
346 stream << p.holePath(i);
347 }
348 break;
349 }
350 }
351
352 return stream;
353}
354
355QDataStream &QGeoShape::dataStreamIn(QDataStream &stream, QGeoShape &shape)
356{
357 quint32 type;
358 stream >> type;
359
360 switch (type) {
361 case QGeoShape::UnknownType:
362 shape = QGeoShape();
363 break;
364 case QGeoShape::RectangleType: {
365 QGeoCoordinate tl;
366 QGeoCoordinate br;
367 stream >> tl >> br;
368 shape = QGeoRectangle(tl, br);
369 break;
370 }
371 case QGeoShape::CircleType: {
372 QGeoCoordinate c;
373 qreal r;
374 stream >> c >> r;
375 shape = QGeoCircle(c, r);
376 break;
377 }
378 case QGeoShape::PathType: {
379 QList<QGeoCoordinate> l;
380 QGeoCoordinate c;
381 qreal width;
382 stream >> width;
383 qsizetype sz;
384 stream >> sz;
385 for (qsizetype i = 0; i < sz; i++) {
386 stream >> c;
387 l.append(c);
388 }
389 shape = QGeoPath(l, width);
390 break;
391 }
392 case QGeoShape::PolygonType: {
393 QList<QGeoCoordinate> l;
394 QGeoCoordinate c;
395 qsizetype sz;
396 stream >> sz;
397 for (qsizetype i = 0; i < sz; i++) {
398 stream >> c;
399 l.append(c);
400 }
401 QGeoPolygon p(l);
402 if (stream.version() >= QDataStream::Qt_6_10) {
403 // deserialize holes
404 qsizetype holesCount = 0;
405 stream >> holesCount;
406 for (qsizetype i = 0; i < holesCount; ++i) {
407 QList<QGeoCoordinate> holePath;
408 stream >> holePath;
409 p.addHole(holePath);
410 }
411 }
412 shape = p;
413 break;
414 }
415 }
416
417 return stream;
418}
419#endif
420
421/*!
422 \relates QGeoShape
423
424 Returns the hash value for the \a shape, using \a seed for the
425 calculation.
426*/
427size_t qHash(const QGeoShape &shape, size_t seed) noexcept
428{
429 if (shape.d_ptr)
430 return shape.d_ptr->hash(seed);
431 else
432 return qHashMulti(seed, shape.type());
433}
434
435QT_END_NAMESPACE
436
437#include "moc_qgeoshape.cpp"
constexpr size_t qHash(const QSize &s, size_t seed=0) noexcept
Definition qsize.h:191