]> err.no Git - mapper/commitdiff
Move more code from poi-gui.c to poi.c
authorKaj-Michael Lang <milang@angel.tal.org>
Sat, 21 Jul 2007 10:08:57 +0000 (13:08 +0300)
committerKaj-Michael Lang <milang@angel.tal.org>
Sat, 21 Jul 2007 10:08:57 +0000 (13:08 +0300)
Allow searching in poi dialog.
Allow sorting columns in poi dialog.
Fix bug with getting lat/lon values from poi selection.

src/poi-gui.c
src/poi.c
src/poi.h

index 258039806c01e41ddcc9c01d1e116b08ce570a02..31c4ba6496c819c87169c82e86a46d420884bffd 100644 (file)
@@ -33,6 +33,8 @@
 #include "settings.h"
 
 guint _poi_zoom = 6;
+gboolean select_poi(guint unitx, guint unity, PoiInfo * poi);
+
 
 gboolean category_delete(GtkWidget * widget, DeletePOI * dpoi)
 {
@@ -488,8 +490,6 @@ gboolean poi_delete_confirm(GtkWidget * widget, DeletePOI * dpoi)
 
 gboolean select_poi(guint unitx, guint unity, PoiInfo * poi)
 {
-       guint x, y;
-       gfloat lat1, lon1, lat2, lon2;
        GtkWidget *dialog;
        GtkWidget *list;
        GtkWidget *sw;
@@ -498,74 +498,22 @@ gboolean select_poi(guint unitx, guint unity, PoiInfo * poi)
        GtkListStore *store;
        GtkTreeIter iter;
        gboolean selected = FALSE;
-       gchar tmp1[16], tmp2[16];
-       guint num_cats = 0;
+       guint num_cats;
        printf("%s()\n", __PRETTY_FUNCTION__);
 
-       x = unitx - pixel2unit(3 * _draw_width);
-       y = unity + pixel2unit(3 * _draw_width);
-       unit2latlon(x, y, lat1, lon1);
-
-       x = unitx + pixel2unit(3 * _draw_width);
-       y = unity - pixel2unit(3 * _draw_width);
-       unit2latlon(x, y, lat2, lon2);
-
-       store = gtk_list_store_new(POI_NUM_COLUMNS, G_TYPE_INT, /* POI ID */
-                                  G_TYPE_INT,  /* Category ID */
-                                  G_TYPE_FLOAT,        /* Latitude */
-                                  G_TYPE_FLOAT,        /* Longitude */
-                                  G_TYPE_STRING,       /* Lat/Lon */
-                                  G_TYPE_STRING,       /* POI Label */
-                                  G_TYPE_STRING,       /* POI Desc. */
-                                  G_TYPE_STRING);      /* Category Label */
-
-       if (SQLITE_OK != sqlite3_bind_double(_stmt_select_poi, 1, lat1) ||
-           SQLITE_OK != sqlite3_bind_double(_stmt_select_poi, 2, lat2) ||
-           SQLITE_OK != sqlite3_bind_double(_stmt_select_poi, 3, lon1) ||
-           SQLITE_OK != sqlite3_bind_double(_stmt_select_poi, 4, lon2)) {
-               g_printerr("Failed to bind values for _stmt_select_poi");
+       if (poi_get_list(unitx, unity, &store, &num_cats)==FALSE)
                return FALSE;
-       }
 
-       while (SQLITE_ROW == sqlite3_step(_stmt_select_poi)) {
-               gfloat lat, lon;
-               lat = sqlite3_column_double(_stmt_select_poi, 0);
-               lon = sqlite3_column_double(_stmt_select_poi, 1);
-               printf("Found POI: %f, %f, %s, %s, %s\n",
-                      lat,
-                      lon,
-                      sqlite3_column_text(_stmt_select_poi, 3),
-                      sqlite3_column_text(_stmt_select_poi, 4),
-                      sqlite3_column_text(_stmt_select_poi, 6));
-               lat_format(lat, tmp1);
-               lon_format(lon, tmp2);
-               gtk_list_store_append(store, &iter);
-               gtk_list_store_set(store, &iter,
-                                  POI_POIID,
-                                  sqlite3_column_int(_stmt_select_poi, 2),
-                                  POI_CATID,
-                                  sqlite3_column_int(_stmt_select_poi, 5),
-                                  POI_LAT, lat, POI_LON, lon, POI_LATLON,
-                                  g_strdup_printf("%s, %s", tmp1, tmp2),
-                                  POI_LABEL,
-                                  sqlite3_column_text(_stmt_select_poi, 3),
-                                  POI_DESC,
-                                  sqlite3_column_text(_stmt_select_poi, 4),
-                                  POI_CATLAB,
-                                  sqlite3_column_text(_stmt_select_poi, 6),
-                                  -1);
-               num_cats++;
-       }
-       sqlite3_reset(_stmt_select_poi);
+       g_printf("N: %d\n", num_cats);
 
        switch (num_cats) {
        case 0:
-               g_object_unref(G_OBJECT(store));
                MACRO_BANNER_SHOW_INFO(_window, _("No POIs found."));
+               g_object_unref(G_OBJECT(store));
                return FALSE;
-               break;
+       break;
        case 1:
-               /* iter is still set to the most-recently added POI. */
+               gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
                gtk_tree_model_get(GTK_TREE_MODEL(store),
                                   &iter,
                                   POI_POIID, &(poi->poi_id),
@@ -576,7 +524,7 @@ gboolean select_poi(guint unitx, guint unity, PoiInfo * poi)
                                   POI_DESC, &(poi->desc), -1);
                g_object_unref(G_OBJECT(store));
                return TRUE;
-               break;
+       break;
        }
 
        /* There are at least 2 matching POI's - let the user select one. */
@@ -598,54 +546,48 @@ gboolean select_poi(guint unitx, guint unity, PoiInfo * poi)
                           sw, TRUE, TRUE, 0);
 
        list = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
-       g_object_unref(G_OBJECT(store));
        gtk_container_add(GTK_CONTAINER(sw), list);
+       g_object_unref(G_OBJECT(store));
 
        gtk_tree_selection_set_mode(gtk_tree_view_get_selection
                                    (GTK_TREE_VIEW(list)),
                                    GTK_SELECTION_SINGLE);
        gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), TRUE);
