[gnome-notes/136-implement-the-list-row-pattern-in-main-view: 4/8] list-view: make list-view-row responsible



commit 8c38db2615ce4ec187a0311744fe16851cd7cf4d
Author: Isaque Galdino <igaldino gmail com>
Date:   Sun Jun 14 23:33:19 2020 -0300

    list-view: make list-view-row responsible
    
    With this change, BjbListView hands over BjbListViewRow information
    ownership to BjbListViewRow itself, which makes more sense.

 data/resources/list-view-row.ui | 16 --------
 src/bjb-list-view-row.c         | 87 +++++++++++++++++++++++++++++++++--------
 src/bjb-list-view-row.h         |  9 +++--
 src/bjb-list-view.c             | 26 +++++-------
 src/bjb-list-view.h             | 16 ++++----
 5 files changed, 95 insertions(+), 59 deletions(-)
---
diff --git a/data/resources/list-view-row.ui b/data/resources/list-view-row.ui
index 98e4035..0c8af9e 100644
--- a/data/resources/list-view-row.ui
+++ b/data/resources/list-view-row.ui
@@ -28,22 +28,6 @@
             <property name="valign">center</property>
             <property name="spacing">6</property>
 
-            <child>
-              <object class="GtkLabel" id="uuid">
-                <property name="visible">False</property>
-                <property name="justify">left</property>
-                <property name="halign">start</property>
-                <property name="hexpand">True</property>
-                <property name="valign">center</property>
-                <property name="ellipsize">end</property>
-                <property name="xalign">0.0</property>
-              </object>
-              <packing>
-                <property name="expand">False</property>
-                <property name="fill">True</property>
-              </packing>
-            </child>
-
             <child>
               <object class="GtkBox">
                 <property name="visible">True</property>
diff --git a/src/bjb-list-view-row.c b/src/bjb-list-view-row.c
index fa92bd1..d8dc21b 100644
--- a/src/bjb-list-view-row.c
+++ b/src/bjb-list-view-row.c
@@ -19,17 +19,22 @@
  */
 
 #include <biji-string.h>
+#include "bjb-list-view.h"
 #include "bjb-list-view-row.h"
+#include "bjb-utils.h"
 
 struct _BjbListViewRow
 {
   GtkListBoxRow   parent_instance;
 
+  BjbListView    *view;
   GtkCheckButton *select_button;
-  GtkLabel       *uuid;
   GtkLabel       *title;
   GtkLabel       *content;
   GtkLabel       *updated_time;
+
+  char           *uuid;
+  char           *model_iter;
 };
 
 G_DEFINE_TYPE (BjbListViewRow, bjb_list_view_row, GTK_TYPE_LIST_BOX_ROW);
@@ -38,33 +43,63 @@ static void
 on_toggled_cb (BjbListViewRow *self,
                gpointer        data)
 {
+  GtkListBox *list_box = bjb_list_view_get_list_box (self->view);
+  BjbController *controller = bjb_list_view_get_controller (self->view);
+
   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->select_button)))
-    gtk_list_box_select_row (GTK_LIST_BOX (gtk_widget_get_parent (GTK_WIDGET (self))), GTK_LIST_BOX_ROW 
(self));
+    {
+      gtk_list_box_select_row (list_box, GTK_LIST_BOX_ROW (self));
+      bjb_controller_select_item (controller, self->model_iter);
+    }
   else
-    gtk_list_box_unselect_row (GTK_LIST_BOX (gtk_widget_get_parent (GTK_WIDGET (self))), GTK_LIST_BOX_ROW 
(self));
+    {
+      gtk_list_box_unselect_row (list_box, GTK_LIST_BOX_ROW (self));
+      bjb_controller_unselect_item (controller, self->model_iter);
+    }
 
   g_signal_emit_by_name (G_OBJECT (gtk_widget_get_parent (GTK_WIDGET (self))), "selected-rows-changed", 0);
