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.
22 #include <champlain/champlain.h>
23 #include <champlain-gtk/champlain-gtk.h>
24 #include <clutter-gtk/gtk-clutter-embed.h>
26 #include <glib/gi18n.h>
27 #include "speed-plot.h"
29 /* List of gpx files */
32 GtkWidget
*champlain_view
= NULL
;
33 GtkBuilder
*builder
= NULL
;
34 GpxGraph
*gpx_graph
=NULL
;
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
{
43 ChamplainPolygon
*polygon
;
48 /* The currently active route */
49 Route
*active_route
= NULL
;
57 GTK_WIDGET(gtk_builder_get_object(builder
, "gpx_viewer_window")));
58 g_object_unref(builder
);
61 * Update on track changes
63 static void interface_update_heading(GtkBuilder
*builder
, GpxTrack
*track
)
67 GtkWidget
*label
= NULL
;
69 label
= (GtkWidget
*)gtk_builder_get_object(builder
, "duration_label");
70 temp
= gpx_track_get_total_time(track
);
73 int minutes
= ((temp
%3600)/60);
74 int seconds
= (temp
%60);
75 GString
*string
= g_string_new("");
77 g_string_append_printf(string
, "%i %s", hour
, (hour
== 1)?"hour":"hours");
81 if(hour
> 0) g_string_append(string
, ", ");
82 g_string_append_printf(string
, "%i %s", minutes
, (minutes
== 1)?"minute":"minutes");
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
);
93 gtk_label_set_text(GTK_LABEL(label
), "n/a");
96 label
= (GtkWidget
*)gtk_builder_get_object(builder
, "distance_label");
97 gtemp
= track
->total_distance
;
100 gchar
*string
= g_strdup_printf("%.2f km", gtemp
);
101 gtk_label_set_text(GTK_LABEL(label
), string
);
104 gtk_label_set_text(GTK_LABEL(label
), "n/a");
108 label
= (GtkWidget
*)gtk_builder_get_object(builder
, "average_label");
109 gtemp
= gpx_track_get_track_average(track
);
112 gchar
*string
= g_strdup_printf("%.2f km/h", gtemp
);
113 gtk_label_set_text(GTK_LABEL(label
), string
);
116 gtk_label_set_text(GTK_LABEL(label
), "n/a");
120 label
= (GtkWidget
*)gtk_builder_get_object(builder
, "moving_average_label");
121 gtemp
= gpx_track_calculate_moving_average(track
, &temp
);
124 gchar
*string
= g_strdup_printf("%.2f km/h", gtemp
);
125 gtk_label_set_text(GTK_LABEL(label
), string
);
128 gtk_label_set_text(GTK_LABEL(label
), "n/a");
131 label
= (GtkWidget
*)gtk_builder_get_object(builder
, "moving_average_time_label");
134 int hour
= temp
/3600;
135 int minutes
= ((temp
%3600)/60);
136 int seconds
= (temp
%60);
137 GString
*string
= g_string_new("");
139 g_string_append_printf(string
, "%i %s", hour
, (hour
== 1)?"hour":"hours");
143 if(hour
> 0) g_string_append(string
, ", ");
144 g_string_append_printf(string
, "%i %s", minutes
, (minutes
== 1)?"minute":"minutes");
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
);
155 gtk_label_set_text(GTK_LABEL(label
), "n/a");
158 label
= (GtkWidget
*)gtk_builder_get_object(builder
, "max_speed_label");
159 gtemp
= track
->max_speed
;
162 gchar
*string
= g_strdup_printf("%.2f km/h", gtemp
);
163 gtk_label_set_text(GTK_LABEL(label
), string
);
166 gtk_label_set_text(GTK_LABEL(label
), "n/a");
170 label
= (GtkWidget
*)gtk_builder_get_object(builder
, "num_points_label");
171 gtemp
= (double)g_list_length(track
->points
);
174 gchar
*string
= g_strdup_printf("%.0f points", gtemp
);
175 gtk_label_set_text(GTK_LABEL(label
), string
);
178 gtk_label_set_text(GTK_LABEL(label
), "n/a");
182 static void interface_map_plot_route(ChamplainView
*view
,struct Route
*route
)
186 route
->polygon
= champlain_polygon_new ();
187 for(iter
= g_list_first(route
->track
->points
); iter
; iter
= iter
->next
)
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
)
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
);
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
);
218 void route_set_visible(GtkCheckButton
*button
, gpointer user_data
)
220 gboolean active
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button
));
223 if(active_route
->visible
!= active
)
225 active_route
->visible
= active
;
227 champlain_polygon_show(active_route
->polygon
);
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
);
238 if(gtk_combo_box_get_active_iter(box
, &iter
))
241 gtk_tree_model_get(model
, &iter
, 1, &route
, -1);
244 champlain_polygon_set_stroke_color (active_route
->polygon
, &normal_track_color
);
245 if(active_route
->visible
)
246 champlain_polygon_show(active_route
->polygon
);
251 ChamplainView
*view
= gtk_champlain_embed_get_view (GTK_CHAMPLAIN_EMBED (champlain_view
));
252 champlain_polygon_set_stroke_color (route
->polygon
, &highlight_track_color
);
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
,
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
,
277 GtkSpinButton
*spinbutton
)
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);
292 champlain_view_set_zoom_level(view
, new);
296 /* Create the interface */
297 static void create_interface(void)
300 GError
*error
= NULL
;
301 const gchar
* const *dirs
= g_get_system_data_dirs();
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
);
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
);
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 */
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 */
339 GtkTreeModel
*model
= (GtkTreeModel
*)gtk_builder_get_object(builder
, "routes_store");
342 struct Route
*route
= g_new0(Route
, 1);
344 route
->track
= iter
->data
;
345 route
->visible
= TRUE
;
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"));
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
),
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
,
386 gtk_combo_box_set_active(GTK_COMBO_BOX(gtk_builder_get_object(builder
, "routes_combo")), 0);
389 int main (int argc
, char **argv
)
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
);
403 g_log(NULL
, G_LOG_LEVEL_ERROR
, "Failed to parse commandline options: %s", error
->message
);
408 g_thread_init (NULL
);
409 gtk_clutter_init (&argc
, &argv
);
410 /* If no file(s) given, ask for it */
414 GtkBuilder
*fbuilder
= gtk_builder_new();
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");
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
)))
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
);
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
);
461 /* Destroy the files*/
462 g_list_foreach(files
, (GFunc
)g_object_unref
, NULL
);