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

cxx-qt-lib: add QRect support #51

Merged
merged 3 commits into from
Jan 28, 2022
Merged
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
31 changes: 31 additions & 0 deletions cxx-qt-gen/src/cxx_qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <QMetaObject>
#include <QPointF>
#include <QPointer>
#include <QRect>
#include <QRectF>
#include <QSize>
#include <QSizeF>
Expand Down Expand Up @@ -243,6 +244,36 @@ extern "C"

namespace {

// We do these checks to ensure that we can safely store a QRect
// inside a block of memory that Rust thinks contains four i32-s.
// We also make sure that i32 and int are equivalent.

static_assert(sizeof(int) == 4);
static_assert(alignof(int) <= 4);

static_assert(sizeof(QRect) == 16);
static_assert(alignof(QRect) <= 16);

// Our Rust code assumes that QRect is trivial. Because it is trivial to move,
// we don't need to use Pin. Because it is trivial to destruct we do not
// need a special C++ function to destruct the object.

static_assert(std::is_trivially_move_assignable<QRect>::value);
static_assert(std::is_trivially_copy_assignable<QRect>::value);
static_assert(std::is_trivially_destructible<QRect>::value);

} // namespace

extern "C"
{
void cxxqt1$qrect$init(QRect* self, int xp, int yp, int w, int h) noexcept
{
new (self) QRect(xp, yp, w, h);
}
}

