]> saetta.ns0.it Git - libzakgtkdecodergdaexqe/commitdiff
Implemented signal iwidget-init of GdaExQueryEditor and test.
authorAndrea Zagli <azagli@libero.it>
Mon, 11 Jan 2016 16:34:31 +0000 (17:34 +0100)
committerAndrea Zagli <azagli@libero.it>
Mon, 11 Jan 2016 16:34:31 +0000 (17:34 +0100)
.gitignore
Makefile.am
configure.ac
tests/Makefile.am [new file with mode: 0644]
tests/query_editor.c [new file with mode: 0644]
tests/query_editor.db [new file with mode: 0644]
tests/query_editor.xml [new file with mode: 0644]

index 8129d065c7f857c37bb7f0e653878e9baf5e5522..66893f04c6d9cdb1926fd48d27ba7692a46c77fe 100644 (file)
@@ -42,7 +42,7 @@ docs/reference/html/
 docs/reference/xml/
 libtool
 stamp-h1
-tests/test
+tests/query_editor
 POTFILES
 mkinstalldirs
 stamp-it
index c49d2ae4e7a371f1ff92d4028185e82062a3fab4..80444668d39bdfa84fa3aad2fd38e83221c5b330 100644 (file)
@@ -1,4 +1,4 @@
-SUBDIRS = src
+SUBDIRS = src tests
 
 ACLOCAL_AMFLAGS = -I m4
 
