]> err.no Git - mapper/blob - src/path.c
Merge branch 'master' of ssh://git.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 void 
15 path_resize(Path *path, guint size)
16 {
17 if (path->head + size != path->cap) {
18         Point *old_head = path->head;
19         WayPoint *curr;
20         path->head = g_renew(Point, old_head, size);
21         path->cap = path->head + size;
22         if (path->head != old_head) {
23                 path->tail = path->head + (path->tail - old_head);
24
25                 /* Adjust all of the waypoints. */
26                 for (curr = path->whead - 1; curr++ != path->wtail;)
27                         curr->point = path->head + (curr->point - old_head);
28         }
29 }
30 }
31
32 void 
33 path_wresize(Path *path, guint wsize)
34 {
35 if (path->whead + wsize != path->wcap) {
36         WayPoint *old_whead = path->whead;
37         path->whead = g_renew(WayPoint, old_whead, wsize);
38         path->wtail = path->whead + (path->wtail - old_whead);
39         path->wcap = path->whead + wsize;
40 }
41 }
42
43 GtkListStore *
44 path_generate_store(Path *path)
45 {
46 WayPoint *wcurr;
47 GtkTreeIter iter;
48 GtkListStore *store;
49 gchar buffer1[80];
50 gchar buffer2[32];
51 gdouble lat1, lon1, lat2, lon2;
52 gdouble sum=0.0;
53
54 if (path->whead==path->wtail)
55         return NULL;
56
57 wcurr=path->whead;
58
59 if (!wcurr)
60         return NULL;
61
62 if (!wcurr->point)
63         return NULL;
64
65 unit2latlon(wcurr->point->unitx, wcurr->point->unity, lat1, lon1);
66
67 store = gtk_list_store_new(ROUTE_NUM_COLUMNS,G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
68
69 while (wcurr!=path->wtail) {
70         if (!wcurr)
71                 break;
72
73         if (!wcurr->point)
74                 break;
75
76         unit2latlon(wcurr->point->unitx, wcurr->point->unity, lat2, lon2);
77         g_snprintf(buffer1, sizeof(buffer1), "%.05f,%.05f", lat2, lon2);
78         sum += calculate_distance(lat1, lon1, lat2, lon2);
79         g_snprintf(buffer2, sizeof(buffer2), "%.02f %s", sum * UNITS_CONVERT[_units], UNITS_TEXT[_units]);
80
81         gtk_list_store_append(store, &iter);
82         gtk_list_store_set(store, &iter,
83         ROUTE_LATLON, buffer1,
84                 ROUTE_DISTANCE, buffer2,
85                 ROUTE_WAYPOINT, wcurr->desc,
86                 ROUTE_LAT, lat2,
87                 ROUTE_LON, lon2,
88                 -1);
89
90         lat1=lat2;
91         lon1=lon2;
92
93         wcurr++;
94 }
95
96 return store;
97 }