Add automatic timezone selection

master
Zwarf 2022-05-06 23:22:57 +02:00
rodzic 73c014f646
commit 4c8f9ec147
6 zmienionych plików z 87 dodań i 54 usunięć

Wyświetl plik

@ -21,7 +21,7 @@
<summary>Zoom Level</summary>
<description>The zoom level of the map</description>
</key>
<key name="time-zone" type="i">
<key name="time-zone" type="d">
<range min="-12" max="12"/>
<default>1</default>
<summary>Time-Zone</summary>

Wyświetl plik

@ -39,7 +39,7 @@ first_run_set_timezone (PicplannerApplication *application)
date_time = g_date_time_new_now_local ();
timezone = g_date_time_get_utc_offset (date_time);
g_settings_set_int (application->settings, "time-zone", timezone/3600/1000/1000);
g_settings_set_double (application->settings, "time-zone", (double) timezone/3600/1000/1000);
g_settings_set_boolean (application->settings, "first-run", false);
g_date_time_unref (date_time);

Wyświetl plik

@ -11,6 +11,7 @@
#include "calculations/calculations_milky_way.h"
#include "map/draw-layer.h"
#include "map/marker.h"
#include <libgweather/gweather.h>
#define LENGTH_SCALE_FACTOR 0.38
#define ICON_SIZE_SMALL 32
@ -33,6 +34,8 @@ struct _PicplannerOverview
GtkWidget *switch_moon;
GtkWidget *switch_milky_way;
GtkWidget *label_time_zone;
GtkWidget *label_sun_rise;
GtkWidget *label_sun_set;
GtkWidget *label_moon_rise;
@ -77,10 +80,60 @@ struct _PicplannerOverview
G_DEFINE_TYPE (PicplannerOverview, picplanner_overview, GTK_TYPE_BOX)
/*
* Changing the date_time variable after a user input was recognized.
* Afterwards, emit a signal to start the calculations.
*/
void
picplanner_update_date_time (PicplannerOverview *overview)
{
int year, month, day, hour, minute;
double latitude, longitude;
char *char_time_zone;
GDateTime *date_selected;
GWeatherLocation *loc;
latitude = shumate_location_get_latitude (SHUMATE_LOCATION (overview->viewport));
longitude = shumate_location_get_longitude (SHUMATE_LOCATION (overview->viewport));
loc = gweather_location_new_detached ("Custom Location", NULL, latitude, longitude);
/*
* Get year, month and day information from the calendar widget.
*/
date_selected = gtk_calendar_get_date (GTK_CALENDAR (overview->calendar));
year = g_date_time_get_year (date_selected);
month = g_date_time_get_month (date_selected);
day = g_date_time_get_day_of_month (date_selected);
/*
* Get hour and minute from the spin buttons.
*/
hour = gtk_spin_button_get_value (GTK_SPIN_BUTTON (overview->spin_button_hour));
minute = gtk_spin_button_get_value (GTK_SPIN_BUTTON (overview->spin_button_minute));
/*
* Save the information into the date_time variable.
*/
overview->date_time = g_date_time_new (gweather_location_get_timezone (loc),
year, month, day,
hour, minute,
0);
g_settings_set_double (overview->settings,
"time-zone",
(double) g_date_time_get_utc_offset (overview->date_time)/3600/1000/1000);
char_time_zone = g_strdup_printf ("Timezone: %.1f", (double) g_date_time_get_utc_offset (overview->date_time)/3600/1000/1000);
gtk_label_set_text (GTK_LABEL (overview->label_time_zone), char_time_zone);
g_free (char_time_zone);
g_date_time_unref (date_selected);
}
static void
emit_signal_changed (GtkWidget *self,
gpointer overview_user_input,
gpointer overview_view_size_change)
gpointer overview_user_input,
gpointer overview_view_size_change)
{
(void) self;
PicplannerOverview *overview;
@ -107,6 +160,7 @@ GDateTime
return overview->date_time;
}
void
picplanner_overview_set_current_coordinates_sun (PicplannerOverview *overview,
double *array_coordinates_sun,
@ -392,7 +446,7 @@ picplanner_overview_update_map_moon (PicplannerOverview *overview,
if (array_coordinates_moon[current_index*2+1]<0)
visible_moon = FALSE;
image_moon = gtk_image_new_from_icon_name ("fcitx-fullwidth-inactive-symbolic");
image_moon = gtk_image_new_from_icon_name ("weather-clear-night-symbolic");
gtk_image_set_pixel_size (GTK_IMAGE (image_moon), ICON_SIZE_LARGE);
gtk_widget_set_visible (image_moon, visible_moon);
@ -515,49 +569,6 @@ picplanner_overview_update_map_milky_way (PicplannerOverview *overview,
picplanner_draw_layer_set_stroke_width (overview->pp_layer_milky_way_rise_set, milky_way_stroke_width);
}
/*
* Changing the date_time variable after a user input was recognized.
* Afterwards, emit a signal to start the calculations.
*/
static void
change_date_time (GtkWidget *widget,
gpointer user_data)
{
(void) widget;
int year, month, day, hour, minute;
GDateTime *date_selected;
PicplannerOverview *overview = PICPLANNER_OVERVIEW (user_data);
/*
* Get year, month and day information from the calendar widget.
*/
date_selected = gtk_calendar_get_date (GTK_CALENDAR (overview->calendar));
year = g_date_time_get_year (date_selected);
month = g_date_time_get_month (date_selected);
day = g_date_time_get_day_of_month (date_selected);
/*
* Get hour and minute from the spin buttons.
*/
hour = gtk_spin_button_get_value (GTK_SPIN_BUTTON (overview->spin_button_hour));
minute = gtk_spin_button_get_value (GTK_SPIN_BUTTON (overview->spin_button_minute));
/*
* Save the information into the date_time variable.
*/
overview->date_time = g_date_time_new (g_date_time_get_timezone (overview->date_time),
year, month, day,
hour, minute,
0);
g_date_time_unref (date_selected);
/*
* Emit a signal that the user changed the input.
*/
emit_signal_changed (widget, user_data, NULL);
}
/*
* Change the text of the GtkSpinButton to always be a two digit number.
@ -662,7 +673,7 @@ picplanner_overview_init (PicplannerOverview *overview)
* Setting the date and time of the date and time selector to the current date and time
* with the timezone set in preferences
*/
timezone = g_time_zone_new_offset (g_settings_get_int (overview->settings, "time-zone") * 3600);
timezone = g_time_zone_new_offset (g_settings_get_double (overview->settings, "time-zone") * 3600);
overview->date_time = g_date_time_new_now (timezone);
gtk_calendar_select_day (GTK_CALENDAR (overview->calendar), overview->date_time);
gtk_spin_button_set_value (GTK_SPIN_BUTTON (overview->spin_button_hour),
@ -750,17 +761,17 @@ picplanner_overview_init (PicplannerOverview *overview)
g_signal_connect (G_OBJECT (overview->spin_button_hour),
"value-changed",
G_CALLBACK (change_date_time),
G_CALLBACK (emit_signal_changed),
overview);
g_signal_connect (G_OBJECT (overview->spin_button_minute),
"value-changed",
G_CALLBACK (change_date_time),
G_CALLBACK (emit_signal_changed),
overview);
g_signal_connect (G_OBJECT (overview->calendar),
"day-selected",
G_CALLBACK (change_date_time),
G_CALLBACK (emit_signal_changed),
overview);
g_signal_connect (G_OBJECT (overview->viewport),
@ -806,6 +817,7 @@ picplanner_overview_class_init (PicplannerOverviewClass *class)
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), PicplannerOverview, switch_sun);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), PicplannerOverview, switch_moon);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), PicplannerOverview, switch_milky_way);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), PicplannerOverview, label_time_zone);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), PicplannerOverview, label_sun_rise);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), PicplannerOverview, label_sun_set);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), PicplannerOverview, label_moon_rise);

