edit: Scale cropping selection area with image

This commit is contained in:
Sophie Herold 2024-09-17 20:13:39 +02:00
parent 6867e0b13f
commit 5b184e6be9
8 changed files with 133 additions and 56 deletions

View file

@ -1,3 +1,20 @@
// Copyright (c) 2024 Sophie Herold
//
// 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 3 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, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-or-later
pub mod crop;
pub mod crop_selection;

View file

@ -1,3 +1,20 @@
// Copyright (c) 2024 Sophie Herold
//
// 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 3 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, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-or-later
use std::cell::{Cell, OnceCell};
use adw::prelude::*;
@ -103,12 +120,11 @@ mod imp {
obj.child().allocate(width, height, baseline, None);
// TODO: Round wrong
self.selection.set_size(
image.image_rendering_x() as i32,
image.image_rendering_y() as i32,
image.image_displayed_width().round() as i32,
image.image_displayed_height().round() as i32,
image.image_rendering_width() as i32,
image.image_rendering_height() as i32,
);
}

View file

@ -56,10 +56,10 @@
<property name="orientation">vertical</property>
<child>
<object class="GtkOverlay">
<property name="margin-top">12</property>
<property name="margin-end">12</property>
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<property name="margin-top">18</property>
<property name="margin-end">18</property>
<property name="margin-bottom">18</property>
<property name="margin-start">18</property>
<child>
<object class="LpImage" id="image">
</object>

View file

