1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2023-05-15
 * Description : geolocation engine based on Marble.
 *               (c) 2007-2022 Marble Team
 *               https://invent.kde.org/education/marble/-/raw/master/data/credits_authors.html
 *
 * SPDX-FileCopyrightText: 2023-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * ============================================================ */

#include "GeoDataIconStyle.h"

// Qt includes

#include <QUrl>
#include <QImageReader>

// Local includes

#include "MarbleDirs.h"
#include "RemoteIconLoader.h"
#include "GeoDataTypes.h"
#include "digikam_debug.h"

namespace Marble
{

class Q_DECL_HIDDEN GeoDataIconStylePrivate
{
public:

    GeoDataIconStylePrivate()
        : m_scale(1.0),
          m_size(0, 0),
          m_aspectRatioMode(Qt::KeepAspectRatio),
          m_iconPath(),
          m_heading(0)
    {
    }

    GeoDataIconStylePrivate(const QString& iconPath, const QPointF& hotSpot)
        : m_scale(1.0),
          m_size(0, 0),
          m_aspectRatioMode(Qt::KeepAspectRatio),
          m_iconPath(iconPath),
          m_hotSpot(hotSpot),
          m_heading(0)
    {
    }

    RemoteIconLoader* remoteIconLoader() const
    {
        static RemoteIconLoader* remoteIconLoader = new RemoteIconLoader();
        return remoteIconLoader;
    }

    QSize scaledSize(const QSize& size) const
    {
        QSize iconSize = size.isNull() ? m_icon.size() : size;

        // Scaling the placemark's icon based on its size, scale, and maximum icon size.
        if (iconSize.width()*m_scale > s_maximumIconSize.width()
            || iconSize.height()*m_scale > s_maximumIconSize.height())
        {
            iconSize.scale(s_maximumIconSize, Qt::KeepAspectRatio);
        }

        else if (iconSize.width()*m_scale < s_minimumIconSize.width()
                 || iconSize.height()*m_scale < s_minimumIconSize.width())
        {
            iconSize.scale(s_minimumIconSize, Qt::KeepAspectRatio);
        }

        else
        {
            iconSize *= m_scale;
        }

        return QSize(iconSize.width() - iconSize.width() % 2,
                     iconSize.height() - iconSize.height() % 2);
    }

    QImage loadIcon(const QString& path, const QSize& size) const
    {
        if (!path.isEmpty())
        {
            // Icons from the local file system
            if (!size.isNull())
            {
                QImageReader imageReader;
                imageReader.setFileName(path);
                auto const imageSize = imageReader.size();
                auto const finalSize = imageSize.scaled(size, m_aspectRatioMode);
                imageReader.setScaledSize(finalSize);
                QImage icon = imageReader.read();

                if (icon.isNull())
                {
                    qCDebug(DIGIKAM_GEOENGINE_LOG) << "GeoDataIconStyle: Failed to read image " << path << ": " << imageReader.errorString();
                }

                return icon;
            }

            QImage icon = QImage(path);

            if (!icon.isNull())
            {
                return icon;
            }
        }

        if (QUrl(m_iconPath).isValid())
        {
            // if image is not found on disk, check whether the icon is
            // at remote location. If yes then go for remote icon loading
            return remoteIconLoader()->load(QUrl(m_iconPath));
        }

        qCDebug(DIGIKAM_GEOENGINE_LOG) << "Unable to open style icon at: " << path;
        return QImage();
    }

    float            m_scale;

