-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathmap.cpp
2504 lines (2118 loc) · 63.1 KB
/
map.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
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2012-2014 Thomas Schöps
* Copyright 2013-2020 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.h"
#include <algorithm>
#include <cmath>
#include <iterator>
#include <memory>
#include <type_traits>
#include <utility>
#include <Qt>
#include <QtGlobal>
#include <QtMath>
#include <QByteArray>
#include <QCoreApplication>
#include <QDebug>
#include <QIODevice>
#include <QPaintEngine>
#include <QPainter>
#include <QPoint>
#include <QPointF>
#include <QSignalBlocker>
#include <QTimer>
#include <QTranslator>
#include "core/georeferencing.h"
#include "core/map_color.h"
#include "core/map_coord.h"
#include "core/map_grid.h"
#include "core/map_part.h"
#include "core/map_printer.h"
#include "core/map_view.h"
#include "core/objects/object.h"
#include "core/objects/object_operations.h"
#include "core/renderables/renderable.h"
#include "core/symbols/combined_symbol.h"
#include "core/symbols/line_symbol.h"
#include "core/symbols/point_symbol.h"
#include "core/symbols/symbol.h"
#include "core/symbols/text_symbol.h"
#include "fileformats/file_format_registry.h"
#include "fileformats/file_import_export.h"
#include "fileformats/xml_file_format_p.h"
#include "gui/map/map_widget.h"
#include "templates/template.h"
#include "undo/map_part_undo.h"
#include "undo/object_undo.h"
#include "undo/undo.h"
#include "undo/undo_manager.h"
#include "util/util.h"
#include "util/transformation.h"
// IWYU pragma: no_forward_declare QRectF
#ifdef __clang_analyzer__
#define singleShot(A, B, C) singleShot(A, B, #C) // NOLINT
#endif
namespace OpenOrienteering {
QPointer<QTranslator> map_symbol_translator{};
namespace {
// ### Misc ###
/** A record of information about the mapping of a color in a source MapColorSet
* to a color in a destination MapColorSet.
*/
struct MapColorSetMergeItem
{
MapColor* src_color;
MapColor* dest_color;
std::size_t dest_index;
std::size_t lower_bound;
std::size_t upper_bound;
int lower_errors;
int upper_errors;
bool filter;
MapColorSetMergeItem()
: src_color(nullptr),
dest_color(nullptr),
dest_index(0),
lower_bound(0),
upper_bound(0),
lower_errors(0),
upper_errors(0),
filter(false)
{ }
};
/** The mapping of all colors in a source MapColorSet
* to colors in a destination MapColorSet. */
typedef std::vector<MapColorSetMergeItem> MapColorSetMergeList;
} // namespace
// ### MapColorSet ###
Map::MapColorSet::MapColorSet()
{
// nothing
}
Map::MapColorSet::~MapColorSet()
{
for (MapColor* color : colors)
delete color;
}
void Map::MapColorSet::insert(int pos, MapColor* color)
{
colors.insert(colors.begin() + pos, color);
adjustColorPriorities(pos + 1, colors.size());
}
void Map::MapColorSet::erase(int pos)
{
colors.erase(colors.begin() + pos);
adjustColorPriorities(pos, colors.size());
}
void Map::MapColorSet::adjustColorPriorities(int first, int last)
{
// TODO: delete or update RenderStates with these colors
for (int i = first; i < last; ++i)
colors[i]->setPriority(i);
}
// This algorithm tries to maintain the relative order of colors.
MapColorMap Map::MapColorSet::importSet(const Map::MapColorSet& other, std::vector< bool >* filter, Map* map)
{
MapColorMap out_pointermap;
// Determine number of colors to import
std::size_t import_count = other.colors.size();
if (filter)
{
Q_ASSERT(filter->size() == other.colors.size());
for (std::size_t i = 0, end = other.colors.size(); i != end; ++i)
{
MapColor* color = other.colors[i];
if (!(*filter)[i] || out_pointermap.contains(color))
{
continue;
}
out_pointermap[color] = nullptr; // temporary used as a flag
// Determine referenced spot colors, and add them to the filter
if (color->getSpotColorMethod() == MapColor::CustomColor)
{
for (const SpotColorComponent& component : color->getComponents())
{
if (!out_pointermap.contains(component.spot_color))
{
// Add this spot color to the filter
int j = 0;
while (other.colors[j] != component.spot_color)
++j;
(*filter)[j] = true;
out_pointermap[component.spot_color] = nullptr;
}
}
}
}
import_count = out_pointermap.size();
out_pointermap.clear();
}
if (import_count > 0)
{
colors.reserve(colors.size() + import_count);
// The conflict resolution algorithm below is simplified by setting
// iterator `selected_item` to a real list element which is not related
// to the actual color sets we are merging. This is the extra element
// identified as `end_of_merge_list`.
MapColorSetMergeList merge_list{other.colors.size() + 1};
const auto end_of_merge_list = end(merge_list) - 1;
bool priorities_changed = false;
// Initialize merge_list
auto merge_list_item = merge_list.begin();
for (std::size_t i = 0; i < other.colors.size(); ++i)
{
merge_list_item->filter = (!filter || (*filter)[i]);
MapColor* src_color = other.colors[i];
merge_list_item->src_color = src_color;
for (std::size_t k = 0, colors_size = colors.size(); k < colors_size; ++k)
{
if (colors[k]->equals(*src_color))
{
merge_list_item->dest_color = colors[k];
merge_list_item->dest_index = k;
out_pointermap[src_color] = colors[k];
// Prefer a matching color at the same priority,
// so just abort early if priority matches
if (merge_list_item->dest_color->getPriority() == merge_list_item->src_color->getPriority())
break;
}
}
++merge_list_item;
}
Q_ASSERT(merge_list_item == end_of_merge_list);
size_t iteration_number = 1;
while (true)
{
// Evaluate bounds and conflicting order of colors
int max_conflict_reduction = 0;
auto selected_item = end_of_merge_list; // Note: non-const copy of an iterator
for (merge_list_item = begin(merge_list); merge_list_item != end_of_merge_list; ++merge_list_item)
{
// Check all lower colors for a higher dest_index
std::size_t& lower_bound(merge_list_item->lower_bound);
lower_bound = merge_list_item->dest_color ? merge_list_item->dest_index : 0;
auto it = merge_list.begin();
for (; it != merge_list_item; ++it)
{
if (it->dest_color)
{
if (it->dest_index > lower_bound)
{
lower_bound = it->dest_index;
}
if (merge_list_item->dest_color && merge_list_item->dest_index < it->dest_index)
{
++merge_list_item->lower_errors;
}
}
}
// Check all higher colors for a lower dest_index
std::size_t& upper_bound(merge_list_item->upper_bound);
upper_bound = merge_list_item->dest_color ? merge_list_item->dest_index : colors.size();
for (++it; it != end_of_merge_list; ++it)
{
if (it->dest_color)
{
if (it->dest_index < upper_bound)
{
upper_bound = it->dest_index;
}
if (merge_list_item->dest_color && merge_list_item->dest_index > it->dest_index)
{
++merge_list_item->upper_errors;
}
}
}
if (merge_list_item->filter)
{
if (merge_list_item->lower_errors == 0 && merge_list_item->upper_errors > max_conflict_reduction)
{
int conflict_reduction = merge_list_item->upper_errors;
// Check new conflicts with insertion index: selected_item->upper_bound
for (it = merge_list.begin(); it != merge_list_item; ++it)
{
if (it->dest_color && selected_item->upper_bound <= it->dest_index)
--conflict_reduction;
}
// Also allow = here to make two-step resolves work
if (conflict_reduction >= max_conflict_reduction)
{
selected_item = merge_list_item;
max_conflict_reduction = conflict_reduction;
}
}
else if (merge_list_item->upper_errors == 0 && merge_list_item->lower_errors > max_conflict_reduction)
{
int conflict_reduction = merge_list_item->lower_errors;
// Check new conflicts with insertion index: (selected_item->lower_bound+1)
it = merge_list_item;
for (++it; it != end_of_merge_list; ++it)
{
if (it->dest_color && (selected_item->lower_bound+1) > it->dest_index)
--conflict_reduction;
}
// Also allow = here to make two-step resolves work
if (conflict_reduction >= max_conflict_reduction)
{
selected_item = merge_list_item;
max_conflict_reduction = conflict_reduction;
}
}
}
}
// Abort if no conflicts or maximum iteration count reached.
// The latter condition is just to prevent endless loops in
// case of bugs and should not occur theoretically.
if (selected_item == end_of_merge_list
|| iteration_number > merge_list.size())
break;
// Solve selected conflict item
auto new_color = new MapColor(*selected_item->dest_color);
selected_item->dest_color = new_color;
out_pointermap[selected_item->src_color] = new_color;
std::size_t insertion_index = (selected_item->lower_errors == 0) ? selected_item->upper_bound : (selected_item->lower_bound+1);
if (map)
map->addColor(new_color, insertion_index);
else
colors.insert(colors.begin() + insertion_index, new_color);
priorities_changed = true;
for (merge_list_item = merge_list.begin(); merge_list_item != merge_list.end(); ++merge_list_item)
{
merge_list_item->lower_errors = 0;
merge_list_item->upper_errors = 0;
if (merge_list_item->dest_color && merge_list_item->dest_index >= insertion_index)
++merge_list_item->dest_index;
}
selected_item->dest_index = insertion_index;
++iteration_number;
}
merge_list.erase(end_of_merge_list); // no longer needed
// Some missing colors may be spot color compositions which can be
// resolved to new colors only after all colors have been created.
// That is why we create all missing colors first.
for (auto it = merge_list.rbegin(); it != merge_list.rend(); ++it)
{
if (it->filter && !it->dest_color)
{
it->dest_color = new MapColor(*it->src_color);
out_pointermap[it->src_color] = it->dest_color;
}
else
{
// Existing colors don't need to be touched again.
it->dest_color = nullptr;
}
}
// Now process all new colors for spot color resolution and insertion
for (auto it = merge_list.rbegin(); it != merge_list.rend(); ++it)
{
MapColor* new_color = it->dest_color;
if (new_color)
{
if (new_color->getSpotColorMethod() == MapColor::CustomColor)
{
SpotColorComponents components = new_color->getComponents();
for (SpotColorComponent& component : components)
{
Q_ASSERT(out_pointermap.contains(component.spot_color));
component.spot_color = out_pointermap[component.spot_color];
}
new_color->setSpotColorComposition(components);
}
std::size_t insertion_index = it->upper_bound;
if (map)
map->addColor(new_color, insertion_index);
else
colors.insert(colors.begin() + insertion_index, new_color);
priorities_changed = true;
}
}
if (map && priorities_changed)
{
map->updateAllObjects();
}
}
return out_pointermap;
}
// ### Map ###
bool Map::static_initialized = false;
MapColor Map::covering_white(MapColor::CoveringWhite);
MapColor Map::covering_red(MapColor::CoveringRed);
MapColor Map::undefined_symbol_color(MapColor::Undefined);
MapColor Map::registration_color(MapColor::Registration);
LineSymbol* Map::covering_white_line;
LineSymbol* Map::covering_red_line;
LineSymbol* Map::undefined_line;
PointSymbol* Map::undefined_point;
TextSymbol* Map::undefined_text;
CombinedSymbol* Map::covering_combined_line;
Map::Map()
: color_set()
, has_spot_colors(false)
, undo_manager(new UndoManager(this))
, renderables(new MapRenderables(this))
, selection_renderables(new MapRenderables(this))
, renderable_options(Symbol::RenderNormal)
, printer_config(nullptr)
{
if (!static_initialized)
initStatic();
georeferencing.reset(new Georeferencing());
init();
connect(this, &Map::colorAdded, this, &Map::checkSpotColorPresence);
connect(this, &Map::colorChanged, this, &Map::checkSpotColorPresence);
connect(this, &Map::colorDeleted, this, &Map::checkSpotColorPresence);
connect(undo_manager.data(), &UndoManager::cleanChanged, this, &Map::undoCleanChanged);
}
Map::~Map()
{
clear(); // properly destruct all children
}
void Map::clear()
{
undo_manager->clear();
templates.clear();
first_front_template = 0;
closed_templates.clear();
object_selection.clear();
first_selected_object = nullptr;
selection_renderables->clear();
renderables->clear();
for (MapPart* part : parts)
delete part;
parts.clear();
current_part_index = 0;
for (auto symbol : symbols)
delete symbol;
symbols.clear();
// Don't clear() color_set: It is shared.
}
void Map::init()
{
color_set = new MapColorSet();
parts.push_back(new MapPart(tr("default part"), this));
widgets.clear();
undo_manager->setClean();
symbol_set_id = QString();
map_notes = QString();
printer_config.reset();
image_template_use_meters_per_pixel = true;
image_template_meters_per_pixel = 0;
image_template_dpi = 0;
image_template_scale = 0;
colors_dirty = false;
symbols_dirty = false;
templates_dirty = false;
objects_dirty = false;
other_dirty = false;
unsaved_changes = false;
}
void Map::reset()
{
clear();
init();
}
void Map::setScaleDenominator(unsigned int value)
{
georeferencing->setScaleDenominator(value);
}
unsigned int Map::getScaleDenominator() const
{
return georeferencing->getScaleDenominator();
}
void Map::changeScale(unsigned int new_scale_denominator, double additional_stretch, const MapCoord& scaling_center, bool scale_symbols, bool scale_objects, bool scale_georeferencing, bool scale_templates)
{
if (new_scale_denominator == getScaleDenominator() && additional_stretch == 1.0)
return;
double factor = (getScaleDenominator() / (double)new_scale_denominator) * additional_stretch;
if (scale_symbols)
scaleAllSymbols(factor);
if (scale_objects)
{
undo_manager->clear();
scaleAllObjects(factor, scaling_center);
if (hasPrinterConfig())
{
auto print_area = printer_config->print_area;
auto center = QPointF(scaling_center);
print_area.setTopLeft(center + factor * (print_area.topLeft() - center));
print_area.setBottomRight(center + factor * (print_area.bottomRight() - center));
printer_config->print_area = print_area;
}
}
if (scale_georeferencing)
georeferencing->setMapRefPoint(scaling_center + factor * (georeferencing->getMapRefPoint() - scaling_center));
if (scale_templates)
{
for (int i = 0; i < getNumTemplates(); ++i)
{
Template* temp = getTemplate(i);
if (temp->isTemplateGeoreferenced())
continue;
setTemplateAreaDirty(i);
temp->scale(factor, scaling_center);
setTemplateAreaDirty(i);
}
for (int i = 0; i < getNumClosedTemplates(); ++i)
{
Template* temp = getClosedTemplate(i);
if (temp->isTemplateGeoreferenced())
continue;
temp->scale(factor, scaling_center);
}
}
setScaleDenominator(new_scale_denominator);
setOtherDirty();
updateAllMapWidgets();
}
void Map::rotateMap(double rotation, const MapCoord& center, bool adjust_georeferencing, bool adjust_declination, bool adjust_templates)
{
if (std::fmod(rotation, 2 * M_PI) == 0)
return;
undo_manager->clear();
rotateAllObjects(rotation, center);
if (adjust_georeferencing)
{
MapCoordF reference_point = MapCoordF(georeferencing->getMapRefPoint() - center);
reference_point.rotate(-rotation);
georeferencing->setMapRefPoint(MapCoord(MapCoordF(center) + reference_point));
}
if (adjust_declination)
{
auto rotation_degrees = qRadiansToDegrees(rotation);
georeferencing->setDeclination(georeferencing->getDeclination() + rotation_degrees);
}
if (adjust_templates)
{
for (int i = 0; i < getNumTemplates(); ++i)
{
Template* temp = getTemplate(i);
if (temp->isTemplateGeoreferenced())
continue;
setTemplateAreaDirty(i);
temp->rotate(rotation, center);
setTemplateAreaDirty(i);
}
for (int i = 0; i < getNumClosedTemplates(); ++i)
{
Template* temp = getClosedTemplate(i);
if (temp->isTemplateGeoreferenced())
continue;
temp->rotate(rotation, center);
}
}
setOtherDirty();
updateAllMapWidgets();
}
void Map::setMapNotes(const QString& text)
{
map_notes = text;
}
QHash<const Symbol*, Symbol*> Map::importMap(
const Map& imported_map,
ImportMode mode,
std::vector<bool>* filter,
int symbol_insert_pos,
bool merge_duplicate_symbols)
{
QTransform q_transform;
if (mode.testFlag(GeorefImport))
{
/// \todo Test and review import of georeferenced and non-georeferenced maps, in all combinations.
/// \todo Handle rotation of patterns and text, cf. Object::transform.
const auto& georef = getGeoreferencing();
const auto& other_georef = imported_map.getGeoreferencing();
const auto src_origin = MapCoordF { other_georef.getMapRefPoint() };
bool ok0, ok1, ok2;
PassPointList passpoints;
passpoints.resize(3);
passpoints[0].src_coords = src_origin;
passpoints[0].dest_coords = georef.toMapCoordF(&other_georef, passpoints[0].src_coords, &ok0);
passpoints[1].src_coords = src_origin + MapCoordF { 128.0, 0.0 }; // 128 mm off horizontally
passpoints[1].dest_coords = georef.toMapCoordF(&other_georef, passpoints[1].src_coords, &ok1);
passpoints[2].src_coords = src_origin + MapCoordF { 0.0, 128.0 }; // 128 mm off vertically
passpoints[2].dest_coords = georef.toMapCoordF(&other_georef, passpoints[2].src_coords, &ok2);
if (ok0 && ok1 && ok2
&& !passpoints.estimateNonIsometricSimilarityTransform(&q_transform))
{
/// \todo proper error message
qDebug("Failed to calculate transformation");
q_transform.reset();
}
}
return importMap(imported_map, mode & ~GeorefImport, q_transform, filter, symbol_insert_pos, merge_duplicate_symbols);
}
bool Map::loadFrom(const QString& path, MapView* view)
{
auto importer = FileFormats.makeImporter(path, *this, view);
return importer && importer->doImport();
}
QHash<const Symbol*, Symbol*> Map::importMap(
const Map& imported_map,
ImportMode mode,
const QTransform& transform,
std::vector<bool>* filter,
int symbol_insert_pos,
bool merge_duplicate_symbols)
{
if (imported_map.getScaleDenominator() != getScaleDenominator())
qWarning("Map::importMap() called for different map scale");
if (mode.testFlag(GeorefImport))
qWarning("Map::importMap() called with GeorefImport flag set");
// Determine which symbols and colors to import
std::vector<bool> color_filter(std::size_t(imported_map.getNumColors()), true);
std::vector<bool> symbol_filter(std::size_t(imported_map.getNumSymbols()), true);
if (mode.testFlag(MinimalImport))
{
switch (mode & 0x0f)
{
case ObjectImport:
if (imported_map.getNumObjects() > 0)
imported_map.determineSymbolsInUse(symbol_filter);
imported_map.determineColorsInUse(symbol_filter, color_filter);
break;
case SymbolImport:
if (filter)
{
Q_ASSERT(filter->size() == symbol_filter.size());
symbol_filter = *filter;
imported_map.determineSymbolUseClosure(symbol_filter);
}
imported_map.determineColorsInUse(symbol_filter, color_filter);
break;
case ColorImport:
if (filter)
{
Q_ASSERT(filter->size() == color_filter.size());
color_filter = *filter;
}
break;
default:
Q_UNREACHABLE();
}
}
// Import colors
auto color_map = color_set->importSet(*imported_map.color_set, &color_filter, this);
QHash<const Symbol*, Symbol*> symbol_map;
if ((mode & 0x0f) != ColorImport)
{
if (imported_map.getNumSymbols() > 0)
{
// Import symbols
symbol_map = importSymbols(imported_map, color_map, symbol_insert_pos, merge_duplicate_symbols, symbol_filter);
}
if ((mode & 0x0f) != SymbolImport
&& imported_map.getNumObjects() > 0)
{
// Import parts like this:
// - if the other map has only one part, import it into the current part
// - else check if there is already a part with an equal name for every part to import and import into this part if found, else create a new part
auto* undo_step = new CombinedUndoStep(this);
for (const auto* part_to_import : imported_map.parts)
{
MapPart* dest_part = nullptr;
if (imported_map.parts.size() == 1)
{
dest_part = getCurrentPart();
}
else
{
for (auto* check_part : parts)
{
if (check_part->getName().compare(part_to_import->getName(), Qt::CaseInsensitive) == 0)
{
dest_part = check_part;
break;
}
}
if (!dest_part)
{
// Import as new part
dest_part = new MapPart(part_to_import->getName(), this);
addPart(dest_part, 0);
undo_step->push(new MapPartUndoStep(this, MapPartUndoStep::RemoveMapPart, 0));
}
}
// Temporarily switch the current part for importing so the undo step gets created for the right part
MapPart* temp_current_part = getCurrentPart();
current_part_index = std::size_t(findPartIndex(dest_part));
bool select_and_center_objects = dest_part == temp_current_part;
if (auto import_undo = dest_part->importPart(part_to_import, symbol_map, transform, select_and_center_objects))
{
undo_step->push(import_undo.release());
if (select_and_center_objects)
ensureVisibilityOfSelectedObjects(Map::FullVisibility);
}
current_part_index = std::size_t(findPartIndex(temp_current_part));
}
push(undo_step);
}
}
return symbol_map;
}
bool Map::exportToIODevice(QIODevice& device) const
{
XMLFileExporter exporter({}, this, nullptr);
exporter.setDevice(&device);
auto success = exporter.doExport();
device.close();
return success;
}
bool Map::importFromIODevice(QIODevice& device)
{
XMLFileImporter importer {{}, this, nullptr};
importer.setDevice(&device);
auto success = importer.doImport();
device.close();
return success;
}
void Map::draw(QPainter* painter, const RenderConfig& config)
{
// Update the renderables of all objects marked as dirty
updateObjects();
// The actual drawing
renderables->draw(painter, config);
}
void Map::drawOverprintingSimulation(QPainter* painter, const RenderConfig& config)
{
// Update the renderables of all objects marked as dirty
updateObjects();
// The actual drawing
renderables->drawOverprintingSimulation(painter, config);
}
void Map::drawColorSeparation(QPainter* painter, const RenderConfig& config, const MapColor* spot_color, bool use_color)
{
// Update the renderables of all objects marked as dirty
updateObjects();
// The actual drawing
renderables->drawColorSeparation(painter, config, spot_color, use_color);
}
void Map::drawGrid(QPainter* painter, const QRectF& bounding_box)
{
grid.draw(painter, bounding_box, this);
}
void Map::drawTemplates(QPainter* painter, const QRectF& bounding_box, int first_template, int last_template, const MapView* view, bool on_screen) const
{
for (int i = first_template; i <= last_template; ++i)
{
const Template* temp = getTemplate(i);
if (temp->getTemplateState() != Template::Loaded)
continue;
double scale = std::max(temp->getTemplateScaleX(), temp->getTemplateScaleY());
auto visibility = TemplateVisibility{ 1, true };
if (view)
{
visibility = view->getTemplateVisibility(temp);
visibility.visible &= visibility.opacity > 0;
scale *= view->getZoom();
}
if (visibility.visible)
{
Q_ASSERT(visibility.opacity == 1 || painter->paintEngine()->hasFeature(QPaintEngine::ConstantOpacity));
painter->save();
temp->drawTemplate(painter, bounding_box, scale, on_screen, visibility.opacity);
painter->restore();
}
}
}
void Map::updateObjects()
{
// TODO: It maybe would be better if the objects entered themselves into a separate list when they get dirty so not all objects have to be traversed here
applyOnAllObjects(&Object::update);
}
void Map::removeRenderablesOfObject(const Object* object, bool mark_area_as_dirty)
{
renderables->removeRenderablesOfObject(object, mark_area_as_dirty);
if (isObjectSelected(object))
removeSelectionRenderables(object);
}
void Map::insertRenderablesOfObject(const Object* object)
{
renderables->insertRenderablesOfObject(object);
if (isObjectSelected(object))
addSelectionRenderables(object);
}
void Map::markAsIrregular(Object* object)
{
irregular_objects.insert(object);
}
const std::set<Object*> Map::irregularObjects() const
{
return irregular_objects;
}
std::size_t Map::deleteIrregularObjects()
{
std::size_t result = 0;
std::set<Object*> unhandled;
for (auto object : irregular_objects)
{
for (auto part : parts)
{
if (part->deleteObject(object))
{
++result;
goto next_object;
}
}
unhandled.insert(object);
next_object:
; // nothing else
}
irregular_objects.swap(unhandled);
return result;
}
void Map::getSelectionToSymbolCompatibility(const Symbol* symbol, bool& out_compatible, bool& out_different) const
{
out_compatible = symbol && !object_selection.empty();
out_different = false;
if (symbol)
{
for (const Object* object : object_selection)
{
if (!symbol->isTypeCompatibleTo(object))
{
out_compatible = false;
out_different = true;
return;
}
else if (symbol != object->getSymbol())
out_different = true;
}
}
}
void Map::deleteSelectedObjects()
{
auto obj = selectedObjectsBegin();
auto end = selectedObjectsEnd();
if (obj != end)
{
// FIXME: this is not ready for multiple map parts.
auto undo_step = new AddObjectsUndoStep(this);
MapPart* part = getCurrentPart();
for (; obj != end; ++obj)
{
int index = part->findObjectIndex(*obj);
if (index >= 0)
{
undo_step->addObject(index, *obj);
part->releaseObject(index);
}
else
{
qDebug() << this << "::deleteSelectedObjects(): Object" << *obj << "not found in current map part.";
}
}
setObjectsDirty();
clearObjectSelection(true);
push(undo_step);
}
}
void Map::includeSelectionRect(QRectF& rect) const
{
for (const Object* object : object_selection)
rectIncludeSafe(rect, object->getExtent());
}
void Map::drawSelection(QPainter* painter, bool force_min_size, MapWidget* widget, MapRenderables* replacement_renderables, bool draw_normal)
{
MapView* view = widget->getMapView();
painter->save();
painter->translate(widget->width() / 2.0 + view->panOffset().x(), widget->height() / 2.0 + view->panOffset().y());
painter->setWorldTransform(view->worldTransform(), true);
if (!replacement_renderables)
replacement_renderables = selection_renderables.data();
RenderConfig::Options options = RenderConfig::Screen | RenderConfig::HelperSymbols;
qreal selection_opacity = 1.0;
if (force_min_size)
options |= RenderConfig::ForceMinSize;
if (!draw_normal)
{
options |= RenderConfig::Highlighted;
selection_opacity = 0.4;
}
RenderConfig config = { *this, view->calculateViewedRect(widget->viewportToView(widget->rect())), view->calculateFinalZoomFactor(), options, selection_opacity };
replacement_renderables->draw(painter, config);
painter->restore();
}
void Map::addObjectToSelection(Object* object, bool emit_selection_changed)
{
Q_ASSERT(!isObjectSelected(object));
// we omit hidden and protected objects from any kind of selection
const auto* object_symbol = object->getSymbol();
if (object_symbol->isProtected() || object_symbol->isHidden())
return;