@ -1,3 +1,20 @@
// Copyright (c) 2024 Sophie Herold
//
// 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 3 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, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-or-later
use std::cell::{Cell, OnceCell};
use std::marker::PhantomData;
@ -156,6 +173,15 @@ mod imp {
total_width: PhantomData<i32>,
#[property(get=Self::total_height, set=Self::set_total_height)]
total_height: PhantomData<i32>,
#[property(get, set=Self::set_crop_x)]
crop_x: Cell<f32>,
#[property(get, set=Self::set_crop_y)]
crop_y: Cell<f32>,
#[property(get, set=Self::set_crop_width)]
crop_width: Cell<f32>,
#[property(get, set=Self::set_crop_height)]
crop_height: Cell<f32>,
}
#[glib::object_subclass]
@ -637,50 +663,54 @@ mod imp {
fn set_crop(&self, rect: graphene::Rect) {
let rect = rect.round_extents();
self.set_crop_x(rect.x() as i32);
self.set_crop_y(rect.y() as i32);
self.set_crop_width(rect.width() as i32);
self.set_crop_height(rect.height() as i32);
self.set_crop_x(rect.x());
self.set_crop_y(rect.y());
self.set_crop_width(rect.width());
self.set_crop_height(rect.height());
}
/// Set x coordinate of crop selection rectangle origin
pub(super) fn set_crop_x(&self, x: i32) {
if x < 0 {
pub(super) fn set_crop_x(&self, x: f32) {
if x < 0. {
eprintln!("Tried to set x coordinate to {x}");
return;
}
self.space_top_left.set_width_request(x);
self.crop_x.set(x);
self.space_top_left.set_width_request(x.round() as i32);
}
/// Set y coordinate of crop selection rectangle origin
pub(super) fn set_crop_y(&self, y: i32) {
if y < 0 {
pub(super) fn set_crop_y(&self, y: f32) {
if y < 0. {
eprintln!("Tried to set y coordinate to {y}");
return;
}
self.space_top_left.set_height_request(y);
self.crop_y.set(y);
self.space_top_left.set_height_request(y.round() as i32);
}
/// Set width of crop selection rectangle
pub(super) fn set_crop_width(&self, width: i32) {
if width < 0 {
pub(super) fn set_crop_width(&self, width: f32) {
if width < 0. {
eprintln!("Tried to set width to {width}");
return;
}
self.selection.set_width_request(width);
self.crop_width.set(width);
self.selection.set_width_request(width.round() as i32);
}
/// Set height of crop selection rectangle
pub(super) fn set_crop_height(&self, height: i32) {
if height < 0 {
pub(super) fn set_crop_height(&self, height: f32) {
if height < 0. {
eprintln!("Tried to set height to {height}");
return;
}
self.selection.set_height_request(height);
self.crop_height.set(height);
self.selection.set_height_request(height.round() as i32);
}
/// Width of area in which the crop selection can exist
@ -734,13 +764,20 @@ impl LpEditCropSelection {
self.set_total_width(width);
self.set_total_height(height);
imp.set_crop_x(0);
imp.set_crop_y(0);
imp.set_crop_width(width);
imp.set_crop_height(height);
imp.set_crop_x(0.);
imp.set_crop_y(0.);
imp.set_crop_width(width as f32);
imp.set_crop_height(height as f32);
}
pub fn set_size(&self, x: i32, y: i32, width: i32, height: i32) {
let ratio = width as f32 / self.total_width() as f32;
self.set_crop_x(self.crop_x() * ratio);
self.set_crop_y(self.crop_y() * ratio);
self.set_crop_width(self.crop_width() * ratio);
self.set_crop_height(self.crop_height() * ratio);
self.set_margin_start(x);
self.set_margin_top(y);
self.set_total_width(width);

View file

@ -29,6 +29,19 @@ impl imp::LpImage {
}
}
/// Image width with current zoom factor and rotation
///
/// During rotation it is an interpolated size that does not
/// represent the actual size. The size returned might well be
/// larger than what can actually be displayed within the widget.
pub fn image_displayed_width(&self) -> f64 {
self.image_width(self.applicable_zoom())
}
pub fn image_displayed_height(&self) -> f64 {
self.image_height(self.applicable_zoom())
}
pub fn image_width(&self, zoom: f64) -> f64 {
let (width, height) = self.original_dimensions();
@ -94,19 +107,4 @@ impl LpImage {
self.imp().original_dimensions()
}
}
/// Image width with current zoom factor and rotation
///
/// During rotation it is an interpolated size that does not
/// represent the actual size. The size returned might well be
/// larger than what can actually be displayed within the widget.
pub fn image_displayed_width(&self) -> f64 {
let imp = self.imp();
imp.image_width(imp.applicable_zoom())
}
pub fn image_displayed_height(&self) -> f64 {
let imp = self.imp();
imp.image_height(imp.applicable_zoom())
}
}

View file

@ -60,8 +60,8 @@ impl WidgetImpl for imp::LpImage {
let obj = self.obj();
let widget_width = self.widget_width();
let widget_height = self.widget_height();
let display_width = obj.image_displayed_width();
let display_height = obj.image_displayed_height();
let display_width = self.image_displayed_width();
let display_height = self.image_displayed_height();
let applicable_zoom = self.applicable_zoom();
@ -338,14 +338,24 @@ impl LpImage {
pub fn image_rendering_x(&self) -> f64 {
let imp = self.imp();
let widget_width = imp.widget_width();
let display_width = self.image_displayed_width();
let display_width = imp.image_displayed_width();
imp.round_f64(f64::max((widget_width - display_width) / 2.0, 0.))
}
pub fn image_rendering_y(&self) -> f64 {
let imp = self.imp();
let widget_height = imp.widget_height();
let display_height = self.image_displayed_height();
let display_height = imp.image_displayed_height();
imp.round_f64(f64::max((widget_height - display_height) / 2.0, 0.))
}
pub fn image_rendering_width(&self) -> f64 {
let imp = self.imp();
imp.round_f64(imp.image_displayed_width())
}
pub fn image_rendering_height(&self) -> f64 {
let imp = self.imp();
imp.round_f64(imp.image_displayed_height())
}
}

View file

@ -73,7 +73,7 @@ impl imp::LpImage {
let hadjustment = obj.hadj();
// round to application pixels to avoid tiny rounding errors from zoom
let content_width = self.round_f64(obj.image_displayed_width());
let content_width = self.round_f64(self.image_displayed_width());
let widget_width = self.widget_width();
hadjustment.configure(
@ -93,7 +93,7 @@ impl imp::LpImage {
let vadjustment = obj.vadj();
// round to application pixels to avoid tiny rounding errors from zoom
let content_height = self.round_f64(obj.image_displayed_height());
let content_height = self.round_f64(self.image_displayed_height());
let widget_height = self.widget_height();
vadjustment.configure(
@ -132,13 +132,11 @@ impl imp::LpImage {
}
pub fn max_hadjustment_value(&self) -> f64 {
let obj = self.obj();
f64::max(obj.image_displayed_width() - self.widget_width(), 0.)
f64::max(self.image_displayed_width() - self.widget_width(), 0.)
}
pub fn max_vadjustment_value(&self) -> f64 {
let obj = self.obj();
f64::max(obj.image_displayed_height() - self.widget_height(), 0.)
f64::max(self.image_displayed_height() - self.widget_height(), 0.)
}
}

View file

@ -104,15 +104,16 @@ impl imp::LpImage {
}
fn horizontal_bar(&self) -> f64 {
let obj = self.obj();
f64::max(0., (self.widget_width() - obj.image_displayed_width()) / 2.)
f64::max(
0.,
(self.widget_width() - self.image_displayed_width()) / 2.,
)
}
fn vertical_bar(&self) -> f64 {
let obj = self.obj();
f64::max(
0.,
(self.widget_height() - obj.image_displayed_height()) / 2.,
(self.widget_height() - self.image_displayed_height()) / 2.,
)
}