2024-09-14 21:34:29 +02:00
|
|
|
// 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
|
|
|
|
|
|
2024-10-18 15:51:34 +02:00
|
|
|
use std::cell::{OnceCell, RefCell};
|
2024-12-16 21:27:20 +01:00
|
|
|
use std::sync::Arc;
|
2024-09-15 21:06:23 +02:00
|
|
|
|
2024-09-14 21:34:29 +02:00
|
|
|
use adw::prelude::*;
|
|
|
|
|
use adw::subclass::prelude::*;
|
2024-10-18 15:51:34 +02:00
|
|
|
use glycin::Operations;
|
2024-09-14 21:34:29 +02:00
|
|
|
|
2024-09-15 21:06:23 +02:00
|
|
|
use super::edit::LpEditCrop;
|
2024-12-18 17:12:17 +01:00
|
|
|
use super::LpImage;
|
2024-09-15 21:06:23 +02:00
|
|
|
use crate::deps::*;
|
2024-12-18 01:29:14 +01:00
|
|
|
use crate::util::gettext::*;
|
2024-12-18 17:12:17 +01:00
|
|
|
use crate::util::root::ParentWindow;
|
|
|
|
|
use crate::util::ErrorType;
|
2024-09-15 21:06:23 +02:00
|
|
|
|
2024-09-17 22:13:35 +02:00
|
|
|
mod imp {
|
2024-09-14 21:34:29 +02:00
|
|
|
use super::*;
|
2024-09-15 21:06:23 +02:00
|
|
|
|
2024-09-14 21:34:29 +02:00
|
|
|
#[derive(Debug, Default, gtk::CompositeTemplate, glib::Properties)]
|
2024-09-15 21:06:23 +02:00
|
|
|
#[properties(wrapper_type = super::LpEditWindow)]
|
2024-09-14 21:39:53 +02:00
|
|
|
#[template(file = "edit_window.ui")]
|
2024-09-15 21:06:23 +02:00
|
|
|
pub struct LpEditWindow {
|
|
|
|
|
#[template_child]
|
|
|
|
|
toolbar_view: TemplateChild<adw::ToolbarView>,
|
2024-09-17 22:13:35 +02:00
|
|
|
#[template_child]
|
|
|
|
|
cancel: TemplateChild<gtk::Button>,
|
2024-10-18 15:51:34 +02:00
|
|
|
#[template_child]
|
|
|
|
|
save: TemplateChild<gtk::Button>,
|
2024-09-15 21:06:23 +02:00
|
|
|
|
|
|
|
|
#[property(get, construct_only)]
|
|
|
|
|
original_image: OnceCell<LpImage>,
|
2024-10-18 15:51:34 +02:00
|
|
|
|
|
|
|
|
pub(super) operations: RefCell<Option<Arc<Operations>>>,
|
2024-09-14 21:34:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[glib::object_subclass]
|
2024-09-15 21:06:23 +02:00
|
|
|
impl ObjectSubclass for LpEditWindow {
|
|
|
|
|
const NAME: &'static str = "LpEditWindow";
|
|
|
|
|
type Type = super::LpEditWindow;
|
2024-09-14 21:34:29 +02:00
|
|
|
type ParentType = adw::Bin;
|
|
|
|
|
|
|
|
|
|
fn class_init(klass: &mut Self::Class) {
|
|
|
|
|
Self::bind_template(klass);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
|
|
|
|
|
obj.init_template();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[glib::derived_properties]
|
2024-09-15 21:06:23 +02:00
|
|
|
impl ObjectImpl for LpEditWindow {
|
2024-09-14 21:34:29 +02:00
|
|
|
fn constructed(&self) {
|
|
|
|
|
self.parent_constructed();
|
2024-09-15 21:06:23 +02:00
|
|
|
|
|
|
|
|
let obj = self.obj();
|
|
|
|
|
|
|
|
|
|
self.toolbar_view
|
2024-10-18 15:51:34 +02:00
|
|
|
.set_content(Some(&LpEditCrop::new(obj.to_owned())));
|
2024-09-17 22:13:35 +02:00
|
|
|
|
|
|
|
|
self.cancel.connect_clicked(glib::clone!(
|
|
|
|
|
#[weak]
|
|
|
|
|
obj,
|
|
|
|
|
move |_| {
|
|
|
|
|
obj.window().show_image();
|
|
|
|
|
}
|
|
|
|
|
));
|
2024-10-18 15:51:34 +02:00
|
|
|
|
|
|
|
|
self.save.connect_clicked(glib::clone!(
|
|
|
|
|
#[weak]
|
|
|
|
|
obj,
|
|
|
|
|
move |_| {
|
|
|
|
|
glib::spawn_future_local(async move { obj.imp().save_image().await });
|
|
|
|
|
}
|
|
|
|
|
));
|
2024-09-14 21:34:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 21:06:23 +02:00
|
|
|
impl WidgetImpl for LpEditWindow {}
|
|
|
|
|
impl BinImpl for LpEditWindow {}
|
2024-10-18 15:51:34 +02:00
|
|
|
|
|
|
|
|
impl LpEditWindow {
|
|
|
|
|
async fn save_image(&self) {
|
|
|
|
|
let obj = self.obj();
|
|
|
|
|
|
|
|
|
|
if let Some(current_file) = obj.original_image().file() {
|
|
|
|
|
let file_dialog = gtk::FileDialog::new();
|
|
|
|
|
|
2024-12-18 01:29:14 +01:00
|
|
|
file_dialog.set_initial_file(Some(¤t_file));
|
2024-10-18 15:51:34 +02:00
|
|
|
|
|
|
|
|
match file_dialog.save_future(Some(&obj.window())).await {
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("{}", err);
|
|
|
|
|
}
|
|
|
|
|
Ok(new_file) => {
|
|
|
|
|
let editor = glycin::Editor::new(current_file);
|
|
|
|
|
if let Some(operations) = obj.operations() {
|
2024-12-18 01:29:14 +01:00
|
|
|
log::debug!("Computing edited image.");
|
2024-10-18 15:51:34 +02:00
|
|
|
let result = editor.apply_complete(&operations).await;
|
2024-12-18 01:29:14 +01:00
|
|
|
match result {
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::warn!("Failed to edit image: {err}");
|
|
|
|
|
obj.window().show_error(
|
|
|
|
|
&gettext("Failed to edit image."),
|
|
|
|
|
&format!(
|
|
|
|
|
"Failed to edit image:\n\n{err}\n\n{operations:#?}"
|
|
|
|
|
),
|
|
|
|
|
ErrorType::General,
|
2024-10-18 15:51:34 +02:00
|
|
|
)
|
2024-12-18 01:29:14 +01:00
|
|
|
}
|
|
|
|
|
Ok(binary_data) => {
|
|
|
|
|
log::debug!("Saving edited image to '{}'", new_file.uri());
|
|
|
|
|
match binary_data.get() {
|
|
|
|
|
Err(err) => {
|
|
|
|
|
obj.window().show_error(
|
|
|
|
|
&gettext("Failed to Save Image"),
|
|
|
|
|
&format!("Failed to get binary data: {err}"),
|
|
|
|
|
ErrorType::General,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
Ok(data) => {
|
|
|
|
|
if let Err(err) = new_file
|
|
|
|
|
.replace_contents_future(
|
|
|
|
|
data,
|
|
|
|
|
None,
|
|
|
|
|
true,
|
|
|
|
|
gio::FileCreateFlags::NONE,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
obj.window().show_error(
|
|
|
|
|
&gettext("Failed to Save Image"),
|
|
|
|
|
&format!("Failed to write file:\n\n{err:?}"),
|
|
|
|
|
ErrorType::General,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-18 15:51:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-14 21:34:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glib::wrapper! {
|
2024-09-15 21:06:23 +02:00
|
|
|
pub struct LpEditWindow(ObjectSubclass<imp::LpEditWindow>)
|
2024-09-14 21:34:29 +02:00
|
|
|
@extends gtk::Widget, adw::Bin;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 21:06:23 +02:00
|
|
|
impl LpEditWindow {
|
2024-12-18 17:12:17 +01:00
|
|
|
pub fn new(image: LpImage) -> Self {
|
2024-09-15 21:06:23 +02:00
|
|
|
glib::Object::builder()
|
|
|
|
|
.property("original_image", image)
|
|
|
|
|
.build()
|
2024-09-14 21:34:29 +02:00
|
|
|
}
|
2024-10-18 15:51:34 +02:00
|
|
|
|
|
|
|
|
pub fn operations(&self) -> Option<Arc<glycin::Operations>> {
|
|
|
|
|
self.imp().operations.borrow().clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_operations(&self, operations: Option<Arc<glycin::Operations>>) {
|
|
|
|
|
let imp = self.imp();
|
|
|
|
|
|
|
|
|
|
imp.operations.replace(operations);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn add_operation(&self, operation: glycin::Operation) {
|
|
|
|
|
let imp = self.imp();
|
|
|
|
|
|
|
|
|
|
let mut operations = imp
|
|
|
|
|
.operations
|
|
|
|
|
.borrow()
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map(|x| x.operations().to_vec())
|
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
|
|
operations.push(operation);
|
|
|
|
|
|
|
|
|
|
self.set_operations(Some(Arc::new(Operations::new(operations))));
|
|
|
|
|
}
|
2024-09-14 21:34:29 +02:00
|
|
|
}
|