-
Notifications
You must be signed in to change notification settings - Fork 105
/
area_symbol.cpp
842 lines (705 loc) · 22.7 KB
/
area_symbol.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
/*
* Copyright 2012, 2013 Thomas Schöps
* Copyright 2012-2019 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 "area_symbol.h"
#include <algorithm>
#include <cmath>
#include <iterator>
#include <memory>
#include <QtMath>
#include <QLatin1String>
#include <QStringRef>
#include <QXmlStreamReader> // IWYU pragma: keep
#include "core/map.h"
#include "core/map_color.h"
#include "core/map_coord.h"
#include "core/objects/object.h"
#include "core/renderables/renderable.h"
#include "core/renderables/renderable_implementation.h"
#include "core/symbols/line_symbol.h"
#include "core/symbols/point_symbol.h"
#include "core/symbols/symbol.h"
#include "core/virtual_coord_vector.h"
#include "util/xml_stream_util.h"
class QXmlStreamWriter;
// IWYU pragma: no_forward_declare QXmlStreamReader
namespace OpenOrienteering {
// ### FillPattern ###
AreaSymbol::FillPattern::FillPattern() noexcept
: type { LinePattern }
, flags { Default }
, angle { 0 }
, line_spacing { 5000 } // 5 mm
, line_offset { 0 }
, line_color { nullptr }
, line_width { 200 } // 0.2 mm
, offset_along_line { 0 }
, point_distance { 5000 } // 5 mm
, point { nullptr }
, name {}
{
// nothing else
}
void AreaSymbol::FillPattern::save(QXmlStreamWriter& xml, const Map& map) const
{
XmlElementWriter element { xml, QLatin1String("pattern") };
element.writeAttribute(QLatin1String("type"), type);
element.writeAttribute(QLatin1String("angle"), angle);
if (auto no_clipping = int(flags & Option::AlternativeToClipping))
element.writeAttribute(QLatin1String("no_clipping"), no_clipping);
if (rotatable())
element.writeAttribute(QLatin1String("rotatable"), true);
element.writeAttribute(QLatin1String("line_spacing"), line_spacing);
element.writeAttribute(QLatin1String("line_offset"), line_offset);
element.writeAttribute(QLatin1String("offset_along_line"), offset_along_line);
switch (type)
{
case LinePattern:
element.writeAttribute(QLatin1String("color"), map.findColorIndex(line_color));
element.writeAttribute(QLatin1String("line_width"), line_width);
break;
case PointPattern:
element.writeAttribute(QLatin1String("point_distance"), point_distance);
if (point)
point->save(xml, map);
break;
}
}
void AreaSymbol::FillPattern::load(QXmlStreamReader& xml, const Map& map, SymbolDictionary& symbol_dict, int version)
{
Q_ASSERT (xml.name() == QLatin1String("pattern"));
XmlElementReader element { xml };
type = element.attribute<Type>(QLatin1String("type"));
angle = element.attribute<float>(QLatin1String("angle"));
flags = Options{element.attribute<int>(QLatin1String("no_clipping")) & Option::AlternativeToClipping};
if (element.attribute<bool>(QLatin1String("rotatable")))
flags |= Option::Rotatable;
line_spacing = element.attribute<int>(QLatin1String("line_spacing"));
line_offset = element.attribute<int>(QLatin1String("line_offset"));
offset_along_line = element.attribute<int>(QLatin1String("offset_along_line"));
switch (type)
{
case LinePattern:
line_color = map.getColor(element.attribute<int>(QLatin1String("color")));
line_width = element.attribute<int>(QLatin1String("line_width"));
break;
case PointPattern:
point_distance = element.attribute<int>(QLatin1String("point_distance"));
while (xml.readNextStartElement())
{
if (xml.name() == QLatin1String("symbol"))
point = static_cast<PointSymbol*>(Symbol::load(xml, map, symbol_dict, version).release());
else
xml.skipCurrentElement();
}
break;
}
}
bool AreaSymbol::FillPattern::equals(const AreaSymbol::FillPattern& other, Qt::CaseSensitivity case_sensitivity) const
{
if (type != other.type)
return false;
if (qAbs(angle - other.angle) > 1e-05)
return false;
if (flags != other.flags)
return false;
if (line_spacing != other.line_spacing)
return false;
if (line_offset != other.line_offset)
return false;
if (type == PointPattern)
{
if (offset_along_line != other.offset_along_line)
return false;
if (point_distance != other.point_distance)
return false;
if (bool(point) != bool(other.point))
return false;
if (point && !point->equals(other.point, case_sensitivity))
return false;
}
else if (type == LinePattern)
{
if (!MapColor::equal(line_color, other.line_color))
return false;
if (line_width != other.line_width)
return false;
}
if (name.compare(other.name, case_sensitivity) != 0)
return false;
return true;
}
void AreaSymbol::FillPattern::setRotatable(bool value)
{
flags = value ? (flags | Option::Rotatable) : (flags & ~Option::Rotatable);
}
void AreaSymbol::FillPattern::setClipping(Options clipping)
{
flags = (flags & ~Option::AlternativeToClipping) | (clipping & Option::AlternativeToClipping);
}
void AreaSymbol::FillPattern::colorDeleted(const MapColor* color)
{
switch (type)
{
case FillPattern::PointPattern:
point->colorDeletedEvent(color);
break;
case FillPattern::LinePattern:
if (line_color == color)
line_color = nullptr;
break;
}
}
bool AreaSymbol::FillPattern::containsColor(const MapColor* color) const
{
switch (type)
{
case FillPattern::PointPattern:
return point && point->containsColor(color);
case FillPattern::LinePattern:
return line_color == color;
}
Q_UNREACHABLE();
}
const MapColor* AreaSymbol::FillPattern::guessDominantColor() const
{
const MapColor* color = nullptr;
switch (type)
{
case FillPattern::PointPattern:
if (point)
color = point->guessDominantColor();
break;
case FillPattern::LinePattern:
color = line_color;
break;
}
return color;
}
template <>
inline
void AreaSymbol::FillPattern::createLine<AreaSymbol::FillPattern::LinePattern>(
MapCoordF first, MapCoordF second,
qreal,
LineSymbol* line,
qreal,
const AreaRenderable&,
ObjectRenderables& output ) const
{
// out of inlining
output.insertRenderable(new LineRenderable(line, first, second));
}
template <>
inline
void AreaSymbol::FillPattern::createLine<AreaSymbol::FillPattern::PointPattern>(
MapCoordF first, MapCoordF second,
qreal delta_offset,
LineSymbol*,
qreal rotation,
const AreaRenderable& outline,
ObjectRenderables& output ) const
{
// out of inlining
createPointPatternLine(first, second, delta_offset, rotation, outline, output);
}
// This template will be instantiated in non-template createRenderables()
// once for each type of pattern, thus duplicating the complex body.
// This is by intention, in order to let the compiler optimize each
// instantiation independently, with regard to unused parameters in
// createLine(), and to eliminate any runtime checks for pattern type
// outside of non-template createRenderables().
template <int T>
void AreaSymbol::FillPattern::createRenderables(
const AreaRenderable& outline,
qreal delta_rotation,
const MapCoord& pattern_origin,
const QRectF& point_extent,
LineSymbol* line,
qreal rotation,
ObjectRenderables& output ) const
{
// Canvas is the entire rectangle which will be filled with renderables.
// It will be clipped by the outline, later.
auto canvas = outline.getExtent();
canvas.adjust(-point_extent.right(), -point_extent.bottom(), -point_extent.left(), -point_extent.top());
MapCoordF first, second;
// Fill
qreal delta_line_offset = 0;
qreal delta_along_line_offset = 0;
if (rotatable())
{
MapCoordF line_normal(0, -1);
line_normal.rotate(rotation);
line_normal.setY(-line_normal.y());
delta_line_offset = MapCoordF::dotProduct(line_normal, MapCoordF(pattern_origin));
MapCoordF line_tangent(1, 0);
line_tangent.rotate(rotation);
line_tangent.setY(-line_tangent.y());
delta_along_line_offset = MapCoordF::dotProduct(line_tangent, MapCoordF(pattern_origin));
}
auto line_spacing_f = 0.001*line_spacing;
const auto offset = 0.001 * line_offset + delta_line_offset;
if (qAbs(rotation - M_PI/2) < 0.0001)
{
// Special case: vertical lines
delta_along_line_offset = -delta_along_line_offset;
double first_offset = offset + ceil((canvas.left() - offset) / line_spacing_f) * line_spacing_f;
for (double cur = first_offset; cur < canvas.right(); cur += line_spacing_f)
{
first = MapCoordF(cur, canvas.top());
second = MapCoordF(cur, canvas.bottom());
createLine<T>(first, second, delta_along_line_offset, line, delta_rotation, outline, output);
}
}
else if (qAbs(rotation - 0) < 0.0001)
{
// Special case: horizontal lines
double first_offset = offset + ceil((canvas.top() - offset) / line_spacing_f) * line_spacing_f;
for (double cur = first_offset; cur < canvas.bottom(); cur += line_spacing_f)
{
first = MapCoordF(canvas.left(), cur);
second = MapCoordF(canvas.right(), cur);
createLine<T>(first, second, delta_along_line_offset, line, delta_rotation, outline, output);
}
}
else
{
// General case
if (rotation < M_PI / 2)
delta_along_line_offset = -delta_along_line_offset;
auto xfactor = 1.0 / sin(rotation);
auto yfactor = 1.0 / cos(rotation);
auto dist_x = xfactor * line_spacing_f;
auto dist_y = yfactor * line_spacing_f;
auto offset_x = xfactor * offset;
auto offset_y = yfactor * offset;
if (rotation < M_PI/2)
{
// Start with the upper left corner
offset_x += (-canvas.top()) / tan(rotation);
offset_y -= canvas.left() * tan(rotation);
auto start_x = offset_x + ceil((canvas.x() - offset_x) / dist_x) * dist_x;
auto start_y = canvas.top();
auto end_x = canvas.left();
auto end_y = offset_y + ceil((canvas.y() - offset_y) / dist_y) * dist_y;
do
{
// Correct coordinates
if (start_x > canvas.right())
{
start_y += ((start_x - canvas.right()) / dist_x) * dist_y;
start_x = canvas.right();
}
if (end_y > canvas.bottom())
{
end_x += ((end_y - canvas.bottom()) / dist_y) * dist_x;
end_y = canvas.bottom();
}
if (start_y > canvas.bottom())
break;
// Create the renderable(s)
first = MapCoordF(start_x, start_y);
second = MapCoordF(end_x, end_y);
createLine<T>(first, second, delta_along_line_offset, line, delta_rotation, outline, output);
// Move to next position
start_x += dist_x;
end_y += dist_y;
} while (true);
}
else
{
// Start with left lower corner
offset_x += (-canvas.bottom()) / tan(rotation);
offset_y -= canvas.x() * tan(rotation);
auto start_x = offset_x + ceil((canvas.x() - offset_x) / dist_x) * dist_x;
auto start_y = canvas.bottom();
auto end_x = canvas.x();
auto end_y = offset_y + ceil((canvas.bottom() - offset_y) / dist_y) * dist_y;
do
{
// Correct coordinates
if (start_x > canvas.right())
{
start_y += ((start_x - canvas.right()) / dist_x) * dist_y;
start_x = canvas.right();
}
if (end_y < canvas.y())
{
end_x += ((end_y - canvas.y()) / dist_y) * dist_x;
end_y = canvas.y();
}
if (start_y < canvas.y())
break;
// Create the renderable(s)
first = MapCoordF(start_x, start_y);
second = MapCoordF(end_x, end_y);
createLine<T>(first, second, delta_along_line_offset, line, delta_rotation, outline, output);
// Move to next position
start_x += dist_x;
end_y += dist_y;
} while (true);
}
}
}
void AreaSymbol::FillPattern::createRenderables(const AreaRenderable& outline, qreal delta_rotation, const MapCoord& pattern_origin, ObjectRenderables& output) const
{
if (line_spacing <= 0)
return;
if (!rotatable())
delta_rotation = 0;
// Make rotation unique
auto rotation = angle + delta_rotation;
rotation = fmod(1.0 * rotation, M_PI);
if (rotation < 0)
rotation = M_PI + rotation;
Q_ASSERT(rotation >= 0 && rotation <= M_PI);
// Handle clipping
const auto old_clip_path = output.getClipPath();
if (!(flags & Option::AlternativeToClipping))
{
output.setClipPath(outline.painterPath());
}
switch (type)
{
case LinePattern:
{
LineSymbol line;
line.setColor(line_color);
auto line_width_f = 0.001*line_width;
line.setLineWidth(line_width_f);
auto margin = line_width_f / 2;
auto point_extent = QRectF{-margin, -margin, line_width_f, line_width_f};
createRenderables<LinePattern>(outline, delta_rotation, pattern_origin, point_extent, &line, rotation, output);
}
break;
case PointPattern:
if (point && point_distance > 0)
{
PointObject point_object(point);
point_object.setRotation(delta_rotation);
point_object.update();
auto point_extent = point_object.getExtent();
createRenderables<PointPattern>(outline, delta_rotation, pattern_origin, point_extent, nullptr, rotation, output);
}
break;
}
output.setClipPath(old_clip_path);
}
void AreaSymbol::FillPattern::createPointPatternLine(
MapCoordF first, MapCoordF second,
qreal delta_offset,
qreal rotation,
const AreaRenderable& outline,
ObjectRenderables& output ) const
{
auto direction = second - first;
auto length = direction.length();
direction /= length; // normalize
auto offset = MapCoordF::dotProduct(direction, first) - 0.001 * offset_along_line - delta_offset;
auto step_length = 0.001 * point_distance;
auto start_length = ceil((offset) / step_length) * step_length - offset;
auto to_next = direction * step_length;
auto coord = first + direction * start_length;
// Duplicated loops for optimum locality of code
switch (flags & Option::AlternativeToClipping)
{
case Option::NoClippingIfCenterInside:
for (auto cur = start_length; cur < length; cur += step_length, coord += to_next)
point->createRenderablesIfCenterInside(coord, -rotation, outline.painterPath(), output);
break;
case Option::NoClippingIfCompletelyInside:
for (auto cur = start_length; cur < length; cur += step_length, coord += to_next)
point->createRenderablesIfCompletelyInside(coord, -rotation, outline.painterPath(), output);
break;
case Option::Default:
#if 1
// Avoids expensive check, but may create objects which won't be rendered.
for (auto cur = start_length; cur < length; cur += step_length, coord += to_next)
point->createRenderablesScaled(coord, -rotation, output);
break;
#endif
case Option::NoClippingIfPartiallyInside:
for (auto cur = start_length; cur < length; cur += step_length, coord += to_next)
point->createRenderablesIfPartiallyInside(coord, -rotation, outline.painterPath(), output);
break;
default:
Q_UNREACHABLE();
}
}
void AreaSymbol::FillPattern::scale(double factor)
{
line_spacing = qRound(factor * line_spacing);
line_width = qRound(factor * line_width);
line_offset = qRound(factor * line_offset);
offset_along_line = qRound(factor * offset_along_line);
point_distance = qRound(factor * point_distance);
if (point)
point->scale(factor);
}
qreal AreaSymbol::FillPattern::dimensionForIcon() const
{
// Ignore large spacing for icon scaling
auto size = qreal(0);
switch (type)
{
case LinePattern:
size = qreal(0.002 * line_width);
break;
case PointPattern:
size = qreal(0.002 * point->dimensionForIcon());
if (point_distance < 5000)
size = std::max(size, qreal(0.002 * point_distance));
break;
}
if (line_spacing < 5000)
size = std::max(size, qreal(0.0015 * line_spacing));
return size;
}
// ### AreaSymbol ###
AreaSymbol::AreaSymbol() noexcept
: Symbol { Symbol::Area }
, color { nullptr }
, minimum_area { 0 }
{
// nothing else
}
AreaSymbol::AreaSymbol(const AreaSymbol& proto)
: Symbol { proto }
, patterns { proto.patterns }
, color { proto.color }
, minimum_area { proto.minimum_area }
{
for (auto& new_pattern : patterns)
{
if (new_pattern.type == FillPattern::PointPattern)
new_pattern.point = Symbol::duplicate(*new_pattern.point).release();
}
}
AreaSymbol::~AreaSymbol()
{
for (auto& pattern : patterns)
{
if (pattern.type == FillPattern::PointPattern)
delete pattern.point;
}
}
AreaSymbol* AreaSymbol::duplicate() const
{
return new AreaSymbol(*this);
}
void AreaSymbol::createRenderables(
const Object *object,
const VirtualCoordVector &coords,
ObjectRenderables &output,
Symbol::RenderableOptions options) const
{
if (coords.size() < 3)
return;
auto path = static_cast<const PathObject*>(object);
PathPartVector path_parts = PathPart::calculatePathParts(coords);
createRenderables(path, path_parts, output, options);
}
void AreaSymbol::createRenderables(
const PathObject* object,
const PathPartVector& path_parts,
ObjectRenderables &output,
Symbol::RenderableOptions options) const
{
if (options == Symbol::RenderNormal)
{
createRenderablesNormal(object, path_parts, output);
}
else
{
const MapColor* dominant_color = guessDominantColor();
createBaselineRenderables(object, path_parts, output, dominant_color);
if (options.testFlag(Symbol::RenderAreasHatched))
{
createHatchingRenderables(object, path_parts, output, dominant_color);
}
}
}
void AreaSymbol::createRenderablesNormal(
const PathObject* object,
const PathPartVector& path_parts,
ObjectRenderables& output) const
{
// The shape output is even created if the area is not filled with a color
// because the QPainterPath created by it is needed as clip path for the fill objects
auto color_fill = new AreaRenderable(this, path_parts);
output.insertRenderable(color_fill);
auto rotation = object->getPatternRotation();
auto origin = object->getPatternOrigin();
for (const auto& pattern : patterns)
{
pattern.createRenderables(*color_fill, rotation, origin, output);
}
}
void AreaSymbol::createHatchingRenderables(
const PathObject* object,
const PathPartVector& path_parts,
ObjectRenderables& output,
const MapColor* color) const
{
Q_ASSERT(getContainedTypes() & Symbol::Area);
if (color)
{
// Insert hatched area renderable
AreaSymbol area_symbol;
area_symbol.setNumFillPatterns(1);
AreaSymbol::FillPattern& pattern = area_symbol.getFillPattern(0);
pattern.type = AreaSymbol::FillPattern::LinePattern;
pattern.angle = qDegreesToRadians(45.0f);
pattern.line_spacing = 1000;
pattern.line_offset = 0;
pattern.line_color = color;
pattern.line_width = 70;
auto symbol = object->getSymbol();
if (symbol && symbol->getType() == Symbol::Area)
{
const AreaSymbol* orig_symbol = symbol->asArea();
if (!orig_symbol->getColor()
&& orig_symbol->getNumFillPatterns() >= 1)
{
const AreaSymbol::FillPattern& orig_pattern = orig_symbol->getFillPattern(0);
pattern.angle = orig_pattern.angle;
pattern.flags = orig_pattern.flags & ~FillPattern::AlternativeToClipping;
if (orig_pattern.type == AreaSymbol::FillPattern::LinePattern)
{
pattern.line_spacing = std::max(1000, orig_pattern.line_spacing);
pattern.line_offset = orig_pattern.line_offset;
}
}
}
area_symbol.createRenderablesNormal(object, path_parts, output);
}
}
void AreaSymbol::colorDeletedEvent(const MapColor* color)
{
if (containsColor(color))
{
if (color == this->color)
this->color = nullptr;
for (auto& pattern : patterns)
pattern.colorDeleted(color);
resetIcon();
}
}
bool AreaSymbol::containsColor(const MapColor* color) const
{
return color == this->color
|| std::any_of(begin(patterns), end(patterns), [color](const auto& pattern){ return pattern.containsColor(color); });
}
const MapColor* AreaSymbol::guessDominantColor() const
{
auto color = this->color;
auto pattern = begin(patterns);
while (!color && pattern != end(patterns))
{
color = pattern->guessDominantColor();
++pattern;
}
return color;
}
void AreaSymbol::replaceColors(const MapColorMap& color_map)
{
color = color_map.value(color);
for (auto& pattern : patterns)
{
switch (pattern.type)
{
case FillPattern::LinePattern:
pattern.line_color = color_map.value(pattern.line_color);
break;
case FillPattern::PointPattern:
pattern.point->replaceColors(color_map);
break;
}
}
}
void AreaSymbol::scale(double factor)
{
minimum_area = qRound(factor*factor * minimum_area);
for (auto& pattern : patterns)
pattern.scale(factor);
resetIcon();
}
qreal AreaSymbol::dimensionForIcon() const
{
qreal size = 0;
for (auto& pattern : patterns)
size = qMax(size, pattern.dimensionForIcon());
return size;
}
bool AreaSymbol::hasRotatableFillPattern() const
{
return std::any_of(begin(patterns), end(patterns), [](auto& pattern){
return pattern.rotatable();
});
}
void AreaSymbol::saveImpl(QXmlStreamWriter& xml, const Map& map) const
{
XmlElementWriter element { xml, QLatin1String("area_symbol") };
element.writeAttribute(QLatin1String{"inner_color"}, map.findColorIndex(color));
element.writeAttribute(QLatin1String{"min_area"}, minimum_area);
element.writeAttribute(QLatin1String{"patterns"}, patterns.size());
for (const auto& pattern : patterns)
pattern.save(xml, map);
}
bool AreaSymbol::loadImpl(QXmlStreamReader& xml, const Map& map, SymbolDictionary& symbol_dict, int version)
{
if (xml.name() != QLatin1String("area_symbol"))
return false;
XmlElementReader element { xml };
color = map.getColor(element.attribute<int>(QLatin1String("inner_color")));
minimum_area = element.attribute<int>(QLatin1String("min_area"));
auto num_patterns = element.attribute<int>(QLatin1String("patterns"));
patterns.reserve(num_patterns % 100); // 100 is not the limit
while (xml.readNextStartElement())
{
if (xml.name() == QLatin1String("pattern"))
{
patterns.push_back(FillPattern());
patterns.back().load(xml, map, symbol_dict, version);
}
else
{
xml.skipCurrentElement();
}
}
return true;
}
bool AreaSymbol::equalsImpl(const Symbol* other, Qt::CaseSensitivity case_sensitivity) const
{
const AreaSymbol* area = static_cast<const AreaSymbol*>(other);
if (!MapColor::equal(color, area->color))
return false;
if (minimum_area != area->minimum_area)
return false;
if (patterns.size() != area->patterns.size())
return false;
// std::is_permutation would identify equal sets of patterns.
// However, guessDominantColor() depends on pattern order if there is no
// AreaSymbol::color (or after the AreaSymbol::color is set to nullptr).
// So equalsImpl cannot be changed unless guessDominantColor is changed.
return std::equal(begin(patterns), end(patterns), begin(area->patterns), [case_sensitivity](auto& lhs, auto& rhs){
return lhs.equals(rhs, case_sensitivity);
});
}
} // namespace OpenOrienteering