editing: Add save edited file

This commit is contained in:
Sophie Herold 2024-10-18 15:51:34 +02:00
parent a2d5493714
commit 05dbee6a0b
6 changed files with 154 additions and 29 deletions

View file

@ -1,8 +1,26 @@
use crate::deps::*; // 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 glycin::{Operation, Operations}; use glycin::{Operation, Operations};
use gtk::prelude::*; use gtk::prelude::*;
use gufo_common::orientation::Rotation; use gufo_common::orientation::Rotation;
use crate::deps::*;
pub fn apply_operations( pub fn apply_operations(
node: gsk::RenderNode, node: gsk::RenderNode,
width: u32, width: u32,

View file

@ -22,7 +22,7 @@ use adw::subclass::prelude::*;
use adw::{glib, gtk}; use adw::{glib, gtk};
use crate::widgets::edit::LpEditCropSelection; use crate::widgets::edit::LpEditCropSelection;
use crate::widgets::LpImage; use crate::widgets::{edit_window, LpEditWindow, LpImage};
#[derive(Debug, Clone, Copy, Default, glib::Enum)] #[derive(Debug, Clone, Copy, Default, glib::Enum)]
#[enum_type(name = "LpAspectRatio")] #[enum_type(name = "LpAspectRatio")]
@ -75,6 +75,8 @@ mod imp {
#[property(get, construct_only)] #[property(get, construct_only)]
original_image: OnceCell<LpImage>, original_image: OnceCell<LpImage>,
#[property(get, construct_only)]
edit_window: OnceCell<LpEditWindow>,
#[property(get, set)] #[property(get, set)]
child: OnceCell<gtk::Widget>, child: OnceCell<gtk::Widget>,
@ -240,30 +242,47 @@ mod imp {
fn apply_crop(&self) { fn apply_crop(&self) {
if let Some(crop) = self.crop_area_image_coord() { if let Some(crop) = self.crop_area_image_coord() {
self.image.add_operation(Operation::Clip(crop)); let edit_window = self.obj().edit_window();
edit_window.add_operation(Operation::Clip(crop));
self.image.set_operations(edit_window.operations());
self.reset_selection(); self.reset_selection();
} }
} }
fn apply_mirror(&self) { fn apply_mirror(&self) {
self.image.add_operation(Operation::MirrorHorizontally); let edit_window = self.obj().edit_window();
edit_window.add_operation(Operation::MirrorHorizontally);
self.image.set_operations(edit_window.operations());
self.reset_selection(); self.reset_selection();
} }
fn apply_rotate_cw(&self) { fn apply_rotate_cw(&self) {
self.image let edit_window = self.obj().edit_window();
.add_operation(Operation::Rotate(gufo_common::orientation::Rotation::_90));
edit_window.add_operation(Operation::Rotate(gufo_common::orientation::Rotation::_90));
self.image.set_operations(edit_window.operations());
self.reset_selection(); self.reset_selection();
} }
fn apply_rotate_ccw(&self) { fn apply_rotate_ccw(&self) {
self.image let edit_window = self.obj().edit_window();
.add_operation(Operation::Rotate(gufo_common::orientation::Rotation::_270));
edit_window.add_operation(Operation::Rotate(gufo_common::orientation::Rotation::_270));
self.image.set_operations(edit_window.operations());
self.reset_selection(); self.reset_selection();
} }
fn apply_reset(&self) { fn apply_reset(&self) {
let obj = self.obj();
self.image.set_operations(None); self.image.set_operations(None);
obj.edit_window().set_operations(None);
self.reset_selection(); self.reset_selection();
} }
} }
@ -275,9 +294,10 @@ glib::wrapper! {
} }
impl LpEditCrop { impl LpEditCrop {
pub fn new(original_image: LpImage) -> Self { pub fn new(edit_window: LpEditWindow) -> Self {
glib::Object::builder() glib::Object::builder()
.property("original_image", original_image) .property("original_image", edit_window.original_image())
.property("edit_window", edit_window)
.build() .build()
} }

View file

@ -15,10 +15,12 @@
// //
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
use std::cell::OnceCell; use std::cell::{OnceCell, RefCell};
use adw::prelude::*; use adw::prelude::*;
use adw::subclass::prelude::*; use adw::subclass::prelude::*;
use glycin::Operations;
use std::sync::Arc;
use super::edit::LpEditCrop; use super::edit::LpEditCrop;
use super::{LpImage, LpWindow}; use super::{LpImage, LpWindow};
@ -35,11 +37,15 @@ mod imp {
toolbar_view: TemplateChild<adw::ToolbarView>, toolbar_view: TemplateChild<adw::ToolbarView>,
#[template_child] #[template_child]
cancel: TemplateChild<gtk::Button>, cancel: TemplateChild<gtk::Button>,
#[template_child]
save: TemplateChild<gtk::Button>,
#[property(get, construct_only)] #[property(get, construct_only)]
window: OnceCell<LpWindow>, window: OnceCell<LpWindow>,
#[property(get, construct_only)] #[property(get, construct_only)]
original_image: OnceCell<LpImage>, original_image: OnceCell<LpImage>,
pub(super) operations: RefCell<Option<Arc<Operations>>>,
} }
#[glib::object_subclass] #[glib::object_subclass]
@ -65,7 +71,7 @@ mod imp {
let obj = self.obj(); let obj = self.obj();
self.toolbar_view self.toolbar_view
.set_content(Some(&LpEditCrop::new(obj.original_image()))); .set_content(Some(&LpEditCrop::new(obj.to_owned())));
self.cancel.connect_clicked(glib::clone!( self.cancel.connect_clicked(glib::clone!(
#[weak] #[weak]
@ -74,11 +80,57 @@ mod imp {
obj.window().show_image(); obj.window().show_image();
} }
)); ));
self.save.connect_clicked(glib::clone!(
#[weak]
obj,
move |_| {
glib::spawn_future_local(async move { obj.imp().save_image().await });
}
));
} }
} }
impl WidgetImpl for LpEditWindow {} impl WidgetImpl for LpEditWindow {}
impl BinImpl for LpEditWindow {} impl BinImpl for LpEditWindow {}
impl LpEditWindow {
async fn save_image(&self) {
dbg!("save");
let obj = self.obj();
if let Some(current_file) = obj.original_image().file() {
let file_dialog = gtk::FileDialog::new();
file_dialog.set_initial_file(obj.original_image().file().as_ref());
match file_dialog.save_future(Some(&obj.window())).await {
Err(err) => {
log::error!("{}", err);
}
Ok(new_file) => {
dbg!(new_file.path());
let editor = glycin::Editor::new(current_file);
if let Some(operations) = obj.operations() {
let result = editor.apply_complete(&operations).await;
dbg!(&result);
dbg!(
new_file
.replace_contents_future(
result.unwrap().get().unwrap(),
None,
true,
gio::FileCreateFlags::NONE,
)
.await
);
}
}
}
}
}
}
} }
glib::wrapper! { glib::wrapper! {
@ -93,4 +145,29 @@ impl LpEditWindow {
.property("original_image", image) .property("original_image", image)
.build() .build()
} }
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))));
}
} }

