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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 143 by harbaum, Mon Oct 26 19:55:00 2009 UTC revision 152 by harbaum, Mon Nov 2 10:53:01 2009 UTC
# Line 22  Line 22 
22   * http://topo.geofabrik.de/trail/${z}/${x}/${y}.png   8-15   * http://topo.geofabrik.de/trail/${z}/${x}/${y}.png   8-15
23   */   */
24    
25    /*
26     * TODO:
27     * - draw caches only once
28     * - prefer duplicates from selected files
29     * - make semi-transparent caches selectable
30     */
31    
32  #include "gpxview.h"  #include "gpxview.h"
33  #include "converter.h"  #include "converter.h"
34  #include <math.h>    // for isnan  #include <math.h>    // for isnan
# Line 45  Line 52 
52  static const char *get_proxy_uri(appdata_t *appdata) {  static const char *get_proxy_uri(appdata_t *appdata) {
53    static char proxy_buffer[64] = "";    static char proxy_buffer[64] = "";
54    
   printf("get_proxy_uri in\n");  
   
55    /* use environment settings if preset */    /* use environment settings if preset */
56    const char *proxy = g_getenv("http_proxy");    const char *proxy = g_getenv("http_proxy");
57    if(proxy) {    if(proxy) {
# Line 77  static const char *get_proxy_uri(appdata Line 82  static const char *get_proxy_uri(appdata
82      return proxy_buffer;      return proxy_buffer;
83    }    }
84    
   printf("get_proxy_uri out\n");  
85    return NULL;    return NULL;
86  }  }
87    
88  static void  static void
89  cb_map_gps(osd_button_t but, map_context_t *context) {  cb_map_gps(osd_button_t but, map_context_t *context) {
   printf("cb_map_gps in\n");  
90    
91    if(but == OSD_GPS) {    if(but == OSD_GPS) {
92      pos_t *refpos = get_pos(context->appdata);      pos_t *refpos = get_pos(context->appdata);
# Line 105  cb_map_gps(osd_button_t but, map_context Line 108  cb_map_gps(osd_button_t but, map_context
108                                  0.0, 0.0, 1);                                  0.0, 0.0, 1);
109      }      }
110    }    }
   printf("cb_map_gps out\n");  
111  }  }
112    
113  static int dist2pixel(map_context_t *context, float km, float lat) {  static int dist2pixel(map_context_t *context, float km, float lat) {
# Line 114  static int dist2pixel(map_context_t *con Line 116  static int dist2pixel(map_context_t *con
116    
117  static gboolean map_gps_update(gpointer data) {  static gboolean map_gps_update(gpointer data) {
118    map_context_t *context = (map_context_t*)data;    map_context_t *context = (map_context_t*)data;
119      static gboolean goto_is_enabled = FALSE;
   printf("map_gps_update in\n");  
120    
121    /* get reference position ... */    /* get reference position ... */
122    pos_t *refpos = get_pos(context->appdata);    pos_t *refpos = get_pos(context->appdata);
123    gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon);    gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon);
124    
125    /* ... and enable "goto" button if it's valid */    /* ... and enable "goto" button if it's valid */
126    osm_gps_map_osd_enable_gps (OSM_GPS_MAP(context->widget),    if(ok != goto_is_enabled) {
127                   OSM_GPS_MAP_OSD_CALLBACK(ok?cb_map_gps:NULL), context);      osm_gps_map_osd_enable_gps (OSM_GPS_MAP(context->widget),
128                    OSM_GPS_MAP_OSD_CALLBACK(ok?cb_map_gps:NULL), context);
129        goto_is_enabled = ok;
130      }
131    
132    if(ok) {    if(ok) {
133      float heading = NAN;      float heading = NAN;
# Line 138  static gboolean map_gps_update(gpointer Line 142  static gboolean map_gps_update(gpointer
142          radius = dist2pixel(context, eph/1000, refpos->lat);          radius = dist2pixel(context, eph/1000, refpos->lat);
143      }      }
144    
145        /* TODO: in order to save energy: only draw if state actually changed */
146    
147      g_object_set(context->widget, "gps-track-highlight-radius", radius, NULL);      g_object_set(context->widget, "gps-track-highlight-radius", radius, NULL);
148      osm_gps_map_draw_gps(OSM_GPS_MAP(context->widget),      osm_gps_map_draw_gps(OSM_GPS_MAP(context->widget),
149                           refpos->lat, refpos->lon, heading);                           refpos->lat, refpos->lon, heading);
150    } else    } else
151      osm_gps_map_clear_gps(OSM_GPS_MAP(context->widget));      osm_gps_map_clear_gps(OSM_GPS_MAP(context->widget));
152    
   printf("map_gps_update out\n");  
