Skip to content

Commit

Permalink
qml_features: enable deny(missing_docs)
Browse files Browse the repository at this point in the history
This ensures we don't regress with any missing docs for
generated public items, related to KDAB#517
  • Loading branch information
ahayzen-kdab committed Apr 17, 2023
1 parent 9efffa5 commit eacba5c
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 23 deletions.
13 changes: 12 additions & 1 deletion examples/qml_features/rust/src/containers.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

//! This example shows how container types can be used
#[cxx_qt::bridge(cxx_file_stem = "rust_containers")]
mod ffi {
unsafe extern "C++" {
Expand All @@ -22,6 +24,9 @@ mod ffi {
type QVector_i32 = cxx_qt_lib::QVector<i32>;
}

/// A QObject which stores container types internally
///
/// Then it has Q_PROPERTYs which expose a string of the container to be show in QML
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
#[derive(Default)]
pub struct RustContainers {
Expand All @@ -44,6 +49,7 @@ mod ffi {
}

impl qobject::RustContainers {
/// Reset all the container types
#[qinvokable]
pub fn reset(mut self: Pin<&mut Self>) {
self.as_mut().set_hash(QHash_QString_QVariant::default());
Expand All @@ -55,42 +61,47 @@ mod ffi {
self.update_strings();
}

/// Append the given number to the vector container
#[qinvokable]
pub fn append_vector(mut self: Pin<&mut Self>, value: i32) {
self.as_mut().vector_mut().append(value);

self.update_strings();
}

/// Append the given number to the list container
#[qinvokable]
pub fn append_list(mut self: Pin<&mut Self>, value: i32) {
self.as_mut().list_mut().append(value);

self.update_strings();
}

/// Insert the given number into the set container
#[qinvokable]
pub fn insert_set(mut self: Pin<&mut Self>, value: i32) {
self.as_mut().set_mut().insert(value);

self.update_strings();
}

/// Insert the given string and variant to the hash container
#[qinvokable]
pub fn insert_hash(mut self: Pin<&mut Self>, key: QString, value: QVariant) {
self.as_mut().hash_mut().insert(key, value);

self.update_strings();
}

/// Insert the given string and variant to the map container
#[qinvokable]
pub fn insert_map(mut self: Pin<&mut Self>, key: QString, value: QVariant) {
self.as_mut().map_mut().insert(key, value);

self.update_strings();
}

pub fn update_strings(mut self: Pin<&mut Self>) {
fn update_strings(mut self: Pin<&mut Self>) {
let hash_items = self
.as_ref()
.hash()
Expand Down
16 changes: 16 additions & 0 deletions examples/qml_features/rust/src/custom_base_class.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

//! This example shows how a custom base class and inheritance can be used
// ANCHOR: book_macro_code
#[cxx_qt::bridge(cxx_file_stem = "custom_base_class")]
mod ffi {
Expand All @@ -24,6 +26,7 @@ mod ffi {
type QVector_i32 = cxx_qt_lib::QVector<i32>;
}

/// A struct which will derive from a QAbstractListModel
// ANCHOR: book_inherit_qalm
// ANCHOR: book_qobject_base
#[cxx_qt::qobject(
Expand All @@ -39,24 +42,31 @@ mod ffi {
}
// ANCHOR_END: book_inherit_qalm

/// The signals for our QAbstractListModel struct
// ANCHOR: book_qsignals_inherit
#[cxx_qt::qsignals(CustomBaseClass)]
pub enum Signals<'a> {
/// Inherit the DataChanged signal from the QAbstractListModel base class
#[inherit]
DataChanged {
/// Top left affected index
top_left: &'a QModelIndex,
/// Bottom right affected index
bottom_right: &'a QModelIndex,
/// Roles that have been modified
roles: &'a QVector_i32,
},
}
// ANCHOR_END: book_qsignals_inherit

impl qobject::CustomBaseClass {
/// Add a new row to the QAbstractListModel on the current thread
#[qinvokable]
pub fn add(self: Pin<&mut Self>) {
self.add_cpp_context();
}

/// On a background thread add a given number of rows to the QAbstractListModel
#[qinvokable]
pub fn add_on_thread(self: Pin<&mut Self>, mut counter: i32) {
let qt_thread = self.qt_thread();
Expand Down Expand Up @@ -92,6 +102,7 @@ mod ffi {
}
}

/// Clear the rows in the QAbstractListModel
// ANCHOR: book_inherit_clear
#[qinvokable]
pub fn clear(mut self: Pin<&mut Self>) {
Expand All @@ -104,6 +115,7 @@ mod ffi {
}
// ANCHOR_END: book_inherit_clear

/// Multiply the number in the row with the given index by the given factor
#[qinvokable]
pub fn multiply(mut self: Pin<&mut Self>, index: i32, factor: f64) {
if let Some((_, value)) = self.as_mut().vector_mut().get_mut(index as usize) {
Expand All @@ -121,6 +133,7 @@ mod ffi {
}
}

/// Remove the row with the given index
#[qinvokable]
pub fn remove(mut self: Pin<&mut Self>, index: i32) {
if index < 0 || (index as usize) >= self.vector().len() {
Expand Down Expand Up @@ -193,6 +206,7 @@ mod ffi {
}
// ANCHOR_END: book_inherit_data

/// Return whether the base class can fetch more
// ANCHOR: book_inherit_can_fetch_more
// Example of overriding a C++ virtual method and calling the base class implementation.
#[qinvokable(cxx_override)]
Expand All @@ -201,6 +215,7 @@ mod ffi {
}
// ANCHOR_END: book_inherit_can_fetch_more

/// Return the role names for the QAbstractListModel
#[qinvokable(cxx_override)]
pub fn role_names(&self) -> QHash_i32_QByteArray {
let mut roles = QHash_i32_QByteArray::default();
Expand All @@ -209,6 +224,7 @@ mod ffi {
roles
}

/// Return the row count for the QAbstractListModel
#[qinvokable(cxx_override)]
pub fn row_count(&self, _parent: &QModelIndex) -> i32 {
self.rust().vector.len() as i32
Expand Down
5 changes: 4 additions & 1 deletion examples/qml_features/rust/src/invokables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
//
// SPDX-License-Identifier: MIT OR Apache-2.0

//! This example shows how a Q_INVOKABLE can be used
// ANCHOR: book_macro_code
#[cxx_qt::bridge(cxx_file_stem = "rust_invokables")]
pub mod ffi {
mod ffi {
unsafe extern "C++" {
include!("cxx-qt-lib/qcolor.h");
type QColor = cxx_qt_lib::QColor;
}

/// A QObject which has Q_INVOKABLEs
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
pub struct RustInvokables {
red: f32,
Expand Down
30 changes: 18 additions & 12 deletions examples/qml_features/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
//
// SPDX-License-Identifier: MIT OR Apache-2.0

mod containers;
mod custom_base_class;
mod invokables;
mod multiple_qobjects;
mod nested_qobjects;
mod properties;
mod serialisation;
mod signals;
mod singleton;
mod threading;
mod types;
mod uncreatable;
// Use this crate to test that missing_docs works with our generated code
#![deny(missing_docs)]

//! This example provides demostrations of most of the features of CXX-Qt
//! split into separate modules
pub mod containers;
pub mod custom_base_class;
pub mod invokables;
pub mod multiple_qobjects;
pub mod nested_qobjects;
pub mod properties;
pub mod serialisation;
pub mod signals;
pub mod singleton;
pub mod threading;
pub mod types;
pub mod uncreatable;
12 changes: 12 additions & 0 deletions examples/qml_features/rust/src/multiple_qobjects.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

//! This example shows how multiple QObjects can be defined in one module
#[cxx_qt::bridge(cxx_file_stem = "multiple_qobjects")]
mod ffi {
unsafe extern "C++" {
Expand All @@ -12,6 +14,7 @@ mod ffi {
type QUrl = cxx_qt_lib::QUrl;
}

/// The first QObject
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
pub struct FirstObject {
#[qproperty]
Expand All @@ -29,13 +32,17 @@ mod ffi {
}
}

/// Signals for the first QObject
#[cxx_qt::qsignals(FirstObject)]
pub enum FirstSignals {
/// Accepted Q_SIGNAL
Accepted,
/// Rejected Q_SIGNAL
Rejected,
}

impl qobject::FirstObject {
/// A Q_INVOKABLE on the first QObject which increments a counter
#[qinvokable]
pub fn increment(mut self: Pin<&mut Self>) {
let new_value = self.as_ref().counter() + 1;
Expand All @@ -51,6 +58,7 @@ mod ffi {
}
}

/// The second QObject
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
pub struct SecondObject {
#[qproperty]
Expand All @@ -68,13 +76,17 @@ mod ffi {
}
}

/// Signals for the second QObject
#[cxx_qt::qsignals(SecondObject)]
pub enum SecondSignals {
/// Accepted Q_SIGNAL
Accepted,
/// Rejected Q_SIGNAL
Rejected,
}

impl qobject::SecondObject {
/// A Q_INVOKABLE on the second QObject which increments a counter
#[qinvokable]
pub fn increment(mut self: Pin<&mut Self>) {
let new_value = self.as_ref().counter() + 1;
Expand Down
13 changes: 12 additions & 1 deletion examples/qml_features/rust/src/nested_qobjects.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

//! This example shows how a pointer from one Rust defined QObject to another Rust defined QObject can be used
// ANCHOR: book_macro_code
#[cxx_qt::bridge(cxx_file_stem = "nested_qobjects")]
mod ffi {
Expand All @@ -13,13 +15,15 @@ mod ffi {
}
// ANCHOR_END: book_extern_block

/// The inner QObject
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
#[derive(Default)]
pub struct InnerObject {
#[qproperty]
counter: i32,
}

/// The outer QObject which has a Q_PROPERTY to the inner QObject
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
pub struct OuterObject {
#[qproperty]
Expand All @@ -34,12 +38,18 @@ mod ffi {
}
}

/// Signals for the outer QObject
#[cxx_qt::qsignals(OuterObject)]
pub enum OuterSignals {
Called { inner: *mut CxxInnerObject },
/// A signal showing how to refer to another QObject as an argument
Called {
/// Inner QObject being referred to
inner: *mut CxxInnerObject,
},
}

impl qobject::OuterObject {
/// Print the count of the given inner QObject
#[qinvokable]
pub fn print_count(self: Pin<&mut Self>, inner: *mut CxxInnerObject) {
if let Some(inner) = unsafe { inner.as_ref() } {
Expand All @@ -49,6 +59,7 @@ mod ffi {
self.emit(OuterSignals::Called { inner });
}

/// Reset the cont of the inner QObject stored in the Q_PROPERTY
#[qinvokable]
pub fn reset(self: Pin<&mut Self>) {
// We need to convert the *mut T to a Pin<&mut T> so that we can reach the methods
Expand Down
9 changes: 9 additions & 0 deletions examples/qml_features/rust/src/properties.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

//! This example shows how a Q_PROPERTY can be used
#[cxx_qt::bridge(cxx_file_stem = "rust_properties")]
mod ffi {
unsafe extern "C++" {
Expand All @@ -12,18 +14,23 @@ mod ffi {
type QUrl = cxx_qt_lib::QUrl;
}

/// A QObject which has Q_PROPERTYs
// ANCHOR: book_properties_struct
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
pub struct RustProperties {
/// A connected Q_PROPERTY
#[qproperty]
connected: bool,

/// A connected_url Q_PROPERTY
#[qproperty]
connected_url: QUrl,

/// A previous_connected_url Q_PROPERTY
#[qproperty]
previous_connected_url: QUrl,

/// A status_message Q_PROPERTY
#[qproperty]
status_message: QString,
}
Expand All @@ -43,6 +50,7 @@ mod ffi {
// ANCHOR_END: book_properties_default

impl qobject::RustProperties {
/// Connect to the given url
#[qinvokable]
pub fn connect(mut self: Pin<&mut Self>, mut url: QUrl) {
// Check that the url starts with kdab
Expand All @@ -66,6 +74,7 @@ mod ffi {
}
}

/// Disconnect from the stored url
#[qinvokable]
pub fn disconnect(mut self: Pin<&mut Self>) {
self.as_mut().set_connected(false);
Expand Down
Loading

0 comments on commit eacba5c

Please sign in to comment.