// Copyright (c) 2023 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 .
//
// SPDX-License-Identifier: GPL-3.0-or-later
//! 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 super::image;
use crate::deps::*;
use crate::widgets::LpPrint;
mod imp_page {
use super::*;
#[derive(Debug, Default)]
pub struct LpPrintPreviewPage {}
#[glib::object_subclass]
impl ObjectSubclass for LpPrintPreviewPage {
const NAME: &'static str = "LpPrintPreviewPage";
type Type = super::LpPrintPreviewPage;
type ParentType = gtk::Widget;
fn class_init(klass: &mut Self::Class) {
klass.set_css_name("lpprintpreviewpage")
}
}
impl ObjectImpl for LpPrintPreviewPage {
fn constructed(&self) {
self.parent_constructed();
self.obj().set_overflow(gtk::Overflow::Hidden);
}
fn dispose(&self) {
self.obj().first_child().unwrap().unparent();
}
}
impl WidgetImpl for LpPrintPreviewPage {
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();
let image = self.print().preview_image();
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 {
pub(super) fn preview(&self) -> super::LpPrintPreview {
self.obj().parent().unwrap().downcast().unwrap()
}
fn print(&self) -> LpPrint {
self.preview().print()
}
fn display_scale(&self) -> f64 {
let print = self.print();
let page_width = print.page_setup().paper_width(gtk::Unit::Inch) * print.dpi();
self.obj().width() as f64 / page_width
}
}
}
mod imp {
use super::*;
#[derive(Debug, Default, Properties)]
#[properties(wrapper_type = super::LpPrintPreview)]
pub struct LpPrintPreview {
#[property(type = LpPrint, get, set = Self::set_print)]
print: RefCell