[gnome-photos/wip/rishi/edit-mode: 21/24] gegl-gtk-view-helper, preview-view: Merge AUTOSCALE_CONTENT logic



commit 91f2467cecfcfb5e035dd0c241dc3efcaa88ff91
Author: Debarshi Ray <debarshir gnome org>
Date:   Mon Jun 15 09:22:41 2015 +0200

    gegl-gtk-view-helper, preview-view: Merge AUTOSCALE_CONTENT logic
    
    We want AUTOSCALE_CONTENT to always centre the image inside the
    widget's allocation irrespective of the current origin. GeglGtkView
    wasn't doing this. So we had our own code to centre the image by
    calculating a new origin and setting it, under the following scenarios:
      - Setting a new GeglNode
      - Completion of graph processing (ie. the last GeglNode::compute)
      - Size allocation has changed (ie. GtkWidget::size_allocate)
    
    Calculating a new origin also required us to calculate a new scaling
    factor, just like GeglGtkView. Unlike our code, GeglGtkView was doing
    it 'iteratively'. It would convert the image's bounding box to widget
    co-ordinates by using the existing scaling factor, and then adjust it
    by multiplying with a factor. Setting the new scale and the origin
    would (needlessly) re-run the AUTOSCALE_CONTENT logic until it reached
    a steady state. Ideally, the subsequent runs of the logic would be
    NOPs, but inaccuracies of floating arithmetic often introduced small
    errors.
    
    Now, we completely ignore the existing scaling factor, and calculate
    the new scale and origin from scratch. We also don't re-run the logic
    after setting the new values.

 src/gegl-gtk-view-helper.c |   29 +++++++++++++++----
 src/photos-preview-view.c  |   65 --------------------------------------------
 2 files changed, 23 insertions(+), 71 deletions(-)
---
diff --git a/src/gegl-gtk-view-helper.c b/src/gegl-gtk-view-helper.c
index 225bd8e..7a052c2 100644
--- a/src/gegl-gtk-view-helper.c
+++ b/src/gegl-gtk-view-helper.c
@@ -15,6 +15,7 @@
  *
  * Copyright (C) 2003, 2004, 2006 Øyvind Kolås <pippin gimp org>
  * Copyright (C) 2011 Jon Nordby <jononor gmail com>
+ * Copyright (C) 2015 Red Hat, Inc.
  */
 
 #include "config.h"
@@ -121,24 +122,40 @@ update_autoscale(ViewHelper *self)
         return;
 
     bbox = gegl_node_get_bounding_box(self->node);
-    model_rect_to_view_rect(self, &bbox);
     if (bbox.width < 0 || bbox.height < 0)
         return;
 
     if (self->autoscale_policy == GEGL_GTK_VIEW_AUTOSCALE_WIDGET) {
         /* Request widget size change */
         /* XXX: Should we reset scale/x/y here? */
+        model_rect_to_view_rect(self, &bbox);
         g_signal_emit(self, view_helper_signals[SIGNAL_SIZE_CHANGED],
                       0, &bbox, NULL);
 
     } else if (self->autoscale_policy == GEGL_GTK_VIEW_AUTOSCALE_CONTENT) {
         /* Calculate and set scaling factor to make the content fit inside */
-        float width_ratio = bbox.width / (float)viewport.width;
-        float height_ratio = bbox.height / (float)viewport.height;
-        float max_ratio = width_ratio >= height_ratio ? width_ratio : height_ratio;
+        float delta_x;
+        float delta_y;
+        float scale = 1.0;
 
-        float current_scale = view_helper_get_scale(self);
-        view_helper_set_scale(self, current_scale * (1.0 / max_ratio));
+        if (bbox.width > viewport.width || bbox.height > viewport.height) {
+            float width_ratio = bbox.width / (float)viewport.width;
+            float height_ratio = bbox.height / (float)viewport.height;
+            float max_ratio = width_ratio >= height_ratio ? width_ratio : height_ratio;
+
+            scale = 1.0 / max_ratio;
+
+            bbox.width = (gint) (scale * bbox.width + 0.5);
+            bbox.height = (gint) (scale * bbox.height + 0.5);
+        }
+
+        self->scale = scale;
+
+        /* At this point, viewport is definitely bigger than bbox. */
+        delta_x = (viewport.width - bbox.width) / 2.0;
+        delta_y = (viewport.height - bbox.height) / 2.0;
+        self->x = -delta_x;
+        self->y = -delta_y;
     }
 
 }
