]> err.no Git - mapper/commitdiff
More work on generic search interface
authorKaj-Michael Lang <milang@onion.tal.org>
Wed, 31 Oct 2007 13:10:42 +0000 (15:10 +0200)
committerKaj-Michael Lang <milang@onion.tal.org>
Wed, 31 Oct 2007 13:10:42 +0000 (15:10 +0200)
src/search.c
src/search.h

index abc9d3de1a2bd66466f2f6b905df8228b33dd111..930c2e031c418d09fff2e8e5a5dab229643b6d58 100644 (file)
 #include "settings.h"
 #include "poi-gui.h"
 #include "osm.h"
-
 #include "help.h"
 
-typedef enum {
-       SEARCH_TYPE_POI=0,
-       SEARCH_TYPE_WAY=1,
-       SEARCH_TYPE_PLACE=2,
-} search_type;
-
-typedef enum {
-       ITEM_ID,
-       ITEM_CATID,
-       ITEM_LAT,
-       ITEM_LON,
-       ITEM_LATLON,
-       ITEM_LABEL,
-       ITEM_DESC,
-       ITEM_CATLAB,
-       ITEM_DIST,
-       ITEM_NUM_COLUMNS
-} item_list_id;
+#include "search.h"
 
 typedef struct _search_query poi_search_query;
 struct _search_query {
@@ -87,9 +69,11 @@ struct _search_dialog {
        GtkWidget *cmb_category;
        GtkWidget *list;
        GtkWidget *search_entry;
+       GtkWidget *btn_edit;
        GtkListStore *store;
        gdouble lat;
        gdouble lon;
+       mapper_search_type stype;
 };
 
 static search_dialog sd;
@@ -97,42 +81,41 @@ static search_dialog sd;
 static gboolean
 search_do_cb(GtkWidget *widget, gpointer data)
 {
-gchar *s;
+gchar *st;
 guint cid;
-GtkListStore *store;
 GtkTreeIter iter;
-search_dialog *sd=(search_dialog *)data;
-
-if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(sd->cmb_category), &iter)==TRUE)
-       gtk_tree_model_get(gtk_combo_box_get_model(GTK_COMBO_BOX(sd->cmb_category)), &iter, 0, &cid, -1);
-else
-       cid=-1;
+search_dialog *s=(search_dialog *)data;
 
-if ((strlen(gtk_entry_get_text(sd->search_entry))<2) && (cid==-1)) {
-       popup_error(sd->dialog, _("Query string too short. Minimum 2 characters."));
+if ((strlen(gtk_entry_get_text(s->search_entry))<2) && (cid==-1)) {
+       popup_error(s->dialog, _("Query string too short. Minimum 2 characters."));
        return TRUE;
 }
 
-s=g_strdup(gtk_entry_get_text(sd->search_entry));
-g_printf("Search: [%s]\n", s);
-
-#if 0
-store=gtk_tree_view_get_model(GTK_TREE_VIEW(sd->list));
-if (store) {
-       gtk_list_store_clear(store);
-       g_object_unref(G_OBJECT(store));
+st=g_strdup(gtk_entry_get_text(s->search_entry));
+
+switch (s->stype) {
+       case SEARCH_TYPE_POI:
+       if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(s->cmb_category), &iter)==TRUE)
+               gtk_tree_model_get(gtk_combo_box_get_model(GTK_COMBO_BOX(s->cmb_category)), &iter, 0, &cid, -1);
+       else
+               cid=-1;
+
+       if (poi_search((cid==-1) ? POI_SEARCH_TEXT : POI_SEARCH_TEXT_CAT, s->lat, s->lon, st, cid, &s->store)==TRUE) {
+               gtk_tree_view_set_model(GTK_TREE_VIEW(s->list), GTK_TREE_MODEL(s->store));
+               g_object_unref(G_OBJECT(s->store));
+       } else {
+               s->store=NULL;
+               gtk_tree_view_set_model(GTK_TREE_VIEW(s->list), GTK_TREE_MODEL(s->store));
+       }
+       break;
+       case SEARCH_TYPE_WAY:
+               popup_error(s->dialog, "Not yet implemented");
+       break;
+       case SEARCH_TYPE_PLACE:
+               popup_error(s->dialog, "Not yet implemented");
+       break;
 }
-#endif
-
-if (poi_search((cid==-1) ? POI_SEARCH_TEXT : POI_SEARCH_TEXT_CAT, sd->lat, sd->lon, s, cid, &sd->store)==TRUE) {
-       gtk_tree_view_set_model(GTK_TREE_VIEW(sd->list), GTK_TREE_MODEL(sd->store));
-       g_object_unref(G_OBJECT(sd->store));
-} else {
-       sd->store=NULL;
-       gtk_tree_view_set_model(GTK_TREE_VIEW(sd->list), GTK_TREE_MODEL(sd->store));
-}
-
-g_free(s);
+g_free(st);
 return TRUE;
 }
 
@@ -142,19 +125,19 @@ search_goto_cb(GtkWidget *widget, gpointer data)
 GtkTreeIter iter;
 GtkTreeModel *model;
 gdouble lat, lon;
-search_dialog *sd=(search_dialog *)data;
+search_dialog *s=(search_dialog *)data;
 
-if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(sd->list)), NULL, &iter)) {
-       popup_error(sd->dialog, _("Select one POI from the list."));
+if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(s->list)), NULL, &iter)) {
+       popup_error(s->dialog, _("Select one POI from the list."));
        return TRUE;
 }
 
-model=gtk_tree_view_get_model(GTK_TREE_VIEW(sd->list));
+model=gtk_tree_view_get_model(GTK_TREE_VIEW(s->list));
 if (!model)
        return TRUE;
 
-gtk_tree_model_get(model, &iter, POI_LAT, &lat, -1);
-gtk_tree_model_get(model, &iter, POI_LON, &lon, -1);
+gtk_tree_model_get(model, &iter, ITEM_LAT, &lat, -1);
+gtk_tree_model_get(model, &iter, ITEM_LON, &lon, -1);
 
 map_center_latlon(lat, lon);
 map_update_location_from_center();
@@ -167,20 +150,20 @@ search_route_to_cb(GtkWidget *widget, gpointer data)
 {
 GtkTreeIter iter;
 GtkTreeModel *model;
-search_dialog *sd=(search_dialog *)data;
+search_dialog *s=(search_dialog *)data;
 
-if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(sd->list)), NULL, &iter)) {
-       popup_error(sd->dialog, _("Select one POI from the list."));
+if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(s->list)), NULL, &iter)) {
+       popup_error(s->dialog, _("Select one POI from the list."));
        return TRUE;
 }
 
-model=gtk_tree_view_get_model(GTK_TREE_VIEW(sd->list));
+model=gtk_tree_view_get_model(GTK_TREE_VIEW(s->list));
 if (!model)
        return TRUE;
 
 _dest.valid=TRUE;
-gtk_tree_model_get(model, &iter, POI_LAT, &_dest.lat, -1);
-gtk_tree_model_get(model, &iter, POI_LON, &_dest.lon, -1);
+gtk_tree_model_get(model, &iter, ITEM_LAT, &_dest.lat, -1);
+gtk_tree_model_get(model, &iter, ITEM_LON, &_dest.lon, -1);
 
 map_update_location_from_center();
 
@@ -194,13 +177,55 @@ search_edit_cb(GtkWidget *widget, gpointer data)
 return TRUE;
 }
 
+static void
+search_set_dialog_mode(search_dialog *s)
+{
+s->stype=gtk_combo_box_get_active(GTK_COMBO_BOX(s->cmb_type));
+switch (s->stype) {
+       case SEARCH_TYPE_POI:
+               gtk_widget_show(s->cmb_category);
+               gtk_widget_set_sensitive(s->btn_edit, TRUE);
+       break;
+       case SEARCH_TYPE_WAY:
+       case SEARCH_TYPE_PLACE:
+               gtk_widget_hide(s->cmb_category);
+               gtk_widget_set_sensitive(s->btn_edit, FALSE);
+       break;
+       default:
+               g_assert_not_reached();
+       break;
+}
+}
+
+static gboolean
+search_type_changed_cb(GtkWidget *widget, gpointer data)
+{
+search_dialog *s=(search_dialog *)data;
+search_set_dialog_mode(s);
+if (s->store)
+       gtk_list_store_clear(s->store);
+s->store=NULL;
+gtk_tree_view_set_model(GTK_TREE_VIEW(s->list), GTK_TREE_MODEL(s->store));
+return TRUE;
+}
+
+static void
+search_item_activated_cb(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
+{
+g_printf("Boom!\n");
+}
+
 gboolean
-mapper_search_dialog(gdouble lat, gdouble lon)
+mapper_search_dialog(mapper_search_type stype, gdouble lat, gdouble lon)
 {
-GtkWidget *sw, *hbox, *label, *btn_search, *btn_goto, *btn_route_to, *btn_edit;
+GtkWidget *sw, *hbox, *label, *btn_search, *btn_goto, *btn_route_to;
 GtkTreeViewColumn *column;
 GtkCellRenderer *renderer;
-GtkTreeIter iter;
+
+sd.store=NULL;
+sd.lat=lat;
+sd.lon=lon;
+sd.stype=stype;
 
 sd.dialog = gtk_dialog_new_with_buttons(_("Search"),
                        GTK_WINDOW(_window),
@@ -209,17 +234,9 @@ sd.dialog = gtk_dialog_new_with_buttons(_("Search"),
                        GTK_RESPONSE_REJECT, 
                        NULL);
 
-sd.lat=lat;
-sd.lon=lon;
-
-gtk_container_add(GTK_CONTAINER(GTK_DIALOG(sd.dialog)->action_area),
-       btn_goto = gtk_button_new_with_label(_("Go")));
-
-gtk_container_add(GTK_CONTAINER(GTK_DIALOG(sd.dialog)->action_area),
-       btn_route_to = gtk_button_new_with_label(_("Route to")));
-
-gtk_container_add(GTK_CONTAINER(GTK_DIALOG(sd.dialog)->action_area),
-       btn_edit = gtk_button_new_with_label(_("Edit")));
+gtk_container_add(GTK_CONTAINER(GTK_DIALOG(sd.dialog)->action_area), btn_goto = gtk_button_new_with_label(_("Go to")));
+gtk_container_add(GTK_CONTAINER(GTK_DIALOG(sd.dialog)->action_area), btn_route_to = gtk_button_new_with_label(_("Route to")));
+gtk_container_add(GTK_CONTAINER(GTK_DIALOG(sd.dialog)->action_area), sd.btn_edit = gtk_button_new_with_label(_("Edit")));
 
 gtk_window_set_default_size(GTK_WINDOW(sd.dialog), 500, 300);
 
@@ -229,10 +246,10 @@ gtk_box_pack_start(GTK_BOX(GTK_DIALOG(sd.dialog)->vbox), hbox, FALSE, FALSE, 0);
 label=gtk_label_new(_("Search:"));
 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
 
-sd.cmb_type=gtk_combo_box_new();
+sd.cmb_type=gtk_combo_box_new_text();
 gtk_combo_box_append_text(GTK_COMBO_BOX(sd.cmb_type), "POI");
 gtk_combo_box_append_text(GTK_COMBO_BOX(sd.cmb_type), "Street");
-/* gtk_combo_box_append_text(GTK_COMBO_BOX(sd.cmb_type), "Place"); */
+gtk_combo_box_append_text(GTK_COMBO_BOX(sd.cmb_type), "Place");
 gtk_box_pack_start(GTK_BOX(hbox), sd.cmb_type, FALSE, FALSE, 0);
 
 sd.cmb_category = category_combo_new();
@@ -247,28 +264,23 @@ gtk_box_pack_start(GTK_BOX(hbox), btn_search, FALSE, FALSE, 0);
 
 sw = gtk_scrolled_window_new(NULL, NULL);
 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_ETCHED_IN);
-gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
+gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(sd.dialog)->vbox), sw, TRUE, TRUE, 0);
 
-if (store!=NULL) {
-       sd.list=gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
-       sd.store=store;
-       g_object_unref(G_OBJECT(store));
-} else {
-       sd.list=gtk_tree_view_new();
-       sd.store=NULL;
-}
-gtk_container_add(GTK_CONTAINER(sw), sd.list);
-
+sd.list=gtk_tree_view_new();
 gtk_tree_selection_set_mode(gtk_tree_view_get_selection (GTK_TREE_VIEW(sd.list)), GTK_SELECTION_SINGLE);
 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(sd.list), TRUE);
-gtk_tree_view_set_rules_hint (GTK_TREE_VIEW(sd.list), TRUE);
-gtk_tree_view_set_search_column (GTK_TREE_VIEW(sd.list), ITEM_LABEL);
+gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(sd.list), TRUE);
 
 renderer = gtk_cell_renderer_text_new();
 column = gtk_tree_view_column_new_with_attributes(_("Location"), renderer, "text", ITEM_LATLON, NULL);
 gtk_tree_view_append_column(GTK_TREE_VIEW(sd.list), column);
 
+renderer = gtk_cell_renderer_text_new();
+column = gtk_tree_view_column_new_with_attributes(_("Distance"), renderer, "text", ITEM_DIST, NULL);
+gtk_tree_view_column_set_sort_column_id (column, ITEM_DIST);
+gtk_tree_view_append_column(GTK_TREE_VIEW(sd.list), column);
+
 renderer = gtk_cell_renderer_text_new();
 column = gtk_tree_view_column_new_with_attributes(_("Label"), renderer, "text", ITEM_LABEL, NULL);
 gtk_tree_view_column_set_sort_column_id (column, ITEM_LABEL);
@@ -279,14 +291,21 @@ column = gtk_tree_view_column_new_with_attributes(_("Category"), renderer, "text
 gtk_tree_view_column_set_sort_column_id (column, ITEM_CATLAB);
 gtk_tree_view_append_column(GTK_TREE_VIEW(sd.list), column);
 
+gtk_tree_view_set_search_column (GTK_TREE_VIEW(sd.list), ITEM_LABEL);
+gtk_container_add(GTK_CONTAINER(sw), sd.list);
+
 g_signal_connect(G_OBJECT(btn_search), "clicked", G_CALLBACK(search_do_cb), &sd);
 g_signal_connect(G_OBJECT(btn_goto), "clicked", G_CALLBACK(search_goto_cb), &sd);
 g_signal_connect(G_OBJECT(btn_route_to), "clicked", G_CALLBACK(search_route_to_cb), &sd);
-g_signal_connect(G_OBJECT(btn_edit), "clicked", G_CALLBACK(search_edit_cb), &sd);
+g_signal_connect(G_OBJECT(sd.btn_edit), "clicked", G_CALLBACK(search_edit_cb), &sd);
+g_signal_connect(G_OBJECT(sd.cmb_type), "changed", G_CALLBACK(search_type_changed_cb), &sd);
+g_signal_connect(G_OBJECT(sd.list), "row-activated", G_CALLBACK(search_item_activated_cb), &sd);
 
 gtk_widget_show_all(sd.dialog);
+gtk_combo_box_set_active(GTK_COMBO_BOX(sd.cmb_type), sd.stype);
+search_set_dialog_mode(&sd);
 
-gtk_dialog_run(GTK_DIALOG(sd.dialog);
+gtk_dialog_run(GTK_DIALOG(sd.dialog));
 
 gtk_widget_destroy(sd.dialog);
 return TRUE;
index cc7ea34c895be0519696d6ce21a709c63e7709e9..79da37c4b2962c22928763543ef19fa564dc57de 100644 (file)
 
 #include <glib.h>
 
-gboolean mapper_search_dialog(gdouble lat, gdouble lon);
+typedef enum {
+       SEARCH_TYPE_POI=0,
+       SEARCH_TYPE_WAY=1,
+       SEARCH_TYPE_PLACE=2,
+} mapper_search_type;
+
+gboolean mapper_search_dialog(mapper_search_type stype, gdouble lat, gdouble lon);
 
 #endif