From d446c2cd222670a370242e1cd8b237aa259d375a Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 21 Feb 2016 16:41:04 +0100 Subject: [PATCH] Added and managed field "icona" in "categorie". --- data/bcity_be/gui/bcity_be.ui | 46 +++++++++++- doc/bcity_be/db/bcity.sql | 3 +- src/categoria.c | 136 +++++++++++++++++++++++++++++++++- 3 files changed, 182 insertions(+), 3 deletions(-) diff --git a/data/bcity_be/gui/bcity_be.ui b/data/bcity_be/gui/bcity_be.ui index eeefda3..658bdc7 100644 --- a/data/bcity_be/gui/bcity_be.ui +++ b/data/bcity_be/gui/bcity_be.ui @@ -115,7 +115,7 @@ 1 - 3 + 4 @@ -126,11 +126,55 @@ 0 0 + + 0 + 4 + + + + + True + False + Icona + 0 + 0 3 + + + True + True + True + + + True + False + gtk-missing-image + + + + + 1 + 3 + + + + + False + True + lbl_icona_percorso + + + 0 + 5 + + + + + True diff --git a/doc/bcity_be/db/bcity.sql b/doc/bcity_be/db/bcity.sql index 9d4efc7..bbe0831 100644 --- a/doc/bcity_be/db/bcity.sql +++ b/doc/bcity_be/db/bcity.sql @@ -26,7 +26,8 @@ CREATE TABLE categorie ( id_categorie integer DEFAULT 0, descrizione character varying(255) DEFAULT ''::character varying, status character varying(1) DEFAULT ''::character varying, - note text DEFAULT ''::text + note text DEFAULT ''::text, + icona oid ); diff --git a/src/categoria.c b/src/categoria.c index 4ee6212..70b7d3d 100644 --- a/src/categoria.c +++ b/src/categoria.c @@ -22,6 +22,7 @@ #endif #include +#include #include #include @@ -64,6 +65,9 @@ static gboolean categoria_on_w_categoria_delete_event (GtkWidget *widget, static void categoria_on_btn_categoria_browse_clicked (GtkButton *button, gpointer user_data); +static void categoria_on_btn_icona_clicked (GtkButton *button, + gpointer user_data); + static void categoria_on_btn_annulla_clicked (GtkButton *button, gpointer user_data); static void categoria_on_btn_salva_clicked (GtkButton *button, @@ -84,6 +88,7 @@ struct _CategoriaPrivate GtkWidget *w; GObject *lbl_id; + GObject *lbl_icona_percorso; gint id; }; @@ -160,6 +165,8 @@ Categoria priv->lbl_id = gtk_builder_get_object (priv->commons->gtkbuilder, "label43"); + priv->lbl_icona_percorso = gtk_builder_get_object (priv->commons->gtkbuilder, + "label5"); g_signal_connect (priv->w, "key-release-event", G_CALLBACK (categoria_on_key_release_event), (gpointer)a); @@ -171,6 +178,9 @@ Categoria g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "zakgtkdecoder4"), "decode", G_CALLBACK (categoria_on_categoria_decode), (gpointer)a); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button1"), + "clicked", G_CALLBACK (categoria_on_btn_icona_clicked), (gpointer)a); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button75"), "clicked", G_CALLBACK (categoria_on_btn_annulla_clicked), (gpointer)a); g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button78"), @@ -180,6 +190,8 @@ Categoria zak_form_form_clear (ZAK_FORM_FORM (priv->form)); + gtk_label_set_text (GTK_LABEL (priv->lbl_icona_percorso), ""); + priv->id = id; if (priv->id == 0) @@ -314,6 +326,32 @@ categoria_carica (Categoria *categoria) if (zak_form_form_load (ZAK_FORM_FORM (priv->form), ZAK_FORM_IPROVIDER (priv->form_provider))) { + /* carico l'icona nel pulsante */ + GError *error; + gchar *sql; + const gchar *percorso; + GdkPixbuf *pixbuf; + + sql = g_strdup_printf ("SELECT icona" + " FROM categorie" + " WHERE id = %d", + priv->id); + percorso = gdaex_get_blob (priv->commons->gdaex, sql, + NULL, "icona"); + error = NULL; + if (percorso != NULL) + { + pixbuf = gdk_pixbuf_new_from_file (percorso, &error); + gtk_image_set_from_pixbuf (GTK_IMAGE (gtk_builder_get_object (priv->commons->gtkbuilder, "image1")), pixbuf); + g_unlink (percorso); + } + else + { + gtk_image_set_from_icon_name (GTK_IMAGE (gtk_builder_get_object (priv->commons->gtkbuilder, "image1")), + "gtk-missing-image", + GTK_ICON_SIZE_BUTTON); + } + g_free (sql); } } @@ -325,6 +363,11 @@ categoria_salva (Categoria *categoria, gboolean chiudi) CategoriaClass *klass = CATEGORIA_GET_CLASS (categoria); CategoriaPrivate *priv = CATEGORIA_GET_PRIVATE (categoria); + gchar *sql; + gchar *percorso; + + gboolean success; + if (!zak_form_gtk_form_is_valid (priv->form, priv->w)) { return; @@ -348,7 +391,8 @@ categoria_salva (Categoria *categoria, gboolean chiudi) ret = zak_form_form_update (ZAK_FORM_FORM (priv->form), ZAK_FORM_IPROVIDER (priv->form_provider)); } - if (ret) + success = ret; + if (success) { g_signal_emit (categoria, klass->aggiornato_signal_id, 0); @@ -359,6 +403,30 @@ categoria_salva (Categoria *categoria, gboolean chiudi) priv->id = strtol (gtk_label_get_text (GTK_LABEL (priv->lbl_id)), NULL, 10); } + /* salvo il file nel db */ + percorso = g_strdup (gtk_label_get_text (GTK_LABEL (priv->lbl_icona_percorso))); + if (g_strcmp0 (percorso, "") != 0) + { + sql = g_strdup_printf ("UPDATE categorie" + " SET icona = ##/*name:'icona' type:'GdaBlob'*/" + " WHERE id = %d", + priv->id); + if (!gdaex_save_file_in_blob (priv->commons->gdaex, + sql, + "icona", + percorso)) + { + success = FALSE; + + g_free (sql); + } + } + + g_free (percorso); + } + + if (success) + { solipa_message_dialog (priv->w, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, @@ -513,6 +581,72 @@ categoria_on_btn_categoria_browse_clicked (GtkButton *button, categorie_show (c, GTK_WINDOW (priv->w)); } +static void +update_preview_cb (GtkFileChooser *file_chooser, gpointer data) +{ + GtkWidget *preview; + char *filename; + GdkPixbuf *pixbuf; + gboolean have_preview; + + preview = GTK_WIDGET (data); + filename = gtk_file_chooser_get_preview_filename (file_chooser); + + pixbuf = gdk_pixbuf_new_from_file_at_size (filename, 128, 128, NULL); + have_preview = (pixbuf != NULL); + g_free (filename); + + gtk_image_set_from_pixbuf (GTK_IMAGE (preview), pixbuf); + if (pixbuf) + g_object_unref (pixbuf); + + gtk_file_chooser_set_preview_widget_active (file_chooser, have_preview); +} + +static void +categoria_on_btn_icona_clicked (GtkButton *button, + gpointer user_data) +{ + GtkWidget *dlg; + GtkWidget *preview; + + Categoria *categoria = (Categoria *)user_data; + CategoriaPrivate *priv = CATEGORIA_GET_PRIVATE (categoria); + + preview = gtk_image_new (); + + dlg = gtk_file_chooser_dialog_new ("Scegli icona", + GTK_WINDOW (priv->w), + GTK_FILE_CHOOSER_ACTION_OPEN, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, + NULL); + + gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER (dlg), preview); + g_signal_connect (dlg, "update-preview", + G_CALLBACK (update_preview_cb), preview); + + if (gtk_dialog_run (GTK_DIALOG (dlg)) == GTK_RESPONSE_ACCEPT) + { + gchar *filename; + + filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dlg)); + + gtk_image_set_from_file (GTK_IMAGE (gtk_builder_get_object (priv->commons->gtkbuilder, "image1")), filename); + if (gtk_image_get_pixbuf (GTK_IMAGE (gtk_builder_get_object (priv->commons->gtkbuilder, "image1"))) != NULL) + { + gtk_label_set_text (GTK_LABEL (priv->lbl_icona_percorso), filename); + } + else + { + gtk_label_set_text (GTK_LABEL (priv->lbl_icona_percorso), ""); + } + + g_free (filename); + } + gtk_widget_destroy (dlg); +} + static void categoria_on_btn_annulla_clicked (GtkButton *button, gpointer user_data) -- 2.49.0