Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement as_ref for overloads in cxx-qt-lib #901

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion crates/cxx-qt-lib/src/gui/qpainter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use core::pin::Pin;

#[cxx::bridge]
mod ffi {
#[namespace = "Qt"]
Expand Down Expand Up @@ -344,7 +346,7 @@ mod ffi {
fn setOpacity(self: Pin<&mut QPainter>, opacity: f64);

/// Sets the painter's pen to be the given pen.
#[rust_name = "set_pen"]
#[rust_name = "set_pen_from_pen"]
fn setPen(self: Pin<&mut QPainter>, pen: &QPen);

/// Sets the given render hint on the painter if on is true; otherwise clears the render hint.
Expand Down Expand Up @@ -415,4 +417,8 @@ impl QPainter {
None
}
}

pub fn set_pen(self: Pin<&mut ffi::QPainter>, pen: &impl AsRef<ffi::QPen>) {
self.set_pen_from_pen(pen.as_ref());
}
}
13 changes: 13 additions & 0 deletions crates/cxx-qt-lib/src/gui/qpen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@ impl fmt::Display for QPen {
}
}

impl AsRef<ffi::QPen> for ffi::QColor {
fn as_ref(&self) -> &ffi::QPen {
let pen = ffi::qpen_init_from_qcolor(&self);
pen
}
}

impl Into<QPen> for ffi::QColor {
fn into(self) -> QPen {
ffi::qpen_init_from_qcolor(&self)
}
}

// Safety:
//
// Static checks on the C++ side to ensure the size is the same.
Expand Down
2 changes: 2 additions & 0 deletions examples/qml_features/rust/src/custom_parent_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ impl qobject::CustomParentClass {
// Now pinned painter can be used as normal
// to render a rectangle with two colours
let size = self.as_ref().size();
let color = self.as_ref().color();
pinned_painter.set_pen(color);
pinned_painter.as_mut().fill_rect(
&QRectF::new(0.0, 0.0, size.width() / 2.0, size.height()),
self.as_ref().color(),
Expand Down
Loading