Wyświetl plik

@ -9,9 +9,15 @@ G_DECLARE_FINAL_TYPE (PicplannerOverview, picplanner_overview, PICPLANNER, OVERV
PicplannerOverview
*picplanner_overview_new ();
void
picplanner_update_date_time (PicplannerOverview *overview);
GDateTime
*picplanner_overview_get_date_time (PicplannerOverview *overview);
void
picplanner_overview_update_date_time (PicplannerOverview *overview);
void
picplanner_overview_set_current_coordinates_sun (PicplannerOverview *overview,
double *array_coordinates_sun,

Wyświetl plik

@ -96,6 +96,12 @@
<signal name="output" handler="time_spin_button_text"/>
</object>
</child>
<child>
<object class="GtkLabel" id="label_time_zone">
<property name="margin_start">20</property>
<property name="label">Timezone: </property>
</object>
</child>
</object>
</child>

Wyświetl plik

@ -232,7 +232,11 @@ calculate_positions (PicplannerWindow *window)
longitude = gtk_spin_button_get_value (GTK_SPIN_BUTTON (window->east_entry));
latitude = gtk_spin_button_get_value (GTK_SPIN_BUTTON (window->north_entry));
picplanner_update_date_time (PICPLANNER_OVERVIEW (window->overview_box));
date_time = picplanner_overview_get_date_time (PICPLANNER_OVERVIEW (window->overview_box));
date_time_noon = g_date_time_new (g_date_time_get_timezone (date_time),
g_date_time_get_year (date_time),
g_date_time_get_month (date_time),
@ -241,12 +245,13 @@ calculate_positions (PicplannerWindow *window)
current_index = g_date_time_difference (date_time, date_time_noon)/1000/1000/60 * NUM_DATA_POINTS/24/60 + NUM_DATA_POINTS/2;
g_print("Date: %02d.%02d.%02d, Time: %02d:%02d\n",
g_print("Date: %02d.%02d.%02d, Time: %02d:%02d, Timezone: %.2f\n",
g_date_time_get_day_of_month (date_time),
g_date_time_get_month (date_time),
g_date_time_get_year (date_time),
g_date_time_get_hour(date_time),
g_date_time_get_minute (date_time));
g_date_time_get_minute (date_time),
(double) g_date_time_get_utc_offset (date_time)/3600/1000/1000);
g_print ("Coordinates changed to: N %f, E %f \n",
latitude,
longitude);
@ -358,6 +363,10 @@ calculate_positions (PicplannerWindow *window)
g_free (array_coordinates_moon);
g_free (array_coordinates_milky_way);
g_date_time_unref (date_time_noon);
/*
* TODO:
* Free loc?
*/
}