Contents of /trunk/src/map-tool.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 61 - (show annotations)
Tue Aug 18 14:32:45 2009 UTC (14 years, 8 months ago) by harbaum
File MIME type: text/plain
File size: 16637 byte(s)
osm-gps-map navigator
1 /*
2 * Copyright (C) 2008-2009 Till Harbaum <till@harbaum.org>.
3 *
4 * This file is part of GPXView.
5 *
6 * GPXView is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GPXView is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GPXView. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "gpxview.h"
21 #include "converter.h"
22 #include <math.h> // for isnan
23
24 #ifdef ENABLE_OSM_GPS_MAP
25 #include "osm-gps-map.h"
26 #endif
27
28 #define MAP_SOURCE OSM_GPS_MAP_SOURCE_OPENSTREETMAP
29 #define GPS_DEFAULT_ZOOM 13
30
31 #define PROXY_KEY "/system/http_proxy/"
32
33 static const char *get_proxy_uri(appdata_t *appdata) {
34 static char proxy_buffer[64] = "";
35
36 /* use environment settings if preset */
37 const char *proxy = g_getenv("http_proxy");
38 if(proxy) {
39 printf("http_proxy: %s\n", proxy);
40 return proxy;
41 }
42
43 /* ------------- get proxy settings -------------------- */
44 if(gconf_client_get_bool(appdata->gconf_client,
45 PROXY_KEY "use_http_proxy", NULL)) {
46
47 /* we can savely ignore things like "ignore_hosts" since we */
48 /* are pretty sure not inside the net of one of our map renderers */
49 /* (unless the user works at google :-) */
50
51 /* get basic settings */
52 char *host =
53 gconf_client_get_string(appdata->gconf_client, PROXY_KEY "host", NULL);
54 if(host) {
55 int port =
56 gconf_client_get_int(appdata->gconf_client, PROXY_KEY "port", NULL);
57
58 snprintf(proxy_buffer, sizeof(proxy_buffer),
59 "http://%s:%u", host, port);
60
61 g_free(host);
62 }
63 return proxy_buffer;
64 }
65
66 return NULL;
67 }
68
69 static void map_zoom(map_context_t *context, int step) {
70 gint zoom;
71 OsmGpsMap *map = OSM_GPS_MAP(context->widget);
72 g_object_get(map, "zoom", &zoom, NULL);
73 zoom = osm_gps_map_set_zoom(map, zoom+step);
74
75 /* enable/disable zoom buttons as required */
76 gtk_widget_set_sensitive(context->zoomin,
77 zoom < osm_gps_map_source_get_max_zoom(MAP_SOURCE));
78 gtk_widget_set_sensitive(context->zoomout,
79 zoom > osm_gps_map_source_get_min_zoom(MAP_SOURCE));
80
81 /* save new zoom */
82 context->appdata->map.zoom = zoom;
83 }
84
85 static gboolean
86 cb_map_zoomin(GtkButton *button, map_context_t *context) {
87 map_zoom(context, +1);
88 return FALSE;
89 }
90
91 static gboolean
92 cb_map_zoomout(GtkButton *button, map_context_t *context) {
93 map_zoom(context, -1);
94 return FALSE;
95 }
96
97 static gboolean
98 cb_map_gps(GtkButton *button, map_context_t *context) {
99 pos_t *refpos = get_pos(context->appdata);
100 if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {
101 osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
102 refpos->lat, refpos->lon, GPS_DEFAULT_ZOOM);
103 } else {
104 /* no coordinates given: display the entire world */
105 osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
106 0.0, 0.0, 1);
107 }
108
109 return FALSE;
110 }
111
112 static GtkWidget
113 *map_add_button(int icon, GCallback cb, gpointer data,
114 char *tooltip) {
115 GtkWidget *button = gtk_button_new();
116 gtk_button_set_image(GTK_BUTTON(button), icon_get_widget(ICON_MISC, icon));
117 g_signal_connect(button, "clicked", cb, data);
118 #ifndef USE_MAEMO
119 gtk_widget_set_tooltip_text(button, tooltip);
120 #endif
121 return button;
122 }
123
124 static int dist2pixel(map_context_t *context, float km, float lat) {
125 return 1000.0*km/osm_gps_map_get_scale(OSM_GPS_MAP(context->widget));
126 }
127
128 static gboolean map_gps_update(gpointer data) {
129 map_context_t *context = (map_context_t*)data;
130
131 /* get reference position ... */
132 pos_t *refpos = get_pos(context->appdata);
133 gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon);
134
135 /* ... and enable "goto" button if it's valid */
136 gtk_widget_set_sensitive(context->gps, ok);
137
138 if(ok) {
139 float heading = NAN;
140 int radius = 0;
141
142 if(context->appdata->use_gps) {
143 heading = gps_get_heading(context->appdata);
144
145 /* get error */
146 float eph = gps_get_eph(context->appdata);
147 if(!isnan(eph))
148 radius = dist2pixel(context, eph/1000, refpos->lat);
149 }
150
151 g_object_set(context->widget, "gps-track-highlight-radius", radius, NULL);
152 osm_gps_map_draw_gps(OSM_GPS_MAP(context->widget),
153 refpos->lat, refpos->lon, heading);
154 } else
155 osm_gps_map_clear_gps(OSM_GPS_MAP(context->widget));
156
157 return TRUE;
158 }
159
160 static gboolean on_map_configure(GtkWidget *widget,
161 GdkEventConfigure *event,
162 map_context_t *context) {
163
164 /* set default values if they are invalid */
165 if(!context->appdata->map.zoom ||
166 isnan(context->appdata->map.pos.lat) ||
167 isnan(context->appdata->map.pos.lon)) {
168 printf("no valid map position found\n");
169
170 pos_t *refpos = get_pos(context->appdata);
171 if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {
172 /* use gps position if present */
173 context->appdata->map.pos = *refpos;
174 context->appdata->map.zoom = GPS_DEFAULT_ZOOM;
175 } else {
176 /* use world map otherwise */
177 context->appdata->map.pos.lat = 0.0;
178 context->appdata->map.pos.lon = 0.0;
179 context->appdata->map.zoom = 1;
180 }
181 }
182
183 /* jump to initial position */
184 osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
185 context->appdata->map.pos.lat,
186 context->appdata->map.pos.lon,
187 context->appdata->map.zoom);
188
189 return FALSE;
190 }
191
192 static void map_draw_cachelist(GtkWidget *map, cache_t *cache) {
193 while(cache) {
194 GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type);
195
196 osm_gps_map_add_image(OSM_GPS_MAP(map),
197 cache->pos.lat, cache->pos.lon, icon);
198
199 cache = cache->next;
200 }
201 }
202
203 static void
204 map_cachelist_nearest(cache_t *cache, pos_t *pos,
205 cache_t **result, float *distance) {
206 while(cache) {
207 float dist =
208 pow(cache->pos.lat - pos->lat, 2) +
209 pow(cache->pos.lon - pos->lon, 2);
210
211 if(!(dist > *distance)) {
212 *result = cache;
213 *distance = dist;
214 }
215
216 cache = cache->next;
217 }
218 }
219
220 static cache_t *map_closest(map_context_t *context, pos_t *pos) {
221 cache_t *result = NULL;
222 float distance = NAN;
223
224 #ifdef USE_MAEMO
225 if(!context->appdata->cur_gpx) {
226 #endif
227 /* search all geocaches */
228 gpx_t *gpx = context->appdata->gpx;
229 while(gpx) {
230 map_cachelist_nearest(gpx->cache, pos, &result, &distance);
231 gpx = gpx->next;
232 }
233 #ifdef USE_MAEMO
234 } else {
235 map_cachelist_nearest(context->appdata->cur_gpx->cache,
236 pos, &result, &distance);
237 }
238 #endif
239
240 return result;
241 }
242
243 /* translate between osm-gps-map positions and gpxview ones */
244 pos_t coord2pos(coord_t coo) {
245 pos_t pos;
246 pos.lat = rad2deg(coo.rlat);
247 pos.lon = rad2deg(coo.rlon);
248 return pos;
249 }
250
251 #define CLICK_FUZZ (24)
252
253 static gboolean
254 on_map_button_press_event(GtkWidget *widget,
255 GdkEventButton *event, map_context_t *context) {
256 OsmGpsMap *map = OSM_GPS_MAP(context->widget);
257
258 /* got a press event without release event? eat it! */
259 if(context->press_on != NULL) {
260 printf("PRESS: already\n");
261 return TRUE;
262 }
263
264 pos_t pos =
265 coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));
266
267 cache_t *nearest = map_closest(context, &pos);
268 if(nearest) {
269 float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE);
270 if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ)
271 context->press_on = nearest;
272 }
273
274 return FALSE;
275 }
276
277 static void
278 cairo_draw_pixbuf(cairo_t *cr, GdkPixbuf *buf, gint x, gint y) {
279 /* convert the pixbuf into something cairo can handle */
280
281 // Create a new ImageSurface
282 cairo_surface_t *image_surface =
283 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
284 gdk_pixbuf_get_width(buf),
285 gdk_pixbuf_get_height(buf));
286
287 // Create the new Context for the ImageSurface
288 cairo_t *context = cairo_create(image_surface);
289
290 // Draw the image on the new Context
291 gdk_cairo_set_source_pixbuf(context, buf, 0.0, 0.0);
292 cairo_paint(context);
293
294 // now draw this onto the original context
295 cairo_set_source_surface(cr, image_surface, x, y);
296
297 cairo_paint(cr);
298 }
299
300 #define LINE_SKIP 7
301
302 static void
303 balloon_draw_cb(cairo_t *cr, OsmGpsMapRect_t *rect, gpointer data) {
304 cache_t *cache = (cache_t*)data;
305
306 // printf("draw cb for \"%s\"\n", cache->name);
307
308 #if 0
309 /* draw pink background to check clipping */
310 cairo_rectangle (cr, rect->x-20, rect->y-20, rect->w+40, rect->h+40);
311 cairo_set_source_rgba (cr, 1, 0, 0, 0.3);
312 cairo_fill_preserve (cr);
313 cairo_set_line_width (cr, 0);
314 cairo_stroke (cr);
315 #endif
316
317 /* leave a little border top and left */
318 gint x = rect->x, y = rect->y;
319
320 /* draw the cache type icon ... */
321 GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type);
322 cairo_draw_pixbuf(cr, icon, x, y);
323
324 /* ... and right of it the waypoint id */
325 cairo_text_extents_t extents;
326
327 if(cache->id) {
328 cairo_select_font_face (cr, "Sans",
329 CAIRO_FONT_SLANT_NORMAL,
330 CAIRO_FONT_WEIGHT_BOLD);
331
332 cairo_set_font_size (cr, 20.0);
333 cairo_text_extents (cr, cache->id, &extents);
334
335 /* display id right of icon vertically centered */
336 x += gdk_pixbuf_get_width(icon) + 5;
337 y += (gdk_pixbuf_get_height(icon) + extents.height)/2;
338 cairo_move_to (cr, x, y);
339 cairo_set_source_rgba (cr, 0, 0, 0, 1);
340 cairo_show_text (cr, cache->id);
341 cairo_stroke (cr);
342
343 y += (gdk_pixbuf_get_height(icon) - extents.height)/2 + LINE_SKIP;
344 } else
345 y += gdk_pixbuf_get_height(icon);
346
347 /* return to the left border and below icon/text */
348 x = rect->x;
349
350 /* everything from here uses the same font */
351 cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
352 CAIRO_FONT_WEIGHT_NORMAL);
353 cairo_set_font_size (cr, 14.0);
354
355 if(cache->name) {
356 /* draw cache name */
357 cairo_text_extents (cr, cache->name, &extents);
358 y += extents.height;
359 cairo_move_to (cr, x, y);
360 cairo_set_source_rgba (cr, 0, 0, 0, 1);
361 cairo_show_text (cr, cache->name);
362 cairo_stroke (cr);
363
364 /* return to the left border and below text */
365 y += LINE_SKIP;
366 x = rect->x;
367 }
368
369 if(cache->terrain) {
370 /* draw cache rating */
371 const char *terrain = "Terrain:";
372 icon = icon_get(ICON_STARS, (int)(cache->terrain*2-2));
373 cairo_text_extents (cr, _(terrain), &extents);
374 y += (gdk_pixbuf_get_height(icon) + extents.height)/2;
375
376 /* draw "Terrain:" string */
377 cairo_move_to (cr, x, y);
378 cairo_set_source_rgba (cr, 0, 0, 0, 1);
379 cairo_show_text (cr, _(terrain));
380 cairo_stroke (cr);
381 x += extents.width + 2;
382
383 /* draw terrain stars */
384 cairo_draw_pixbuf(cr, icon, x, y -
385 (gdk_pixbuf_get_height(icon) + extents.height)/2);
386
387 x += gdk_pixbuf_get_width(icon) + LINE_SKIP;
388 y -= (gdk_pixbuf_get_height(icon) + extents.height)/2;
389 }
390
391 if(cache->difficulty) {
392 const char *difficulty = "Difficulty:";
393 cairo_text_extents (cr, _(difficulty), &extents);
394 y += (gdk_pixbuf_get_height(icon) + extents.height)/2;
395
396 /* draw "Difficulty:" string */
397 cairo_move_to (cr, x, y);
398 cairo_set_source_rgba (cr, 0, 0, 0, 1);
399 cairo_show_text (cr, _(difficulty));
400 cairo_stroke (cr);
401 x += extents.width + 2;
402
403 icon = icon_get(ICON_STARS, (int)(cache->difficulty*2-2));
404 cairo_draw_pixbuf(cr, icon, x, y -
405 (gdk_pixbuf_get_height(icon) + extents.height)/2);
406 }
407 }
408
409 static gboolean
410 on_map_button_release_event(GtkWidget *widget,
411 GdkEventButton *event, map_context_t *context) {
412 OsmGpsMap *map = OSM_GPS_MAP(context->widget);
413
414 if(context->press_on) {
415 coord_t coo;
416 coo = osm_gps_map_get_co_ordinates(map, event->x, event->y);
417
418 pos_t pos =
419 coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));
420
421 cache_t *nearest = map_closest(context, &pos);
422 if(nearest && nearest == context->press_on) {
423 float dist = gpx_pos_get_distance(pos, nearest->pos, FALSE);
424 if(dist2pixel(context, dist, nearest->pos.lat) < CLICK_FUZZ) {
425
426 osm_gps_map_draw_balloon(map, nearest->pos.lat, nearest->pos.lon,
427 balloon_draw_cb, nearest);
428 }
429 }
430 context->press_on = NULL;
431 } else {
432 /* save new map position */
433 gfloat lat, lon;
434 g_object_get(map, "latitude", &lat, "longitude", &lon, NULL);
435 context->appdata->map.pos.lat = lat;
436 context->appdata->map.pos.lon = lon;
437 }
438
439 return FALSE;
440 }
441
442 static void on_window_destroy(GtkWidget *widget, map_context_t *context) {
443 appdata_t *appdata = context->appdata;
444
445 printf("destroy map window\n");
446
447 /* save map parameters */
448 OsmGpsMap *map = OSM_GPS_MAP(context->widget);
449 gint zoom;
450 g_object_get(map, "zoom", &zoom, NULL);
451 context->appdata->map.zoom = zoom;
452
453 gfloat lat, lon;
454 g_object_get(map, "latitude", &lat, "longitude", &lon, NULL);
455 context->appdata->map.pos.lat = lat;
456 context->appdata->map.pos.lon = lon;
457
458 #if MAEMO_VERSION_MAJOR == 5
459 /* restore cur_view */
460 context->appdata->cur_view = context->old_view;
461 #endif
462
463 gtk_timeout_remove(context->handler_id);
464
465 g_free(context);
466 appdata->map.context = NULL;
467 }
468
469 void map(appdata_t *appdata) {
470 map_context_t *context = NULL;
471
472 /* if the map window already exists, just raise it */
473 if(appdata->map.context) {
474 gtk_window_present(GTK_WINDOW(appdata->map.context->window));
475 return;
476 }
477
478 context = appdata->map.context = g_new0(map_context_t, 1);
479 context->appdata = appdata;
480
481 GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
482
483 char *path = g_strdup_printf("%s/map/", appdata->image_path);
484 const char *proxy = get_proxy_uri(appdata);
485
486 context->widget = g_object_new(OSM_TYPE_GPS_MAP,
487 "map-source", MAP_SOURCE,
488 "tile-cache", path,
489 "auto-center", FALSE,
490 "record-trip-history", FALSE,
491 "show-trip-history", FALSE,
492 proxy?"proxy-uri":NULL, proxy,
493 NULL);
494
495 g_free(path);
496
497 char *name = NULL;
498 #ifdef USE_MAEMO
499 if(!appdata->cur_gpx) {
500 #endif
501 /* draw all geocaches */
502 gpx_t *gpx = appdata->gpx;
503 while(gpx) {
504 map_draw_cachelist(context->widget, gpx->cache);
505 gpx = gpx->next;
506 }
507 name = g_strdup(_("all geocaches"));
508 #ifdef USE_MAEMO
509 } else {
510 map_draw_cachelist(context->widget, appdata->cur_gpx->cache);
511 name = g_strdup(appdata->cur_gpx->name);
512 }
513 #endif
514
515 char *title = g_strdup_printf(_("Map - %s"), name);
516 g_free(name);
517
518 #ifdef USE_MAEMO
519 #ifdef USE_STACKABLE_WINDOW
520 context->window = hildon_stackable_window_new();
521 #else
522 context->window = hildon_window_new();
523 #endif
524 #else
525 context->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
526 #endif
527
528 gtk_window_set_title(GTK_WINDOW(context->window), title);
529
530 #ifndef USE_MAEMO
531 gtk_window_set_default_size(GTK_WINDOW(context->window), 640, 480);
532 #endif
533
534 g_free(title);
535
536 g_signal_connect(G_OBJECT(context->widget), "configure-event",
537 G_CALLBACK(on_map_configure), context);
538
539 g_signal_connect(G_OBJECT(context->widget), "button-press-event",
540 G_CALLBACK(on_map_button_press_event), context);
541
542 g_signal_connect(G_OBJECT(context->widget), "button-release-event",
543 G_CALLBACK(on_map_button_release_event), context);
544
545 gtk_box_pack_start_defaults(GTK_BOX(hbox), context->widget);
546 /* zoom button box */
547 GtkWidget *vbox = gtk_vbox_new(FALSE,0);
548
549 context->zoomin =
550 map_add_button(10, G_CALLBACK(cb_map_zoomin),
551 context, _("Zoom in"));
552 gtk_box_pack_start(GTK_BOX(vbox), context->zoomin, FALSE, FALSE, 0);
553
554 context->zoomout =
555 map_add_button(11, G_CALLBACK(cb_map_zoomout),
556 context, _("Zoom out"));
557 gtk_box_pack_start(GTK_BOX(vbox), context->zoomout, FALSE, FALSE, 0);
558
559 context->gps =
560 map_add_button(9, G_CALLBACK(cb_map_gps),
561 context, _("Jump to GPS position"));
562 gtk_widget_set_sensitive(context->gps, FALSE);
563
564 /* install handler for timed updates of the gps button */
565 context->handler_id = gtk_timeout_add(1000, map_gps_update, context);
566 gtk_box_pack_start(GTK_BOX(vbox), context->gps, FALSE, FALSE, 0);
567
568 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
569
570 #if MAEMO_VERSION_MAJOR == 5
571 /* prevent some of the main screen things */
572 context->old_view = appdata->cur_view;
573 appdata->cur_view = NULL;
574 #endif
575
576 g_signal_connect(G_OBJECT(context->window), "destroy",
577 G_CALLBACK(on_window_destroy), context);
578
579 gtk_container_add(GTK_CONTAINER(context->window), hbox);
580 gtk_widget_show_all(GTK_WIDGET(context->window));
581 }