153    return TRUE;    return TRUE;
154  }  }
155    
156  static void map_draw_cache(GtkWidget *map, cache_t *cache) {  static void map_draw_cache(GtkWidget *map, cache_t *cache, gboolean semi) {
157    printf("map_draw_cache in\n");    GdkPixbuf *icon = icon_get(semi?ICON_CACHE_TYPE_SEMI:ICON_CACHE_TYPE,
158                                 cache->type);
   GdkPixbuf *icon = icon_get(ICON_CACHE_TYPE, cache->type);  
159    
160    /* check if there's also an overwritten coordinate */    /* check if there's also an overwritten coordinate */
161    if(cache->notes && cache->notes->override) {    if(cache->notes && cache->notes->override) {
# Line 167  static void map_draw_cache(GtkWidget *ma Line 171  static void map_draw_cache(GtkWidget *ma
171        osm_gps_map_add_image(OSM_GPS_MAP(map),        osm_gps_map_add_image(OSM_GPS_MAP(map),
172                              cache->pos.lat, cache->pos.lon, icon);                              cache->pos.lat, cache->pos.lon, icon);
173    }    }
   printf("map_draw_cache out\n");  
174  }  }
175    
176  static void map_draw_gpx(appdata_t *appdata, GtkWidget *map, gpx_t *gpx) {  static void map_draw_gpx(appdata_t *appdata,
177    printf("map_draw_gpx in\n");                           GtkWidget *map, gpx_t *gpx,
178                             cache_t *nav, gboolean semi) {
179    if(!gpx->notes_loaded) {    if(!gpx->notes_loaded) {
180      notes_load_all(appdata, gpx);      notes_load_all(appdata, gpx);
181      gpx->notes_loaded = TRUE;      gpx->notes_loaded = TRUE;
# Line 180  static void map_draw_gpx(appdata_t *appd Line 183  static void map_draw_gpx(appdata_t *appd
183    
184    cache_t *cache = gpx->cache;    cache_t *cache = gpx->cache;
185    while(cache) {    while(cache) {
186      map_draw_cache(map, cache);      /* if nav is given draw all other caches semitransparent. */
187        /* if nav is not given do what semi sais */
188        map_draw_cache(map, cache, nav?(cache != nav):semi);
189      cache = cache->next;      cache = cache->next;
190    }    }
   printf("map_draw_gpx out\n");  
191  }  }
192    
193  /* draw geocaches and set window title */  /* draw geocaches and set window title */
194  static void map_setup(map_context_t *context) {  static void map_setup(map_context_t *context) {
195    char *name = NULL;    char *name = NULL;
196    
197    printf("map_setup in\n");    int cache_num = gpx_total_caches_global(context->appdata->gpx);
198    
199      if(context->caches_displayed && (cache_num != context->cache_list_len)) {
200        g_free(context->caches_displayed);
201        context->caches_displayed = NULL;
202        context->cache_list_len = 0;
203      }
204    
205      /* allocate buffer */
206      if(cache_num) {
207        context->cache_list_len = cache_num;
208        context->caches_displayed = g_new0(cache_display_t, cache_num);
209        printf("allocated space to handle %d map icons\n", cache_num);
210      }
211    
212    if(!context->appdata->cur_gpx && !context->appdata->cur_cache) {    if(!context->appdata->cur_gpx && !context->appdata->cur_cache) {
213      if(context->state != MAP_ALL) {      if(context->state != MAP_ALL) {
# Line 201  static void map_setup(map_context_t *con Line 218  static void map_setup(map_context_t *con
218        osm_gps_map_osd_clear_nav (OSM_GPS_MAP(context->widget));        osm_gps_map_osd_clear_nav (OSM_GPS_MAP(context->widget));
219  #endif  #endif
220    
221        /* clear all existing ccahe images */        /* clear all existing cache images */
222        osm_gps_map_clear_images (OSM_GPS_MAP(context->widget));        osm_gps_map_clear_images (OSM_GPS_MAP(context->widget));
223    
224        /* draw all geocaches */        /* draw all geocaches and none is semi-transparent */
225        gpx_t *gpx = context->appdata->gpx;        gpx_t *gpx = context->appdata->gpx;
226        while(gpx) {        while(gpx) {
227          map_draw_gpx(context->appdata, context->widget, gpx);          map_draw_gpx(context->appdata, context->widget, gpx, NULL, FALSE);
228          gpx = gpx->next;          gpx = gpx->next;
229        }        }
230        name = g_strdup(_("all"));        name = g_strdup(_("all"));
# Line 225  static void map_setup(map_context_t *con Line 242  static void map_setup(map_context_t *con
242        /* clear all existing ccahe images */        /* clear all existing ccahe images */
243        osm_gps_map_clear_images (OSM_GPS_MAP(context->widget));        osm_gps_map_clear_images (OSM_GPS_MAP(context->widget));
244    
245        map_draw_gpx(context->appdata, context->widget,        /* draw all geocaches and all other gpx files are semi-transparent */
246                     context->appdata->cur_gpx);        gpx_t *gpx = context->appdata->gpx;
247          while(gpx) {
248            map_draw_gpx(context->appdata, context->widget, gpx, NULL,
249                         gpx != context->appdata->cur_gpx);
250            gpx = gpx->next;
251          }
252    
253        name = g_strdup(context->appdata->cur_gpx->name);        name = g_strdup(context->appdata->cur_gpx->name);
254        context->state = MAP_GPX;        context->state = MAP_GPX;
255      }      }
# Line 242  static void map_setup(map_context_t *con Line 265  static void map_setup(map_context_t *con
265      /* clear all existing ccahe images */      /* clear all existing ccahe images */
266      osm_gps_map_clear_images (OSM_GPS_MAP(context->widget));      osm_gps_map_clear_images (OSM_GPS_MAP(context->widget));
267    
268      map_draw_cache(context->widget, cache);      /* draw all geocaches and all but selected one are semi-transparent */
269        gpx_t *gpx = context->appdata->gpx;
270        while(gpx) {
271          map_draw_gpx(context->appdata, context->widget, gpx, cache, FALSE);
272          gpx = gpx->next;
273        }
274    
275      name = g_strdup(cache->name);      name = g_strdup(cache->name);
276      context->state = MAP_CACHE;      context->state = MAP_CACHE;
277    
# Line 267  static void map_setup(map_context_t *con Line 296  static void map_setup(map_context_t *con
296      g_free(title);      g_free(title);
297    } else    } else
298      printf("map_setup(keep)\n");      printf("map_setup(keep)\n");
   
   printf("map_setup out\n");  
299  }  }
300    
301  static gboolean on_map_configure(GtkWidget *widget,  static gboolean on_map_configure(GtkWidget *widget,
# Line 325  static gboolean on_map_configure(GtkWidg Line 352  static gboolean on_map_configure(GtkWidg
352      context->map_complete = TRUE;      context->map_complete = TRUE;
353    }    }
354    
   printf("map configure done\n");  
355    return FALSE;    return FALSE;
356  }  }
357    
358  static void  static void
359  map_cachelist_nearest(cache_t *cache, pos_t *pos,  map_cachelist_nearest(cache_t *cache, pos_t *pos,
360                        cache_t **result, float *distance) {                        cache_t **result, float *distance) {
361    printf("map_cachelist_nearest in\n");  
362    while(cache) {    while(cache) {
363      pos_t cpos = gpx_cache_pos(cache);      pos_t cpos = gpx_cache_pos(cache);
364    
# Line 347  map_cachelist_nearest(cache_t *cache, po Line 373  map_cachelist_nearest(cache_t *cache, po
373    
374      cache = cache->next;      cache = cache->next;
375    }    }
   printf("map_cachelist_nearest out\n");  
376  }  }
377    
378  static cache_t *map_closest(map_context_t *context, pos_t *pos) {  static cache_t *map_closest(map_context_t *context, pos_t *pos) {
379    cache_t *result = NULL;    cache_t *result = NULL;
380    float distance = NAN;    float distance = NAN;
381    
   printf("map_closest in\n");  
   
382    if(!context->appdata->cur_gpx && !context->appdata->cur_cache) {    if(!context->appdata->cur_gpx && !context->appdata->cur_cache) {
383      /* search all geocaches */      /* search all geocaches */
384      gpx_t *gpx = context->appdata->gpx;      gpx_t *gpx = context->appdata->gpx;
# Line 369  static cache_t *map_closest(map_context_ Line 392  static cache_t *map_closest(map_context_
392    } else    } else
393      result = context->appdata->cur_gpx->cache;      result = context->appdata->cur_gpx->cache;
394    
   printf("map_closest out\n");  
395    return result;    return result;
396  }  }
397    
# Line 386  pos_t coord2pos(coord_t coo) { Line 408  pos_t coord2pos(coord_t coo) {
408  static gboolean  static gboolean
409  on_map_button_press_event(GtkWidget *widget,  on_map_button_press_event(GtkWidget *widget,
410                              GdkEventButton *event, map_context_t *context) {                              GdkEventButton *event, map_context_t *context) {
   printf("on_map_button_press_event in\n");  
411    
412    OsmGpsMap *map = OSM_GPS_MAP(context->widget);    OsmGpsMap *map = OSM_GPS_MAP(context->widget);
413    
414    /* check if we actually clicked parts of the OSD */    /* check if we actually clicked parts of the OSD */
415    if(osm_gps_map_osd_check(map, event->x, event->y) != OSD_NONE) {    if(osm_gps_map_osd_check(map, event->x, event->y) != OSD_NONE)
     printf("on_map_button_press_event out 1\n");  
416      return FALSE;      return FALSE;
   }  
417    
418    /* got a press event without release event? eat it! */    /* got a press event without release event? eat it! */
419    if(context->press_on != NULL) {    if(context->press_on != NULL) {
# Line 414  on_map_button_press_event(GtkWidget *wid Line 433  on_map_button_press_event(GtkWidget *wid
433        context->press_on = nearest;        context->press_on = nearest;
434    }    }
435    
   printf("on_map_button_press_event out\n");  
436    return FALSE;    return FALSE;
437  }  }
438    
# Line 422  static void Line 440  static void
440  cairo_draw_pixbuf(cairo_t *cr, GdkPixbuf *buf, gint x, gint y) {  cairo_draw_pixbuf(cairo_t *cr, GdkPixbuf *buf, gint x, gint y) {
441    /* convert the pixbuf into something cairo can handle */    /* convert the pixbuf into something cairo can handle */
442    
   printf("cairo_draw_pixbuf in\n");  
   
443    // Create a new ImageSurface    // Create a new ImageSurface
444    cairo_surface_t *image_surface =    cairo_surface_t *image_surface =
445      cairo_image_surface_create(CAIRO_FORMAT_ARGB32,      cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
# Line 431  cairo_draw_pixbuf(cairo_t *cr, GdkPixbuf Line 447  cairo_draw_pixbuf(cairo_t *cr, GdkPixbuf
447                                 gdk_pixbuf_get_height(buf));                                 gdk_pixbuf_get_height(buf));
448    
449    // Create the new Context for the ImageSurface    // Create the new Context for the ImageSurface
450      g_assert(image_surface);
451    cairo_t *context = cairo_create(image_surface);    cairo_t *context = cairo_create(image_surface);
452    
453    // Draw the image on the new Context    // Draw the image on the new Context
# Line 441  cairo_draw_pixbuf(cairo_t *cr, GdkPixbuf Line 458  cairo_draw_pixbuf(cairo_t *cr, GdkPixbuf
458    cairo_set_source_surface(cr, image_surface, x, y);    cairo_set_source_surface(cr, image_surface, x, y);
459    
460    cairo_paint(cr);    cairo_paint(cr);
   printf("cairo_draw_pixbuf out\n");  
461  }  }
462    
463  #ifndef BIG_BALLOONS  #ifndef BIG_BALLOONS
# Line 452  cairo_draw_pixbuf(cairo_t *cr, GdkPixbuf Line 468  cairo_draw_pixbuf(cairo_t *cr, GdkPixbuf
468    
469  static void  static void
470  balloon_cb(osm_gps_map_balloon_event_t *event, gpointer data) {  balloon_cb(osm_gps_map_balloon_event_t *event, gpointer data) {
471    printf("balloon event:\n");    printf("balloon event: ");
472    
473    map_context_t *context = (map_context_t*)data;    map_context_t *context = (map_context_t*)data;
474    cache_t *cache = context->balloon;    cache_t *cache = context->balloon;
# Line 628  balloon_cb(osm_gps_map_balloon_event_t * Line 644  balloon_cb(osm_gps_map_balloon_event_t *
644      printf("removed\n");      printf("removed\n");
645      context->balloon = NULL;      context->balloon = NULL;
646    }    }
   printf("balloon out\n");  
