From 2e73766a229dcf4e47fd89a3da8e4c92012a6c48 Mon Sep 17 00:00:00 2001 From: Zwarf Date: Fri, 25 Mar 2022 18:52:18 +0100 Subject: [PATCH] Improve search functionality --- src/window/picplanner-window.c | 97 ++++++++++++++++++++++++--------- src/window/picplanner-window.ui | 26 ++++++--- 2 files changed, 88 insertions(+), 35 deletions(-) diff --git a/src/window/picplanner-window.c b/src/window/picplanner-window.c index f17f594..47e16a7 100644 --- a/src/window/picplanner-window.c +++ b/src/window/picplanner-window.c @@ -32,6 +32,11 @@ */ #define INPUT_CHANGED_TIMEOUT_LENGTH 300 +/* + * The amount of search results to be shown + */ +#define NUM_SEARCH_RESULTS 15 + struct _PicplannerWindow { @@ -40,7 +45,7 @@ struct _PicplannerWindow /* Template widgets */ GtkHeaderBar *header_bar; GtkWidget *box_main; - GtkWidget *stack; + GtkWidget *stack; /* Stack containing overview, sun, moon and milky way page */ GtkWidget *search_bar; /* Search bar for the location */ GtkWidget *search_entry; /* The search entry inside the search bar */ GtkWidget *search_button; /* The search button in the header bar so show the search bar */ @@ -48,24 +53,23 @@ struct _PicplannerWindow GtkWidget *sun_box; /* The sun page */ GtkWidget *moon_box; /* The moon page */ 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]; + GtkWidget *north_entry; /* SpinButton for North coordinates */ + GtkWidget *east_entry; /* SpinButton for East coordinates */ + /* Search functionality */ + GtkWidget *search_result_box; /* ListBox containing search results */ + GtkWidget *search_results_scroll; /* ScrollView containing search_result_box */ + GtkWidget *search_result_row[NUM_SEARCH_RESULTS]; + GtkWidget *search_result_label[NUM_SEARCH_RESULTS]; + GWeatherLocation *search_result_location[NUM_SEARCH_RESULTS]; + + /* Regulating amount of inputs per time */ gint input_count; guint input_timeout_id; gboolean input_new; - GSettings *settings; /* All settings of PicPlanner */ - - /* Variables to limit the amount of user searches on can make per second */ - gboolean search_new; - gint search_count; - guint search_timeout_id; + /* All settings of PicPlanner */ + GSettings *settings; }; G_DEFINE_TYPE (PicplannerWindow, picplanner_window, ADW_TYPE_APPLICATION_WINDOW) @@ -80,14 +84,23 @@ search_location_chosen (GtkWidget *self, PicplannerWindow *window = user_data; const char *title; int i; - for (i=0; i<10; i++) + for (i=0; isearch_result_row[i]) break; } title = adw_preferences_row_get_title (ADW_PREFERENCES_ROW (window->search_result_row[i])); g_print ("Chosen: %s\n", title); + double latitude; + double longitude; + double *p_latitude = &latitude; + double *p_longitude = &longitude; + gweather_location_get_coords (GWEATHER_LOCATION (window->search_result_location[i]), p_latitude, p_longitude); + g_print ("N: %f, E:%f\n", latitude, longitude); gtk_search_bar_set_search_mode (GTK_SEARCH_BAR (window->search_bar), FALSE); + + gtk_spin_button_set_value (GTK_SPIN_BUTTON (window->north_entry), latitude); + gtk_spin_button_set_value (GTK_SPIN_BUTTON (window->east_entry), longitude); } static gboolean @@ -102,20 +115,40 @@ find_loc_children (GWeatherLocation *location, if (gweather_location_get_level (child) == GWEATHER_LOCATION_CITY) { const char *city = gweather_location_get_name (child); - 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)) + const char *region = gweather_location_get_name (gweather_location_get_parent (child)); + const char *country = gweather_location_get_country_name (child); + char *location_string_merge; + if (gweather_location_get_level (gweather_location_get_parent (child)) + == GWEATHER_LOCATION_COUNTRY) + { + location_string_merge = g_strdup_printf ("%s, %s", city, country); + } + else + { + location_string_merge = g_strdup_printf ("%s, %s, %s", + city, + region, + country); + } + + char *location_norm = g_utf8_normalize (location_string_merge, + strlen (location_string_merge), + G_NORMALIZE_ALL_COMPOSE); + char *location_string = g_utf8_casefold (location_norm, strlen (location_norm)); + + g_free (location_string_merge); + g_free (location_norm); + if (strstr (location_string, search_str)) { locations[*iteration] = g_object_ref(child); (*iteration)++; - if (*iteration>=10) + if (*iteration>=NUM_SEARCH_RESULTS) { - g_free (city_string); + g_free (location_string); return TRUE; } } - g_free (city_string); + g_free (location_string); } else { @@ -141,7 +174,7 @@ search_location (GtkWidget *self, world = gweather_location_get_world (); - for (int i=0; i<10; i++) + for (int i=0; isearch_result_location[i])) g_object_unref (window->search_result_location[i]); @@ -159,14 +192,14 @@ search_location (GtkWidget *self, double *p_longitude = &longitude; double *p_latitude = &latitude; char *char_coordinates; + char *char_subtitle; 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++) + for (int i=0; isearch_result_row[i])) { @@ -185,8 +218,19 @@ search_location (GtkWidget *self, adw_preferences_row_set_title (ADW_PREFERENCES_ROW (window->search_result_row[i]), gweather_location_get_name (window->search_result_location[i])); + if (gweather_location_get_level ( + gweather_location_get_parent (window->search_result_location[i])) == GWEATHER_LOCATION_COUNTRY || + gweather_location_get_level (window->search_result_location[i]) == GWEATHER_LOCATION_COUNTRY) + char_subtitle = g_strdup_printf ("%s", + gweather_location_get_country_name (window->search_result_location[i])); + else + char_subtitle = g_strdup_printf ("%s, %s", + gweather_location_get_name (gweather_location_get_parent (window->search_result_location[i])), + gweather_location_get_country_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])); + char_subtitle); window->search_result_label[i] = gtk_label_new (char_coordinates); @@ -209,7 +253,6 @@ search_location (GtkWidget *self, else { 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); } diff --git a/src/window/picplanner-window.ui b/src/window/picplanner-window.ui index 1045b05..3066623 100644 --- a/src/window/picplanner-window.ui +++ b/src/window/picplanner-window.ui @@ -89,7 +89,7 @@ true true - Search... + Search... City, Region, Country @@ -139,18 +139,28 @@ fill - - false + true fill true - fill - 10 - 10 - 5 - 5 + start + 15 + 15 + 15 + 15 + + + + true + fill + true + fill + + + +