From 371f08c97c81ead6b2ea0987ccc2d3e8040ed66f Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Fri, 23 Feb 2024 14:39:30 +0100 Subject: [PATCH 01/14] Add QBrush support --- .../cxx-qt-lib-headers/include/gui/qbrush.h | 22 +++++ crates/cxx-qt-lib-headers/src/lib.rs | 1 + crates/cxx-qt-lib/build.rs | 2 + crates/cxx-qt-lib/src/gui/mod.rs | 3 + crates/cxx-qt-lib/src/gui/qbrush.cpp | 21 +++++ crates/cxx-qt-lib/src/gui/qbrush.rs | 89 +++++++++++++++++++ 6 files changed, 138 insertions(+) create mode 100644 crates/cxx-qt-lib-headers/include/gui/qbrush.h create mode 100644 crates/cxx-qt-lib/src/gui/qbrush.cpp create mode 100644 crates/cxx-qt-lib/src/gui/qbrush.rs diff --git a/crates/cxx-qt-lib-headers/include/gui/qbrush.h b/crates/cxx-qt-lib-headers/include/gui/qbrush.h new file mode 100644 index 000000000..951dabab4 --- /dev/null +++ b/crates/cxx-qt-lib-headers/include/gui/qbrush.h @@ -0,0 +1,22 @@ +// clang-format off +// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company +// clang-format on +// SPDX-FileContributor: Laurent Montel +// +// SPDX-License-Identifier: MIT OR Apache-2.0 +#pragma once + +#include + +#include "rust/cxx.h" + +// Define namespace otherwise we hit a GCC bug +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480 +namespace rust { + +template<> +struct IsRelocatable : ::std::true_type +{ +}; + +} // namespace rust diff --git a/crates/cxx-qt-lib-headers/src/lib.rs b/crates/cxx-qt-lib-headers/src/lib.rs index 9e25d96ff..8f471f195 100644 --- a/crates/cxx-qt-lib-headers/src/lib.rs +++ b/crates/cxx-qt-lib-headers/src/lib.rs @@ -42,6 +42,7 @@ pub fn build_opts() -> cxx_qt_build::CxxQtBuildersOpts { "qmodelindex.h", ), #[cfg(feature = "qt_gui")] + (include_str!("../include/gui/qbrush.h"), "qbrush.h"), (include_str!("../include/gui/qpen.h"), "qpen.h"), ( include_str!("../include/core/qpersistentmodelindex.h"), diff --git a/crates/cxx-qt-lib/build.rs b/crates/cxx-qt-lib/build.rs index d0ad14c61..b66530297 100644 --- a/crates/cxx-qt-lib/build.rs +++ b/crates/cxx-qt-lib/build.rs @@ -154,6 +154,7 @@ fn main() { "core/qlist/qlist_qcolor", "core/qvariant/qvariant_qcolor", "core/qvector/qvector_qcolor", + "gui/qbrush", "gui/qcolor", "gui/qfont", "gui/qguiapplication", @@ -218,6 +219,7 @@ fn main() { if feature_qt_gui_enabled { cpp_files.extend([ + "gui/qbrush", "gui/qcolor", "gui/qfont", "gui/qguiapplication", diff --git a/crates/cxx-qt-lib/src/gui/mod.rs b/crates/cxx-qt-lib/src/gui/mod.rs index 3aa8e6b01..8d7025e86 100644 --- a/crates/cxx-qt-lib/src/gui/mod.rs +++ b/crates/cxx-qt-lib/src/gui/mod.rs @@ -38,3 +38,6 @@ pub use qpainterpath::QPainterPath; mod qpainter; pub use qpainter::QPainter; + +mod qbrush; +pub use qbrush::QBrush; diff --git a/crates/cxx-qt-lib/src/gui/qbrush.cpp b/crates/cxx-qt-lib/src/gui/qbrush.cpp new file mode 100644 index 000000000..2f784d423 --- /dev/null +++ b/crates/cxx-qt-lib/src/gui/qbrush.cpp @@ -0,0 +1,21 @@ +// clang-format off +// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company +// clang-format on +// SPDX-FileContributor: Laurent Montel +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +#include "cxx-qt-lib/qbrush.h" + +#include "../assertion_utils.h" + +// https://code.qt.io/cgit/qt/qtbase.git/tree/src/gui/painting/qbrush.h?h=v5.15.6-lts-lgpl#n130 +// https://code.qt.io/cgit/qt/qtbase.git/tree/src/gui/painting/qbrush.h?h=v6.2.4#n123 +assert_alignment_and_size(QBrush, alignof(::std::size_t), sizeof(::std::size_t)); + +static_assert(!::std::is_trivially_copy_assignable::value); +static_assert(!::std::is_trivially_copy_constructible::value); + +static_assert(!::std::is_trivially_destructible::value); + +static_assert(QTypeInfo::isRelocatable); diff --git a/crates/cxx-qt-lib/src/gui/qbrush.rs b/crates/cxx-qt-lib/src/gui/qbrush.rs new file mode 100644 index 000000000..79c9db2b9 --- /dev/null +++ b/crates/cxx-qt-lib/src/gui/qbrush.rs @@ -0,0 +1,89 @@ +// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Laurent Montel +// +// SPDX-License-Identifier: MIT OR Apache-2.0 +use cxx::{type_id, ExternType}; +use std::mem::MaybeUninit; + +#[cxx::bridge] +mod ffi { + unsafe extern "C++" { + include!("cxx-qt-lib/qbrush.h"); + type QBrush = super::QBrush; + include!("cxx-qt-lib/qcolor.h"); + type QColor = crate::QColor; + include!("cxx-qt-lib/qimage.h"); + type QImage = crate::QImage; + + /// Returns true if the brush is fully opaque otherwise false + #[rust_name = "is_opaque"] + fn isOpaque(self: &QBrush) -> bool; + + /// Returns the brush color. + fn color(self: &QBrush) -> &QColor; + + /// Sets the brush color to the given color. + #[rust_name = "set_color"] + fn setColor(self: &mut QBrush, color: &QColor); + + /// Sets the brush image to image. The style is set to Qt::TexturePattern. + #[rust_name = "set_texture_image"] + fn setTextureImage(self: &mut QBrush, image: &QImage); + + /// Returns the custom brush pattern, or a null image if no custom brush pattern has been set. + #[rust_name = "texture_image"] + fn textureImage(self: &QBrush) -> QImage; + } + + #[namespace = "rust::cxxqtlib1"] + unsafe extern "C++" { + include!("cxx-qt-lib/common.h"); + + #[doc(hidden)] + #[rust_name = "qbrush_init_default"] + fn construct() -> QBrush; + + #[doc(hidden)] + #[rust_name = "qbrush_drop"] + fn drop(brush: &mut QBrush); + + #[doc(hidden)] + #[rust_name = "qbrush_clone"] + fn construct(brush: &QBrush) -> QBrush; + } +} + +#[repr(C)] +pub struct QBrush { + #[cfg(qt_version_major = "5")] + _cspec: MaybeUninit<[i32; 2]>, + #[cfg(qt_version_major = "6")] + _cspec: MaybeUninit, +} + +impl Default for QBrush { + /// Constructs a default black brush with the style Qt::NoBrush (i.e. this brush will not fill shapes). + fn default() -> Self { + ffi::qbrush_init_default() + } +} + +impl Drop for QBrush { + fn drop(&mut self) { + ffi::qbrush_drop(self); + } +} + +impl Clone for QBrush { + fn clone(&self) -> Self { + ffi::qbrush_clone(self) + } +} + +// Safety: +// +// Static checks on the C++ side to ensure the size is the same. +unsafe impl ExternType for QBrush { + type Id = type_id!("QBrush"); + type Kind = cxx::kind::Trivial; +} From 6293e869f5ac6283c6371d053fda9610398b0325 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Fri, 23 Feb 2024 15:11:04 +0100 Subject: [PATCH 02/14] Fix coding style --- crates/cxx-qt-lib/src/gui/qbrush.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/cxx-qt-lib/src/gui/qbrush.cpp b/crates/cxx-qt-lib/src/gui/qbrush.cpp index 2f784d423..6f9716ad7 100644 --- a/crates/cxx-qt-lib/src/gui/qbrush.cpp +++ b/crates/cxx-qt-lib/src/gui/qbrush.cpp @@ -11,7 +11,9 @@ // https://code.qt.io/cgit/qt/qtbase.git/tree/src/gui/painting/qbrush.h?h=v5.15.6-lts-lgpl#n130 // https://code.qt.io/cgit/qt/qtbase.git/tree/src/gui/painting/qbrush.h?h=v6.2.4#n123 -assert_alignment_and_size(QBrush, alignof(::std::size_t), sizeof(::std::size_t)); +assert_alignment_and_size(QBrush, + alignof(::std::size_t), + sizeof(::std::size_t)); static_assert(!::std::is_trivially_copy_assignable::value); static_assert(!::std::is_trivially_copy_constructible::value); From d0e4ef82ee365280326516b45e1cfb85544008ea Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Fri, 23 Feb 2024 15:14:36 +0100 Subject: [PATCH 03/14] Add QBush in changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8ea38124..7dd529b9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Support for further types: `QLine`, `QLineF`, `QImage`, `QPainter`, `QFont`, `QPen`, `QPolygon`, `QPolygonF` +- Support for further types: `QLine`, `QLineF`, `QImage`, `QPainter`, `QFont`, `QPen`, `QPolygon`, `QPolygonF`, `QBrush` - `internal_pointer_mut()` function on `QModelIndex` - `c_void` in CXX-Qt-lib for easy access to `void *` - `CxxQtThread` is now marked as `Sync` so that it can be used by reference From b86e46f214f16fc3f019fbb94503b6da7ca8e8db Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Fri, 23 Feb 2024 15:28:03 +0100 Subject: [PATCH 04/14] Add qbrush method in qpainter --- crates/cxx-qt-lib/src/gui/qpainter.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/crates/cxx-qt-lib/src/gui/qpainter.rs b/crates/cxx-qt-lib/src/gui/qpainter.rs index f1adc34de..b35c2e56c 100644 --- a/crates/cxx-qt-lib/src/gui/qpainter.rs +++ b/crates/cxx-qt-lib/src/gui/qpainter.rs @@ -39,11 +39,19 @@ mod ffi { type QPen = crate::QPen; include!("cxx-qt-lib/qpolygon.h"); type QPolygon = crate::QPolygon; + include!("cxx-qt-lib/qbrush.h"); + type QBrush = crate::QBrush; + + /// Returns the current background brush. + fn background(self: &QPainter) -> &QBrush; /// Returns the current background mode. #[rust_name = "background_mode"] fn backgroundMode(self: &QPainter) -> BGMode; + /// Returns the painter's current brush. + fn brush(self: &QPainter) -> &QBrush; + /// Returns the currently set brush origin. #[rust_name = "brush_origin"] fn brushOrigin(self: &QPainter) -> QPoint; @@ -131,6 +139,10 @@ mod ffi { #[rust_name = "erase_rect"] fn eraseRect(self: Pin<&mut QPainter>, rectangle: &QRectF); + /// Fills the given path using the given brush. The outline is not drawn. + #[rust_name = "fill_path"] + fn fillPath(self: Pin<&mut QPainter>, path: &QPainterPath, brush: &QBrush); + /// Fills the given rectangle with the color specified. #[rust_name = "fill_rect"] fn fillRect(self: Pin<&mut QPainter>, rectangle: &QRectF, color: &QColor); @@ -160,10 +172,18 @@ mod ffi { /// A save() must be followed by a corresponding restore(); the end() function unwinds the stack. fn save(self: Pin<&mut QPainter>); + /// Sets the background brush of the painter to the given brush. + #[rust_name = "set_background"] + fn setBackground(self: Pin<&mut QPainter>, brush: &QBrush); + /// Sets the background mode of the painter to the given mode #[rust_name = "set_background_mode"] fn setBackgroundMode(self: Pin<&mut QPainter>, mode: BGMode); + /// Sets the painter's brush to the given brush. + #[rust_name = "set_brush"] + fn setBrush(self: Pin<&mut QPainter>, brush: &QBrush); + /// Enables clipping if enable is true, or disables clipping if enable is false. #[rust_name = "set_clipping"] fn setClipping(self: Pin<&mut QPainter>, enable: bool); From 6fa9ebebe30228d47c10070a0abd9b575a2f657a Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Fri, 23 Feb 2024 16:40:06 +0100 Subject: [PATCH 05/14] Add qbrush method in qpen --- crates/cxx-qt-lib/src/gui/qpen.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/cxx-qt-lib/src/gui/qpen.rs b/crates/cxx-qt-lib/src/gui/qpen.rs index e456549e6..5ef2e5153 100644 --- a/crates/cxx-qt-lib/src/gui/qpen.rs +++ b/crates/cxx-qt-lib/src/gui/qpen.rs @@ -20,6 +20,11 @@ mod ffi { type QPen = super::QPen; include!("cxx-qt-lib/qcolor.h"); type QColor = crate::QColor; + include!("cxx-qt-lib/qbrush.h"); + type QBrush = crate::QBrush; + + /// Returns the brush used to fill strokes generated with this pen. + fn brush(self: &QPen) -> QBrush; /// Returns the pen's cap style. #[rust_name = "cap_style"] @@ -49,6 +54,10 @@ mod ffi { #[rust_name = "miter_limit"] fn miterLimit(self: &QPen) -> f64; + /// Sets the brush used to fill strokes generated with this pen to the given brush. + #[rust_name = "set_brush"] + fn setBrush(self: &mut QPen, brush: &QBrush); + /// Sets the pen's cap style to the given style. The default value is Qt::SquareCap. #[rust_name = "set_cap_style"] fn setCapStyle(self: &mut QPen, style: PenCapStyle); From 290ba1946e311829e3c2a00f7dec97e94fd45b87 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Fri, 23 Feb 2024 16:48:32 +0100 Subject: [PATCH 06/14] Implement BrushStyle --- crates/cxx-qt-lib/src/core/mod.rs | 6 +++--- crates/cxx-qt-lib/src/core/qt.rs | 29 +++++++++++++++++++++++++++-- crates/cxx-qt-lib/src/gui/qbrush.rs | 13 +++++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/crates/cxx-qt-lib/src/core/mod.rs b/crates/cxx-qt-lib/src/core/mod.rs index 58214834b..8c9db00e4 100644 --- a/crates/cxx-qt-lib/src/core/mod.rs +++ b/crates/cxx-qt-lib/src/core/mod.rs @@ -70,9 +70,9 @@ pub use qstringlist::QStringList; mod qt; pub use qt::{ - AspectRatioMode, BGMode, CaseSensitivity, ClipOperation, ConnectionType, DateFormat, FillRule, - LayoutDirection, PenCapStyle, PenJoinStyle, PenStyle, SplitBehaviorFlags, TimeSpec, - TransformationMode, + AspectRatioMode, BGMode, BrushStyle, CaseSensitivity, ClipOperation, ConnectionType, + DateFormat, FillRule, LayoutDirection, PenCapStyle, PenJoinStyle, PenStyle, 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 6903f64fc..866eeed63 100644 --- a/crates/cxx-qt-lib/src/core/qt.rs +++ b/crates/cxx-qt-lib/src/core/qt.rs @@ -134,6 +134,29 @@ mod ffi { IntersectClip, } + #[repr(i32)] + enum BrushStyle { + NoBrush, + SolidPattern, + Dense1Pattern, + Dense2Pattern, + Dense3Pattern, + Dense4Pattern, + Dense5Pattern, + Dense6Pattern, + Dense7Pattern, + HorPattern, + VerPattern, + CrossPattern, + BDiagPattern, + FDiagPattern, + DiagCrossPattern, + LinearGradientPattern, + RadialGradientPattern, + ConicalGradientPattern, + TexturePattern = 24, + } + unsafe extern "C++" { include!("cxx-qt-lib/qt.h"); type AspectRatioMode; @@ -149,12 +172,14 @@ mod ffi { type LayoutDirection; type BGMode; type ClipOperation; + type BrushStyle; } } pub use ffi::{ - AspectRatioMode, BGMode, CaseSensitivity, ClipOperation, DateFormat, FillRule, LayoutDirection, - PenCapStyle, PenJoinStyle, PenStyle, SplitBehaviorFlags, TimeSpec, TransformationMode, + AspectRatioMode, BGMode, BrushStyle, CaseSensitivity, ClipOperation, DateFormat, FillRule, + LayoutDirection, PenCapStyle, PenJoinStyle, PenStyle, SplitBehaviorFlags, TimeSpec, + TransformationMode, }; // Reexport ConnectionType from cxx-qt diff --git a/crates/cxx-qt-lib/src/gui/qbrush.rs b/crates/cxx-qt-lib/src/gui/qbrush.rs index 79c9db2b9..276e3a976 100644 --- a/crates/cxx-qt-lib/src/gui/qbrush.rs +++ b/crates/cxx-qt-lib/src/gui/qbrush.rs @@ -7,6 +7,12 @@ use std::mem::MaybeUninit; #[cxx::bridge] mod ffi { + #[namespace = "Qt"] + unsafe extern "C++" { + include!("cxx-qt-lib/qt.h"); + type BrushStyle = crate::BrushStyle; + } + unsafe extern "C++" { include!("cxx-qt-lib/qbrush.h"); type QBrush = super::QBrush; @@ -22,6 +28,10 @@ mod ffi { /// Returns the brush color. fn color(self: &QBrush) -> &QColor; + /// Sets the brush style to style. + #[rust_name = "set_style"] + fn setStyle(self: &mut QBrush, style: BrushStyle); + /// Sets the brush color to the given color. #[rust_name = "set_color"] fn setColor(self: &mut QBrush, color: &QColor); @@ -30,6 +40,9 @@ mod ffi { #[rust_name = "set_texture_image"] fn setTextureImage(self: &mut QBrush, image: &QImage); + /// Returns the brush style. + fn style(self: &QBrush) -> BrushStyle; + /// Returns the custom brush pattern, or a null image if no custom brush pattern has been set. #[rust_name = "texture_image"] fn textureImage(self: &QBrush) -> QImage; From 1c90139556a5712e982ee075f92b0ceafcdbfbad Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Thu, 29 Feb 2024 10:08:04 +0100 Subject: [PATCH 07/14] Fix _cspec value --- crates/cxx-qt-lib/src/gui/qbrush.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/crates/cxx-qt-lib/src/gui/qbrush.rs b/crates/cxx-qt-lib/src/gui/qbrush.rs index 276e3a976..9b99d8140 100644 --- a/crates/cxx-qt-lib/src/gui/qbrush.rs +++ b/crates/cxx-qt-lib/src/gui/qbrush.rs @@ -68,10 +68,7 @@ mod ffi { #[repr(C)] pub struct QBrush { - #[cfg(qt_version_major = "5")] - _cspec: MaybeUninit<[i32; 2]>, - #[cfg(qt_version_major = "6")] - _cspec: MaybeUninit, + _cspec: MaybeUninit, } impl Default for QBrush { From ef26b782841e5f28e9f5ca307cf8d95f7c0c6a07 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Thu, 29 Feb 2024 10:11:43 +0100 Subject: [PATCH 08/14] Add doc --- crates/cxx-qt-lib/src/core/qt.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/cxx-qt-lib/src/core/qt.rs b/crates/cxx-qt-lib/src/core/qt.rs index 866eeed63..a7e4adafe 100644 --- a/crates/cxx-qt-lib/src/core/qt.rs +++ b/crates/cxx-qt-lib/src/core/qt.rs @@ -134,26 +134,47 @@ mod ffi { IntersectClip, } + /// This enum type defines the brush styles supported by Qt, i.e. + /// the fill pattern of shapes drawn using QPainter. #[repr(i32)] enum BrushStyle { + /// No brush pattern. NoBrush, + /// Uniform color. SolidPattern, + /// Extremely dense brush pattern. Dense1Pattern, + /// Very dense brush pattern. Dense2Pattern, + /// Somewhat dense brush pattern. Dense3Pattern, + /// Half dense brush pattern. Dense4Pattern, + /// Somewhat sparse brush pattern. Dense5Pattern, + /// Very sparse brush pattern. Dense6Pattern, + /// Extremely sparse brush pattern. Dense7Pattern, + /// Horizontal lines. HorPattern, + /// Vertical lines. VerPattern, + /// Crossing horizontal and vertical lines. CrossPattern, + /// Backward diagonal lines. BDiagPattern, + /// Forward diagonal lines. FDiagPattern, + /// Crossing diagonal lines. DiagCrossPattern, + /// Linear gradient (set using a dedicated QBrush constructor). LinearGradientPattern, + /// Conical gradient (set using a dedicated QBrush constructor). RadialGradientPattern, + /// Radial gradient (set using a dedicated QBrush constructor). ConicalGradientPattern, + /// Custom pattern (see QBrush::setTexture()). TexturePattern = 24, } From 15979b689354785eb6eb91b11ea32d9dbd745082 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Thu, 29 Feb 2024 10:38:45 +0100 Subject: [PATCH 09/14] Add PartialEq + constructor from QImage/BrushStyle --- crates/cxx-qt-lib/src/gui/qbrush.rs | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/crates/cxx-qt-lib/src/gui/qbrush.rs b/crates/cxx-qt-lib/src/gui/qbrush.rs index 9b99d8140..1ed2b7c79 100644 --- a/crates/cxx-qt-lib/src/gui/qbrush.rs +++ b/crates/cxx-qt-lib/src/gui/qbrush.rs @@ -57,12 +57,23 @@ mod ffi { fn construct() -> QBrush; #[doc(hidden)] + #[rust_name = "qcolor_init_from_qimage"] + fn construct(image: &QImage) -> QBrush; + + #[doc(hidden)] + #[rust_name = "qcolor_init_from_brush_style"] + fn construct(brushstyle: &BrushStyle) -> QBrush; + #[rust_name = "qbrush_drop"] fn drop(brush: &mut QBrush); #[doc(hidden)] #[rust_name = "qbrush_clone"] fn construct(brush: &QBrush) -> QBrush; + + #[doc(hidden)] + #[rust_name = "qbrush_eq"] + fn operatorEq(a: &QBrush, b: &QBrush) -> bool; } } @@ -90,6 +101,26 @@ impl Clone for QBrush { } } +impl PartialEq for QBrush { + fn eq(&self, other: &Self) -> bool { + ffi::qbrush_eq(self, other) + } +} + +impl Eq for QBrush {} + +impl From<&ffi::QImage> for QBrush { + fn from(image: &ffi::QImage) -> Self { + ffi::qcolor_init_from_qimage(image) + } +} + +impl From<&ffi::BrushStyle> for QBrush { + fn from(brushstyle: &ffi::BrushStyle) -> Self { + ffi::qcolor_init_from_brush_style(brushstyle) + } +} + // Safety: // // Static checks on the C++ side to ensure the size is the same. From 8956943a47322e4ee8af2fdcaa260962043b9494 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Thu, 29 Feb 2024 11:17:09 +0100 Subject: [PATCH 10/14] Add test --- tests/qt_types_standalone/CMakeLists.txt | 1 + tests/qt_types_standalone/cpp/main.cpp | 2 ++ tests/qt_types_standalone/cpp/qbrush.h | 31 ++++++++++++++++++++ tests/qt_types_standalone/rust/build.rs | 1 + tests/qt_types_standalone/rust/src/lib.rs | 2 ++ tests/qt_types_standalone/rust/src/qbrush.rs | 30 +++++++++++++++++++ 6 files changed, 67 insertions(+) create mode 100644 tests/qt_types_standalone/cpp/qbrush.h create mode 100644 tests/qt_types_standalone/rust/src/qbrush.rs diff --git a/tests/qt_types_standalone/CMakeLists.txt b/tests/qt_types_standalone/CMakeLists.txt index df17f85e1..31bea8eef 100644 --- a/tests/qt_types_standalone/CMakeLists.txt +++ b/tests/qt_types_standalone/CMakeLists.txt @@ -49,6 +49,7 @@ target_link_libraries(${CRATE} INTERFACE add_executable(${APP_NAME} cpp/main.cpp cpp/qbytearray.h + cpp/qbrush.h cpp/qcolor.h cpp/qcoreapplication.h cpp/qdate.h diff --git a/tests/qt_types_standalone/cpp/main.cpp b/tests/qt_types_standalone/cpp/main.cpp index d6c24532f..c6432dd1e 100644 --- a/tests/qt_types_standalone/cpp/main.cpp +++ b/tests/qt_types_standalone/cpp/main.cpp @@ -7,6 +7,7 @@ #include #include +#include "qbrush.h" #include "qbytearray.h" #include "qcolor.h" #include "qcoreapplication.h" @@ -56,6 +57,7 @@ main(int argc, char* argv[]) }; runTest(QScopedPointer(new QByteArrayTest)); + runTest(QScopedPointer(new QBrushTest)); runTest(QScopedPointer(new QColorTest)); runTest(QScopedPointer(new QCoreApplicationTest)); runTest(QScopedPointer(new QDateTest)); diff --git a/tests/qt_types_standalone/cpp/qbrush.h b/tests/qt_types_standalone/cpp/qbrush.h new file mode 100644 index 000000000..3a7109843 --- /dev/null +++ b/tests/qt_types_standalone/cpp/qbrush.h @@ -0,0 +1,31 @@ +// clang-format off +// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company +// clang-format on +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 +#pragma once + +#include +#include + +#include "cxx-qt-gen/qbrush.cxx.h" + +class QBrushTest : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void construct() + { + const auto p = construct_qbrush(); + QCOMPARE(p.color(), QColor(Qt::red)); + } + + void clone() + { + const auto p = construct_qbrush(); + const auto c = clone_qbrush(p); + QCOMPARE(c.color(), QColor(Qt::red)); + } +}; diff --git a/tests/qt_types_standalone/rust/build.rs b/tests/qt_types_standalone/rust/build.rs index b1a4e265c..9fa90a7de 100644 --- a/tests/qt_types_standalone/rust/build.rs +++ b/tests/qt_types_standalone/rust/build.rs @@ -7,6 +7,7 @@ use cxx_qt_build::CxxQtBuilder; fn main() { CxxQtBuilder::new() + .file("src/qbrush.rs") .file("src/qbytearray.rs") .file("src/qcolor.rs") .file("src/qcoreapplication.rs") diff --git a/tests/qt_types_standalone/rust/src/lib.rs b/tests/qt_types_standalone/rust/src/lib.rs index 83c42df73..36d6ded7e 100644 --- a/tests/qt_types_standalone/rust/src/lib.rs +++ b/tests/qt_types_standalone/rust/src/lib.rs @@ -1,9 +1,11 @@ // SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company // SPDX-FileContributor: Andrew Hayzen // SPDX-FileContributor: Gerhard de Clercq +// SPDX-FileContributor: Laurent Montel // // SPDX-License-Identifier: MIT OR Apache-2.0 +mod qbrush; mod qbytearray; mod qcolor; mod qcoreapplication; diff --git a/tests/qt_types_standalone/rust/src/qbrush.rs b/tests/qt_types_standalone/rust/src/qbrush.rs new file mode 100644 index 000000000..36b88fc44 --- /dev/null +++ b/tests/qt_types_standalone/rust/src/qbrush.rs @@ -0,0 +1,30 @@ +// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Laurent Montel +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +use cxx_qt_lib::QBrush; +use cxx_qt_lib::QColor; + +#[cxx::bridge] +mod qpoint_cxx { + unsafe extern "C++" { + include!("cxx-qt-lib/qbrush.h"); + type QBrush = cxx_qt_lib::QBrush; + } + + extern "Rust" { + fn construct_qbrush() -> QBrush; + fn clone_qbrush(p: &QBrush) -> QBrush; + } +} + +fn construct_qbrush() -> QBrush { + let mut brush = QBrush::default(); + brush.set_color(&QColor::from_rgb(255, 0, 0)); + brush +} + +fn clone_qbrush(p: &QBrush) -> QBrush { + p.clone() +} From 49982d96d883292fbb681a01f46e5587e3ae8e36 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Thu, 29 Feb 2024 11:20:53 +0100 Subject: [PATCH 11/14] Fix coding style --- crates/cxx-qt-lib/src/core/mod.rs | 6 +++--- crates/cxx-qt-lib/src/core/qt.rs | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/cxx-qt-lib/src/core/mod.rs b/crates/cxx-qt-lib/src/core/mod.rs index a9c7e53b2..1614f00cb 100644 --- a/crates/cxx-qt-lib/src/core/mod.rs +++ b/crates/cxx-qt-lib/src/core/mod.rs @@ -70,9 +70,9 @@ pub use qstringlist::QStringList; mod qt; pub use qt::{ - AspectRatioMode, BGMode, BrushStyle, CaseSensitivity, ClipOperation, ConnectionType, DateFormat, FillRule, - LayoutDirection, PenCapStyle, PenJoinStyle, PenStyle, SizeMode, SplitBehaviorFlags, TimeSpec, - TransformationMode, + AspectRatioMode, BGMode, BrushStyle, CaseSensitivity, ClipOperation, ConnectionType, + DateFormat, FillRule, LayoutDirection, PenCapStyle, PenJoinStyle, PenStyle, SizeMode, + 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 cd6855bfa..6d58b9c2f 100644 --- a/crates/cxx-qt-lib/src/core/qt.rs +++ b/crates/cxx-qt-lib/src/core/qt.rs @@ -177,7 +177,7 @@ mod ffi { /// Custom pattern (see QBrush::setTexture()). TexturePattern = 24, } - + /// This enum is used by QPainter::drawRoundedRect() and QPainterPath::addRoundedRect() /// functions to specify the radii of rectangle corners with respect to the dimensions /// of the bounding rectangles specified. @@ -210,11 +210,11 @@ mod ffi { } pub use ffi::{ - AspectRatioMode, BGMode, BrushStyle, CaseSensitivity, ClipOperation, DateFormat, FillRule, - LayoutDirection, PenCapStyle, PenJoinStyle, PenStyle, SizeMode, SplitBehaviorFlags, TimeSpec, - AspectRatioMode, BGMode, BrushStyle, CaseSensitivity, ClipOperation, DateFormat, FillRule, LayoutDirection, - PenCapStyle, PenJoinStyle, PenStyle, SizeMode, SplitBehaviorFlags, TimeSpec, - TransformationMode, + AspectRatioMode, AspectRatioMode, BGMode, BGMode, BrushStyle, BrushStyle, CaseSensitivity, + CaseSensitivity, ClipOperation, ClipOperation, DateFormat, DateFormat, FillRule, FillRule, + LayoutDirection, LayoutDirection, PenCapStyle, PenCapStyle, PenJoinStyle, PenJoinStyle, + PenStyle, PenStyle, SizeMode, SizeMode, SplitBehaviorFlags, SplitBehaviorFlags, TimeSpec, + TimeSpec, TransformationMode, }; // Reexport ConnectionType from cxx-qt From 7a80dc94ad8dc09c2dfd6440d200ac908ffaabd4 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Thu, 29 Feb 2024 14:11:49 +0100 Subject: [PATCH 12/14] Fix duplicate entries --- crates/cxx-qt-lib/src/core/qt.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/cxx-qt-lib/src/core/qt.rs b/crates/cxx-qt-lib/src/core/qt.rs index 6d58b9c2f..8503ec1e9 100644 --- a/crates/cxx-qt-lib/src/core/qt.rs +++ b/crates/cxx-qt-lib/src/core/qt.rs @@ -210,11 +210,10 @@ mod ffi { } pub use ffi::{ - AspectRatioMode, AspectRatioMode, BGMode, BGMode, BrushStyle, BrushStyle, CaseSensitivity, - CaseSensitivity, ClipOperation, ClipOperation, DateFormat, DateFormat, FillRule, FillRule, - LayoutDirection, LayoutDirection, PenCapStyle, PenCapStyle, PenJoinStyle, PenJoinStyle, - PenStyle, PenStyle, SizeMode, SizeMode, SplitBehaviorFlags, SplitBehaviorFlags, TimeSpec, - TimeSpec, TransformationMode, + AspectRatioMode, BGMode, BrushStyle, CaseSensitivity, CaseSensitivity, ClipOperation, + ClipOperation, DateFormat, DateFormat, FillRule, FillRule, LayoutDirection, LayoutDirection, + PenCapStyle, PenCapStyle, PenJoinStyle, PenJoinStyle, PenStyle, PenStyle, SizeMode, SizeMode, + SplitBehaviorFlags, SplitBehaviorFlags, TimeSpec, TimeSpec, TransformationMode, }; // Reexport ConnectionType from cxx-qt From 96f7d8a996d49da4b74d0ec2ca2c69781eb65bbd Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Thu, 29 Feb 2024 14:21:39 +0100 Subject: [PATCH 13/14] Fix duplicate here too --- crates/cxx-qt-lib/src/core/qt.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/cxx-qt-lib/src/core/qt.rs b/crates/cxx-qt-lib/src/core/qt.rs index 8503ec1e9..fedd7969c 100644 --- a/crates/cxx-qt-lib/src/core/qt.rs +++ b/crates/cxx-qt-lib/src/core/qt.rs @@ -210,10 +210,9 @@ mod ffi { } pub use ffi::{ - AspectRatioMode, BGMode, BrushStyle, CaseSensitivity, CaseSensitivity, ClipOperation, - ClipOperation, DateFormat, DateFormat, FillRule, FillRule, LayoutDirection, LayoutDirection, - PenCapStyle, PenCapStyle, PenJoinStyle, PenJoinStyle, PenStyle, PenStyle, SizeMode, SizeMode, - SplitBehaviorFlags, SplitBehaviorFlags, TimeSpec, TimeSpec, TransformationMode, + AspectRatioMode, BGMode, BrushStyle, CaseSensitivity, ClipOperation, DateFormat, DateFormat, + FillRule, LayoutDirection, PenCapStyle, PenJoinStyle, PenStyle, SizeMode, SplitBehaviorFlags, + TimeSpec, TransformationMode, }; // Reexport ConnectionType from cxx-qt From 759140618d56821e39311d2032421f4e1f7836fd Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Thu, 29 Feb 2024 14:36:22 +0100 Subject: [PATCH 14/14] Remove this duplicate entry (cargo fmt create pbs ???) --- crates/cxx-qt-lib/src/core/qt.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/cxx-qt-lib/src/core/qt.rs b/crates/cxx-qt-lib/src/core/qt.rs index fedd7969c..98b43bfe0 100644 --- a/crates/cxx-qt-lib/src/core/qt.rs +++ b/crates/cxx-qt-lib/src/core/qt.rs @@ -210,9 +210,9 @@ mod ffi { } pub use ffi::{ - AspectRatioMode, BGMode, BrushStyle, CaseSensitivity, ClipOperation, DateFormat, DateFormat, - FillRule, LayoutDirection, PenCapStyle, PenJoinStyle, PenStyle, SizeMode, SplitBehaviorFlags, - TimeSpec, TransformationMode, + AspectRatioMode, BGMode, BrushStyle, CaseSensitivity, ClipOperation, DateFormat, FillRule, + LayoutDirection, PenCapStyle, PenJoinStyle, PenStyle, SizeMode, SplitBehaviorFlags, TimeSpec, + TransformationMode, }; // Reexport ConnectionType from cxx-qt