-
 }
 
 void
 bjb_list_view_row_setup (BjbListViewRow *self,
-                         const char     *uuid,
-                         const char     *title,
-                         const char     *content,
-                         const char     *updated_time)
+                         BjbListView    *view,
+                         const char     *model_iter)
 {
-  g_auto (GStrv)   lines    = NULL;
-  g_autofree char *one_line = NULL;
-  g_autofree char *preview  = NULL;
+  GtkTreeModel    *model;
+  GtkTreeIter      iter;
+  char            *uuid;
+  char            *title;
+  char            *text;
+  gint64           mtime;
+  g_autofree char *updated_time = NULL;
+  g_auto (GStrv)   lines        = NULL;
+  g_autofree char *one_line     = NULL;
+  g_autofree char *preview      = NULL;
+
+  self->view = view;
+
+  model = bjb_controller_get_model (bjb_list_view_get_controller (self->view));
+  if (!gtk_tree_model_get_iter_from_string (model, &iter, model_iter))
+    return;
+  self->model_iter = g_strdup (model_iter);
+
+  gtk_tree_model_get (model,
+                      &iter,
+                      BJB_MODEL_COLUMN_UUID,  &uuid,
+                      BJB_MODEL_COLUMN_TITLE, &title,
+                      BJB_MODEL_COLUMN_TEXT,  &text,
+                      BJB_MODEL_COLUMN_MTIME, &mtime,
+                      -1);
+
+  updated_time = bjb_utils_get_human_time (mtime);
 
   if (uuid)
-    gtk_label_set_text (self->uuid, uuid);
+    self->uuid = g_strdup (uuid);
   if (title)
     gtk_label_set_text (self->title, title);
-  if (content)
+  if (text)
     {
-      lines = g_strsplit (content, "\n", -1);
+      lines = g_strsplit (text, "\n", -1);
       one_line = g_strjoinv (" ", lines);
       preview = biji_str_clean (one_line);
       gtk_label_set_text (self->content, preview);
@@ -78,12 +113,19 @@ bjb_list_view_row_show_select_button (BjbListViewRow *self,
                                       gboolean        show)
 {
   gtk_widget_set_visible (GTK_WIDGET (self->select_button), show);
+  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->select_button), FALSE);
 }
 
 const char *
 bjb_list_view_row_get_uuid (BjbListViewRow *self)
 {
-  return gtk_label_get_text (self->uuid);
+  return self->uuid;
+}
+
+const char *
+bjb_list_view_row_get_model_iter (BjbListViewRow *self)
+{
+  return self->model_iter;
 }
 
 static void
@@ -92,15 +134,28 @@ bjb_list_view_row_init (BjbListViewRow *self)
   gtk_widget_init_template (GTK_WIDGET (self));
 }
 
