cxx_qt_lib/core/
qmargins.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 = super::QMargins;
14        include!("cxx-qt-lib/qstring.h");
15        type QString = crate::QString;
16        include!("cxx-qt-lib/qmarginsf.h");
17        #[allow(dead_code)]
18        type QMarginsF = crate::QMarginsF;
19
20        /// Returns the bottom margin.
21        fn bottom(self: &QMargins) -> i32;
22
23        /// Returns true if all margins are is 0; otherwise returns false.
24        #[rust_name = "is_null"]
25        fn isNull(self: &QMargins) -> bool;
26
27        /// Returns the left margin.
28        fn left(self: &QMargins) -> i32;
29
30        /// Returns the right margin.
31        fn right(self: &QMargins) -> i32;
32
33        /// Sets the bottom margin to bottom.
34        #[rust_name = "set_bottom"]
35        fn setBottom(self: &mut QMargins, bottom: i32);
36
37        /// Sets the left margin to left.
38        #[rust_name = "set_left"]
39        fn setLeft(self: &mut QMargins, left: i32);
40
41        /// Sets the right margin to right.
42        #[rust_name = "set_right"]
43        fn setRight(self: &mut QMargins, right: i32);
44
45        /// Sets the Top margin to Top.
46        #[rust_name = "set_top"]
47        fn setTop(self: &mut QMargins, top: i32);
48
49        /// Returns these margins as margins with floating point accuracy.
50        /// since Qt6.4
51        #[cfg(any(cxxqt_qt_version_at_least_7, cxxqt_qt_version_at_least_6_4))]
52        #[rust_name = "to_marginsf"]
53        fn toMarginsF(self: &QMargins) -> QMarginsF;
54
55        /// Returns the top margin.
56        fn top(self: &QMargins) -> i32;
57    }
58
59    #[namespace = "rust::cxxqtlib1"]
60    unsafe extern "C++" {
61        include!("cxx-qt-lib/common.h");
62
63        #[doc(hidden)]
64        #[rust_name = "qmargins_default"]
65        fn construct() -> QMargins;
66        #[doc(hidden)]
67        #[rust_name = "qmargins_new"]
68        fn construct(left: i32, top: i32, right: i32, bottom: i32) -> QMargins;
69        #[doc(hidden)]
70        #[rust_name = "qmargins_to_qstring"]
71        fn toQString(value: &QMargins) -> QString;
72        #[doc(hidden)]
73        #[rust_name = "qmargins_plus"]
74        fn operatorPlus(a: &QMargins, b: &QMargins) -> QMargins;
75        #[doc(hidden)]
76        #[rust_name = "qmargins_plus_i32"]
77        fn operatorPlus(a: &QMargins, b: &i32) -> QMargins;
78        #[doc(hidden)]
79        #[rust_name = "qmargins_minus"]
80        fn operatorMinus(a: &QMargins, b: &QMargins) -> QMargins;
81        #[doc(hidden)]
82        #[rust_name = "qmargins_minus_i32"]
83        fn operatorMinus(a: &QMargins, b: &i32) -> QMargins;
84        #[doc(hidden)]
85        #[rust_name = "qmargins_mul_i32"]
86        fn operatorMul(a: i32, b: &QMargins) -> QMargins;
87        #[doc(hidden)]
88        #[rust_name = "qmargins_mul_f64"]
89        fn operatorMul(a: f64, b: &QMargins) -> QMargins;
90        #[doc(hidden)]
91        #[rust_name = "qmargins_div_i32"]
92        fn operatorDiv(a: i32, b: &QMargins) -> QMargins;
93        #[doc(hidden)]
94        #[rust_name = "qmargins_div_f64"]
95        fn operatorDiv(a: f64, b: &QMargins) -> QMargins;
96    }
97}
98
99/// The QMargins class defines the four margins of a rectangle.
100#[derive(Debug, Clone, PartialEq, Eq)]
101#[repr(C)]
102pub struct QMargins {
103    left: i32,
104    top: i32,
105    right: i32,
106    bottom: i32,
107}
108
109impl QMargins {
110    /// Constructs margins with the given left, top, right, and bottom
111    pub fn new(left: i32, top: i32, right: i32, bottom: i32) -> Self {
112        ffi::qmargins_new(left, top, right, bottom)
113    }
114}
115
116impl Default for QMargins {
117    /// Constructs a margins object with all margins set to 0.
118    fn default() -> Self {
119        ffi::qmargins_default()
120    }
121}
122
123impl fmt::Display for QMargins {
124    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125        write!(f, "{}", ffi::qmargins_to_qstring(self))
126    }
127}
128
129impl std::ops::Add for QMargins {
130    type Output = Self;
131    fn add(self, other: Self) -> Self {
132        ffi::qmargins_plus(&self, &other)
133    }
134}
135
136impl std::ops::Add<i32> for QMargins {
137    type Output = Self;
138    fn add(self, other: i32) -> Self {
139        ffi::qmargins_plus_i32(&self, &other)
140    }
141}
142
143impl std::ops::Sub for QMargins {
144    type Output = Self;
145    fn sub(self, other: Self) -> Self {
146        ffi::qmargins_minus(&self, &other)
147    }
148}
149
150impl std::ops::Sub<i32> for QMargins {
151    type Output = Self;
152    fn sub(self, other: i32) -> Self {
153        ffi::qmargins_minus_i32(&self, &other)
154    }
155}
156
157impl std::ops::Mul<i32> for QMargins {
158    type Output = Self;
159    fn mul(self, rhs: i32) -> Self {
160        ffi::qmargins_mul_i32(rhs, &self)
161    }
162}
163
164impl std::ops::Mul<f64> for QMargins {
165    type Output = Self;
166    fn mul(self, rhs: f64) -> Self {
167        ffi::qmargins_mul_f64(rhs, &self)
168    }
169}
170
171impl std::ops::Div<i32> for QMargins {
172    type Output = Self;
173    fn div(self, rhs: i32) -> Self {
174        ffi::qmargins_div_i32(rhs, &self)
175    }
176}
177
178impl std::ops::Div<f64> for QMargins {
179    type Output = Self;
180    fn div(self, rhs: f64) -> Self {
181        ffi::qmargins_div_f64(rhs, &self)
182    }
183}
184
185// Safety:
186//
187// Static checks on the C++ side ensure that QMargins is trivial.
188unsafe impl ExternType for QMargins {
189    type Id = type_id!("QMargins");
190    type Kind = cxx::kind::Trivial;
191}