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
/* ============================================================
 *
 * 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 "VisiblePlacemark.h"

// Qt includes

#include <QApplication>
#include <QPainter>
#include <QPainterPath>
#include <QPalette>
#include <QPixmapCache>

// Local includes

#include "RemoteIconLoader.h"
#include "GeoDataPlacemark.h"
#include "GeoDataStyle.h"
#include "GeoDataIconStyle.h"
#include "GeoDataLabelStyle.h"
#include "PlacemarkLayer.h"
#include "digikam_debug.h"

namespace Marble
{

VisiblePlacemark::VisiblePlacemark(const GeoDataPlacemark* placemark, const GeoDataCoordinates& coordinates, const GeoDataStyle::ConstPtr& style)
    : m_placemark(placemark),
      m_selected(false),
      m_labelDirty(true),
      m_style(style),
      m_coordinates(coordinates)
{
    const RemoteIconLoader* remoteLoader = style->iconStyle().remoteIconLoader();
    QObject::connect(remoteLoader, SIGNAL(iconReady()),
                     this, SLOT(setSymbolPixmap()));

    setSymbolPixmap();
}

const GeoDataPlacemark* VisiblePlacemark::placemark() const
{
    return m_placemark;
}

const QPixmap& VisiblePlacemark::symbolPixmap() const
{
    if (!m_symbolId.isEmpty() && m_symbolPixmap.isNull())
    {
        if (!QPixmapCache::find(m_symbolId, &m_symbolPixmap))
        {
            m_symbolPixmap = QPixmap::fromImage(m_style->iconStyle().scaledIcon());
            QPixmapCache::insert(m_symbolId, m_symbolPixmap);
        }
    }

    return m_symbolPixmap;
}

const QString& VisiblePlacemark::symbolId() const
{
    return m_symbolId;
}

bool VisiblePlacemark::selected() const
{
    return m_selected;
}

void VisiblePlacemark::setSelected(bool selected)<--- Shadow argument
{
    if (selected != m_selected)
    {
        m_selected = selected;
        m_labelDirty = true;
    }
}

const QPointF& VisiblePlacemark::symbolPosition() const
{
    return m_symbolPosition;
}

const QPointF VisiblePlacemark::hotSpot() const
{
    const QSize iconSize = m_style->iconStyle().scaledIcon().size();

    GeoDataHotSpot::Units xunits;
    GeoDataHotSpot::Units yunits;
    QPointF pixelHotSpot = m_style->iconStyle().hotSpot(xunits, yunits);

    switch (xunits)
    {
        case GeoDataHotSpot::Fraction:
            pixelHotSpot.setX(iconSize.width() * pixelHotSpot.x());
            break;

        case GeoDataHotSpot::Pixels:
            /* nothing to do */
            break;

        case GeoDataHotSpot::InsetPixels:
            pixelHotSpot.setX(iconSize.width() - pixelHotSpot.x());
            break;
    }

    switch (yunits)
    {
        case GeoDataHotSpot::Fraction:
            pixelHotSpot.setY(iconSize.height() * (1.0 - pixelHotSpot.y()));
            break;

        case GeoDataHotSpot::Pixels:
            /* nothing to do */
            break;

        case GeoDataHotSpot::InsetPixels:
            pixelHotSpot.setY(iconSize.height() - pixelHotSpot.y());
            break;
    }

    return pixelHotSpot;
}

void VisiblePlacemark::setSymbolPosition(const QPointF& position)
{
    m_symbolPosition = position;
}

const QPixmap& VisiblePlacemark::labelPixmap()
{
    if (m_labelDirty)
    {
        drawLabelPixmap();
    }

    return m_labelPixmap;
}

void VisiblePlacemark::setSymbolPixmap()
{
    if (m_style)
    {
        m_symbolId = m_style->iconStyle().iconPath() + QString::number(m_style->iconStyle().scale());

        if (m_style->iconStyle().iconPath().isEmpty())
        {
            m_symbolId.clear();
            m_symbolPixmap = QPixmap::fromImage(m_style->iconStyle().scaledIcon());
        }

        Q_EMIT updateNeeded();
    }

    else
    {
        qCDebug(DIGIKAM_GEOENGINE_LOG) << "Style pointer is Null";
    }
}