+static void
+bjb_list_view_row_finalize (GObject *object)
+{
+  BjbListViewRow *self = BJB_LIST_VIEW_ROW (object);
+
+  g_free (self->uuid);
+  g_free (self->model_iter);
+
+  G_OBJECT_CLASS (bjb_list_view_row_parent_class)->finalize (object);
+}
+
 static void
 bjb_list_view_row_class_init (BjbListViewRowClass *klass)
 {
+  GObjectClass* object_class = G_OBJECT_CLASS (klass);
   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
+  object_class->finalize = bjb_list_view_row_finalize;
+
   gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Notes/ui/list-view-row.ui");
 
   gtk_widget_class_bind_template_child (widget_class, BjbListViewRow, select_button);
-  gtk_widget_class_bind_template_child (widget_class, BjbListViewRow, uuid);
   gtk_widget_class_bind_template_child (widget_class, BjbListViewRow, title);
   gtk_widget_class_bind_template_child (widget_class, BjbListViewRow, content);
   gtk_widget_class_bind_template_child (widget_class, BjbListViewRow, updated_time);
diff --git a/src/bjb-list-view-row.h b/src/bjb-list-view-row.h
index 656ec57..bc375ee 100644
--- a/src/bjb-list-view-row.h
+++ b/src/bjb-list-view-row.h
@@ -21,6 +21,7 @@
 #pragma once
 
 #include <gtk/gtk.h>
+#include "bjb-list-view.h"
 
 G_BEGIN_DECLS
 
@@ -31,14 +32,14 @@ G_DECLARE_FINAL_TYPE (BjbListViewRow, bjb_list_view_row, BJB, LIST_VIEW_ROW, Gtk
 BjbListViewRow *bjb_list_view_row_new                (void);
 
 void            bjb_list_view_row_setup              (BjbListViewRow *self,
-                                                      const char     *uuid,
-                                                      const char     *title,
-                                                      const char     *content,
-                                                      const char     *updated_time);
+                                                      BjbListView    *view,
+                                                      const char     *model_iter);
 
 void            bjb_list_view_row_show_select_button (BjbListViewRow *self,
                                                       gboolean        show);
 
 const char     *bjb_list_view_row_get_uuid           (BjbListViewRow *self);
 
+const char     *bjb_list_view_row_get_model_iter     (BjbListViewRow *self);
+
 G_END_DECLS
diff --git a/src/bjb-list-view.c b/src/bjb-list-view.c
index 547511a..d06656e 100644
--- a/src/bjb-list-view.c
+++ b/src/bjb-list-view.c
@@ -47,28 +47,16 @@ bjb_list_view_create_row_cb (GtkTreeModel *model,
                              GtkTreeIter  *iter,
                              gpointer      data)
 {
-  BjbListView     *self         = NULL;
-  char            *uuid;
-  char            *title;
-  char            *text;
-  gint64           mtime;
+  BjbListView     *self;
   BjbListViewRow  *row;
-  g_autofree char *updated_time = NULL;
+  g_autofree char *model_iter = NULL;
 
   self = BJB_LIST_VIEW (data);
 
-  gtk_tree_model_get (model,
-                      iter,
-                      BJB_MODEL_COLUMN_UUID,  &uuid,
-                      BJB_MODEL_COLUMN_TITLE, &title,
-                      BJB_MODEL_COLUMN_TEXT,  &text,
-                      BJB_MODEL_COLUMN_MTIME, &mtime,
-                      -1);
-
-  updated_time = bjb_utils_get_human_time (mtime);
+  model_iter = gtk_tree_model_get_string_from_iter (model, iter);
 
   row = bjb_list_view_row_new ();
-  bjb_list_view_row_setup (row, uuid, title, text, updated_time);
+  bjb_list_view_row_setup (row, self, model_iter);
   gtk_widget_show (GTK_WIDGET (row));
   gtk_container_add (GTK_CONTAINER (self->list_box), GTK_WIDGET (row));
 
@@ -146,6 +134,12 @@ bjb_list_view_get_list_box (BjbListView *self)
   return self->list_box;
 }
 
+BjbController *
+bjb_list_view_get_controller (BjbListView *self)
+{
+  return self->controller;
+}
+
 static void
 bjb_list_view_init (BjbListView *self)
 {
diff --git a/src/bjb-list-view.h b/src/bjb-list-view.h
index 5ddcf16..b32309d 100644
--- a/src/bjb-list-view.h
+++ b/src/bjb-list-view.h
@@ -29,17 +29,19 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (BjbListView, bjb_list_view, BJB, LIST_VIEW, GtkScrolledWindow)
 
-BjbListView *bjb_list_view_new                (void);
+BjbListView   *bjb_list_view_new                (void);
 
-void         bjb_list_view_setup              (BjbListView   *self,
-                                               BjbController *controller);
+void           bjb_list_view_setup              (BjbListView   *self,
+                                                 BjbController *controller);
 
-void         bjb_list_view_update             (BjbListView   *self);
+void           bjb_list_view_update             (BjbListView   *self);
 
-GtkListBox  *bjb_list_view_get_list_box       (BjbListView   *self);
+GtkListBox    *bjb_list_view_get_list_box       (BjbListView   *self);
 
-void         bjb_list_view_set_selection_mode (BjbListView   *self,
-                                               gboolean       mode);
+BjbController *bjb_list_view_get_controller     (BjbListView   *self);
+
+void           bjb_list_view_set_selection_mode (BjbListView   *self,
+                                                 gboolean       mode);
 
 G_END_DECLS
 


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