[gnome-photos/wip/rishi/edit-mode: 20/25] Add PhotosToolCrop



commit 056d91b8c1217f1b491a777eff1f779a90fb07b4
Author: Debarshi Ray <debarshir gnome org>
Date:   Fri Jun 12 01:35:43 2015 +0200

    Add PhotosToolCrop

 src/Makefile.am        |    2 +
 src/photos-icons.h     |    1 +
 src/photos-tool-crop.c |  437 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/photos-tool-crop.h |   45 +++++
 src/photos-utils.c     |    2 +
 5 files changed, 487 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 310a981..7f9e6be 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -218,6 +218,8 @@ gnome_photos_SOURCES = \
        photos-spinner-box.h \
        photos-tool.c \
        photos-tool.h \
+       photos-tool-crop.c \
+       photos-tool-crop.h \
        photos-tool-filters.c \
        photos-tool-filters.h \
        photos-tool-sharpen.c \
diff --git a/src/photos-icons.h b/src/photos-icons.h
index 80864ed..4e84f6f 100644
--- a/src/photos-icons.h
+++ b/src/photos-icons.h
@@ -40,6 +40,7 @@ G_BEGIN_DECLS
 #define PHOTOS_ICON_GO_NEXT_SYMBOLIC "go-next-symbolic"
 #define PHOTOS_ICON_GO_PREVIOUS_SYMBOLIC "go-previous-symbolic"
 
+#define PHOTOS_ICON_IMAGE_CROP_SYMBOLIC "image-crop-symbolic"
 #define PHOTOS_ICON_IMAGE_FILTER_SYMBOLIC "image-filter-symbolic"
 #define PHOTOS_ICON_IMAGE_SHARPEN_SYMBOLIC "image-sharpen-symbolic"
 
