qt_macros/lib.rs
1//! Macros for Qt.
2//!
3//! This crate shouldn't be used directly. The macros are reexported by `qt_core`
4//! and `qt_ui_tools` crates.
5//!
6//! This crate is part of the [ritual](https://github.com/rust-qt/ritual) project.
7
8#![deny(missing_docs)]
9
10use proc_macro::TokenStream;
11use proc_macro_hack::proc_macro_hack;
12
13mod q_init_resource;
14mod slot;
15mod ui_form;
16
17/// Generates a method that returns a slot object bound to `self`.
18///
19/// # Usage
20///
21/// This attribute may be used on methods:
22///
23/// ```ignore
24/// impl TodoWidget {
25/// #[slot(SlotNoArgs)]
26/// unsafe fn on_add_clicked(self: &Rc<Self>) {
27/// //...
28/// }
29/// }
30/// ```
31///
32/// The type of slot wrapper (e.g. `SlotNoArgs`) must be specified as an argument to the attribute.
33/// This type must be in scope.
34///
35/// The macro generates another method that is called `slot_{original_name}` and can be used for
36/// making a connection like this:
37/// ```ignore
38/// self.form.add.clicked().connect(&self.slot_on_add_clicked());
39/// ```
40///
41/// The method accepts a `&Rc<Self>` and returns a `QBox<Slot>`, where `Slot` is the slot wrapper
42/// type passed to the attribute. The slot wrapper retains a weak reference to `Self`, so it
43/// doesn't prevent deletion of the object.
44///
45/// Note that each invokation of the slot getter will create a new object.
46///
47/// # Requirements
48/// - Target method must have `self: &Rc<Self>` argument.
49/// - The rest of the arguments must correspond to arguments expected by the specified
50/// slot wrapper type.
51/// - `Self` must implement `StaticUpcast<QObject>`. Created slots will use the result of this
52/// conversion as the parent object.
53#[proc_macro_attribute]
54pub fn slot(attrs: TokenStream, input: TokenStream) -> TokenStream {
55 crate::slot::slot(attrs, input)
56}
57
58/// Generates code for loading an UI file.
59///
60/// # Usage
61///
62/// This attribute should be used on structs:
63/// ```ignore
64/// #[ui_form("../ui/form.ui")]
65/// #[derive(Debug)]
66/// struct Form {
67/// widget: QBox<QWidget>,
68/// add: QPtr<QPushButton>,
69/// //...
70/// }
71/// ```
72///
73/// Specify path to the UI file as an argument of the attribute. The path must be relative to
74/// the current file. Content of the UI file will be embedded into the executable.
75///
76/// The macro will generate the function `fn load() -> Self`.
77///
78/// # Requirements
79///
80/// - The struct must contain named fields.
81/// - The first argument must have `QBox<QWidget>` type. This argument will contain the main widget.
82/// - Each of the following arguments must have a name corresponding to `objectName` of an object
83/// in the UI file. The type of the field must be `QPtr<T>`, where `T` must correspond to the type
84/// of the object.
85///
86/// The `load()` function will panic if the UI file is invalid or if a name or type of any field
87/// doesn't match the objects in the UI file.
88#[proc_macro_attribute]
89pub fn ui_form(attrs: TokenStream, input: TokenStream) -> TokenStream {
90 crate::ui_form::ui_form(attrs, input)
91}
92
93// This is an implementation detail of the `qt_core::q_init_resource` macro.
94#[doc(hidden)]
95#[proc_macro_hack]
96pub fn q_init_resource(input: TokenStream) -> TokenStream {
97 crate::q_init_resource::q_init_resource(input)
98}