+       gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (list), TRUE);
+       gtk_tree_view_set_search_column (GTK_TREE_VIEW (list), POI_LABEL);
 
        renderer = gtk_cell_renderer_text_new();
-       column =
-           gtk_tree_view_column_new_with_attributes(_("Location"), renderer,
+       column = gtk_tree_view_column_new_with_attributes(_("Location"), renderer,
                                                     "text", POI_LATLON, NULL);
        gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
 
        renderer = gtk_cell_renderer_text_new();
-       column =
-           gtk_tree_view_column_new_with_attributes(_("Label"), renderer,
+       column = gtk_tree_view_column_new_with_attributes(_("Label"), renderer,
                                                     "text", POI_LABEL, NULL);
+       gtk_tree_view_column_set_sort_column_id (column, POI_LABEL);
        gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
 
        renderer = gtk_cell_renderer_text_new();
-       column =
-           gtk_tree_view_column_new_with_attributes(_("Category"), renderer,
+       column = gtk_tree_view_column_new_with_attributes(_("Category"), renderer,
                                                     "text", POI_CATLAB, NULL);
+       gtk_tree_view_column_set_sort_column_id (column, POI_CATLAB);
        gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
 
        gtk_widget_show_all(dialog);
 
        while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(dialog))) {
-               if (gtk_tree_selection_get_selected
-                   (gtk_tree_view_get_selection(GTK_TREE_VIEW(list)), NULL,
-                    &iter)) {
-                       gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
-                                          POI_POIID, &(poi->poi_id), -1);
-                       gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
-                                          POI_CATID, &(poi->cat_id), -1);
-                       gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
-                                          POI_LATLON, &(poi->lat), -1);
-                       gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
-                                          POI_LATLON, &(poi->lon), -1);
-                       gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
-                                          POI_LABEL, &(poi->label), -1);
-                       gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
-                                          POI_DESC, &(poi->desc), -1);
+               if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(list)), NULL, &iter)) {
+                       gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, POI_POIID, &(poi->poi_id), -1);
+                       gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, POI_CATID, &(poi->cat_id), -1);
+                       gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, POI_LAT, &(poi->lat), -1);
+                       gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, POI_LON, &(poi->lon), -1);
+                       gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, POI_LABEL, &(poi->label), -1);
+                       gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, POI_DESC, &(poi->desc), -1);
                        selected = TRUE;
                        break;
