cxx_qt_lib/core/
qrect.rs

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