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



commit 33be662354e7d420c68a76c2ab935e95322f13b4
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 |  385 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/photos-tool-crop.h |   45 ++++++
 src/photos-utils.c     |    2 +
 5 files changed, 435 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..7995527
--- /dev/null
+++ b/src/photos-tool-crop.c
@@ -0,0 +1,385 @@
+/*
+ * 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;
+};
+
+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 crop_height;
+  gdouble crop_width;
+  gdouble crop_x;
+  gdouble crop_y;
+  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)
+    {
+      crop_height = 0.7 * self->bbox_scaled.height;
+      crop_width = crop_height * crop_aspect_ratio;
+    }
+  else
+    {
+      crop_width = 0.7 * self->bbox_scaled.width;
+      crop_height = crop_width / crop_aspect_ratio;
+    }
+
+  crop_x = ((gdouble) self->bbox_scaled.width - crop_width) / 2.0;
+  crop_y = ((gdouble) self->bbox_scaled.height - crop_height) / 2.0;
+
+  cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.0);
+  cairo_rectangle (cr, crop_x, crop_y, crop_width, 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, crop_x - handle_offset, crop_y - handle_offset, handle_radius, 0.0, 2.0 * M_PI);
+  cairo_fill (cr);
+
+  cairo_new_sub_path (cr);
+  cairo_arc (cr, crop_x + crop_width + handle_offset, crop_y - handle_offset, handle_radius, 0.0, 2.0 * 
M_PI);
+  cairo_fill (cr);
+
+  cairo_new_sub_path (cr);
+  cairo_arc (cr,
+             crop_x + crop_width + handle_offset,
+             crop_y + crop_height + handle_offset,
+             handle_radius,
+             0.0,
+             2.0 * M_PI);
+  cairo_fill (cr);
+
+  cairo_new_sub_path (cr);
+  cairo_arc (cr, crop_x - handle_offset, crop_y + 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 = crop_width / 3.0;
+  one_third_y = crop_height / 3.0;
+
+  cairo_move_to (cr, crop_x + one_third_x, crop_y);
+  cairo_line_to (cr, crop_x + one_third_x, crop_y + crop_height);
+  cairo_stroke (cr);
+
+  cairo_move_to (cr, crop_x + 2.0 * one_third_x, crop_y);
+  cairo_line_to (cr, crop_x + 2.0 * one_third_x, crop_y + crop_height);
+  cairo_stroke (cr);
+
+  cairo_move_to (cr, crop_x, crop_y + one_third_y);
+  cairo_line_to (cr, crop_x + crop_width, crop_y + one_third_y);
+  cairo_stroke (cr);
+
+  cairo_move_to (cr, crop_x, crop_y + 2.0 * one_third_y);
+  cairo_line_to (cr, crop_x + crop_width, 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 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 const gchar *
+photos_tool_crop_get_icon_name (PhotosTool *tool)
+{
+  return PHOTOS_ICON_IMAGE_CROP_SYMBOLIC;
+}
+
+
+static const gchar *
+photos_tool_crop_get_name (PhotosTool *tool)
+{
+  return _("Crop");
+}
+
+
+static GtkWidget *
+photos_tool_crop_get_widget (PhotosTool *tool)
+{
+  PhotosToolCrop *self = PHOTOS_TOOL_CROP (tool);
+  return self->combo_box;
+}
+
+
+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);
+
+  object_class->dispose = photos_tool_crop_dispose;
+  tool_class->activate = photos_tool_crop_activate;
+  tool_class->draw = photos_tool_crop_draw;
+  tool_class->get_icon_name = photos_tool_crop_get_icon_name;
+  tool_class->get_name = photos_tool_crop_get_name;
+  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;
+}
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]