diff --git a/src/photos-tool-crop.c b/src/photos-tool-crop.c
new file mode 100644
index 0000000..02cb06d
--- /dev/null
+++ b/src/photos-tool-crop.c
@@ -0,0 +1,437 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2015 Red Hat, Inc.
+ *
+ * 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+
+#include "config.h"
+
+#include <cairo.h>
+#include <gdk/gdk.h>
+#include <gio/gio.h>
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+
+#include "gegl-gtk-view.h"
+#include "photos-icons.h"
+#include "photos-tool.h"
+#include "photos-tool-crop.h"
+#include "photos-utils.h"
+
+
+struct _PhotosToolCrop
+{
+  PhotosTool parent_instance;
+  GeglRectangle bbox_scaled;
+  GeglRectangle bbox_source;
+  GtkListStore *model;
+  GtkWidget *combo_box;
+  GtkWidget *view;
+  cairo_surface_t *surface;
+  gdouble crop_height;
+  gdouble crop_width;
+  gdouble crop_x;
+  gdouble crop_y;
+};
+
+struct _PhotosToolCropClass
+{
+  PhotosToolClass parent_class;
+};
+
+
+G_DEFINE_TYPE_WITH_CODE (PhotosToolCrop, photos_tool_crop, PHOTOS_TYPE_TOOL,
+                         photos_utils_ensure_extension_points ();
+                         g_io_extension_point_implement (PHOTOS_TOOL_EXTENSION_POINT_NAME,
+                                                         g_define_type_id,
+                                                         "crop",
+                                                         100));
+
+
+#define ASPECT_RATIO_ANY -1.0
+#define ASPECT_RATIO_BASIS -2.0
+#define ASPECT_RATIO_ORIGINAL -3.0
+#define ASPECT_RATIO_SCREEN -4.0
+
+typedef struct _PhotosToolCropConstraint PhotosToolCropConstraint;
+
+struct _PhotosToolCropConstraint
+{
+  const gchar *name;
+  gdouble aspect_ratio;
+  guint basis_height;
+  guint basis_width;
+};
+
+enum
+{
+  CONSTRAINT_COLUMN_NAME = 0,
+  CONSTRAINT_COLUMN_ASPECT_RATIO = 1,
+  CONSTRAINT_COLUMN_BASIS_HEIGHT = 2,
+  CONSTRAINT_COLUMN_BASIS_WIDTH = 3
+};
+
+static PhotosToolCropConstraint CONSTRAINTS[] =
+{
+  { N_("Free"), ASPECT_RATIO_ANY, 0, 0 },
+  { N_("Original Size"), ASPECT_RATIO_ORIGINAL, 0, 0 },
+  { N_("Screen"), ASPECT_RATIO_SCREEN, 0, 0 },
+  { N_("Square"), ASPECT_RATIO_BASIS, 1, 1 }
+};
+
+
+static gdouble
+photos_tool_crop_calculate_aspect_ratio (PhotosToolCrop *self)
+{
+  gdouble ret_val = 1.0;
+  gint active;
+
+  active = gtk_combo_box_get_active (GTK_COMBO_BOX (self->combo_box));
+
+  if (CONSTRAINTS[active].aspect_ratio == ASPECT_RATIO_ANY)
+    g_assert_not_reached ();
+
+  if (CONSTRAINTS[active].aspect_ratio == ASPECT_RATIO_BASIS)
+    {
+      ret_val = (gdouble) CONSTRAINTS[active].basis_width / CONSTRAINTS[active].basis_height;
+    }
+  else if (CONSTRAINTS[active].aspect_ratio == ASPECT_RATIO_ORIGINAL)
+    {
+      ret_val = (gdouble) self->bbox_source.width / self->bbox_source.height;
+    }
+  else if (CONSTRAINTS[active].aspect_ratio == ASPECT_RATIO_SCREEN)
+    {
+      GdkScreen *screen;
+      gint height;
+      gint width;
+
+      screen = gdk_screen_get_default ();
+      height = gdk_screen_get_height (screen);
+      width = gdk_screen_get_width (screen);
+      ret_val = (gdouble) width / height;
+    }
+
+  return ret_val;
+}
+
+
+static void
+photos_tool_crop_surface_create (PhotosToolCrop *self)
+{
+  GdkWindow *window;
+  gfloat scale;
+
+  g_clear_pointer (&self->surface, (GDestroyNotify) cairo_surface_destroy);
+
+  window = gtk_widget_get_window (self->view);
+  scale = gegl_gtk_view_get_scale (GEGL_GTK_VIEW (self->view));
+  self->bbox_scaled.height = (gint) (scale * self->bbox_source.height + 0.5);
+  self->bbox_scaled.width = (gint) (scale * self->bbox_source.width + 0.5);
+  self->surface = gdk_window_create_similar_surface (window,
+                                                     CAIRO_CONTENT_COLOR_ALPHA,
+                                                     self->bbox_scaled.width,
+                                                     self->bbox_scaled.height);
+}
+
+
+static void
+photos_tool_crop_surface_reset (PhotosToolCrop *self)
+{
+  cairo_t *cr;
+  const gdouble handle_offset = 3.0;
+  const gdouble handle_radius = 8.0;
+  gdouble crop_aspect_ratio;
+  gdouble aspect_ratio;
+  gdouble one_third_x;
+  gdouble one_third_y;
+
+  cr = cairo_create (self->surface);
+  cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
+  cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.5);
+  cairo_paint (cr);
+
+  aspect_ratio = (gdouble) self->bbox_source.width / self->bbox_source.height;
+  crop_aspect_ratio = photos_tool_crop_calculate_aspect_ratio (self);
+
+  if (crop_aspect_ratio < aspect_ratio)
+    {
+      self->crop_height = 0.7 * self->bbox_scaled.height;
+      self->crop_width = self->crop_height * crop_aspect_ratio;
+    }
+  else
+    {
+      self->crop_width = 0.7 * self->bbox_scaled.width;
+      self->crop_height = self->crop_width / crop_aspect_ratio;
+    }
+
+  self->crop_x = ((gdouble) self->bbox_scaled.width - self->crop_width) / 2.0;
+  self->crop_y = ((gdouble) self->bbox_scaled.height - self->crop_height) / 2.0;
+
+  cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.0);
+  cairo_rectangle (cr, self->crop_x, self->crop_y, self->crop_width, self->crop_height);
+  cairo_fill (cr);
+
+  cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
+  cairo_set_source_rgba (cr, 0.25, 0.507, 0.828, 1.0);
+
+  cairo_new_sub_path (cr);
+  cairo_arc (cr, self->crop_x - handle_offset, self->crop_y - handle_offset, handle_radius, 0.0, 2.0 * M_PI);
+  cairo_fill (cr);
+
+  cairo_new_sub_path (cr);
+  cairo_arc (cr,
+             self->crop_x + self->crop_width + handle_offset,
+             self->crop_y - handle_offset,
+             handle_radius,
+             0.0,
+             2.0 * M_PI);
+  cairo_fill (cr);
+
+  cairo_new_sub_path (cr);
+  cairo_arc (cr,
+             self->crop_x + self->crop_width + handle_offset,
+             self->crop_y + self->crop_height + handle_offset,
+             handle_radius,
+             0.0,
+             2.0 * M_PI);
+  cairo_fill (cr);
+
+  cairo_new_sub_path (cr);
+  cairo_arc (cr,
+             self->crop_x - handle_offset,
+             self->crop_y + self->crop_height + handle_offset,
+             handle_radius,
+             0.0,
+             2.0 * M_PI);
+  cairo_fill (cr);
+
+  cairo_set_source_rgba (cr, 0.8, 0.8, 0.8, 1.0);
+  cairo_set_line_width (cr, 0.5);
+  one_third_x = self->crop_width / 3.0;
+  one_third_y = self->crop_height / 3.0;
+
+  cairo_move_to (cr, self->crop_x + one_third_x, self->crop_y);
+  cairo_line_to (cr, self->crop_x + one_third_x, self->crop_y + self->crop_height);
+  cairo_stroke (cr);
+
+  cairo_move_to (cr, self->crop_x + 2.0 * one_third_x, self->crop_y);
+  cairo_line_to (cr, self->crop_x + 2.0 * one_third_x, self->crop_y + self->crop_height);
+  cairo_stroke (cr);
+
+  cairo_move_to (cr, self->crop_x, self->crop_y + one_third_y);
+  cairo_line_to (cr, self->crop_x + self->crop_width, self->crop_y + one_third_y);
+  cairo_stroke (cr);
+
+  cairo_move_to (cr, self->crop_x, self->crop_y + 2.0 * one_third_y);
+  cairo_line_to (cr, self->crop_x + self->crop_width, self->crop_y + 2.0 * one_third_y);
+  cairo_stroke (cr);
+
+  cairo_destroy (cr);
+}
+
+
+static void
+photos_tool_crop_changed (PhotosToolCrop *self)
+{
+  photos_tool_crop_surface_reset (self);
+  gtk_widget_queue_draw (self->view);
+}
+
+
+static void
+photos_tool_crop_size_allocate (PhotosToolCrop *self, GdkRectangle *allocation)
+{
+  photos_tool_crop_surface_create (self);
+  photos_tool_crop_surface_reset (self);
+}
+
+
+static void
+photos_tool_crop_activate (PhotosTool *tool, PhotosBaseItem *item, GeglGtkView *view)
+{
+  PhotosToolCrop *self = PHOTOS_TOOL_CROP (tool);
+
+  if (!photos_base_item_get_bbox_source (item, &self->bbox_source))
+    g_assert_not_reached ();
+
+  self->view = GTK_WIDGET (view);
+  g_signal_connect_swapped (self->view, "size-allocate", G_CALLBACK (photos_tool_crop_size_allocate), self);
+
+  photos_tool_crop_surface_create (self);
+  photos_tool_crop_surface_reset (self);
+}
+
+
+static void
+photos_tool_crop_draw (PhotosTool *tool, cairo_t *cr, GdkRectangle *rect)
+{
+  PhotosToolCrop *self = PHOTOS_TOOL_CROP (tool);
+  gdouble x;
+  gdouble y;
+
+  x = (gdouble) gegl_gtk_view_get_x (GEGL_GTK_VIEW (self->view));
+  x = -x;
+
+  y = (gdouble) gegl_gtk_view_get_y (GEGL_GTK_VIEW (self->view));
+  y = -y;
+
+  cairo_save (cr);
+  cairo_set_source_surface (cr, self->surface, x, y);
+  cairo_paint (cr);
+  cairo_restore (cr);
+}
+
+
+static GtkWidget *
+photos_tool_crop_get_widget (PhotosTool *tool)
+{
+  PhotosToolCrop *self = PHOTOS_TOOL_CROP (tool);
+  return self->combo_box;
+}
+
+
+static gboolean
+photos_tool_crop_left_click_event (PhotosTool *tool, GdkEventButton *event)
+{
+  return GDK_EVENT_PROPAGATE;
+}
+
+
+static gboolean
+photos_tool_crop_left_unclick_event (PhotosTool *tool, GdkEventButton *event)
+{
+  return GDK_EVENT_PROPAGATE;
+}
+
+
+static gboolean
+photos_tool_crop_motion_event (PhotosTool *tool, GdkEventMotion *event)
+{
+  PhotosToolCrop *self = PHOTOS_TOOL_CROP (tool);
+  GdkCursor *cursor = NULL;
+  GdkCursorType cursor_type = GDK_LEFT_PTR;
+  GdkDisplay *display;
+  GdkWindow *window;
+  const gdouble edge_fuzz = 12.0;
+  gdouble crop_x;
+  gdouble crop_y;
+  gdouble x;
+  gdouble y;
+
+  x = (gdouble) gegl_gtk_view_get_x (GEGL_GTK_VIEW (self->view));
+  y = (gdouble) gegl_gtk_view_get_y (GEGL_GTK_VIEW (self->view));
+  crop_x = self->crop_x - x;
+  crop_y = self->crop_y - y;
+
+  if (event->x > crop_x - edge_fuzz
+      && event->y > crop_y - edge_fuzz
+      && event->x < crop_x + self->crop_width + edge_fuzz
+      && event->y < crop_y + self->crop_height + edge_fuzz)
+    {
+      if (event->x < crop_x + edge_fuzz && event->y < crop_y + edge_fuzz)
+        cursor_type = GDK_TOP_LEFT_CORNER;
+      else if (event->x > crop_x + self->crop_width - edge_fuzz && event->y < crop_y + edge_fuzz)
+        cursor_type = GDK_TOP_RIGHT_CORNER;
+      else if (event->x > crop_x + self->crop_width - edge_fuzz && event->y > crop_y + self->crop_height - 
edge_fuzz)
+        cursor_type = GDK_BOTTOM_RIGHT_CORNER;
+      else if (event->x < crop_x + edge_fuzz && event->y > crop_y + self->crop_height - edge_fuzz)
+        cursor_type = GDK_BOTTOM_LEFT_CORNER;
+      else if (event->y < crop_y + edge_fuzz)
+        cursor_type = GDK_TOP_SIDE;
+      else if (event->x > crop_x + self->crop_width - edge_fuzz)
+        cursor_type = GDK_RIGHT_SIDE;
+      else if (event->y > crop_y + self->crop_height - edge_fuzz)
+        cursor_type = GDK_BOTTOM_SIDE;
+      else if (event->x < crop_x + edge_fuzz)
+        cursor_type = GDK_LEFT_SIDE;
+      else
+        cursor_type = GDK_FLEUR;
+    }
+
+  window = gtk_widget_get_window (self->view);
+  display = gdk_window_get_display (window);
+  cursor = gdk_cursor_new_for_display (display, cursor_type);
+  gdk_window_set_cursor (window, cursor);
+
+  g_object_unref (cursor);
+  return GDK_EVENT_STOP;
+}
+
+
+static void
+photos_tool_crop_dispose (GObject *object)
+{
+  PhotosToolCrop *self = PHOTOS_TOOL_CROP (object);
+
+  g_clear_object (&self->model);
+  g_clear_object (&self->combo_box);
+  g_clear_pointer (&self->surface, (GDestroyNotify) cairo_surface_destroy);
+
+  G_OBJECT_CLASS (photos_tool_crop_parent_class)->dispose (object);
+}
+
+
+static void
+photos_tool_crop_init (PhotosToolCrop *self)
+{
+  GtkCellRenderer *renderer;
+  guint i;
+
+  self->model = gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_DOUBLE, G_TYPE_UINT, G_TYPE_UINT);
+
+  for (i = 0; i < G_N_ELEMENTS (CONSTRAINTS); i++)
+    {
+      GtkTreeIter iter;
+
+      gtk_list_store_append (self->model, &iter);
+      gtk_list_store_set (self->model,
+                          &iter,
+                          CONSTRAINT_COLUMN_NAME, CONSTRAINTS[i].name,
+                          CONSTRAINT_COLUMN_ASPECT_RATIO, CONSTRAINTS[i].aspect_ratio,
+                          CONSTRAINT_COLUMN_BASIS_HEIGHT, CONSTRAINTS[i].basis_height,
+                          CONSTRAINT_COLUMN_BASIS_WIDTH, CONSTRAINTS[i].basis_width,
+                          -1);
+    }
+
+  self->combo_box = g_object_ref_sink (gtk_combo_box_new_with_model (GTK_TREE_MODEL (self->model)));
+  gtk_combo_box_set_active (GTK_COMBO_BOX (self->combo_box), 1);
+  g_signal_connect_swapped (self->combo_box, "changed", G_CALLBACK (photos_tool_crop_changed), self);
+
+  renderer = gtk_cell_renderer_text_new ();
+  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self->combo_box), renderer, TRUE);
+  gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (self->combo_box), renderer, "text", 
CONSTRAINT_COLUMN_NAME);
+}
+
+
+static void
+photos_tool_crop_class_init (PhotosToolCropClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+  PhotosToolClass *tool_class = PHOTOS_TOOL_CLASS (class);
+
+  tool_class->icon_name = PHOTOS_ICON_IMAGE_CROP_SYMBOLIC;
+  tool_class->name = _("Crop");
+
+  object_class->dispose = photos_tool_crop_dispose;
+  tool_class->activate = photos_tool_crop_activate;
+  tool_class->draw = photos_tool_crop_draw;
+  tool_class->get_widget = photos_tool_crop_get_widget;
+  tool_class->left_click_event = photos_tool_crop_left_click_event;
+  tool_class->left_unclick_event = photos_tool_crop_left_unclick_event;
+  tool_class->motion_event = photos_tool_crop_motion_event;
+}
diff --git a/src/photos-tool-crop.h b/src/photos-tool-crop.h
new file mode 100644
index 0000000..bd921d5
--- /dev/null
+++ b/src/photos-tool-crop.h
@@ -0,0 +1,45 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2015 Red Hat, Inc.
+ *
+ * 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef PHOTOS_TOOL_CROP_H
+#define PHOTOS_TOOL_CROP_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define PHOTOS_TYPE_TOOL_CROP (photos_tool_crop_get_type ())
+
+#define PHOTOS_TOOL_CROP(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+   PHOTOS_TYPE_TOOL_CROP, PhotosToolCrop))
+
+#define PHOTOS_IS_TOOL_CROP(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+   PHOTOS_TYPE_TOOL_CROP))
+
+typedef struct _PhotosToolCrop      PhotosToolCrop;
+typedef struct _PhotosToolCropClass PhotosToolCropClass;
+
+GType               photos_tool_crop_get_type           (void) G_GNUC_CONST;
+
+G_END_DECLS
+
+#endif /* PHOTOS_TOOL_CROP_H */
diff --git a/src/photos-utils.c b/src/photos-utils.c
index 80aab65..8aee7fb 100644
--- a/src/photos-utils.c
+++ b/src/photos-utils.c
@@ -46,6 +46,7 @@
 #include "photos-query.h"
 #include "photos-source.h"
 #include "photos-tool.h"
+#include "photos-tool-crop.h"
 #include "photos-tool-filters.h"
 #include "photos-tool-sharpen.h"
 #include "photos-tracker-queue.h"
@@ -538,6 +539,7 @@ photos_utils_ensure_builtins (void)
       g_type_ensure (PHOTOS_TYPE_OPERATION_INSTA_CURVE);
       g_type_ensure (PHOTOS_TYPE_OPERATION_INSTA_FILTER);
 
+      g_type_ensure (PHOTOS_TYPE_TOOL_CROP);
       g_type_ensure (PHOTOS_TYPE_TOOL_FILTERS);
       g_type_ensure (PHOTOS_TYPE_TOOL_SHARPEN);
 


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