]> err.no Git - mapper/blob - src/path.c
Merge branch 'master' of git+ssh://tal.org/home/git/mapper
[mapper] / src / path.c
1 #include <config.h>
2
3 #include <gtk/gtk.h>
4
5 #include "utils.h"
6 #include "map.h"
7 #include "route.h"
8 #include "mapper-types.h"
9 #include "track.h"
10 #include "path.h"
11 #include "settings.h"
12 #include "latlon.h"
13
14 Path *
15 path_new(void)
16 {
17 Path *p;
18
19 p=g_slice_new0(Path);
20 MACRO_PATH_INIT(*p);
21 return p;
22 }
23
24 void
25 path_free(Path *p)
26 {
27 g_return_if_fail(p);
28 MACRO_PATH_FREE(*p);
29 g_slice_free(Path, p);
30 }
31
32 void 
33 path_resize(Path *path, guint size)
34 {
35 if (path->head + size != path->cap) {
36         Point *old_head = path->head;
37         WayPoint *curr;
38         path->head = g_renew(Point, old_head, size);
39         path->cap = path->head + size;
40         if (path->head != old_head) {
41                 path->tail = path->head + (path->tail - old_head);
42
43                 /* Adjust all of the waypoints. */
44                 for (curr = path->whead - 1; curr++ != path->wtail;)
45                         curr->point = path->head + (curr->point - old_head);
46         }
47 }
48 }
49
50 void 
51 path_wresize(Path *path, guint wsize)
52 {
53 if (path->whead + wsize != path->wcap) {
54         WayPoint *old_whead = path->whead;
55         path->whead = g_renew(WayPoint, old_whead, wsize);
56         path->wtail = path->whead + (path->wtail - old_whead);
57         path->wcap = path->whead + wsize;
58 }
59 }
60
61 gboolean 
62 path_insert_break(Path *path)
63 {
64 if (!path)
65         return FALSE;
66
67 if (!path->tail)
68         return FALSE;
69
70 if (path->tail->unity) {
71         guint x1, y1;
72
73         /* To mark a "waypoint" in a track, we'll add a (0, 0) point and then
74          * another instance of the most recent track point. */
75         MACRO_PATH_INCREMENT_TAIL(*path);
76         *path->tail=_point_null;
77         MACRO_PATH_INCREMENT_TAIL(*path);
78         *path->tail=path->tail[-2];
79
80 #if 0
81         /* Instead of calling map_render_paths(), we'll just draw the waypoint ourselves. */
82         x1 = unit2bufx(path->tail->unitx);
83         y1 = unit2bufy(path->tail->unity);
84         map_render_waypoint(x1, y1, _gc[COLORABLE_TRACK_BREAK]);
85 #endif
86 }
87 return FALSE;
88 }
89
90 /**
91  * Add a text description at current point
92  * 
93  */
94 void 
95 path_insert_mark_text(Path *path, gchar *text)
96 {
97 MACRO_PATH_INCREMENT_WTAIL(*path);
98 path->wtail->point = path->tail;
99 path->wtail->desc = text;
100 }
101
102 GtkListStore *
103 path_generate_store(Path *path)
104 {
105 WayPoint *wcurr;
106 GtkTreeIter iter;
107 GtkListStore *store;
108 gchar buffer1[80];
109 gchar buffer2[32];
110 gchar tmp1[16], tmp2[16];
111 gdouble lat1, lon1, lat2, lon2;
112 gdouble sum=0.0;
113
114 if (path->whead==path->wtail)
115         return NULL;
116
117 wcurr=path->whead;
118
119 if (!wcurr)
120         return NULL;
121
122 if (!wcurr->point)
123         return NULL;
124
125 unit2latlon(wcurr->point->unitx, wcurr->point->unity, lat1, lon1);
126
127 store=gtk_list_store_new(ROUTE_NUM_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
128
129 while (wcurr!=path->wtail) {
130         if (!wcurr) {
131                 break;
132         }
133
134         if (!wcurr->point) {
135                 g_debug("No point for waypoint (%s) skipping", wcurr->desc);
136                 wcurr++;
137                 continue;
138         }
139
140         unit2latlon(wcurr->point->unitx, wcurr->point->unity, lat2, lon2);
141         lat_format(_degformat, lat2, tmp1);
142         lon_format(_degformat, lon2, tmp2);
143
144         g_snprintf(buffer1, sizeof(buffer1), "%s,%s", tmp1, tmp2);
145         sum += calculate_distance(lat1, lon1, lat2, lon2);
146         g_snprintf(buffer2, sizeof(buffer2), "%.02f %s", sum * UNITS_CONVERT[_units], UNITS_TEXT[_units]);
147
148         gtk_list_store_append(store, &iter);
149         gtk_list_store_set(store, &iter,
150         ROUTE_LATLON, buffer1,
151                 ROUTE_DISTANCE, buffer2,
152                 ROUTE_WAYPOINT, wcurr->desc,
153                 ROUTE_LAT, lat2,
154                 ROUTE_LON, lon2,
155                 -1);
156
157         lat1=lat2;
158         lon1=lon2;
159
160         wcurr++;
161 }
162
163 return store;
164 }