Skip to content

Commit

Permalink
Merge branch 'main' into add_qfontmetrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Montel authored Mar 15, 2024
2 parents 781bd5b + 4e8c0ff commit c42f8fa
Show file tree
Hide file tree
Showing 20 changed files with 349 additions and 178 deletions.
18 changes: 12 additions & 6 deletions crates/cxx-qt-gen/src/generator/cpp/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ mod tests {

use syn::parse_quote;

fn type_names_with_qobject() -> TypeNames {
let mut type_names = TypeNames::mock();
type_names.insert("QObject", None, None, None);
type_names
}

fn qobject_for_testing() -> GeneratedCppQObject {
GeneratedCppQObject {
ident: "MyObject".to_string(),
Expand Down Expand Up @@ -197,7 +203,7 @@ mod tests {
&[],
"BaseClass".to_owned(),
&["member1(1)".to_string(), "member2{ 2 }".to_string()],
&TypeNames::default(),
&type_names_with_qobject(),
)
.unwrap();

Expand Down Expand Up @@ -227,7 +233,7 @@ mod tests {
&[],
"BaseClass".to_owned(),
&[],
&TypeNames::default(),
&type_names_with_qobject(),
)
.unwrap();

Expand Down Expand Up @@ -258,7 +264,7 @@ mod tests {
&[],
"BaseClass".to_owned(),
&[],
&TypeNames::default(),
&type_names_with_qobject(),
)
.unwrap();

Expand Down Expand Up @@ -290,7 +296,7 @@ mod tests {
}],
"BaseClass".to_owned(),
&[],
&TypeNames::default(),
&type_names_with_qobject(),
)
.unwrap();

Expand Down Expand Up @@ -340,7 +346,7 @@ mod tests {
}],
"BaseClass".to_owned(),
&["initializer".to_string()],
&TypeNames::default(),
&type_names_with_qobject(),
)
.unwrap();

Expand Down Expand Up @@ -394,7 +400,7 @@ mod tests {
],
"BaseClass".to_owned(),
&["initializer".to_string()],
&TypeNames::default(),
&type_names_with_qobject(),
)
.unwrap();

Expand Down
10 changes: 7 additions & 3 deletions crates/cxx-qt-gen/src/generator/cpp/externcxxqt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,18 @@ mod tests {
type MyObject;

#[qsignal]
fn signal1(self: Pin<&mut ObjRust>);
fn signal1(self: Pin<&mut MyObject>);

#[qsignal]
fn signal2(self: Pin<&mut ObjRust>);
fn signal2(self: Pin<&mut MyObject>);
}
})
.unwrap()];
let generated = generate(&blocks, &TypeNames::default()).unwrap();

// Unknown types
assert!(generate(&blocks, &TypeNames::default()).is_err());

let generated = generate(&blocks, &TypeNames::mock()).unwrap();
assert_eq!(generated.len(), 2);
}

Expand Down
6 changes: 4 additions & 2 deletions crates/cxx-qt-gen/src/generator/cpp/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,10 @@ mod tests {
];
let qobject_idents = create_qobjectname();

let generated =
generate_cpp_methods(&invokables, &qobject_idents, &TypeNames::default()).unwrap();
let mut type_names = TypeNames::mock();
type_names.insert("QColor", None, None, None);

let generated = generate_cpp_methods(&invokables, &qobject_idents, &type_names).unwrap();

// methods
assert_eq!(generated.methods.len(), 5);
Expand Down
9 changes: 5 additions & 4 deletions crates/cxx-qt-gen/src/generator/cpp/property/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ mod tests {
];
let qobject_idents = create_qobjectname();

let generated =
generate_cpp_properties(&properties, &qobject_idents, &TypeNames::default()).unwrap();
let mut type_names = TypeNames::mock();
type_names.insert("QColor", None, None, None);
let generated = generate_cpp_properties(&properties, &qobject_idents, &type_names).unwrap();

