From 166fc07a2de0ecad9db5cbda3d055bec50eeb624 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Mon, 20 Nov 2023 14:56:29 +0100 Subject: [PATCH 01/18] Improve QImage support --- .../cxx-qt-lib-headers/include/gui/qimage.h | 3 + crates/cxx-qt-lib/src/gui/qimage.cpp | 5 + crates/cxx-qt-lib/src/gui/qimage.rs | 96 +++++++++++++++++++ 3 files changed, 104 insertions(+) diff --git a/crates/cxx-qt-lib-headers/include/gui/qimage.h b/crates/cxx-qt-lib-headers/include/gui/qimage.h index c4fecab89..4f8dfbe79 100644 --- a/crates/cxx-qt-lib-headers/include/gui/qimage.h +++ b/crates/cxx-qt-lib-headers/include/gui/qimage.h @@ -29,6 +29,9 @@ QImage qimageInitFromData(const rust::Slice data, rust::Str format); +::std::int64_t +qimageCacheKey(const QImage& image); + } // namespace cxxqtlib1 } // namespace rust #endif diff --git a/crates/cxx-qt-lib/src/gui/qimage.cpp b/crates/cxx-qt-lib/src/gui/qimage.cpp index 489823e42..ef0a4e5cb 100644 --- a/crates/cxx-qt-lib/src/gui/qimage.cpp +++ b/crates/cxx-qt-lib/src/gui/qimage.cpp @@ -45,6 +45,11 @@ qimageInitFromData(const rust::Slice data, rust::Str format) formatString.empty() ? nullptr : formatString.data()); } +::std::int64_t +qimageCacheKey(const QImage& image) +{ + return static_cast<::std::int64_t>(image.cacheKey()); +} } } diff --git a/crates/cxx-qt-lib/src/gui/qimage.rs b/crates/cxx-qt-lib/src/gui/qimage.rs index dc1360389..67ca4709b 100644 --- a/crates/cxx-qt-lib/src/gui/qimage.rs +++ b/crates/cxx-qt-lib/src/gui/qimage.rs @@ -11,12 +11,100 @@ mod ffi { unsafe extern "C++" { include!("cxx-qt-lib/qimage.h"); type QImage = super::QImage; + include!("cxx-qt-lib/qsize.h"); + type QSize = crate::QSize; + include!("cxx-qt-lib/qrect.h"); + type QRect = crate::QRect; + include!("cxx-qt-lib/qcolor.h"); + type QColor = crate::QColor; + include!("cxx-qt-lib/qpoint.h"); + type QPoint = crate::QPoint; /// Whether the QImage is null. /// /// This means that the QImage has all parameters set to zero and no allocated data. #[rust_name = "is_null"] fn isNull(self: &QImage) -> bool; + + /// Returns true if all the colors in the image are shades of gray + #[rust_name = "all_gray"] + fn allGray(self: &QImage) -> bool; + + /// For 32-bit images, this function is equivalent to allGray(). + /// For color indexed images, this function returns true if color(i) is QRgb(i, i, i) + /// for all indexes of the color table; otherwise returns false. + #[rust_name = "is_gray_scale"] + fn isGrayscale(self: &QImage) -> bool; + + /// Returns true if the image has a format that respects the alpha channel, otherwise returns false. + #[rust_name = "has_alpha_channel"] + fn hasAlphaChannel(self: &QImage) -> bool; + + /// Returns a sub-area of the image as a new image. + fn copy(self: &QImage, rect: &QRect) -> QImage; + + /// Returns the size of the color table for the image. + #[rust_name = "color_count"] + fn colorCount(self: &QImage) -> i32; + + /// Resizes the color table to contain colorCount entries. + #[rust_name = "set_color_count"] + fn setColorCount(self: &mut QImage, colorCount: i32); + + /// Returns the depth of the image. + fn depth(self: &QImage) -> i32; + + /// Fills the entire image with the given color. + fn fill(self: &mut QImage, color: &QColor); + + /// Returns the height of the image. + fn height(self: &QImage) -> i32; + + /// Returns the width of the image. + fn width(self: &QImage) -> i32; + + /// Returns the size of the image. + fn size(self: &QImage) -> QSize; + + /// Returns the enclosing rectangle (0, 0, width(), height()) of the image. + fn rect(self: &QImage) -> QRect; + + /// Returns the color of the pixel at coordinates (x, y) as a QColor. + #[rust_name = "pixel_color"] + fn pixelColor(self: &QImage, x: i32, y: i32) -> QColor; + + /// Sets the pixel color at (x, y) to color. + #[rust_name = "set_pixel_color"] + fn setPixelColor(self: &mut QImage, x: i32, y: i32, color: &QColor); + + /// Returns the number of pixels that fit horizontally in a physical meter. + #[rust_name = "dots_per_meterx"] + fn dotsPerMeterX(self: &QImage) -> i32; + + /// Returns the number of pixels that fit vertically in a physical meter. + #[rust_name = "dots_per_metery"] + fn dotsPerMeterY(self: &QImage) -> i32; + + /// Returns true if pos is a valid coordinate pair within the image. + fn valid(self: &QImage, x: i32, y: i32) -> bool; + + /// Returns the number of pixels by which the image is intended to be offset by when positioning relative to other images. + fn offset(self: &QImage) -> QPoint; + + /// Sets the number of pixels by which the image is intended to be offset by when positioning relative to other images, to offset. + #[rust_name = "set_offset"] + fn setOffset(self: &mut QImage, point: &QPoint); + + /// Returns the pixel index at (x, y). + #[rust_name = "pixel_index"] + fn pixelIndex(self: &QImage, x: i32, y: i32) -> i32; + + /// Mirrors of the image in the horizontal and/or the vertical direction depending on whether horizontal and vertical are set to true or false. + fn mirror(self: &mut QImage, horizontal: bool, vertical: bool); + + /// Sets the alpha channel of this image to the given alphaChannel. + #[rust_name = "set_alpha_channel"] + fn setAlphaChannel(self: &mut QImage, alphaChannel: &QImage); } #[namespace = "rust::cxxqtlib1"] @@ -30,6 +118,10 @@ mod ffi { #[doc(hidden)] #[rust_name = "qimage_init_from_data"] fn qimageInitFromData(data: &[u8], format: &str) -> QImage; + + #[doc(hidden)] + #[rust_name = "qimage_cache_key"] + fn qimageCacheKey(image: &QImage) -> i64; } } @@ -79,4 +171,8 @@ impl QImage { None } } + /// Returns a number that identifies the contents of this QImage object. + pub fn cache_key(&self) -> i64 { + ffi::qimage_cache_key(self) + } } From 014af924e56ae726f2b94ae39bbaa30dbdc1d259 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Tue, 21 Nov 2023 14:50:33 +0100 Subject: [PATCH 02/18] Reorder method + try to make mirror method specific qt6 --- crates/cxx-qt-lib/src/gui/qimage.rs | 100 +++++++++++++++------------- 1 file changed, 55 insertions(+), 45 deletions(-) diff --git a/crates/cxx-qt-lib/src/gui/qimage.rs b/crates/cxx-qt-lib/src/gui/qimage.rs index 67ca4709b..a76bc335c 100644 --- a/crates/cxx-qt-lib/src/gui/qimage.rs +++ b/crates/cxx-qt-lib/src/gui/qimage.rs @@ -20,16 +20,37 @@ mod ffi { include!("cxx-qt-lib/qpoint.h"); type QPoint = crate::QPoint; + /// Returns true if all the colors in the image are shades of gray + #[rust_name = "all_gray"] + fn allGray(self: &QImage) -> bool; + + /// Returns a sub-area of the image as a new image. + fn copy(self: &QImage, rect: &QRect) -> QImage; + + /// Returns the size of the color table for the image. + #[rust_name = "color_count"] + fn colorCount(self: &QImage) -> i32; + + /// Returns the depth of the image. + fn depth(self: &QImage) -> i32; + + /// Returns the number of pixels that fit horizontally in a physical meter. + #[rust_name = "dots_per_meterx"] + fn dotsPerMeterX(self: &QImage) -> i32; + + /// Returns the number of pixels that fit vertically in a physical meter. + #[rust_name = "dots_per_metery"] + fn dotsPerMeterY(self: &QImage) -> i32; + + /// Fills the entire image with the given color. + fn fill(self: &mut QImage, color: &QColor); + /// Whether the QImage is null. /// /// This means that the QImage has all parameters set to zero and no allocated data. #[rust_name = "is_null"] fn isNull(self: &QImage) -> bool; - /// Returns true if all the colors in the image are shades of gray - #[rust_name = "all_gray"] - fn allGray(self: &QImage) -> bool; - /// For 32-bit images, this function is equivalent to allGray(). /// For color indexed images, this function returns true if color(i) is QRgb(i, i, i) /// for all indexes of the color table; otherwise returns false. @@ -40,71 +61,47 @@ mod ffi { #[rust_name = "has_alpha_channel"] fn hasAlphaChannel(self: &QImage) -> bool; - /// Returns a sub-area of the image as a new image. - fn copy(self: &QImage, rect: &QRect) -> QImage; + /// Returns the height of the image. + fn height(self: &QImage) -> i32; - /// Returns the size of the color table for the image. - #[rust_name = "color_count"] - fn colorCount(self: &QImage) -> i32; + /// Returns the enclosing rectangle (0, 0, width(), height()) of the image. + fn rect(self: &QImage) -> QRect; /// Resizes the color table to contain colorCount entries. #[rust_name = "set_color_count"] fn setColorCount(self: &mut QImage, colorCount: i32); - /// Returns the depth of the image. - fn depth(self: &QImage) -> i32; - - /// Fills the entire image with the given color. - fn fill(self: &mut QImage, color: &QColor); - - /// Returns the height of the image. - fn height(self: &QImage) -> i32; + /// Sets the alpha channel of this image to the given alphaChannel. + #[rust_name = "set_alpha_channel"] + fn setAlphaChannel(self: &mut QImage, alphaChannel: &QImage); - /// Returns the width of the image. - fn width(self: &QImage) -> i32; + /// Sets the number of pixels by which the image is intended to be offset by when positioning relative to other images, to offset. + #[rust_name = "set_offset"] + fn setOffset(self: &mut QImage, point: &QPoint); /// Returns the size of the image. fn size(self: &QImage) -> QSize; - /// Returns the enclosing rectangle (0, 0, width(), height()) of the image. - fn rect(self: &QImage) -> QRect; - - /// Returns the color of the pixel at coordinates (x, y) as a QColor. - #[rust_name = "pixel_color"] - fn pixelColor(self: &QImage, x: i32, y: i32) -> QColor; - /// Sets the pixel color at (x, y) to color. #[rust_name = "set_pixel_color"] fn setPixelColor(self: &mut QImage, x: i32, y: i32, color: &QColor); - /// Returns the number of pixels that fit horizontally in a physical meter. - #[rust_name = "dots_per_meterx"] - fn dotsPerMeterX(self: &QImage) -> i32; - - /// Returns the number of pixels that fit vertically in a physical meter. - #[rust_name = "dots_per_metery"] - fn dotsPerMeterY(self: &QImage) -> i32; - - /// Returns true if pos is a valid coordinate pair within the image. - fn valid(self: &QImage, x: i32, y: i32) -> bool; - /// Returns the number of pixels by which the image is intended to be offset by when positioning relative to other images. fn offset(self: &QImage) -> QPoint; - /// Sets the number of pixels by which the image is intended to be offset by when positioning relative to other images, to offset. - #[rust_name = "set_offset"] - fn setOffset(self: &mut QImage, point: &QPoint); + /// Returns the color of the pixel at coordinates (x, y) as a QColor. + #[rust_name = "pixel_color"] + fn pixelColor(self: &QImage, x: i32, y: i32) -> QColor; /// Returns the pixel index at (x, y). #[rust_name = "pixel_index"] fn pixelIndex(self: &QImage, x: i32, y: i32) -> i32; - /// Mirrors of the image in the horizontal and/or the vertical direction depending on whether horizontal and vertical are set to true or false. - fn mirror(self: &mut QImage, horizontal: bool, vertical: bool); + /// Returns true if pos is a valid coordinate pair within the image. + fn valid(self: &QImage, x: i32, y: i32) -> bool; - /// Sets the alpha channel of this image to the given alphaChannel. - #[rust_name = "set_alpha_channel"] - fn setAlphaChannel(self: &mut QImage, alphaChannel: &QImage); + /// Returns the width of the image. + fn width(self: &QImage) -> i32; } #[namespace = "rust::cxxqtlib1"] @@ -124,7 +121,20 @@ mod ffi { fn qimageCacheKey(image: &QImage) -> i64; } } +/* +#[cfg(qt_version_major = "6")] +#[cxx::bridge] +mod ffi_qt_6 { + unsafe extern "C++" { + include!("cxx-qt-lib/qimage.h"); + type QImage = super::QImage; + /// Mirrors of the image in the horizontal and/or the vertical direction depending on whether horizontal and vertical are set to true or false. + // #[cfg(qt_version_major = "6")] + fn mirror(self: &mut QImage, horizontal: bool, vertical: bool); + } +} +*/ /// > ⚠ **Warning**: The QImage API in CXX-Qt-lib is not yet complete and subject to change. /// /// This struct is the Rust representation of the [`QImage`](https://doc.qt.io/qt-6/qimage.html) From 5737c8a74b477f8cba78e75d3e19cafa43c6ca99 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Tue, 21 Nov 2023 15:09:50 +0100 Subject: [PATCH 03/18] Reorder method --- crates/cxx-qt-lib/src/gui/qimage.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/cxx-qt-lib/src/gui/qimage.rs b/crates/cxx-qt-lib/src/gui/qimage.rs index a76bc335c..bf947c630 100644 --- a/crates/cxx-qt-lib/src/gui/qimage.rs +++ b/crates/cxx-qt-lib/src/gui/qimage.rs @@ -79,13 +79,13 @@ mod ffi { #[rust_name = "set_offset"] fn setOffset(self: &mut QImage, point: &QPoint); - /// Returns the size of the image. - fn size(self: &QImage) -> QSize; - /// Sets the pixel color at (x, y) to color. #[rust_name = "set_pixel_color"] fn setPixelColor(self: &mut QImage, x: i32, y: i32, color: &QColor); + /// Returns the size of the image. + fn size(self: &QImage) -> QSize; + /// Returns the number of pixels by which the image is intended to be offset by when positioning relative to other images. fn offset(self: &QImage) -> QPoint; From 9df793e99699c22111baf9348265daf65f6948df Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Tue, 21 Nov 2023 15:17:12 +0100 Subject: [PATCH 04/18] Add swap method --- crates/cxx-qt-lib/src/gui/qimage.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/cxx-qt-lib/src/gui/qimage.rs b/crates/cxx-qt-lib/src/gui/qimage.rs index bf947c630..6f130178a 100644 --- a/crates/cxx-qt-lib/src/gui/qimage.rs +++ b/crates/cxx-qt-lib/src/gui/qimage.rs @@ -86,6 +86,9 @@ mod ffi { /// Returns the size of the image. fn size(self: &QImage) -> QSize; + /// Swaps image other with this image. This operation is very fast and never fails. + fn swap(self: &mut QImage, other: &mut QImage); + /// Returns the number of pixels by which the image is intended to be offset by when positioning relative to other images. fn offset(self: &QImage) -> QPoint; From a4fcf4d007e6caf58202c6617a8aaf5a04c80cfa Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Tue, 21 Nov 2023 16:53:25 +0100 Subject: [PATCH 05/18] Add TransformationMode enum support --- crates/cxx-qt-lib/src/core/mod.rs | 1 + crates/cxx-qt-lib/src/core/qt.rs | 8 ++++++++ crates/cxx-qt-lib/src/gui/qimage.rs | 14 ++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/crates/cxx-qt-lib/src/core/mod.rs b/crates/cxx-qt-lib/src/core/mod.rs index c4349c8b2..1d50d6a9b 100644 --- a/crates/cxx-qt-lib/src/core/mod.rs +++ b/crates/cxx-qt-lib/src/core/mod.rs @@ -65,6 +65,7 @@ pub use qstringlist::QStringList; mod qt; pub use qt::{ AspectRatioMode, CaseSensitivity, ConnectionType, DateFormat, SplitBehaviorFlags, TimeSpec, + TransformationMode, }; mod qtime; diff --git a/crates/cxx-qt-lib/src/core/qt.rs b/crates/cxx-qt-lib/src/core/qt.rs index d846664d3..d1eb098b2 100644 --- a/crates/cxx-qt-lib/src/core/qt.rs +++ b/crates/cxx-qt-lib/src/core/qt.rs @@ -67,6 +67,12 @@ mod ffi { TimeZone, } + #[repr(i32)] + enum TransformationMode { + FastTransformation, + SmoothTransformation, + } + unsafe extern "C++" { include!("cxx-qt-lib/qt.h"); type AspectRatioMode; @@ -75,9 +81,11 @@ mod ffi { type DateFormat; type SplitBehaviorFlags; type TimeSpec; + type TransformationMode; } } pub use ffi::{ AspectRatioMode, CaseSensitivity, ConnectionType, DateFormat, SplitBehaviorFlags, TimeSpec, + TransformationMode, }; diff --git a/crates/cxx-qt-lib/src/gui/qimage.rs b/crates/cxx-qt-lib/src/gui/qimage.rs index 6f130178a..c9c635462 100644 --- a/crates/cxx-qt-lib/src/gui/qimage.rs +++ b/crates/cxx-qt-lib/src/gui/qimage.rs @@ -8,6 +8,12 @@ use std::mem::MaybeUninit; #[cxx::bridge] mod ffi { + #[namespace = "Qt"] + unsafe extern "C++" { + include!("cxx-qt-lib/qt.h"); + type TransformationMode = crate::TransformationMode; + } + unsafe extern "C++" { include!("cxx-qt-lib/qimage.h"); type QImage = super::QImage; @@ -67,6 +73,14 @@ mod ffi { /// Returns the enclosing rectangle (0, 0, width(), height()) of the image. fn rect(self: &QImage) -> QRect; + /// Returns a scaled copy of the image. The returned image is scaled to the given height using the specified transformation mode. + #[rust_name = "scaled_to_height"] + fn scaledToHeight(self: &QImage, width: i32, mode: TransformationMode) -> QImage; + + /// Returns a scaled copy of the image. The returned image is scaled to the given width using the specified transformation mode. + #[rust_name = "scaled_to_width"] + fn scaledToWidth(self: &QImage, width: i32, mode: TransformationMode) -> QImage; + /// Resizes the color table to contain colorCount entries. #[rust_name = "set_color_count"] fn setColorCount(self: &mut QImage, colorCount: i32); From f83fbc5cbc2ef585a7e93f02c91e40e1683e3549 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Wed, 22 Nov 2023 09:38:58 +0100 Subject: [PATCH 06/18] Add enum doc --- crates/cxx-qt-lib/src/core/qt.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/cxx-qt-lib/src/core/qt.rs b/crates/cxx-qt-lib/src/core/qt.rs index d1eb098b2..57647f99c 100644 --- a/crates/cxx-qt-lib/src/core/qt.rs +++ b/crates/cxx-qt-lib/src/core/qt.rs @@ -67,9 +67,12 @@ mod ffi { TimeZone, } + /// This enum type defines whether image transformations (e.g., scaling) should be smooth or not. #[repr(i32)] enum TransformationMode { + /// The transformation is performed quickly, with no smoothing. FastTransformation, + /// The resulting image is transformed using bilinear filtering. SmoothTransformation, } From 8342957b29c7171e4068633e93b10e200e9d942a Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Wed, 22 Nov 2023 09:45:10 +0100 Subject: [PATCH 07/18] Add missing scaled method --- crates/cxx-qt-lib/src/gui/qimage.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/cxx-qt-lib/src/gui/qimage.rs b/crates/cxx-qt-lib/src/gui/qimage.rs index c9c635462..f0ee6bad4 100644 --- a/crates/cxx-qt-lib/src/gui/qimage.rs +++ b/crates/cxx-qt-lib/src/gui/qimage.rs @@ -12,6 +12,7 @@ mod ffi { unsafe extern "C++" { include!("cxx-qt-lib/qt.h"); type TransformationMode = crate::TransformationMode; + type AspectRatioMode = crate::AspectRatioMode; } unsafe extern "C++" { @@ -73,6 +74,15 @@ mod ffi { /// Returns the enclosing rectangle (0, 0, width(), height()) of the image. fn rect(self: &QImage) -> QRect; + /// Returns a copy of the image scaled to a rectangle with the given width and height according to the given aspectRatioMode and transformMode. + fn scaled( + self: &QImage, + width: i32, + height: i32, + aspectRatioMode: AspectRatioMode, + transformMode: TransformationMode, + ) -> QImage; + /// Returns a scaled copy of the image. The returned image is scaled to the given height using the specified transformation mode. #[rust_name = "scaled_to_height"] fn scaledToHeight(self: &QImage, width: i32, mode: TransformationMode) -> QImage; From 6df0049831beff1ee1f3ca6d9c9550e3afc6ad64 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Wed, 22 Nov 2023 11:27:00 +0100 Subject: [PATCH 08/18] Remove qt6 support at the moment. It failed. We need to find another method --- crates/cxx-qt-lib/src/gui/qimage.rs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/crates/cxx-qt-lib/src/gui/qimage.rs b/crates/cxx-qt-lib/src/gui/qimage.rs index f0ee6bad4..1ac9561fc 100644 --- a/crates/cxx-qt-lib/src/gui/qimage.rs +++ b/crates/cxx-qt-lib/src/gui/qimage.rs @@ -148,20 +148,7 @@ mod ffi { fn qimageCacheKey(image: &QImage) -> i64; } } -/* -#[cfg(qt_version_major = "6")] -#[cxx::bridge] -mod ffi_qt_6 { - unsafe extern "C++" { - include!("cxx-qt-lib/qimage.h"); - type QImage = super::QImage; - /// Mirrors of the image in the horizontal and/or the vertical direction depending on whether horizontal and vertical are set to true or false. - // #[cfg(qt_version_major = "6")] - fn mirror(self: &mut QImage, horizontal: bool, vertical: bool); - } -} -*/ /// > ⚠ **Warning**: The QImage API in CXX-Qt-lib is not yet complete and subject to change. /// /// This struct is the Rust representation of the [`QImage`](https://doc.qt.io/qt-6/qimage.html) From 6d77fb26b5eb1c422b9467fa18bef00a9f3016d1 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Thu, 23 Nov 2023 10:02:13 +0100 Subject: [PATCH 09/18] Fix typo in run method name --- crates/cxx-qt-lib/src/gui/qimage.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/cxx-qt-lib/src/gui/qimage.rs b/crates/cxx-qt-lib/src/gui/qimage.rs index 1ac9561fc..eb37a3d37 100644 --- a/crates/cxx-qt-lib/src/gui/qimage.rs +++ b/crates/cxx-qt-lib/src/gui/qimage.rs @@ -42,11 +42,11 @@ mod ffi { fn depth(self: &QImage) -> i32; /// Returns the number of pixels that fit horizontally in a physical meter. - #[rust_name = "dots_per_meterx"] + #[rust_name = "dots_per_meter_x"] fn dotsPerMeterX(self: &QImage) -> i32; /// Returns the number of pixels that fit vertically in a physical meter. - #[rust_name = "dots_per_metery"] + #[rust_name = "dots_per_meter_y"] fn dotsPerMeterY(self: &QImage) -> i32; /// Fills the entire image with the given color. From ad7e31d5a0aaafbb767df655987e0e289fb2b2e7 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Thu, 23 Nov 2023 10:02:30 +0100 Subject: [PATCH 10/18] Remove comment --- crates/cxx-qt-lib/src/gui/qimage.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/crates/cxx-qt-lib/src/gui/qimage.rs b/crates/cxx-qt-lib/src/gui/qimage.rs index eb37a3d37..1472e9dc7 100644 --- a/crates/cxx-qt-lib/src/gui/qimage.rs +++ b/crates/cxx-qt-lib/src/gui/qimage.rs @@ -149,8 +149,6 @@ mod ffi { } } -/// > ⚠ **Warning**: The QImage API in CXX-Qt-lib is not yet complete and subject to change. -/// /// This struct is the Rust representation of the [`QImage`](https://doc.qt.io/qt-6/qimage.html) /// class. /// From 06332ebce704e9c6986f156cbd4a33a477677aaf Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Thu, 23 Nov 2023 10:02:41 +0100 Subject: [PATCH 11/18] Add clone method --- crates/cxx-qt-lib/src/gui/qimage.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/cxx-qt-lib/src/gui/qimage.rs b/crates/cxx-qt-lib/src/gui/qimage.rs index 1472e9dc7..e8bf09f92 100644 --- a/crates/cxx-qt-lib/src/gui/qimage.rs +++ b/crates/cxx-qt-lib/src/gui/qimage.rs @@ -163,6 +163,13 @@ pub struct QImage { _data: MaybeUninit<[usize; 3]>, } +impl Clone for QImage { + /// Constructs a copy of other. + fn clone(&self) -> Self { + self.copy(&self.rect()) + } +} + // Safety: // // Static checks on the C++ side to ensure the size & alignment is the same. From 4995d7d756cdda9c846b0ebfbccefd52678d156a Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Thu, 23 Nov 2023 10:42:37 +0100 Subject: [PATCH 12/18] Add autotest about default values --- crates/cxx-qt-lib/src/gui/qimage.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/crates/cxx-qt-lib/src/gui/qimage.rs b/crates/cxx-qt-lib/src/gui/qimage.rs index e8bf09f92..6d8b4e376 100644 --- a/crates/cxx-qt-lib/src/gui/qimage.rs +++ b/crates/cxx-qt-lib/src/gui/qimage.rs @@ -135,6 +135,10 @@ mod ffi { unsafe extern "C++" { include!("cxx-qt-lib/common.h"); + #[doc(hidden)] + #[rust_name = "qimage_init_default"] + fn construct() -> QImage; + #[doc(hidden)] #[rust_name = "qimage_drop"] fn drop(image: &mut QImage); @@ -170,6 +174,13 @@ impl Clone for QImage { } } +impl Default for QImage { + /// Constructs a null image. + fn default() -> Self { + ffi::qimage_init_default() + } +} + // Safety: // // Static checks on the C++ side to ensure the size & alignment is the same. @@ -205,3 +216,18 @@ impl QImage { ffi::qimage_cache_key(self) } } + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_default_values() { + let default_image = QImage::default(); + assert_eq!(default_image.all_gray(), true); + assert_eq!(default_image.is_null(), true); + assert_eq!(default_image.width(), 0); + assert_eq!(default_image.height(), 0); + assert_eq!(default_image.depth(), 0); + assert_eq!(default_image.size().is_null(), true); + } +} From dc4a3ff080458968ff8585ed9021aa2d670e5730 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Thu, 23 Nov 2023 11:33:53 +0100 Subject: [PATCH 13/18] Add QImage(height, width, format) constructor --- crates/cxx-qt-lib/src/gui/qimage.rs | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/crates/cxx-qt-lib/src/gui/qimage.rs b/crates/cxx-qt-lib/src/gui/qimage.rs index 6d8b4e376..052c895d9 100644 --- a/crates/cxx-qt-lib/src/gui/qimage.rs +++ b/crates/cxx-qt-lib/src/gui/qimage.rs @@ -15,6 +15,50 @@ mod ffi { type AspectRatioMode = crate::AspectRatioMode; } + /// The type of image format available in Qt. + #[repr(i32)] + #[namespace = "rust::cxxqtlib1"] + enum QImageFormat { + Format_Invalid, + Format_Mono, + Format_MonoLSB, + Format_Indexed8, + Format_RGB32, + Format_ARGB32, + Format_ARGB32_Premultiplied, + Format_RGB16, + Format_ARGB8565_Premultiplied, + Format_RGB666, + Format_ARGB6666_Premultiplied, + Format_RGB555, + Format_ARGB8555_Premultiplied, + Format_RGB888, + Format_RGB444, + Format_ARGB4444_Premultiplied, + Format_RGBX8888, + Format_RGBA8888, + Format_RGBA8888_Premultiplied, + Format_BGR30, + Format_A2BGR30_Premultiplied, + Format_RGB30, + Format_A2RGB30_Premultiplied, + Format_Alpha8, + Format_Grayscale8, + Format_RGBX64, + Format_RGBA64, + Format_RGBA64_Premultiplied, + Format_Grayscale16, + Format_BGR888, + /* Qt 6.2 + Format_RGBX16FPx4, + Format_RGBA16FPx4, + Format_RGBA16FPx4_Premultiplied, + Format_RGBX32FPx4, + Format_RGBA32FPx4, + Format_RGBA32FPx4_Premultiplied, + */ + } + unsafe extern "C++" { include!("cxx-qt-lib/qimage.h"); type QImage = super::QImage; @@ -134,11 +178,16 @@ mod ffi { #[namespace = "rust::cxxqtlib1"] unsafe extern "C++" { include!("cxx-qt-lib/common.h"); + type QImageFormat; #[doc(hidden)] #[rust_name = "qimage_init_default"] fn construct() -> QImage; + #[doc(hidden)] + #[rust_name = "qimage_init_from_width_and_height_and_image_format"] + fn construct(width: i32, height: i32, format: QImageFormat) -> QImage; + #[doc(hidden)] #[rust_name = "qimage_drop"] fn drop(image: &mut QImage); @@ -215,6 +264,15 @@ impl QImage { pub fn cache_key(&self) -> i64 { ffi::qimage_cache_key(self) } + + /// Construct a Rust QImage from a given width, height, and QImage Format + pub fn from_height_width_and_format( + width: i32, + height: i32, + format: ffi::QImageFormat, + ) -> Self { + ffi::qimage_init_from_width_and_height_and_image_format(width, height, format) + } } #[cfg(test)] From a49ed41eea705528b258b12331eb4e9d2a8eeeab Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Thu, 23 Nov 2023 11:34:33 +0100 Subject: [PATCH 14/18] Add constructor support --- crates/cxx-qt-lib/src/gui/qimage.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/cxx-qt-lib/src/gui/qimage.rs b/crates/cxx-qt-lib/src/gui/qimage.rs index 052c895d9..26d07eb8a 100644 --- a/crates/cxx-qt-lib/src/gui/qimage.rs +++ b/crates/cxx-qt-lib/src/gui/qimage.rs @@ -288,4 +288,13 @@ mod tests { assert_eq!(default_image.depth(), 0); assert_eq!(default_image.size().is_null(), true); } + + #[test] + fn test_create_qimage_from_format() { + let qimage = QImage::from_height_width_and_format(50, 70, ffi::QImageFormat::Format_Mono); + assert_eq!(qimage.width(), 50); + assert_eq!(qimage.height(), 70); + assert_eq!(default_image.is_null(), false); + assert_eq!(default_image.size().is_null(), false); + } } From 62176348d6158d0e2dc04dd167af58b13390b9f1 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Thu, 23 Nov 2023 11:53:28 +0100 Subject: [PATCH 15/18] Add format() method + add autotest --- crates/cxx-qt-lib/src/gui/qimage.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/crates/cxx-qt-lib/src/gui/qimage.rs b/crates/cxx-qt-lib/src/gui/qimage.rs index 26d07eb8a..d02fc42fb 100644 --- a/crates/cxx-qt-lib/src/gui/qimage.rs +++ b/crates/cxx-qt-lib/src/gui/qimage.rs @@ -18,6 +18,7 @@ mod ffi { /// The type of image format available in Qt. #[repr(i32)] #[namespace = "rust::cxxqtlib1"] + #[derive(Debug)] enum QImageFormat { Format_Invalid, Format_Mono, @@ -96,6 +97,9 @@ mod ffi { /// Fills the entire image with the given color. fn fill(self: &mut QImage, color: &QColor); + /// Returns the format of the image. + fn format(self: &QImage) -> QImageFormat; + /// Whether the QImage is null. /// /// This means that the QImage has all parameters set to zero and no allocated data. @@ -294,7 +298,16 @@ mod tests { let qimage = QImage::from_height_width_and_format(50, 70, ffi::QImageFormat::Format_Mono); assert_eq!(qimage.width(), 50); assert_eq!(qimage.height(), 70); - assert_eq!(default_image.is_null(), false); - assert_eq!(default_image.size().is_null(), false); + assert_eq!(qimage.is_null(), false); + assert_eq!(qimage.format(), ffi::QImageFormat::Format_Mono); + } + + #[test] + fn test_copy() { + let qimage = QImage::from_height_width_and_format(50, 70, ffi::QImageFormat::Format_Mono); + let qimage2 = qimage.copy(&qimage.rect()); + assert_eq!(qimage.width(), qimage2.width()); + assert_eq!(qimage.height(), qimage2.height()); + assert_eq!(qimage.format(), qimage2.format()); } } From a4977429a01c1987bcfe544c132a404fd63c2523 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Thu, 23 Nov 2023 12:44:25 +0100 Subject: [PATCH 16/18] Fix cargo clippy --- crates/cxx-qt-lib/src/gui/qimage.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/cxx-qt-lib/src/gui/qimage.rs b/crates/cxx-qt-lib/src/gui/qimage.rs index d02fc42fb..278606a78 100644 --- a/crates/cxx-qt-lib/src/gui/qimage.rs +++ b/crates/cxx-qt-lib/src/gui/qimage.rs @@ -285,12 +285,12 @@ mod tests { #[test] fn test_default_values() { let default_image = QImage::default(); - assert_eq!(default_image.all_gray(), true); - assert_eq!(default_image.is_null(), true); + assert!(default_image.all_gray()); + assert!(default_image.is_null()); assert_eq!(default_image.width(), 0); assert_eq!(default_image.height(), 0); assert_eq!(default_image.depth(), 0); - assert_eq!(default_image.size().is_null(), true); + assert!(default_image.size().is_null()); } #[test] @@ -298,7 +298,7 @@ mod tests { let qimage = QImage::from_height_width_and_format(50, 70, ffi::QImageFormat::Format_Mono); assert_eq!(qimage.width(), 50); assert_eq!(qimage.height(), 70); - assert_eq!(qimage.is_null(), false); + assert!(!qimage.is_null()); assert_eq!(qimage.format(), ffi::QImageFormat::Format_Mono); } From 2851a79ec3a31105044bf21b7abe8d39f95a321c Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Fri, 24 Nov 2023 14:07:19 +0100 Subject: [PATCH 17/18] Improve CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 759b1dcbe..1c14e3c7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `extern "C++Qt"` block support for declaring existing types with methods and signals - `#[qenum]` attribute for `Q_ENUM` and `Q_ENUM_NS` support - `qnamespace!` macro to support exposing namespaced enums to QML +- Support for further types: `QLine`, `QLineF`, `QImage` ### Changed From 33aae31030c59c86d4549ff371ff2b78baf9905f Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Fri, 24 Nov 2023 14:48:07 +0100 Subject: [PATCH 18/18] 0.6 was already released. Move it on unreleased section --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c14e3c7b..8dd8e931f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/KDAB/cxx-qt/compare/v0.6.0...HEAD) +### Added + +- Support for further types: `QLine`, `QLineF`, `QImage` + ## [0.6.0](https://github.com/KDAB/cxx-qt/compare/v0.5.3...v0.6.0) - 2023-11-17 ### Added @@ -29,7 +33,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `extern "C++Qt"` block support for declaring existing types with methods and signals - `#[qenum]` attribute for `Q_ENUM` and `Q_ENUM_NS` support - `qnamespace!` macro to support exposing namespaced enums to QML -- Support for further types: `QLine`, `QLineF`, `QImage` ### Changed