From c34ba055bc1a9c6264186be295ee045c4b4c0ce6 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 27 Jun 2014 16:52:52 +0200 Subject: [PATCH] Add SVG export functionality --- src/ag-chart.c | 12 ++++++++ src/ag-chart.h | 4 +++ src/ag-window.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++ src/astrognome.ui | 6 ++++ 4 files changed, 98 insertions(+) diff --git a/src/ag-chart.c b/src/ag-chart.c index e2c2dd0..4d98478 100644 --- a/src/ag-chart.c +++ b/src/ag-chart.c @@ -858,3 +858,15 @@ ag_chart_create_svg(AgChart *chart, gsize *length, GError **err) return save_content; } +void +ag_chart_export_svg_to_file(AgChart *chart, GFile *file, GError **err) +{ + gchar *svg; + gsize length; + + if ((svg = ag_chart_create_svg(chart, &length, err)) == NULL) { + return; + } + + g_file_replace_contents(file, (const gchar *)svg, length, NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, err); +} diff --git a/src/ag-chart.h b/src/ag-chart.h index 9724b56..758474f 100644 --- a/src/ag-chart.h +++ b/src/ag-chart.h @@ -43,6 +43,10 @@ void ag_chart_save_to_file(AgChart *chart, GFile *file, GError **err); +void ag_chart_export_svg_to_file(AgChart *chart, + GFile *file, + GError **err); + void ag_chart_set_name(AgChart *chart, const gchar *name); gchar *ag_chart_get_name(AgChart *chart); diff --git a/src/ag-window.c b/src/ag-window.c index 0f8c0b6..71a1d23 100644 --- a/src/ag-window.c +++ b/src/ag-window.c @@ -170,6 +170,81 @@ ag_window_save_as_action(GSimpleAction *action, GVariant *parameter, gpointer us // TODO: Check err! } +static void +ag_window_export_svg(AgWindow *window, GError **err) +{ + gchar *name; + gchar *file_name; + GtkWidget *fs; + gint response; + + recalculate_chart(window); + + // We should never enter here, but who knows... + if (window->priv->chart == NULL) { + GtkWidget *dialog; + + dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Chart cannot be calculated.")); + gtk_dialog_run(GTK_DIALOG(dialog)); + gtk_widget_destroy(dialog); + g_set_error(err, AG_WINDOW_ERROR, AG_WINDOW_ERROR_EMPTY_CHART, "Chart is empty"); + + return; + } + + name = ag_chart_get_name(window->priv->chart); + + if ((name == NULL) || (*name == 0)) { + GtkWidget *dialog; + + g_free(name); + + dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("You must enter a name before saving a chart.")); + gtk_dialog_run(GTK_DIALOG(dialog)); + gtk_widget_destroy(dialog); + g_set_error(err, AG_WINDOW_ERROR, AG_WINDOW_ERROR_NO_NAME, "No name specified"); + + return; + } + + file_name = g_strdup_printf("%s.svg", name); + g_free(name); + + fs = gtk_file_chooser_dialog_new(_("Export Chart as SVG"), + GTK_WINDOW(window), + GTK_FILE_CHOOSER_ACTION_SAVE, + _("_Cancel"), GTK_RESPONSE_CANCEL, + _("_Save"), GTK_RESPONSE_ACCEPT, + NULL); + gtk_dialog_set_default_response(GTK_DIALOG(fs), GTK_RESPONSE_ACCEPT); + gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(fs), FALSE); + gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(fs), TRUE); + gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(fs), file_name); + g_free(file_name); + + response = gtk_dialog_run(GTK_DIALOG(fs)); + gtk_widget_hide(fs); + + if (response == GTK_RESPONSE_ACCEPT) { + GFile *file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(fs)); + + ag_chart_export_svg_to_file(window->priv->chart, file, err); + } + + gtk_widget_destroy(fs); +} + +static void +ag_window_export_svg_action(GSimpleAction *action, GVariant *parameter, gpointer user_data) +{ + AgWindow *window = AG_WINDOW(user_data); + GError *err = NULL; + + ag_window_export_svg(window, &err); + + // TODO: Check err! +} + void ag_window_redraw_chart(AgWindow *window) { @@ -321,6 +396,7 @@ static GActionEntry win_entries[] = { { "close", ag_window_close_action, NULL, NULL, NULL }, { "save", ag_window_save_action, NULL, NULL, NULL }, { "save-as", ag_window_save_as_action, NULL, NULL, NULL }, + { "export-svg", ag_window_export_svg_action, NULL, NULL, NULL }, { "gear-menu", ag_window_gear_menu_action, NULL, "false", NULL }, { "change-tab", ag_window_change_tab_action, "s", "'edit'", NULL }, }; diff --git a/src/astrognome.ui b/src/astrognome.ui index e23ec2e..2989172 100644 --- a/src/astrognome.ui +++ b/src/astrognome.ui @@ -46,6 +46,12 @@ <Primary><Shift>s +
+ + Export as SVG + win.export-svg + +
Close