From ef6cec3c90cb8380346c2ef7ebe27da67420ee0a Mon Sep 17 00:00:00 2001 From: Kaj-Michael Lang Date: Mon, 12 May 2008 16:18:51 +0300 Subject: [PATCH] Path: Start to GObjectify --- src/path.c | 150 ++++++++++++++++++++++++++++++++++++++++++++--------- src/path.h | 24 ++++++++- 2 files changed, 148 insertions(+), 26 deletions(-) diff --git a/src/path.c b/src/path.c index a840603..e8420cb 100644 --- a/src/path.c +++ b/src/path.c @@ -66,36 +66,109 @@ static struct sql_select_stmt sql; /* Path point add sensitivity */ static gint sensitivity=3; +enum { + NEW_POINT, /* A new point was appended to track track */ + NEW_BREAK, /* A break was appended to the track */ + NEW_WAYPOINT, /* A new waypoint/marker has been added */ + NEAR_WAYPOINT, /* We are near the next route waypoint */ + REACHED_WAYPOINT, /* We have reached the next route waypoint */ + REACHED_DESTINATION, /* We have reached the last route waypoint */ + LAST_SIGNAL +}; +static guint32 signals[LAST_SIGNAL] = {0}; + +G_DEFINE_TYPE(Path, path, G_TYPE_OBJECT); + +/* #define GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), PATH_TYPE, PathPrivate)) */ + +static void +path_dispose(GObject *object) +{ +g_debug("path_dispose"); + +G_OBJECT_CLASS(path_parent_class)->dispose(object); +} + +static void +path_finalize(GObject *object) +{ +Path *path=PATH(object); + +g_debug("path_finalize"); +MACRO_PATH_FREE(*path); + +if (path->name) + g_free(path->name); +if (path->desc) + g_free(path->desc); +if (path->author) + g_free(path->author); +if (path->keywords) + g_free(path->keywords); +if (path->copyright) + g_free(path->copyright); +if (path->src) + g_free(path->src); +} + +static void +path_class_init(PathClass *klass) +{ +GObjectClass *object_class=G_OBJECT_CLASS(klass); + +g_debug("path_class_init"); + +object_class->dispose=path_dispose; +object_class->finalize=path_finalize; +/* g_type_class_add_private (klass, sizeof(PathPrivate)); */ + +signals[NEW_POINT]=g_signal_new("new-point", G_OBJECT_CLASS_TYPE(object_class), + G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET(PathClass, new_point), + NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL); +signals[NEW_BREAK]=g_signal_new("new-break", G_OBJECT_CLASS_TYPE(object_class), + G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET(PathClass, new_break), + NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL); +signals[NEW_WAYPOINT]=g_signal_new("new-waypoint", G_OBJECT_CLASS_TYPE(object_class), + G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET(PathClass, new_waypoint), + NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL); +} + +static void +path_init(Path *path) +{ +g_debug("path_init"); +MACRO_PATH_INIT(*path); +} + Path * path_new(PathType type, guint id) { Path *p; -p=g_slice_new0(Path); -MACRO_PATH_INIT(*p); +p=g_object_new(PATH_TYPE, NULL); p->type=type; p->id=id; return p; } void -path_free(Path *p) +path_free(Path *path) { -g_return_if_fail(p); -MACRO_PATH_FREE(*p); -g_slice_free(Path, p); +g_return_if_fail(path); +g_debug("path_free"); +g_object_unref(path); } void -path_clear(Path *p) +path_clear(Path *path) { -g_return_if_fail(p); -MACRO_PATH_FREE(*p); -p->length=p->avgspeed=0.0; -p->points=0; +g_return_if_fail(path); +MACRO_PATH_FREE(*path); +path->length=path->avgspeed=0.0; +path->points=0; } -gboolean +gboolean path_resize(Path *path, guint size) { g_return_val_if_fail(path, FALSE); @@ -164,6 +237,8 @@ if (abs((gint)gps->unitx-path->tail->unitx) > sensitivity || abs((gint)gps->unit path->avgspeed=(path->points>0) ? path->tspeed/path->points : 0.0; path->points++; g_debug("TRACK: %f %f (%d)", path->length, path->avgspeed, path->points); + + g_signal_emit(G_OBJECT(path), signals[NEW_POINT], 0, NULL); } return TRUE; @@ -175,22 +250,19 @@ path_insert_break(Path *path) g_return_val_if_fail(path, FALSE); g_return_val_if_fail(path->tail, FALSE); -if (path->tail->unity) { +if (path->tail->unity && path->tail->unitx) { 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. */ + /* To mark a "break" in a track, we'll add a (0, 0) point and then + another instance of the most recent track point. */ + MACRO_PATH_INCREMENT_TAIL(*path); *path->tail=_point_null; MACRO_PATH_INCREMENT_TAIL(*path); *path->tail=path->tail[-2]; -#if 0 - /* Instead of calling map_render_paths(), we'll just draw the waypoint ourselves. */ - x1 = unit2bufx(path->tail->unitx); - y1 = unit2bufy(path->tail->unity); - map_render_waypoint(x1, y1, _gc[COLORABLE_TRACK_BREAK]); -#endif + g_signal_emit(G_OBJECT(path), signals[NEW_BREAK], 0, NULL); + return TRUE; } return FALSE; @@ -327,7 +399,37 @@ if (text) { path->wpcnt++; path->wtail->desc=g_strdup_printf("WP: %u", path->wpcnt); } +g_signal_emit(G_OBJECT(path), signals[NEW_WAYPOINT], 0, NULL); +} + +#if 0 +static void +path_store_append_waypoint(GtkListStore *, WayPoint *w) +{ +gchar tmp1[16], tmp2[16]; +gdouble lat2, lon2; + +unit2latlon(wcurr->point->unitx, wcurr->point->unity, lat2, lon2); +lat_format(_degformat, lat2, tmp1); +lon_format(_degformat, lon2, tmp2); + +g_snprintf(buffer1, sizeof(buffer1), "%s,%s", tmp1, tmp2); +sum += calculate_distance(lat1, lon1, lat2, lon2); +g_snprintf(buffer2, sizeof(buffer2), "%.02f %s", sum * UNITS_CONVERT[_units], UNITS_TEXT[_units]); + +gtk_list_store_append(store, &iter); +gtk_list_store_set(store, &iter, + ROUTE_LATLON, buffer1, + ROUTE_DISTANCE, buffer2, + ROUTE_WAYPOINT, wcurr->desc, + ROUTE_LAT, lat2, + ROUTE_LON, lon2, + -1); + +lat1=lat2; +lon1=lon2; } +#endif /** * Generate a GtkListStore with information about path waypoints (location, distance) @@ -344,12 +446,12 @@ gchar tmp1[16], tmp2[16]; gdouble lat1, lon1, lat2, lon2; gdouble sum=0.0; -g_return_val_if_fail(path, NULL); +g_return_val_if_fail(path!=NULL, NULL); wcurr=path->whead; -g_return_val_if_fail(wcurr, NULL); -g_return_val_if_fail(wcurr->point, NULL); +g_return_val_if_fail(wcurr!=NULL, NULL); +g_return_val_if_fail(wcurr->point!=NULL, NULL); if (!path_has_waypoints(path)) return NULL; diff --git a/src/path.h b/src/path.h index 78cf283..de491d5 100644 --- a/src/path.h +++ b/src/path.h @@ -25,6 +25,15 @@ #include #include +G_BEGIN_DECLS + +#define PATH_TYPE (path_get_type ()) +#define PATH(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PATH_TYPE, Path)) +#define PATH_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PATH_TYPE, PathClass)) +#define IS_PATH(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PATH_TYPE)) +#define IS_PATH_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PATH_TYPE)) +#define PATH_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PATH_TYPE, PathClass)) + typedef enum { PATH_TYPE_TRACK=0, PATH_TYPE_ROUTE, @@ -54,6 +63,8 @@ struct _WayPoint { /** A Path is a set of PathPoints and WayPoints. */ typedef struct _Path Path; struct _Path { + GObject parent; + PathType type; guint id; @@ -97,10 +108,17 @@ struct _Path { time_t time; }; -/* Null point */ -Point _point_null; +typedef struct _PathClass PathClass; +struct _PathClass { + GObjectClass parent; + void (*new_point) (Path *path); + void (*new_break) (Path *path); + void (*new_waypoint) (Path *path); +}; +/* Null point */ +Point _point_null; Path *path_new(PathType type, guint id); void path_free(Path *p); @@ -149,4 +167,6 @@ GtkListStore *path_get_waypoints_store(Path *path); path_wresize(&(route), (route).wcap - (route).whead + ARRAY_CHUNK_SIZE); \ } +G_END_DECLS + #endif -- 2.39.5