]> err.no Git - mapper/blobdiff - src/track.c
Remove old map sources
[mapper] / src / track.c
index 2f59da230d0cc530d2de79b51f5f5ea6f100d575..49d5abc33ecbaceb27caebbbb36121b2391cece8 100644 (file)
@@ -1,67 +1,48 @@
 #include <config.h>
 
+#include <libgnomevfs/gnome-vfs.h>
+#include <sqlite3.h>
+
 #include "utils.h"
 #include "gps.h"
-#include "map.h"
 #include "route.h"
 #include "settings.h"
 #include "mapper-types.h"
 #include "ui-common.h"
+#include "dialogs.h"
 #include "file.h"
-
 #include "track.h"
 #include "latlon.h"
 #include "path.h"
-#include "gpx.h"
-#include "speak.h"
-
-struct sql_select_stmt {
-       sqlite3_stmt *select_track;
-       sqlite3_stmt *select_track_nodes;
-       sqlite3_stmt *insert_track;
-       sqlite3_stmt *insert_track_node;
-       sqlite3_stmt *delete_track_nodes;
-       sqlite3_stmt *delete_track;
-};
-static struct sql_select_stmt sql;
-
-void
-track_init(void)
-{
-memset(&_track, 0, sizeof(_track));
-MACRO_PATH_INIT(_track);
-}
 
-void
-track_deinit(void)
-{
-MACRO_PATH_FREE(_track);
-}
+/* Add sensitivity */
+static gint sensitivity=3;
 
 void 
-track_clear(void)
+track_clear(Path *track)
 {
 GtkWidget *confirm;
 
 confirm = hildon_note_new_confirmation(GTK_WINDOW(_window), _("Clear the track?"));
 
 if (GTK_RESPONSE_OK == gtk_dialog_run(GTK_DIALOG(confirm))) {
-       _track.tail = _track.head;
+       path_clear(track);
        map_force_redraw();
 }
 gtk_widget_destroy(confirm);
 }
 
