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
qplace.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 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 "qplace.h"
5#include "qplace_p.h"
6
7#include <QtPositioning/QGeoLocation>
8
9#ifdef QPLACE_DEBUG
10#include <QDebug>
11#endif
12
13#include <QStringList>
14
16
17template<>
18QPlacePrivate *QSharedDataPointer<QPlacePrivate>::clone()
19{
20 return d->clone();
21}
22
23QT_DEFINE_QSDP_SPECIALIZATION_DTOR(QPlacePrivate)
24
25/*!
26 \class QPlace
27 \inmodule QtLocation
28 \ingroup QtLocation-places
29 \ingroup QtLocation-places-data
30 \since 5.6
31
32 \brief The QPlace class represents a set of data about a place.
33
34 \input place-definition.qdocinc
35
36 \section2 Contact Information
37 The contact information of a place is based around a common set of
38 \l {Contact Types}{contact types}. To retrieve all the phone numbers
39 of a place, one would do:
40
41 \snippet places/requesthandler.h Phone numbers
42
43 The contact types are string values by design to allow for providers
44 to introduce new contact types.
45
46 For convenience there are a set of functions which return the value
47 of the first contact detail of each type.
48 \list
49 \li QPlace::primaryPhone()
50 \li QPlace::primaryEmail()
51 \li QPlace::primaryWebsite()
52 \li QPlace::primaryFax()
53 \endlist
54
55 \section2 Extended Attributes
56 Places may have additional attributes which are not covered in the formal API.
57 Similar to contacts attributes are based around a common set of
58 \l {Attribute Types}{attribute types}. To retrieve an extended attribute one
59 would do:
60 \snippet places/requesthandler.h Opening hours
61
62 The attribute types are string values by design to allow providers
63 to introduce new attribute types.
64
65 \section2 Content
66 The QPlace object is only meant to be a convenient container to hold
67 rich content such as images, reviews and so on. Retrieval of content
68 should happen via QPlaceManager::getPlaceContent().
69
70 The content is stored as a QPlaceContent::Collection which contains
71 both the index of the content, as well as the content itself. This enables
72 developers to check whether a particular item has already been retrieved
73 and if not, then request that content.
74
75 \section3 Attribution
76 Places have a field for a rich text attribution string. Some providers
77 may require that the attribution be shown when a place is displayed
78 to a user.
79
80 \section2 Categories
81 Different categories may be assigned to a place to indicate that the place
82 is associated with those categories. When saving a place, the only meaningful
83 data is the category id, the rest of the category data is effectively ignored.
84 The category must already exist before saving the place (it is not possible
85 to create a new category, assign it to the place, save the place and expect
86 the category to be created).
87
88 \section2 Saving Caveats
89 \input place-caveats.qdocinc
90*/
91
92/*!
93 Constructs an empty place object.
94*/
95QPlace::QPlace()
96 : d_ptr(new QPlacePrivateDefault())
97{
98}
99
100/*!
101 Constructs an place object using \a dd as private implementation.
102*/
103QPlace::QPlace(const QSharedDataPointer<QPlacePrivate> &dd): d_ptr(dd)
104{
105}
106
107
108/*!
109 Constructs a copy of \a other.
110*/
111QPlace::QPlace(const QPlace &other) noexcept = default;
112
113/*!
114 Destroys this place.
115*/
116QPlace::~QPlace() = default;
117
118/*!
119 Assigns \a other to this place and returns a reference
120 to this place.
121*/
122QPlace &QPlace::operator= (const QPlace & other) noexcept
123{
124 if (this == &other)
125 return *this;
126
127 d_ptr = other.d_ptr;
128 return *this;
129}
130
131inline QPlacePrivate *QPlace::d_func()
132{
133 return static_cast<QPlacePrivate *>(d_ptr.data());
134}
135
136inline const QPlacePrivate *QPlace::d_func() const
137{
138 return static_cast<const QPlacePrivate *>(d_ptr.constData());
139}
140
141/*!
142 \fn bool QPlace::operator==(const QPlace &lhs, const QPlace &rhs) noexcept
143
144 Returns true if \a lhs is equal to \a rhs,
145 otherwise returns false.
146*/
147
148/*!
149 \fn bool QPlace::operator!=(const QPlace &lhs, const QPlace &rhs) noexcept
150
151 Returns true if \a lhs is not equal to \a rhs,
152 otherwise returns false.
153*/
154
155bool QPlace::isEqual(const QPlace &other) const noexcept
156{
157 return ( (d_ptr.constData() == other.d_ptr.constData())
158 || (*d_ptr) == (*other.d_ptr));
159}
160
161/*!
162 Returns categories that this place belongs to.
163*/
164QList<QPlaceCategory> QPlace::categories() const
165{
166 return d_ptr->categories();
167}
168
169/*!
170 Sets a single \a category that this place belongs to.
171*/
172void QPlace::setCategory(const QPlaceCategory &category)
173{
174 d_ptr->setCategories(QList<QPlaceCategory>());
175 d_ptr->setCategories(QList<QPlaceCategory>() << category);
176}
177
178/*!
179 Sets the \a categories that this place belongs to.
180*/
181void QPlace::setCategories(const QList<QPlaceCategory> &categories)
182{
183 d_ptr->setCategories(categories);
184}
185
186/*!
187 Returns the location of the place.
188*/
189QGeoLocation QPlace::location() const
190{
191 return d_ptr->location();
192}
193
194/*!
195 Sets the \a location of the place.
196*/
197void QPlace::setLocation(const QGeoLocation &location)
198{
199 d_ptr->setLocation(location);
200}
201
202/*!
203 Returns an aggregated rating of the place.
204*/
205QPlaceRatings QPlace::ratings() const
206{
207 return d_ptr->ratings();
208}
209
210/*!
211 Sets the aggregated \a rating of the place.
212*/
213void QPlace::setRatings(const QPlaceRatings &rating)
214{
215 d_ptr->setRatings(rating);
216}
217
218/*!
219 Returns the supplier of this place.
220*/
221QPlaceSupplier QPlace::supplier() const
222{
223 return d_ptr->supplier();
224}
225
226/*!
227 Sets the supplier of this place to \a supplier.
228*/
229void QPlace::setSupplier(const QPlaceSupplier &supplier)
230{
231 d_ptr->setSupplier(supplier);
232}
233
234/*!
235 Returns a collection of content associated with a place.
236 This collection is a map with the key being the index of the content object
237 and value being the content object itself.
238
239 The \a type specifies which kind of content is to be retrieved.
240*/
241QPlaceContent::Collection QPlace::content(QPlaceContent::Type type) const
242{
243 return d_ptr->m_contentCollections.value(type);
244}
245
246/*!
247 Sets a collection of \a content for the given \a type.
248*/
249void QPlace::setContent(QPlaceContent::Type type, const QPlaceContent::Collection &content)
250{
251 d_ptr->m_contentCollections.insert(type, content);
252}
253
254/*!
255 Adds a collection of \a content of the given \a type to the place. Any index in \a content
256 that already exists is overwritten.
257*/
258void QPlace::insertContent(QPlaceContent::Type type, const QPlaceContent::Collection &content)
259{
260 for (auto iter = content.cbegin(), end = content.cend(); iter != end; ++iter)
261 d_ptr->m_contentCollections[type].insert(iter.key(), iter.value());
262}
263
264/*!
265 Returns the total count of content objects of the given \a type.
266 This total count indicates how many the manager/provider should have available.
267 (As opposed to how many objects this place instance is currently assigned).
268
269 A negative count indicates that the total number of items is unknown.
270 By default the total content count is set to 0.
271*/
272int QPlace::totalContentCount(QPlaceContent::Type type) const
273{
274 return d_ptr->m_contentCounts.value(type, 0);
275}
276
277/*!
278 Sets the \a totalCount of content objects of the given \a type.
279*/
280void QPlace::setTotalContentCount(QPlaceContent::Type type, int totalCount)
281{
282 d_ptr->m_contentCounts.insert(type, totalCount);
283}
284
285/*!
286 Returns the name of the place.
287*/
288QString QPlace::name() const
289{
290 return d_ptr->name();
291}
292
293/*!
294 Sets the \a name of the place.
295*/
296void QPlace::setName(const QString &name)
297{
298 d_ptr->setName(name);
299}
300
301/*!
302 Returns the identifier of the place. The place identifier is only meaningful to the QPlaceManager that
303 generated it and is not transferable between managers. The place identifier is not guaranteed
304 to be universally unique, but unique for the manager that generated it.
305*/
306QString QPlace::placeId() const
307{
308 return d_ptr->placeId();
309}
310
311/*!
312 Sets the \a identifier of the place.
313*/
314void QPlace::setPlaceId(const QString &identifier)
315{
316 d_ptr->setPlaceId(identifier);
317}
318
319/*!
320 Returns a rich text attribution string of the place. Note, some providers may have a
321 requirement where the attribution must be shown whenever a place is displayed to an end user.
322*/
323QString QPlace::attribution() const
324{
325 return d_ptr->attribution();
326}
327
328/*!
329 Sets the \a attribution string of the place.
330*/
331void QPlace::setAttribution(const QString &attribution)
332{
333 d_ptr->setAttribution(attribution);
334}
335
336/*!
337 Returns the icon of the place.
338*/
339QPlaceIcon QPlace::icon() const
340{
341 return d_ptr->icon();
342}
343
344/*!
345 Sets the \a icon of the place.
346*/
347void QPlace::setIcon(const QPlaceIcon &icon)
348{
349 d_ptr->setIcon(icon);
350}
351
352/*!
353 Returns the primary phone number for this place. This accesses the first contact detail
354 of the \l {QPlaceContactDetail::Phone}{phone number type}. If no phone details exist, then an empty string is returned.
355*/
356QString QPlace::primaryPhone() const
357{
358 QList<QPlaceContactDetail> phoneNumbers = d_ptr->contacts().value(QPlaceContactDetail::Phone);
359 if (!phoneNumbers.isEmpty())
360 return phoneNumbers.at(0).value();
361 else
362 return QString();
363}
364
365/*!
366 Returns the primary fax number for this place. This convenience function accesses the first contact
367 detail of the \l {QPlaceContactDetail::Fax}{fax type}. If no fax details exist, then an empty string is returned.
368*/
369QString QPlace::primaryFax() const
370{
371 QList<QPlaceContactDetail> faxNumbers = d_ptr->contacts().value(QPlaceContactDetail::Fax);
372 if (!faxNumbers.isEmpty())
373 return faxNumbers.at(0).value();
374 else
375 return QString();
376}
377
378/*!
379 Returns the primary email address for this place. This convenience function accesses the first
380 contact detail of the \l {QPlaceContactDetail::Email}{email type}. If no email addresses exist, then
381 an empty string is returned.
382*/
383QString QPlace::primaryEmail() const
384{
385 QList<QPlaceContactDetail> emailAddresses = d_ptr->contacts().value(QPlaceContactDetail::Email);
386 if (!emailAddresses.isEmpty())
387 return emailAddresses.at(0).value();
388 else
389 return QString();
390}
391
392/*!
393 Returns the primary website of the place. This convenience function accesses the first
394 contact detail of the \l {QPlaceContactDetail::Website}{website type}. If no websites exist,
395 then an empty string is returned.
396*/
397QUrl QPlace::primaryWebsite() const
398{
399 QList<QPlaceContactDetail> websites = d_ptr->contacts().value(QPlaceContactDetail::Website);
400 if (!websites.isEmpty())
401 return QUrl(websites.at(0).value());
402 else
403 return QString();
404}
405
406/*!
407 Returns true if the details of this place have been fetched,
408 otherwise returns false.
409*/
410bool QPlace::detailsFetched() const
411{
412 return d_ptr->detailsFetched();
413}
414
415/*!
416 Sets whether the details of this place have been \a fetched or not.
417*/
418void QPlace::setDetailsFetched(bool fetched)
419{
420 d_ptr->setDetailsFetched(fetched);
421}
422
423/*!
424 Returns the types of extended attributes that this place has.
425*/
426QStringList QPlace::extendedAttributeTypes() const
427{
428 return d_ptr->extendedAttributes().keys();
429}
430
431/*!
432 Returns the exteded attribute corresponding to the specified \a attributeType.
433 If the place does not have that particular attribute type, a default constructed
434 QPlaceExtendedAttribute is returned.
435*/
436QPlaceAttribute QPlace::extendedAttribute(const QString &attributeType) const
437{
438 return d_ptr->extendedAttribute(attributeType);
439}
440
441/*!
442 Assigns an \a attribute of the given \a attributeType to a place. If the given \a attributeType
443 already exists in the place, then it is overwritten.
444
445 If \a attribute is a default constructed QPlaceAttribute, then the \a attributeType
446 is removed from the place which means it will not be listed by QPlace::extendedAttributeTypes().
447*/
448void QPlace::setExtendedAttribute(const QString &attributeType,
449 const QPlaceAttribute &attribute)
450{
451 if (attribute == QPlaceAttribute())
452 d_ptr->extendedAttributes().remove(attributeType);
453 else
454 d_ptr->extendedAttributes().insert(attributeType, attribute);
455}
456
457/*!
458 Remove the attribute of \a attributeType from the place.
459
460 The attribute will no longer be listed by QPlace::extendedAttributeTypes()
461*/
462void QPlace::removeExtendedAttribute(const QString &attributeType)
463{
464 setExtendedAttribute(attributeType, QPlaceAttribute());
465}
466
467/*!
468 Returns the type of contact details this place has.
469
470 See QPlaceContactDetail for a list of common \l {QPlaceContactDetail::Email}{contact types}.
471*/
472QStringList QPlace::contactTypes() const
473{
474 return d_ptr->contacts().keys();
475}
476
477/*!
478 Returns a list of contact details of the specified \a contactType.
479
480 See QPlaceContactDetail for a list of common \l {QPlaceContactDetail::Email}{contact types}.
481*/
482QList<QPlaceContactDetail> QPlace::contactDetails(const QString &contactType) const
483{
484 return d_ptr->contacts().value(contactType);
485}
486
487/*!
488 Sets the contact \a details of a specified \a contactType.
489
490 If \a details is empty, then the \a contactType is removed from the place such
491 that it is no longer returned by QPlace::contactTypes().
492
493 See QPlaceContactDetail for a list of common \l {QPlaceContactDetail::Email}{contact types}.
494*/
495void QPlace::setContactDetails(const QString &contactType, QList<QPlaceContactDetail> details)
496{
497 if (details.isEmpty())
498 d_ptr->contacts().remove(contactType);
499 else
500 d_ptr->contacts().insert(contactType, details);
501}
502
503/*!
504 Appends a contact \a detail of a specified \a contactType.
505
506 See QPlaceContactDetail for a list of common \l {QPlaceContactDetail::Email}{contact types}.
507*/
508void QPlace::appendContactDetail(const QString &contactType, const QPlaceContactDetail &detail)
509{
510 QList<QPlaceContactDetail> details = d_ptr->contacts().value(contactType);
511 details.append(detail);
512 d_ptr->contacts().insert(contactType, details);
513}
514
515/*!
516 Removes all the contact details of a given \a contactType.
517
518 The \a contactType is no longer returned when QPlace::contactTypes() is called.
519*/
520void QPlace::removeContactDetails(const QString &contactType)
521{
522 d_ptr->contacts().remove(contactType);
523}
524
525/*!
526 Sets the visibility of the place to \a visibility.
527*/
528void QPlace::setVisibility(QLocation::Visibility visibility)
529{
530 d_ptr->setVisibility(visibility);
531}
532
533/*!
534 Returns the visibility of the place.
535
536 The default visibility of a new place is set to QtLocatin::Unspecified visibility.
537 If a place is saved with unspecified visibility the backend chooses an appropriate
538 default visibility to use when saving.
539*/
540QLocation::Visibility QPlace::visibility() const
541{
542 return d_ptr->visibility();
543}
544
545/*!
546 Returns a boolean indicating whether the all the fields of the place are empty or not.
547*/
548bool QPlace::isEmpty() const
549{
550 return d_ptr->isEmpty();
551}
552
553/*******************************************************************************
554*******************************************************************************/
555
556QPlacePrivate::QPlacePrivate()
557: QSharedData()
558{
559}
560
561QPlacePrivate::QPlacePrivate(const QPlacePrivate &other)
562 : QSharedData(other),
563 m_contentCollections(other.m_contentCollections),
564 m_contentCounts(other.m_contentCounts)
565{
566}
567
568QPlacePrivate::~QPlacePrivate() {}
569
570bool QPlacePrivate::operator== (const QPlacePrivate &other) const
571{
572 return (categories() == other.categories()
573 && location() == other.location()
574 && ratings() == other.ratings()
575 && supplier() == other.supplier()
576 && m_contentCollections == other.m_contentCollections
577 && m_contentCounts == other.m_contentCounts
578 && name() == other.name()
579 && placeId() == other.placeId()
580 && attribution() == other.attribution()
581 && contacts() == other.contacts()
582 && extendedAttributes() == other.extendedAttributes()
583 && visibility() == other.visibility()
584 && icon() == other.icon()
585 );
586}
587
588
589bool QPlacePrivate::isEmpty() const
590{
591 return (categories().isEmpty()
592 && location().isEmpty()
593 && ratings().isEmpty()
594 && supplier().isEmpty()
595 && m_contentCollections.isEmpty()
596 && m_contentCounts.isEmpty()
597 && name().isEmpty()
598 && placeId().isEmpty()
599 && attribution().isEmpty()
600 && contacts().isEmpty()
601 && extendedAttributes().isEmpty()
602 && QLocation::UnspecifiedVisibility == visibility()
603 && icon().isEmpty()
604 );
605}
606
607QPlaceAttribute QPlacePrivate::extendedAttribute(const QString &attributeType) const
608{
609 return extendedAttributes().value(attributeType);
610}
611
612
613
614//
615// Default implementation
616//
617
618QPlacePrivateDefault::QPlacePrivateDefault()
619 : QPlacePrivate(), m_visibility(QLocation::UnspecifiedVisibility), m_detailsFetched(false)
620{
621}
622
623QPlacePrivateDefault::QPlacePrivateDefault(const QPlacePrivateDefault &other)
624 : QPlacePrivate(other),
625 m_categories(other.m_categories),
626 m_location(other.m_location),
627 m_ratings(other.m_ratings),
628 m_supplier(other.m_supplier),
629 m_name(other.m_name),
630 m_placeId(other.m_placeId),
631 m_attribution(other.m_attribution),
632 m_extendedAttributes(other.m_extendedAttributes),
633 m_contacts(other.m_contacts),
634 m_visibility(other.m_visibility),
635 m_icon(other.m_icon),
636 m_detailsFetched(other.m_detailsFetched)
637{
638}
639
640QPlacePrivateDefault::~QPlacePrivateDefault()
641{
642}
643
644QPlacePrivate *QPlacePrivateDefault::clone()
645{
646 return new QPlacePrivateDefault(*this);
647}
648
649QList<QPlaceCategory> QPlacePrivateDefault::categories() const
650{
651 return m_categories;
652}
653
654void QPlacePrivateDefault::setCategories(const QList<QPlaceCategory> &categories)
655{
656 m_categories = categories;
657}
658
659QGeoLocation QPlacePrivateDefault::location() const
660{
661 return m_location;
662}
663
664void QPlacePrivateDefault::setLocation(const QGeoLocation &location)
665{
666 m_location = location;
667}
668
669QPlaceRatings QPlacePrivateDefault::ratings() const
670{
671 return m_ratings;
672}
673
674void QPlacePrivateDefault::setRatings(const QPlaceRatings &ratings)
675{
676 m_ratings = ratings;
677}
678
679QPlaceSupplier QPlacePrivateDefault::supplier() const
680{
681 return m_supplier;
682}
683
684void QPlacePrivateDefault::setSupplier(const QPlaceSupplier &supplier)
685{
686 m_supplier = supplier;
687}
688
689QString QPlacePrivateDefault::name() const
690{
691 return m_name;
692}
693
694void QPlacePrivateDefault::setName(const QString &name)
695{
696 m_name = name;
697}
698
699QString QPlacePrivateDefault::placeId() const
700{
701 return m_placeId;
702}
703
704void QPlacePrivateDefault::setPlaceId(const QString &placeIdentifier)
705{
706 m_placeId = placeIdentifier;
707}
708
709QString QPlacePrivateDefault::attribution() const
710{
711 return m_attribution;
712}
713
714void QPlacePrivateDefault::setAttribution(const QString &attribution)
715{
716 m_attribution = attribution;
717}
718
719QLocation::Visibility QPlacePrivateDefault::visibility() const
720{
721 return m_visibility;
722}
723
724void QPlacePrivateDefault::setVisibility(QLocation::Visibility visibility)
725{
726 m_visibility = visibility;
727}
728
729QPlaceIcon QPlacePrivateDefault::icon() const
730{
731 return m_icon;
732}
733
734void QPlacePrivateDefault::setIcon(const QPlaceIcon &icon)
735{
736 m_icon = icon;
737}
738
739bool QPlacePrivateDefault::detailsFetched() const
740{
741 return m_detailsFetched;
742}
743
744void QPlacePrivateDefault::setDetailsFetched(bool fetched)
745{
746 m_detailsFetched = fetched;
747}
748
749QMap<QString, QPlaceAttribute> QPlacePrivateDefault::extendedAttributes() const
750{
751 return m_extendedAttributes;
752}
753
754QMap<QString, QPlaceAttribute> &QPlacePrivateDefault::extendedAttributes()
755{
756 return m_extendedAttributes;
757}
758
759QMap<QString, QList<QPlaceContactDetail> > QPlacePrivateDefault::contacts() const
760{
761 return m_contacts;
762}
763
764QMap<QString, QList<QPlaceContactDetail> > &QPlacePrivateDefault::contacts()
765{
766 return m_contacts;
767}
768
769
770
771QT_END_NAMESPACE
Combined button and popup list for selecting options.