const QRectF& VisiblePlacemark::labelRect() const
{
    return m_labelRect;
}

void VisiblePlacemark::setLabelRect(const QRectF& labelRect)<--- Shadow argument
{
    m_labelRect = labelRect;
}

void VisiblePlacemark::setStyle(const GeoDataStyle::ConstPtr& style)<--- Shadow argument
{
    m_style = style;
    m_labelDirty = true;
    setSymbolPixmap();
}

GeoDataStyle::ConstPtr VisiblePlacemark::style() const
{
    return m_style;
}

QRectF VisiblePlacemark::symbolRect() const
{
    return QRectF(m_symbolPosition, m_symbolPixmap.size());
}

QRectF VisiblePlacemark::boundingBox() const
{
    return m_labelRect.isEmpty() ? symbolRect() : m_labelRect.united(symbolRect());
}

const GeoDataCoordinates& VisiblePlacemark::coordinates() const
{
    return m_coordinates;
}

void VisiblePlacemark::drawLabelPixmap()
{
    m_labelDirty = false;
    QString labelName = m_placemark->displayName();

    if (labelName.isEmpty() || m_style->labelStyle().color() == QColor(Qt::transparent))
    {
        m_labelPixmap = QPixmap();
        return;
    }

    QFont  labelFont  = m_style->labelStyle().scaledFont();
    QColor labelColor = m_style->labelStyle().color();

    LabelStyle labelStyle = Normal;

    if (m_selected)
    {
        labelStyle = Selected;
    }

    else if (m_style->labelStyle().glow())
    {
        labelStyle = Glow;
    }

    int textHeight = QFontMetrics(labelFont).height();

    int textWidth;

    if (m_style->labelStyle().glow())
    {
        labelFont.setWeight(QFont::Bold);   // Needed to calculate the correct pixmap size;
        textWidth = (QFontMetrics(labelFont).horizontalAdvance(labelName)
                     + qRound(2 * s_labelOutlineWidth));
    }

    else
    {
        textWidth = (QFontMetrics(labelFont).horizontalAdvance(labelName));
    }

    m_labelPixmap = QPixmap(QSize(textWidth, textHeight));
    m_labelPixmap.fill(Qt::transparent);

    QPainter labelPainter(&m_labelPixmap);

    drawLabelText(labelPainter, labelName, labelFont, labelStyle, labelColor);
}

void VisiblePlacemark::drawLabelText(QPainter& labelPainter, const QString& text,
                                     const QFont& labelFont, LabelStyle labelStyle, const QColor& color)
{
    QFont font = labelFont;
    QFontMetrics metrics = QFontMetrics(font);
    int fontAscent = metrics.ascent();

    switch (labelStyle)
    {
        case Selected:
        {
            labelPainter.setPen(color);
            labelPainter.setFont(font);
            QRect textRect(0, 0, metrics.horizontalAdvance(text), metrics.height());
            labelPainter.fillRect(textRect, QApplication::palette().highlight());
            labelPainter.setPen(QPen(QApplication::palette().highlightedText(), 1));
            labelPainter.drawText(0, fontAscent, text);
            break;
        }

        case Glow:
        {
            font.setWeight(QFont::Bold);
            fontAscent = QFontMetrics(font).ascent();

            QPen outlinepen((color.red() + color.green() + color.blue()) / 3 < 160 ? Qt::white : Qt::black);
            outlinepen.setWidthF(s_labelOutlineWidth);
            QBrush  outlinebrush(color);

            QPainterPath outlinepath;

            const QPointF  baseline(s_labelOutlineWidth / 2.0, fontAscent);
            outlinepath.addText(baseline, font, text);
            labelPainter.setRenderHint(QPainter::Antialiasing, true);
            labelPainter.setPen(outlinepen);
            labelPainter.setBrush(outlinebrush);
            labelPainter.drawPath(outlinepath);
            labelPainter.setPen(Qt::NoPen);
            labelPainter.drawPath(outlinepath);
            labelPainter.setRenderHint(QPainter::Antialiasing, false);
            break;
        }

        default:
        {
            labelPainter.setPen(color);
            labelPainter.setFont(font);
            labelPainter.drawText(0, fontAscent, text);
        }
    }
}

} // namespace Marble

#include "moc_VisiblePlacemark.cpp"