Loupe/src/widgets/edit/crop.rs

345 lines
10 KiB
Rust
Raw Normal View History

// Copyright (c) 2024-2025 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
//! Shows an resizable cropping selection
use std::cell::OnceCell;
2024-09-15 21:06:23 +02:00
use adw::prelude::*;
use adw::subclass::prelude::*;
use adw::{glib, gtk};
2024-12-18 13:36:20 +01:00
use glycin::Operation;
2024-09-15 21:06:23 +02:00
2024-12-18 13:36:20 +01:00
use crate::editing::preview::EditingError;
use crate::util::gettext::*;
use crate::util::root::ParentWindow;
use crate::util::ErrorType;
2024-09-15 21:06:23 +02:00
use crate::widgets::edit::LpEditCropSelection;
2025-01-22 15:41:56 +01:00
use crate::widgets::LpImage;
2024-09-15 21:06:23 +02:00
/// Aspect ratio modes that can be selected
#[derive(Debug, Clone, Copy, Default, glib::Enum, glib::Variant)]
2024-09-15 21:06:23 +02:00
#[enum_type(name = "LpAspectRatio")]
pub enum LpAspectRatio {
#[default]
Free,
Original,
/// 1.0
Square,
/// 1.25
R5to4,
/// 1.33
R4to3,
/// 1.5
R3to2,
/// 1.77
R16to9,
}
#[derive(Debug, Clone, Copy, Default, glib::Enum, glib::Variant)]
2024-09-15 21:06:23 +02:00
#[enum_type(name = "LpOrientation")]
pub enum LpOrientation {
#[default]
Landscape,
Portrait,
}
mod imp {
2025-01-22 15:41:56 +01:00
use gtk::gdk;
2024-09-15 21:06:23 +02:00
use super::*;
2024-12-18 13:36:20 +01:00
2024-09-15 21:06:23 +02:00
#[derive(Debug, Default, gtk::CompositeTemplate, glib::Properties)]
#[properties(wrapper_type = super::LpEditCrop)]
#[template(file = "crop.ui")]
pub struct LpEditCrop {
#[template_child]
pub(super) image: TemplateChild<LpImage>,
2024-09-15 21:06:23 +02:00
#[template_child]
pub(super) selection: TemplateChild<LpEditCropSelection>,
#[property(get, construct_only)]
original_image: OnceCell<LpImage>,
#[property(get, set)]
child: OnceCell<gtk::Widget>,
}
#[glib::object_subclass]
impl ObjectSubclass for LpEditCrop {
const NAME: &'static str = "LpEditCrop";
type Type = super::LpEditCrop;
type ParentType = adw::Bin;
2024-09-15 21:06:23 +02:00
fn class_init(klass: &mut Self::Class) {
Self::bind_template(klass);
2024-12-21 21:39:49 +01:00
klass.install_action("edit-crop.mirror-horizontally", None, |obj, _, _| {
obj.imp().apply_mirror_horizontally();
});
klass.install_action("edit-crop.mirror-vertically", None, |obj, _, _| {
obj.imp().apply_mirror_vertically()
});
klass.install_action("edit-crop.rotate-cw", None, |obj, _, _| {
2024-12-21 21:39:49 +01:00
obj.imp().apply_rotate_cw()
});
klass.install_action("edit-crop.rotate-ccw", None, |obj, _, _| {
2024-12-21 21:39:49 +01:00
obj.imp().apply_rotate_ccw()
});
klass.install_action("edit-crop.reset", None, |obj, _, _| {
2024-12-21 21:39:49 +01:00
obj.handle_error(obj.imp().apply_reset())
});
klass.install_action("edit-crop.apply-crop", None, |obj, _, _| {
2024-12-21 21:39:49 +01:00
obj.imp().apply_crop()
});
klass.add_shortcut(
&gtk::Shortcut::builder()
.action(&gtk::NamedAction::new("edit-crop.mirror-horizontally"))
.trigger(&gtk::KeyvalTrigger::new(
gdk::Key::H,
gdk::ModifierType::NO_MODIFIER_MASK,
))
.build(),
);
klass.add_shortcut(
&gtk::Shortcut::builder()
.action(&gtk::NamedAction::new("edit-crop.mirror-vertically"))
.trigger(&gtk::KeyvalTrigger::new(
gdk::Key::V,
gdk::ModifierType::NO_MODIFIER_MASK,
))
.build(),
);
klass.add_shortcut(
&gtk::Shortcut::builder()
.action(&gtk::NamedAction::new("edit-crop.rotate-cw"))
.trigger(&gtk::KeyvalTrigger::new(
gdk::Key::R,
gdk::ModifierType::CONTROL_MASK,
))
.build(),
);
klass.add_shortcut(
&gtk::Shortcut::builder()
.action(&gtk::NamedAction::new("edit-crop.rotate-ccw"))
.trigger(&gtk::KeyvalTrigger::new(
gdk::Key::R,
gdk::ModifierType::CONTROL_MASK.union(gdk::ModifierType::SHIFT_MASK),
))
.build(),
);
klass.add_shortcut(
&gtk::Shortcut::builder()
.action(&gtk::NamedAction::new("edit-crop.apply-crop"))
.trigger(&gtk::KeyvalTrigger::new(
gdk::Key::Return,
gdk::ModifierType::CONTROL_MASK,
))
.build(),
);
2024-09-15 21:06:23 +02:00
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
#[glib::derived_properties]
impl ObjectImpl for LpEditCrop {
fn constructed(&self) {
self.parent_constructed();
let obj = &*self.obj();
obj.child().set_parent(obj);
self.image.duplicate_from(&obj.original_image());
let actions = gio::SimpleActionGroup::new();
actions.add_action(&gio::PropertyAction::new(
"aspect-ratio",
&*self.selection,
"aspect_ratio",
));
actions.add_action(&gio::PropertyAction::new(
"orientation",
&*self.selection,
"orientation",
));
obj.insert_action_group("edit-crop", Some(&actions));
obj.action_set_enabled("edit-crop.reset", false);
self.selection.connect_cropped_notify(glib::clone!(
#[weak]
obj,
move |_| {
obj.action_set_enabled("edit-crop.reset", obj.imp().is_reset_enabled());
}
));
2024-09-15 21:06:23 +02:00
}
fn dispose(&self) {
self.obj().child().unparent();
}
}
impl WidgetImpl for LpEditCrop {}
impl BinImpl for LpEditCrop {}
2024-09-19 20:13:53 +02:00
impl LpEditCrop {
fn reset_selection(&self) {
self.selection.reset();
}
2025-01-22 15:41:56 +01:00
pub(super) fn apply_crop(&self) {
if let Some(crop) = self.selection.crop_area_image_coord() {
2024-12-21 21:39:49 +01:00
self.add_operation(Operation::Clip(crop));
2024-10-18 15:51:34 +02:00
self.reset_selection();
}
}
2024-12-21 21:39:49 +01:00
fn apply_mirror_horizontally(&self) {
self.apply_crop();
2024-12-21 21:39:49 +01:00
self.add_operation(Operation::MirrorHorizontally);
2024-10-18 15:51:34 +02:00
self.reset_selection();
}
2024-12-21 21:39:49 +01:00
fn apply_mirror_vertically(&self) {
self.apply_crop();
2024-12-21 21:39:49 +01:00
self.add_operation(Operation::MirrorVertically);
2024-10-18 15:51:34 +02:00
self.reset_selection();
}
2024-12-21 21:39:49 +01:00
fn apply_rotate_cw(&self) {
self.apply_crop();
self.add_operation(Operation::Rotate(gufo_common::orientation::Rotation::_270));
2024-10-18 15:51:34 +02:00
self.reset_selection();
2024-12-21 21:39:49 +01:00
}
2024-12-18 13:36:20 +01:00
2024-12-21 21:39:49 +01:00
fn apply_rotate_ccw(&self) {
self.apply_crop();
self.add_operation(Operation::Rotate(gufo_common::orientation::Rotation::_90));
2024-12-21 21:39:49 +01:00
self.reset_selection();
}
/// Undo all operations that can be done in this window
2024-12-18 13:36:20 +01:00
fn apply_reset(&self) -> Result<(), EditingError> {
2024-10-18 15:51:34 +02:00
let obj = self.obj();
2024-12-18 13:36:20 +01:00
self.image.set_operations(None)?;
2024-10-18 15:51:34 +02:00
obj.edit_window().set_operations(None);
self.reset_selection();
let res = self.obj().activate_action(
"edit-crop.aspect-ratio",
Some(&LpAspectRatio::default().to_variant()),
);
if let Err(err) = res {
log::error!("Failed to call action edit-crop.aspect-ratio: {err}");
}
2024-12-18 13:36:20 +01:00
let (w, h) = self.image.image_size();
let orientation = if h > w {
LpOrientation::Portrait
} else {
LpOrientation::Landscape
};
let res = self
.obj()
.activate_action("edit-crop.orientation", Some(&orientation.to_variant()));
if let Err(err) = res {
log::error!("Failed to call action edit-crop.orientation: {err}");
}
self.obj().action_set_enabled("edit-crop.reset", false);
Ok(())
}
2024-12-21 21:39:49 +01:00
fn add_operation(&self, operation: Operation) {
let obj = self.obj();
let edit_window = obj.edit_window();
2024-12-21 21:39:49 +01:00
let previous_operiatons = edit_window.operations();
edit_window.add_operation(operation);
2024-12-21 21:39:49 +01:00
let res = self.image.set_operations(edit_window.operations());
if res.is_err() {
obj.handle_error(res);
// Reset to set of hopefully working operations
edit_window.set_operations(previous_operiatons);
} else {
obj.action_set_enabled("edit-crop.reset", true);
}
2024-09-15 21:06:23 +02:00
}
fn is_reset_enabled(&self) -> bool {
self.obj().edit_window().operations().is_some() || self.selection.cropped()
}
2024-09-15 21:06:23 +02:00
}
}
glib::wrapper! {
pub struct LpEditCrop(ObjectSubclass<imp::LpEditCrop>)
@extends gtk::Widget, adw::Bin;
}
impl LpEditCrop {
2025-01-22 15:41:56 +01:00
pub fn new(original_image: LpImage) -> Self {
let obj: Self = glib::Object::builder()
.property("original_image", original_image)
.build();
obj
2024-09-15 21:06:23 +02:00
}
pub fn selection(&self) -> LpEditCropSelection {
self.imp().selection.clone()
}
2024-12-18 13:36:20 +01:00
fn handle_error(&self, res: Result<(), EditingError>) {
if let Err(err) = res {
2025-01-22 15:41:56 +01:00
self.window().show_error(
2024-12-18 13:36:20 +01:00
&gettext("Failed to Edit Image"),
&format!("Failed to edit image: {err}"),
ErrorType::General,
);
}
}
2025-01-22 15:41:56 +01:00
pub fn apply_crop(&self) {
self.imp().apply_crop();
}
pub fn image(&self) -> LpImage {
self.imp().image.clone()
}
2024-09-15 21:06:23 +02:00
}