cxx_qt_lib/core/qrectf.rs
1// SPDX-FileCopyrightText: 2021 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/qmarginsf.h");
13 type QMarginsF = crate::QMarginsF;
14 include!("cxx-qt-lib/qpointf.h");
15 type QPointF = crate::QPointF;
16 include!("cxx-qt-lib/qrect.h");
17 type QRect = crate::QRect;
18 include!("cxx-qt-lib/qrectf.h");
19 type QRectF = super::QRectF;
20 include!("cxx-qt-lib/qsizef.h");
21 type QSizeF = crate::QSizeF;
22 include!("cxx-qt-lib/qstring.h");
23 type QString = crate::QString;
24
25 /// Adds dx1, dy1, dx2 and dy2 respectively to the existing coordinates of the rectangle. All parameters must be finite.
26 fn adjust(self: &mut QRectF, dx1: f64, dy1: f64, dx2: f64, dy2: f64);
27
28 /// Returns a new rectangle with dx1, dy1, dx2 and dy2 added respectively to the existing coordinates of this rectangle. All parameters must be finite.
29 fn adjusted(self: &QRectF, dx1: f64, dy1: f64, dx2: f64, dy2: f64) -> QRectF;
30
31 /// Returns the y-coordinate of the rectangle's bottom edge.
32 fn bottom(self: &QRectF) -> f64;
33
34 /// Returns the position of the rectangle's bottom-left corner.
35 #[rust_name = "bottom_left"]
36 fn bottomLeft(self: &QRectF) -> QPointF;
37
38 /// Returns the position of the rectangle's bottom-right corner.
39 #[rust_name = "bottom_right"]
40 fn bottomRight(self: &QRectF) -> QPointF;
41
42 /// Returns the center point of the rectangle.
43 fn center(self: &QRectF) -> QPointF;
44
45 /// Returns true if the given point is inside or on the edge of the rectangle; otherwise returns false.
46 fn contains(self: &QRectF, point: &QPointF) -> bool;
47
48 /// Returns the height of the rectangle.
49 fn height(self: &QRectF) -> f64;
50
51 /// Returns the intersection of this rectangle and the given rectangle. Note that r.intersected(s) is equivalent to r & s.
52 fn intersected(self: &QRectF, rectangle: &QRectF) -> QRectF;
53
54 /// Returns true if this rectangle intersects with the given rectangle (i.e. there is a non-empty area of overlap between them), otherwise returns false.
55 fn intersects(self: &QRectF, rectangle: &QRectF) -> bool;
56
57 /// Returns true if the rectangle is empty, otherwise returns false.
58 ///
59 /// An empty rectangle has width() <= 0 or height() <= 0. An empty rectangle is not valid (i.e., isEmpty() == !isValid()).
60 #[rust_name = "is_empty"]
61 fn isEmpty(self: &QRectF) -> bool;
62
63 /// Returns true if the rectangle is a null rectangle, otherwise returns false.
64 ///
65 /// A null rectangle has both the width and the height set to 0. A null rectangle is also empty, and hence not valid.
66 #[rust_name = "is_null"]
67 fn isNull(self: &QRectF) -> bool;
68
69 /// Returns true if the rectangle is valid, otherwise returns false.
70 ///
71 /// A valid rectangle has a width() > 0 and height() > 0. Note that non-trivial operations like intersections are not defined for invalid rectangles. A valid rectangle is not empty (i.e., isValid() == !isEmpty()).
72 #[rust_name = "is_valid"]
73 fn isValid(self: &QRectF) -> bool;
74
75 /// Returns the x-coordinate of the rectangle's left edge. Equivalent to x().
76 fn left(self: &QRectF) -> f64;
77
78 /// Returns a rectangle grown by the margins.
79 #[rust_name = "margins_added"]
80 fn marginsAdded(self: &QRectF, margins: &QMarginsF) -> QRectF;
81
82 /// Removes the margins from the rectangle, shrinking it.
83 #[rust_name = "margins_removed"]
84 fn marginsRemoved(self: &QRectF, margins: &QMarginsF) -> QRectF;
85
86 /// Moves the rectangle vertically, leaving the rectangle's bottom edge at the given finite y coordinate. The rectangle's size is unchanged.
87 #[rust_name = "move_bottom"]
88 fn moveBottom(self: &mut QRectF, y: f64);
89
90 /// Moves the rectangle, leaving the bottom-left corner at the given position. The rectangle's size is unchanged.
91 #[rust_name = "move_bottom_left"]
92 fn moveBottomLeft(self: &mut QRectF, position: &QPointF);
93
94 /// Moves the rectangle, leaving the bottom-right corner at the given position. The rectangle's size is unchanged.
95 #[rust_name = "move_bottom_right"]
96 fn moveBottomRight(self: &mut QRectF, position: &QPointF);
97
98 /// Moves the rectangle, leaving the center point at the given position. The rectangle's size is unchanged.
99 #[rust_name = "move_center"]
100 fn moveCenter(self: &mut QRectF, position: &QPointF);
101
102 /// Moves the rectangle horizontally, leaving the rectangle's left edge at the given finite x coordinate. The rectangle's size is unchanged.
103 #[rust_name = "move_left"]
104 fn moveLeft(self: &mut QRectF, x: f64);
105
106 /// Moves the rectangle horizontally, leaving the rectangle's right edge at the given finite x coordinate. The rectangle's size is unchanged.
107 #[rust_name = "move_right"]
108 fn moveRight(self: &mut QRectF, x: f64);
109
110 /// Moves the rectangle, leaving the top-left corner at the given position.
111 #[rust_name = "move_to"]
112 fn moveTo(self: &mut QRectF, position: &QPointF);
113
114 /// Moves the rectangle vertically, leaving the rectangle's top line at the given finite y coordinate. The rectangle's size is unchanged.
115 #[rust_name = "move_top"]
116 fn moveTop(self: &mut QRectF, y: f64);
117
118 /// Moves the rectangle, leaving the top-left corner at the given position. The rectangle's size is unchanged.
119 #[rust_name = "move_top_left"]
120 fn moveTopLeft(self: &mut QRectF, position: &QPointF);
121
122 /// Moves the rectangle, leaving the top-right corner at the given position. The rectangle's size is unchanged.
123 #[rust_name = "move_top_right"]
124 fn moveTopRight(self: &mut QRectF, position: &QPointF);
125
126 /// Returns a normalized rectangle; i.e., a rectangle that has a non-negative width and height.
127 fn normalized(self: &QRectF) -> QRectF;
128
129 /// Returns the x-coordinate of the rectangle's right edge.
130 fn right(self: &QRectF) -> f64;
131
132 /// Sets the bottom edge of the rectangle to the given finite y coordinate.
133 /// May change the height, but will never change the top edge of the rectangle.
134 #[rust_name = "set_bottom"]
135 fn setBottom(self: &mut QRectF, y: f64);
136
137 /// Set the bottom-left corner of the rectangle to the given position.
138 /// May change the size, but will never change the top-right corner of the rectangle.
139 #[rust_name = "set_bottom_left"]
140 fn setBottomLeft(self: &mut QRectF, position: &QPointF);
141
142 /// Set the bottom-right corner of the rectangle to the given position.
143 /// May change the size, but will never change the top-left corner of the rectangle.
144 #[rust_name = "set_bottom_right"]
145 fn setBottomRight(self: &mut QRectF, position: &QPointF);
146
147 /// Sets the coordinates of the rectangle's top-left corner to (x1, y1),
148 /// and the coordinates of its bottom-right corner to (x2, y2). All parameters must be finite.
149 #[rust_name = "set_coords"]
150 fn setCoords(self: &mut QRectF, x1: f64, y1: f64, x2: f64, y2: f64);
151
152 /// Sets the height of the rectangle to the given finite height. The bottom edge is changed, but not the top one.
153 #[rust_name = "set_height"]
154 fn setHeight(self: &mut QRectF, h: f64);
155
156 /// Sets the left edge of the rectangle to the given finite x coordinate.
157 /// May change the width, but will never change the right edge of the rectangle.
158 #[rust_name = "set_left"]
159 fn setLeft(self: &mut QRectF, x: f64);
160
161 /// Sets the coordinates of the rectangle's top-left corner to (x, y), and its size to the given width and height. All parameters must be finite.
162 #[rust_name = "set_rect"]
163 fn setRect(self: &mut QRectF, x: f64, y: f64, width: f64, height: f64);
164
165 /// Sets the right edge of the rectangle to the given finite x coordinate.
166 /// May change the width, but will never change the left edge of the rectangle.
167 #[rust_name = "set_right"]
168 fn setRight(self: &mut QRectF, x: f64);
169
170 /// Sets the size of the rectangle to the given finite size. The top-left corner is not moved.
171 #[rust_name = "set_size"]
172 fn setSize(self: &mut QRectF, size: &QSizeF);
173
174 /// Sets the top edge of the rectangle to the given finite y coordinate.
175 /// May change the height, but will never change the bottom edge of the rectangle.
176 #[rust_name = "set_top"]
177 fn setTop(self: &mut QRectF, y: f64);
178
179 /// Set the top-left corner of the rectangle to the given position.
180 /// May change the size, but will never change the bottom-right corner of the rectangle.
181 #[rust_name = "set_top_left"]
182 fn setTopLeft(self: &mut QRectF, position: &QPointF);
183
184 /// Set the top-right corner of the rectangle to the given position.
185 /// May change the size, but will never change the bottom-left corner of the rectangle.
186 #[rust_name = "set_top_right"]
187 fn setTopRight(self: &mut QRectF, position: &QPointF);
188
189 /// Sets the width of the rectangle to the given finite width. The right edge is changed, but not the left one.
190 #[rust_name = "set_width"]
191 fn setWidth(self: &mut QRectF, w: f64);
192
193 /// Sets the left edge of the rectangle to the given finite x coordinate.
194 /// May change the width, but will never change the right edge of the rectangle.
195 #[rust_name = "set_x"]
196 fn setX(self: &mut QRectF, x: f64);
197
198 /// Sets the top edge of the rectangle to the given finite y coordinate.
199 /// May change the height, but will never change the bottom edge of the rectangle.
200 #[rust_name = "set_y"]
201 fn setY(self: &mut QRectF, y: f64);
202
203 /// Returns the size of the rectangle.
204 fn size(self: &QRectF) -> QSizeF;
205
206 /// Returns a QRect based on the values of this rectangle that is the smallest possible integer rectangle that completely contains this rectangle.
207 #[rust_name = "to_aligned_rect"]
208 fn toAlignedRect(self: &QRectF) -> QRect;
209
210 /// Returns a QRect based on the values of this rectangle.
211 /// Note that the coordinates in the returned rectangle are rounded to the nearest integer.
212 #[rust_name = "to_rect"]
213 fn toRect(self: &QRectF) -> QRect;
214
215 /// Returns the y-coordinate of the rectangle's top edge. Equivalent to y().
216 fn top(self: &QRectF) -> f64;
217
218 /// Returns the position of the rectangle's top-left corner.
219 #[rust_name = "top_left"]
220 fn topLeft(self: &QRectF) -> QPointF;
221
222 /// Returns the position of the rectangle's top-right corner.
223 #[rust_name = "top_right"]
224 fn topRight(self: &QRectF) -> QPointF;
225
226 /// Moves the rectangle offset.x() along the x axis and offset.y() along the y axis, relative to the current position.
227 fn translate(self: &mut QRectF, offset: &QPointF);
228
229 /// Returns a copy of the rectangle that is translated offset.x() along the x axis and offset.y() along the y axis, relative to the current position.
230 fn translated(self: &QRectF, offset: &QPointF) -> QRectF;
231
232 /// Returns a copy of the rectangle that has its width and height exchanged.
233 fn transposed(self: &QRectF) -> QRectF;
234
235 /// Returns the bounding rectangle of this rectangle and the given rectangle.
236 fn united(self: &QRectF, rectangle: &QRectF) -> QRectF;
237
238 /// Returns the width of the rectangle.
239 fn width(self: &QRectF) -> f64;
240
241 /// Returns the x-coordinate of the rectangle's left edge.
242 fn x(self: &QRectF) -> f64;
243
244 /// Returns the y-coordinate of the rectangle's top edge.
245 fn y(self: &QRectF) -> f64;
246 }
247
248 #[namespace = "rust::cxxqtlib1"]
249 unsafe extern "C++" {
250 include!("cxx-qt-lib/common.h");
251
252 #[doc(hidden)]
253 #[rust_name = "qrectf_init_default"]
254 fn construct() -> QRectF;
255 #[doc(hidden)]
256 #[rust_name = "qrectf_init"]
257 fn construct(x: f64, y: f64, width: f64, height: f64) -> QRectF;
258 #[doc(hidden)]
259 #[rust_name = "qrectf_from_qrect"]
260 fn construct(rectangle: &QRect) -> QRectF;
261 #[doc(hidden)]
262 #[rust_name = "qrectf_to_qstring"]
263 fn toQString(value: &QRectF) -> QString;
264 #[doc(hidden)]
265 #[rust_name = "qrectf_plus"]
266 fn operatorPlus(a: &QRectF, b: &QMarginsF) -> QRectF;
267 #[doc(hidden)]
268 #[rust_name = "qrectf_minus"]
269 fn operatorMinus(a: &QRectF, b: &QMarginsF) -> QRectF;
270 }
271}
272
273/// The QRectF struct defines a rectangle in the plane using floating point precision.
274#[derive(Debug, Clone, PartialEq)]
275#[repr(C)]
276pub struct QRectF {
277 xp: f64,
278 yp: f64,
279 w: f64,
280 h: f64,
281}
282
283impl QRectF {
284 /// Constructs a rectangle with (x, y) as its top-left corner and the given width and height.
285 pub fn new(x: f64, y: f64, width: f64, height: f64) -> Self {
286 ffi::qrectf_init(x, y, width, height)
287 }
288}
289
290impl Default for QRectF {
291 /// Constructs a null rectangle.
292 fn default() -> Self {
293 ffi::qrectf_init_default()
294 }
295}
296
297impl fmt::Display for QRectF {
298 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
299 write!(f, "{}", ffi::qrectf_to_qstring(self))
300 }
301}
302
303impl From<&ffi::QRect> for QRectF {
304 /// Constructs a QRectF rectangle from the given QRect rectangle.
305 fn from(rectangle: &ffi::QRect) -> Self {
306 ffi::qrectf_from_qrect(rectangle)
307 }
308}
309
310impl From<&QRectF> for ffi::QRect {
311 /// Returns a QRect based on the values of this rectangle.
312 /// Note that the coordinates in the returned rectangle are rounded to the nearest integer.
313 fn from(value: &QRectF) -> Self {
314 value.to_rect()
315 }
316}
317
318impl std::ops::Add<ffi::QMarginsF> for QRectF {
319 type Output = Self;
320 fn add(self, other: ffi::QMarginsF) -> Self {
321 ffi::qrectf_plus(&self, &other)
322 }
323}
324
325impl std::ops::Sub<ffi::QMarginsF> for QRectF {
326 type Output = Self;
327 fn sub(self, other: ffi::QMarginsF) -> Self {
328 ffi::qrectf_minus(&self, &other)
329 }
330}
331
332// Safety:
333//
334// Static checks on the C++ side ensure that QRectF is trivial.
335unsafe impl ExternType for QRectF {
336 type Id = type_id!("QRectF");
337 type Kind = cxx::kind::Trivial;
338}