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

Draft: Add support for attributes to #[qproperty] #451

Closed
wants to merge 2 commits into from

Conversation

CarlSchwan
Copy link

Make it possible to define if only a getter should be generated or only a setter and/or define special getter and setter.

Syntax is as follow

struct MyObject {
    #[qproperty]
    prop1: f32, // generate default getter and setter

    #[qproperty(get)]
    prop2: f32, // generate default getter only

    #[qproperty(set)]
    prop3: f32, // generate default setter only

    #[qproperty(get = Self::prop4_fn)]
    prop4: f32, // use prop4_fn as getter
}

WIP since the custom getter part doesn't work yet

@@ -33,6 +33,7 @@ impl GeneratedRustQObjectBlocks {
}
}

#[derive(Debug)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reasoning for these extra derive Debug's ? They could be done in a separate commit if they are useful ...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly for debugging, I'll remove then

Comment on lines 42 to 45
let default_setter = match property.set {
Some(MaybeCustomFn::Default) => true,
_ => false,
};
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let default_setter = match property.set {
Some(MaybeCustomFn::Default) => true,
_ => false,
};

@@ -33,6 +33,7 @@ impl GeneratedRustQObjectBlocks {
}
}

#[derive(Debug)]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly for debugging, I'll remove then

@ahayzen-kdab
Copy link
Collaborator

Also note the clippy warnings :-)

@@ -4,15 +4,30 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't forget to add an entry to the CHANGELOG.md :-)

And update anything in the book and we'll have to find somewhere in the properties example of qml_features to use it

Carl Schwan added 2 commits April 27, 2023 15:21
Make it possible to define if only a getter should be generated or only
a setter and/or define special getter and setter.

Syntax is as follow

```rust
struct MyObject {
    #[qproperty]
    prop1: f32, // generate default getter and setter

    #[qproperty(get)]
    prop2: f32, // generate default getter only

    #[qproperty(set)]
    prop3: f32, // generate default setter only

    #[qproperty(get = Self::prop4_fn)]
    prop4: f32, // use prop4_fn as getter
}
```

WIP since the custom getter part doesn't work yet

Signed-off-by: Carl Schwan <[email protected]>
@CarlSchwan CarlSchwan force-pushed the work/carl/custom-getter branch from a6171e2 to 3928c86 Compare April 27, 2023 14:58
@@ -407,7 +407,7 @@ impl CxxQtBuilder {
// Use a separate cc::Build for the little amount of code that needs to be linked with +whole-archive
// to avoid bloating the binary.
let mut cc_builder_whole_archive = cc::Build::new();
cc_builder_whole_archive.link_lib_modifier("+whole-archive");
//cc_builder_whole_archive.link_lib_modifier("+whole-archive");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//cc_builder_whole_archive.link_lib_modifier("+whole-archive");

remove before merging

@LeonMatthesKDAB
Copy link
Collaborator

@CarlSchwan good to see this change progressing nicely 👍

Quick chime-in on the #[qproperty(get=Self::x)] API.
I've just noticed that the Self:: part might be redundant, or even misleading in the worst-case, so it would be simpler to just leave it out in favor of #[qproperty(get=x)].

The Self:: within the struct MyObject technically refers to MyObject, not qobject::MyObject, but the getter x must be implemented on qobject::MyObject, so Self:: doesn't really refer to the right struct, technically.
Furthermore, a getter (or setter for that matter) must always be implemented on the qobject:: of the given struct, so there's no real point of listing the Self::, it's just boilerplate I think and parsing would be simplified by removing it.

Or is there any way one could specify a getter/setter that is not on the qobject::MyObject? 🤔

@CarlSchwan
Copy link
Author

@CarlSchwan good to see this change progressing nicely +1

Quick chime-in on the #[qproperty(get=Self::x)] API. I've just noticed that the Self:: part might be redundant, or even misleading in the worst-case, so it would be simpler to just leave it out in favor of #[qproperty(get=x)].

The Self:: within the struct MyObject technically refers to MyObject, not qobject::MyObject, but the getter x must be implemented on qobject::MyObject, so Self:: doesn't really refer to the right struct, technically. Furthermore, a getter (or setter for that matter) must always be implemented on the qobject:: of the given struct, so there's no real point of listing the Self::, it's just boilerplate I think and parsing would be simplified by removing it.

Or is there any way one could specify a getter/setter that is not on the qobject::MyObject? thinking

I don't think there is a way to specify a getter/setter that is not on the qobject::MyObject and I don't think this would make sense. Regarding the syntax, I often prefer to make stuff more explicit and adding Self:: make it explicit and I originally tried to have the custom getter/settter in the MyObject instead of the qobject::MyObject but I didn't manage to do it. Syntax wise my objective is to have something as convenient as gtk-rs https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/derive.Properties.html which are quite nice.

But there are still quite some work needed to get there.

@LeonMatthesKDAB
Copy link
Collaborator

I don't think there is a way to specify a getter/setter that is not on the qobject::MyObject and I don't think this would make sense. Regarding the syntax, I often prefer to make stuff more explicit and adding Self:: make it explicit and I originally tried to have the custom getter/settter in the MyObject instead of the qobject::MyObject but I didn't manage to do it. Syntax wise my objective is to have something as convenient as gtk-rs https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/derive.Properties.html which are quite nice.

But there are still quite some work needed to get there.

IMHO, we should still just leave out the Self:: part then, it's more "explicit", but also points you to the wrong object!

GTK-rs also seems to just use the function name.

@LeonMatthesKDAB
Copy link
Collaborator

Superseded by #994

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants