]> err.no Git - mapper/commitdiff
Start to make search generic for POIs and Ways
authorKaj-Michael Lang <milang@tal.org>
Wed, 31 Oct 2007 07:38:32 +0000 (09:38 +0200)
committerKaj-Michael Lang <milang@tal.org>
Wed, 31 Oct 2007 07:38:32 +0000 (09:38 +0200)
src/search.c [new file with mode: 0644]
src/search.h [new file with mode: 0644]

diff --git a/src/search.c b/src/search.c
new file mode 100644 (file)
index 0000000..ad2e7b9
--- /dev/null
@@ -0,0 +1,271 @@
+/*
+ * This file is part of mapper
+ *
+ * Copyright (C) 2007 Kaj-Michael Lang
+ * Copyright (C) 2006-2007 John Costigan.
+ *
+ * POI and GPS-Info code originally written by Cezary Jackiewicz.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <config.h>
+
+#define _GNU_SOURCE
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <stddef.h>
+#include <locale.h>
+#include <math.h>
+#include <errno.h>
+#include <sys/wait.h>
+#include <glib/gstdio.h>
+#include <gtk/gtk.h>
+#include <fcntl.h>
+#include <libintl.h>
+#include <locale.h>
+
+#include "utils.h"
+#include "poi.h"
+#include "gps.h"
+#include "map.h"
+#include "mapper-types.h"
+#include "ui-common.h"
+#include "settings.h"
+#include "poi-gui.h"
+#include "osm.h"
+
+#include "help.h"
+
+typedef struct _search_query poi_search_query;
+struct _search_query {
+       gdouble lat;
+       gdouble lon;
+       guint cat_id;
+       gchar *query;
+};
+
+typedef struct _search_dialog search_dialog;
+struct _search_dialog {
+       GtkWidget *dialog;
+       GtkWidget *list;
+       GtkWidget *cmb_category;
+       GtkWidget *search_entry;
+       GtkListStore *store;
+       gdouble lat;
+       gdouble lon;
+};
+
+static search_dialog sd;
+
+static gboolean
+search_do_cb(GtkWidget *widget, gpointer data)
+{
+gchar *s;
+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;
+
+if ((strlen(gtk_entry_get_text(sd->search_entry))<2) && (cid==-1)) {
+       popup_error(sd->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));
+}
+#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);
+return TRUE;
+}
+
+static gboolean
+search_goto_cb(GtkWidget *widget, gpointer data)
+{
+GtkTreeIter iter;
+GtkTreeModel *model;
+gdouble lat, lon;
+search_dialog *sd=(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."));
+       return TRUE;
+}
+
+model=gtk_tree_view_get_model(GTK_TREE_VIEW(sd->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);
+
+map_center_latlon(lat, lon);
+map_update_location_from_center();
+
+return TRUE;
+}
+
+static gboolean
+search_route_to_cb(GtkWidget *widget, gpointer data)
+{
+GtkTreeIter iter;
+GtkTreeModel *model;
+search_dialog *sd=(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."));
+       return TRUE;
+}
+
+model=gtk_tree_view_get_model(GTK_TREE_VIEW(sd->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);
+
+map_update_location_from_center();
+
+return TRUE;
+}
+
+gboolean
+mapper_search_dialog(gdouble lat, gdouble lon)
+{
+GtkWidget *sw, *hbox, *label, *btn_search, *btn_goto, *btn_route_to;
+GtkTreeViewColumn *column;
+GtkCellRenderer *renderer;
+GtkTreeIter iter;
+gboolean selected = FALSE;
+
+sd.dialog = gtk_dialog_new_with_buttons(_("Search"),
+                       GTK_WINDOW(_window),
+                       GTK_DIALOG_MODAL, 
+                       GTK_STOCK_OK,
+                       GTK_RESPONSE_ACCEPT,
+                       GTK_STOCK_CANCEL,
+                       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_window_set_default_size(GTK_WINDOW(sd.dialog), 500, 300);
+
+hbox = gtk_hbox_new(FALSE, 4),
+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_category = category_combo_new();
+gtk_box_pack_start(GTK_BOX(hbox), sd.cmb_category, FALSE, FALSE, 0);
+poi_category_combo_populate(sd.cmb_category, 0, TRUE);
+
+sd.search_entry=gtk_entry_new();
+gtk_box_pack_start(GTK_BOX(hbox), sd.search_entry, TRUE, TRUE, 0);
+
+btn_search=gtk_button_new_with_label(_("Search"));
+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_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);
+
+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), POI_LABEL);
+
+renderer = gtk_cell_renderer_text_new();
+column = gtk_tree_view_column_new_with_attributes(_("Location"), renderer, "text", POI_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(_("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(sd.list), column);
+
+renderer = gtk_cell_renderer_text_new();
+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(sd.list), column);
+
+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);
+
+gtk_widget_show_all(sd.dialog);
+
+while (GTK_RESPONSE_ACCEPT == gtk_dialog_run(GTK_DIALOG(sd.dialog))) {
+       if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(sd.list)), NULL, &iter)) {
+               gtk_tree_model_get(GTK_TREE_MODEL(sd.store), &iter, POI_POIID, &(poi->poi_id), -1);
+               gtk_tree_model_get(GTK_TREE_MODEL(sd.store), &iter, POI_CATID, &(poi->cat_id), -1);
+               gtk_tree_model_get(GTK_TREE_MODEL(sd.store), &iter, POI_LAT, &(poi->lat), -1);
+               gtk_tree_model_get(GTK_TREE_MODEL(sd.store), &iter, POI_LON, &(poi->lon), -1);
+               gtk_tree_model_get(GTK_TREE_MODEL(sd.store), &iter, POI_LABEL, &(poi->label), -1);
+               gtk_tree_model_get(GTK_TREE_MODEL(sd.store), &iter, POI_DESC, &(poi->desc), -1);
+               selected = TRUE;
+               break;
+       } else {
+               popup_error(sd.dialog, _("Select one POI from the list."));
+       }
+}
+
+gtk_widget_destroy(sd.dialog);
+return selected;
+}
+
diff --git a/src/search.h b/src/search.h
new file mode 100644 (file)
index 0000000..cc7ea34
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * This file is part of mapper
+ *
+ * Copyright (C) 2007 Kaj-Michael Lang
+ *
+ * POI and GPS-Info code originally written by Cezary Jackiewicz.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#ifndef _MAPPER_SEARCH_H
+#define _MAPPER_SEARCH_H
+
+#include <glib.h>
+
+gboolean mapper_search_dialog(gdouble lat, gdouble lon);
+
+#endif