[gnome-photos/wip/rishi/edit-mode: 22/27] Add PhotosToolCrop
- From: Debarshi Ray <debarshir src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-photos/wip/rishi/edit-mode: 22/27] Add PhotosToolCrop
- Date: Thu, 5 Nov 2015 19:17:39 +0000 (UTC)
commit b2f8f1d5cdfbb01768f67d6f44bcad1b2f10bdfd
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 | 805 ++++++++++++++++++++++++++++++++++++++++++++++++
src/photos-tool-crop.h | 45 +++
src/photos-utils.c | 62 ++++-
src/photos-utils.h | 15 +
6 files changed, 929 insertions(+), 1 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 192eeb2..0f02932 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -190,6 +190,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 5dc471a..b02a793 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..3baa7d4
--- /dev/null
+++ b/src/photos-tool-crop.c
@@ -0,0 +1,805 @@
+/*
+ * 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.
+ */
+
+/* Based on code from:
+ * + Shotwell
+ */
+
+
+#include "config.h"
+
+#include <math.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"
+
+
+typedef enum
+{
+ PHOTOS_TOOL_CROP_LOCATION_NONE,
+ PHOTOS_TOOL_CROP_LOCATION_BOTTOM_LEFT,
+ PHOTOS_TOOL_CROP_LOCATION_BOTTOM_RIGHT,
+ PHOTOS_TOOL_CROP_LOCATION_BOTTOM_SIDE,
+ PHOTOS_TOOL_CROP_LOCATION_CENTER,
+ PHOTOS_TOOL_CROP_LOCATION_LEFT_SIDE,
+ PHOTOS_TOOL_CROP_LOCATION_RIGHT_SIDE,
+ PHOTOS_TOOL_CROP_LOCATION_TOP_LEFT,
+ PHOTOS_TOOL_CROP_LOCATION_TOP_RIGHT,
+ PHOTOS_TOOL_CROP_LOCATION_TOP_SIDE
+} PhotosToolCropLocation;
+
+struct _PhotosToolCrop
+{
+ PhotosTool parent_instance;
+ GeglRectangle bbox_scaled;
+ GeglRectangle bbox_source;
+ GtkListStore *model;
+ GtkWidget *combo_box;
+ GtkWidget *view;
+ PhotosToolCropLocation location;
+ cairo_surface_t *surface;
+ gboolean grabbed;
+ gdouble crop_aspect_ratio;
+ gdouble crop_height;
+ gdouble crop_height0;
+ gdouble crop_width;
+ gdouble crop_width0;
+ gdouble crop_x;
+ gdouble crop_x0;
+ gdouble crop_y;
+ gdouble crop_y0;
+ gdouble event_x0;
+ gdouble event_y0;
+};
+
+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));
+
+
+typedef enum
+{
+ PHOTOS_TOOL_CROP_ASPECT_RATIO_ANY,
+ PHOTOS_TOOL_CROP_ASPECT_RATIO_BASIS,
+ PHOTOS_TOOL_CROP_ASPECT_RATIO_ORIGINAL,
+ PHOTOS_TOOL_CROP_ASPECT_RATIO_SCREEN
+} PhotosToolCropAspectRatioType;
+
+typedef struct _PhotosToolCropConstraint PhotosToolCropConstraint;
+
+struct _PhotosToolCropConstraint
+{
+ PhotosToolCropAspectRatioType aspect_ratio_type;
+ const gchar *name;
+ guint basis_height;
+ guint basis_width;
+};
+
+enum
+{
+ CONSTRAINT_COLUMN_ASPECT_RATIO = 0,
+ CONSTRAINT_COLUMN_NAME = 1,
+ CONSTRAINT_COLUMN_BASIS_HEIGHT = 2,
+ CONSTRAINT_COLUMN_BASIS_WIDTH = 3
+};
+
+static PhotosToolCropConstraint CONSTRAINTS[] =
+{
+ { PHOTOS_TOOL_CROP_ASPECT_RATIO_ANY, N_("Free"), 0, 0 },
+ { PHOTOS_TOOL_CROP_ASPECT_RATIO_ORIGINAL, N_("Original Size"), 0, 0 },
+ { PHOTOS_TOOL_CROP_ASPECT_RATIO_SCREEN, N_("Screen"), 0, 0 },
+ { PHOTOS_TOOL_CROP_ASPECT_RATIO_BASIS, N_("Square"), 1, 1 }
+};
+
+static const gdouble HANDLE_OFFSET = 3.0;
+static const gdouble HANDLE_RADIUS = 8.0;
+
+
+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));
+
+ switch (CONSTRAINTS[active].aspect_ratio_type)
+ {
+ case PHOTOS_TOOL_CROP_ASPECT_RATIO_ANY:
+ if (self->crop_height > 0.0 && self->crop_width > 0.0)
+ ret_val = self->crop_width / self->crop_height;
+ else
+ g_assert_not_reached ();
+ break;
+
+ case PHOTOS_TOOL_CROP_ASPECT_RATIO_BASIS:
+ ret_val = (gdouble) CONSTRAINTS[active].basis_width / CONSTRAINTS[active].basis_height;
+ break;
+
+ case PHOTOS_TOOL_CROP_ASPECT_RATIO_ORIGINAL:
+ ret_val = (gdouble) self->bbox_source.width / self->bbox_source.height;
+ break;
+
+ case PHOTOS_TOOL_CROP_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;
+ break;
+ }
+
+ default:
+ g_assert_not_reached ();
+ }
+
+ return ret_val;
+}
+
+
+static void
+photos_tool_crop_set_horiz_from_vert (PhotosToolCrop *self, gboolean centered, gboolean move_origin)
+{
+ gdouble crop_width_new;
+
+ crop_width_new = self->crop_height * self->crop_aspect_ratio;
+
+ if (move_origin)
+ {
+ gdouble x_offset;
+
+ if (centered)
+ x_offset = (crop_width_new - self->crop_width) / 2.0;
+ else
+ x_offset = crop_width_new - self->crop_width;
+
+ self->crop_x -= x_offset;
+ }
+
+ self->crop_width = crop_width_new;
+}
+
+
+static void
+photos_tool_crop_set_vert_from_horiz (PhotosToolCrop *self, gboolean centered, gboolean move_origin)
+{
+ gdouble crop_height_new;
+
+ crop_height_new = self->crop_width / self->crop_aspect_ratio;
+
+ if (move_origin)
+ {
+ gdouble y_offset;
+
+ if (centered)
+ y_offset = (crop_height_new - self->crop_height) / 2.0;
+ else
+ y_offset = crop_height_new - self->crop_height;
+
+ self->crop_y -= y_offset;
+ }
+
+ self->crop_height = crop_height_new;
+}
+
+
+static gboolean
+photos_tool_crop_set_crop (PhotosToolCrop *self, gdouble event_x, gdouble event_y)
+{
+ PhotosToolCropLocation location;
+ gboolean centered;
+ gboolean move_origin;
+ gboolean ret_val = TRUE;
+ gdouble crop_height_old;
+ gdouble crop_width_old;
+ gdouble crop_x_old;
+ gdouble crop_y_old;
+ gdouble delta_x;
+ gdouble delta_x_abs;
+ gdouble delta_y;
+ gdouble delta_y_abs;
+
+ crop_height_old = self->crop_height;
+ crop_width_old = self->crop_width;
+ crop_x_old = self->crop_x;
+ crop_y_old = self->crop_y;
+
+ delta_x = event_x - self->event_x0;
+ delta_x_abs = ABS (delta_x);
+
+ delta_y = event_y - self->event_y0;
+ delta_y_abs = ABS (delta_y);
+
+ switch (self->location)
+ {
+ case PHOTOS_TOOL_CROP_LOCATION_BOTTOM_SIDE:
+ case PHOTOS_TOOL_CROP_LOCATION_CENTER:
+ case PHOTOS_TOOL_CROP_LOCATION_LEFT_SIDE:
+ case PHOTOS_TOOL_CROP_LOCATION_RIGHT_SIDE:
+ case PHOTOS_TOOL_CROP_LOCATION_TOP_SIDE:
+ location = self->location;
+ centered = TRUE;
+ move_origin = TRUE;
+ break;
+
+ case PHOTOS_TOOL_CROP_LOCATION_BOTTOM_LEFT:
+ if (delta_x_abs > delta_y_abs)
+ {
+ location = PHOTOS_TOOL_CROP_LOCATION_LEFT_SIDE;
+ move_origin = FALSE;
+ }
+ else
+ {
+ location = PHOTOS_TOOL_CROP_LOCATION_BOTTOM_SIDE;
+ move_origin = TRUE;
+ }
+
+ centered = FALSE;
+ break;
+
+ case PHOTOS_TOOL_CROP_LOCATION_BOTTOM_RIGHT:
+ if (delta_x_abs > delta_y_abs)
+ location = PHOTOS_TOOL_CROP_LOCATION_RIGHT_SIDE;
+ else
+ location = PHOTOS_TOOL_CROP_LOCATION_BOTTOM_SIDE;
+
+ centered = FALSE;
+ move_origin = FALSE;
+ break;
+
+ case PHOTOS_TOOL_CROP_LOCATION_TOP_LEFT:
+ if (delta_x_abs > delta_y_abs)
+ location = PHOTOS_TOOL_CROP_LOCATION_LEFT_SIDE;
+ else
+ location = PHOTOS_TOOL_CROP_LOCATION_TOP_SIDE;
+
+ centered = FALSE;
+ move_origin = TRUE;
+ break;
+
+ case PHOTOS_TOOL_CROP_LOCATION_TOP_RIGHT:
+ if (delta_x_abs > delta_y_abs)
+ {
+ location = PHOTOS_TOOL_CROP_LOCATION_RIGHT_SIDE;
+ move_origin = TRUE;
+ }
+ else
+ {
+ location = PHOTOS_TOOL_CROP_LOCATION_TOP_SIDE;
+ move_origin = FALSE;
+ }
+
+ centered = FALSE;
+ break;
+
+ case PHOTOS_TOOL_CROP_LOCATION_NONE:
+ default:
+ g_assert_not_reached ();
+ break;
+ }
+
+ switch (location)
+ {
+ case PHOTOS_TOOL_CROP_LOCATION_BOTTOM_SIDE:
+ self->crop_height = self->crop_height0 + delta_y;
+ photos_tool_crop_set_horiz_from_vert (self, centered, move_origin);
+ break;
+
+ case PHOTOS_TOOL_CROP_LOCATION_CENTER:
+ self->crop_x = self->crop_x0 + delta_x;
+ self->crop_y = self->crop_y0 + delta_y;
+ break;
+
+ case PHOTOS_TOOL_CROP_LOCATION_LEFT_SIDE:
+ self->crop_width = self->crop_width0 - delta_x;
+ self->crop_x = self->crop_x0 + delta_x;
+ photos_tool_crop_set_vert_from_horiz (self, centered, move_origin);
+ break;
+
+ case PHOTOS_TOOL_CROP_LOCATION_RIGHT_SIDE:
+ self->crop_width = self->crop_width0 + delta_x;
+ photos_tool_crop_set_vert_from_horiz (self, centered, move_origin);
+ break;
+
+ case PHOTOS_TOOL_CROP_LOCATION_TOP_SIDE:
+ self->crop_height = self->crop_height0 - delta_y;
+ self->crop_y = self->crop_y0 + delta_y;
+ photos_tool_crop_set_horiz_from_vert (self, centered, move_origin);
+ break;
+
+ case PHOTOS_TOOL_CROP_LOCATION_NONE:
+ case PHOTOS_TOOL_CROP_LOCATION_BOTTOM_LEFT:
+ case PHOTOS_TOOL_CROP_LOCATION_BOTTOM_RIGHT:
+ case PHOTOS_TOOL_CROP_LOCATION_TOP_LEFT:
+ case PHOTOS_TOOL_CROP_LOCATION_TOP_RIGHT:
+ default:
+ g_assert_not_reached ();
+ break;
+ }
+
+ if (G_UNLIKELY (self->crop_x < 0.0
+ || self->crop_y < 0.0
+ || self->crop_x + self->crop_width > self->bbox_scaled.width
+ || self->crop_y + self->crop_height > self->bbox_scaled.height))
+ {
+ self->crop_height = crop_height_old;
+ self->crop_width = crop_width_old;
+ self->crop_x = crop_x_old;
+ self->crop_y = crop_y_old;
+ ret_val = FALSE;
+ }
+
+ return ret_val;
+}
+
+
+static void
+photos_tool_crop_set_cursor (PhotosToolCrop *self)
+{
+ GdkCursor *cursor = NULL;
+ GdkCursorType cursor_type;
+ GdkDisplay *display;
+ GdkWindow *window;
+
+ window = gtk_widget_get_window (self->view);
+
+ switch (self->location)
+ {
+ case PHOTOS_TOOL_CROP_LOCATION_NONE:
+ goto set_cursor;
+
+ case PHOTOS_TOOL_CROP_LOCATION_BOTTOM_LEFT:
+ cursor_type = GDK_BOTTOM_LEFT_CORNER;
+ break;
+
+ case PHOTOS_TOOL_CROP_LOCATION_BOTTOM_RIGHT:
+ cursor_type = GDK_BOTTOM_RIGHT_CORNER;
+ break;
+
+ case PHOTOS_TOOL_CROP_LOCATION_BOTTOM_SIDE:
+ cursor_type = GDK_BOTTOM_SIDE;
+ break;
+
+ case PHOTOS_TOOL_CROP_LOCATION_CENTER:
+ cursor_type = GDK_FLEUR;
+ break;
+
+ case PHOTOS_TOOL_CROP_LOCATION_LEFT_SIDE:
+ cursor_type = GDK_LEFT_SIDE;
+ break;
+
+ case PHOTOS_TOOL_CROP_LOCATION_RIGHT_SIDE:
+ cursor_type = GDK_RIGHT_SIDE;
+ break;
+
+ case PHOTOS_TOOL_CROP_LOCATION_TOP_LEFT:
+ cursor_type = GDK_TOP_LEFT_CORNER;
+ break;
+
+ case PHOTOS_TOOL_CROP_LOCATION_TOP_RIGHT:
+ cursor_type = GDK_TOP_RIGHT_CORNER;
+ break;
+
+ case PHOTOS_TOOL_CROP_LOCATION_TOP_SIDE:
+ cursor_type = GDK_TOP_SIDE;
+ break;
+
+ default:
+ g_assert_not_reached ();
+ break;
+ }
+
+ display = gdk_window_get_display (window);
+ cursor = gdk_cursor_new_for_display (display, cursor_type);
+
+ set_cursor:
+ gdk_window_set_cursor (window, cursor);
+ g_clear_object (&cursor);
+}
+
+
+static void
+photos_tool_crop_set_location (PhotosToolCrop *self, gdouble cur_x, gdouble cur_y)
+{
+ const gdouble edge_fuzz = 12.0;
+ gdouble crop_x;
+ gdouble crop_y;
+ gdouble x;
+ gdouble y;
+
+ self->location = PHOTOS_TOOL_CROP_LOCATION_NONE;
+
+ 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 (cur_x > crop_x - edge_fuzz
+ && cur_y > crop_y - edge_fuzz
+ && cur_x < crop_x + self->crop_width + edge_fuzz
+ && cur_y < crop_y + self->crop_height + edge_fuzz)
+ {
+ if (cur_x < crop_x + edge_fuzz && cur_y < crop_y + edge_fuzz)
+ self->location = PHOTOS_TOOL_CROP_LOCATION_TOP_LEFT;
+ else if (cur_x > crop_x + self->crop_width - edge_fuzz && cur_y < crop_y + edge_fuzz)
+ self->location = PHOTOS_TOOL_CROP_LOCATION_TOP_RIGHT;
+ else if (cur_x > crop_x + self->crop_width - edge_fuzz && cur_y > crop_y + self->crop_height -
edge_fuzz)
+ self->location = PHOTOS_TOOL_CROP_LOCATION_BOTTOM_RIGHT;
+ else if (cur_x < crop_x + edge_fuzz && cur_y > crop_y + self->crop_height - edge_fuzz)
+ self->location = PHOTOS_TOOL_CROP_LOCATION_BOTTOM_LEFT;
+ else if (cur_y < crop_y + edge_fuzz)
+ self->location = PHOTOS_TOOL_CROP_LOCATION_TOP_SIDE;
+ else if (cur_x > crop_x + self->crop_width - edge_fuzz)
+ self->location = PHOTOS_TOOL_CROP_LOCATION_RIGHT_SIDE;
+ else if (cur_y > crop_y + self->crop_height - edge_fuzz)
+ self->location = PHOTOS_TOOL_CROP_LOCATION_BOTTOM_SIDE;
+ else if (cur_x < crop_x + edge_fuzz)
+ self->location = PHOTOS_TOOL_CROP_LOCATION_LEFT_SIDE;
+ else
+ self->location = PHOTOS_TOOL_CROP_LOCATION_CENTER;
+ }
+}
+
+
+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_draw (PhotosToolCrop *self)
+{
+ cairo_t *cr;
+
+ 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);
+
+ 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);
+ photos_utils_draw_rectangle_handles (cr,
+ self->crop_x,
+ self->crop_y,
+ self->crop_width,
+ self->crop_height,
+ HANDLE_OFFSET,
+ HANDLE_RADIUS);
+
+ cairo_set_source_rgba (cr, 0.8, 0.8, 0.8, 1.0);
+ cairo_set_line_width (cr, 0.5);
+ photos_utils_draw_rectangle_thirds (cr, self->crop_x, self->crop_y, self->crop_width, self->crop_height);
+
+ cairo_destroy (cr);
+}
+
+
+static void
+photos_tool_crop_surface_change_constraint (PhotosToolCrop *self)
+{
+ gdouble crop_center_x;
+ gdouble crop_center_y;
+ gdouble old_area;
+
+ crop_center_x = self->crop_x + self->crop_width / 2.0;
+ crop_center_y = self->crop_y + self->crop_height / 2.0;
+ old_area = self->crop_height * self->crop_width;
+
+ self->crop_aspect_ratio = photos_tool_crop_calculate_aspect_ratio (self);
+ self->crop_height = sqrt (old_area / self->crop_aspect_ratio);
+ self->crop_width = sqrt (old_area * self->crop_aspect_ratio);
+ self->crop_x = crop_center_x - self->crop_width / 2.0;
+ self->crop_y = crop_center_y - self->crop_height / 2.0;
+
+ photos_tool_crop_surface_draw (self);
+}
+
+
+static void
+photos_tool_crop_surface_reset (PhotosToolCrop *self)
+{
+ gdouble aspect_ratio;
+
+ aspect_ratio = (gdouble) self->bbox_source.width / self->bbox_source.height;
+ self->crop_aspect_ratio = photos_tool_crop_calculate_aspect_ratio (self);
+
+ if (self->crop_aspect_ratio < aspect_ratio)
+ {
+ self->crop_height = 0.7 * self->bbox_scaled.height;
+ self->crop_width = self->crop_height * self->crop_aspect_ratio;
+ }
+ else
+ {
+ self->crop_width = 0.7 * self->bbox_scaled.width;
+ self->crop_height = self->crop_width / self->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;
+
+ photos_tool_crop_surface_draw (self);
+}
+
+
+static void
+photos_tool_crop_redraw_damaged_area (PhotosToolCrop *self)
+{
+ cairo_rectangle_int_t area;
+ cairo_region_t *region;
+ gdouble damage_offset = HANDLE_OFFSET + HANDLE_RADIUS;
+ gdouble x;
+ gdouble y;
+
+ x = (gdouble) gegl_gtk_view_get_x (GEGL_GTK_VIEW (self->view));
+ x = -x + self->crop_x - damage_offset;
+
+ y = (gdouble) gegl_gtk_view_get_y (GEGL_GTK_VIEW (self->view));
+ y = -y + self->crop_y - damage_offset;
+
+ area.height = (gint) (self->crop_height + 2 * damage_offset + 0.5) + 2;
+ area.width = (gint) (self->crop_width + 2 * damage_offset + 0.5) + 2;
+ area.x = (gint) (x + 0.5) - 1;
+ area.y = (gint) (y + 0.5) - 1;
+
+ region = cairo_region_create_rectangle (&area);
+ gtk_widget_queue_draw_region (self->view, region);
+ cairo_region_destroy (region);
+}
+
+static void
+photos_tool_crop_changed (PhotosToolCrop *self)
+{
+ gint active;
+
+ active = gtk_combo_box_get_active (GTK_COMBO_BOX (self->combo_box));
+ if (CONSTRAINTS[active].aspect_ratio_type == PHOTOS_TOOL_CROP_ASPECT_RATIO_ANY)
+ return;
+
+ photos_tool_crop_redraw_damaged_area (self);
+ photos_tool_crop_surface_change_constraint (self);
+ photos_tool_crop_redraw_damaged_area (self);
+}
+
+
+static void
+photos_tool_crop_size_allocate (PhotosToolCrop *self, GdkRectangle *allocation)
+{
+ gdouble crop_height_ratio;
+ gdouble crop_width_ratio;
+ gdouble crop_x_ratio;
+ gdouble crop_y_ratio;
+
+ crop_height_ratio = self->crop_height / (gdouble) self->bbox_scaled.height;
+ crop_width_ratio = self->crop_width / (gdouble) self->bbox_scaled.width;
+ crop_x_ratio = self->crop_x / (gdouble) self->bbox_scaled.width;
+ crop_y_ratio = self->crop_y / (gdouble) self->bbox_scaled.height;
+
+ photos_tool_crop_surface_create (self);
+
+ self->crop_height = crop_height_ratio * (gdouble) self->bbox_scaled.height;
+ self->crop_width = crop_width_ratio * (gdouble) self->bbox_scaled.width;
+ self->crop_x = crop_x_ratio * (gdouble) self->bbox_scaled.width;
+ self->crop_y = crop_y_ratio * (gdouble) self->bbox_scaled.height;
+
+ photos_tool_crop_surface_draw (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)
+{
+ PhotosToolCrop *self = PHOTOS_TOOL_CROP (tool);
+
+ self->grabbed = TRUE;
+ self->crop_height0 = self->crop_height;
+ self->crop_width0 = self->crop_width;
+ self->crop_x0 = self->crop_x;
+ self->crop_y0 = self->crop_y;
+ self->event_x0 = event->x;
+ self->event_y0 = event->y;
+
+ return GDK_EVENT_PROPAGATE;
+}
+
+
+static gboolean
+photos_tool_crop_left_unclick_event (PhotosTool *tool, GdkEventButton *event)
+{
+ PhotosToolCrop *self = PHOTOS_TOOL_CROP (tool);
+
+ self->grabbed = FALSE;
+ return GDK_EVENT_PROPAGATE;
+}
+
+
+static gboolean
+photos_tool_crop_motion_event (PhotosTool *tool, GdkEventMotion *event)
+{
+ PhotosToolCrop *self = PHOTOS_TOOL_CROP (tool);
+
+ if (self->grabbed)
+ {
+ if (self->location != PHOTOS_TOOL_CROP_LOCATION_NONE)
+ {
+ photos_tool_crop_redraw_damaged_area (self);
+ if (photos_tool_crop_set_crop (self, event->x, event->y))
+ {
+ photos_tool_crop_surface_draw (self);
+ photos_tool_crop_redraw_damaged_area (self);
+ }
+ }
+ }
+ else
+ {
+ photos_tool_crop_set_location (self, event->x, event->y);
+ photos_tool_crop_set_cursor (self);
+ }
+
+ 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_INT, G_TYPE_STRING, 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_ASPECT_RATIO, CONSTRAINTS[i].aspect_ratio_type,
+ CONSTRAINT_COLUMN_NAME, CONSTRAINTS[i].name,
+ 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 b8e02ad..a5140b3 100644
--- a/src/photos-utils.c
+++ b/src/photos-utils.c
@@ -29,7 +29,6 @@
#include <string.h>
-#include <cairo.h>
#include <glib.h>
#include <libgnome-desktop/gnome-desktop-thumbnail.h>
#include <tracker-sparql.h>
@@ -46,6 +45,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-collections-controller.h"
@@ -509,6 +509,65 @@ photos_utils_downscale_pixbuf_for_scale (GdkPixbuf *pixbuf, gint size, gint scal
void
+photos_utils_draw_rectangle_handles (cairo_t *cr,
+ gdouble x,
+ gdouble y,
+ gdouble width,
+ gdouble height,
+ gdouble offset,
+ gdouble radius)
+{
+ cairo_save (cr);
+
+ cairo_new_sub_path (cr);
+ cairo_arc (cr, x - offset, y - offset, radius, 0.0, 2.0 * M_PI);
+ cairo_fill (cr);
+
+ cairo_new_sub_path (cr);
+ cairo_arc (cr, x + width + offset, y - offset, radius, 0.0, 2.0 * M_PI);
+ cairo_fill (cr);
+
+ cairo_new_sub_path (cr);
+ cairo_arc (cr, x + width + offset, y + height + offset, radius, 0.0, 2.0 * M_PI);
+ cairo_fill (cr);
+
+ cairo_new_sub_path (cr);
+ cairo_arc (cr, x - offset, y + height + offset, radius, 0.0, 2.0 * M_PI);
+ cairo_fill (cr);
+
+ cairo_restore (cr);
+}
+
+
+void
+photos_utils_draw_rectangle_thirds (cairo_t *cr, gdouble x, gdouble y, gdouble width, gdouble height)
+{
+ const gdouble one_third_x = width / 3.0;
+ const gdouble one_third_y = height / 3.0;
+
+ cairo_save (cr);
+
+ cairo_move_to (cr, x + one_third_x, y);
+ cairo_line_to (cr, x + one_third_x, y + height);
+ cairo_stroke (cr);
+
+ cairo_move_to (cr, x + 2.0 * one_third_x, y);
+ cairo_line_to (cr, x + 2.0 * one_third_x, y + height);
+ cairo_stroke (cr);
+
+ cairo_move_to (cr, x, y + one_third_y);
+ cairo_line_to (cr, x + width, y + one_third_y);
+ cairo_stroke (cr);
+
+ cairo_move_to (cr, x, y + 2.0 * one_third_y);
+ cairo_line_to (cr, x + width, y + 2.0 * one_third_y);
+ cairo_stroke (cr);
+
+ cairo_restore (cr);
+}
+
+
+void
photos_utils_ensure_builtins (void)
{
static gsize once_init_value = 0;
@@ -526,6 +585,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);
diff --git a/src/photos-utils.h b/src/photos-utils.h
index f124ae2..82f699f 100644
--- a/src/photos-utils.h
+++ b/src/photos-utils.h
@@ -27,6 +27,7 @@
#ifndef PHOTOS_UTILS_H
#define PHOTOS_UTILS_H
+#include <cairo.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>
#include <gio/gio.h>
@@ -66,6 +67,20 @@ GIcon *photos_utils_get_icon_from_cursor (TrackerSparqlCursor *
GdkPixbuf *photos_utils_downscale_pixbuf_for_scale (GdkPixbuf *pixbuf, gint size, gint scale);
+void photos_utils_draw_rectangle_handles (cairo_t *cr,
+ gdouble x,
+ gdouble y,
+ gdouble width,
+ gdouble height,
+ gdouble offset,
+ gdouble radius);
+
+void photos_utils_draw_rectangle_thirds (cairo_t *cr,
+ gdouble x,
+ gdouble y,
+ gdouble width,
+ gdouble height);
+
void photos_utils_ensure_builtins (void);
void photos_utils_ensure_extension_points (void);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]