Skip to content

Commit

Permalink
cxx-qt-gen: remove generated qobject module and use bridge mod instead
Browse files Browse the repository at this point in the history
Related to KDAB#559
  • Loading branch information
ahayzen-kdab authored and Be-ing committed Jul 18, 2023
1 parent 0414d25 commit 11d6dcf
Show file tree
Hide file tree
Showing 28 changed files with 58 additions and 205 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `rust_mut` is now safe to call
- `#[qproperty]` is now defined as an attribute on the qobject rather than the field
- QObject struct is now split between the bridge and implementation outside via a type alias
- `qobject` module is no longer generated

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion book/src/concepts/nested_objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Rust Qt objects can be nested as properties or parameters of each other.

A nested object is referred to by using a pointer to its QObject representation.

First define a type with an extern block for your bridge, this should point to the `qobject::T` of the QObject and the `cxx_name` should match the QObject name.
First define a type within an extern block for your bridge as normal.

```rust,ignore,noplayground
{{#include ../../../examples/qml_features/rust/src/nested_qobjects.rs:book_extern_block}}
Expand Down
40 changes: 0 additions & 40 deletions crates/cxx-qt-gen/src/writer/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,6 @@ pub fn write_rust(generated: &GeneratedRustBlocks) -> TokenStream {
})
}

// Create the qobject block for the type alias
cxx_qt_mod_contents.push(
syn::parse2(quote! {
/// Generated CXX-Qt module containing type alias to the C++ types of the QObjects
pub mod qobject {
#(#qobject_types)*
}
})
.expect("Could not build qobject block"),
);

// Inject the CXX blocks
if let Some((_, items)) = &mut cxx_mod.content {
items.extend(cxx_mod_contents.into_iter());
Expand Down Expand Up @@ -385,17 +374,6 @@ mod tests {
self.cxx_qt_ffi_rust_mut()
}
}

/// Generated CXX-Qt module containing type alias to the C++ types of the QObjects
pub mod qobject {
#[doc = "The C++ type for the QObject "]
#[doc = "MyObject"]
#[doc = "\n"]
#[doc = "Use this type when referring to the QObject as a pointer"]
#[doc = "\n"]
#[doc = "See the book for more information: <https://kdab.github.io/cxx-qt/book/qobject/generated-qobject.html>"]
pub type MyObject = super::MyObject;
}
}
}
.into_token_stream()
Expand Down Expand Up @@ -516,24 +494,6 @@ mod tests {
self.cxx_qt_ffi_rust_mut()
}
}

/// Generated CXX-Qt module containing type alias to the C++ types of the QObjects
pub mod qobject {
#[doc = "The C++ type for the QObject "]
#[doc = "FirstObject"]
#[doc = "\n"]
#[doc = "Use this type when referring to the QObject as a pointer"]
#[doc = "\n"]
#[doc = "See the book for more information: <https://kdab.github.io/cxx-qt/book/qobject/generated-qobject.html>"]
pub type FirstObject = super::FirstObject;
#[doc = "The C++ type for the QObject "]
#[doc = "SecondObject"]
#[doc = "\n"]
#[doc = "Use this type when referring to the QObject as a pointer"]
#[doc = "\n"]
#[doc = "See the book for more information: <https://kdab.github.io/cxx-qt/book/qobject/generated-qobject.html>"]
pub type SecondObject = super::SecondObject;
}
}
}
.into_token_stream()
Expand Down
10 changes: 0 additions & 10 deletions crates/cxx-qt-gen/test_outputs/inheritance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,4 @@ pub mod cxx_qt_inheritance {
self.cxx_qt_ffi_rust_mut()
}
}
#[doc = r" Generated CXX-Qt module containing type alias to the C++ types of the QObjects"]
pub mod qobject {
#[doc = "The C++ type for the QObject "]
#[doc = "MyObject"]
#[doc = "\n"]
#[doc = "Use this type when referring to the QObject as a pointer"]
#[doc = "\n"]
#[doc = "See the book for more information: <https://kdab.github.io/cxx-qt/book/qobject/generated-qobject.html>"]
pub type MyObject = super::MyObject;
}
}
10 changes: 0 additions & 10 deletions crates/cxx-qt-gen/test_outputs/invokables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,14 +328,4 @@ pub mod cxx_qt_ffi {
self.cxx_qt_ffi_rust_mut()
}
}
#[doc = r" Generated CXX-Qt module containing type alias to the C++ types of the QObjects"]
pub mod qobject {
#[doc = "The C++ type for the QObject "]
#[doc = "MyObject"]
#[doc = "\n"]
#[doc = "Use this type when referring to the QObject as a pointer"]
#[doc = "\n"]
#[doc = "See the book for more information: <https://kdab.github.io/cxx-qt/book/qobject/generated-qobject.html>"]
pub type MyObject = super::MyObject;
}
}
17 changes: 0 additions & 17 deletions crates/cxx-qt-gen/test_outputs/passthrough_and_naming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,21 +377,4 @@ pub mod cxx_qt_ffi {
self.cxx_qt_ffi_rust_mut()
}
}
#[doc = r" Generated CXX-Qt module containing type alias to the C++ types of the QObjects"]
pub mod qobject {
#[doc = "The C++ type for the QObject "]
#[doc = "MyObject"]
#[doc = "\n"]
#[doc = "Use this type when referring to the QObject as a pointer"]
#[doc = "\n"]
#[doc = "See the book for more information: <https://kdab.github.io/cxx-qt/book/qobject/generated-qobject.html>"]
pub type MyObject = super::MyObject;
#[doc = "The C++ type for the QObject "]
#[doc = "SecondObject"]
#[doc = "\n"]
#[doc = "Use this type when referring to the QObject as a pointer"]
#[doc = "\n"]
#[doc = "See the book for more information: <https://kdab.github.io/cxx-qt/book/qobject/generated-qobject.html>"]
pub type SecondObject = super::SecondObject;
}
}
10 changes: 0 additions & 10 deletions crates/cxx-qt-gen/test_outputs/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,4 @@ pub mod cxx_qt_ffi {
self.cxx_qt_ffi_rust_mut()
}
}
#[doc = r" Generated CXX-Qt module containing type alias to the C++ types of the QObjects"]
pub mod qobject {
#[doc = "The C++ type for the QObject "]
#[doc = "MyObject"]
#[doc = "\n"]
#[doc = "Use this type when referring to the QObject as a pointer"]
#[doc = "\n"]
#[doc = "See the book for more information: <https://kdab.github.io/cxx-qt/book/qobject/generated-qobject.html>"]
pub type MyObject = super::MyObject;
}
}
10 changes: 0 additions & 10 deletions crates/cxx-qt-gen/test_outputs/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,4 @@ pub mod cxx_qt_ffi {
self.cxx_qt_ffi_rust_mut()
}
}
#[doc = r" Generated CXX-Qt module containing type alias to the C++ types of the QObjects"]
pub mod qobject {
#[doc = "The C++ type for the QObject "]
#[doc = "MyObject"]
#[doc = "\n"]
#[doc = "Use this type when referring to the QObject as a pointer"]
#[doc = "\n"]
#[doc = "See the book for more information: <https://kdab.github.io/cxx-qt/book/qobject/generated-qobject.html>"]
pub type MyObject = super::MyObject;
}
}
6 changes: 2 additions & 4 deletions crates/cxx-qt-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use cxx_qt_gen::{write_rust, GeneratedRustBlocks, Parser};
///
/// ```rust
/// #[cxx_qt::bridge(namespace = "cxx_qt::my_object")]
/// mod ffi {
/// mod qobject {
/// unsafe extern "RustQt" {
/// #[cxx_qt::qobject]
/// # // Note that we can't use properties as this confuses the linker on Windows
Expand All @@ -32,9 +32,7 @@ use cxx_qt_gen::{write_rust, GeneratedRustBlocks, Parser};
/// #[derive(Default)]
/// pub struct MyObjectRust;
///
/// # // TODO: this will change to qobject::MyObject once
/// # // https://github.com/KDAB/cxx-qt/issues/559 is done
/// impl ffi::MyObject {
/// impl qobject::MyObject {
/// fn invokable(&self, a: i32, b: i32) -> i32 {
/// a + b
/// }
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub trait Threading: Locking + Sized {
/// # Minimal Example
/// ```
/// #[cxx_qt::bridge]
/// mod ffi {
/// mod qobject {
/// extern "RustQt" {
/// #[cxx_qt::qobject]
/// type MyStruct = super::MyStructRust;
Expand Down
6 changes: 2 additions & 4 deletions examples/demo_threading/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod workers;

// This mod defines our QObject called EnergyUsage
#[cxx_qt::bridge(cxx_file_stem = "energy_usage", namespace = "cxx_qt::energy_usage")]
mod ffi {
mod qobject {
#[namespace = ""]
unsafe extern "C++" {
include!("cxx-qt-lib/qstring.h");
Expand Down Expand Up @@ -99,9 +99,7 @@ impl Default for EnergyUsageRust {
}
}

// TODO: this will change to qobject::EnergyUsage once
// https://github.com/KDAB/cxx-qt/issues/559 is done
impl ffi::EnergyUsage {
impl qobject::EnergyUsage {
/// A Q_INVOKABLE that returns the current power usage for a given uuid
fn sensor_power(self: Pin<&mut Self>, uuid: &QString) -> f64 {
let sensors = SensorsWorker::read_sensors(&self.rust_mut().sensors_map);
Expand Down
6 changes: 2 additions & 4 deletions examples/qml_extension_plugin/plugin/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl From<&MyObjectRust> for DataSerde {
const DEFAULT_STR: &str = r#"{"number": 1, "string": "Hello World!"}"#;

#[cxx_qt::bridge(cxx_file_stem = "my_object", namespace = "core")]
mod ffi {
mod qobject {
#[namespace = ""]
unsafe extern "C++" {
include!("cxx-qt-lib/qstring.h");
Expand Down Expand Up @@ -79,9 +79,7 @@ impl From<DataSerde> for MyObjectRust {
}
}

// TODO: this will change to qobject::MyObject once
// https://github.com/KDAB/cxx-qt/issues/559 is done
impl ffi::MyObject {
impl qobject::MyObject {
pub fn increment(self: Pin<&mut Self>) {
let new_number = self.number() + 1;
self.set_number(new_number);
Expand Down
6 changes: 2 additions & 4 deletions examples/qml_features/rust/src/containers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/// A CXX-Qt bridge which shows how to use Qt container types
#[cxx_qt::bridge(cxx_file_stem = "rust_containers")]
pub mod ffi {
pub mod qobject {
unsafe extern "C++" {
include!("cxx-qt-lib/qhash.h");
/// QHash<QString, QVariant> from cxx_qt_lib
Expand Down Expand Up @@ -94,9 +94,7 @@ pub struct RustContainersRust {
pub(crate) vector: QVector<i32>,
}

// TODO: this will change to qobject::RustContainers once
// https://github.com/KDAB/cxx-qt/issues/559 is done
impl ffi::RustContainers {
impl qobject::RustContainers {
/// Reset all the containers
fn reset(mut self: Pin<&mut Self>) {
// Update the private rust fields via the rust_mut
Expand Down
19 changes: 7 additions & 12 deletions examples/qml_features/rust/src/custom_base_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/// A CXX-Qt bridge which shows a custom base class and inheritance can be used
// ANCHOR: book_macro_code
#[cxx_qt::bridge(cxx_file_stem = "custom_base_class")]
pub mod ffi {
pub mod qobject {
// ANCHOR: book_base_include
unsafe extern "C++" {
include!(< QAbstractListModel >);
Expand Down Expand Up @@ -182,9 +182,7 @@ pub struct CustomBaseClassRust {
pub(crate) vector: Vec<(u32, f64)>,
}

// TODO: this will change to qobject::RustContainers once
// https://github.com/KDAB/cxx-qt/issues/559 is done
impl ffi::CustomBaseClass {
impl qobject::CustomBaseClass {
/// Add a new row to the QAbstractListModel on the current thread
pub fn add(self: Pin<&mut Self>) {
self.add_cpp_context();
Expand Down Expand Up @@ -227,7 +225,7 @@ impl ffi::CustomBaseClass {
}

// ANCHOR: book_inherit_clear
impl ffi::CustomBaseClass {
impl qobject::CustomBaseClass {
/// Clear the rows in the QAbstractListModel
pub fn clear(mut self: Pin<&mut Self>) {
unsafe {
Expand All @@ -240,7 +238,7 @@ impl ffi::CustomBaseClass {
}
// ANCHOR_END: book_inherit_clear

impl ffi::CustomBaseClass {
impl qobject::CustomBaseClass {
/// Multiply the number in the row with the given index by the given factor
pub fn multiply(mut self: Pin<&mut Self>, index: i32, factor: f64) {
if let Some((_, value)) = self.as_mut().rust_mut().vector.get_mut(index as usize) {
Expand Down Expand Up @@ -270,13 +268,10 @@ impl ffi::CustomBaseClass {
}
}

// TODO: this will change to qobject::RustContainers once
// https://github.com/KDAB/cxx-qt/issues/559 is done
//
// QAbstractListModel implementation
//
// ANCHOR: book_inherit_data
impl ffi::CustomBaseClass {
impl qobject::CustomBaseClass {
/// i32 representing the id role
pub const ID_ROLE: i32 = 0;
/// i32 representing the value role
Expand All @@ -297,7 +292,7 @@ impl ffi::CustomBaseClass {
// ANCHOR_END: book_inherit_data

// ANCHOR: book_inherit_can_fetch_more
impl ffi::CustomBaseClass {
impl qobject::CustomBaseClass {
/// Return whether the base class can fetch more
// Example of overriding a C++ virtual method and calling the base class implementation.
pub fn can_fetch_more(&self, parent: &QModelIndex) -> bool {
Expand All @@ -306,7 +301,7 @@ impl ffi::CustomBaseClass {
}
// ANCHOR_END: book_inherit_can_fetch_more

impl ffi::CustomBaseClass {
impl qobject::CustomBaseClass {
/// Return the role names for the QAbstractListModel
pub fn role_names(&self) -> QHash<QHashPair_i32_QByteArray> {
let mut roles = QHash::<QHashPair_i32_QByteArray>::default();
Expand Down
12 changes: 5 additions & 7 deletions examples/qml_features/rust/src/custom_parent_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/// A CXX-Qt bridge which shows a custom parent class can be used
#[cxx_qt::bridge(cxx_file_stem = "custom_parent_class")]
mod ffi {
mod qobject {
unsafe extern "C++" {
/// QColor from cxx_qt_lib
type QColor = cxx_qt_lib::QColor;
Expand Down Expand Up @@ -78,11 +78,9 @@ pub struct CustomParentClassRust {
color: QColor,
}

// TODO: this will change to qobject::RustInvokables once
// https://github.com/KDAB/cxx-qt/issues/559 is done
impl ffi::CustomParentClass {
impl qobject::CustomParentClass {
/// Override QQuickPaintedItem::paint to draw two rectangles in Rust using QPainter
fn paint(self: Pin<&mut Self>, painter: *mut ffi::QPainter) {
fn paint(self: Pin<&mut Self>, painter: *mut qobject::QPainter) {
// We need to convert the *mut QPainter to a Pin<&mut QPainter> so that we can reach the methods
if let Some(painter) = unsafe { painter.as_mut() } {
let mut pinned_painter = unsafe { Pin::new_unchecked(painter) };
Expand All @@ -103,9 +101,9 @@ impl ffi::CustomParentClass {
}
}

impl cxx_qt::Constructor<()> for ffi::CustomParentClass {
impl cxx_qt::Constructor<()> for qobject::CustomParentClass {
type NewArguments = ();
type BaseArguments = (*mut ffi::QQuickItem,);
type BaseArguments = (*mut qobject::QQuickItem,);
type InitializeArguments = ();

fn route_arguments(
Expand Down
7 changes: 2 additions & 5 deletions examples/qml_features/rust/src/invokables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/// A CXX-Qt bridge which shows how a Q_INVOKABLE can be used
// ANCHOR: book_macro_code
#[cxx_qt::bridge(cxx_file_stem = "rust_invokables")]
pub mod ffi {
pub mod qobject {
unsafe extern "C++" {
include!("cxx-qt-lib/qcolor.h");
/// QColor from cxx_qt_lib
Expand Down Expand Up @@ -58,11 +58,8 @@ impl Default for RustInvokablesRust {
}
}

// TODO: this will change to qobject::RustInvokables once
// https://github.com/KDAB/cxx-qt/issues/559 is done
//
// ANCHOR: book_invokable_impl
impl ffi::RustInvokables {
impl qobject::RustInvokables {
/// Immutable invokable method that returns the QColor
fn load_color(&self) -> QColor {
self.rust().as_qcolor()
Expand Down
Loading

0 comments on commit 11d6dcf

Please sign in to comment.