From 401fdbece249e116743f7fe027ad5118e5b4cb74 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Wed, 25 Aug 2010 11:16:21 +0200 Subject: [PATCH] Gestione vie. --- data/territorio/form/Makefile.am | 3 +- data/territorio/form/via.form | 40 +++ data/territorio/gui/territorio.gui | 351 +++++++++++++++++++++++ src/Makefile.am | 6 +- src/main.c | 14 + src/via.c | 376 ++++++++++++++++++++++++ src/via.h | 66 +++++ src/vie.c | 445 +++++++++++++++++++++++++++++ src/vie.h | 66 +++++ 9 files changed, 1365 insertions(+), 2 deletions(-) create mode 100644 data/territorio/form/via.form create mode 100644 src/via.c create mode 100644 src/via.h create mode 100644 src/vie.c create mode 100644 src/vie.h diff --git a/data/territorio/form/Makefile.am b/data/territorio/form/Makefile.am index c051a3a..0c41989 100644 --- a/data/territorio/form/Makefile.am +++ b/data/territorio/form/Makefile.am @@ -6,6 +6,7 @@ form_DATA = \ provincia.form \ regione.form \ stato.form \ - tipo_aggregazione_territoriale.form + tipo_aggregazione_territoriale.form \ + via.form EXTRA_DIST = $(form_DATA) diff --git a/data/territorio/form/via.form b/data/territorio/form/via.form new file mode 100644 index 0000000..ca05d4e --- /dev/null +++ b/data/territorio/form/via.form @@ -0,0 +1,40 @@ + + + + vie
+ + + + t + t + + + + + + + + + + + + + + f + f + + + + + + entry20 + + SELECT nome FROM comuni WHERE id = ##label46::gint + + + + t + + + +
diff --git a/data/territorio/gui/territorio.gui b/data/territorio/gui/territorio.gui index 4aa5fd6..78add34 100644 --- a/data/territorio/gui/territorio.gui +++ b/data/territorio/gui/territorio.gui @@ -81,6 +81,7 @@ True _Vie True + @@ -249,6 +250,14 @@ + + + + + + + + Stati - Territorio True @@ -2177,4 +2186,346 @@ + + Vie - Territorio + True + center-on-parent + 550 + 400 + True + + + True + 5 + vertical + 5 + + + True + <b>Vie</b> + True + + + False + 0 + + + + + True + True + automatic + automatic + etched-in + + + True + True + lstore_vie + False + 0 + + + Nome + + + + 1 + + + + + + + + + 1 + + + + + True + 5 + end + + + gtk-new + True + True + True + True + + + False + False + 0 + + + + + gtk-edit + True + True + True + True + + + False + False + 1 + + + + + gtk-delete + True + True + True + True + + + False + False + 2 + + + + + gtk-cancel + True + True + True + True + + + False + False + 3 + + + + + gtk-ok + True + True + True + True + + + False + False + 4 + + + + + False + 2 + + + + + + + Via - Territorio + True + center-on-parent + True + + + True + 5 + vertical + 5 + + + True + 4 + 2 + 5 + 5 + + + True + 0 + ID + + + GTK_FILL + GTK_FILL + + + + + True + 0 + lbl_id + + + 1 + 2 + GTK_FILL + + + + + True + 0 + Nome + + + 1 + 2 + GTK_FILL + GTK_FILL + + + + + True + True + 255 + + + + 1 + 2 + 1 + 2 + GTK_FILL + + + + + True + 0 + CAP + + + 2 + 3 + GTK_FILL + GTK_FILL + + + + + True + True + 5 + + + + 1 + 2 + 2 + 3 + GTK_FILL + + + + + True + 0 + Comune + + + 3 + 4 + GTK_FILL + GTK_FILL + + + + + True + 5 + + + True + True + False + + + + 0 + + + + + ... + True + True + True + + + False + 1 + + + + + lbl_id_comuni + + + False + 2 + + + + + 1 + 2 + 3 + 4 + GTK_FILL + + + + + 0 + + + + + True + 5 + end + + + gtk-cancel + True + True + True + True + + + False + False + 0 + + + + + gtk-save + True + True + True + True + + + False + False + 1 + + + + + False + 1 + + + + + diff --git a/src/Makefile.am b/src/Makefile.am index 6ea9f91..0fa55d7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -36,4 +36,8 @@ territorio_SOURCES = \ tipiaggregazioneterritoriale.c \ tipiaggregazioneterritoriale.h \ tipoaggregazioneterritoriale.c \ - tipoaggregazioneterritoriale.h \ No newline at end of file + tipoaggregazioneterritoriale.h \ + via.c \ + via.h \ + vie.c \ + vie.h \ No newline at end of file diff --git a/src/main.c b/src/main.c index 9d5ff1c..94ccf7d 100644 --- a/src/main.c +++ b/src/main.c @@ -34,6 +34,7 @@ #include "regioni.h" #include "stati.h" #include "tipiaggregazioneterritoriale.h" +#include "vie.h" static Commons *commons; @@ -108,6 +109,19 @@ on_mnu_view_comuni_activate (GtkMenuItem *menuitem, main_set_vbx_body_child (vbx); } +G_MODULE_EXPORT void +on_mnu_view_vie_activate (GtkMenuItem *menuitem, + gpointer user_data) +{ + GtkWidget *vbx; + + Vie *m = vie_new (commons, FALSE); + + vbx = vie_get_widget (m); + + main_set_vbx_body_child (vbx); +} + G_MODULE_EXPORT void on_mnu_tables_tipi_aggregazione_territoriale_activate (GtkMenuItem *menuitem, gpointer user_data) diff --git a/src/via.c b/src/via.c new file mode 100644 index 0000000..6bef73e --- /dev/null +++ b/src/via.c @@ -0,0 +1,376 @@ +/* + * Copyright (C) 2010 Andrea Zagli + * + * 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 + +#include "via.h" +#include "comuni.h" + +static void via_class_init (ViaClass *klass); +static void via_init (Via *via); + +static void via_carica (Via *via); +static void via_salva (Via *via); + +static gboolean via_conferma_chiusura (Via *via); + +static void via_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void via_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void via_on_comune_selezionato (gpointer instance, guint id, gpointer user_data); + +static gboolean via_on_w_via_delete_event (GtkWidget *widget, + GdkEvent *event, + gpointer user_data); + +static void via_on_btn_comune_clicked (GtkButton *button, + gpointer user_data); + +static void via_on_btn_annulla_clicked (GtkButton *button, + gpointer user_data); +static void via_on_btn_salva_clicked (GtkButton *button, + gpointer user_data); + +#define VIA_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_VIA, ViaPrivate)) + +enum +{ + LBL_ID +}; + +typedef struct _ViaPrivate ViaPrivate; +struct _ViaPrivate + { + Commons *commons; + + GtkForm *form; + + GtkWidget *w; + + GObject **objects; + + gint id; + }; + +G_DEFINE_TYPE (Via, via, G_TYPE_OBJECT) + +static void +via_class_init (ViaClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (object_class, sizeof (ViaPrivate)); + + object_class->set_property = via_set_property; + object_class->get_property = via_get_property; + + /** + * Via::aggiornato: + * @via: + * + */ + klass->aggiornato_signal_id = g_signal_new ("aggiornato", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST, + 0, + NULL, + NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, + 0); +} + +static void +via_init (Via *via) +{ + ViaPrivate *priv = VIA_GET_PRIVATE (via); +} + +/** + * via_new: + * @commons: + * @id: + * + * Returns: the newly created #Via object. + */ +Via +*via_new (Commons *commons, gint id) +{ + GError *error; + + Via *a = VIA (g_object_new (via_get_type (), NULL)); + + ViaPrivate *priv = VIA_GET_PRIVATE (a); + + priv->commons = commons; + + error = NULL; + gtk_builder_add_objects_from_file (priv->commons->gtkbuilder, priv->commons->guifile, + g_strsplit (g_strconcat ("w_via", NULL), "|", -1), + &error); + if (error != NULL) + { + g_warning ("Errore: %s.", error->message); + return NULL; + } + + priv->form = gtk_form_new (); + g_object_set (G_OBJECT (priv->form), "gdaex", priv->commons->gdaex, NULL); + gtk_form_load_from_file (priv->form, g_build_filename (priv->commons->formdir, "via.form", NULL), priv->commons->gtkbuilder); + + priv->w = GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "w_via")); + + priv->objects = gtk_form_get_objects_by_name (priv->form, + "label41", + NULL); + + g_signal_connect (priv->w, + "delete-event", G_CALLBACK (via_on_w_via_delete_event), (gpointer *)a); + + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button50"), + "clicked", G_CALLBACK (via_on_btn_comune_clicked), (gpointer *)a); + + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button51"), + "clicked", G_CALLBACK (via_on_btn_annulla_clicked), (gpointer *)a); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button52"), + "clicked", G_CALLBACK (via_on_btn_salva_clicked), (gpointer *)a); + + priv->id = id; + + if (priv->id == 0) + { + gtk_label_set_text (GTK_LABEL (priv->objects[LBL_ID]), ""); + + gtk_form_set_as_origin (priv->form); + } + else + { + gtk_label_set_text (GTK_LABEL (priv->objects[LBL_ID]), g_strdup_printf ("%d", priv->id)); + via_carica (a); + } + + return a; +} + +/** + * via_get_widget: + * @via: + * + */ +GtkWidget +*via_get_widget (Via *via) +{ + ViaPrivate *priv = VIA_GET_PRIVATE (via); + + return priv->w; +} + +/* PRIVATE */ +static void +via_carica (Via *via) +{ + ViaPrivate *priv = VIA_GET_PRIVATE (via); + + if (gtk_form_fill_from_table (priv->form)) + { + } +} + +static void +via_salva (Via *via) +{ + GError *error = NULL; + gchar *sql; + GtkWidget *dialog; + + GDate *da; + GDate *a; + + ViaClass *klass = VIA_GET_CLASS (via); + + ViaPrivate *priv = VIA_GET_PRIVATE (via); + + if (!gtk_form_check (priv->form, (priv->id != 0), NULL, TRUE, priv->w, TRUE)) + { + return; + } + + if (priv->id == 0) + { + sql = gtk_form_get_sql (priv->form, GTK_FORM_SQL_INSERT); + } + else + { + sql = gtk_form_get_sql (priv->form, GTK_FORM_SQL_UPDATE); + } + + if (gdaex_execute (priv->commons->gdaex, sql) == 1) + { + g_signal_emit (via, klass->aggiornato_signal_id, 0); + + gtk_form_set_as_origin (priv->form); + + if (priv->id == 0) + { + priv->id = strtol (gtk_label_get_text (GTK_LABEL (priv->objects[LBL_ID])), NULL, 10); + } + + dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_INFO, + GTK_BUTTONS_OK, + "Salvataggio eseguito con successo."); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + } + else + { + if (priv->id == 0) + { + gtk_label_set_text (GTK_LABEL (priv->objects[LBL_ID]), ""); + } + + dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Errore durante il salvataggio."); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + } +} + +static void +via_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) +{ + Via *via = VIA (object); + + ViaPrivate *priv = VIA_GET_PRIVATE (via); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +via_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) +{ + Via *via = VIA (object); + + ViaPrivate *priv = VIA_GET_PRIVATE (via); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static gboolean +via_conferma_chiusura (Via *via) +{ + GtkWidget *dialog; + + gboolean ret; + + ViaPrivate *priv = VIA_GET_PRIVATE (via); + + ret = TRUE; + if (gtk_form_is_changed (priv->form)) + { + dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_QUESTION, + GTK_BUTTONS_YES_NO, + "Sicuro di voler chiudere senza salvare?"); + if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_NO) + { + ret = FALSE; + } + gtk_widget_destroy (dialog); + } + + return ret; +} + +/* CALLBACK */ +static void +via_on_comune_selezionato (gpointer instance, guint id, gpointer user_data) +{ + Via *f = (Via *)user_data; + ViaPrivate *priv = VIA_GET_PRIVATE (f); + + gtk_form_widget_set_value_stringify (gtk_form_get_form_widget_from_name (priv->form, "label46"), g_strdup_printf ("%d", id)); +} + +static gboolean +via_on_w_via_delete_event (GtkWidget *widget, + GdkEvent *event, + gpointer user_data) +{ + return !via_conferma_chiusura ((Via *)user_data); +} + +static void +via_on_btn_comune_clicked (GtkButton *button, + gpointer user_data) +{ + GtkWidget *w; + + Via *rf = (Via *)user_data; + + ViaPrivate *priv = VIA_GET_PRIVATE (rf); + + Comuni *c = comuni_new (priv->commons, TRUE); + + g_signal_connect (G_OBJECT (c), "selezionato", + G_CALLBACK (via_on_comune_selezionato), user_data); + + w = comuni_get_widget (c); + gtk_window_set_transient_for (GTK_WINDOW (w), GTK_WINDOW (priv->w)); + gtk_widget_show (w); +} + +static void +via_on_btn_annulla_clicked (GtkButton *button, + gpointer user_data) +{ + Via *via = (Via *)user_data; + + ViaPrivate *priv = VIA_GET_PRIVATE (via); + + if (via_conferma_chiusura (via)) gtk_widget_destroy (priv->w); +} + +static void +via_on_btn_salva_clicked (GtkButton *button, + gpointer user_data) +{ + via_salva ((Via *)user_data); +} diff --git a/src/via.h b/src/via.h new file mode 100644 index 0000000..5bc24e5 --- /dev/null +++ b/src/via.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2010 Andrea Zagli + * + * 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 + * + */ + +#ifndef __VIA_H__ +#define __VIA_H__ + +#include +#include + +#include +#include + +#include "commons.h" + +G_BEGIN_DECLS + + +#define TYPE_VIA (via_get_type ()) +#define VIA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_VIA, Via)) +#define VIA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_VIA, ViaClass)) +#define IS_VIA(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_VIA)) +#define IS_VIA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_VIA)) +#define VIA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_VIA, ViaClass)) + + +typedef struct _Via Via; +typedef struct _ViaClass ViaClass; + +struct _Via + { + GObject parent; + }; + +struct _ViaClass + { + GObjectClass parent_class; + + guint aggiornato_signal_id; + }; + +GType via_get_type (void) G_GNUC_CONST; + +Via *via_new (Commons *commons, gint id); + +GtkWidget *via_get_widget (Via *via); + + +G_END_DECLS + +#endif /* __VIA_H__ */ diff --git a/src/vie.c b/src/vie.c new file mode 100644 index 0000000..2a5b90f --- /dev/null +++ b/src/vie.c @@ -0,0 +1,445 @@ +/* + * Copyright (C) 2010 Andrea Zagli + * + * 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 "vie.h" +#include "via.h" + +static void vie_class_init (VieClass *klass); +static void vie_init (Vie *vie); + +static void vie_carica (Vie *vie); +static void vie_modifica (Vie *vie); + +static void vie_selezionato (Vie *vie); +static void vie_on_via_aggiornato (gpointer instance, gpointer user_data); + +static void vie_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void vie_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void vie_on_btn_nuovo_clicked (GtkButton *button, + gpointer user_data); +static void vie_on_btn_modifica_clicked (GtkButton *button, + gpointer user_data); +static void vie_on_btn_elimina_clicked (GtkButton *button, + gpointer user_data); +static void vie_on_trv_vie_row_activated (GtkTreeView *tree_view, + GtkTreePath *tree_path, + GtkTreeViewColumn *column, + gpointer user_data); + +static void vie_on_btn_annulla_clicked (GtkButton *button, + gpointer user_data); +static void vie_on_btn_seleziona_clicked (GtkButton *button, + gpointer user_data); + +#define VIE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_VIE, ViePrivate)) + +enum +{ + COL_ID, + COL_NOME +}; + +typedef struct _ViePrivate ViePrivate; +struct _ViePrivate + { + Commons *commons; + + GtkWidget *widget; + GtkTreeSelection *selection; + GtkListStore *lstore_vie; + + gboolean selezione; + GtkWindow *wtransient; + }; + +G_DEFINE_TYPE (Vie, vie, G_TYPE_OBJECT) + +static void +vie_class_init (VieClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (object_class, sizeof (ViePrivate)); + + object_class->set_property = vie_set_property; + object_class->get_property = vie_get_property; + + /** + * Vie::selezionato: + * @vie: + * + */ + klass->selezionato_signal_id = g_signal_new ("selezionato", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST, + 0, + NULL, + NULL, + g_cclosure_marshal_VOID__UINT, + G_TYPE_NONE, + 1, G_TYPE_UINT); +} + +static void +vie_init (Vie *vie) +{ + ViePrivate *priv = VIE_GET_PRIVATE (vie); +} + +/** + * vie_new: + * @commons: + * @selezione: + * + * Returns: the newly created #Vie object. + */ +Vie +*vie_new (Commons *commons, gboolean selezione) +{ + GError *error; + + Vie *a = VIE (g_object_new (vie_get_type (), NULL)); + + ViePrivate *priv = VIE_GET_PRIVATE (a); + + priv->commons = commons; + priv->selezione = selezione; + + error = NULL; + gtk_builder_add_objects_from_file (priv->commons->gtkbuilder, priv->commons->guifile, + g_strsplit (g_strconcat ("lstore_vie|", + (selezione ? "w_vie" : "vbox14"), + NULL), "|", -1), + &error); + if (error != NULL) + { + g_warning ("Errore: %s.", error->message); + return NULL; + } + + priv->widget = GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, (selezione ? "w_vie" : "vbox14"))); + priv->selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (gtk_builder_get_object (priv->commons->gtkbuilder, "treeview7"))); + priv->lstore_vie = GTK_LIST_STORE (gtk_builder_get_object (priv->commons->gtkbuilder, "lstore_vie")); + + if (priv->selezione) + { + priv->wtransient = GTK_WINDOW (priv->widget); + } + else + { + priv->wtransient = GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")); + } + + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button45"), + "clicked", G_CALLBACK (vie_on_btn_nuovo_clicked), (gpointer)a); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button46"), + "clicked", G_CALLBACK (vie_on_btn_modifica_clicked), (gpointer)a); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button47"), + "clicked", G_CALLBACK (vie_on_btn_elimina_clicked), (gpointer)a); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "treeview7"), + "row-activated", G_CALLBACK (vie_on_trv_vie_row_activated), (gpointer)a); + + if (priv->selezione) + { + gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "label39"))); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button48"), + "clicked", G_CALLBACK (vie_on_btn_annulla_clicked), (gpointer)a); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button49"), + "clicked", G_CALLBACK (vie_on_btn_seleziona_clicked), (gpointer)a); + } + else + { + gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "button48"))); + gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "button49"))); + } + + vie_carica (a); + + return a; +} + +/** + * vie_get_widget: + * @vie: + * + */ +GtkWidget +*vie_get_widget (Vie *vie) +{ + ViePrivate *priv = VIE_GET_PRIVATE (vie); + + return priv->widget; +} + +/* PRIVATE */ +static void +vie_carica (Vie *vie) +{ + GtkTreeIter iter; + GdaDataModel *dm; + + gint rows; + gint row; + + ViePrivate *priv = VIE_GET_PRIVATE (vie); + + gtk_list_store_clear (priv->lstore_vie); + + dm = gdaex_query (priv->commons->gdaex, + "SELECT c.id, c.nome" + " FROM vie AS c" + " WHERE c.status <> 'E'" + " ORDER BY c.nome"); + if (dm != NULL) + { + rows = gda_data_model_get_n_rows (dm); + for (row = 0; row < rows; row++) + { + gtk_list_store_append (priv->lstore_vie, &iter); + + gtk_list_store_set (priv->lstore_vie, &iter, + COL_ID, gdaex_data_model_get_field_value_integer_at (dm, row, "id"), + COL_NOME, gdaex_data_model_get_field_value_stringify_at (dm, row, "nome"), + -1); + } + + g_object_unref (dm); + } +} + +static void +vie_modifica (Vie *vie) +{ + GtkTreeIter iter; + guint id; + + ViePrivate *priv = VIE_GET_PRIVATE (vie); + + if (gtk_tree_selection_get_selected (priv->selection, NULL, &iter)) + { + GtkWidget *w; + + gtk_tree_model_get (GTK_TREE_MODEL (priv->lstore_vie), &iter, + COL_ID, &id, + -1); + + Via *c = via_new (priv->commons, id); + + g_signal_connect (G_OBJECT (c), "aggiornato", + G_CALLBACK (vie_on_via_aggiornato), (gpointer)vie); + + w = via_get_widget (c); + gtk_window_set_transient_for (GTK_WINDOW (w), priv->wtransient); + gtk_widget_show (w); + } + else + { + GtkWidget *dialog = gtk_message_dialog_new (priv->wtransient, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Occorre prima selezionare una via"); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + } +} + +static void +vie_selezionato (Vie *vie) +{ + GtkTreeIter iter; + guint *id; + + VieClass *klass = VIE_GET_CLASS (vie); + ViePrivate *priv = VIE_GET_PRIVATE (vie); + + if (gtk_tree_selection_get_selected (priv->selection, NULL, &iter)) + { + gtk_tree_model_get (GTK_TREE_MODEL (priv->lstore_vie), &iter, + COL_ID, &id, + -1); + + g_signal_emit (G_OBJECT (vie), klass->selezionato_signal_id, 0, id); + + gtk_widget_destroy (priv->widget); + g_object_unref (G_OBJECT (vie)); + } + else + { + GtkWidget *dialog = gtk_message_dialog_new (priv->wtransient, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Occorre prima selezionare una via"); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + } +} + +static void +vie_on_via_aggiornato (gpointer instance, gpointer user_data) +{ + vie_carica ((Vie *)user_data); +} + +static void +vie_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) +{ + Vie *vie = VIE (object); + + ViePrivate *priv = VIE_GET_PRIVATE (vie); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +vie_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) +{ + Vie *vie = VIE (object); + + ViePrivate *priv = VIE_GET_PRIVATE (vie); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +/* CALLBACK */ +static void +vie_on_btn_nuovo_clicked (GtkButton *button, + gpointer user_data) +{ + GtkWidget *w; + + Vie *vie = (Vie *)user_data; + ViePrivate *priv = VIE_GET_PRIVATE (vie); + + Via *c = via_new (priv->commons, 0); + + g_signal_connect (G_OBJECT (c), "aggiornato", + G_CALLBACK (vie_on_via_aggiornato), (gpointer)vie); + + w = via_get_widget (c); + gtk_window_set_transient_for (GTK_WINDOW (w), priv->wtransient); + gtk_widget_show (w); +} + +static void +vie_on_btn_modifica_clicked (GtkButton *button, + gpointer user_data) +{ + vie_modifica ((Vie *)user_data); +} + +static void +vie_on_btn_elimina_clicked (GtkButton *button, + gpointer user_data) +{ + GtkWidget *dialog; + gboolean risp; + + GtkTreeIter iter; + guint id; + + Vie *vie = (Vie *)user_data; + ViePrivate *priv = VIE_GET_PRIVATE (vie); + + if (gtk_tree_selection_get_selected (priv->selection, NULL, &iter)) + { + dialog = gtk_message_dialog_new (priv->wtransient, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_QUESTION, + GTK_BUTTONS_YES_NO, + "Sicuro di voler eliminare la via selezionata?"); + risp = gtk_dialog_run (GTK_DIALOG (dialog)); + if (risp == GTK_RESPONSE_YES) + { + gtk_tree_model_get (GTK_TREE_MODEL (priv->lstore_vie), &iter, + COL_ID, &id, + -1); + + gdaex_execute (priv->commons->gdaex, + g_strdup_printf ("UPDATE vie SET status = 'E' WHERE id = %d", id)); + + vie_carica (vie); + } + gtk_widget_destroy (dialog); + } + else + { + dialog = gtk_message_dialog_new (priv->wtransient, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Occorre prima selezionare una via"); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + } +} + +static void +vie_on_trv_vie_row_activated (GtkTreeView *tree_view, + GtkTreePath *tree_path, + GtkTreeViewColumn *column, + gpointer user_data) +{ + ViePrivate *priv = VIE_GET_PRIVATE ((Vie *)user_data); + + if (priv->selezione) + { + vie_selezionato ((Vie *)user_data); + } + else + { + vie_modifica ((Vie *)user_data); + } +} + + +static void +vie_on_btn_annulla_clicked (GtkButton *button, + gpointer user_data) +{ + ViePrivate *priv = VIE_GET_PRIVATE ((Vie *)user_data); + + gtk_widget_destroy (priv->widget); + g_object_unref (G_OBJECT (user_data)); +} + +static void +vie_on_btn_seleziona_clicked (GtkButton *button, + gpointer user_data) +{ + vie_selezionato ((Vie *)user_data); +} diff --git a/src/vie.h b/src/vie.h new file mode 100644 index 0000000..359b755 --- /dev/null +++ b/src/vie.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2010 Andrea Zagli + * + * 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 + * + */ + +#ifndef __VIE_H__ +#define __VIE_H__ + +#include +#include + +#include +#include + +#include "commons.h" + +G_BEGIN_DECLS + + +#define TYPE_VIE (vie_get_type ()) +#define VIE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_VIE, Vie)) +#define VIE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_VIE, VieClass)) +#define IS_VIE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_VIE)) +#define IS_VIE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_VIE)) +#define VIE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_VIE, VieClass)) + + +typedef struct _Vie Vie; +typedef struct _VieClass VieClass; + +struct _Vie + { + GObject parent; + }; + +struct _VieClass + { + GObjectClass parent_class; + + guint selezionato_signal_id; + }; + +GType vie_get_type (void) G_GNUC_CONST; + +Vie *vie_new (Commons *commons, gboolean selezione); + +GtkWidget *vie_get_widget (Vie *vie); + + +G_END_DECLS + +#endif /* __VIE_H__ */ -- 2.49.0