print: Use LpImage for preview
This commit is contained in:
parent
e387d8dc66
commit
6827a9c176
9 changed files with 143 additions and 44 deletions
|
|
@ -17,14 +17,17 @@
|
|||
|
||||
//! Print preview widget that shows a fake page with the image on it
|
||||
|
||||
use std::cell::RefCell;
|
||||
|
||||
use adw::subclass::prelude::*;
|
||||
use glib::Properties;
|
||||
use gtk::prelude::*;
|
||||
use once_cell::sync::OnceCell;
|
||||
|
||||
use crate::deps::*;
|
||||
use crate::widgets::LpPrint;
|
||||
|
||||
use super::image;
|
||||
|
||||
mod imp_page {
|
||||
use super::*;
|
||||
|
||||
|
|
@ -47,42 +50,35 @@ mod imp_page {
|
|||
self.parent_constructed();
|
||||
self.obj().set_overflow(gtk::Overflow::Hidden);
|
||||
}
|
||||
|
||||
fn dispose(&self) {
|
||||
self.obj().first_child().unwrap().unparent();
|
||||
}
|
||||
}
|
||||
|
||||
impl WidgetImpl for LpPrintPreviewPage {
|
||||
fn snapshot(&self, snapshot: >k::Snapshot) {
|
||||
snapshot.save();
|
||||
|
||||
let scale = self.print().user_scale_original() * self.display_scale();
|
||||
fn size_allocate(&self, _width: i32, _height: i32, _baseline: i32) {
|
||||
let margin_left = self.print().effective_margin_left() * self.display_scale();
|
||||
let margin_top = self.print().effective_margin_top() * self.display_scale();
|
||||
let width = self.print().user_width() * self.display_scale();
|
||||
let height = self.print().user_height() * self.display_scale();
|
||||
|
||||
if let Some(texture) = self.print().image().print_data(scale) {
|
||||
let area = graphene::Rect::new(
|
||||
margin_left as f32,
|
||||
margin_top as f32,
|
||||
texture.width() as f32,
|
||||
texture.height() as f32,
|
||||
);
|
||||
let image = self.print().image();
|
||||
|
||||
snapshot.append_texture(&texture, &area);
|
||||
|
||||
// Border around image
|
||||
snapshot.append_inset_shadow(
|
||||
&gsk::RoundedRect::from_rect(area, 0.),
|
||||
&gdk::RGBA::new(0., 0., 0., 0.3),
|
||||
0.,
|
||||
0.,
|
||||
1.,
|
||||
0.,
|
||||
);
|
||||
}
|
||||
snapshot.restore();
|
||||
image.allocate(
|
||||
width as i32,
|
||||
height as i32,
|
||||
-1,
|
||||
Some(
|
||||
gsk::Transform::new()
|
||||
.translate(&graphene::Point::new(margin_left as f32, margin_top as f32)),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl LpPrintPreviewPage {
|
||||
fn preview(&self) -> super::LpPrintPreview {
|
||||
pub(super) fn preview(&self) -> super::LpPrintPreview {
|
||||
self.obj().parent().unwrap().downcast().unwrap()
|
||||
}
|
||||
|
||||
|
|
@ -99,13 +95,14 @@ mod imp_page {
|
|||
}
|
||||
|
||||
mod imp {
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug, Default, Properties)]
|
||||
#[properties(wrapper_type = super::LpPrintPreview)]
|
||||
pub struct LpPrintPreview {
|
||||
#[property(get, set)]
|
||||
print: OnceCell<LpPrint>,
|
||||
#[property(type = LpPrint, get, set = Self::set_print)]
|
||||
print: RefCell<Option<LpPrint>>,
|
||||
page: LpPrintPreviewPage,
|
||||
}
|
||||
|
||||
|
|
@ -127,6 +124,10 @@ mod imp {
|
|||
self.page.insert_after(&*self.obj(), gtk::Widget::NONE);
|
||||
}
|
||||
|
||||
fn dispose(&self) {
|
||||
self.page.unparent();
|
||||
}
|
||||
|
||||
fn properties() -> &'static [glib::ParamSpec] {
|
||||
Self::derived_properties()
|
||||
}
|
||||
|
|
@ -152,11 +153,21 @@ mod imp {
|
|||
baseline,
|
||||
);
|
||||
|
||||
self.page.queue_draw();
|
||||
self.page.queue_resize();
|
||||
}
|
||||
}
|
||||
|
||||
impl LpPrintPreview {
|
||||
fn set_print(&self, print: LpPrint) {
|
||||
let page = self.page.clone();
|
||||
print.connect_image_notify(move |_| {
|
||||
page.init();
|
||||
});
|
||||
|
||||
self.print.replace(Some(print));
|
||||
self.obj().notify_print();
|
||||
}
|
||||
|
||||
fn aspect_preserving_size(&self, for_width: i32, for_height: i32) -> (i32, i32) {
|
||||
let print = self.obj().print();
|
||||
|
||||
|
|
@ -182,6 +193,17 @@ impl Default for LpPrintPreviewPage {
|
|||
}
|
||||
}
|
||||
|
||||
impl LpPrintPreviewPage {
|
||||
fn init(&self) {
|
||||
let imp = self.imp();
|
||||
let image = imp.preview().print().image();
|
||||
|
||||
image.set_sensitive(false);
|
||||
image.set_parent(self);
|
||||
image.set_fit_mode(image::FitMode::LargeFit);
|
||||
}
|
||||
}
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct LpPrintPreview(ObjectSubclass<imp::LpPrintPreview>)
|
||||
@extends gtk::Widget;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue