1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtLocation module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL3$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or later as published by the Free
28 ** Software Foundation and appearing in the file LICENSE.GPL included in
29 ** the packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 2.0 requirements will be
31 ** met: http://www.gnu.org/licenses/gpl-2.0.html.
32 **
33 ** $QT_END_LICENSE$
34 **
35 ****************************************************************************/
36 
37 #include "qplaceicon.h"
38 #include "qplaceicon_p.h"
39 #include "qplacemanager.h"
40 #include "qplacemanagerengine.h"
41 
42 QT_USE_NAMESPACE
43 
QPlaceIconPrivate()44 QPlaceIconPrivate::QPlaceIconPrivate()
45     : QSharedData(), manager(0)
46 {
47 }
48 
QPlaceIconPrivate(const QPlaceIconPrivate & other)49 QPlaceIconPrivate::QPlaceIconPrivate(const QPlaceIconPrivate &other)
50     : QSharedData(other),
51       manager(other.manager),
52       parameters(other.parameters)
53 {
54 }
55 
~QPlaceIconPrivate()56 QPlaceIconPrivate::~QPlaceIconPrivate()
57 {
58 }
59 
operator =(const QPlaceIconPrivate & other)60 QPlaceIconPrivate &QPlaceIconPrivate::operator=(const QPlaceIconPrivate &other)
61 {
62     if (this == &other)
63         return *this;
64 
65     manager = other.manager;
66     parameters = other.parameters;
67 
68     return *this;
69 }
70 
operator ==(const QPlaceIconPrivate & other) const71 bool QPlaceIconPrivate::operator == (const QPlaceIconPrivate &other) const
72 {
73     return manager == other.manager
74             && parameters == other.parameters;
75 }
76 
77 /*!
78     \class QPlaceIcon
79     \inmodule QtLocation
80     \ingroup QtLocation-places
81     \ingroup QtLocation-places-data
82     \since 5.6
83 
84     \brief The QPlaceIcon class represents an icon.
85 
86     The typical usage of an icon is to use the url() function to specify
87     a preferred icon size.
88 
89     \snippet places/requesthandler.h icon
90 
91     The icons are typically backend dependent, if a manager backend does not support a given size, the URL of the icon that most
92     closely matches those parameters is returned.
93 
94     The icon class also has a key-value set of parameters.  The precise key one
95     needs to use depends on the \l {Qt Location#Plugin References and Parameters}{plugin}
96     being used.  These parameters influence which icon URL is returned by
97     the manager and may also be used to specify icon URL locations when
98     saving icons.
99 
100     If there is only ever one image for an icon, then QPlaceIcon::SingleUrl can be used as a parameter
101     key with a QUrl as the associated value.  If this key is set, then the url() function will always return the specified URL
102     and not defer to any manager.
103 */
104 
105 /*!
106     \variable QPlaceIcon::SingleUrl
107     \brief Parameter key for an icon that only has a single image URL.
108 
109     The parameter value to be used with this key is a QUrl.  An icon with this parameter set will
110     always return the specified URL regardless of the requested size when url() is called.
111 */
112 const QString QPlaceIcon::SingleUrl(QLatin1String("singleUrl"));
113 
114 /*!
115     Constructs an icon.
116 */
QPlaceIcon()117 QPlaceIcon::QPlaceIcon()
118     : d(new QPlaceIconPrivate)
119 {
120 }
121 
122 /*!
123     Constructs a copy of \a other.
124 */
QPlaceIcon(const QPlaceIcon & other)125 QPlaceIcon::QPlaceIcon(const QPlaceIcon &other)
126     : d(other.d)
127 {
128 }
129 
130 /*!
131     Destroys the icon.
132 */
~QPlaceIcon()133 QPlaceIcon::~QPlaceIcon()
134 {
135 }
136 
137 /*!
138     Assigns \a other to this icon and returns a reference to this icon.
139 */
operator =(const QPlaceIcon & other)140 QPlaceIcon &QPlaceIcon::operator=(const QPlaceIcon &other)
141 {
142     if (this == &other)
143         return *this;
144 
145     d = other.d;
146     return *this;
147 }
148 
149 /*!
150     Returns true if this icon is equal to \a other, otherwise returns false.
151 */
operator ==(const QPlaceIcon & other) const152 bool QPlaceIcon::operator==(const QPlaceIcon &other) const
153 {
154     return *d == *(other.d);
155 }
156 
157 /*!
158     \fn QPlaceIcon::operator!=(const QPlaceIcon &other) const
159 
160     Returns true if \a other is not equal to this icon, otherwise returns false.
161 */
162 
163 /*!
164     Returns an icon URL according to the given \a size.
165 
166     If no manager has been assigned to the icon, and the parameters do not contain the QPlaceIcon::SingleUrl key, a default constructed QUrl
167     is returned.
168 */
url(const QSize & size) const169 QUrl QPlaceIcon::url(const QSize &size) const
170 {
171     if (d->parameters.contains(QPlaceIcon::SingleUrl)) {
172         QVariant value = d->parameters.value(QPlaceIcon::SingleUrl);
173         if (value.type() == QVariant::Url)
174             return value.toUrl();
175         else if (value.type() == QVariant::String)
176             return QUrl::fromUserInput(value.toString());
177 
178         return QUrl();
179     }
180 
181     if (!d->manager)
182         return QUrl();
183 
184     return d->manager->d->constructIconUrl(*this, size);
185 }
186 
187 /*!
188     Returns a set of parameters for the icon that are manager/plugin specific.
189     These parameters are used by the manager to return the appropriate
190     URL when url() is called and to specify locations to save to
191     when saving icons.
192 
193     Consult the \l {Qt Location#Plugin References and Parameters}{plugin documentation}
194     for what parameters are supported and how they should be used.
195 */
parameters() const196 QVariantMap QPlaceIcon::parameters() const
197 {
198     return d->parameters;
199 }
200 
201 /*!
202     Sets the parameters of the icon to \a parameters.
203 */
setParameters(const QVariantMap & parameters)204 void QPlaceIcon::setParameters(const QVariantMap &parameters)
205 {
206     d->parameters = parameters;
207 }
208 
209 /*!
210     Returns the manager that this icon is associated with.
211 */
manager() const212 QPlaceManager *QPlaceIcon::manager() const
213 {
214     return d->manager;
215 }
216 
217 /*!
218     Sets the \a manager that this icon is associated with.  The icon does not take
219     ownership of the pointer.
220 */
setManager(QPlaceManager * manager)221 void QPlaceIcon::setManager(QPlaceManager *manager)
222 {
223     d->manager = manager;
224 }
225 
226 /*!
227     Returns a boolean indicating whether the all the fields of the icon are empty or not.
228 */
isEmpty() const229 bool QPlaceIcon::isEmpty() const
230 {
231     return (d->manager == 0
232             && d->parameters.isEmpty());
233 }
234