[gnome-control-center/wip/jsparber/background: 46/58] [refactor] moved gallery item code to grid-item class



commit f3fc51f739b9af3624a985f6d0fc565db5268ab3
Author: Julian Sparber <julian sparber net>
Date:   Mon Nov 13 16:29:31 2017 +0100

    [refactor] moved gallery item code to grid-item class

 panels/background/cc-background-grid-item.c |  143 ++++++++++++++++++++++---
 panels/background/cc-background-grid-item.h |    3 +-
 panels/background/cc-background-panel.c     |  154 +--------------------------
 3 files changed, 134 insertions(+), 166 deletions(-)
---
diff --git a/panels/background/cc-background-grid-item.c b/panels/background/cc-background-grid-item.c
index c2341cc..5d83502 100644
--- a/panels/background/cc-background-grid-item.c
+++ b/panels/background/cc-background-grid-item.c
@@ -24,11 +24,9 @@ struct _CcBackgroundGridItem
 {
   GtkFlowBoxChild            parent;
 
-  GtkImage                  *image;
-
   /* data */
   CcBackgroundItem      *item;
-
+  GdkPixbuf *cached_pixbuf;
 };
 
 
@@ -36,15 +34,116 @@ G_DEFINE_TYPE (CcBackgroundGridItem, cc_background_grid_item, GTK_TYPE_FLOW_BOX_
 
     enum {
       PROP_0,
-      PROP_ITEM
+      PROP_ITEM,
+      PROP_PIXBUF_CACHE
     };
 
+static void
+add_slideshow_emblem (GdkPixbuf *pixbuf,
+                      gint w,
+                      gint h,
+                      gint scale_factor)
+{
+  GdkPixbuf *slideshow_emblem;
+  GIcon *icon = NULL;
+  GtkIconInfo *icon_info = NULL;
+  GError *error = NULL;
+  GtkIconTheme *theme;
+
+  int eh;
+  int ew;
+  //int h;
+  //int w;
+  int x;
+  int y;
+
+  icon = g_themed_icon_new ("slideshow-emblem");
+  theme = gtk_icon_theme_get_default ();
+  icon_info = gtk_icon_theme_lookup_by_gicon_for_scale (theme,
+                                                        icon,
+                                                        16,
+                                                        scale_factor,
+                                                        GTK_ICON_LOOKUP_FORCE_SIZE |
+                                                        GTK_ICON_LOOKUP_USE_BUILTIN);
+  if (icon_info == NULL) {
+    g_warning ("Your icon theme is missing the slideshow-emblem icon, "
+               "please file a bug against it");
+  }
+  else {
+
+    slideshow_emblem = gtk_icon_info_load_icon (icon_info, &error);
+    if (slideshow_emblem == NULL) {
+      g_warning ("Failed to load slideshow emblem: %s", error->message);
+      g_error_free (error);
+    }
+    else {
+      eh = gdk_pixbuf_get_height (slideshow_emblem);
+      ew = gdk_pixbuf_get_width (slideshow_emblem);
+      //h = gdk_pixbuf_get_height (pixbuf);
+      //w = gdk_pixbuf_get_width (pixbuf);
+      x = w - ew - 5;
+      y = h - eh - 5;
+
+      gdk_pixbuf_composite (slideshow_emblem, pixbuf, x, y, ew, eh, x, y, 1.0, 1.0, GDK_INTERP_BILINEAR, 
255);
+    }
+  }
+
+  g_clear_object (&icon_info);
+  g_clear_object (&icon);
+}
+
+static gboolean
+on_gallery_item_draw (GtkWidget         *widget,
+                      cairo_t           *cr,
+                      CcBackgroundGridItem *item)
+{
+  GdkPixbuf *pixbuf = item->cached_pixbuf;
+  GdkPixbuf *new_pixbuf;
+  const gint space_width = gtk_widget_get_allocated_width (widget);
+  const gint space_height = gtk_widget_get_allocated_height ( (widget));
+  //const gint pixbuf_width = gdk_pixbuf_get_width (pixbuf);
+  //const gint pixbuf_height = gdk_pixbuf_get_height (pixbuf);
+  const gint scale_factor = gtk_widget_get_scale_factor (widget);
+  gint new_width;
+  gint new_height;
+
+  if (space_width * 9/16 > space_height) {
+    new_width = space_width;
+    new_height = space_width * 9/16;
+  }
+  else {
+    new_width = space_height * 16/9;
+    new_height = space_height;
+  }
+
+  new_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
+                                        new_width,
+                                        new_height,
+                                        GDK_INTERP_BILINEAR);
+
+  if (cc_background_item_changes_with_time (cc_background_grid_item_get_ref 
(gtk_widget_get_parent(widget)))) {
+    add_slideshow_emblem (new_pixbuf, (space_width + new_width) / 2, (space_height + new_height)/2, 
scale_factor);
+  }
+
+
+  gdk_cairo_set_source_pixbuf (cr,
+                               new_pixbuf,
+                               (space_width - new_width) / 2,
+                               (space_height - new_height) / 2);
+
+  g_object_unref (new_pixbuf);
+  cairo_paint (cr);
+
+  return TRUE;
+}
+
 GtkWidget*
