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