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 | /* ============================================================
*
* 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-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* ============================================================ */
#include "AbstractGeoPolygonGraphicsItem.h"
// Qt includes
#include <QtMath>
#include <QImageReader>
#include <QPixmapCache>
// Local includes
#include "GeoDataLinearRing.h"
#include "GeoDataPolygon.h"
#include "GeoDataBuilding.h"
#include "GeoPainter.h"
#include "GeoDataLatLonAltBox.h"
#include "GeoDataStyle.h"
#include "GeoDataIconStyle.h"
#include "GeoDataLineStyle.h"
#include "GeoDataPlacemark.h"
#include "GeoDataPolyStyle.h"
#include "OsmPlacemarkData.h"
#include "ViewportParams.h"
#include "digikam_debug.h"
namespace Marble
{
const void* AbstractGeoPolygonGraphicsItem::s_previousStyle = nullptr;
AbstractGeoPolygonGraphicsItem::AbstractGeoPolygonGraphicsItem(const GeoDataPlacemark* placemark, const GeoDataPolygon* polygon)
: GeoGraphicsItem(placemark),
m_polygon(polygon),
m_ring(nullptr),
m_building(nullptr)
{
}
AbstractGeoPolygonGraphicsItem::AbstractGeoPolygonGraphicsItem(const GeoDataPlacemark* placemark, const GeoDataLinearRing* ring)
: GeoGraphicsItem(placemark),
m_polygon(nullptr),
m_ring(ring),
m_building(nullptr)
{
}
AbstractGeoPolygonGraphicsItem::AbstractGeoPolygonGraphicsItem(const GeoDataPlacemark* placemark, const GeoDataBuilding* building)
: GeoGraphicsItem(placemark),
m_polygon(nullptr),
m_ring(nullptr),
m_building(building)
{
}
AbstractGeoPolygonGraphicsItem::~AbstractGeoPolygonGraphicsItem()
{
}
const GeoDataLatLonAltBox& AbstractGeoPolygonGraphicsItem::latLonAltBox() const
{
if (m_polygon)
{
return m_polygon->latLonAltBox();
}
else if (m_ring)
{
return m_ring->latLonAltBox();
}
return m_building->latLonAltBox();
}
void AbstractGeoPolygonGraphicsItem::paint(GeoPainter* painter, const ViewportParams* viewport, const QString& layer, int tileZoomLevel)
{
Q_UNUSED(layer);
Q_UNUSED(tileZoomLevel);
bool isValid = true;<--- Assignment 'isValid=true', assigned value is 1
if (s_previousStyle != style().data())
{
isValid = configurePainter(painter, *viewport);
}
s_previousStyle = style().data();
if (!isValid)<--- Condition '!isValid' is always false
{
return;
}
if (m_polygon)
{
bool innerResolved = false;
for (auto const& ring : m_polygon->innerBoundaries())
{
if (viewport->resolves(ring.latLonAltBox(), 4))
{
innerResolved = true;
break;
}
}
if (innerResolved)
{
painter->drawPolygon(*m_polygon);
}
else
{
painter->drawPolygon(m_polygon->outerBoundary());
}
}
else if (m_ring)
{
painter->drawPolygon(*m_ring);
}
}
bool AbstractGeoPolygonGraphicsItem::contains(const QPoint& screenPosition, const ViewportParams* viewport) const
{
auto const visualCategory = static_cast<const GeoDataPlacemark*>(feature())->visualCategory();
if (visualCategory == GeoDataPlacemark::Landmass ||
visualCategory == GeoDataPlacemark::UrbanArea ||
(visualCategory >= GeoDataPlacemark::LanduseAllotments && visualCategory <= GeoDataPlacemark::LanduseVineyard))
{
return false;
}
double lon, lat;
viewport->geoCoordinates(screenPosition.x(), screenPosition.y(), lon, lat, GeoDataCoordinates::Radian);
auto const coordinates = GeoDataCoordinates(lon, lat);
if (m_polygon)
{
return m_polygon->contains(coordinates);
}
else if (m_ring)
{
return m_ring->contains(coordinates);
}
return false;
}
bool AbstractGeoPolygonGraphicsItem::configurePainter(GeoPainter* painter, const ViewportParams& viewport) const
{
QPen currentPen = painter->pen();
GeoDataStyle::ConstPtr style = this->style();
if (!style)
{
painter->setPen(QPen()); // "style-less" polygons: a 1px black solid line
}
else
{
const GeoDataPolyStyle& polyStyle = style->polyStyle();
if (polyStyle.outline())
{
const GeoDataLineStyle& lineStyle = style->lineStyle();
// To save performance we avoid making changes to the painter's pen.
// So we first take a copy of the actual painter pen, make changes to it
// and only if the resulting pen is different from the actual pen
// we replace the painter's pen with our new pen.
// We want to avoid the mandatory detach in QPen::setColor(),
// so we carefully check whether applying the setter is needed
currentPen.setColor(lineStyle.paintedColor());
currentPen.setWidthF(lineStyle.width());
currentPen.setCapStyle(lineStyle.capStyle());
currentPen.setStyle(lineStyle.penStyle());
if (painter->pen().color() != currentPen.color())
{
painter->setPen(currentPen);
}
}
else
{
// polygons without outline: Qt::NoPen (not drawn)
if (currentPen.style() != Qt::NoPen)
{
painter->setPen(Qt::NoPen);
}
}
if (!polyStyle.fill())
{
painter->setBrush(Qt::transparent);
}
else
{
const QColor paintedColor = polyStyle.paintedColor();
if (painter->brush().color() != paintedColor ||
painter->brush().style() != polyStyle.brushStyle())
{
if (!polyStyle.texturePath().isEmpty() || !polyStyle.textureImage().isNull())
{
GeoDataCoordinates coords = latLonAltBox().center();
qreal x, y;
viewport.screenCoordinates(coords, x, y);
QBrush brush(texture(polyStyle.texturePath(), paintedColor));
painter->setBrush(brush);
painter->setBrushOrigin(QPoint(x, y));
}
else
{
painter->setBrush(QBrush(paintedColor, polyStyle.brushStyle()));
}
}
}
}
return true;
}
int AbstractGeoPolygonGraphicsItem::extractElevation(const GeoDataPlacemark& placemark)
{
int elevation = 0;
const OsmPlacemarkData& osmData = placemark.osmData();
const auto tagIter = osmData.findTag(QStringLiteral("ele"));
if (tagIter != osmData.tagsEnd())
{
elevation = tagIter.value().toInt();
}
return elevation;
}
QPixmap AbstractGeoPolygonGraphicsItem::texture(const QString& texturePath, const QColor& color) const
{
QString const key = QString::number(color.rgba()) + QLatin1Char('/') + texturePath;
QPixmap texture;
if (!QPixmapCache::find(key, &texture))
{
QImageReader imageReader(style()->polyStyle().resolvePath(texturePath));
texture = QPixmap::fromImageReader(&imageReader);
if (texture.hasAlphaChannel())
{
QPixmap pixmap(texture.size());
pixmap.fill(color);
QPainter imagePainter(&pixmap);
imagePainter.drawPixmap(0, 0, texture);
imagePainter.end();
texture = pixmap;
}
QPixmapCache::insert(key, texture);
}
return texture;
}
void AbstractGeoPolygonGraphicsItem::setLinearRing(GeoDataLinearRing* ring)
{
Q_ASSERT(m_building);
Q_ASSERT(!m_polygon);
m_ring = ring;
}
void AbstractGeoPolygonGraphicsItem::setPolygon(GeoDataPolygon* polygon)
{
Q_ASSERT(m_building);
Q_ASSERT(!m_ring);
m_polygon = polygon;
}
} // namespace Marble
|