-cc_background_grid_item_new (CcBackgroundItem *item)
+cc_background_grid_item_new (CcBackgroundItem *item, GdkPixbuf *pixbuf)
 {
 
   return g_object_new (CC_TYPE_BACKGROUND_GRID_ITEM,
                        "item", item,
+                       "cached_pixbuf", pixbuf,
                        NULL);
 }
 
@@ -88,8 +187,10 @@ cc_background_grid_item_set_property (GObject *object,
     {
 
     case PROP_ITEM:
-      self->item = g_value_dup_object (value);
-      g_debug ("Every set %p -> %p", value, self->item);
+      g_set_object (&self->item, g_value_get_object (value));
+      break;
+    case PROP_PIXBUF_CACHE:
+      g_set_object (&self->cached_pixbuf, g_value_get_object (value));
       break;
 
     default:
@@ -111,6 +212,10 @@ cc_background_grid_item_get_property (GObject    *object,
       g_value_set_object (value, self->item);
       break;
 
+    case PROP_PIXBUF_CACHE:
+      g_value_set_object (value, self->cached_pixbuf);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -128,10 +233,6 @@ cc_background_grid_item_class_init (CcBackgroundGridItemClass *klass)
   object_class->get_property = cc_background_grid_item_get_property;
   object_class->set_property = cc_background_grid_item_set_property;
 
-  /*g_object_class_override_property (object_class,
-                                    PROP_ITEM,
-                                    "item");
-                                    */
   g_object_class_install_property (object_class,
                                    PROP_ITEM,
                                    g_param_spec_object ("item",
@@ -139,12 +240,28 @@ cc_background_grid_item_class_init (CcBackgroundGridItemClass *klass)
                                                         "The reference to this background item",
                                                         CC_TYPE_BACKGROUND_ITEM,
                                                         G_PARAM_READWRITE));
+  g_object_class_install_property (object_class,
+                                   PROP_PIXBUF_CACHE,
+                                   g_param_spec_object ("cached_pixbuf",
+                                                        "Cached Pixbuf for preview",
+                                                        "The pixbuf for caching the preview in gallery",
+                                                        GDK_TYPE_PIXBUF,
+                                                        G_PARAM_READWRITE));
 
 }
 
 static void
 cc_background_grid_item_init (CcBackgroundGridItem *self)
 {
-  g_debug ("Item ref: %p", self->item);
-  //gtk_widget_init_template (GTK_WIDGET (self));
+  GtkWidget *drawing;
+
+  drawing = gtk_drawing_area_new ();
+  gtk_widget_set_hexpand(drawing, TRUE);
+  gtk_widget_set_vexpand(drawing, TRUE);
+  g_signal_connect (G_OBJECT (drawing), "draw",
+                    G_CALLBACK (on_gallery_item_draw), self);
+
+  gtk_widget_set_size_request (self, 250, 200);
+  gtk_widget_show (drawing);
+  gtk_container_add (GTK_CONTAINER (self), drawing);
 }
diff --git a/panels/background/cc-background-grid-item.h b/panels/background/cc-background-grid-item.h
index 2c0a197..1e90a7f 100644
--- a/panels/background/cc-background-grid-item.h
+++ b/panels/background/cc-background-grid-item.h
@@ -28,7 +28,8 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (CcBackgroundGridItem, cc_background_grid_item, CC, BACKGROUND_GRID_LIST, 
GtkFlowBoxChild)
 
-GtkWidget *             cc_background_grid_item_new             (CcBackgroundItem             *);
+GtkWidget *             cc_background_grid_item_new             (CcBackgroundItem             *,
+                                                                 GdkPixbuf                    *);
 void                    cc_background_grid_item_set_ref         (GtkWidget                    *,
                                                                  CcBackgroundItem             *);
 CcBackgroundItem *      cc_background_grid_item_get_ref         (GtkWidget                    *);
diff --git a/panels/background/cc-background-panel.c b/panels/background/cc-background-panel.c
index 9075652..0325a31 100644
--- a/panels/background/cc-background-panel.c
+++ b/panels/background/cc-background-panel.c
@@ -162,88 +162,6 @@ get_save_path (const char *filename)
                            NULL);
 }
 
