21
21
22
22
#include " template_track.h"
23
23
24
+ #include < utility>
25
+
24
26
#include < Qt>
25
27
#include < QBrush>
26
28
#include < QByteArray>
44
46
#include " core/georeferencing.h"
45
47
#include " core/latlon.h"
46
48
#include " core/map.h"
49
+ #include " core/map_color.h"
47
50
#include " core/map_coord.h"
48
51
#include " core/map_part.h"
49
52
#include " core/objects/object.h"
@@ -59,6 +62,85 @@ class QAbstractButton;
59
62
60
63
namespace OpenOrienteering {
61
64
65
+ class Symbol ;
66
+
67
+ namespace {
68
+
69
+ const MapColor& makeTrackColor (Map& map)
70
+ {
71
+ auto * track_color = new MapColor (QLatin1String{" Purple" }, 0 );
72
+ track_color->setSpotColorName (QLatin1String{" PURPLE" });
73
+ track_color->setCmyk ({0 .35f , 0 .85f , 0.0 , 0.0 });
74
+ track_color->setRgbFromCmyk ();
75
+ map.addColor (track_color, 0 );
76
+ return *track_color;
77
+ }
78
+
79
+ const LineSymbol& makeTrackSymbol (Map& map, const MapColor& color)
80
+ {
81
+ auto * track_symbol = new LineSymbol ();
82
+ track_symbol->setName (TemplateTrack::tr (" Track" ));
83
+ track_symbol->setNumberComponent (0 , 1 );
84
+ track_symbol->setColor (&color);
85
+ track_symbol->setLineWidth (0.1 ); // mm, almost cosmetic
86
+ track_symbol->setCapStyle (LineSymbol::FlatCap);
87
+ track_symbol->setJoinStyle (LineSymbol::MiterJoin);
88
+ map.addSymbol (track_symbol, map.getNumSymbols ());
89
+ return *track_symbol;
90
+ }
91
+
92
+ const LineSymbol& makeRouteSymbol (Map& map, const MapColor& color)
93
+ {
94
+ auto * route_symbol = new LineSymbol ();
95
+ route_symbol->setName (TemplateTrack::tr (" Route" ));
96
+ route_symbol->setNumberComponent (0 , 2 );
97
+ route_symbol->setColor (&color);
98
+ route_symbol->setLineWidth (0.5 ); // mm
99
+ route_symbol->setCapStyle (LineSymbol::FlatCap);
100
+ route_symbol->setJoinStyle (LineSymbol::MiterJoin);
101
+ map.addSymbol (route_symbol, map.getNumSymbols ());
102
+ return *route_symbol;
103
+ }
104
+
105
+ const PointSymbol& makeWaypointSymbol (Map& map, const MapColor& color)
106
+ {
107
+ auto * waypoint_symbol = new PointSymbol ();
108
+ waypoint_symbol->setName (TemplateTrack::tr (" Waypoint" ));
109
+ waypoint_symbol->setNumberComponent (0 , 3 );
110
+ waypoint_symbol->setInnerColor (&color);
111
+ waypoint_symbol->setInnerRadius (500 ); // (um)
112
+ map.addSymbol (waypoint_symbol, map.getNumSymbols ());
113
+ return *waypoint_symbol;
114
+ }
115
+
116
+ PathObject* importPath (Map& map, const Symbol& symbol, MapCoordVector coords)
117
+ {
118
+ if (coords.empty ())
119
+ return nullptr ;
120
+
121
+ if (coords.size () == 1 )
122
+ coords.push_back (coords.front ());
123
+
124
+ auto * path = new PathObject (&symbol, std::move (coords));
125
+ map.addObject (path);
126
+ map.addObjectToSelection (path, false );
127
+ return path;
128
+ }
129
+
130
+ PointObject* importPoint (Map& map, const Symbol& symbol, const MapCoordF& position, const QString& name)
131
+ {
132
+ auto * point = new PointObject (&symbol);
133
+ point->setPosition (position);
134
+ if (!name.isEmpty ())
135
+ point->setTag (QStringLiteral (" name" ), name);
136
+ map.addObject (point);
137
+ map.addObjectToSelection (point, false );
138
+ return point;
139
+ }
140
+
141
+ } // namespace
142
+
143
+
62
144
const std::vector<QByteArray>& TemplateTrack::supportedExtensions ()
63
145
{
64
146
static std::vector<QByteArray> extensions = { " gpx" };
@@ -387,31 +469,13 @@ bool TemplateTrack::hasAlpha() const
387
469
}
388
470
389
471
390
- PathObject* TemplateTrack::importPathStart ()
391
- {
392
- auto * path = new PathObject ();
393
- path->setSymbol (map->getUndefinedLine (), true );
394
- return path;
395
- }
396
-
397
- void TemplateTrack::importPathEnd (PathObject* path)
398
- {
399
- map->addObject (path);
400
- map->addObjectToSelection (path, false );
401
- }
402
-
403
- PointObject* TemplateTrack::importWaypoint (const MapCoordF& position, const QString& name)
404
- {
405
- auto * point = new PointObject (map->getUndefinedPoint ());
406
- point->setPosition (position);
407
- point->setTag (QStringLiteral (" name" ), name);
408
- map->addObject (point);
409
- map->addObjectToSelection (point, false );
410
- return point;
411
- }
412
-
413
472
bool TemplateTrack::import (QWidget* dialog_parent)
414
473
{
474
+ if (!map)
475
+ {
476
+ return false ;
477
+ }
478
+
415
479
if (track.getNumWaypoints () == 0 && track.getNumSegments () == 0 )
416
480
{
417
481
QMessageBox::critical (dialog_parent, tr (" Error" ), tr (" The path is empty, there is nothing to import!" ));
@@ -425,51 +489,70 @@ bool TemplateTrack::import(QWidget* dialog_parent)
425
489
426
490
map->clearObjectSelection (false );
427
491
428
- if (track.getNumWaypoints () > 0 )
492
+ auto & track_color = makeTrackColor (*map);
493
+ auto & track_symbol = makeTrackSymbol (*map, track_color);
494
+
495
+ auto const num_waypoints = track.getNumWaypoints ();
496
+ if (num_waypoints > 0 )
429
497
{
430
- int res = QMessageBox::question (dialog_parent, tr (" Question" ), tr (" Should the waypoints be imported as a line going through all points?" ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
498
+ auto res = QMessageBox::No;
499
+ if (num_waypoints > 1 )
500
+ {
501
+ res = QMessageBox::question (
502
+ dialog_parent,
503
+ tr (" Question" ),
504
+ tr (" Should the waypoints be imported as a line going through all points?" ),
505
+ QMessageBox::Yes | QMessageBox::No,
506
+ QMessageBox::No);
507
+ }
431
508
if (res == QMessageBox::No)
432
509
{
433
- for (int i = 0 ; i < track.getNumWaypoints (); i++)
434
- result.push_back (importWaypoint (templateToMap (track.getWaypoint (i).map_coord ), track.getWaypointName (i)));
510
+ auto & waypoint_symbol = makeWaypointSymbol (*map, track_color);
511
+ for (int i = 0 ; i < num_waypoints; i++)
512
+ {
513
+ auto pos = templateToMap (track.getWaypoint (i).map_coord );
514
+ auto & name = track.getWaypointName (i);
515
+ if (auto * waypoint = importPoint (*map, waypoint_symbol, pos, name))
516
+ result.push_back (waypoint);
517
+ }
435
518
}
436
519
else
437
520
{
438
- PathObject* path = importPathStart ();
521
+ MapCoordVector coords;
522
+ coords.reserve (MapCoordVector::size_type (track.getNumWaypoints ()));
439
523
for (int i = 0 ; i < track.getNumWaypoints (); i++)
440
- path->addCoordinate (MapCoord (templateToMap (track.getWaypoint (i).map_coord )));
441
- importPathEnd (path);
442
- path->setTag (QStringLiteral (" name" ), QString{});
443
- result.push_back (path);
524
+ coords.push_back (MapCoord (templateToMap (track.getWaypoint (i).map_coord )));
525
+
526
+ auto & route_symbol = makeRouteSymbol (*map, track_color);
527
+ if (auto * path = importPath (*map, route_symbol, std::move (coords)))
528
+ result.push_back (path);
444
529
}
445
530
}
446
531
447
532
int skipped_paths = 0 ;
448
533
for (int i = 0 ; i < track.getNumSegments (); i++)
449
534
{
450
- const int segment_size = track.getSegmentPointCount (i);
535
+ auto const segment_size = track.getSegmentPointCount (i);
451
536
if (segment_size == 0 )
452
537
{
453
538
++skipped_paths;
454
539
continue ; // Don't create path without objects.
455
540
}
456
541
457
- PathObject* path = importPathStart ();
542
+ MapCoordVector coords;
543
+ coords.reserve (MapCoordVector::size_type (segment_size));
458
544
for (int j = 0 ; j < segment_size; j++)
545
+ coords.push_back (MapCoord (templateToMap (track.getSegmentPoint (i, j).map_coord )));
546
+
547
+ if (auto * path = importPath (*map, track_symbol, std::move (coords)))
459
548
{
460
- const TrackPoint& track_point = track.getSegmentPoint (i, j);
461
- auto coord = MapCoord { templateToMap (track_point.map_coord ) };
462
- path->addCoordinate (coord);
463
- }
464
- if (track.getSegmentPoint (i, 0 ).latlon == track.getSegmentPoint (i, segment_size-1 ).latlon )
465
- {
466
- path->closeAllParts ();
549
+ if (track.getSegmentPoint (i, 0 ).latlon == track.getSegmentPoint (i, segment_size-1 ).latlon )
550
+ path->closeAllParts ();
551
+ result.push_back (path);
467
552
}
468
- importPathEnd (path);
469
- result.push_back (path);
470
553
}
471
554
472
- for (const auto & object : result) // keep as separate loop to get the correct (final) indices
555
+ for (const auto * object : result) // keep as separate loop to get the correct (final) indices
473
556
undo_step->addObject (part->findObjectIndex (object));
474
557
475
558
map->setObjectsDirty ();
0 commit comments