cxx_qt_lib/core/
qmarginsf.rs

1// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
2// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
3//
4// SPDX-License-Identifier: MIT OR Apache-2.0
5
6use cxx::{type_id, ExternType};
7use std::fmt;
8
9#[cxx::bridge]
10mod ffi {
11    unsafe extern "C++" {
12        include!("cxx-qt-lib/qmargins.h");
13        type QMargins = crate::QMargins;
14        include!("cxx-qt-lib/qmarginsf.h");
15        type QMarginsF = super::QMarginsF;
16        include!("cxx-qt-lib/qstring.h");
17        type QString = crate::QString;
18
19        /// Returns the bottom margin.
20        fn bottom(self: &QMarginsF) -> f64;
21
22        /// Returns true if all margins are very close to 0; otherwise returns false.
23        #[rust_name = "is_null"]
24        fn isNull(self: &QMarginsF) -> bool;
25
26        /// Returns the left margin.
27        fn left(self: &QMarginsF) -> f64;
28
29        /// Returns the right margin.
30        fn right(self: &QMarginsF) -> f64;
31
32        /// Sets the bottom margin to abottom (which must be finite).
33        #[rust_name = "set_bottom"]
34        fn setBottom(self: &mut QMarginsF, bottom: f64);
35
36        /// Sets the left margin to aleft (which must be finite).
37        #[rust_name = "set_left"]
38        fn setLeft(self: &mut QMarginsF, left: f64);
39
40        /// Sets the right margin to aright (which must be finite).
41        #[rust_name = "set_right"]
42        fn setRight(self: &mut QMarginsF, right: f64);
43
44        /// Sets the top margin to atop (which must be finite).
45        #[rust_name = "set_top"]
46        fn setTop(self: &mut QMarginsF, top: f64);
47
48        /// Returns an integer-based copy of this margins object.
49        ///
50        /// Note that the components in the returned margins will be rounded to the nearest integer.
51        #[rust_name = "to_margins"]
52        fn toMargins(self: &QMarginsF) -> QMargins;
53
54        /// Returns the top margin.
55        fn top(self: &QMarginsF) -> f64;
56    }
57
58    #[namespace = "rust::cxxqtlib1"]
59    unsafe extern "C++" {
60        include!("cxx-qt-lib/common.h");
61
62        #[doc(hidden)]
63        #[rust_name = "qmarginsf_default"]
64        fn construct() -> QMarginsF;
65        #[doc(hidden)]
66        #[rust_name = "qmarginsf_from_qmargin"]
67        fn construct(margins: &QMargins) -> QMarginsF;
68        #[doc(hidden)]
69        #[rust_name = "qmarginsf_new"]
70        fn construct(left: f64, top: f64, right: f64, bottom: f64) -> QMarginsF;
71        #[doc(hidden)]
72        #[rust_name = "qmarginsf_to_qstring"]
73        fn toQString(value: &QMarginsF) -> QString;
74        #[doc(hidden)]
75        #[rust_name = "qmarginsf_plus"]
76        fn operatorPlus(a: &QMarginsF, b: &QMarginsF) -> QMarginsF;
77        #[doc(hidden)]
78        #[rust_name = "qmarginsf_plus_f64"]
79        fn operatorPlus(a: &QMarginsF, b: &f64) -> QMarginsF;
80        #[doc(hidden)]
81        #[rust_name = "qmarginsf_minus"]
82        fn operatorMinus(a: &QMarginsF, b: &QMarginsF) -> QMarginsF;
83        #[doc(hidden)]
84        #[rust_name = "qmarginsf_minus_f64"]
85        fn operatorMinus(a: &QMarginsF, b: &f64) -> QMarginsF;
86        #[doc(hidden)]
87        #[rust_name = "qmarginsf_mul"]
88        fn operatorMul(a: f64, b: &QMarginsF) -> QMarginsF;
89        #[doc(hidden)]
90        #[rust_name = "qmarginsf_div"]
91        fn operatorDiv(a: f64, b: &QMarginsF) -> QMarginsF;
92    }
93}
94
95/// The QMarginsF class defines the four margins of a rectangle.
96#[derive(Debug, Clone, PartialEq)]
97#[repr(C)]
98pub struct QMarginsF {
99    left: f64,
100    top: f64,
101    right: f64,
102    bottom: f64,
103}
104
105impl QMarginsF {
106    /// Constructs margins with the given left, top, right, and bottom. All parameters must be finite.
107    pub fn new(left: f64, top: f64, right: f64, bottom: f64) -> Self {
108        ffi::qmarginsf_new(left, top, right, bottom)
109    }
110}
111
112impl Default for QMarginsF {
113    /// Constructs a margins object with all margins set to 0.
114    fn default() -> Self {
115        ffi::qmarginsf_default()
116    }
117}
118
119impl fmt::Display for QMarginsF {
120    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
121        write!(f, "{}", ffi::qmarginsf_to_qstring(self))
122    }
123}
124
125impl std::ops::Add for QMarginsF {
126    type Output = Self;
127    fn add(self, other: Self) -> Self {
128        ffi::qmarginsf_plus(&self, &other)
129    }
130}
131
132impl std::ops::Add<f64> for QMarginsF {
133    type Output = Self;
134    fn add(self, other: f64) -> Self {
135        ffi::qmarginsf_plus_f64(&self, &other)
136    }
137}
138
139impl std::ops::Sub for QMarginsF {
140    type Output = Self;
141    fn sub(self, other: Self) -> Self {
142        ffi::qmarginsf_minus(&self, &other)
143    }
144}
145
146impl std::ops::Sub<f64> for QMarginsF {
147    type Output = Self;
148    fn sub(self, other: f64) -> Self {
149        ffi::qmarginsf_minus_f64(&self, &other)
150    }
151}
152
153impl std::ops::Mul<f64> for QMarginsF {
154    type Output = Self;
155    fn mul(self, rhs: f64) -> Self {
156        ffi::qmarginsf_mul(rhs, &self)
157    }
158}
159
160impl std::ops::Div<f64> for QMarginsF {
161    type Output = Self;
162    fn div(self, rhs: f64) -> Self {
163        ffi::qmarginsf_div(rhs, &self)
164    }
165}
166
167impl From<&ffi::QMargins> for QMarginsF {
168    /// Constructs margins copied from the given margins.
169    fn from(margins: &ffi::QMargins) -> Self {
170        ffi::qmarginsf_from_qmargin(margins)
171    }
172}
173
174impl From<&QMarginsF> for ffi::QMargins {
175    /// Returns an integer-based copy of this margins object.
176    ///
177    /// Note that the components in the returned margins will be rounded to the nearest integer.
178    fn from(value: &QMarginsF) -> Self {
179        value.to_margins()
180    }
181}
182
183// Safety:
184//
185// Static checks on the C++ side ensure that QMarginsF is trivial.
186unsafe impl ExternType for QMarginsF {
187    type Id = type_id!("QMarginsF");
188    type Kind = cxx::kind::Trivial;
189}