-static void
-add_slideshow_emblem (GdkPixbuf *pixbuf,
-                      gint w,
-                      gint h,
-                      gint scale_factor)
-{
-  GdkPixbuf *slideshow_emblem;
-  GIcon *icon = NULL;
-  GtkIconInfo *icon_info = NULL;
-  GError *error = NULL;
-  GtkIconTheme *theme;
-
-  int eh;
-  int ew;
-  //int h;
-  //int w;
-  int x;
-  int y;
-
-  icon = g_themed_icon_new ("slideshow-emblem");
-  theme = gtk_icon_theme_get_default ();
-  icon_info = gtk_icon_theme_lookup_by_gicon_for_scale (theme,
-                                                        icon,
-                                                        16,
-                                                        scale_factor,
-                                                        GTK_ICON_LOOKUP_FORCE_SIZE |
-                                                        GTK_ICON_LOOKUP_USE_BUILTIN);
-  if (icon_info == NULL) {
-    g_warning ("Your icon theme is missing the slideshow-emblem icon, "
-               "please file a bug against it");
-  }
-  else {
-
-    slideshow_emblem = gtk_icon_info_load_icon (icon_info, &error);
-    if (slideshow_emblem == NULL) {
-      g_warning ("Failed to load slideshow emblem: %s", error->message);
-      g_error_free (error);
-    }
-    else {
-      eh = gdk_pixbuf_get_height (slideshow_emblem);
-      ew = gdk_pixbuf_get_width (slideshow_emblem);
-      //h = gdk_pixbuf_get_height (pixbuf);
-      //w = gdk_pixbuf_get_width (pixbuf);
-      x = w - ew - 5;
-      y = h - eh - 5;
-
-      gdk_pixbuf_composite (slideshow_emblem, pixbuf, x, y, ew, eh, x, y, 1.0, 1.0, GDK_INTERP_BILINEAR, 
255);
-    }
-  }
-
-  g_clear_object (&icon_info);
-  g_clear_object (&icon);
-}
-
-
-
-static GdkPixbuf*
-get_or_create_cached_pixbuf_with_size (CcBackgroundPanel *panel,
-                                       const gint width,
-                                       const gint height,
-                                       CcBackgroundItem  *item)
-{
-  gint scale_factor;
-  GdkPixbuf *pixbuf;
-
-  pixbuf = g_object_get_data (G_OBJECT (item), "pixbuf");
-  if (pixbuf == NULL ||
-      gdk_pixbuf_get_width (pixbuf) != width ||
-      gdk_pixbuf_get_height (pixbuf) != height) {
-    scale_factor = gtk_widget_get_scale_factor (panel);
-    pixbuf = cc_background_item_get_frame_thumbnail (item,
-                                                     panel->thumb_factory,
-                                                     width,
-                                                     height,
-                                                     scale_factor,
-                                                     -2, TRUE);
-    g_object_set_data_full (G_OBJECT (item), "pixbuf", pixbuf, g_object_unref);
-  }
-
-  return pixbuf;
-}
-
 static GdkPixbuf*
 get_or_create_cached_pixbuf (CcBackgroundPanel *panel,
                              GtkWidget         *widget,
@@ -288,55 +206,6 @@ on_preview_draw (GtkWidget         *widget,
   return TRUE;
 }
 
-static gboolean
-on_gallery_item_draw (GtkWidget         *widget,
-                      cairo_t           *cr,
-                      GdkPixbuf *pixbuf)
-{
-  //GdkPixbuf *pixbuf;
-  const gint space_width = gtk_widget_get_allocated_width (widget);
-  const gint space_height = gtk_widget_get_allocated_height ( (widget));
-  const gint pixbuf_width = gdk_pixbuf_get_width (pixbuf);
-  const gint pixbuf_height = gdk_pixbuf_get_height (pixbuf);
-  const gint scale_factor = gtk_widget_get_scale_factor (widget);
-  gint new_width;
-  gint new_height;
-
-  if (space_width * 9/16 > space_height) {
-    new_width = space_width;
-    new_height = space_width * 9/16;
-  }
-  else {
-    new_width = space_height * 16/9;
-    new_height = space_height;
-  }
-
-  /* pixbuf = get_or_create_cached_pixbuf_with_size (panel,
-     new_width,
-     new_height,
-     cc_background_grid_item_get_ref (gtk_widget_get_parent(widget)));
-
-*/
-
-  pixbuf = gdk_pixbuf_scale_simple (pixbuf,
-                                    new_width,
-                                    new_height,
-                                    GDK_INTERP_BILINEAR);
-
-  if (cc_background_item_changes_with_time (cc_background_grid_item_get_ref 
(gtk_widget_get_parent(widget)))) {
-    add_slideshow_emblem (pixbuf, (space_width + new_width) / 2, (space_height + new_height)/2, 
scale_factor);
-  }
-
-  gdk_cairo_set_source_pixbuf (cr,
-                               pixbuf,
-                               (space_width - new_width) / 2,
-                               (space_height - new_height) / 2);
-  cairo_paint (cr);
-
-  return TRUE;
-}
-
-
 static void
 on_panel_resize (GtkWidget *widget,
                  GdkRectangle *allocation,
@@ -740,10 +609,9 @@ create_gallery_item (gpointer item,
                      gpointer user_data)
 {
   CcBackgroundPanel *panel = user_data;
+  CcBackgroundItem *self = item;
   GtkWidget *flow;
-  GtkWidget *widget;
   GdkPixbuf *pixbuf;
-  CcBackgroundItem *self = item;
   gint scale_factor;
   const gint preview_width = 400;
   const gint preview_height = 400 * 9 / 16;
@@ -756,25 +624,7 @@ create_gallery_item (gpointer item,
                                                    preview_height,
                                                    scale_factor,
                                                    -2, TRUE);
-
-  /*if (cc_background_item_changes_with_time (self)) {
-    add_slideshow_emblem (pixbuf, scale_factor);
-    }
-    */
-  widget = gtk_drawing_area_new ();
-  gtk_widget_set_hexpand(widget, TRUE);
-  gtk_widget_set_vexpand(widget, TRUE);
-  g_signal_connect (G_OBJECT (widget), "draw",
-                    G_CALLBACK (on_gallery_item_draw), pixbuf);
-
-  flow = cc_background_grid_item_new(self);
-
-  gtk_widget_set_size_request (flow, 250, 200);
-  cc_background_grid_item_set_ref (flow, self);
-  gtk_widget_show (flow);
-  gtk_widget_show (widget);
-  gtk_container_add (GTK_CONTAINER (flow), widget);
-
+  flow = cc_background_grid_item_new(self, pixbuf);
   return flow;
 }
 


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