647  }  }
648    
649  static gboolean  static gboolean
# Line 636  on_map_button_release_event(GtkWidget *w Line 651  on_map_button_release_event(GtkWidget *w
651                              GdkEventButton *event, map_context_t *context) {                              GdkEventButton *event, map_context_t *context) {
652    OsmGpsMap *map = OSM_GPS_MAP(context->widget);    OsmGpsMap *map = OSM_GPS_MAP(context->widget);
653    
   printf("on_map_button_release_event in\n");  
   
654    /* in "MAP_CACHE" state only one cache is visible */    /* in "MAP_CACHE" state only one cache is visible */
655    /* and the map is in navigation mode. the balloon is */    /* and the map is in navigation mode. the balloon is */
656    /* pretty useless there */    /* pretty useless there */
# Line 670  on_map_button_release_event(GtkWidget *w Line 683  on_map_button_release_event(GtkWidget *w
683      context->appdata->map.pos.lon = lon;      context->appdata->map.pos.lon = lon;
684    }    }
685    
   printf("on_map_button_release_event out\n");  
686    return FALSE;    return FALSE;
687  }  }
688    
689  static void on_window_destroy(GtkWidget *widget, map_context_t *context) {  static void on_window_destroy(GtkWidget *widget, map_context_t *context) {
690    appdata_t *appdata = context->appdata;    appdata_t *appdata = context->appdata;
691    
   printf("destroy map window\n");  
   
692    /* save map parameters */    /* save map parameters */
693    OsmGpsMap *map = OSM_GPS_MAP(context->widget);    OsmGpsMap *map = OSM_GPS_MAP(context->widget);
694    gint zoom;    gint zoom;
# Line 700  static void on_window_destroy(GtkWidget Line 710  static void on_window_destroy(GtkWidget
710  #endif  #endif
711    
712    gtk_timeout_remove(context->handler_id);    gtk_timeout_remove(context->handler_id);
713      gps_unregister_callback(appdata, context->cb_id);
714    
715      if(context->caches_displayed) {
716        g_free(context->caches_displayed);
717        context->caches_displayed = NULL;
718      }
719    
720    g_free(context);    g_free(context);
721    appdata->map.context = NULL;    appdata->map.context = NULL;
# Line 743  void map_update(appdata_t *appdata) { Line 759  void map_update(appdata_t *appdata) {
759  #endif  #endif
760  }  }
761    
762    /* callback called by the gps layer whenever gps state changes */
763    static void
764    gps_callback(struct gps_state *state, gpointer data) {
765      printf("map: gps callback\n");
766    }
767    
768  void map(appdata_t *appdata) {  void map(appdata_t *appdata) {
769    map_context_t *context = NULL;    map_context_t *context = NULL;
770    
   printf("map 1\n");  
   
771    /* if the map window already exists, just raise it */    /* if the map window already exists, just raise it */
772    if(appdata->map.context) {    if(appdata->map.context) {
773      printf("using existing map!\n");      printf("using existing map!\n");
# Line 756  void map(appdata_t *appdata) { Line 776  void map(appdata_t *appdata) {
776      return;      return;
777    }    }
778    
   printf("map 2\n");  
   
779    context = appdata->map.context = g_new0(map_context_t, 1);    context = appdata->map.context = g_new0(map_context_t, 1);
780    context->appdata = appdata;    context->appdata = appdata;
781    context->map_complete = FALSE;    context->map_complete = FALSE;
# Line 770  void map(appdata_t *appdata) { Line 788  void map(appdata_t *appdata) {
788      rmdir_recursive(old_path);      rmdir_recursive(old_path);
789    }    }
790    
   printf("map 3\n");  
   
