Python: Some Image docs

This commit is contained in:
Simon Hausmann 2025-02-09 13:50:26 +01:00
parent c46846b3cb
commit 30aefd4957

View file

@ -3,6 +3,8 @@
use pyo3::prelude::*; use pyo3::prelude::*;
/// Image objects can be set on Slint Image elements for display. Construct Image objects from a path to an
/// image file on disk, using `Image.load_from_path`.
#[pyclass(unsendable, name = "Image")] #[pyclass(unsendable, name = "Image")]
pub struct PyImage { pub struct PyImage {
pub image: slint_interpreter::Image, pub image: slint_interpreter::Image,
@ -15,32 +17,38 @@ impl PyImage {
Ok(Self { image: Default::default() }) Ok(Self { image: Default::default() })
} }
/// The size of the image as tuple of `width` and `height`.
#[getter] #[getter]
fn size(&self) -> PyResult<(u32, u32)> { fn size(&self) -> PyResult<(u32, u32)> {
Ok(self.image.size().into()) Ok(self.image.size().into())
} }
/// The width of the image in pixels.
#[getter] #[getter]
fn width(&self) -> PyResult<u32> { fn width(&self) -> PyResult<u32> {
Ok(self.image.size().width) Ok(self.image.size().width)
} }
/// The height of the image in pixels.
#[getter] #[getter]
fn height(&self) -> PyResult<u32> { fn height(&self) -> PyResult<u32> {
Ok(self.image.size().height) Ok(self.image.size().height)
} }
/// The path of the image if it was loaded from disk, or None.
#[getter] #[getter]
fn path(&self) -> PyResult<Option<&std::path::Path>> { fn path(&self) -> PyResult<Option<&std::path::Path>> {
Ok(self.image.path()) Ok(self.image.path())
} }
/// Loads the image from the specified path. Returns None if the image can't be loaded.
#[staticmethod] #[staticmethod]
fn load_from_path(path: std::path::PathBuf) -> Result<Self, crate::errors::PyLoadImageError> { fn load_from_path(path: std::path::PathBuf) -> Result<Self, crate::errors::PyLoadImageError> {
let image = slint_interpreter::Image::load_from_path(&path)?; let image = slint_interpreter::Image::load_from_path(&path)?;
Ok(Self { image }) Ok(Self { image })
} }
/// Creates a new image from a string that describes the image in SVG format.
#[staticmethod] #[staticmethod]
fn load_from_svg_data(data: &[u8]) -> Result<Self, crate::errors::PyLoadImageError> { fn load_from_svg_data(data: &[u8]) -> Result<Self, crate::errors::PyLoadImageError> {
let image = slint_interpreter::Image::load_from_svg_data(data)?; let image = slint_interpreter::Image::load_from_svg_data(data)?;