View file

@ -14,6 +14,14 @@
<property name="label" translatable="true">Cancel</property> <property name="label" translatable="true">Cancel</property>
</object> </object>
</child> </child>
<child type="end">
<object class="GtkButton" id="save">
<property name="label" translatable="true">Save</property>
<style>
<class name="suggested-action" />
</style>
</object>
</child>
</object> </object>
</child> </child>
</object> </object>

View file

@ -247,7 +247,7 @@ mod imp {
/// Editing queue /// Editing queue
pub(super) editing_queue: editing::Queue, pub(super) editing_queue: editing::Queue,
pub(super) operations: RefCell<Option<Operations>>, pub(super) operations: RefCell<Option<Arc<Operations>>>,
} }
#[glib::object_subclass] #[glib::object_subclass]

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 glycin::Operations; use glycin::Operations;
use tiling::RenderOptions; use tiling::RenderOptions;
@ -28,7 +45,7 @@ impl imp::LpImage {
} }
impl LpImage { impl LpImage {
pub fn set_operations(&self, operations: Option<glycin::Operations>) { pub fn set_operations(&self, operations: Option<Arc<glycin::Operations>>) {
let imp = self.imp(); let imp = self.imp();
if operations.is_none() { if operations.is_none() {
@ -55,19 +72,4 @@ impl LpImage {
imp.configure_best_fit(); imp.configure_best_fit();
self.queue_draw(); self.queue_draw();
} }
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(Operations::new(operations)));
}
} }