2025-01-01 15:30:03 +01:00
|
|
|
// Copyright (c) 2024-2025 Sophie Herold
|
2024-09-17 20:13:39 +02:00
|
|
|
//
|
|
|
|
|
// 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
|
|
|
|
|
|
2024-12-21 13:44:46 +01:00
|
|
|
//! Shows an resizable cropping selection
|
|
|
|
|
|
2025-01-27 20:12:48 +01:00
|
|
|
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
|
|
|
|
2025-02-25 22:57:32 +01:00
|
|
|
use crate::deps::*;
|
2024-12-18 13:36:20 +01:00
|
|
|
use crate::editing::preview::EditingError;
|
2024-12-21 03:09:15 +01:00
|
|
|
use crate::util::gettext::*;
|
2024-12-18 17:12:17 +01:00
|
|
|
use crate::util::root::ParentWindow;
|
2024-12-21 03:09:15 +01:00
|
|
|
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
|
|
|
|
2024-12-21 13:44:46 +01:00
|
|
|
/// Aspect ratio modes that can be selected
|
2025-01-13 23:14:37 +01:00
|
|
|
#[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,
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-17 16:51:06 +01:00
|
|
|
#[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 {
|
|
|
|
|
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]
|
2025-01-27 20:12:48 +01:00
|
|
|
pub(super) image: TemplateChild<LpImage>,
|
2024-09-15 21:06:23 +02:00
|
|
|
#[template_child]
|
|
|
|
|
pub(super) selection: TemplateChild<LpEditCropSelection>,
|
2025-02-25 22:57:32 +01:00
|
|
|
#[template_child]
|
|
|
|
|
aspect_ratio_buttons: TemplateChild<gtk::Widget>,
|
2024-09-15 21:06:23 +02:00
|
|
|
|
|
|
|
|
#[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;
|
2025-01-27 20:12:48 +01:00
|
|
|
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()
|
2024-10-04 14:05:11 +02:00
|
|
|
});
|
|
|
|
|
klass.install_action("edit-crop.rotate-cw", None, |obj, _, _| {
|
2024-12-21 21:39:49 +01:00
|
|
|
obj.imp().apply_rotate_cw()
|
2024-10-04 14:05:11 +02:00
|
|
|
});
|
|
|
|
|
klass.install_action("edit-crop.rotate-ccw", None, |obj, _, _| {
|
2024-12-21 21:39:49 +01:00
|
|
|
obj.imp().apply_rotate_ccw()
|
2024-10-04 14:05:11 +02:00
|
|
|
});
|
|
|
|
|
klass.install_action("edit-crop.reset", None, |obj, _, _| {
|
2024-12-21 21:39:49 +01:00
|
|
|
obj.handle_error(obj.imp().apply_reset())
|
2024-10-04 14:05:11 +02:00
|
|
|
});
|
2024-12-21 13:44:46 +01:00
|
|
|
klass.install_action("edit-crop.apply-crop", None, |obj, _, _| {
|
2024-12-21 21:39:49 +01:00
|
|
|
obj.imp().apply_crop()
|
2024-12-21 13:44:46 +01:00
|
|
|
});
|
2025-01-29 12:29:26 +01:00
|
|
|
|
|
|
|
|
klass.add_shortcut(
|
|
|
|
|
>k::Shortcut::builder()
|
|
|
|
|
.action(>k::NamedAction::new("edit-crop.mirror-horizontally"))
|
|
|
|
|
.trigger(>k::KeyvalTrigger::new(
|
|
|
|
|
gdk::Key::H,
|
|
|
|
|
gdk::ModifierType::NO_MODIFIER_MASK,
|
|
|
|
|
))
|
|
|
|
|
.build(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
klass.add_shortcut(
|
|
|
|
|
>k::Shortcut::builder()
|
|
|
|
|
.action(>k::NamedAction::new("edit-crop.mirror-vertically"))
|
|
|
|
|
.trigger(>k::KeyvalTrigger::new(
|
|
|
|
|
gdk::Key::V,
|
|
|
|
|
gdk::ModifierType::NO_MODIFIER_MASK,
|
|
|
|
|
))
|
|
|
|
|
.build(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
klass.add_shortcut(
|
|
|
|
|
>k::Shortcut::builder()
|
|
|
|
|
.action(>k::NamedAction::new("edit-crop.rotate-cw"))
|
|
|
|
|
.trigger(>k::KeyvalTrigger::new(
|
|
|
|
|
gdk::Key::R,
|
|
|
|
|
gdk::ModifierType::CONTROL_MASK,
|
|
|
|
|
))
|
|
|
|
|
.build(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
klass.add_shortcut(
|
|
|
|
|
>k::Shortcut::builder()
|
|
|
|
|
.action(>k::NamedAction::new("edit-crop.rotate-ccw"))
|
|
|
|
|
.trigger(>k::KeyvalTrigger::new(
|
|
|
|
|
gdk::Key::R,
|
|
|
|
|
gdk::ModifierType::CONTROL_MASK.union(gdk::ModifierType::SHIFT_MASK),
|
|
|
|
|
))
|
|
|
|
|
.build(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
klass.add_shortcut(
|
|
|
|
|
>k::Shortcut::builder()
|
|
|
|
|
.action(>k::NamedAction::new("edit-crop.apply-crop"))
|
|
|
|
|
.trigger(>k::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());
|
2024-10-04 14:05:11 +02:00
|
|
|
|
2025-01-27 20:12:48 +01:00
|
|
|
let actions = gio::SimpleActionGroup::new();
|
|
|
|
|
actions.add_action(&gio::PropertyAction::new(
|
|
|
|
|
"aspect-ratio",
|
|
|
|
|
&*self.selection,
|
|
|
|
|
"aspect_ratio",
|
2024-10-04 14:05:11 +02:00
|
|
|
));
|
2025-01-27 20:12:48 +01:00
|
|
|
actions.add_action(&gio::PropertyAction::new(
|
|
|
|
|
"orientation",
|
|
|
|
|
&*self.selection,
|
|
|
|
|
"orientation",
|
2024-10-04 14:05:11 +02:00
|
|
|
));
|
|
|
|
|
|
2025-01-27 20:12:48 +01:00
|
|
|
obj.insert_action_group("edit-crop", Some(&actions));
|
2024-10-04 14:05:11 +02:00
|
|
|
|
2025-01-27 20:12:48 +01:00
|
|
|
obj.action_set_enabled("edit-crop.reset", false);
|
2025-01-29 00:20:03 +01:00
|
|
|
|
|
|
|
|
self.selection.connect_cropped_notify(glib::clone!(
|
|
|
|
|
#[weak]
|
|
|
|
|
obj,
|
|
|
|
|
move |_| {
|
|
|
|
|
obj.action_set_enabled("edit-crop.reset", obj.imp().is_reset_enabled());
|
|
|
|
|
}
|
|
|
|
|
));
|
2025-02-25 22:57:32 +01:00
|
|
|
|
|
|
|
|
self.selection.connect_orientation_notify(glib::clone!(
|
|
|
|
|
#[weak]
|
|
|
|
|
obj,
|
|
|
|
|
move |_| {
|
|
|
|
|
obj.imp().asepect_ratio_orientation_changed();
|
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
self.asepect_ratio_orientation_changed();
|
2024-09-15 21:06:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn dispose(&self) {
|
|
|
|
|
self.obj().child().unparent();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-27 20:12:48 +01:00
|
|
|
impl WidgetImpl for LpEditCrop {}
|
2024-10-04 14:05:11 +02:00
|
|
|
impl BinImpl for LpEditCrop {}
|
2024-09-19 20:13:53 +02:00
|
|
|
|
2024-10-04 14:05:11 +02:00
|
|
|
impl LpEditCrop {
|
|
|
|
|
fn reset_selection(&self) {
|
2025-01-27 20:12:48 +01:00
|
|
|
self.selection.reset();
|
2024-10-04 14:05:11 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-22 15:41:56 +01:00
|
|
|
pub(super) fn apply_crop(&self) {
|
2025-01-27 20:12:48 +01:00
|
|
|
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
|
|
|
|
2024-10-04 14:05:11 +02:00
|
|
|
self.reset_selection();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-21 21:39:49 +01:00
|
|
|
fn apply_mirror_horizontally(&self) {
|
2025-01-17 23:23:59 +01:00
|
|
|
self.apply_crop();
|
2024-12-21 21:39:49 +01:00
|
|
|
self.add_operation(Operation::MirrorHorizontally);
|
2024-10-18 15:51:34 +02:00
|
|
|
|
2024-10-04 14:05:11 +02:00
|
|
|
self.reset_selection();
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-21 21:39:49 +01:00
|
|
|
fn apply_mirror_vertically(&self) {
|
2025-01-17 23:23:59 +01:00
|
|
|
self.apply_crop();
|
2024-12-21 21:39:49 +01:00
|
|
|
self.add_operation(Operation::MirrorVertically);
|
2024-10-18 15:51:34 +02:00
|
|
|
|
2024-10-04 14:05:11 +02:00
|
|
|
self.reset_selection();
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-21 21:39:49 +01:00
|
|
|
fn apply_rotate_cw(&self) {
|
2025-01-17 23:23:59 +01:00
|
|
|
self.apply_crop();
|
2025-01-01 15:30:03 +01:00
|
|
|
self.add_operation(Operation::Rotate(gufo_common::orientation::Rotation::_270));
|
2024-10-18 15:51:34 +02:00
|
|
|
|
2024-10-04 14:05:11 +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) {
|
2025-01-17 23:23:59 +01:00
|
|
|
self.apply_crop();
|
2025-01-01 15:30:03 +01:00
|
|
|
self.add_operation(Operation::Rotate(gufo_common::orientation::Rotation::_90));
|
2024-12-21 21:39:49 +01:00
|
|
|
|
|
|
|
|
self.reset_selection();
|
2024-10-04 14:05:11 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-13 23:14:37 +01:00
|
|
|
/// 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);
|
2024-10-04 14:05:11 +02:00
|
|
|
self.reset_selection();
|
2025-01-17 16:51:06 +01:00
|
|
|
|
2025-01-13 23:14:37 +01:00
|
|
|
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
|
|
|
|
2025-01-17 16:51:06 +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}");
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-21 13:44:46 +01:00
|
|
|
self.obj().action_set_enabled("edit-crop.reset", false);
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-21 21:39:49 +01:00
|
|
|
fn add_operation(&self, operation: Operation) {
|
2024-12-21 13:44:46 +01:00
|
|
|
let obj = self.obj();
|
|
|
|
|
|
|
|
|
|
let edit_window = obj.edit_window();
|
2024-12-21 21:39:49 +01:00
|
|
|
let previous_operiatons = edit_window.operations();
|
2024-12-21 13:44:46 +01:00
|
|
|
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
|
|
|
}
|
2025-01-29 00:20:03 +01:00
|
|
|
|
|
|
|
|
fn is_reset_enabled(&self) -> bool {
|
|
|
|
|
self.obj().edit_window().operations().is_some() || self.selection.cropped()
|
|
|
|
|
}
|
2025-02-25 22:57:32 +01:00
|
|
|
|
|
|
|
|
fn asepect_ratio_orientation_changed(&self) {
|
|
|
|
|
match self.selection.orientation() {
|
|
|
|
|
LpOrientation::Landscape => {
|
|
|
|
|
self.aspect_ratio_buttons
|
|
|
|
|
.remove_css_class("button-icons-portrait");
|
|
|
|
|
}
|
|
|
|
|
LpOrientation::Portrait => {
|
|
|
|
|
self.aspect_ratio_buttons
|
|
|
|
|
.add_css_class("button-icons-portrait");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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();
|
|
|
|
|
}
|
2025-01-27 20:12:48 +01:00
|
|
|
|
|
|
|
|
pub fn image(&self) -> LpImage {
|
|
|
|
|
self.imp().image.clone()
|
|
|
|
|
}
|
2024-09-15 21:06:23 +02:00
|
|
|
}
|