From 05120a74bef48d1a51c39e06a4403a7f882c65c4 Mon Sep 17 00:00:00 2001 From: Kaj-Michael Lang Date: Fri, 9 May 2008 16:44:40 +0300 Subject: [PATCH] POI: - Use pre-created store - Prefer using proper lat/lon values - Fix category query SQL statement --- src/map-poi.c | 3 ++- src/poi-gui.c | 13 +++++++---- src/poi.c | 61 +++++++++++++++++++-------------------------------- src/poi.h | 18 +++++++-------- 4 files changed, 41 insertions(+), 54 deletions(-) diff --git a/src/map-poi.c b/src/map-poi.c index 55f264d..7bf6a6f 100644 --- a/src/map-poi.c +++ b/src/map-poi.c @@ -64,6 +64,7 @@ pango_layout_set_font_description (layout, fontdesc); pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); pango_layout_set_width(layout, 80*PANGO_SCALE); poi_gc=gdk_gc_new(map_widget->window); +poi_store=poi_list_store_new(); return TRUE; } @@ -127,7 +128,7 @@ if (last_zoom<_zoom || prev_ux!=unitx || prev_uy!=unity) { map_poi_cache_clear(); } -if (poi_get_list_inside(lat1, lon1, lat2, lon2, &poi_store, &pois)==FALSE) +if (poi_get_list_inside(lat1, lon1, lat2, lon2, poi_store, &pois)==FALSE) return FALSE; return TRUE; diff --git a/src/poi-gui.c b/src/poi-gui.c index 0ec3f20..ff2a380 100644 --- a/src/poi-gui.c +++ b/src/poi-gui.c @@ -661,17 +661,23 @@ poi_select(guint unitx, guint unity, guint range, poi_info *poi) GtkListStore *store; guint num_pois; GtkTreeIter iter; -gdouble lat, lon; +gdouble lat, lon, r; + +r=pixel2unit(2*range); +unit2latlon(unitx, unity, lat, lon); -if (poi_get_list_near_unit(unitx, unity, range, &store, &num_pois)==FALSE) +store=poi_list_store_new(); +if (poi_get_list_near(lat, lon, r, store, &num_pois)==FALSE) { + g_object_unref(G_OBJECT(store)); return FALSE; +} switch (num_pois) { case 0: MACRO_BANNER_SHOW_INFO(_window, _("No POIs found.")); g_object_unref(G_OBJECT(store)); return FALSE; - break; +break; case 1: gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter); gtk_tree_model_get(GTK_TREE_MODEL(store), @@ -687,7 +693,6 @@ case 1: break; } -unit2latlon(unitx, unity, lat, lon); return poi_search_dialog(store, poi, lat, lon); } diff --git a/src/poi.c b/src/poi.c index c1c4866..7613ef0 100644 --- a/src/poi.c +++ b/src/poi.c @@ -307,24 +307,15 @@ DB_PREP(db, "delete from poi where poi_id=?", poisql.delete_poi); DB_PREP(db, "delete from poi where cat_id=?", poisql.delete_poi_by_catid); /* select from category */ -DB_PREP(db, "select c.label, c.desc, c.enabled" - " from category c where c.cat_id = ?", - poisql.select_cat); +DB_PREP(db, "select c.label, c.desc, c.enabled from category c where c.cat_id = ?", poisql.select_cat); /* insert into category */ -DB_PREP(db, "insert into category (label, desc, enabled)" - " values (?, ?, ?)", - poisql.insert_cat); +DB_PREP(db, "insert into category (label, desc, enabled) values (?, ?, ?)", poisql.insert_cat); /* update category */ -DB_PREP(db, "update category set label = ?, desc = ?," - " enabled = ? where poi_id = ?", - poisql.update_cat); +DB_PREP(db, "update category set label = ?, desc = ?, enabled = ? where cat_id = ?", poisql.update_cat); /* delete from category */ -DB_PREP(db,"delete from category where cat_id = ?", - poisql.delete_cat); +DB_PREP(db,"delete from category where cat_id = ?", poisql.delete_cat); /* enable category */ -DB_PREP(db, "update category set enabled = ?" - " where cat_id = ?", - poisql.toggle_cat); +DB_PREP(db, "update category set enabled = ? where cat_id = ?", poisql.toggle_cat); /* select all category */ DB_PREP(db, "select c.cat_id, c.label, c.desc, c.enabled, c.icon, c.color," " count(p.poi_id)" @@ -420,7 +411,7 @@ if (p->cat_desc) g_slice_free(poi_info, p); } -static GtkListStore * +GtkListStore * poi_list_store_new(void) { return gtk_list_store_new(ITEM_NUM_COLUMNS, G_TYPE_INT, /* POI ID */ @@ -573,7 +564,7 @@ return TRUE; } gboolean -poi_search(poi_search_type pst, gdouble lat, gdouble lon, gchar *text, guint cat, GtkListStore **store) +poi_search(poi_search_type pst, gdouble lat, gdouble lon, gchar *text, guint cat, GtkListStore *store) { GtkTreeIter iter; sqlite3_stmt *sql=NULL; @@ -586,6 +577,7 @@ if (!poidb) return FALSE; g_return_val_if_fail(poisql.select_poi, FALSE); +g_return_val_if_fail(store, FALSE); g_printf("POI Search: [%s] around %.6f %.6f (%d %d)\n", text, lat, lon, cat, pst); @@ -648,9 +640,6 @@ if (SQLITE_OK != sqlite3_bind_double(sql, 1, lat-range) || return FALSE; } -if (*store==NULL) - *store = poi_list_store_new(); - while (SQLITE_ROW == sqlite3_step(sql)) { gdouble rlat, rlon, dist; @@ -660,8 +649,8 @@ while (SQLITE_ROW == sqlite3_step(sql)) { lon_format(_degformat, rlon, tmp2); dist=calculate_distance(lat, lon, rlat, rlon) * UNITS_CONVERT[_units]; - gtk_list_store_append(*store, &iter); - gtk_list_store_set(*store, &iter, + gtk_list_store_append(store, &iter); + gtk_list_store_set(store, &iter, ITEM_ID, sqlite3_column_int(sql, 2), ITEM_CATID, sqlite3_column_int(sql, 5), ITEM_LAT, rlat, @@ -684,7 +673,7 @@ return TRUE; } gboolean -poi_get_list_inside(gdouble lat1, gdouble lon1, gdouble lat2, gdouble lon2, GtkListStore **store, guint *num_poi) +poi_get_list_inside(gdouble lat1, gdouble lon1, gdouble lat2, gdouble lon2, GtkListStore *store, guint *num_poi) { static gboolean active=FALSE; GtkTreeIter iter; @@ -696,22 +685,20 @@ if (!_db) return FALSE; g_return_val_if_fail(poisql.select_poi, FALSE); +g_return_val_if_fail(store, FALSE); if (active) return FALSE; active=TRUE; if (SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 1, lat1) || - SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 2, lat2) || - SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 3, lon1) || - SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 4, lon2)) { + SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 2, lat2) || + SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 3, lon1) || + SQLITE_OK != sqlite3_bind_double(poisql.select_poi, 4, lon2)) { g_printerr("Failed to bind values for poisql.select_poi\n"); return FALSE; } -if (*store==NULL) - *store = poi_list_store_new(); - while (SQLITE_ROW == sqlite3_step(poisql.select_poi)) { gdouble lat, lon, dist=0; @@ -720,8 +707,8 @@ while (SQLITE_ROW == sqlite3_step(poisql.select_poi)) { lat_format(_degformat, lat, tmp1); lon_format(_degformat, lon, tmp2); - gtk_list_store_append(*store, &iter); - gtk_list_store_set(*store, &iter, + gtk_list_store_append(store, &iter); + gtk_list_store_set(store, &iter, ITEM_ID, sqlite3_column_int(poisql.select_poi, 2), ITEM_CATID, sqlite3_column_int(poisql.select_poi, 5), ITEM_LAT, lat, @@ -743,18 +730,14 @@ return TRUE; } gboolean -poi_get_list_near_unit(guint unitx, guint unity, guint range, GtkListStore **store, guint *num_poi) +poi_get_list_near(gdouble lat, gdouble lon, gfloat range, GtkListStore *store, guint *num_poi) { gdouble lat1, lon1, lat2, lon2; -guint x, y; - -x=unitx-pixel2unit(3*range); -y=unity+pixel2unit(3*range); -unit2latlon(x, y, lat1, lon1); -x=unitx+pixel2unit(3*range); -y=unity-pixel2unit(3*range); -unit2latlon(x, y, lat2, lon2); +lat1=lat-range; +lon1=lon-range; +lat2=lat+range; +lon2=lon+range; return poi_get_list_inside(lat1, lon1, lat2, lon2, store, num_poi); } diff --git a/src/poi.h b/src/poi.h index 93ef00e..34d003c 100644 --- a/src/poi.h +++ b/src/poi.h @@ -125,19 +125,18 @@ gboolean poi_db_prepare(sqlite3 *db); poi_info *poi_new(void); void poi_free(poi_info *p); -gboolean poi_get_list_near_unit(guint unitx, guint unity, guint range, GtkListStore **store, guint *num_poi); -gboolean poi_get_list_inside(gdouble lat1, gdouble lon1, gdouble lat2, gdouble lon2, GtkListStore **store, guint *num_poi); - -poi_info *poi_get_by_id(guint id); - gboolean poi_update(poi_info *p); gboolean poi_add(poi_info *p); gboolean poi_delete(guint id); +poi_info *poi_get_by_id(guint id); -gboolean poi_search(poi_search_type pst, gdouble lat, gdouble lon, gchar *text, guint cat, GtkListStore **store); -poi_info *poi_find_nearest(gdouble lat, gdouble lon); +GtkListStore *poi_list_store_new(void); -gboolean poi_get_list_near_unit(guint unitx, guint unity, guint range, GtkListStore **store, guint *num_poi); +gboolean poi_get_list_near(gdouble lat, gdouble lon, gfloat range, GtkListStore *store, guint *num_poi); +gboolean poi_get_list_inside(gdouble lat1, gdouble lon1, gdouble lat2, gdouble lon2, GtkListStore *store, guint *num_poi); +gboolean poi_search(poi_search_type pst, gdouble lat, gdouble lon, gchar *text, guint cat, GtkListStore *store); + +poi_info *poi_find_nearest(gdouble lat, gdouble lon); poi_category *poi_category_new(void); void poi_category_free(poi_category *c); @@ -146,11 +145,10 @@ gboolean poi_category_get(guint cat_id, poi_category **c); gboolean poi_category_delete(guint id); gboolean poi_category_update(guint cat_id, poi_category *c); gboolean poi_category_toggle(guint cat_id, gboolean cat_enabled); +GtkListStore *poi_category_generate_store(void); GdkPixbuf *poi_get_icon(const gchar *icon, gboolean big); const gchar *poi_get_icon_from_type(node_type_t t); void poi_icon_hash_clear(void); -GtkListStore *poi_category_generate_store(void); - #endif -- 2.39.5