Improve search functionality

master
Zwarf 2022-03-24 23:46:01 +01:00
rodzic 58519b5b8d
commit 5939858559
2 zmienionych plików z 136 dodań i 33 usunięć

Wyświetl plik

@ -50,6 +50,11 @@ struct _PicplannerWindow
GtkWidget *milky_way_box; /* The milky way page */
GtkWidget *north_entry;
GtkWidget *east_entry;
GtkWidget *search_result_box;
GtkWidget *search_results_scroll;
GtkWidget *search_result_row[10];
GtkWidget *search_result_label[10];
GWeatherLocation *search_result_location[10];
gint input_count;
guint input_timeout_id;
@ -65,82 +70,151 @@ struct _PicplannerWindow
G_DEFINE_TYPE (PicplannerWindow, picplanner_window, ADW_TYPE_APPLICATION_WINDOW)
static void
search_location_chosen (GtkWidget *self,
GtkWidget *row,
gpointer user_data)
{
(void) self;
(void) user_data;
PicplannerWindow *window = user_data;
const char *title;
int i;
for (i=0; i<10; i++)
{
if (row == window->search_result_row[i])
break;
}
title = adw_preferences_row_get_title (ADW_PREFERENCES_ROW (window->search_result_row[i]));
g_print ("Chosen: %s\n", title);
gtk_search_bar_set_search_mode (GTK_SEARCH_BAR (window->search_bar), FALSE);
}
static gboolean
find_loc_children (GWeatherLocation *location,
const char *search_str,
GWeatherLocation **locations,
int *iteration)
{
GWeatherLocation *child = NULL;
g_autoptr (GWeatherLocation) child = NULL;
while ((child = gweather_location_next_child (location, child)) != NULL)
{
if (gweather_location_get_level (child) == GWEATHER_LOCATION_CITY &&
*iteration < 10)
if (gweather_location_get_level (child) == GWEATHER_LOCATION_CITY)
{
const char *city = gweather_location_get_name (child);
const char *city_norm = g_utf8_normalize (city, strlen (city), G_NORMALIZE_ALL_COMPOSE);
const char *city_string = g_utf8_casefold (city_norm, strlen (city_norm));
char *city_norm = g_utf8_normalize (city, strlen (city), G_NORMALIZE_ALL_COMPOSE);
char *city_string = g_utf8_casefold (city_norm, strlen (city_norm));
g_free (city_norm);
if (strstr (city_string, search_str))
{
locations[*iteration] = g_object_ref(child);
(*iteration)++;
//g_print ("Search: %s, City to append: %s\n",search_str, city_string);
if (*iteration>=10)
{
g_free (city_string);
return TRUE;
}
}
g_free (city_string);
}
else
{
if (find_loc_children (child, search_str, locations, iteration))
return TRUE;
return TRUE;
}
}
return FALSE;
return FALSE;
}
static void
find_loc (GWeatherLocation *world,
const char *search_str,
GWeatherLocation **locations)
{
int *iteration;
int iter = 0;
iteration = &iter;
find_loc_children (world, search_str, locations, iteration);
}
static void
search_location (GtkWidget *self,
gpointer user_data)
{
(void) self;
PicplannerWindow *window = user_data;
(void) window;
const char *search_text = gtk_editable_get_text (GTK_EDITABLE (window->search_entry));
const char *search_norm = g_utf8_normalize (search_text, strlen (search_text), G_NORMALIZE_ALL_COMPOSE);
const char *search_string = g_utf8_casefold (search_text, strlen (search_norm));
g_print ("Search string: %s\n", search_string);
char *search_norm = g_utf8_normalize (search_text, strlen (search_text), G_NORMALIZE_ALL_COMPOSE);
char *search_string = g_utf8_casefold (search_text, strlen (search_norm));
g_autoptr (GWeatherLocation) world = NULL;
GWeatherLocation *world = gweather_location_get_world ();
GWeatherLocation *locations[10];
world = gweather_location_get_world ();
for (int i=0; i<10; i++)
locations[i] = NULL;
{
if (GWEATHER_IS_LOCATION (window->search_result_location[i]))
g_object_unref (window->search_result_location[i]);
window->search_result_location[i] = NULL;
}
if (!strstr ("", search_string))
{
find_loc (world, search_string, locations);
int *iteration;
int iter = 0;
iteration = &iter;
double longitude;
double latitude;
double *p_longitude = &longitude;
double *p_latitude = &latitude;
char *char_coordinates;
gtk_widget_set_visible (window->stack, FALSE);
gtk_widget_set_visible (window->search_result_box, TRUE);
gtk_widget_set_visible (window->search_results_scroll, TRUE);
find_loc_children (world, search_string, window->search_result_location, iteration);
for (int i=0; i<10; i++)
{
if (GTK_IS_WIDGET (window->search_result_row[i]))
{
gtk_list_box_remove (GTK_LIST_BOX (window->search_result_box),
window->search_result_row[i]);
}
window->search_result_row[i] = NULL;
if (window->search_result_location[i])
{
window->search_result_row[i] = adw_action_row_new ();
gweather_location_get_coords (window->search_result_location[i],
p_latitude, p_longitude);
char_coordinates = g_strdup_printf ("N: %.04f\nE: %.04f", latitude, longitude);
adw_preferences_row_set_title (ADW_PREFERENCES_ROW (window->search_result_row[i]),
gweather_location_get_name (window->search_result_location[i]));
adw_action_row_set_subtitle (ADW_ACTION_ROW (window->search_result_row[i]),
gweather_location_get_country_name (window->search_result_location[i]));
window->search_result_label[i] = gtk_label_new (char_coordinates);
adw_action_row_add_suffix (ADW_ACTION_ROW (window->search_result_row[i]),
window->search_result_label[i]);
gtk_widget_set_can_focus (window->search_result_row[i], TRUE);
gtk_list_box_row_set_activatable (GTK_LIST_BOX_ROW (window->search_result_row[i]), TRUE);
gtk_list_box_row_set_selectable (GTK_LIST_BOX_ROW (window->search_result_row[i]), TRUE);
gtk_list_box_append (GTK_LIST_BOX (window->search_result_box),
window->search_result_row[i]);
g_free (char_coordinates);
}
}
}
for (int i=0; i<10; i++)
else
{
if (locations[i])
g_print ("Found locations: %s\n", gweather_location_get_name (locations[i]));
gtk_widget_set_visible (window->stack, TRUE);
gtk_widget_set_visible (window->search_result_box, FALSE);
gtk_widget_set_visible (window->search_results_scroll, FALSE);
}
g_free (search_norm);
g_free (search_string);
}
static void
@ -303,6 +377,8 @@ picplanner_window_class_init (PicplannerWindowClass *klass)
gtk_widget_class_bind_template_child (widget_class, PicplannerWindow, milky_way_box);
gtk_widget_class_bind_template_child (widget_class, PicplannerWindow, north_entry);
gtk_widget_class_bind_template_child (widget_class, PicplannerWindow, east_entry);
gtk_widget_class_bind_template_child (widget_class, PicplannerWindow, search_result_box);
gtk_widget_class_bind_template_child (widget_class, PicplannerWindow, search_results_scroll);
gtk_widget_class_bind_template_callback (widget_class, search_location);
}
@ -339,6 +415,8 @@ picplanner_window_init (PicplannerWindow *window)
g_signal_connect (G_OBJECT (window->overview_box), "input-changed", G_CALLBACK (input_changed), window);
g_signal_connect (G_OBJECT (window->search_result_box), "row-activated", G_CALLBACK (search_location_chosen), window);
/*
* Initialisation of values needed to detect when a user input ends
*/
@ -346,6 +424,7 @@ picplanner_window_init (PicplannerWindow *window)
window->input_count = 0;
window->input_timeout_id = 0;
/*
* Calculate values after application start
*/

Wyświetl plik

@ -130,6 +130,30 @@
</object>
</child>
<child>
<object class="GtkScrolledWindow" id="search_results_scroll">
<property name="visible">false</property>
<property name="hexpand">true</property>
<property name="halign">fill</property>
<property name="vexpand">true</property>
<property name="valign">fill</property>
<child>
<object class="GtkListBox" id="search_result_box">
<property name="visible">false</property>
<property name="hexpand">true</property>
<property name="halign">fill</property>
<property name="vexpand">true</property>
<property name="valign">fill</property>
<property name="margin-start">10</property>
<property name="margin-end">10</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>
</object>
</child>
</object>
</child>
<child>
<object class="AdwViewStack" id="stack">
<property name="vexpand">true</property>