-gfloat 
-track_calculate_distance_from(Point * point)
+gdouble
+track_calculate_distance_from(Point *point)
 {
-gfloat lat1, lon1, lat2, lon2;
-gfloat sum = 0.0;
 Point *curr;
-unit2latlon(_pos.unitx, _pos.unity, lat1, lon1);
+gdouble lat1, lon1, lat2, lon2;
+gdouble sum = 0.0;
+
+unit2latlon(_gps->data.unitx, _gps->data.unity, lat1, lon1);
 
-/* Skip _track.tail because that should be _pos. */
-for (curr = _track.tail; curr > point; --curr) {
+/* Skip _track->tail because that should be _gps. */
+for (curr = _track->tail; curr > point; --curr) {
        if (curr->unity) {
                unit2latlon(curr->unitx, curr->unity, lat2, lon2);
                sum += calculate_distance(lat1, lon1, lat2, lon2);
@@ -73,15 +54,13 @@ return sum;
 }
 
 void 
-track_show_distance_from(Point * point)
+track_show_distance_from(Point *point)
 {
 gchar buffer[80];
-gfloat sum;
+gdouble sum;
 
 sum = track_calculate_distance_from(point);
-
-g_snprintf(buffer, sizeof(buffer), "%s: %.02f %s", _("Distance"),
-        sum * UNITS_CONVERT[_units], UNITS_TEXT[_units]);
+g_snprintf(buffer, sizeof(buffer), "%s: %.02f %s", _("Distance"), sum * UNITS_CONVERT[_units], UNITS_TEXT[_units]);
 MACRO_BANNER_SHOW_INFO(_window, buffer);
 }
 
@@ -89,10 +68,10 @@ void
 track_show_distance_from_last()
 {
 /* Find last zero point. */
-if (_track.head != _track.tail) {
+if (_track->head != _track->tail) {
        Point *point;
        /* Find last zero point. */
-       for (point = _track.tail; point->unity; point--) {
+       for (point = _track->tail; point->unity; point--) {
        }
        track_show_distance_from(point);
 } else {
@@ -103,34 +82,35 @@ if (_track.head != _track.tail) {
 void 
 track_show_distance_from_first()
 {
-if (_track.head != _track.tail) {
-       track_show_distance_from(_track.head);
+if (_track->head != _track->tail) {
+       track_show_distance_from(_track->head);
 } else {
        MACRO_BANNER_SHOW_INFO(_window, _("The current track is empty."));
 }
 }
 
 /**
- * Add a point to the _track list.  This function is slightly overloaded,
- * since it is what houses the check for "have we moved
- * significantly": it also initiates the re-calculation of the _near_point
- * data, as well as calling osso_display_state_on() when we have the focus.
+ * Add a point to the _track list.
  *
- * If a non-zero time is given, then the current position (as taken from the
- * _pos variable) is appended to _track with the given time.  If time is zero,
- * then _point_null is appended to _track with time zero (this produces a "break"
- * in the track).
+ * If a non-null gps is given, then the current position is appended 
+ * to _track with the given time.  If gps is null, then _point_null is 
+ * appended to _track with time zero (this produces a "break" in the track).
  */
-void 
-track_add(time_t time, gboolean newly_fixed)
+gboolean
+track_add(Path *track, GpsData *gps)
 {
-gboolean show_directions = TRUE;
-gint announce_thres_unsquared;
+if (!gps) {
+       MACRO_PATH_INCREMENT_TAIL(*track);
+       *track->tail=_point_null;
+       return FALSE;
+}
 
-if (abs((gint)_pos.unitx-_track.tail->unitx) > _draw_width || abs((gint)_pos.unity-_track.tail->unity) > _draw_width) {
-       /* If time != 0, update the nearest-waypoint data. */
-       if (time && _route.head != _route.tail && 
-               (newly_fixed ? (route_find_nearest_point(), TRUE) : route_update_nears(TRUE))) {
+g_assert(track);
+if (abs((gint)gps->unitx-track->tail->unitx) > sensitivity || abs((gint)gps->unity-track->tail->unity) > sensitivity) {
+
+       /* If gps is available, update the nearest-waypoint data. */
+       /* XXX: MOVE THIS */
+       if (gps && _route->head != _route->tail && (gps->newly_fixed ? (route_find_nearest_point(_route), TRUE) : route_update_nears(_route, TRUE))) {
                /* Nearest waypoint has changed - re-render paths. */
                map_render_paths();
                MACRO_QUEUE_DRAW_AREA();
@@ -141,16 +121,16 @@ if (abs((gint)_pos.unitx-_track.tail->unitx) > _draw_width || abs((gint)_pos.uni
 
                /* Instead of calling map_render_paths(), we'll draw the new line
                 * ourselves and call gtk_widget_queue_draw_area(). */
-               map_render_segment(_gc[COLORABLE_TRACK],
-                                       _gc[COLORABLE_TRACK_BREAK],
-                                       _track.tail->unitx, _track.tail->unity, 
-                                       _pos.unitx, _pos.unity);
-
-               if (time && _track.tail->unity) {
-                       tx1 = unit2x(_track.tail->unitx);
-                       ty1 = unit2y(_track.tail->unity);
-                       tx2 = unit2x(_pos.unitx);
-                       ty2 = unit2y(_pos.unity);
+               map_render_segment(_gc[COLORABLE_TRACK], _gc[COLORABLE_TRACK_BREAK], 
+                       track->tail->unitx, track->tail->unity, gps->unitx, gps->unity);
+
+               if (track->tail->unity && track->tail->unitx) {
+                       tx1 = unit2x(track->tail->unitx);
+                       ty1 = unit2y(track->tail->unity);
+                       tx2 = unit2x(gps->unitx);
+                       ty2 = unit2y(gps->unity);
+
+                       /* XXX: This should not be here... */
                        gtk_widget_queue_draw_area(_map_widget,
                                           MIN(tx1, tx2) - _draw_width, 
                                           MIN(ty1, ty2) - _draw_width,
@@ -159,73 +139,40 @@ if (abs((gint)_pos.unitx-_track.tail->unitx) > _draw_width || abs((gint)_pos.uni
                }
        }
 
-       MACRO_PATH_INCREMENT_TAIL(_track);
-
-       if (time)
-               *_track.tail = _pos;
-       else
-               *_track.tail = _point_null;
-
-       if (_autoroute_data.enabled && !_autoroute_data.in_progress && _near_point_dist_squared > 400) {
-               MACRO_BANNER_SHOW_INFO(_window, _("Recalculating directions..."));
-               _autoroute_data.in_progress = TRUE;
-               show_directions = FALSE;
-               g_idle_add((GSourceFunc) auto_route_dl_idle, NULL);
+       if (track->tail->unity && track->tail->unitx) {
+               gdouble lat, lon;
+
+               unit2latlon(track->tail->unitx, track->tail->unity, lat, lon);
+               track->length += calculate_distance(lat, lon, gps->lat, gps->lon);
+               track->tspeed+=gps->speed;
+               if (track->points>0)
+                       track->avgspeed=track->tspeed/track->points;
+               else
+                       track->avgspeed=0.0;
+               g_debug("TRACK: %f %f (%d)", track->length, track->avgspeed, track->points);
        }
 
-       /* Keep the display on. */
-       KEEP_DISPLAY_ON();
+       MACRO_PATH_INCREMENT_TAIL(*track);
+       track->tail->unitx=gps->unitx;
+       track->tail->unity=gps->unity;
+       track->tail->time=gps->time;
+       track->tail->altitude=gps->altitude;
+       track->maxspeed=gps->maxspeed;
+       track->points++;
 }
 
-announce_thres_unsquared=(20+(guint)_gps.speed)*_announce_notice_ratio*3;
-
-/* Check if we should announce upcoming waypoints. */
-if (show_directions && time && _next_way_dist_squared < (announce_thres_unsquared * announce_thres_unsquared)) {
-       if (_enable_voice && g_strcmp0(_next_way->desc, _last_spoken_phrase)) {
-               g_free(_last_spoken_phrase);
-               _last_spoken_phrase=g_strdup(_next_way->desc);
-               speak_text(_last_spoken_phrase);
-       }
-       MACRO_BANNER_SHOW_INFO(_window, _next_way->desc);
-}
-}
-
-void 
-track_insert_break(void)
-{
-if (_track.tail->unity) {
-       guint x1, y1;
-
-/* To mark a "waypoint" in a track, we'll add a (0, 0) point and then
- * another instance of the most recent track point. */
-MACRO_PATH_INCREMENT_TAIL(_track);
-*_track.tail=_point_null;
-MACRO_PATH_INCREMENT_TAIL(_track);
-*_track.tail=_track.tail[-2];
-
-/* Instead of calling map_render_paths(), we'll just add the waypoint ourselves. */
-x1 = unit2bufx(_track.tail->unitx);
-y1 = unit2bufy(_track.tail->unity);
-/* Make sure this circle will be visible. */
-if ((x1 < BUF_WIDTH_PIXELS) && ((unsigned)y1 < BUF_HEIGHT_PIXELS))
-       gdk_draw_arc(_map_pixmap, _gc[COLORABLE_TRACK_BREAK], FALSE,
-               x1 - _draw_width, y1 - _draw_width,
-               2 * _draw_width, 2 * _draw_width, 0,
-               360 * 64);
-} else {
-       MACRO_BANNER_SHOW_INFO(_window, _("Break already inserted."));
-}
+return TRUE;
 }
 
 gboolean
-track_open(void)
+track_open(Path *track)
 {
 gchar *buffer;
 gint size;
 gboolean r = FALSE;
 
 if (file_open_get_contents(&_track_file_uri, &buffer, &size)) {
-       if (parse_gpx(&_track, buffer, size, -1)) {
+       if (gpx_parse(track, buffer, size, GPX_PATH_NEW)) {
                map_force_redraw();
                MACRO_BANNER_SHOW_INFO(_window, _("Track Opened"));
                r = TRUE;
@@ -238,16 +185,16 @@ return r;
 }
 
 gboolean
-track_save(void)
+track_save(Path *track)
 {
 GnomeVFSHandle *handle;
 gboolean r = FALSE;
 
 if (file_save(&_track_file_uri, &_track_file_uri, &handle)) {
-       if (write_gpx(&_track, handle)) {
+       if (gpx_write(track, handle)) {
                MACRO_BANNER_SHOW_INFO(_window, _("Track Saved"));
                r = TRUE;
-               track_clear();
+               track_clear(track);
        } else {
                popup_error(_window, _("Error writing GPX file."));
        }
@@ -256,46 +203,34 @@ if (file_save(&_track_file_uri, &_track_file_uri, &handle)) {
 return r;
 }
 
-/**
- * Add a text description at current point
- * 
- */
-void 
-track_insert_mark_text(gchar *text)
-{
-MACRO_PATH_INCREMENT_WTAIL(_track);
-_track.wtail->point = _track.tail;
-_track.wtail->desc = text;
-}
-
 /**
  * Ask for a text description for the current point
  *
  */
 gboolean
-track_insert_mark(void)
+track_insert_mark(Path *track)
 {
-gfloat lat, lon;
+gdouble lat, lon;
 gchar tmp1[16], tmp2[16], *p_latlon;
 GtkWidget *dialog;
 GtkWidget *table;
 GtkWidget *label;
 GtkWidget *txt_scroll;
 GtkWidget *txt_desc;
+gboolean ret;
 
 dialog = gtk_dialog_new_with_buttons(_("Insert Mark"),
-                                    GTK_WINDOW(_window),
-                                    GTK_DIALOG_MODAL, GTK_STOCK_OK,
-                                    GTK_RESPONSE_ACCEPT,
-                                    GTK_STOCK_CANCEL,
-                                    GTK_RESPONSE_REJECT, NULL);
+                               GTK_WINDOW(_window), GTK_DIALOG_MODAL, 
+                               GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
+                               GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, 
+                               NULL);
 
 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), table = gtk_table_new(2, 2, FALSE), TRUE, TRUE, 0);
 
 gtk_table_attach(GTK_TABLE(table), label = gtk_label_new(_("Lat, Lon")), 0, 1, 0, 1, GTK_FILL, 0, 2, 4);
 gtk_misc_set_alignment(GTK_MISC(label), 1.f, 0.5f);
 
-unit2latlon(_pos.unitx, _pos.unity, lat, lon);
+unit2latlon(_gps->data.unitx, _gps->data.unity, lat, lon);
 lat_format(_degformat, lat, tmp1);
 lon_format(_degformat, lon, tmp2);
 p_latlon = g_strdup_printf("%s, %s", tmp1, tmp2);
@@ -316,10 +251,11 @@ txt_desc = gtk_text_view_new();
 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(txt_desc), GTK_WRAP_WORD);
 
 gtk_container_add(GTK_CONTAINER(txt_scroll), txt_desc);
-gtk_widget_set_size_request(GTK_WIDGET(txt_scroll), 450, 80);
+gtk_widget_set_size_request(GTK_WIDGET(txt_scroll), 450, 100);
 
 gtk_widget_show_all(dialog);
 
+ret=FALSE;
 while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) {
        GtkTextBuffer *tbuf;
        GtkTextIter ti1, ti2;
@@ -329,16 +265,15 @@ while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) {
        gtk_text_buffer_get_end_iter(tbuf, &ti2);
 
        if (gtk_text_buffer_get_char_count(tbuf)>0) {
-               track_insert_mark_text(gtk_text_buffer_get_text(tbuf, &ti1, &ti2, TRUE));
+               path_insert_mark_text(track, gtk_text_buffer_get_text(tbuf, &ti1, &ti2, TRUE));
        } else {
                popup_error(dialog, _("Please provide a description for the mark."));
                continue;
        }
 
-       map_render_paths();
-       MACRO_QUEUE_DRAW_AREA();
+       ret=TRUE;
        break;
 }
 gtk_widget_destroy(dialog);
-return TRUE;
+return ret;
 }