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 Returns the coordinate located at the geometric center of the geo shape.
229
230 \since 5.5
231*/
232QGeoCoordinate QGeoShape::center() const
233{
234 Q_D(const QGeoShape);
235
236 if (d)
237 return d->center();
238 else
239 return QGeoCoordinate();
240}
241
242/*!
243 \fn bool QGeoShape::operator==(const QGeoShape &lhs, const QGeoShape &rhs)
244
245 Returns \c true if the \a lhs geo shape is equivalent to the \a rhs geo
246 shape, otherwise returns \c false.
247*/
248
249/*!
250 \fn bool QGeoShape::operator!=(const QGeoShape &lhs, const QGeoShape &rhs)
251
252 Returns \c true if the \a lhs geo shape is not equivalent to the \a rhs geo
253 shape, otherwise returns \c false.
254*/
255
256/*!
257 Assigns \a other to this geo shape and returns a reference to this geo shape.
258*/
259QGeoShape &QGeoShape::operator=(const QGeoShape &other)
260{
261 if (this == &other)
262 return *this;
263
264 d_ptr = other.d_ptr;
265 return *this;
266}
267
268/*!
269 Returns a string representation of this geo shape.
270
271 \since 5.5
272*/
273QString QGeoShape::toString() const
274{
275 return QStringLiteral("QGeoShape(%1)").arg(type());
276}
277
278#ifndef QT_NO_DEBUG_STREAM
279QDebug QGeoShape::debugStreaming(QDebug dbg, const QGeoShape &shape)
280{
281 QDebugStateSaver saver(dbg);
282 dbg.nospace() << "QGeoShape(";
283 switch (shape.type()) {
284 case QGeoShape::UnknownType:
285 dbg << "Unknown";
286 break;
287 case QGeoShape::RectangleType:
288 dbg << "Rectangle";
289 break;
290 case QGeoShape::PathType:
291 dbg << "Path";
292 break;
293 case QGeoShape::PolygonType:
294 dbg << "Polygon";
295 break;
296 case QGeoShape::CircleType:
297 dbg << "Circle";
298 }
299
300 dbg << ')';
301
302 return dbg;
303}
304#endif
305
306#ifndef QT_NO_DATASTREAM
307QDataStream &QGeoShape::dataStreamOut(QDataStream &stream, const QGeoShape &shape)
308{
309 stream << quint32(shape.type());
310 switch (shape.type()) {
311 case QGeoShape::UnknownType:
312 break;
313 case QGeoShape::RectangleType: {
314 QGeoRectangle r = shape;
315 stream << r.topLeft() << r.bottomRight();
316 break;
317 }
318 case QGeoShape::CircleType: {
319 QGeoCircle c = shape;
320 stream << c.center() << c.radius();
321 break;
322 }
323 case QGeoShape::PathType: {
324 QGeoPath p = shape;
325 stream << p.width();
326 stream << p.path().size();
327 for (const auto &c: p.path())
328 stream << c;
329 break;
330 }
331 case QGeoShape::PolygonType: {
332 QGeoPolygon p = shape;
333 stream << p.perimeter().size();
334 for (const auto &c: p.perimeter())
335 stream << c;
336 break;
337 }
338 }
339
340 return stream;
341}
342
343QDataStream &QGeoShape::dataStreamIn(QDataStream &stream, QGeoShape &shape)
344{
345 quint32 type;
346 stream >> type;
347
348 switch (type) {
349 case QGeoShape::UnknownType:
350 shape = QGeoShape();
351 break;
352 case QGeoShape::RectangleType: {
353 QGeoCoordinate tl;
354 QGeoCoordinate br;
355 stream >> tl >> br;
356 shape = QGeoRectangle(tl, br);
357 break;
358 }
359 case QGeoShape::CircleType: {
360 QGeoCoordinate c;
361 qreal r;
362 stream >> c >> r;
363 shape = QGeoCircle(c, r);
364 break;
365 }
366 case QGeoShape::PathType: {
367 QList<QGeoCoordinate> l;
368 QGeoCoordinate c;
369 qreal width;
370 stream >> width;
371 qsizetype sz;
372 stream >> sz;
373 for (qsizetype i = 0; i < sz; i++) {
374 stream >> c;
375 l.append(c);
376 }
377 shape = QGeoPath(l, width);
378 break;
379 }
380 case QGeoShape::PolygonType: {
381 QList<QGeoCoordinate> l;
382 QGeoCoordinate c;
383 qsizetype sz;
384 stream >> sz;
385 for (qsizetype i = 0; i < sz; i++) {
386 stream >> c;
387 l.append(c);
388 }
389 shape = QGeoPolygon(l);
390 break;
391 }
392 }
393
394 return stream;
395}
396#endif
397
398/*!
399 \relates QGeoShape
400
401 Returns the hash value for the \a shape, using \a seed for the
402 calculation.
403*/
404size_t qHash(const QGeoShape &shape, size_t seed) noexcept
405{
406 if (shape.d_ptr)
407 return shape.d_ptr->hash(seed);
408 else
409 return qHashMulti(seed, shape.type());
410}
411
412QT_END_NAMESPACE
413
414#include "moc_qgeoshape.cpp"
constexpr size_t qHash(const QSize &s, size_t seed=0) noexcept
Definition qsize.h:191