/
map_view.cpp
546 lines (465 loc) · 15 KB
/
map_view.cpp
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
/*
* Copyright 2012, 2013 Thomas Schöps
* Copyright 2014-2016 Kai Pastor
*
* This file is part of OpenOrienteering.
*
* OpenOrienteering is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenOrienteering is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenOrienteering. If not, see <http://www.gnu.org/licenses/>.
*/
#include "map_view.h"
#include <algorithm>
#include <cmath>
#include <iterator>
#include <stdexcept>
#include <QRectF>
#include <Qt>
#include <QIODevice>
#include <QLatin1String>
#include <QString>
#include <QStringRef>
#include <QXmlStreamReader>
#include "core/map.h"
#include "core/map_coord.h"
#include "gui/util_gui.h"
#include "templates/template.h" // IWYU pragma: keep
#include "util/util.h"
#include "util/xml_stream_util.h"
namespace literal
{
static const QLatin1String zoom("zoom");
static const QLatin1String rotation("rotation");
static const QLatin1String position_x("position_x");
static const QLatin1String position_y("position_y");
static const QLatin1String grid("grid");
static const QLatin1String overprinting_simulation_enabled("overprinting_simulation_enabled");
static const QLatin1String map("map");
static const QLatin1String opacity("opacity");
static const QLatin1String visible("visible");
static const QLatin1String templates("templates");
static const QLatin1String hidden("hidden");
static const QLatin1String ref("ref");
static const QLatin1String template_string("template");
}
namespace OpenOrienteering {
const double MapView::zoom_in_limit = 512;
const double MapView::zoom_out_limit = 1 / 16.0;
MapView::MapView(QObject* parent, Map* map)
: QObject { parent }
, map { map }
, zoom { 1.0 }
, rotation { 0.0 }
, map_visibility{ 1.0f, true }
, all_templates_hidden{ false }
, grid_visible{ false }
, overprinting_simulation_enabled{ false }
{
Q_ASSERT(map);
updateTransform();
connect(map, &Map::templateAdded, this, &MapView::onTemplateAdded);
connect(map, &Map::templateDeleted, this, &MapView::onTemplateDeleted, Qt::QueuedConnection);
}
MapView::MapView(Map* map)
: MapView { map, map }
{
// nothing else
}
MapView::~MapView()
{
// nothing, not inlined
}
#ifndef NO_NATIVE_FILE_FORMAT
void MapView::load(QIODevice* file, int version)
{
qint64 center_x, center_y;
int unused;
file->read((char*)&zoom, sizeof(double));
file->read((char*)&rotation, sizeof(double));
file->read((char*)¢er_x, sizeof(qint64));
file->read((char*)¢er_y, sizeof(qint64));
file->read((char*)&unused /*view_x*/, sizeof(int));
file->read((char*)&unused /*view_y*/, sizeof(int));
file->read((char*)&pan_offset, sizeof(QPoint));
try
{
center_pos = MapCoord::fromNative64withOffset(center_x, center_y);
}
catch (std::range_error&)
{
// leave center_pos unchanged
}
updateTransform();
if (version >= 26)
{
file->read((char*)&map_visibility.visible, sizeof(bool));
file->read((char*)&map_visibility.opacity, sizeof(float));
}
int num_template_visibilities;
file->read((char*)&num_template_visibilities, sizeof(int));
for (int i = 0; i < num_template_visibilities; ++i)
{
int pos;
file->read((char*)&pos, sizeof(int));
TemplateVisibility vis;
file->read((char*)&vis.visible, sizeof(bool));
file->read((char*)&vis.opacity, sizeof(float));
setTemplateVisibilityHelper(map->getTemplate(pos), vis);
}
if (version >= 29)
file->read((char*)&all_templates_hidden, sizeof(bool));
if (version >= 24)
file->read((char*)&grid_visible, sizeof(bool));
emit viewChanged(CenterChange | ZoomChange | RotationChange);
emit visibilityChanged(MultipleFeatures, true);
}
#endif
void MapView::save(QXmlStreamWriter& xml, const QLatin1String& element_name, bool template_details) const
{
XmlElementWriter mapview_element(xml, element_name);
mapview_element.writeAttribute(literal::zoom, zoom);
mapview_element.writeAttribute(literal::rotation, rotation);
mapview_element.writeAttribute(literal::position_x, center_pos.nativeX());
mapview_element.writeAttribute(literal::position_y, center_pos.nativeY());
mapview_element.writeAttribute(literal::grid, grid_visible);
mapview_element.writeAttribute(literal::overprinting_simulation_enabled, overprinting_simulation_enabled);
{
XmlElementWriter map_element(xml, literal::map);
map_element.writeAttribute(literal::opacity, map_visibility.opacity);
map_element.writeAttribute(literal::visible, map_visibility.visible);
}
{
XmlElementWriter templates_element(xml, literal::templates);
templates_element.writeAttribute(literal::hidden, all_templates_hidden);
if (template_details)
{
templates_element.writeAttribute(XmlStreamLiteral::count, template_visibilities.size());
for (auto entry : template_visibilities)
{
XmlElementWriter ref_element(xml, literal::ref);
ref_element.writeAttribute(literal::template_string, map->findTemplateIndex(entry.temp));
ref_element.writeAttribute(literal::visible, entry.visible);
ref_element.writeAttribute(literal::opacity, entry.opacity);
}
}
}
}
void MapView::load(QXmlStreamReader& xml)
{
XmlElementReader mapview_element(xml);
zoom = qMin(mapview_element.attribute<double>(literal::zoom), zoom_in_limit);
if (zoom < zoom_out_limit)
zoom = 1.0;
rotation = mapview_element.attribute<double>(literal::rotation);
auto center_x = mapview_element.attribute<qint64>(literal::position_x);
auto center_y = mapview_element.attribute<qint64>(literal::position_y);
try
{
center_pos = MapCoord::fromNative64withOffset(center_x, center_y);
}
catch (std::range_error&)
{
// leave center_pos unchanged
}
updateTransform();
grid_visible = mapview_element.attribute<bool>(literal::grid);
overprinting_simulation_enabled = mapview_element.attribute<bool>(literal::overprinting_simulation_enabled);
while (xml.readNextStartElement())
{
if (xml.name() == literal::map)
{
XmlElementReader map_element(xml);
map_visibility.opacity = map_element.attribute<float>(literal::opacity);
if (map_element.hasAttribute(literal::visible))
map_visibility.visible = map_element.attribute<bool>(literal::visible);
else
map_visibility.visible = true;
}
else if (xml.name() == literal::templates)
{
XmlElementReader templates_element(xml);
auto num_template_visibilities = templates_element.attribute<unsigned int>(XmlStreamLiteral::count);
template_visibilities.reserve(qBound(20u, num_template_visibilities, 1000u));
all_templates_hidden = templates_element.attribute<bool>(literal::hidden);
while (xml.readNextStartElement())
{
if (xml.name() == literal::ref)
{
XmlElementReader ref_element(xml);
int pos = ref_element.attribute<int>(literal::template_string);
if (pos >= 0 && pos < map->getNumTemplates())
{
TemplateVisibility vis {
qBound(0.0f, ref_element.attribute<float>(literal::opacity), 1.0f),
ref_element.attribute<bool>(literal::visible)
};
setTemplateVisibilityHelper(map->getTemplate(pos), vis);
}
}
else
xml.skipCurrentElement();
}
}
else
xml.skipCurrentElement(); // unsupported
}
emit viewChanged(CenterChange | ZoomChange | RotationChange);
emit visibilityChanged(MultipleFeatures, true);
}
void MapView::updateAllMapWidgets()
{
emit visibilityChanged(MultipleFeatures, true);
}
MapCoord MapView::viewToMap(double x, double y) const
{
return MapCoord(view_to_map.m11() * x + view_to_map.m12() * y + view_to_map.m13(),
view_to_map.m21() * x + view_to_map.m22() * y + view_to_map.m23());
}
MapCoordF MapView::viewToMapF(double x, double y) const
{
return MapCoordF(view_to_map.m11() * x + view_to_map.m12() * y + view_to_map.m13(),
view_to_map.m21() * x + view_to_map.m22() * y + view_to_map.m23());
}
QPointF MapView::mapToView(MapCoord coords) const
{
return QPointF(map_to_view.m11() * coords.x() + map_to_view.m12() * coords.y() + map_to_view.m13(),
map_to_view.m21() * coords.x() + map_to_view.m22() * coords.y() + map_to_view.m23());
}
QPointF MapView::mapToView(MapCoordF coords) const
{
return QPointF(map_to_view.m11() * coords.x() + map_to_view.m12() * coords.y() + map_to_view.m13(),
map_to_view.m21() * coords.x() + map_to_view.m22() * coords.y() + map_to_view.m23());
}
qreal MapView::lengthToPixel(qreal length) const
{
return Util::mmToPixelPhysical(zoom * length / 1000.0);
}
qreal MapView::pixelToLength(qreal pixel) const
{
return Util::pixelToMMPhysical(pixel / zoom) * 1000.0;
}
QRectF MapView::calculateViewedRect(QRectF rect) const
{
auto top_left = viewToMapF(rect.topLeft());
auto top_right = viewToMapF(rect.topRight());
auto bottom_right = viewToMapF(rect.bottomRight());
auto bottom_left = viewToMapF(rect.bottomLeft());
rect = QRectF{ top_left, bottom_right }.normalized();
rectInclude(rect, top_right);
rectInclude(rect, bottom_left);
rect.adjust(-0.001, -0.001, +0.001, +0.001);
return rect;
}
QRectF MapView::calculateViewBoundingBox(QRectF rect) const
{
auto top_left = mapToView(static_cast<MapCoordF>(rect.topLeft()));
auto top_right = mapToView(static_cast<MapCoordF>(rect.topRight()));
auto bottom_right = mapToView(static_cast<MapCoordF>(rect.bottomRight()));
auto bottom_left = mapToView(static_cast<MapCoordF>(rect.bottomLeft()));
rect = QRectF{ top_left, bottom_right }.normalized();
rectInclude(rect, top_right);
rectInclude(rect, bottom_left);
rect.adjust(-1.0, -1.0, +1.0, +1.0);
return rect;
}
void MapView::setPanOffset(QPoint offset)
{
if (offset != pan_offset)
{
pan_offset = offset;
emit panOffsetChanged(offset);
}
}
void MapView::finishPanning(QPoint offset)
{
setPanOffset({0,0});
try
{
auto rotated_offset = MapCoord::fromNative64(qRound64(-pixelToLength(offset.x())),
qRound64(-pixelToLength(offset.y())) );
auto rotated_offset_f = MapCoordF{ rotated_offset };
rotated_offset_f.rotate(-rotation);
auto move = MapCoord{ rotated_offset_f };
setCenter(center() + move);
}
catch (std::range_error&)
{
// Do nothing
}
}
void MapView::zoomSteps(double num_steps, QPointF cursor_pos_view)
{
auto zoom_to = getZoom() * pow(sqrt(2.0), num_steps);
setZoom(zoom_to, cursor_pos_view);
}
void MapView::zoomSteps(double num_steps)
{
auto zoom_to = getZoom() * pow(sqrt(2.0), num_steps);
setZoom(zoom_to);
}
void MapView::setZoom(double value, QPointF center)
{
auto pos = this->center();
auto zoom_pos = viewToMap(center);
auto old_zoom = getZoom();
setZoom(value);
if (!qFuzzyCompare(old_zoom, getZoom()))
{
auto zoom_factor = getZoom() / old_zoom ;
setCenter(zoom_pos + (pos - zoom_pos) / zoom_factor);
}
}
void MapView::setZoom(double value)
{
zoom = qBound(zoom_out_limit, value, zoom_in_limit);
updateTransform();
emit viewChanged(ZoomChange);
}
void MapView::setRotation(double value)
{
rotation = value;
updateTransform();
emit viewChanged(RotationChange);
}
void MapView::setCenter(MapCoord pos)
{
center_pos = pos;
updateTransform();
emit viewChanged(CenterChange);
}
void MapView::updateTransform()
{
double final_zoom = calculateFinalZoomFactor();
double final_zoom_cosr = final_zoom * cos(rotation);
double final_zoom_sinr = final_zoom * sin(rotation);
auto center_x = center_pos.x();
auto center_y = center_pos.y();
// Create map_to_view
map_to_view.setMatrix(final_zoom_cosr, -final_zoom_sinr, -final_zoom_cosr * center_x + final_zoom_sinr * center_y,
final_zoom_sinr, final_zoom_cosr, -final_zoom_sinr * center_x - final_zoom_cosr * center_y,
0, 0, 1);
view_to_map = map_to_view.inverted();
world_transform = map_to_view.transposed();
}
TemplateVisibility MapView::effectiveMapVisibility() const
{
if (all_templates_hidden)
return { 1.0f, true };
else if (map_visibility.opacity < 0.005f)
return { 0.0f, false };
else
return map_visibility;
}
void MapView::setMapVisibility(TemplateVisibility vis)
{
if (map_visibility != vis)
{
map_visibility = vis;
emit visibilityChanged(VisibilityFeature::MapVisible, vis.visible && vis.opacity > 0, nullptr);
}
}
MapView::TemplateVisibilityVector::const_iterator MapView::findVisibility(const Template* temp) const
{
return std::find_if(begin(template_visibilities), end(template_visibilities), [temp](const TemplateVisibilityEntry& entry)
{
return entry.temp == temp;
} );
}
MapView::TemplateVisibilityVector::iterator MapView::findVisibility(const Template* temp)
{
return std::find_if(begin(template_visibilities), end(template_visibilities), [temp](const TemplateVisibilityEntry& entry)
{
return entry.temp == temp;
} );
}
bool MapView::isTemplateVisible(const Template* temp) const
{
auto entry = findVisibility(temp);
return entry != end(template_visibilities)
&& entry->visible
&& entry->opacity > 0;
}
TemplateVisibility MapView::getTemplateVisibility(const Template* temp) const
{
auto entry = findVisibility(temp);
if (entry != end(template_visibilities))
return *entry;
else
return { 1.0f, false };
}
void MapView::setTemplateVisibility(Template* temp, TemplateVisibility vis)
{
auto visible = vis.visible && vis.opacity > 0;
if (visible
&& temp->getTemplateState() != Template::Loaded
&& !templateLoadingBlocked())
{
vis.visible = visible = temp->loadTemplateFile(false);
}
if (setTemplateVisibilityHelper(temp, vis))
{
emit visibilityChanged(VisibilityFeature::TemplateVisible, visible, temp);
}
}
bool MapView::setTemplateVisibilityHelper(const Template *temp, TemplateVisibility vis)
{
auto stored = findVisibility(temp);
if (stored == end(template_visibilities))
{
template_visibilities.emplace_back(temp, vis);
return true;
}
if (*stored != vis)
{
stored->opacity = vis.opacity;
stored->visible = vis.visible;
return true;
}
return false;
}
void MapView::onTemplateAdded(int, Template* temp)
{
setTemplateVisibility(temp, { 1.0f, true });
}
void MapView::onTemplateDeleted(int, const Template* temp)
{
template_visibilities.erase(findVisibility(temp));
}
void MapView::setAllTemplatesHidden(bool value)
{
if (all_templates_hidden != value)
{
all_templates_hidden = value;
emit visibilityChanged(VisibilityFeature::AllTemplatesHidden, value);
}
}
void MapView::setGridVisible(bool visible)
{
if (grid_visible != visible)
{
grid_visible = visible;
emit visibilityChanged(VisibilityFeature::GridVisible, visible);
}
}
void MapView::setOverprintingSimulationEnabled(bool enabled)
{
if (overprinting_simulation_enabled != enabled)
{
overprinting_simulation_enabled = enabled;
emit visibilityChanged(VisibilityFeature::OverprintingEnabled, enabled);
}
}
void MapView::setTemplateLoadingBlocked(bool blocked)
{
template_loading_blocked = blocked;
}
} // namespace OpenOrienteering