Debug + remove goffice from README
[gpx-viewer.git] / src / gpx-viewer.c
blob9cdd56bc8798c01895b433e3a64d3d81466f3eb9
1 /* Gpx Viewer
2 * Copyright (C) 2009-2009 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://blog.sarine.nl/
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <gtk/gtk.h>
21 #include <stdlib.h>
22 #include <champlain/champlain.h>
23 #include <champlain-gtk/champlain-gtk.h>
24 #include <clutter-gtk/gtk-clutter-embed.h>
25 #include <config.h>
26 #include <glib/gi18n.h>
27 #include "speed-plot.h"
29 /* List of gpx files */
30 GList *files = NULL;
32 GtkWidget *champlain_view = NULL;
33 GtkBuilder *builder = NULL;
34 GpxGraph *gpx_graph =NULL;
35 /* Clutter collors */
36 ClutterColor waypoint = { 0xf3, 0x94, 0x07, 0xff };
37 ClutterColor highlight_track_color = { 0xf3, 0x94, 0x07, 0xff };
38 ClutterColor normal_track_color = { 0x00, 0x00, 0xff, 0x66 };
40 typedef struct Route {
41 GpxFile *file;
42 GpxTrack *track;
43 ChamplainPolygon *polygon;
44 gboolean visible;
45 } Route;
46 /* List of routes */
47 GList *routes = NULL;
48 /* The currently active route */
49 Route *active_route = NULL;
51 void on_destroy(void)
53 printf("Quit...\n");
54 gtk_main_quit();
56 gtk_widget_destroy(
57 GTK_WIDGET(gtk_builder_get_object(builder, "gpx_viewer_window")));
58 g_object_unref(builder);
60 /**
61 * Update on track changes
63 static void interface_update_heading(GtkBuilder *builder, GpxTrack *track)
65 time_t temp;
66 gdouble gtemp;
67 GtkWidget *label = NULL;
68 /* Duration */
69 label = (GtkWidget *)gtk_builder_get_object(builder, "duration_label");
70 temp = gpx_track_get_total_time(track);
71 if(temp > 0){
72 int hour = temp/3600;
73 int minutes = ((temp%3600)/60);
74 int seconds = (temp%60);
75 GString *string = g_string_new("");
76 if(hour > 0) {
77 g_string_append_printf(string, "%i %s", hour, (hour == 1)?"hour":"hours");
80 if(minutes > 0) {
81 if(hour > 0) g_string_append(string, ", ");
82 g_string_append_printf(string, "%i %s", minutes, (minutes == 1)?"minute":"minutes");
85 if(seconds > 0) {
86 if(minutes > 0) g_string_append(string, ", ");
87 g_string_append_printf(string, "%i %s", seconds, (seconds == 1)?"second":"seconds");
90 gtk_label_set_text(GTK_LABEL(label), string->str);
91 g_string_free(string, TRUE);
92 }else{
93 gtk_label_set_text(GTK_LABEL(label), "n/a");
95 /* Distance */
96 label = (GtkWidget *)gtk_builder_get_object(builder, "distance_label");
97 gtemp = track->total_distance;
98 if(gtemp > 0)
100 gchar *string = g_strdup_printf("%.2f km", gtemp);
101 gtk_label_set_text(GTK_LABEL(label), string);
102 g_free(string);
103 }else{
104 gtk_label_set_text(GTK_LABEL(label), "n/a");
107 /* Average */
108 label = (GtkWidget *)gtk_builder_get_object(builder, "average_label");
109 gtemp = gpx_track_get_track_average(track);
110 if(gtemp > 0)
112 gchar *string = g_strdup_printf("%.2f km/h", gtemp);
113 gtk_label_set_text(GTK_LABEL(label), string);
114 g_free(string);
115 }else{
116 gtk_label_set_text(GTK_LABEL(label), "n/a");
119 /* Moving Average */
120 label = (GtkWidget *)gtk_builder_get_object(builder, "moving_average_label");
121 gtemp = gpx_track_calculate_moving_average(track, &temp);
122 if(gtemp > 0)
124 gchar *string = g_strdup_printf("%.2f km/h", gtemp);
125 gtk_label_set_text(GTK_LABEL(label), string);
126 g_free(string);
127 }else{
128 gtk_label_set_text(GTK_LABEL(label), "n/a");
131 label = (GtkWidget *)gtk_builder_get_object(builder, "moving_average_time_label");
132 if(gtemp > 0)
134 int hour = temp/3600;
135 int minutes = ((temp%3600)/60);
136 int seconds = (temp%60);
137 GString *string = g_string_new("");
138 if(hour > 0) {
139 g_string_append_printf(string, "%i %s", hour, (hour == 1)?"hour":"hours");
142 if(minutes > 0) {
143 if(hour > 0) g_string_append(string, ", ");
144 g_string_append_printf(string, "%i %s", minutes, (minutes == 1)?"minute":"minutes");
147 if(seconds > 0) {
148 if(minutes > 0) g_string_append(string, ", ");
149 g_string_append_printf(string, "%i %s", seconds, (seconds == 1)?"second":"seconds");
152 gtk_label_set_text(GTK_LABEL(label), string->str);
153 g_string_free(string, TRUE);
154 }else{
155 gtk_label_set_text(GTK_LABEL(label), "n/a");
157 /* Max speed*/
158 label = (GtkWidget *)gtk_builder_get_object(builder, "max_speed_label");
159 gtemp = track->max_speed;
160 if(gtemp > 0)
162 gchar *string = g_strdup_printf("%.2f km/h", gtemp);
163 gtk_label_set_text(GTK_LABEL(label), string);
164 g_free(string);
165 }else{
166 gtk_label_set_text(GTK_LABEL(label), "n/a");
169 /* GPS Points */
170 label = (GtkWidget *)gtk_builder_get_object(builder, "num_points_label");
171 gtemp = (double)g_list_length(track->points);
172 if(gtemp > 0)
174 gchar *string = g_strdup_printf("%.0f points", gtemp);
175 gtk_label_set_text(GTK_LABEL(label), string);
176 g_free(string);
177 }else{
178 gtk_label_set_text(GTK_LABEL(label), "n/a");
182 static void interface_map_plot_route(ChamplainView *view,struct Route *route)
184 GpxPoint *p = NULL;
185 GList *iter;
186 route->polygon = champlain_polygon_new ();
187 for(iter = g_list_first(route->track->points); iter; iter = iter->next)
189 p = iter->data;
190 champlain_polygon_append_point(route->polygon, p->lat_dec, p->lon_dec);
192 champlain_polygon_set_stroke_width (route->polygon, 5.0);
193 champlain_polygon_set_stroke_color (route->polygon, &normal_track_color);
194 champlain_view_add_polygon (CHAMPLAIN_VIEW (view), route->polygon);
196 static void interface_map_make_waypoints(ChamplainView *view)
198 GList *iter;
199 ChamplainLayer *layer = champlain_layer_new();
200 for(iter = g_list_first(files);iter != NULL; iter = g_list_next(iter))
202 GpxFile *file = iter->data;
203 GList *it = g_list_first(file->waypoints);
204 while(it)
206 GpxPoint *p = it->data;
207 ClutterActor *marker = champlain_marker_new_with_text(p->name, "Seric 12", NULL, NULL);
208 champlain_base_marker_set_position(CHAMPLAIN_BASE_MARKER(marker), p->lat_dec, p->lon_dec);
209 champlain_marker_set_color (CHAMPLAIN_MARKER (marker), &waypoint);
210 clutter_container_add(CLUTTER_CONTAINER(layer), CLUTTER_ACTOR(marker),NULL);
211 clutter_actor_show(CLUTTER_ACTOR(layer));
212 it = g_list_next(it);
215 champlain_view_add_layer(view, layer);
217 /* UI functions */
218 void route_set_visible(GtkCheckButton *button, gpointer user_data)
220 gboolean active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
221 if(active_route)
223 if(active_route->visible != active)
225 active_route->visible = active;
226 if(active) {
227 champlain_polygon_show(active_route->polygon);
228 }else{
229 champlain_polygon_hide(active_route->polygon);
234 void routes_combo_changed_cb(GtkComboBox *box, gpointer user_data)
236 GtkTreeModel *model = gtk_combo_box_get_model(box);
237 GtkTreeIter iter;
238 if(gtk_combo_box_get_active_iter(box, &iter))
240 Route *route = NULL;
241 gtk_tree_model_get(model, &iter, 1, &route, -1);
242 if(active_route)
244 champlain_polygon_set_stroke_color (active_route->polygon, &normal_track_color);
245 if(active_route->visible)
246 champlain_polygon_show(active_route->polygon);
249 if(route)
251 ChamplainView *view = gtk_champlain_embed_get_view (GTK_CHAMPLAIN_EMBED (champlain_view));
252 champlain_polygon_set_stroke_color (route->polygon, &highlight_track_color);
254 if(route->visible)
255 champlain_polygon_show(route->polygon);
257 interface_update_heading(builder, route->track);
259 if(route->track->top && route->track->bottom)
261 champlain_view_ensure_visible(view,
262 route->track->top->lat_dec, route->track->top->lon_dec,
263 route->track->bottom->lat_dec,route->track->bottom->lon_dec,
264 FALSE);
266 gpx_graph_set_track(gpx_graph, route->track);
268 active_route = route;
270 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "route_visible_check_button")),
271 active_route->visible);
275 static void map_zoom_changed (ChamplainView *view,
276 GParamSpec *gobject,
277 GtkSpinButton *spinbutton)
279 gint zoom;
280 g_object_get(G_OBJECT(view), "zoom-level", &zoom, NULL);
281 gtk_spin_button_set_value(spinbutton, zoom);
282 printf("zoom changed: %i\n", zoom);
284 void map_zoom_level_change_value_cb(GtkSpinButton *spin, gpointer user_data)
286 ChamplainView *view = gtk_champlain_embed_get_view (GTK_CHAMPLAIN_EMBED (champlain_view));
287 int current = champlain_view_get_zoom_level(view);
288 int new = gtk_spin_button_get_value_as_int(spin);
289 printf("changed: %i %i\n", current, new);
290 if(current != new)
292 champlain_view_set_zoom_level(view, new);
296 /* Create the interface */
297 static void create_interface(void)
299 time_t temp;
300 GError *error = NULL;
301 const gchar * const *dirs = g_get_system_data_dirs();
302 int i=0;
303 GList *fiter;
304 builder = gtk_builder_new();
305 gchar *path = g_build_filename(DATA_DIR, "gpx-viewer.ui", NULL);
306 if(!gtk_builder_add_from_file(builder,path, NULL)){
307 g_error("Failed to create ui: %s\n", error->message);
309 printf("path: %s\n", path);
310 g_free(path);
312 /* Create map view */
313 champlain_view = gtk_champlain_embed_new();
314 gtk_widget_set_size_request (champlain_view, 640, 280);
315 gtk_paned_pack1(GTK_PANED(gtk_builder_get_object(builder, "main_view_pane")), champlain_view, TRUE, TRUE);
316 /* graph */
317 gpx_graph = gpx_graph_new();
318 gtk_paned_pack2(GTK_PANED(gtk_builder_get_object(builder, "main_view_pane")), GTK_WIDGET(gpx_graph),FALSE, TRUE);
319 gtk_paned_set_position(GTK_PANED(gtk_builder_get_object(builder, "main_view_pane")),200);
321 /* show the interface */
322 gtk_widget_show_all(
323 GTK_WIDGET(gtk_builder_get_object(builder, "gpx_viewer_window")));
325 ChamplainView *view = gtk_champlain_embed_get_view (GTK_CHAMPLAIN_EMBED (champlain_view));
326 g_object_set (G_OBJECT (view), "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC,
327 "zoom-level", 5, NULL);
329 interface_map_make_waypoints(view);
331 double lon1= 1000, lon2=-1000,lat1=1000, lat2=-1000;
332 for(fiter = g_list_first(files); fiter; fiter = g_list_next(fiter))
334 GpxFile *file = fiter->data;
335 GpxTrack *track = file->tracks->data;
336 GList *iter = g_list_first(file->tracks);
337 /* Plot all tracks, and get total bounding box */
338 GtkTreeIter liter;
339 GtkTreeModel *model = (GtkTreeModel *)gtk_builder_get_object(builder, "routes_store");
340 while(iter)
342 struct Route *route= g_new0(Route, 1);
343 route->file = file;
344 route->track = iter->data;
345 route->visible = TRUE;
347 /* draw the track */
348 interface_map_plot_route(view, route);
349 if(track->top && track->top->lat_dec < lat1) lat1 = track->top->lat_dec;
350 if(track->top && track->top->lon_dec < lon1) lon1 = track->top->lon_dec;
352 if(track->bottom && track->bottom->lat_dec > lat2) lat2 = track->bottom->lat_dec;
353 if(track->bottom && track->bottom->lon_dec > lon2) lon2 = track->bottom->lon_dec;
355 gtk_list_store_append(GTK_LIST_STORE(model), &liter);
356 gtk_list_store_set(GTK_LIST_STORE(model), &liter, 0, (route->track->name)?route->track->name:"n/a", 1, route, -1);
358 routes = g_list_append(routes, route);
360 iter = g_list_next(iter);
363 /* Set up the zoom widget */
365 GtkWidget *sp = GTK_WIDGET(gtk_builder_get_object(builder, "map_zoom_level"));
366 int current;
367 champlain_view_set_min_zoom_level(view,1);
368 champlain_view_set_max_zoom_level(view,18);
369 current = champlain_view_get_zoom_level(view);
370 gtk_spin_button_set_value(GTK_SPIN_BUTTON(sp), (double)current);
372 g_signal_connect (view, "notify::zoom-level", G_CALLBACK (map_zoom_changed),
373 sp);
376 gtk_builder_connect_signals(builder,NULL);
377 /* Try to center the track on map correctly */
378 if(lon1 < 1000.0 && lon2 < 1000.0)
380 champlain_view_set_zoom_level(view,8);
381 champlain_view_ensure_visible(view,
382 lat1, lon1,
383 lat2,lon2,
384 FALSE);
386 gtk_combo_box_set_active(GTK_COMBO_BOX(gtk_builder_get_object(builder, "routes_combo")), 0);
389 int main (int argc, char **argv)
392 int i =0;
393 GOptionContext *context = NULL;
394 GError *error = NULL;
395 context = g_option_context_new (_("GPX Viewer"));
398 g_option_context_add_group (context, gtk_get_option_group (TRUE));
399 g_option_context_parse (context, &argc, &argv, &error);
400 g_option_context_free(context);
402 if(error) {
403 g_log(NULL, G_LOG_LEVEL_ERROR, "Failed to parse commandline options: %s", error->message);
404 g_error_free(error);
408 g_thread_init (NULL);
409 gtk_clutter_init (&argc, &argv);
410 /* If no file(s) given, ask for it */
411 if(argc < 2)
413 GtkWidget *dialog;
414 GtkBuilder *fbuilder = gtk_builder_new();
415 /* Show dialog */
416 gchar *path = g_build_filename(DATA_DIR, "gpx-viewer-file-chooser.ui", NULL);
417 if(!gtk_builder_add_from_file(fbuilder,path, NULL)){
418 g_error("Failed to load gpx-viewer.ui");
420 g_free(path);
421 /* update filter */
423 GtkFileFilter *filter = (GtkFileFilter *)gtk_builder_get_object(fbuilder, "gpx_viewer_file_chooser_filter");
424 gtk_file_filter_add_pattern(filter, "*.gpx");
427 dialog = GTK_WIDGET(gtk_builder_get_object(fbuilder, "gpx_viewer_file_chooser"));
428 switch(gtk_dialog_run(GTK_DIALOG(dialog)))
430 case 1:
432 GSList *iter, *choosen_files = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
433 for(iter =choosen_files; iter; iter = g_slist_next(iter))
435 /* Try to open the gpx file */
436 GpxFile *file = gpx_file_new((gchar *)iter->data);
437 files = g_list_append(files, file);
439 g_slist_foreach(choosen_files, (GFunc)g_free, NULL);
440 g_slist_free(choosen_files);
442 break;
444 gtk_widget_destroy(dialog);
445 g_object_unref(fbuilder);
446 if(files == NULL) return EXIT_SUCCESS;
449 for(i =1; i < argc; i++)
451 /* Try to open the gpx file */
452 GpxFile *file = gpx_file_new(argv[i]);
453 files = g_list_append(files, file);
456 create_interface();
459 gtk_main();
460 /* Cleanup office */
461 /* Destroy the files*/
462 g_list_foreach(files, (GFunc)g_object_unref, NULL);
463 g_list_free(files);
465 return EXIT_SUCCESS;