[recipes/inline-editing] wip: Ingredients list refactor



commit 01713aa2e0bfb1122fc5dbf1ee0437f76508fd37
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Apr 17 18:55:38 2017 -0400

    wip: Ingredients list refactor
    
    Split out two new widgets: GrIngredientsViewer and GrIngredientsViewerRow.
    Editing is not implemented yet, and quite a few other things are missing.

 src/gr-ingredients-viewer-row.c  |  130 +++++++++++++++++++++++++++
 src/gr-ingredients-viewer-row.h  |   33 +++++++
 src/gr-ingredients-viewer-row.ui |   22 +++++
 src/gr-ingredients-viewer.c      |  182 ++++++++++++++++++++++++++++++++++++++
 src/gr-ingredients-viewer.h      |   32 +++++++
 src/gr-ingredients-viewer.ui     |   93 +++++++++++++++++++
 src/meson.build                  |    2 +
 src/recipes.gresource.xml        |    2 +
 8 files changed, 496 insertions(+), 0 deletions(-)
---
diff --git a/src/gr-ingredients-viewer-row.c b/src/gr-ingredients-viewer-row.c
new file mode 100644
index 0000000..5119044
--- /dev/null
+++ b/src/gr-ingredients-viewer-row.c
@@ -0,0 +1,130 @@
+/* gr-ingredients-viewer-row.c:
+ *
+ * Copyright (C) 2017 Matthias Clasen <mclasen redhat com>
+ *
+ * Licensed under the GNU General Public License Version 3
+ *
+ * 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 3 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <glib/gi18n.h>
+
+#include "gr-ingredients-viewer-row.h"
+#include "gr-ingredient.h"
+
+
+struct _GrIngredientsViewerRow
+{
+        GtkListBoxRow parent_instance;
+
+        GtkWidget *label;
+
+        char *ingredient;
+};
+
+G_DEFINE_TYPE (GrIngredientsViewerRow, gr_ingredients_viewer_row, GTK_TYPE_LIST_BOX_ROW)
+
+enum {
+        PROP_0,
+        PROP_INGREDIENT
+};
+
+static void
+gr_ingredients_viewer_row_finalize (GObject *object)
+{
+        GrIngredientsViewerRow *self = GR_INGREDIENTS_VIEWER_ROW (object);
+
+        g_free (self->ingredient);
+
+        G_OBJECT_CLASS (gr_ingredients_viewer_row_parent_class)->finalize (object);
+}
+
+static void
+gr_ingredients_viewer_row_get_property (GObject    *object,
+                                       guint       prop_id,
+                                       GValue     *value,
+                                       GParamSpec *pspec)
+{
+        GrIngredientsViewerRow *self = GR_INGREDIENTS_VIEWER_ROW (object);
+
+        switch (prop_id)
+          {
+          case PROP_INGREDIENT:
+                g_value_set_string (value, self->ingredient);
+                break;
+          default:
+                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+          }
+}
+
+static void
+gr_ingredients_viewer_row_set_property (GObject      *object,
+                                       guint         prop_id,
+                                       const GValue *value,
+                                       GParamSpec   *pspec)
+{
+        GrIngredientsViewerRow *self = GR_INGREDIENTS_VIEWER_ROW (object);
+
+        switch (prop_id)
+          {
+          case PROP_INGREDIENT:
+                {
+                        g_free (self->ingredient);
+                        self->ingredient = g_value_dup_string (value);
+                        gtk_label_set_label (GTK_LABEL (self->label), self->ingredient);
+                }
+                break;
+          default:
+                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+          }
+}
+
+static void
+gr_ingredients_viewer_row_class_init (GrIngredientsViewerRowClass *klass)
+{
+        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+        GObjectClass *object_class = G_OBJECT_CLASS (klass);
+        GParamSpec *pspec;
+
+        object_class->finalize = gr_ingredients_viewer_row_finalize;
+        object_class->get_property = gr_ingredients_viewer_row_get_property;
+        object_class->set_property = gr_ingredients_viewer_row_set_property;
+
+        pspec = g_param_spec_string ("ingredient", NULL, NULL,
+                                     NULL,
+                                     G_PARAM_READWRITE);
+        g_object_class_install_property (object_class, PROP_INGREDIENT, pspec);
+
+        gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/Recipes/gr-ingredients-viewer-row.ui");
+
+        gtk_widget_class_bind_template_child (widget_class, GrIngredientsViewerRow, label);
+}
+
+static void
+gr_ingredients_viewer_row_init (GrIngredientsViewerRow *self)
+{
+        gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);
+        gtk_widget_init_template (GTK_WIDGET (self));
+}
+
+GrIngredientsViewerRow *
+gr_ingredients_viewer_row_new (const char *ingredient)
+{
+        return GR_INGREDIENTS_VIEWER_ROW (g_object_new (GR_TYPE_INGREDIENTS_VIEWER_ROW,
+                                         "ingredient", ingredient,
+                                         NULL));
+}
diff --git a/src/gr-ingredients-viewer-row.h b/src/gr-ingredients-viewer-row.h
new file mode 100644
index 0000000..a3d9fd2
--- /dev/null
+++ b/src/gr-ingredients-viewer-row.h
@@ -0,0 +1,33 @@
+/* gr-ingredients-viewer-row.h
+ *
+ * Copyright (C) 2017 Matthias Clasen <mclasen redhat com>
+ *
+ * Licensed under the GNU General Public License Version 3
+ *
+ * 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 3 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GR_TYPE_INGREDIENTS_VIEWER_ROW (gr_ingredients_viewer_row_get_type())
+
+G_DECLARE_FINAL_TYPE (GrIngredientsViewerRow, gr_ingredients_viewer_row, GR, INGREDIENTS_VIEWER_ROW, 
GtkListBoxRow)
+
+GrIngredientsViewerRow *gr_ingredients_viewer_row_new (const char *ingredient);
+
+G_END_DECLS
diff --git a/src/gr-ingredients-viewer-row.ui b/src/gr-ingredients-viewer-row.ui
new file mode 100644
index 0000000..5fb713f
--- /dev/null
+++ b/src/gr-ingredients-viewer-row.ui
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface domain="gnome-recipes">
+  <template class="GrIngredientsViewerRow" parent="GtkListBoxRow">
+    <property name="visible">True</property>
+    <child>
+      <object class="GtkBox" id="box">
+        <property name="visible">1</property>
+        <property name="spacing">12</property>
+        <property name="margin">6</property>
+        <child>
+          <object class="GtkLabel" id="label">
+            <property name="visible">1</property>
+            <property name="xalign">0</property>
+          </object>
+          <packing>
+            <property name="expand">1</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/gr-ingredients-viewer.c b/src/gr-ingredients-viewer.c
new file mode 100644
index 0000000..51478fd
--- /dev/null
+++ b/src/gr-ingredients-viewer.c
@@ -0,0 +1,182 @@
+/* gr-ingredients-viewer.c:
+ *
+ * Copyright (C) 2017 Matthias Clasen <mclasen redhat com>
+ *
+ * Licensed under the GNU General Public License Version 3.
+ *
+ * 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 3 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <string.h>
+
+#include "gr-ingredients-viewer.h"
+#include "gr-ingredients-viewer-row.h"
+#include "gr-utils.h"
+
+
+struct _GrIngredientsViewer
+{
+        GtkEventBox parent_instance;
+
+        GtkWidget *title_stack;
+        GtkWidget *title_entry;
+        GtkWidget *list;
+
+        char **ingredients;
+};
+
+
+G_DEFINE_TYPE (GrIngredientsViewer, gr_ingredients_viewer, GTK_TYPE_BOX)
+
+enum {
+        PROP_0,
+        PROP_TITLE,
+        PROP_EDITABLE_TITLE,
+        PROP_INGREDIENTS
+};
+
+static void
+gr_ingredients_viewer_finalize (GObject *object)
+{
+        GrIngredientsViewer *viewer = GR_INGREDIENTS_VIEWER (object);
+
+        g_strfreev (viewer->ingredients);
+
+        G_OBJECT_CLASS (gr_ingredients_viewer_parent_class)->finalize (object);
+}
+
+static void
+gr_ingredients_viewer_get_property (GObject    *object,
+                                    guint       prop_id,
+                                    GValue     *value,
+                                    GParamSpec *pspec)
+{
+        GrIngredientsViewer *self = GR_INGREDIENTS_VIEWER (object);
+
+        switch (prop_id)
+          {
+          case PROP_TITLE:
+                g_value_set_string (value, gtk_entry_get_text (GTK_ENTRY (self->title_entry)));
+                break;
+
+          case PROP_EDITABLE_TITLE: {
+                        const char *visible;
+
+                        visible = gtk_stack_get_visible_child_name (GTK_STACK (self->title_stack));
+                        g_value_set_boolean (value, strcmp (visible, "entry") == 0);
+                }
+                break;
+
+          case PROP_INGREDIENTS:
+                g_value_set_boxed (value, self->ingredients);
+                break;
+
+          default:
+                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+          }
+}
+
+static void
+gr_ingredients_viewer_set_ingredients (GrIngredientsViewer  *viewer,
+                                       const char          **ings)
+{
+        int i;
+
+        g_strfreev (viewer->ingredients);
+        viewer->ingredients = g_strdupv ((char **)ings);
+
+        container_remove_all (GTK_CONTAINER (viewer->list));
+
+        for (i = 0; ings && ings[i]; i++) {
+                GtkWidget *row;
+
+                row = g_object_new (GR_TYPE_INGREDIENTS_VIEWER_ROW, "ingredient", ings[i], NULL);
+
+                gtk_container_add (GTK_CONTAINER (viewer->list), row);
+        }
+}
+
+static void
+gr_ingredients_viewer_set_property (GObject      *object,
+                                    guint         prop_id,
+                                    const GValue *value,
+                                    GParamSpec   *pspec)
+{
+        GrIngredientsViewer *self = GR_INGREDIENTS_VIEWER (object);
+
+        switch (prop_id)
+          {
+          case PROP_TITLE:
+                gtk_entry_set_text (GTK_ENTRY (self->title_entry), g_value_get_string (value));
+                break;
+
+          case PROP_EDITABLE_TITLE: {
+                        gboolean editable;
+
+                        editable = g_value_get_boolean (value);
+                        gtk_stack_set_visible_child_name (GTK_STACK (self->title_stack),
+                                                                     editable ? "entry" : "label");
+                }
+                break;
+
+          case PROP_INGREDIENTS:
+                gr_ingredients_viewer_set_ingredients (self, (const char **)g_value_get_boxed (value));
+                break;
+
+          default:
+                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+          }
+}
+
+static void
+gr_ingredients_viewer_init (GrIngredientsViewer *self)
+{
+        gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);
+        gtk_widget_init_template (GTK_WIDGET (self));
+}
+
+
+static void
+gr_ingredients_viewer_class_init (GrIngredientsViewerClass *klass)
+{
+        GObjectClass *object_class = G_OBJECT_CLASS (klass);
+        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+        GParamSpec *pspec;
+
+        object_class->finalize = gr_ingredients_viewer_finalize;
+        object_class->get_property = gr_ingredients_viewer_get_property;
+        object_class->set_property = gr_ingredients_viewer_set_property;
+
+        pspec = g_param_spec_boolean ("editable-title", NULL, NULL,
+                                      FALSE,
+                                      G_PARAM_READWRITE);
+        g_object_class_install_property (object_class, PROP_EDITABLE_TITLE, pspec);
+
+        pspec = g_param_spec_string ("title", NULL, NULL,
+                                     NULL,
+                                     G_PARAM_READWRITE);
+        g_object_class_install_property (object_class, PROP_TITLE, pspec);
+
+        pspec = g_param_spec_boxed ("ingredients", NULL, NULL,
+                                    G_TYPE_STRV,
+                                    G_PARAM_READWRITE);
+        g_object_class_install_property (object_class, PROP_INGREDIENTS, pspec);
+
+        gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/Recipes/gr-ingredients-viewer.ui");
+        gtk_widget_class_bind_template_child (widget_class, GrIngredientsViewer, title_stack);
+        gtk_widget_class_bind_template_child (widget_class, GrIngredientsViewer, title_entry);
+        gtk_widget_class_bind_template_child (widget_class, GrIngredientsViewer, list);
+}
diff --git a/src/gr-ingredients-viewer.h b/src/gr-ingredients-viewer.h
new file mode 100644
index 0000000..caef6a4
--- /dev/null
+++ b/src/gr-ingredients-viewer.h
@@ -0,0 +1,32 @@
+/* gr-ingredients-viewer.h
+ *
+ * Copyright (C) 2017 Matthias Clasen <mclasen redhat com>
+ *
+ * Licensed under the GNU General Public License Version 3.
+ *
+ * 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 3 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GR_TYPE_INGREDIENTS_VIEWER (gr_ingredients_viewer_get_type())
+
+G_DECLARE_FINAL_TYPE (GrIngredientsViewer, gr_ingredients_viewer, GR, INGREDIENTS_VIEWER, GtkBox)
+
+G_END_DECLS
+
diff --git a/src/gr-ingredients-viewer.ui b/src/gr-ingredients-viewer.ui
new file mode 100644
index 0000000..f02c15d
--- /dev/null
+++ b/src/gr-ingredients-viewer.ui
@@ -0,0 +1,93 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface domain="gnome-recipes">
+  <template class="GrIngredientsViewer" parent="GtkBox">
+    <property name="visible">1</property>
+    <property name="orientation">vertical</property>
+    <child>
+      <object class="GtkStack" id="title_stack">
+        <property name="visible">1</property>
+        <child>
+          <object class="GtkLabel">
+            <property name="visible">1</property>
+            <property name="xalign">0</property>
+            <property name="label" translatable="yes">Ingredients</property>
+            <style>
+              <class name="heading"/>
+            </style>
+          </object>
+          <packing>
+            <property name="name">label</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkBox">
+            <property name="visible">1</property>
+            <property name="spacing">10</property>
+            <property name="orientation">horizontal</property>
+            <style>
+              <class name="heading"/>
+            </style>
+            <child>
+              <object class="GtkEntry" id="title_entry">
+                <property name="visible">1</property>
+                <property name="placeholder-text" translatable="yes">Name of the List</property>
+              </object>
+              <packing>
+                <property name="expand">1</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkButton">
+                <property name="visible">1</property>
+                <property name="label" translatable="yes">Remove</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="name">entry</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+    <child>
+      <object class="GtkListBox" id="list">
+        <property name="visible">1</property>
+        <property name="selection-mode">single</property>
+        <style>
+          <class name="frame"/>
+        </style>
+        <child type="placeholder">
+          <object class="GtkLabel">
+            <property name="visible">1</property>
+            <property name="xalign">0.5</property>
+            <property name="halign">fill</property>
+            <property name="margin-start">20</property>
+            <property name="margin-end">20</property>
+            <property name="margin-top">10</property>
+            <property name="margin-bottom">10</property>
+            <property name="label" translatable="yes">No ingredients added yet</property>
+            <style>
+              <class name="dim-label"/>
+            </style>
+          </object>
+        </child>
+      </object>
+    </child>
+    <child>
+      <object class="GtkButton">
+        <property name="visible">1</property>
+        <property name="halign">start</property>
+        <property name="margin-top">10</property>
+        <style>
+          <class name="dim-label"/>
+        </style>
+        <child>
+          <object class="GtkImage">
+            <property name="visible">1</property>
+            <property name="icon-name">list-add-symbolic</property>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/meson.build b/src/meson.build
index 42296dc..ea379d7 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -67,6 +67,8 @@ src += ['main.c',
        'gr-ingredient.c',
        'gr-ingredient-row.c',
        'gr-ingredients-list.c',
+       'gr-ingredients-viewer.c',
+       'gr-ingredients-viewer-row.c',
        'gr-list-page.c',
        'gr-logging.c',
        'gr-mail.c',
diff --git a/src/recipes.gresource.xml b/src/recipes.gresource.xml
index b33e1fc..23a04aa 100644
--- a/src/recipes.gresource.xml
+++ b/src/recipes.gresource.xml
@@ -15,6 +15,8 @@
     <file preprocess="xml-stripblanks">gr-image-viewer.ui</file>
     <file preprocess="xml-stripblanks">gr-image-page.ui</file>
     <file preprocess="xml-stripblanks">gr-ingredient-row.ui</file>
+    <file preprocess="xml-stripblanks">gr-ingredients-viewer.ui</file>
+    <file preprocess="xml-stripblanks">gr-ingredients-viewer-row.ui</file>
     <file preprocess="xml-stripblanks">gr-list-page.ui</file>
     <file preprocess="xml-stripblanks">gr-meal-row.ui</file>
     <file preprocess="xml-stripblanks">gr-recipes-page.ui</file>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]