index 024979696d50416140e62d1a64abcfa0e80d4c62..f4abb8c8e081eff80b52b335f441b0fd08f12be5 100644 (file)
@@ -74,5 +74,6 @@ AC_CONFIG_FILES([
        libzakgtkdecodergdaexqe.pc
        Makefile
        src/Makefile
+       tests/Makefile
 ])
 AC_OUTPUT
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100644 (file)
index 0000000..5bad756
--- /dev/null
@@ -0,0 +1,15 @@
+LIBS = $(ZAKGTKDECODERGDAEXQE_LIBS) \
+       -export-dynamic
+
+AM_CPPFLAGS = $(ZAKGTKDECODERGDAEXQE_CFLAGS) \
+              -I$(top_srcdir)/src \
+              -DTESTSDIR="\"@abs_builddir@\""
+
+noinst_PROGRAMS = \
+                  query_editor
+
+LDADD = $(top_builddir)/src/libzakgtkdecodergdaexqe.la
+
+EXTRA_DIST = \
+             query_editor.xml \
+             query_editor.db
diff --git a/tests/query_editor.c b/tests/query_editor.c
new file mode 100644 (file)
index 0000000..1d9d8d6
--- /dev/null
@@ -0,0 +1,348 @@
+/*
+ * Copyright (C) 2016 Andrea Zagli <azagli@libero.it>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include <libgdaex/libgdaex.h>
+
+#include "decoderqe.h"
+
+GdaExQueryEditor *qe;
+
+gchar *xmlfile;
+
+GdaEx *gdaex;
+
+GtkWidget *w;
+
+GtkWidget *widget_qe;
+GtkWidget *btn_destroy;
+GtkWidget *btn_clean;
+GtkWidget *btn_get_sql;
+GtkWidget *btn_save_xml;
+GtkWidget *btn_load_xml;
+GtkWidget *btn_ok;
+
+GtkWidget *vbox;
+
+static void
+city_on_browse_clicked (gpointer instance, gpointer user_data)
+{
+       zak_gtk_decoder_set_key (ZAK_GTK_DECODER (instance), "1");
+}
+
+static gchar
+*city_on_decode (gpointer instance, gchar *key, gpointer user_data)
+{
+       return g_strdup_printf ("Decode from «%s»", key);
+}
+
+static void
+city_on_iwidget_init (gpointer instance, GdaExQueryEditorIWidget *iwidget, gchar *table_name, gchar *field_name, gpointer user_data)
+{
+       g_warning ("signal iwidget_init");
+
+       g_signal_connect (G_OBJECT (iwidget), "btn-browse-clicked",
+                                         G_CALLBACK (city_on_browse_clicked), NULL);
+       g_signal_connect (G_OBJECT (iwidget), "decode",
+                                         G_CALLBACK (city_on_decode), NULL);
+}
+
+static void
+do_query_editor ()
+{
+       GdaExQueryEditorField *field;
+
+       qe = gdaex_query_editor_new (gdaex);
+
+       g_signal_connect (G_OBJECT (qe), "iwidget-init",
+                                         G_CALLBACK (city_on_iwidget_init), NULL);
+
+       gdaex_query_editor_load_tables_from_file (qe, xmlfile, TRUE);
+
+       widget_qe = gdaex_query_editor_get_widget (qe);
+       gtk_box_pack_start (GTK_BOX (vbox), widget_qe, TRUE, TRUE, 5);
+}
+
+static void
+on_btn_destroy_clicked (GtkButton *button,
+                      gpointer user_data)
+{
+       if (widget_qe != NULL)
+               {
+                       g_object_unref (G_OBJECT (qe));
+                       widget_qe = NULL;
+               }
+       else
+               {
+                       do_query_editor ();
+               }
+}
+
+static void
+on_btn_clean_clicked (GtkButton *button,
+                      gpointer user_data)
+{
+       gdaex_query_editor_clean_choices (qe);
+}
+
+static void
+on_btn_save_xml_clicked (GtkButton *button,
+                      gpointer user_data)
+{
+       xmlDoc *doc;
+       xmlNode *node;
+       GtkWidget *dialog;
+
+       doc = xmlNewDoc ("1.0");
+       node = gdaex_query_editor_get_sql_as_xml (qe);
+       xmlDocSetRootElement (doc, node);
+
+       dialog = gtk_file_chooser_dialog_new ("Save xml to...",
+                                             GTK_WINDOW (w),
+                                             GTK_FILE_CHOOSER_ACTION_SAVE,
+                                             GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                                             GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
+                                             NULL);
+       if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
+               {
+                       gchar *filename;
+                       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
+                       xmlSaveFormatFile (filename, doc, 2);
+                       g_free (filename);
+               }
+       gtk_widget_destroy (dialog);
+}
+
+static void
+on_btn_load_xml_clicked (GtkButton *button,
+                      gpointer user_data)
+{
+       GtkWidget *dialog;
+       xmlDoc *doc;
+       xmlNode *node;
+
+       dialog = gtk_file_chooser_dialog_new ("Load xml from...",
+                                             GTK_WINDOW (w),
+                                             GTK_FILE_CHOOSER_ACTION_OPEN,
+                                             GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                                             GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
+                                             NULL);
+       if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
+               {
+                       gchar *filename;
+                       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
+                       doc = xmlParseFile (filename);
+                       if (doc != NULL)
+                               {
+                                       node = xmlDocGetRootElement (doc);
+                                       if (node != NULL)
+                                               {
+                                                       gdaex_query_editor_load_choices_from_xml (qe, node, TRUE);
+                                               }
+                               }
+                       g_free (filename);
+               }
+       gtk_widget_destroy (dialog);
+}
+
+static void
+on_btn_get_sql_clicked (GtkButton *button,
+                      gpointer user_data)
+{
+       xmlDoc *doc;
+       xmlNode *node;
+       xmlChar *buf;
+
+       gchar *sql;
+
+       gint size;
+
+       /* SQL */
+       sql = (gchar *)gdaex_query_editor_get_sql (qe);
+       sql = g_strjoinv ("%%", g_strsplit (sql, "%", -1));
+       GtkWidget *dialog = gtk_message_dialog_new (GTK_WINDOW (w),
+                                                   GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                   GTK_MESSAGE_INFO,
+                                                   GTK_BUTTONS_OK,
+                                                   sql);
+       gtk_window_set_title (GTK_WINDOW (dialog), "Sql");
+       gtk_dialog_run (GTK_DIALOG (dialog));
+       gtk_widget_destroy (dialog);
+
+       /* SQL SELECT */
+       sql = (gchar *)gdaex_query_editor_get_sql_select (qe);
+       sql = g_strjoinv ("%%", g_strsplit (sql, "%", -1));
+       dialog = gtk_message_dialog_new (GTK_WINDOW (w),
+                                        GTK_DIALOG_DESTROY_WITH_PARENT,
+                                        GTK_MESSAGE_INFO,
+                                        GTK_BUTTONS_OK,
+                                        sql);
+       gtk_window_set_title (GTK_WINDOW (dialog), "Sql Select");
+       gtk_dialog_run (GTK_DIALOG (dialog));
+       gtk_widget_destroy (dialog);
+       g_free (sql);
+
+       /* SQL FROM */
+       sql = (gchar *)gdaex_query_editor_get_sql_from (qe);
+       sql = g_strjoinv ("%%", g_strsplit (sql, "%", -1));
+       dialog = gtk_message_dialog_new (GTK_WINDOW (w),
+                                        GTK_DIALOG_DESTROY_WITH_PARENT,
+                                        GTK_MESSAGE_INFO,
+                                        GTK_BUTTONS_OK,
+                                        sql);
+       gtk_window_set_title (GTK_WINDOW (dialog), "Sql From");
+       gtk_dialog_run (GTK_DIALOG (dialog));
+       gtk_widget_destroy (dialog);
+       g_free (sql);
+
+       /* SQL WHERE */
+       sql = (gchar *)gdaex_query_editor_get_sql_where (qe);
+       sql = g_strjoinv ("%%", g_strsplit (sql, "%", -1));
+       dialog = gtk_message_dialog_new (GTK_WINDOW (w),
+                                        GTK_DIALOG_DESTROY_WITH_PARENT,
+                                        GTK_MESSAGE_INFO,
+                                        GTK_BUTTONS_OK,
+                                        sql);
+       gtk_window_set_title (GTK_WINDOW (dialog), "Sql Where");
+       gtk_dialog_run (GTK_DIALOG (dialog));
+       gtk_widget_destroy (dialog);
+       g_free (sql);
+
+       /* SQL ORDER */
+       sql = (gchar *)gdaex_query_editor_get_sql_order (qe);
+       sql = g_strjoinv ("%%", g_strsplit (sql, "%", -1));
+       dialog = gtk_message_dialog_new (GTK_WINDOW (w),
+                                        GTK_DIALOG_DESTROY_WITH_PARENT,
+                                        GTK_MESSAGE_INFO,
+                                        GTK_BUTTONS_OK,
+                                        sql);
+       gtk_window_set_title (GTK_WINDOW (dialog), "Sql Order By");
+       gtk_dialog_run (GTK_DIALOG (dialog));
+       gtk_widget_destroy (dialog);
+       g_free (sql);
+
+       /* XML */
+       doc = xmlNewDoc ("1.0");
+       node = gdaex_query_editor_get_sql_as_xml (qe);
+       xmlDocSetRootElement (doc, node);
+       xmlDocDumpMemory (doc, &buf, &size);
+       buf = g_strjoinv ("%%", g_strsplit (buf, "%", -1));
+
+       dialog = gtk_message_dialog_new (GTK_WINDOW (w),
+                                        GTK_DIALOG_DESTROY_WITH_PARENT,
+                                        GTK_MESSAGE_INFO,
+                                        GTK_BUTTONS_OK,
+                                        buf);
+       gtk_window_set_title (GTK_WINDOW (dialog), "Xml");
+       gtk_dialog_run (GTK_DIALOG (dialog));
+       gtk_widget_destroy (dialog);
+}
+
+int
+main (int argc, char *argv[])
+{
+       GError *error;
+
+       GOptionContext *context;
+
+       GtkWidget *hbtnbox;
+
+       xmlfile = NULL;
+
+       GOptionEntry entries[] =
+               {
+                       { "xml-file", 'x', 0, G_OPTION_ARG_FILENAME, &xmlfile, "Xml file", NULL },
+                       { NULL }
+               };
+
+       gtk_init (&argc, &argv);
+
+       gdaex = gdaex_new_from_string (g_strdup_printf ("SQLite://DB_DIR=%s;DB_NAME=query_editor.db", TESTSDIR));
+       if (gdaex == NULL)
+               {
+                       g_error ("Error on GdaEx initialization.");
+                       return 0;
+               }
+
+       error = NULL;
+       context = g_option_context_new ("tests");
+       g_option_context_add_main_entries (context, entries, "tests");
+       g_option_context_add_group (context, gdaex_get_option_group (gdaex));
+       g_option_context_parse (context, &argc, &argv, &error);
+       if (error != NULL)
+               {
+                       g_warning ("Error on command line parsing: %s", error->message);
+               }
+
+       if (xmlfile == NULL)
+               {
+                       g_error ("xml-file missing on command line.");
+               }
+
+       w = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+       gtk_window_set_default_size (GTK_WINDOW (w), 610, 400);
+       gtk_window_set_modal (GTK_WINDOW (w), TRUE);
+       gtk_window_set_position (GTK_WINDOW (w), GTK_WIN_POS_CENTER);
+       g_signal_connect (G_OBJECT (w), "delete-event",
+                         gtk_main_quit, NULL);
+
+       vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
+       gtk_container_add (GTK_CONTAINER (w), vbox);
+
+       hbtnbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
+       gtk_button_box_set_layout (GTK_BUTTON_BOX (hbtnbox), GTK_BUTTONBOX_END);
+       gtk_box_set_spacing (GTK_BOX (hbtnbox), 5);
+       gtk_box_pack_start (GTK_BOX (vbox), hbtnbox, FALSE, FALSE, 5);
+
+       btn_destroy = gtk_button_new_with_mnemonic ("_Destroy");
+       gtk_box_pack_start (GTK_BOX (hbtnbox), btn_destroy, TRUE, TRUE, 5);
+       g_signal_connect (G_OBJECT (btn_destroy), "clicked",
+                         G_CALLBACK (on_btn_destroy_clicked), NULL);
+
+       btn_clean = gtk_button_new_from_stock ("gtk-clear");
+       gtk_box_pack_start (GTK_BOX (hbtnbox), btn_clean, TRUE, TRUE, 5);
+       g_signal_connect (G_OBJECT (btn_clean), "clicked",
+                         G_CALLBACK (on_btn_clean_clicked), NULL);
+
+       btn_get_sql = gtk_button_new_with_mnemonic ("Get _Sql");
+       gtk_box_pack_start (GTK_BOX (hbtnbox), btn_get_sql, TRUE, TRUE, 5);
+       g_signal_connect (G_OBJECT (btn_get_sql), "clicked",
+                         G_CALLBACK (on_btn_get_sql_clicked), NULL);
+
+       btn_save_xml = gtk_button_new_from_stock ("gtk-save");
+       gtk_box_pack_start (GTK_BOX (hbtnbox), btn_save_xml, TRUE, TRUE, 5);
+       g_signal_connect (G_OBJECT (btn_save_xml), "clicked",
+                         G_CALLBACK (on_btn_save_xml_clicked), NULL);
+
+       btn_load_xml = gtk_button_new_from_stock ("gtk-open");
+       gtk_box_pack_start (GTK_BOX (hbtnbox), btn_load_xml, TRUE, TRUE, 5);
+       g_signal_connect (G_OBJECT (btn_load_xml), "clicked",
+                         G_CALLBACK (on_btn_load_xml_clicked), NULL);
+
+       btn_ok = gtk_button_new_from_stock ("gtk-close");
+       gtk_box_pack_start (GTK_BOX (hbtnbox), btn_ok, TRUE, TRUE, 5);
+       g_signal_connect (G_OBJECT (btn_ok), "clicked",
+                         gtk_main_quit, NULL);
+
+       do_query_editor ();
+
+       gtk_widget_show_all (w);
+
+       gtk_main ();
+
+       return 0;
+}
diff --git a/tests/query_editor.db b/tests/query_editor.db
new file mode 100644 (file)
index 0000000..cd92e10
Binary files /dev/null and b/tests/query_editor.db differ
diff --git a/tests/query_editor.xml b/tests/query_editor.xml
new file mode 100644 (file)
index 0000000..e48e06b
--- /dev/null
@@ -0,0 +1,56 @@
+<gdaex_query_editor>
+
+       <tables>
+               <table>
+                       <name>clients</name>
+                       <name_visible>Clients</name_visible>
+               </table>
+       </tables>
+
+       <fields table="clients">
+               <field>
+                       <name>id</name>
+                       <name_visible>ID</name_visible>
+                       <type>integer</type>
+                       <always_showed>true</always_showed>
+                       <available_where_type>equal</available_where_type>
+               </field>
+               <field>
+                       <name>name</name>
+                       <name_visible>Name</name_visible>
+                       <description>The client's name</description>
+                       <type>text</type>
+                       <available_where_type>starts|contains|ends|istarts|icontains|iends</available_where_type>
+               </field>
+               <field>
+                       <name>surname</name>
+                       <name_visible>Surname</name_visible>
+                       <description>The client's surname</description>
+                       <type>text</type>
+                       <available_where_type>string|is_null</available_where_type>
+               </field>
+               <field>
+                       <name>age</name>
+                       <name_visible>Age</name_visible>
+                       <description>The client's age</description>
+                       <type>integer</type>
+                       <available_where_type>number</available_where_type>
+               </field>
+               <field>
+                       <name>datetime</name>
+                       <name_visible>DateTime</name_visible>
+                       <description>???</description>
+                       <type>datetime</type>
+                       <available_where_type>datetime|is_null</available_where_type>
+               </field>
+               <field>
+                       <name>id_cities</name>
+                       <name_visible>City</name_visible>
+                       <description>The client's city</description>
+                       <type>integer</type>
+                       <available_where_type>equal</available_where_type>
+                       <widget type="zak_gtk_decoder_gdaex_qe"></widget>
+               </field>
+       </fields>
+
+</gdaex_query_editor>