// metaobjects
assert_eq!(generated.metaobjects.len(), 2);
Expand Down Expand Up @@ -356,11 +357,11 @@ mod tests {
fn test_generate_cpp_properties_mapped_cxx_name() {
let properties = vec![ParsedQProperty {
ident: format_ident!("mapped_property"),
ty: parse_quote! { A1 },
ty: parse_quote! { A },
}];
let qobject_idents = create_qobjectname();

let mut type_names = TypeNames::default();
let mut type_names = TypeNames::mock();
type_names.insert("A", None, Some("A1"), None);

let generated = generate_cpp_properties(&properties, &qobject_idents, &type_names).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/src/generator/cpp/qenum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn generate(
let mut generated = GeneratedCppQObjectBlocks::default();

for qenum in qenums {
let mut qualified_name = type_names.cxx_qualified(&qenum.ident);
let mut qualified_name = type_names.cxx_qualified(&qenum.ident)?;
let enum_name = type_names.cxx_unqualified(&qenum.ident)?;
// TODO: this is a workaround for type_names.cxx_qualified not always returning a fully-qualified
// identifier.
Expand Down
25 changes: 13 additions & 12 deletions crates/cxx-qt-gen/src/generator/cpp/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn parameter_types_and_values(
let parameter_named_types = parameter_named_types_with_self.join(", ");

// Insert the extra argument into the closure
let self_ty = type_names.cxx_qualified(self_ty);
let self_ty = type_names.cxx_qualified(self_ty)?;
parameter_named_types_with_self.insert(0, format!("{self_ty}& self"));
parameter_types_with_self.insert(0, format!("{self_ty}&"));
parameter_values_with_self.insert(0, "self".to_owned());
Expand All @@ -92,11 +92,11 @@ pub fn generate_cpp_signal(
.insert("#include <cxx-qt/signalhandler.h>".to_owned());

// Build a namespace that includes any namespace for the T
let qobject_ident_namespaced = type_names.cxx_qualified(qobject_ident);
let qobject_ident_namespaced = type_names.cxx_qualified(qobject_ident)?;

// Prepare the idents
let idents = QSignalName::from(signal);
let idents_helper = QSignalHelperName::new(&idents, qobject_ident, type_names);
let idents_helper = QSignalHelperName::new(&idents, qobject_ident, type_names)?;

let signal_ident = idents.name.cpp;
let free_connect_ident_cpp = idents_helper.connect_name.cpp;
Expand Down Expand Up @@ -250,8 +250,9 @@ mod tests {
}];
let qobject_idents = create_qobjectname();

let generated =
generate_cpp_signals(&signals, &qobject_idents, &TypeNames::default()).unwrap();
let mut type_names = TypeNames::mock();
type_names.insert("QColor", None, None, None);
let generated = generate_cpp_signals(&signals, &qobject_idents, &type_names).unwrap();

assert_eq!(generated.methods.len(), 1);
let header = if let CppFragment::Header(header) = &generated.methods[0] {
Expand Down Expand Up @@ -331,13 +332,13 @@ mod tests {
fn test_generate_cpp_signals_mapped_cxx_name() {
let signals = vec![ParsedSignal {
method: parse_quote! {
fn data_changed(self: Pin<&mut MyObject>, mapped: A1);
fn data_changed(self: Pin<&mut MyObject>, mapped: A);
},
qobject_ident: format_ident!("MyObject"),
mutable: true,
parameters: vec![ParsedFunctionParameter {
ident: format_ident!("mapped"),
ty: parse_quote! { A1 },
ty: parse_quote! { A },
}],
ident: CombinedIdent {
cpp: format_ident!("dataChanged"),
Expand All @@ -349,7 +350,7 @@ mod tests {
}];
let qobject_idents = create_qobjectname();

let mut type_names = TypeNames::default();
let mut type_names = TypeNames::mock();
type_names.insert("A", None, Some("A1"), None);

let generated = generate_cpp_signals(&signals, &qobject_idents, &type_names).unwrap();
Expand Down Expand Up @@ -444,9 +445,8 @@ mod tests {
private: false,
}];
let qobject_idents = create_qobjectname();

let generated =
generate_cpp_signals(&signals, &qobject_idents, &TypeNames::default()).unwrap();
generate_cpp_signals(&signals, &qobject_idents, &TypeNames::mock()).unwrap();

assert_eq!(generated.methods.len(), 0);
assert_eq!(generated.fragments.len(), 1);
Expand Down Expand Up @@ -531,8 +531,9 @@ mod tests {
private: false,
};

let generated =
generate_cpp_signal(&signal, &signal.qobject_ident, &TypeNames::default()).unwrap();
let mut type_names = TypeNames::default();
type_names.insert("ObjRust", None, None, None);
let generated = generate_cpp_signal(&signal, &signal.qobject_ident, &type_names).unwrap();

assert_eq!(generated.methods.len(), 0);

Expand Down
14 changes: 9 additions & 5 deletions crates/cxx-qt-gen/src/generator/naming/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::parser::signals::ParsedSignal;
use crate::{generator::naming::CombinedIdent, naming::TypeNames};
use convert_case::{Case, Casing};
use quote::format_ident;
use syn::Ident;
use syn::{Ident, Result};

/// Names for parts of a Q_SIGNAL
pub struct QSignalName {
Expand Down Expand Up @@ -52,7 +52,11 @@ pub struct QSignalHelperName {
}

impl QSignalHelperName {
pub fn new(idents: &QSignalName, qobject_ident: &Ident, type_names: &TypeNames) -> Self {
pub fn new(
idents: &QSignalName,
qobject_ident: &Ident,
type_names: &TypeNames,
) -> Result<Self> {
let signal_ident = &idents.name.cpp;
let handler_alias = format_ident!("{qobject_ident}CxxQtSignalHandler{signal_ident}");
let namespace = {
Expand All @@ -67,7 +71,7 @@ impl QSignalHelperName {
//
// See the comment on TypeNames::cxx_qualified for why fully qualifying is
// unfortunately not possible.
let qobject_namespace = type_names.namespace(qobject_ident);
let qobject_namespace = type_names.namespace(qobject_ident)?;
let namespace: Vec<_> = qobject_namespace
.into_iter()
.chain(vec!["rust::cxxqtgen1".to_owned()])
Expand All @@ -78,7 +82,7 @@ impl QSignalHelperName {

// TODO: in the future we might improve the naming of the methods
// to avoid collisions (maybe use a separator similar to how CXX uses $?)
Self {
Ok(Self {
connect_name: CombinedIdent {
cpp: format_ident!("{}_{}", qobject_ident, idents.connect_name.cpp),
rust: format_ident!("{}_{}", qobject_ident, idents.connect_name.rust),
Expand All @@ -90,7 +94,7 @@ impl QSignalHelperName {
struct_param: format_ident!("{qobject_ident}CxxQtSignalParams{signal_ident}"),
namespace,
handler_alias,
}
})
}
}

Expand Down
Loading

0 comments on commit c42f8fa

Please sign in to comment.