    QImage           m_icon;
    QSize            m_size;
    Qt::AspectRatioMode m_aspectRatioMode;
    QImage           m_scaledIcon;
    QString          m_iconPath;
    GeoDataHotSpot   m_hotSpot;
    int              m_heading;
};

GeoDataIconStyle::GeoDataIconStyle() :
    d(new GeoDataIconStylePrivate())
{
}

GeoDataIconStyle::GeoDataIconStyle(const GeoDataIconStyle& other) :
    GeoDataColorStyle(other), d(new GeoDataIconStylePrivate(*other.d))
{
}

GeoDataIconStyle::GeoDataIconStyle(const QString& iconPath, const QPointF& hotSpot) :
    d(new GeoDataIconStylePrivate(iconPath, hotSpot))
{
}

GeoDataIconStyle::~GeoDataIconStyle()
{
    delete d;
}

GeoDataIconStyle& GeoDataIconStyle::operator=(const GeoDataIconStyle& other)
{
    GeoDataColorStyle::operator=(other);
    *d = *other.d;
    return *this;
}

bool GeoDataIconStyle::operator==(const GeoDataIconStyle& other) const
{
    if (GeoDataColorStyle::operator!=(other))
    {
        return false;
    }

    return d->m_scale == other.d->m_scale &&
           d->m_icon == other.d->m_icon &&
           d->m_size == other.d->m_size &&
           d->m_iconPath == other.d->m_iconPath &&
           d->m_hotSpot == other.d->m_hotSpot &&
           d->m_heading == other.d->m_heading;
}

bool GeoDataIconStyle::operator!=(const GeoDataIconStyle& other) const
{
    return !this->operator==(other);
}

const char* GeoDataIconStyle::nodeType() const
{
    return GeoDataTypes::GeoDataIconStyleType;
}

void GeoDataIconStyle::setIcon(const QImage& icon)<--- Shadow argument
{
    d->m_icon = icon;
    d->m_scaledIcon = QImage();
}

void GeoDataIconStyle::setIconPath(const QString& filename)
{
    d->m_iconPath = filename;

    /**
     * Set the m_icon to be a default-constructed icon
     * so that m_icon is null and icon() doesn't return
     * previously loaded icon.
     */
    d->m_icon = QImage();
    d->m_scaledIcon = QImage();
}

QString GeoDataIconStyle::iconPath() const
{
    return d->m_iconPath;
}

QImage GeoDataIconStyle::icon() const
{
    if (!d->m_icon.isNull())
    {
        return d->m_icon;
    }

    else if (!d->m_iconPath.isEmpty())
    {
        d->m_icon = d->loadIcon(resolvePath(d->m_iconPath), d->m_size);
        return d->m_icon;
    }

    else
    {
        return QImage();
    }
}

void GeoDataIconStyle::setHotSpot(const QPointF& hotSpot,<--- Shadow argument
                                  GeoDataHotSpot::Units xunits,
                                  GeoDataHotSpot::Units yunits)
{
    d->m_hotSpot.setHotSpot(hotSpot, xunits, yunits);
}

QPointF GeoDataIconStyle::hotSpot(GeoDataHotSpot::Units& xunits, GeoDataHotSpot::Units& yunits) const
{
    return d->m_hotSpot.hotSpot(xunits, yunits);
}

void GeoDataIconStyle::setSize(const QSize& size, Qt::AspectRatioMode aspectRatioMode)<--- Shadow argument
{
    if (size == d->m_size && aspectRatioMode == d->m_aspectRatioMode)
    {
        return;
    }

    d->m_aspectRatioMode = aspectRatioMode;
    d->m_size = QSize(size.width() - size.width() % 2, size.height() - size.height() % 2);

    if (!d->m_size.isNull() && !d->m_icon.isNull())
    {
        // Resize existing icon that cannot be restored from an image path
        d->m_icon = d->m_icon.scaled(d->m_size);
    }

    else if (!d->m_iconPath.isEmpty())
    {
        // Lazily reload the icons
        d->m_icon = QImage();
        d->m_scaledIcon = QImage();
    }
}

QSize GeoDataIconStyle::size() const
{
    return d->m_size;
}

void GeoDataIconStyle::setScale(float scale)<--- Shadow argument
{
    d->m_scale = scale;
    d->m_scaledIcon = QImage();
}

float GeoDataIconStyle::scale() const
{
    return d->m_scale;
}

QImage GeoDataIconStyle::scaledIcon() const
{
    if (!d->m_scaledIcon.isNull())
    {
        return d->m_scaledIcon;
    }

    // Invalid or trivial scale
    if (d->m_scale <= 0 || d->m_scale == 1.0)
    {
        return icon();
    }

    // Try to load it
    d->m_scaledIcon = d->loadIcon(resolvePath(d->m_iconPath), d->scaledSize(d->m_size));

    if (d->m_scaledIcon.isNull())
    {
        // Direct loading failed, try to scale the icon as a last resort
        QImage const image = icon();

        if (!image.isNull())
        {
            QSize iconSize = d->scaledSize(image.size());
            d->m_scaledIcon = image.scaled(iconSize, Qt::KeepAspectRatio, Qt::SmoothTransformation) ;
        }
    }

    return d->m_scaledIcon;
}

int GeoDataIconStyle::heading() const
{
    return d->m_heading;
}

void GeoDataIconStyle::setHeading(int heading)<--- Shadow argument
{
    d->m_heading = heading;
}

RemoteIconLoader* GeoDataIconStyle::remoteIconLoader() const
{
    return d->remoteIconLoader();
}

void GeoDataIconStyle::pack(QDataStream& stream) const
{
    GeoDataColorStyle::pack(stream);

    stream << d->m_scale;
    stream << d->m_icon;
    d->m_hotSpot.pack(stream);
}

void GeoDataIconStyle::unpack(QDataStream& stream)
{
    GeoDataColorStyle::unpack(stream);

    stream >> d->m_scale;
    stream >> d->m_icon;
    d->m_hotSpot.unpack(stream);
}

} // namespace Marble