Merge pull request #44 from gergelypolonkai/recalc-if-needed

Recalculate chart only if needed
This commit is contained in:
Gergely Polonkai 2014-08-06 00:08:44 +02:00
commit 75e7bba711
6 changed files with 151 additions and 72 deletions

View File

@ -1060,7 +1060,7 @@ string_collate(const gchar *str1, const gchar *str2)
* with string_collate()), FALSE otherwise
*/
gboolean
ag_db_save_identical(const AgDbSave *a, const AgDbSave *b)
ag_db_save_identical(const AgDbSave *a, const AgDbSave *b, gboolean chart_only)
{
if (a == b) {
return TRUE;
@ -1070,15 +1070,15 @@ ag_db_save_identical(const AgDbSave *a, const AgDbSave *b)
return FALSE;
}
if (string_collate(a->name, b->name) != 0) {
if (!chart_only && string_collate(a->name, b->name) != 0) {
return FALSE;
}
if (string_collate(a->country, b->country) != 0) {
if (!chart_only && string_collate(a->country, b->country) != 0) {
return FALSE;
}
if (string_collate(a->city, b->city) != 0) {
if (!chart_only && string_collate(a->city, b->city) != 0) {
return FALSE;
}
@ -1126,7 +1126,7 @@ ag_db_save_identical(const AgDbSave *a, const AgDbSave *b)
return FALSE;
}
if (string_collate(a->note, b->note) != 0) {
if (!chart_only && string_collate(a->note, b->note) != 0) {
return FALSE;
}

View File

@ -68,7 +68,9 @@ GList *ag_db_get_chart_list(AgDb *db, GError **err);
AgDbSave *ag_db_get_chart_data_by_id(AgDb *db, guint row_id, GError **err);
gboolean ag_db_save_identical(const AgDbSave *a, const AgDbSave *b);
gboolean ag_db_save_identical(const AgDbSave *a,
const AgDbSave *b,
gboolean chart_only);
#define AG_DB_ERROR (ag_db_error_quark())
GQuark ag_db_error_quark(void);

View File

@ -58,7 +58,7 @@ G_DEFINE_QUARK(ag_window_error_quark, ag_window_error);
G_DEFINE_TYPE_WITH_PRIVATE(AgWindow, ag_window, GTK_TYPE_APPLICATION_WINDOW);
static void recalculate_chart(AgWindow *window);
static void ag_window_recalculate_chart(AgWindow *window);
static void
ag_window_gear_menu_action(GSimpleAction *action,
@ -108,7 +108,7 @@ ag_window_can_close(AgWindow *window, gboolean display_dialog)
save_data = ag_chart_get_db_save(priv->chart, db_id);
if (
!ag_db_save_identical(priv->saved_data, save_data)
!ag_db_save_identical(priv->saved_data, save_data, FALSE)
|| !(priv->saved_data)
|| (priv->saved_data->db_id == -1)
) {
@ -194,7 +194,7 @@ ag_window_export(AgWindow *window, GError **err)
gint response;
AgWindowPrivate *priv = ag_window_get_instance_private(window);
recalculate_chart(window);
ag_window_recalculate_chart(window);
// We should never enter here, but who knows...
if (priv->chart == NULL) {
@ -270,7 +270,7 @@ ag_window_save_action(GSimpleAction *action,
gint old_id;
AgDbSave *save_data;
recalculate_chart(window);
ag_window_recalculate_chart(window);
if (!ag_window_can_close(window, FALSE)) {
old_id = (priv->saved_data) ? priv->saved_data->db_id : -1;
@ -298,7 +298,7 @@ ag_window_export_action(GSimpleAction *action,
AgWindow *window = AG_WINDOW(user_data);
GError *err = NULL;
recalculate_chart(window);
ag_window_recalculate_chart(window);
ag_window_export(window, &err);
if (err) {
@ -319,7 +319,7 @@ ag_window_export_svg(AgWindow *window, GError **err)
gint response;
AgWindowPrivate *priv = ag_window_get_instance_private(window);
recalculate_chart(window);
ag_window_recalculate_chart(window);
// We should never enter here, but who knows...
if (priv->chart == NULL) {
@ -838,56 +838,76 @@ chart_changed(AgChart *chart, AgWindow *window)
}
static void
recalculate_chart(AgWindow *window)
ag_window_recalculate_chart(AgWindow *window)
{
GsweTimestamp *timestamp;
GtkTextIter start_iter,
end_iter;
AgDbSave *edit_data,
*chart_data;
AgWindowPrivate *priv = ag_window_get_instance_private(window);
gboolean south,
west;
GtkTreeIter house_system_iter;
GsweHouseSystem house_system;
gchar *note;
AgWindowPrivate *priv = ag_window_get_instance_private(window);
gint year = gtk_spin_button_get_value_as_int(
GTK_SPIN_BUTTON(priv->year)
),
month = gtk_spin_button_get_value_as_int(
GTK_SPIN_BUTTON(priv->month)
),
day = gtk_spin_button_get_value_as_int(
GTK_SPIN_BUTTON(priv->day)
),
hour = gtk_spin_button_get_value_as_int(
GTK_SPIN_BUTTON(priv->hour)
),
minute = gtk_spin_button_get_value_as_int(
GTK_SPIN_BUTTON(priv->minute)
),
second = gtk_spin_button_get_value_as_int(
GTK_SPIN_BUTTON(priv->second)
);
gdouble longitude = gtk_spin_button_get_value(
GTK_SPIN_BUTTON(priv->longitude)
),
latitude = gtk_spin_button_get_value(
GTK_SPIN_BUTTON(priv->latitude)
);
gboolean south = gtk_toggle_button_get_active(
GtkTextIter start_iter,
end_iter;
GsweTimestamp *timestamp;
gint db_id = (priv->saved_data) ? priv->saved_data->db_id : -1;
south = gtk_toggle_button_get_active(
GTK_TOGGLE_BUTTON(priv->south_lat)
),
west = gtk_toggle_button_get_active(
);
west = gtk_toggle_button_get_active(
GTK_TOGGLE_BUTTON(priv->west_long)
);
g_debug("Recalculating chart data");
edit_data = g_new0(AgDbSave, 1);
if (south) {
latitude = 0 - latitude;
}
edit_data->db_id = db_id;
edit_data->name = g_strdup(gtk_entry_get_text(GTK_ENTRY(priv->name)));
// TODO: This will cause problems with imported charts…
edit_data->country = NULL;
edit_data->city = NULL;
edit_data->longitude = gtk_spin_button_get_value(
GTK_SPIN_BUTTON(priv->longitude)
);
if (west) {
longitude = 0 - longitude;
edit_data->longitude = - edit_data->longitude;
}
edit_data->latitude = gtk_spin_button_get_value(
GTK_SPIN_BUTTON(priv->latitude)
);
if (south) {
edit_data->latitude = - edit_data->latitude;
}
// TODO: So as this…
edit_data->altitude = 280.0;
edit_data->year = gtk_spin_button_get_value_as_int(
GTK_SPIN_BUTTON(priv->year)
);
edit_data->month = gtk_spin_button_get_value_as_int(
GTK_SPIN_BUTTON(priv->month)
);
edit_data->day = gtk_spin_button_get_value_as_int(
GTK_SPIN_BUTTON(priv->day)
);
edit_data->hour = gtk_spin_button_get_value_as_int(
GTK_SPIN_BUTTON(priv->hour)
);
edit_data->minute = gtk_spin_button_get_value_as_int(
GTK_SPIN_BUTTON(priv->minute)
);
edit_data->second = gtk_spin_button_get_value_as_int(
GTK_SPIN_BUTTON(priv->second)
);
edit_data->timezone = gtk_spin_button_get_value(
GTK_SPIN_BUTTON(priv->timezone)
);
if (!gtk_combo_box_get_active_iter(
GTK_COMBO_BOX(priv->house_system),
&house_system_iter
@ -901,17 +921,44 @@ recalculate_chart(AgWindow *window)
0, &house_system,
-1
);
edit_data->house_system = g_strdup(
ag_house_system_id_to_nick(house_system)
);
gtk_text_buffer_get_bounds(priv->note_buffer, &start_iter, &end_iter);
edit_data->note = gtk_text_buffer_get_text(
priv->note_buffer,
&start_iter, &end_iter,
TRUE
);
chart_data = (priv->chart)
? ag_chart_get_db_save(priv->chart, db_id)
: NULL
;
if (ag_db_save_identical(edit_data, chart_data, TRUE)) {
g_debug("No redrawing needed");
ag_db_save_data_free(edit_data);
ag_db_save_data_free(chart_data);
return;
}
ag_db_save_data_free(chart_data);
g_debug("Recalculating chart data");
// TODO: Set timezone according to the city selected!
if (priv->chart == NULL) {
timestamp = gswe_timestamp_new_from_gregorian_full(
year, month, day,
hour, minute, second, 0,
1.0
edit_data->year, edit_data->month, edit_data->day,
edit_data->hour, edit_data->minute, edit_data->second, 0,
edit_data->timezone
);
priv->chart = ag_chart_new_full(
timestamp,
longitude, latitude, 380.0,
edit_data->longitude, edit_data->latitude, edit_data->altitude,
house_system
);
g_signal_connect(
@ -926,22 +973,14 @@ recalculate_chart(AgWindow *window)
timestamp = gswe_moment_get_timestamp(GSWE_MOMENT(priv->chart));
gswe_timestamp_set_gregorian_full(
timestamp,
year, month, day,
hour, minute, second, 0,
1.0,
edit_data->year, edit_data->month, edit_data->day,
edit_data->hour, edit_data->minute, edit_data->second, 0,
edit_data->timezone,
NULL
);
}
ag_chart_set_name(priv->chart, gtk_entry_get_text(GTK_ENTRY(priv->name)));
gtk_text_buffer_get_bounds(priv->note_buffer, &start_iter, &end_iter);
note = gtk_text_buffer_get_text(
priv->note_buffer,
&start_iter, &end_iter,
TRUE
);
ag_chart_set_note(priv->chart, note);
g_free(note);
ag_db_save_data_free(edit_data);
}
void
@ -971,14 +1010,10 @@ ag_window_tab_changed_cb(GtkStack *stack, GParamSpec *pspec, AgWindow *window)
gtk_stack_set_visible_child_name(GTK_STACK(priv->new_back_stack), "back");
}
// If we are coming from the Edit tab, lets assume the chart data has
// changed. This is a bad idea, though, it should be checked instead!
// (TODO)
// Note that priv->current_tab is actually the previously selected tab, not
// the real active one!
if (priv->current_tab == priv->tab_edit) {
recalculate_chart(window);
ag_window_recalculate_chart(window);
}
priv->current_tab = active_tab;
@ -1321,6 +1356,11 @@ ag_window_class_init(AgWindowClass *klass)
AgWindow,
db_chart_data
);
gtk_widget_class_bind_template_child_private(
widget_class,
AgWindow,
tab_edit
);
gtk_widget_class_bind_template_child_private(widget_class, AgWindow, name);
gtk_widget_class_bind_template_child_private(widget_class, AgWindow, year);
gtk_widget_class_bind_template_child_private(widget_class, AgWindow, month);

View File

@ -143,6 +143,38 @@ xml_read_gresource(void *context, char *buffer, int len)
return len;
}
const gchar *
ag_house_system_id_to_nick(GsweHouseSystem house_system)
{
GEnumClass *house_system_class;
GEnumValue *enum_value;
house_system_class = g_type_class_ref(GSWE_TYPE_HOUSE_SYSTEM);
enum_value = g_enum_get_value(house_system_class, house_system);
if (enum_value) {
return enum_value->value_nick;
}
return NULL;
}
GsweHouseSystem
ag_house_system_nick_to_id(const gchar *nick)
{
GEnumClass *house_system_class;
GEnumValue *enum_value;
house_system_class = g_type_class_ref(GSWE_TYPE_HOUSE_SYSTEM);
enum_value = g_enum_get_value_by_nick(house_system_class, nick);
if (enum_value) {
return enum_value->value;
}
return GSWE_HOUSE_SYSTEM_NONE;
}
int
main(int argc, char *argv[])
{

View File

@ -1,6 +1,8 @@
#ifndef __ASTROGNOME_H__
#define __ASTROGNOME_H__
#include <swe-glib.h>
typedef struct {
gboolean version;
gboolean quit;
@ -10,5 +12,8 @@ typedef struct {
extern GtkFileFilter *filter_all;
extern GtkFileFilter *filter_chart;
const gchar *ag_house_system_id_to_nick(GsweHouseSystem house_system);
GsweHouseSystem ag_house_system_nick_to_id(const gchar *nick);
#endif /* __ASTROGNOME_H__ */

View File

@ -306,7 +306,7 @@
<property name="hexpand">True</property>
<signal name="notify::visible-child" handler="ag_window_tab_changed_cb" object="AgWindow" swapped="no"/>
<child>
<object class="GtkGrid" id="edit_tab">
<object class="GtkGrid" id="tab_edit">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>