namespace {

// We do these checks to ensure that we can safely store a QSizeF
// inside a block of memory that Rust thinks contains two f64-s.
// We also make sure that f64 and qreal are equivalent.
Expand Down
2 changes: 2 additions & 0 deletions cxx-qt-gen/src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub(crate) enum QtTypes {
I32,
QPoint,
QPointF,
QRect,
QRectF,
QSize,
QSizeF,
Expand Down Expand Up @@ -195,6 +196,7 @@ fn extract_qt_type(
"i32" => Ok(QtTypes::I32),
"QPoint" => Ok(QtTypes::QPoint),
"QPointF" => Ok(QtTypes::QPointF),
"QRect" => Ok(QtTypes::QRect),
"QRectF" => Ok(QtTypes::QRectF),
"QSizeF" => Ok(QtTypes::QSizeF),
"QString" => Ok(QtTypes::QString),
Expand Down
5 changes: 5 additions & 0 deletions cxx-qt-gen/src/gen_cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl CppType for QtTypes {
Self::I8 | Self::I16 | Self::I32 => None,
Self::QPoint => None,
Self::QPointF => None,
Self::QRect => None,
Self::QRectF => None,
Self::QSize => None,
Self::QSizeF => None,
Expand Down Expand Up @@ -105,6 +106,7 @@ impl CppType for QtTypes {
)],
Self::QPoint => vec!["#include <QtCore/QPoint>".to_owned()],
Self::QPointF => vec!["#include <QtCore/QPointF>".to_owned()],
Self::QRect => vec!["#include <QtCore/QRect>".to_owned()],
Self::QRectF => vec!["#include <QtCore/QRectF>".to_owned()],
Self::QSize => vec!["#include <QtCore/QSize>".to_owned()],
Self::QSizeF => vec!["#include <QtCore/QSizeF>".to_owned()],
Expand All @@ -126,6 +128,7 @@ impl CppType for QtTypes {
Self::I8 | Self::I16 | Self::I32 => false,
Self::QPoint => true,
Self::QPointF => true,
Self::QRect => true,
Self::QRectF => true,
Self::QSize => true,
Self::QSizeF => true,
Expand Down Expand Up @@ -171,6 +174,7 @@ impl CppType for QtTypes {
Self::I8 | Self::I16 | Self::I32 => false,
Self::QPoint => true,
Self::QPointF => true,
Self::QRect => true,
Self::QRectF => true,
Self::QSize => true,
Self::QSizeF => true,
Expand Down Expand Up @@ -207,6 +211,7 @@ impl CppType for QtTypes {
Self::I32 => "qint32",
Self::QPoint => "QPoint",
Self::QPointF => "QPointF",
Self::QRect => "QRect",
Self::QRectF => "QRectF",
Self::QSize => "QSize",
Self::QSizeF => "QSizeF",
Expand Down
5 changes: 5 additions & 0 deletions cxx-qt-gen/src/gen_rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl RustType for QtTypes {
match self {
Self::QPoint => true,
Self::QPointF => true,
Self::QRect => true,
Self::QRectF => true,
Self::QSize => true,
Self::QSizeF => true,
Expand Down Expand Up @@ -57,6 +58,7 @@ impl RustType for QtTypes {
Self::I32 => format_ident!("i32"),
Self::QPoint => format_ident!("QPoint"),
Self::QPointF => format_ident!("QPointF"),
Self::QRect => format_ident!("QRect"),
Self::QRectF => format_ident!("QRectF"),
Self::QSize => format_ident!("QSize"),
Self::QSizeF => format_ident!("QSizeF"),
Expand Down Expand Up @@ -87,6 +89,7 @@ impl RustType for QtTypes {
Self::I32 => quote! {i32},
Self::QPoint => quote! {cxx_qt_lib::QPoint},
Self::QPointF => quote! {cxx_qt_lib::QPointF},
Self::QRect => quote! {cxx_qt_lib::QRect},
Self::QRectF => quote! {cxx_qt_lib::QRectF},
Self::QSize => quote! {cxx_qt_lib::QSize},
Self::QSizeF => quote! {cxx_qt_lib::QSizeF},
Expand Down Expand Up @@ -430,6 +433,8 @@ pub fn generate_qobject_cxx(
#[namespace = ""]
type QPointF = cxx_qt_lib::QPointF;
#[namespace = ""]
type QRect = cxx_qt_lib::QRect;
#[namespace = ""]
type QRectF = cxx_qt_lib::QRectF;
#[namespace = ""]
type QSize = cxx_qt_lib::QSize;
Expand Down
5 changes: 5 additions & 0 deletions cxx-qt-gen/test_inputs/types_qt_invokable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ mod my_object {
pointf
}

#[invokable]
fn test_rect(&self, _cpp: &mut CppObj, rect: &QRect) -> QRect {
rect
}

#[invokable]
fn test_rectf(&self, _cpp: &mut CppObj, rectf: &QRectF) -> QRectF {
rectf
Expand Down
1 change: 1 addition & 0 deletions cxx-qt-gen/test_inputs/types_qt_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod my_object {
struct Data {
point: QPoint,
pointf: QPointF,
rect: QRect,
rectf: QRectF,
size: QSize,
sizef: QSizeF,
Expand Down
2 changes: 2 additions & 0 deletions cxx-qt-gen/test_outputs/basic_change_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ mod my_object {
#[namespace = ""]
type QPointF = cxx_qt_lib::QPointF;
#[namespace = ""]
type QRect = cxx_qt_lib::QRect;
#[namespace = ""]
type QRectF = cxx_qt_lib::QRectF;
#[namespace = ""]
type QSize = cxx_qt_lib::QSize;
Expand Down
2 changes: 2 additions & 0 deletions cxx-qt-gen/test_outputs/basic_custom_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ mod my_object {
#[namespace = ""]
type QPointF = cxx_qt_lib::QPointF;
#[namespace = ""]
type QRect = cxx_qt_lib::QRect;
#[namespace = ""]
type QRectF = cxx_qt_lib::QRectF;
#[namespace = ""]
type QSize = cxx_qt_lib::QSize;
Expand Down
2 changes: 2 additions & 0 deletions cxx-qt-gen/test_outputs/basic_ident_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ mod my_object {
#[namespace = ""]
type QPointF = cxx_qt_lib::QPointF;
#[namespace = ""]
type QRect = cxx_qt_lib::QRect;
#[namespace = ""]
type QRectF = cxx_qt_lib::QRectF;
#[namespace = ""]
type QSize = cxx_qt_lib::QSize;
Expand Down
2 changes: 2 additions & 0 deletions cxx-qt-gen/test_outputs/basic_invokable_and_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ mod my_object {
#[namespace = ""]
type QPointF = cxx_qt_lib::QPointF;
#[namespace = ""]
type QRect = cxx_qt_lib::QRect;
#[namespace = ""]
type QRectF = cxx_qt_lib::QRectF;
#[namespace = ""]
type QSize = cxx_qt_lib::QSize;
Expand Down
2 changes: 2 additions & 0 deletions cxx-qt-gen/test_outputs/basic_mod_attrs_vis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub mod my_object {
#[namespace = ""]
type QPointF = cxx_qt_lib::QPointF;
#[namespace = ""]
type QRect = cxx_qt_lib::QRect;
#[namespace = ""]
type QRectF = cxx_qt_lib::QRectF;
#[namespace = ""]
type QSize = cxx_qt_lib::QSize;
Expand Down
2 changes: 2 additions & 0 deletions cxx-qt-gen/test_outputs/basic_mod_passthrough.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ mod my_object {
#[namespace = ""]
type QPointF = cxx_qt_lib::QPointF;
#[namespace = ""]
type QRect = cxx_qt_lib::QRect;
#[namespace = ""]
type QRectF = cxx_qt_lib::QRectF;
#[namespace = ""]
type QSize = cxx_qt_lib::QSize;
Expand Down
2 changes: 2 additions & 0 deletions cxx-qt-gen/test_outputs/basic_only_invokable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ mod my_object {
#[namespace = ""]
type QPointF = cxx_qt_lib::QPointF;
#[namespace = ""]
type QRect = cxx_qt_lib::QRect;
#[namespace = ""]
type QRectF = cxx_qt_lib::QRectF;
#[namespace = ""]
type QSize = cxx_qt_lib::QSize;
Expand Down
2 changes: 2 additions & 0 deletions cxx-qt-gen/test_outputs/basic_only_invokable_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ mod my_object {
#[namespace = ""]
type QPointF = cxx_qt_lib::QPointF;
#[namespace = ""]
type QRect = cxx_qt_lib::QRect;
#[namespace = ""]
type QRectF = cxx_qt_lib::QRectF;
#[namespace = ""]
type QSize = cxx_qt_lib::QSize;
Expand Down
2 changes: 2 additions & 0 deletions cxx-qt-gen/test_outputs/basic_only_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ mod my_object {
#[namespace = ""]
type QPointF = cxx_qt_lib::QPointF;
#[namespace = ""]
type QRect = cxx_qt_lib::QRect;
#[namespace = ""]
type QRectF = cxx_qt_lib::QRectF;
#[namespace = ""]
type QSize = cxx_qt_lib::QSize;
Expand Down
2 changes: 2 additions & 0 deletions cxx-qt-gen/test_outputs/basic_pin_invokable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ mod my_object {
#[namespace = ""]
type QPointF = cxx_qt_lib::QPointF;
#[namespace = ""]
type QRect = cxx_qt_lib::QRect;
#[namespace = ""]
type QRectF = cxx_qt_lib::QRectF;
#[namespace = ""]
type QSize = cxx_qt_lib::QSize;
Expand Down
2 changes: 2 additions & 0 deletions cxx-qt-gen/test_outputs/basic_unknown_rust_obj_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ mod my_object {
#[namespace = ""]
type QPointF = cxx_qt_lib::QPointF;
#[namespace = ""]
type QRect = cxx_qt_lib::QRect;
#[namespace = ""]
type QRectF = cxx_qt_lib::QRectF;
#[namespace = ""]
type QSize = cxx_qt_lib::QSize;
Expand Down
2 changes: 2 additions & 0 deletions cxx-qt-gen/test_outputs/basic_update_requester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ mod my_object {
#[namespace = ""]
type QPointF = cxx_qt_lib::QPointF;
#[namespace = ""]
type QRect = cxx_qt_lib::QRect;
#[namespace = ""]
type QRectF = cxx_qt_lib::QRectF;
#[namespace = ""]
type QSize = cxx_qt_lib::QSize;
Expand Down
2 changes: 2 additions & 0 deletions cxx-qt-gen/test_outputs/subobject_pin_invokable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ mod my_object {
#[namespace = ""]
type QPointF = cxx_qt_lib::QPointF;
#[namespace = ""]
type QRect = cxx_qt_lib::QRect;
#[namespace = ""]
type QRectF = cxx_qt_lib::QRectF;
#[namespace = ""]
type QSize = cxx_qt_lib::QSize;
Expand Down
2 changes: 2 additions & 0 deletions cxx-qt-gen/test_outputs/subobject_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ mod my_object {
#[namespace = ""]
type QPointF = cxx_qt_lib::QPointF;
#[namespace = ""]
type QRect = cxx_qt_lib::QRect;
#[namespace = ""]
type QRectF = cxx_qt_lib::QRectF;
#[namespace = ""]
type QSize = cxx_qt_lib::QSize;
Expand Down
2 changes: 2 additions & 0 deletions cxx-qt-gen/test_outputs/types_primitive_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ mod my_object {
#[namespace = ""]
type QPointF = cxx_qt_lib::QPointF;
#[namespace = ""]
type QRect = cxx_qt_lib::QRect;
#[namespace = ""]
type QRectF = cxx_qt_lib::QRectF;
#[namespace = ""]
type QSize = cxx_qt_lib::QSize;
Expand Down
7 changes: 7 additions & 0 deletions cxx-qt-gen/test_outputs/types_qt_invokable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ MyObject::testPointf(const QPointF& pointf)
return m_rustObj->testPointfWrapper(*this, pointf);
}

QRect
MyObject::testRect(const QRect& rect)
{
const std::lock_guard<std::mutex> guard(m_rustObjMutex);
return m_rustObj->testRectWrapper(*this, rect);
}

QRectF
MyObject::testRectf(const QRectF& rectf)
{
Expand Down
2 changes: 2 additions & 0 deletions cxx-qt-gen/test_outputs/types_qt_invokable.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <QtCore/QPoint>
#include <QtCore/QPointF>
#include <QtCore/QRect>
#include <QtCore/QRectF>
#include <QtCore/QSize>
#include <QtCore/QSizeF>
Expand All @@ -25,6 +26,7 @@ class MyObject : public CxxQObject

Q_INVOKABLE QPoint testPoint(const QPoint& point);
Q_INVOKABLE QPointF testPointf(const QPointF& pointf);
Q_INVOKABLE QRect testRect(const QRect& rect);
Q_INVOKABLE QRectF testRectf(const QRectF& rectf);
Q_INVOKABLE QSize testSize(const QSize& size);
Q_INVOKABLE QSizeF testSizef(const QSizeF& sizef);
Expand Down
14 changes: 14 additions & 0 deletions cxx-qt-gen/test_outputs/types_qt_invokable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ mod my_object {
#[namespace = ""]
type QPointF = cxx_qt_lib::QPointF;
#[namespace = ""]
type QRect = cxx_qt_lib::QRect;
#[namespace = ""]
type QRectF = cxx_qt_lib::QRectF;
#[namespace = ""]
type QSize = cxx_qt_lib::QSize;
Expand Down Expand Up @@ -48,6 +50,9 @@ mod my_object {
pointf: &QPointF,
) -> QPointF;

#[cxx_name = "testRectWrapper"]
fn test_rect_wrapper(self: &RustObj, _cpp: Pin<&mut MyObject>, rect: &QRect) -> QRect;

#[cxx_name = "testRectfWrapper"]
fn test_rectf_wrapper(
self: &RustObj,
Expand Down Expand Up @@ -112,6 +117,11 @@ mod my_object {
return self.test_pointf(&mut _cpp, pointf);
}

fn test_rect_wrapper(&self, _cpp: std::pin::Pin<&mut FFICppObj>, rect: &QRect) -> QRect {
let mut _cpp = CppObj::new(_cpp);
return self.test_rect(&mut _cpp, rect);
}

fn test_rectf_wrapper(
&self,
_cpp: std::pin::Pin<&mut FFICppObj>,
Expand Down Expand Up @@ -161,6 +171,10 @@ mod my_object {
pointf
}

fn test_rect(&self, _cpp: &mut CppObj, rect: &QRect) -> QRect {
rect
}

fn test_rectf(&self, _cpp: &mut CppObj, rectf: &QRectF) -> QRectF {
rectf
}
Expand Down
21 changes: 21 additions & 0 deletions cxx-qt-gen/test_outputs/types_qt_property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ MyObject::setPointf(const QPointF& value)
}
}

const QRect&
MyObject::getRect() const
{
return m_rect;
}

void
MyObject::setRect(const QRect& value)
{
if (!m_initialised) {
m_rect = value;
return;
}

if (value != m_rect) {
m_rect = value;

runOnGUIThread([&]() { Q_EMIT rectChanged(); });
}
}

const QRectF&
MyObject::getRectf() const
{
Expand Down
Loading