From e12ce18209e90b298ff150458f716aae47a3bbf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Canek=20Pel=C3=A1ez=20Vald=C3=A9s?= Date: Sat, 8 Apr 2017 16:43:17 -0500 Subject: [PATCH] Start photo zooming operations. --- data/gqpe.ui | 108 ++++++++++++++++++++++++++++++------ src/application-window.vala | 50 ++++++++++------- src/photograph.vala | 34 +++++++----- 3 files changed, 141 insertions(+), 51 deletions(-) diff --git a/data/gqpe.ui b/data/gqpe.ui index 3d26f88..7071de7 100644 --- a/data/gqpe.ui +++ b/data/gqpe.ui @@ -111,6 +111,77 @@ + + + True + False + 0 + horizontal + + + True + False + Zoom in + + + + True + False + zoom-in-symbolic + 1 + + + + + start + + + + + True + False + Zoom out + + + + True + False + zoom-out-symbolic + 1 + + + + + start + + + + + True + False + Zoom fit best + + + + True + False + zoom-fit-best-symbolic + 1 + + + + + start + + + + + True @@ -158,19 +229,29 @@ - - 500 - 500 + True False - 6 - True - 420 - start-here-symbolic - - + GTK_SHADOW_NONE + 500 + 500 + automatic + automatic + + + 500 + 500 + True + False + 6 + True + 420 + start-here-symbolic + + + @@ -260,11 +341,6 @@ - - - - - False diff --git a/src/application-window.vala b/src/application-window.vala index 8f26e3c..15f9b11 100644 --- a/src/application-window.vala +++ b/src/application-window.vala @@ -71,6 +71,9 @@ namespace GQPE { /* The save button. */ [GtkChild] private Gtk.Button save; + /* The image scroll. */ + [GtkChild] + private Gtk.ScrolledWindow image_scroll; /* The image label. */ [GtkChild] private Gtk.Label label; @@ -174,6 +177,33 @@ namespace GQPE { rotate(Rotate.RIGHT); } + /** + * Callback for zoom in. + */ + [GtkCallback] + public void on_zoom_in_clicked() { + } + + /** + * Callback for zoom out. + */ + [GtkCallback] + public void on_zoom_out_clicked() { + } + + /** + * Callback for zoom fit. + */ + [GtkCallback] + public void on_zoom_fit_clicked() { + double w = image_scroll.get_allocated_width(); + double h = image_scroll.get_allocated_height(); + if (w <= 0.0 || h <= 0.0) + return; + photograph.resize(w, h); + image.set_from_pixbuf(photograph.pixbuf); + } + /** * Callback for save button clicked. */ @@ -210,26 +240,6 @@ namespace GQPE { enable_ui(Item.SAVE); } - /** - * Callback for image resized. - */ - [GtkCallback] - public void on_image_resize() { - double w = image.get_allocated_width(); - double h = image.get_allocated_height(); - if (w <= 0.0 || h <= 0.0) - return; - double W = photograph.pixbuf.width; - double H = photograph.pixbuf.height; - double s1 = w / W; - double s2 = h / H; - if (H * s1 <= h) - photograph.scale(s1); - else - photograph.scale(s2); - - } - /** * Opens an array of files. * @param files the array of files. diff --git a/src/photograph.vala b/src/photograph.vala index 6a041b8..1e83695 100644 --- a/src/photograph.vala +++ b/src/photograph.vala @@ -68,7 +68,7 @@ namespace GQPE { /* The photograph metadata. */ private GExiv2.Metadata metadata; /* Private the original pixbuf. */ - private Gdk.Pixbuf pixbuf original; + private Gdk.Pixbuf original; /** * Creates a new photograph. @@ -86,10 +86,10 @@ namespace GQPE { if (original != null) return; + original = new Gdk.Pixbuf.from_file(file.get_path()); metadata = new GExiv2.Metadata(); metadata.open_path(file.get_path()); - original = new Gdk.Pixbuf.from_file(file.get_path()); if (metadata.has_tag(Tag.ORIENTATION)) { var rot = Gdk.PixbufRotation.NONE; switch (metadata.get_tag_long(Tag.ORIENTATION)) { @@ -110,28 +110,32 @@ namespace GQPE { break; } if (rot != Gdk.PixbufRotation.NONE) - pixbuf = original.rotate_simple(rot); + original = original.rotate_simple(rot); } - double scale = 500.0 / double.max(pixbuf.width, pixbuf.height); - pixbuf = pixbuf.scale_simple((int)(pixbuf.width*scale), - (int)(pixbuf.height*scale), - Gdk.InterpType.BILINEAR); + double scale = 490.0 / double.max(original.width, original.height); + pixbuf = original.scale_simple((int)(original.width*scale), + (int)(original.height*scale), + Gdk.InterpType.BILINEAR); album = (metadata.has_tag(Tag.SUBJECT)) ? metadata.get_tag_string(Tag.SUBJECT).strip() : ""; caption = (metadata.has_tag(Tag.CAPTION)) ? metadata.get_tag_string(Tag.CAPTION).strip() : ""; comment = (metadata.has_tag(Tag.DESCRIPTION)) ? metadata.get_tag_string(Tag.DESCRIPTION).strip() : ""; - - album.strip(); - caption.strip(); - comment.strip(); } - public void scale(double scale) { - pixbuf = pixbuf.scale_simple((int)(pixbuf.width*scale), - (int)(pixbuf.height*scale), - Gdk.InterpType.BILINEAR); + /** + * Resizes the photograph so it fills the given width and height. + */ + public void resize(double w, double h) { + double W = original.width; + double H = original.height; + double s1 = w / W; + double s2 = h / H; + double scale = (H * s1 <= h) ? s1 : s2; + pixbuf = original.scale_simple((int)(original.width*scale), + (int)(original.height*scale), + Gdk.InterpType.BILINEAR); } /** -- GitLab