-               } else
+               } else {
                        popup_error(dialog, _("Select one POI from the list."));
+               }
        }
 
        gtk_widget_destroy(dialog);
@@ -661,8 +603,7 @@ void poi_populate_cat_combo(GtkWidget * cmb_category, guint cat_id)
        gboolean has_active = FALSE;
        printf("%s()\n", __PRETTY_FUNCTION__);
 
-       store =
-           GTK_LIST_STORE(gtk_combo_box_get_model
+       store = GTK_LIST_STORE(gtk_combo_box_get_model
                           (GTK_COMBO_BOX(cmb_category)));
        gtk_list_store_clear(store);
 
index dbe860e8e8f69b93f238ed9ccb00d7f35a8a41c4..a1b01ef85db78b4a7e85c5a37a7ffc8518741950 100644 (file)
--- a/src/poi.c
+++ b/src/poi.c
 #include "db.h"
 #include "osm.h"
 
+struct sql_poi_stmt {
+       sqlite3_stmt *_stmt_select_poi;
+       sqlite3_stmt *_stmt_select_nearest_poi;
+       sqlite3_stmt *_stmt_insert_poi;
+       sqlite3_stmt *_stmt_update_poi;
+       sqlite3_stmt *_stmt_delete_poi;
+       sqlite3_stmt *_stmt_delete_poi_by_catid;
+       sqlite3_stmt *_stmt_nextlabel_poi;
+       sqlite3_stmt *_stmt_select_cat;
+       sqlite3_stmt *_stmt_insert_cat;
+       sqlite3_stmt *_stmt_update_cat;
+       sqlite3_stmt *_stmt_delete_cat;
+       sqlite3_stmt *_stmt_toggle_cat;
+       sqlite3_stmt *_stmt_selall_cat;
+};
+static struct sql_poi_stmt poisql;
+
 sqlite3_stmt *_stmt_select_poi = NULL;
 sqlite3_stmt *_stmt_select_nearest_poi = NULL;
 sqlite3_stmt *_stmt_insert_poi = NULL;
@@ -289,7 +306,7 @@ return results;
 }
 
 gboolean 
