From 9d4b71f0734e1d7b52a0a645b657fed0bc166ce2 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 8 Oct 2011 16:48:04 +0200 Subject: [PATCH] Started function RptReport::new_from_gtktreeview. --- .gitignore | 1 + src/rptreport.c | 182 +++++++++++++++++++++++++++++++++- src/rptreport.h | 3 + tests/Makefile.am | 3 +- tests/rptreport_gtktreeview.c | 104 +++++++++++++++++++ 5 files changed, 290 insertions(+), 3 deletions(-) create mode 100644 tests/rptreport_gtktreeview.c diff --git a/.gitignore b/.gitignore index 419f8c1..d85e9ee 100644 --- a/.gitignore +++ b/.gitignore @@ -45,6 +45,7 @@ libtool stamp-h1 tests/test*.png tests/*.pdf +tests/rptreport_gtktreeview tests/test_report.rptr tests/test_rptprint tests/test_rptreport diff --git a/src/rptreport.c b/src/rptreport.c index fd22bad..be3afab 100644 --- a/src/rptreport.c +++ b/src/rptreport.c @@ -520,6 +520,168 @@ RptReport return rpt_report; } +/** + * rpt_report_new_from_gtktreeview: + * @view: + * @title: + * + * Returns: the newly created #RptReport object. + */ +RptReport +*rpt_report_new_from_gtktreeview (GtkTreeView *view, + const gchar *title) +{ + RptReport *ret; + + GList *columns; + GHashTable *columns_names; + + RptSize *page_size; + + RptPoint *point; + RptSize *size; + RptObject *obj; + + guint x; + GtkTreeViewColumn *col; + gint col_width; + const gchar *col_title; + gchar *field_name; + + guint idx; + + g_return_val_if_fail (GTK_IS_TREE_VIEW (view), NULL); + + ret = rpt_report_new (); + + columns = gtk_tree_view_get_columns (view); + if (columns == NULL) return NULL; + + g_object_set (G_OBJECT (ret), "unit-length", RPT_UNIT_MILLIMETRE, NULL); + + page_size = rpt_common_rptsize_new_with_values (297, 210); + rpt_report_set_page_size (ret, *page_size); + + rpt_report_set_page_margins (ret, 10, 10, 10, 10); + + rpt_report_set_section_height (ret, RPTREPORT_SECTION_PAGE_HEADER, 30); + rpt_report_set_page_header_first_last_page (ret, TRUE, TRUE); + + rpt_report_set_section_height (ret, RPTREPORT_SECTION_BODY, 30); + + if (title != NULL) + { + point = rpt_common_rptpoint_new_with_values (0, 0); + obj = rpt_obj_text_new ("title", *point); + + size = rpt_common_rptsize_new_with_values (page_size->width - 20, 10); + + g_object_set (obj, + "source", title, + "size", size, + NULL); + + g_free (point); + g_free (size); + + rpt_report_add_object_to_section (ret, obj, RPTREPORT_SECTION_PAGE_HEADER); + } + + columns_names = g_hash_table_new (g_str_hash, g_str_equal); + + x = 0; + idx = 0; + while (columns != NULL) + { + col = (GtkTreeViewColumn *)columns->data; + + col_title = g_strdup_printf ("\"%s\"", gtk_tree_view_column_get_title (col)); + col_width = gtk_tree_view_column_get_width (col) / 96 * 25.4; + + point = rpt_common_rptpoint_new_with_values (x, 20); + size = rpt_common_rptsize_new_with_values (col_width, 10); + + obj = rpt_obj_text_new (g_strdup_printf ("title_%d", idx), *point); + + g_object_set (obj, + "source", col_title, + "size", size, + NULL); + + rpt_report_add_object_to_section (ret, obj, RPTREPORT_SECTION_PAGE_HEADER); + + field_name = g_strdup_printf ("field_%d", idx); + obj = rpt_obj_text_new (field_name, *point); + + g_object_set (obj, + "source", g_strdup_printf ("[%s]", field_name), + "size", size, + NULL); + + rpt_report_add_object_to_section (ret, obj, RPTREPORT_SECTION_BODY); + + g_free (point); + g_free (size); + + g_hash_table_insert (columns_names, field_name, g_strdup_printf ("%d", idx)); + + x += col_width + 5; + idx++; + + columns = g_list_next (columns); + } + + point = rpt_common_rptpoint_new_with_values (0, 10); + obj = rpt_obj_line_new ("line1", *point); + + size = rpt_common_rptsize_new_with_values (page_size->width - 20, 0); + + g_object_set (obj, + "size", size, + NULL); + + g_free (point); + g_free (size); + + rpt_report_add_object_to_section (ret, obj, RPTREPORT_SECTION_PAGE_HEADER); + + rpt_report_set_section_height (ret, RPTREPORT_SECTION_PAGE_FOOTER, 15); + + point = rpt_common_rptpoint_new_with_values (0, 0); + obj = rpt_obj_line_new ("line2", *point); + + size = rpt_common_rptsize_new_with_values (page_size->width - 20, 0); + + g_object_set (obj, + "size", size, + NULL); + + g_free (point); + g_free (size); + + rpt_report_add_object_to_section (ret, obj, RPTREPORT_SECTION_PAGE_FOOTER); + rpt_report_set_page_footer_first_last_page (ret, TRUE, TRUE); + + point = rpt_common_rptpoint_new_with_values (0, 0); + obj = rpt_obj_text_new ("pages", *point); + + size = rpt_common_rptsize_new_with_values (page_size->width - 20, 10); + + g_object_set (obj, + "source", "\"Page \" & @Page & \" of \" & @Pages", + "size", size, + NULL); + + g_free (point); + g_free (size); + + rpt_report_add_object_to_section (ret, obj, RPTREPORT_SECTION_PAGE_FOOTER); + + rpt_report_set_database_as_gtktreemodel (ret, gtk_tree_view_get_model (view), columns_names); + + return ret; +} + /** * rpt_report_set_output_type: * @rpt_report: @@ -1794,18 +1956,34 @@ rpt_report_add_object_to_section (RptReport *rpt_report, RptObject *rpt_object, switch (section) { case RPTREPORT_SECTION_REPORT_HEADER: + if (priv->report_header == NULL) + { + rpt_report_section_create (rpt_report, RPTREPORT_SECTION_REPORT_HEADER); + } priv->report_header->objects = g_list_append (priv->report_header->objects, rpt_object);; break; case RPTREPORT_SECTION_REPORT_FOOTER: + if (priv->report_footer == NULL) + { + rpt_report_section_create (rpt_report, RPTREPORT_SECTION_REPORT_FOOTER); + } priv->report_footer->objects = g_list_append (priv->report_footer->objects, rpt_object);; break; case RPTREPORT_SECTION_PAGE_HEADER: + if (priv->page_header == NULL) + { + rpt_report_section_create (rpt_report, RPTREPORT_SECTION_PAGE_HEADER); + } priv->page_header->objects = g_list_append (priv->page_header->objects, rpt_object);; break; case RPTREPORT_SECTION_PAGE_FOOTER: + if (priv->page_footer == NULL) + { + rpt_report_section_create (rpt_report, RPTREPORT_SECTION_PAGE_FOOTER); + } priv->page_footer->objects = g_list_append (priv->page_footer->objects, rpt_object);; break; @@ -2694,7 +2872,7 @@ gchar { g_signal_emit (rpt_report, klass->field_request_signal_id, 0, field, - NULL, NULL, + NULL, -1, priv->db->treemodel, priv->cur_iter, &ret); } @@ -2702,7 +2880,7 @@ gchar else { g_signal_emit (rpt_report, klass->field_request_signal_id, - 0, field, NULL, -1, &ret); + 0, field, NULL, -1, NULL, NULL, &ret); } if (ret != NULL) { diff --git a/src/rptreport.h b/src/rptreport.h index 0f38a3a..681f200 100644 --- a/src/rptreport.h +++ b/src/rptreport.h @@ -71,6 +71,9 @@ RptReport *rpt_report_new (void); RptReport *rpt_report_new_from_xml (xmlDoc *xdoc); RptReport *rpt_report_new_from_file (const gchar *filename); +RptReport *rpt_report_new_from_gtktreeview (GtkTreeView *view, + const gchar *title); + void rpt_report_set_output_type (RptReport *rpt_report, eRptOutputType output_type); void rpt_report_set_output_filename (RptReport *rpt_report, const gchar *output_filename); diff --git a/tests/Makefile.am b/tests/Makefile.am index 6f43e57..1dc2951 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -9,7 +9,8 @@ noinst_PROGRAMS = \ test_rptreport \ test_rptprint \ test_rptreport_creation \ - test_rptreport_liststore + test_rptreport_liststore \ + rptreport_gtktreeview LDADD = $(libreptool) diff --git a/tests/rptreport_gtktreeview.c b/tests/rptreport_gtktreeview.c new file mode 100644 index 0000000..b539411 --- /dev/null +++ b/tests/rptreport_gtktreeview.c @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2011 Andrea Zagli + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA + */ + +#include +#include + +enum +{ + TITLE_COLUMN, + AUTHOR_COLUMN, + CHECKED_COLUMN, + N_COLUMNS +}; + +int +main (int argc, char **argv) +{ + RptReport *rptr; + RptPrint *rptp; + + gtk_init (&argc, &argv); + + GtkListStore *store = gtk_list_store_new (N_COLUMNS, /* Total number of columns */ + G_TYPE_STRING, /* Book title */ + G_TYPE_STRING, /* Author */ + G_TYPE_BOOLEAN); /* Is checked out? */ + + GtkTreeIter iter; + + gtk_list_store_append (store, &iter); /* Acquire an iterator */ + + gtk_list_store_set (store, &iter, + TITLE_COLUMN, "The Principle of Reason", + AUTHOR_COLUMN, "Martin Heidegger", + CHECKED_COLUMN, FALSE, + -1); + + GtkWidget *tree; + + tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store)); + + GtkCellRenderer *renderer; + GtkTreeViewColumn *column; + + renderer = gtk_cell_renderer_text_new (); + column = gtk_tree_view_column_new_with_attributes ("Book's title", + renderer, + "text", TITLE_COLUMN, + NULL); + gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column); + + renderer = gtk_cell_renderer_text_new (); + column = gtk_tree_view_column_new_with_attributes ("Author", + renderer, + "text", AUTHOR_COLUMN, + NULL); + gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column); + + GtkWidget *w = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_default_size (GTK_WINDOW (w), 500, 400); + + gtk_container_add (GTK_CONTAINER (w), tree); + + gtk_widget_show_all (w); + + rptr = rpt_report_new_from_gtktreeview (GTK_TREE_VIEW (tree), "\"Report's Title\""); + + if (rptr != NULL) + { + xmlDoc *report = rpt_report_get_xml (rptr); + xmlSaveFormatFile ("test_report.rpt", report, 2); + + xmlDoc *rptprint = rpt_report_get_xml_rptprint (rptr); + xmlSaveFormatFile ("test_report.rptr", rptprint, 2); + + rptp = rpt_print_new_from_xml (rptprint); + if (rptp != NULL) + { + g_object_set (G_OBJECT (rptp), "path-relatives-to", "..", NULL); + rpt_print_set_output_type (rptp, RPT_OUTPUT_PDF); + rpt_print_set_output_filename (rptp, "rptreport.pdf"); + rpt_print_print (rptp, NULL); + } + } + + gtk_main (); + + return 0; +} -- 2.49.0