diff --git a/src/photos-preview-view.c b/src/photos-preview-view.c
index 914d2de..ed8d358 100644
--- a/src/photos-preview-view.c
+++ b/src/photos-preview-view.c
@@ -147,67 +147,6 @@ photos_preview_view_nav_buttons_activated (PhotosPreviewView *self, PhotosPrevie
 }
 
 
-static void
-photos_preview_view_scale_and_align_image (PhotosPreviewView *self, GtkWidget *view)
-{
-  PhotosPreviewViewPrivate *priv = self->priv;
-  GeglRectangle bbox;
-  GtkAllocation alloc;
-  float delta_x;
-  float delta_y;
-  float scale = 1.0;
-
-  if (priv->node == NULL)
-    return;
-
-  /* Reset these properties, otherwise values from the previous node
-   * will interfere with the current one.
-   */
-  gegl_gtk_view_set_autoscale_policy (GEGL_GTK_VIEW (view), GEGL_GTK_VIEW_AUTOSCALE_DISABLED);
-  gegl_gtk_view_set_scale (GEGL_GTK_VIEW (view), 1.0);
-  gegl_gtk_view_set_x (GEGL_GTK_VIEW (view), 0.0);
-  gegl_gtk_view_set_y (GEGL_GTK_VIEW (view), 0.0);
-
-  bbox = gegl_node_get_bounding_box (priv->node);
-  gtk_widget_get_allocation (view, &alloc);
-
-  if (bbox.width > alloc.width || bbox.height > alloc.height)
-    {
-      float height_ratio;
-      float max_ratio;
-      float width_ratio;
-
-      gegl_gtk_view_set_autoscale_policy (GEGL_GTK_VIEW (view), GEGL_GTK_VIEW_AUTOSCALE_CONTENT);
-
-      /* TODO: since gegl_gtk_view_get_scale is not giving the
-       *       correct value of scale, we calculate it ourselves.
-       */
-      height_ratio = (float) bbox.height / alloc.height;
-      width_ratio = (float) bbox.width / alloc.width;
-      max_ratio = width_ratio >= height_ratio ? width_ratio : height_ratio;
-      scale = 1.0 / max_ratio;
-
-      bbox.width = (gint) (scale * bbox.width + 0.5);
-      bbox.height = (gint) (scale * bbox.height + 0.5);
-    }
-
-
-  /* At this point, alloc is definitely bigger than bbox. */
-  delta_x = (alloc.width - bbox.width) / 2.0;
-  delta_y = (alloc.height - bbox.height) / 2.0;
-  gegl_gtk_view_set_x (GEGL_GTK_VIEW (view), -delta_x);
-  gegl_gtk_view_set_y (GEGL_GTK_VIEW (view), -delta_y);
-}
-
-
-static void
-photos_preview_view_size_allocate (PhotosPreviewView *self, GdkRectangle *allocation, gpointer user_data)
-{
-  GtkWidget *view = GTK_WIDGET (user_data);
-  photos_preview_view_scale_and_align_image (self, view);
-}
-
-
 static GtkWidget *
 photos_preview_view_create_view (PhotosPreviewView *self)
 {
@@ -219,7 +158,6 @@ photos_preview_view_create_view (PhotosPreviewView *self)
   context = gtk_widget_get_style_context (view);
   gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
   gtk_style_context_add_class (context, "content-view");
-  g_signal_connect_swapped (view, "size-allocate", G_CALLBACK (photos_preview_view_size_allocate), self);
   g_signal_connect_swapped (view, "draw-background", G_CALLBACK (photos_preview_view_draw_background), self);
 
   /* It has to be visible to become the visible child of priv->stack. */
@@ -246,7 +184,6 @@ photos_preview_view_process (GObject *source_object, GAsyncResult *res, gpointer
     }
 
   view = gtk_stack_get_visible_child (GTK_STACK (priv->stack));
-  photos_preview_view_scale_and_align_image (self, view);
   gtk_widget_queue_draw (view);
 }
 
@@ -504,8 +441,6 @@ photos_preview_view_set_node (PhotosPreviewView *self, GeglNode *node)
     {
       priv->node = g_object_ref (node);
 
-      photos_preview_view_scale_and_align_image (self, view);
-
       /* Steals the reference to the GeglNode. */
       gegl_gtk_view_set_node (GEGL_GTK_VIEW (view), g_object_ref (priv->node));
       gtk_widget_queue_draw (view);


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