-poi_category_delete(DeletePOI * dpoi)
+poi_category_delete(DeletePOI *dpoi)
 {
 if (SQLITE_OK != sqlite3_bind_int(_stmt_delete_poi_by_catid, 1, dpoi->id)
            || SQLITE_DONE != sqlite3_step(_stmt_delete_poi_by_catid)) {
@@ -311,7 +328,7 @@ return TRUE;
 
 
 gboolean 
-poi_delete(DeletePOI * dpoi)
+poi_delete(DeletePOI *dpoi)
 {
 if (SQLITE_OK != sqlite3_bind_int(_stmt_delete_poi, 1, dpoi->id) ||
     SQLITE_DONE != sqlite3_step(_stmt_delete_poi)) {
@@ -324,6 +341,75 @@ if (SQLITE_OK != sqlite3_bind_int(_stmt_delete_poi, 1, dpoi->id) ||
 return TRUE;
 }
 
+gboolean
+poi_get_list(guint unitx, guint unity, GtkListStore **_store, guint *_num_cats)
+{
+guint x, y;
+gfloat lat1, lon1, lat2, lon2;
+GtkListStore *store;
+GtkTreeIter iter;
+gchar tmp1[16], tmp2[16];
+gint num_cats=0;
+
+store = gtk_list_store_new(POI_NUM_COLUMNS, G_TYPE_INT,        /* POI ID */
+                          G_TYPE_INT,  /* Category ID */
+                          G_TYPE_FLOAT,        /* Latitude */
+                          G_TYPE_FLOAT,        /* Longitude */
+                          G_TYPE_STRING,       /* Lat/Lon */
+                          G_TYPE_STRING,       /* POI Label */
+                          G_TYPE_STRING,       /* POI Desc. */
+                          G_TYPE_STRING);      /* Category Label */
+
+x = unitx - pixel2unit(3 * _draw_width);
+y = unity + pixel2unit(3 * _draw_width);
+unit2latlon(x, y, lat1, lon1);
+
+x = unitx + pixel2unit(3 * _draw_width);
+y = unity - pixel2unit(3 * _draw_width);
+unit2latlon(x, y, lat2, lon2); 
+
+if (SQLITE_OK != sqlite3_bind_double(_stmt_select_poi, 1, lat1) ||
+    SQLITE_OK != sqlite3_bind_double(_stmt_select_poi, 2, lat2) ||
+    SQLITE_OK != sqlite3_bind_double(_stmt_select_poi, 3, lon1) ||
+    SQLITE_OK != sqlite3_bind_double(_stmt_select_poi, 4, lon2)) {
+       g_printerr("Failed to bind values for _stmt_select_poi\n");
+       return FALSE;
+}
+
+while (SQLITE_ROW == sqlite3_step(_stmt_select_poi)) {
+       gfloat lat, lon;
+       lat = sqlite3_column_double(_stmt_select_poi, 0);
+       lon = sqlite3_column_double(_stmt_select_poi, 1);
+       g_printf("Found POI(%d): %f, %f, %s, %s, %s\n",
+               num_cats,
+               lat,
+               lon,
+               sqlite3_column_text(_stmt_select_poi, 3),
+               sqlite3_column_text(_stmt_select_poi, 4),
+               sqlite3_column_text(_stmt_select_poi, 6));
+
+       lat_format(lat, tmp1);
+       lon_format(lon, tmp2);
+       gtk_list_store_append(store, &iter);
+       gtk_list_store_set(store, &iter,
+               POI_POIID, sqlite3_column_int(_stmt_select_poi, 2),
+               POI_CATID, sqlite3_column_int(_stmt_select_poi, 5),
+               POI_LAT, lat, POI_LON, lon, POI_LATLON,
+               g_strdup_printf("%s, %s", tmp1, tmp2),
+               POI_LABEL, sqlite3_column_text(_stmt_select_poi, 3),
+               POI_DESC, sqlite3_column_text(_stmt_select_poi, 4),
+               POI_CATLAB, sqlite3_column_text(_stmt_select_poi, 6),
+               -1);
+       num_cats++;
+}
+g_printf("N: %d\n", num_cats);
+sqlite3_reset(_stmt_select_poi);
+sqlite3_clear_bindings(_stmt_select_poi);
+*_store=store;
+*_num_cats=num_cats;
+return TRUE;
+}
+
 gboolean
 poi_update(gint poi_id, gint cat_id, gchar *poi_label, gchar *poi_desc)
 {
index 6f5ef0aab5c5fcbf99904151013966a4312d803a..35e73bef0b337b716e453ebe1974e62713062a37 100644 (file)
--- a/src/poi.h
+++ b/src/poi.h
@@ -126,4 +126,12 @@ sqlite3_stmt *_stmt_delete_cat;
 sqlite3_stmt *_stmt_toggle_cat;
 sqlite3_stmt *_stmt_selall_cat;
 
+gboolean poi_get_list(guint unitx, guint unity, GtkListStore **store, guint *num_cats);
+gboolean poi_update(gint poi_id, gint cat_id, gchar *poi_label, gchar *poi_desc);
+gboolean poi_add(gdouble lat, gdouble lon, gint cat_id, gchar *poi_label, gchar *poi_desc);
+gboolean poi_delete(DeletePOI * dpoi);
+gboolean poi_category_delete(DeletePOI * dpoi);
+gboolean poi_category_update(gint cat_id, gchar * cat_label, gchar * cat_desc, gint cat_enabled);
+
+
 #endif