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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630 | /* ============================================================
*
* 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 "MarbleAbstractPresenter.h"
// Qt includes
#include <QtMath>
// Local includes
#include "Quaternion.h"
#include "ViewportParams.h"
#include "MarbleLocale.h"
#include "MarbleMap.h"
#include "MarbleModel.h"
#include "Planet.h"
#include "GeoDataGeometry.h"
#include "GeoDataLatLonAltBox.h"
#include "GeoDataPlacemark.h"
#include "GeoDataLookAt.h"
#include "MarbleClock.h"
#include "digikam_debug.h"
namespace Marble
{
MarbleAbstractPresenter::MarbleAbstractPresenter(MarbleMap* map, QObject* parent)
: QObject (parent)
, m_map (map)
, m_physics (this)
, m_animationsEnabled (false)
, m_logzoom (0)
, m_zoomStep (MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen ? 60 : 40)
, m_viewAngle (110)
{
}
MarbleAbstractPresenter::~MarbleAbstractPresenter()
{
}
qreal MarbleAbstractPresenter::zoom(qreal radius) const<--- Shadow argument
{
return (200.0 * log(radius));
}
qreal MarbleAbstractPresenter::radius(qreal zoom) const<--- Shadow argument
{
return pow(M_E, (zoom / 200.0));
}
void MarbleAbstractPresenter::rotateBy(const qreal deltaLon, const qreal deltaLat, FlyToMode mode)
{
GeoDataLookAt target = lookAt();
GeoDataCoordinates coords(map()->viewport()->centerLongitude(), map()->viewport()->centerLatitude());
GeoDataCoordinates movedCoords = coords.moveByBearing(-map()->heading() * DEG2RAD, -deltaLat * DEG2RAD);
movedCoords = movedCoords.moveByBearing((-map()->heading() - 90) * DEG2RAD,
-deltaLon * DEG2RAD * map()->viewport()->polarity());
target.setLongitude(movedCoords.longitude());
target.setLatitude(movedCoords.latitude());
flyTo(target, mode);
}
void MarbleAbstractPresenter::flyTo(const GeoDataLookAt& newLookAt, FlyToMode mode)
{
if (!m_animationsEnabled || (mode == Instant))
{
const int radius = qRound(radiusFromDistance(newLookAt.range() * METER2KM));<--- Shadow local variable
qreal const zoomVal = zoom(radius);
// Prevent exceeding zoom range. Note: Bounding to range is not useful here
if ((qRound(zoomVal) >= minimumZoom()) && (qRound(zoomVal) <= maximumZoom()))
{
map()->setRadius(radius);
m_logzoom = qRound(zoom(radius));
GeoDataCoordinates::Unit deg = GeoDataCoordinates::Degree;
map()->centerOn(newLookAt.longitude(deg), newLookAt.latitude(deg));
Q_EMIT zoomChanged(m_logzoom);
Q_EMIT distanceChanged(distanceString());
}
}
else
{
m_physics.flyTo(newLookAt, mode);
}
}
QString MarbleAbstractPresenter::distanceString() const
{
// distance() returns data in km, so translating to meters
qreal dist = distance() * KM2METER, convertedDistance;
MarbleLocale::MeasureUnit unit;
MarbleLocale* locale = MarbleGlobal::getInstance()->locale();
locale->meterToTargetUnit(dist, locale->measurementSystem(), convertedDistance, unit);
QString unitString = locale->unitAbbreviation(unit);
return QString::fromUtf8("%L1 %2").arg(convertedDistance, 8, 'f', 1, QLatin1Char(' '))
.arg(unitString);
}
GeoDataLookAt MarbleAbstractPresenter::lookAt() const
{
GeoDataLookAt result;
result.setLongitude(map()->viewport()->centerLongitude());
result.setLatitude(map()->viewport()->centerLatitude());
result.setAltitude(0.0);
result.setRange(distance() * KM2METER);
return result;
}
qreal MarbleAbstractPresenter::distance() const
{
return distanceFromRadius(radius());
}
qreal MarbleAbstractPresenter::distanceFromRadius(qreal radius) const<--- Shadow argument
{
// Due to Marble's orthographic projection ("we have no focus")
// it's actually not possible to calculate a "real" distance.
// Additionally the viewing angle of the earth doesn't adjust to
// the window's size.
//
// So the only possible workaround is to come up with a distance
// definition which gives a reasonable approximation of
// reality. Therefore we assume that the average window width
// (about 800 pixels) equals the viewing angle of a human being.
return (model()->planet()->radius() * 0.4
/ radius / tan(0.5 * m_viewAngle * DEG2RAD));
}
qreal MarbleAbstractPresenter::radiusFromDistance(qreal distance) const<--- Shadow argument
{
return (model()->planet()->radius() /
(distance * tan(0.5 * m_viewAngle * DEG2RAD) / 0.4));
}
int MarbleAbstractPresenter::polarity() const
{
return map()->viewport()->polarity();
}
int MarbleAbstractPresenter::zoom() const
{
return m_logzoom;
}
int MarbleAbstractPresenter::minimumZoom() const
{
return map()->minimumZoom();
}
int MarbleAbstractPresenter::maximumZoom() const
{
return map()->maximumZoom();
}
void MarbleAbstractPresenter::setZoom(int newZoom, FlyToMode mode)
{
// It won't fly anyway. So we should do everything to keep the zoom value.
if (!m_animationsEnabled || (mode == Instant))
{
// Check for under and overflow.
if (newZoom < minimumZoom())
{
newZoom = minimumZoom();
}
else if (newZoom > maximumZoom())
{
newZoom = maximumZoom();
}
// Prevent infinite loops.
if (newZoom == m_logzoom)
{
return;
}
map()->setRadius(radius(newZoom));
m_logzoom = newZoom;
Q_EMIT zoomChanged(m_logzoom);
Q_EMIT distanceChanged(distanceString());
}
else
{
GeoDataLookAt target = lookAt();
target.setRange(KM2METER * distanceFromZoom(newZoom));
flyTo(target, mode);
}
}
void MarbleAbstractPresenter::zoomView(int zoom, FlyToMode mode)<--- Shadow argument
{
setZoom(zoom, mode);
}
void MarbleAbstractPresenter::zoomViewBy(int zoomStep, FlyToMode mode)<--- Shadow argument
{
setZoom(zoom() + zoomStep, mode);
}
void MarbleAbstractPresenter::zoomIn(FlyToMode mode)
{
if (map()->tileZoomLevel() < 0)
{
zoomViewBy(m_zoomStep, mode);
}
else
{
qreal radiusVal = map()->preferredRadiusCeil(map()->radius() / 0.95);
radiusVal = qBound(radius(minimumZoom()), radiusVal, radius(maximumZoom()));
GeoDataLookAt target = lookAt();
target.setRange(KM2METER * distanceFromRadius(radiusVal));
flyTo(target, mode);
}
}
void MarbleAbstractPresenter::zoomOut(FlyToMode mode)
{
if (map()->tileZoomLevel() <= 0)
{
zoomViewBy(-m_zoomStep, mode);
}
else
{
qreal radiusVal = map()->preferredRadiusFloor(map()->radius() * 0.95);
radiusVal = qBound(radius(minimumZoom()), radiusVal, radius(maximumZoom()));
GeoDataLookAt target = lookAt();
target.setRange(KM2METER * distanceFromRadius(radiusVal));
flyTo(target, mode);
}
}
void MarbleAbstractPresenter::zoomAtBy(const QPoint& pos, int zoomStep)<--- Shadow argument
{
qreal radiusVal;
if (map()->tileZoomLevel() <= 0)
{
radiusVal = radius(zoom() + zoomStep);
}
else
{
radiusVal = zoomStep > 0 ? map()->preferredRadiusCeil(map()->radius() / 0.95) :
map()->preferredRadiusFloor(map()->radius() * 0.95);
radiusVal = qBound(radius(minimumZoom()), radiusVal, radius(maximumZoom()));
}
zoomAt(pos, distanceFromRadius(radiusVal));
}
qreal MarbleAbstractPresenter::distanceFromZoom(qreal zoom) const<--- Shadow argument
{
return distanceFromRadius(radius(zoom));
}
qreal MarbleAbstractPresenter::zoomFromDistance(qreal distance) const<--- Shadow argument
{
return zoom(radiusFromDistance(distance));
}
void MarbleAbstractPresenter::goHome(FlyToMode mode)
{
qreal homeLon = 0;
qreal homeLat = 0;
int homeZoom = 0;
model()->home(homeLon, homeLat, homeZoom);
GeoDataLookAt target;
target.setLongitude(homeLon, GeoDataCoordinates::Degree);
target.setLatitude(homeLat, GeoDataCoordinates::Degree);
target.setRange(1000 * distanceFromZoom(homeZoom));
flyTo(target, mode);
}
void MarbleAbstractPresenter::moveByStep(int stepsRight, int stepsDown, FlyToMode mode)
{
int polarity = map()->viewport()->polarity();<--- Shadow local variable
qreal left = polarity * stepsRight * moveStep();
qreal down = stepsDown * moveStep();
rotateBy(left, down, mode);
}
qreal MarbleAbstractPresenter::moveStep() const
{
int width = map()->width();
int height = map()->height();
if (radius() < qSqrt((qreal)(width * width + height * height)))
{
return (180.0 * 0.1);
}
else
{
return (180.0 * qAtan((qreal)width
/ (qreal)(2 * radius())) * 0.2);
}
}
int MarbleAbstractPresenter::radius() const
{
return map()->radius();
}
void MarbleAbstractPresenter::setRadius(int radiusVal)
{
Q_ASSERT(radiusVal >= 0);
bool adjustRadius = radiusVal != map()->radius();
qreal const zoomVal = zoom(radiusVal);
// Prevent exceeding zoom range
if (zoomVal < minimumZoom())
{
radiusVal = radius(minimumZoom());
adjustRadius = true;
}
else if (zoomVal > maximumZoom())
{
radiusVal = radius(maximumZoom());
adjustRadius = true;
}
if (adjustRadius)
{
map()->setRadius(radiusVal);
m_logzoom = qRound(zoomVal);
Q_EMIT zoomChanged(m_logzoom);
Q_EMIT distanceChanged(distanceString());
}
}
// NOTE: Moved from MarbleWidgetInputHandlerPrivate - fits more here now
void MarbleAbstractPresenter::zoomAt(const QPoint& pos, qreal newDistance)
{
Q_ASSERT(newDistance > 0.0);
qreal destLat;
qreal destLon;
if (!map()->geoCoordinates(pos.x(), pos.y(), destLon, destLat, GeoDataCoordinates::Degree))
{
return;
}
const ViewportParams* now = map()->viewport();
qreal x(0), y(0);
if (!now->screenCoordinates(destLon * DEG2RAD, destLat * DEG2RAD, x, y))
{
return;
}
ViewportParams soon;
soon.setProjection(now->projection());
soon.centerOn(now->centerLongitude(), now->centerLatitude());
soon.setSize(now->size());
qreal newRadius = radiusFromDistance(newDistance);
soon.setRadius(newRadius);
qreal mouseLon, mouseLat;
if (!soon.geoCoordinates(int(x), int(y), mouseLon, mouseLat, GeoDataCoordinates::Degree))
{
return;
}
const qreal lon = destLon - (mouseLon - map()->centerLongitude());
const qreal lat = destLat - (mouseLat - map()->centerLatitude());
GeoDataLookAt lookAt;<--- Shadow local variable
lookAt.setLongitude(lon, GeoDataCoordinates::Degree);
lookAt.setLatitude(lat, GeoDataCoordinates::Degree);
lookAt.setAltitude(0.0);
lookAt.setRange(newDistance * KM2METER);
map()->viewport()->setFocusPoint(GeoDataCoordinates(destLon, destLat, 0, GeoDataCoordinates::Degree));
flyTo(lookAt, Linear);
}
void MarbleAbstractPresenter::moveTo(const QPoint& pos, qreal factor)
{
Q_ASSERT(factor > 0.0);
qreal destLat;
qreal destLon;
map()->geoCoordinates(pos.x(), pos.y(), destLon, destLat, GeoDataCoordinates::Radian);
GeoDataLookAt lookAt;<--- Shadow local variable
lookAt.setLongitude(destLon);
lookAt.setLatitude(destLat);
lookAt.setAltitude(0.0);
lookAt.setRange(distance() * factor * KM2METER);
flyTo(lookAt);
}
void MarbleAbstractPresenter::centerOn(const qreal lon, const qreal lat, bool animated)
{
GeoDataCoordinates target(lon, lat, 0.0, GeoDataCoordinates::Degree);
centerOn(target, animated);
}
void MarbleAbstractPresenter::centerOn(const GeoDataCoordinates& position, bool animated)
{
GeoDataLookAt target = lookAt();
target.setCoordinates(position);
flyTo(target, animated ? Automatic : Instant);
}
void MarbleAbstractPresenter::centerOn(const GeoDataLatLonBox& box, bool animated)
{
if (box.isEmpty())
{
return;
}
int newRadius = radius();
const ViewportParams* viewparams = map()->viewport();
// prevent divide by zero
if (box.height() && box.width())
{
// work out the needed zoom level
int const horizontalRadius = (0.25 * M_PI) * (viewparams->height() / box.height());
int const verticalRadius = (0.25 * M_PI) * (viewparams->width() / box.width());
newRadius = qMin<int>(horizontalRadius, verticalRadius);
newRadius = qMax<int>(radius(minimumZoom()), qMin<int>(newRadius, radius(maximumZoom())));
}
// move the map
GeoDataLookAt target;
target.setCoordinates(box.center());
target.setAltitude(box.center().altitude());
target.setRange(KM2METER * distanceFromRadius(newRadius));
flyTo(target, animated ? Automatic : Instant);
}
void MarbleAbstractPresenter::centerOn(const GeoDataPlacemark& placemark, bool animated)
{
const GeoDataLookAt* lookAt(placemark.lookAt());<--- Shadow local variable
if (lookAt)
{
flyTo(*lookAt, animated ? Automatic : Instant);
}
else
{
bool icon = false;
GeoDataCoordinates coords = placemark.coordinate(model()->clock()->dateTime(), &icon);
if (icon)
{
centerOn(coords, animated);
}
else
{
centerOn(placemark.geometry()->latLonAltBox(), animated);
}
}
}
void MarbleAbstractPresenter::headingOn(qreal heading)
{
map()->setHeading(heading);
}
void MarbleAbstractPresenter::setCenterLatitude(qreal lat, FlyToMode mode)
{
centerOn(centerLongitude(), lat, mode);
}
void MarbleAbstractPresenter::setCenterLongitude(qreal lon, FlyToMode mode)
{
centerOn(lon, centerLatitude(), mode);
}
qreal MarbleAbstractPresenter::centerLatitude() const
{
return map()->centerLatitude();
}
qreal MarbleAbstractPresenter::centerLongitude() const
{
return map()->centerLongitude();
}
ViewContext MarbleAbstractPresenter::viewContext() const
{
return map()->viewContext();
}
void MarbleAbstractPresenter::setViewContext(ViewContext viewContext)<--- Shadow argument
{
map()->setViewContext(viewContext);
}
bool MarbleAbstractPresenter::animationsEnabled() const
{
return m_animationsEnabled;
}
void MarbleAbstractPresenter::setAnimationsEnabled(bool enabled)
{
m_animationsEnabled = enabled;
}
int MarbleAbstractPresenter::logzoom() const
{
return m_logzoom;
}
void MarbleAbstractPresenter::setLogzoom(int value)
{
m_logzoom = value;
}
int MarbleAbstractPresenter::zoomStep() const
{
return m_zoomStep;
}
qreal MarbleAbstractPresenter::viewAngle() const
{
return m_viewAngle;
}
MarbleMap* MarbleAbstractPresenter::map()
{
return m_map;
}
const MarbleMap* MarbleAbstractPresenter::map() const
{
return m_map;
}
MarbleModel* MarbleAbstractPresenter::model()
{
return m_map->model();
}
const MarbleModel* MarbleAbstractPresenter::model() const
{
return m_map->model();
}
ViewportParams* MarbleAbstractPresenter::viewport()
{
return map()->viewport();
}
const ViewportParams* MarbleAbstractPresenter::viewport() const
{
return map()->viewport();
}
void MarbleAbstractPresenter::setDistance(qreal newDistance)
{
qreal minDistance = 0.001;
if (newDistance <= minDistance)
{
qCDebug(DIGIKAM_GEOENGINE_LOG) << "Invalid distance: 0 m";
newDistance = minDistance;
}
int newRadius = radiusFromDistance(newDistance);
setRadius(newRadius);
}
void MarbleAbstractPresenter::setSelection(const QRect& region)
{
QPoint tl = region.topLeft();
QPoint br = region.bottomRight();
qCDebug(DIGIKAM_GEOENGINE_LOG) << "Selection region: (" << tl.x() << ", " << tl.y() << ") ("
<< br.x() << ", " << br.y() << ")" << Qt::endl;
const GeoDataLatLonAltBox box = viewport()->latLonAltBox(region);
Q_EMIT regionSelected(box);
}
} // namespace Marble
#include "moc_MarbleAbstractPresenter.cpp"
|