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 * with string_collate()), FALSE otherwise
*/ */
gboolean 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) { if (a == b) {
return TRUE; return TRUE;
@ -1070,15 +1070,15 @@ ag_db_save_identical(const AgDbSave *a, const AgDbSave *b)
return FALSE; return FALSE;
} }
if (string_collate(a->name, b->name) != 0) { if (!chart_only && string_collate(a->name, b->name) != 0) {
return FALSE; return FALSE;
} }
if (string_collate(a->country, b->country) != 0) { if (!chart_only && string_collate(a->country, b->country) != 0) {
return FALSE; return FALSE;
} }
if (string_collate(a->city, b->city) != 0) { if (!chart_only && string_collate(a->city, b->city) != 0) {
return FALSE; return FALSE;
} }
@ -1126,7 +1126,7 @@ ag_db_save_identical(const AgDbSave *a, const AgDbSave *b)
return FALSE; return FALSE;
} }
if (string_collate(a->note, b->note) != 0) { if (!chart_only && string_collate(a->note, b->note) != 0) {
return FALSE; 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); 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()) #define AG_DB_ERROR (ag_db_error_quark())
GQuark ag_db_error_quark(void); 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); 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 static void
ag_window_gear_menu_action(GSimpleAction *action, 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); save_data = ag_chart_get_db_save(priv->chart, db_id);
if ( 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)
|| (priv->saved_data->db_id == -1) || (priv->saved_data->db_id == -1)
) { ) {
@ -194,7 +194,7 @@ ag_window_export(AgWindow *window, GError **err)
gint response; gint response;
AgWindowPrivate *priv = ag_window_get_instance_private(window); AgWindowPrivate *priv = ag_window_get_instance_private(window);
recalculate_chart(window); ag_window_recalculate_chart(window);
// We should never enter here, but who knows... // We should never enter here, but who knows...
if (priv->chart == NULL) { if (priv->chart == NULL) {
@ -270,7 +270,7 @@ ag_window_save_action(GSimpleAction *action,
gint old_id; gint old_id;
AgDbSave *save_data; AgDbSave *save_data;
recalculate_chart(window); ag_window_recalculate_chart(window);
if (!ag_window_can_close(window, FALSE)) { if (!ag_window_can_close(window, FALSE)) {
old_id = (priv->saved_data) ? priv->saved_data->db_id : -1; 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); AgWindow *window = AG_WINDOW(user_data);
GError *err = NULL; GError *err = NULL;
recalculate_chart(window); ag_window_recalculate_chart(window);
ag_window_export(window, &err); ag_window_export(window, &err);
if (err) { if (err) {
@ -319,7 +319,7 @@ ag_window_export_svg(AgWindow *window, GError **err)
gint response; gint response;
AgWindowPrivate *priv = ag_window_get_instance_private(window); AgWindowPrivate *priv = ag_window_get_instance_private(window);
recalculate_chart(window); ag_window_recalculate_chart(window);
// We should never enter here, but who knows... // We should never enter here, but who knows...
if (priv->chart == NULL) { if (priv->chart == NULL) {
@ -838,56 +838,76 @@ chart_changed(AgChart *chart, AgWindow *window)
} }
static void static void
recalculate_chart(AgWindow *window) ag_window_recalculate_chart(AgWindow *window)
{ {
GsweTimestamp *timestamp; AgDbSave *edit_data,
GtkTextIter start_iter, *chart_data;
end_iter; AgWindowPrivate *priv = ag_window_get_instance_private(window);
gboolean south,
west;
GtkTreeIter house_system_iter; GtkTreeIter house_system_iter;
GsweHouseSystem house_system; GsweHouseSystem house_system;
gchar *note; GtkTextIter start_iter,
AgWindowPrivate *priv = ag_window_get_instance_private(window); end_iter;
gint year = gtk_spin_button_get_value_as_int( GsweTimestamp *timestamp;
GTK_SPIN_BUTTON(priv->year) gint db_id = (priv->saved_data) ? priv->saved_data->db_id : -1;
),
month = gtk_spin_button_get_value_as_int( south = gtk_toggle_button_get_active(
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(
GTK_TOGGLE_BUTTON(priv->south_lat) GTK_TOGGLE_BUTTON(priv->south_lat)
), );
west = gtk_toggle_button_get_active(
west = gtk_toggle_button_get_active(
GTK_TOGGLE_BUTTON(priv->west_long) GTK_TOGGLE_BUTTON(priv->west_long)
); );
g_debug("Recalculating chart data"); edit_data = g_new0(AgDbSave, 1);
if (south) { edit_data->db_id = db_id;
latitude = 0 - latitude;
} 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) { 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( if (!gtk_combo_box_get_active_iter(
GTK_COMBO_BOX(priv->house_system), GTK_COMBO_BOX(priv->house_system),
&house_system_iter &house_system_iter
@ -901,17 +921,44 @@ recalculate_chart(AgWindow *window)
0, &house_system, 0, &house_system,
-1 -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! // TODO: Set timezone according to the city selected!
if (priv->chart == NULL) { if (priv->chart == NULL) {
timestamp = gswe_timestamp_new_from_gregorian_full( timestamp = gswe_timestamp_new_from_gregorian_full(
year, month, day, edit_data->year, edit_data->month, edit_data->day,
hour, minute, second, 0, edit_data->hour, edit_data->minute, edit_data->second, 0,
1.0 edit_data->timezone
); );
priv->chart = ag_chart_new_full( priv->chart = ag_chart_new_full(
timestamp, timestamp,
longitude, latitude, 380.0, edit_data->longitude, edit_data->latitude, edit_data->altitude,
house_system house_system
); );
g_signal_connect( g_signal_connect(
@ -926,22 +973,14 @@ recalculate_chart(AgWindow *window)
timestamp = gswe_moment_get_timestamp(GSWE_MOMENT(priv->chart)); timestamp = gswe_moment_get_timestamp(GSWE_MOMENT(priv->chart));
gswe_timestamp_set_gregorian_full( gswe_timestamp_set_gregorian_full(
timestamp, timestamp,
year, month, day, edit_data->year, edit_data->month, edit_data->day,
hour, minute, second, 0, edit_data->hour, edit_data->minute, edit_data->second, 0,
1.0, edit_data->timezone,
NULL NULL
); );
} }
ag_chart_set_name(priv->chart, gtk_entry_get_text(GTK_ENTRY(priv->name))); ag_db_save_data_free(edit_data);
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);
} }
void 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"); 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 // Note that priv->current_tab is actually the previously selected tab, not
// the real active one! // the real active one!
if (priv->current_tab == priv->tab_edit) { if (priv->current_tab == priv->tab_edit) {
recalculate_chart(window); ag_window_recalculate_chart(window);
} }
priv->current_tab = active_tab; priv->current_tab = active_tab;
@ -1321,6 +1356,11 @@ ag_window_class_init(AgWindowClass *klass)
AgWindow, AgWindow,
db_chart_data 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, name);
gtk_widget_class_bind_template_child_private(widget_class, AgWindow, year); gtk_widget_class_bind_template_child_private(widget_class, AgWindow, year);
gtk_widget_class_bind_template_child_private(widget_class, AgWindow, month); 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; 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 int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {

View File

@ -1,6 +1,8 @@
#ifndef __ASTROGNOME_H__ #ifndef __ASTROGNOME_H__
#define __ASTROGNOME_H__ #define __ASTROGNOME_H__
#include <swe-glib.h>
typedef struct { typedef struct {
gboolean version; gboolean version;
gboolean quit; gboolean quit;
@ -10,5 +12,8 @@ typedef struct {
extern GtkFileFilter *filter_all; extern GtkFileFilter *filter_all;
extern GtkFileFilter *filter_chart; 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__ */ #endif /* __ASTROGNOME_H__ */

View File

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