791    /* It is recommanded that all applications share these same */    /* It is recommanded that all applications share these same */
792    /* map path, so data is only cached once. The path should be: */    /* map path, so data is only cached once. The path should be: */
793    /* ~/.osm-gps-map on standard PC     (users home) */    /* ~/.osm-gps-map on standard PC     (users home) */
# Line 794  void map(appdata_t *appdata) { Line 810  void map(appdata_t *appdata) {
810    gint source = context->appdata->map.source;    gint source = context->appdata->map.source;
811    if(!source) source = MAP_SOURCE;    if(!source) source = MAP_SOURCE;
812    
   printf("map 4\n");  
   
813    context->widget = g_object_new(OSM_TYPE_GPS_MAP,    context->widget = g_object_new(OSM_TYPE_GPS_MAP,
814                   "map-source",               source,                   "map-source",               source,
815                   "tile-cache",               path,                   "tile-cache",               path,
# Line 809  void map(appdata_t *appdata) { Line 823  void map(appdata_t *appdata) {
823    
824    osm_gps_map_osd_classic_init(OSM_GPS_MAP(context->widget));    osm_gps_map_osd_classic_init(OSM_GPS_MAP(context->widget));
825    
   printf("map 5\n");  
   
826  #ifdef USE_MAEMO  #ifdef USE_MAEMO
827    /* we don't use a stackable window here on fremantle, since */    /* we don't use a stackable window here on fremantle, since */
828    /* this leaves the main window independent from the map and */    /* this leaves the main window independent from the map and */
# Line 825  void map(appdata_t *appdata) { Line 837  void map(appdata_t *appdata) {
837    context->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);    context->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
838  #endif  #endif
839    
   printf("map 6\n");  
   
840  #ifndef USE_MAEMO  #ifndef USE_MAEMO
841    gtk_window_set_default_size(GTK_WINDOW(context->window), 640, 480);    gtk_window_set_default_size(GTK_WINDOW(context->window), 640, 480);
842  #endif  #endif
# Line 845  void map(appdata_t *appdata) { Line 855  void map(appdata_t *appdata) {
855    
856    /* install handler for timed updates of the gps button */    /* install handler for timed updates of the gps button */
857    context->handler_id = gtk_timeout_add(1000, map_gps_update, context);    context->handler_id = gtk_timeout_add(1000, map_gps_update, context);
858      context->cb_id = gps_register_callback(appdata, gps_callback, context);
   printf("map 7\n");  
859    
860  #if MAEMO_VERSION_MAJOR == 5  #if MAEMO_VERSION_MAJOR == 5
861    /* prevent some of the main screen things */    /* prevent some of the main screen things */
# Line 854  void map(appdata_t *appdata) { Line 863  void map(appdata_t *appdata) {
863    appdata->cur_view = NULL;    appdata->cur_view = NULL;
864  #endif  #endif
865    
   printf("map 8\n");  
   
866    g_signal_connect(G_OBJECT(context->window), "destroy",    g_signal_connect(G_OBJECT(context->window), "destroy",
867                     G_CALLBACK(on_window_destroy), context);                     G_CALLBACK(on_window_destroy), context);
868    
   printf("map 9\n");  
869    gtk_container_add(GTK_CONTAINER(context->window), context->widget);    gtk_container_add(GTK_CONTAINER(context->window), context->widget);
   printf("map 10\n");  
870    gtk_widget_show_all(GTK_WIDGET(context->window));    gtk_widget_show_all(GTK_WIDGET(context->window));
   
   printf("map 11\n");  
871  }  }

Legend:
Removed from v.143  
changed lines
  Added in v.152