1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// SPDX-FileCopyrightText: 2021 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use cxx::{type_id, ExternType};
use std::fmt;

#[cxx::bridge]
mod ffi {
    unsafe extern "C++" {
        include!("cxx-qt-lib/qmarginsf.h");
        type QMarginsF = crate::QMarginsF;
        include!("cxx-qt-lib/qpointf.h");
        type QPointF = crate::QPointF;
        include!("cxx-qt-lib/qrect.h");
        type QRect = crate::QRect;
        include!("cxx-qt-lib/qrectf.h");
        type QRectF = super::QRectF;
        include!("cxx-qt-lib/qsizef.h");
        type QSizeF = crate::QSizeF;
        include!("cxx-qt-lib/qstring.h");
        type QString = crate::QString;

        /// Adds dx1, dy1, dx2 and dy2 respectively to the existing coordinates of the rectangle. All parameters must be finite.
        fn adjust(self: &mut QRectF, dx1: f64, dy1: f64, dx2: f64, dy2: f64);

        /// Returns a new rectangle with dx1, dy1, dx2 and dy2 added respectively to the existing coordinates of this rectangle. All parameters must be finite.
        fn adjusted(self: &QRectF, dx1: f64, dy1: f64, dx2: f64, dy2: f64) -> QRectF;

        /// Returns the y-coordinate of the rectangle's bottom edge.
        fn bottom(self: &QRectF) -> f64;

        /// Returns the position of the rectangle's bottom-left corner.
        #[rust_name = "bottom_left"]
        fn bottomLeft(self: &QRectF) -> QPointF;

        /// Returns the position of the rectangle's bottom-right corner.
        #[rust_name = "bottom_right"]
        fn bottomRight(self: &QRectF) -> QPointF;

        /// Returns the center point of the rectangle.
        fn center(self: &QRectF) -> QPointF;

        /// Returns true if the given point is inside or on the edge of the rectangle; otherwise returns false.
        fn contains(self: &QRectF, point: &QPointF) -> bool;

        /// Returns the height of the rectangle.
        fn height(self: &QRectF) -> f64;

        /// Returns the intersection of this rectangle and the given rectangle. Note that r.intersected(s) is equivalent to r & s.
        fn intersected(self: &QRectF, rectangle: &QRectF) -> QRectF;

        /// 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.
        fn intersects(self: &QRectF, rectangle: &QRectF) -> bool;

        /// Returns true if the rectangle is empty, otherwise returns false.
        ///
        /// An empty rectangle has width() <= 0 or height() <= 0. An empty rectangle is not valid (i.e., isEmpty() == !isValid()).
        #[rust_name = "is_empty"]
        fn isEmpty(self: &QRectF) -> bool;

        /// Returns true if the rectangle is a null rectangle, otherwise returns false.
        ///
        /// A null rectangle has both the width and the height set to 0. A null rectangle is also empty, and hence not valid.
        #[rust_name = "is_null"]
        fn isNull(self: &QRectF) -> bool;

        /// Returns true if the rectangle is valid, otherwise returns false.
        ///
        /// 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()).
        #[rust_name = "is_valid"]
        fn isValid(self: &QRectF) -> bool;

        /// Returns the x-coordinate of the rectangle's left edge. Equivalent to x().
        fn left(self: &QRectF) -> f64;

        /// Returns a rectangle grown by the margins.
        #[rust_name = "margins_added"]
        fn marginsAdded(self: &QRectF, margins: &QMarginsF) -> QRectF;

        /// Removes the margins from the rectangle, shrinking it.
        #[rust_name = "margins_removed"]
        fn marginsRemoved(self: &QRectF, margins: &QMarginsF) -> QRectF;

        /// Moves the rectangle vertically, leaving the rectangle's bottom edge at the given finite y coordinate. The rectangle's size is unchanged.
        #[rust_name = "move_bottom"]
        fn moveBottom(self: &mut QRectF, y: f64);

        /// Moves the rectangle, leaving the bottom-left corner at the given position. The rectangle's size is unchanged.
        #[rust_name = "move_bottom_left"]
        fn moveBottomLeft(self: &mut QRectF, position: &QPointF);

        /// Moves the rectangle, leaving the bottom-right corner at the given position. The rectangle's size is unchanged.
        #[rust_name = "move_bottom_right"]
        fn moveBottomRight(self: &mut QRectF, position: &QPointF);

        /// Moves the rectangle, leaving the center point at the given position. The rectangle's size is unchanged.
        #[rust_name = "move_center"]
        fn moveCenter(self: &mut QRectF, position: &QPointF);

        /// Moves the rectangle horizontally, leaving the rectangle's left edge at the given finite x coordinate. The rectangle's size is unchanged.
        #[rust_name = "move_left"]
        fn moveLeft(self: &mut QRectF, x: f64);

        /// Moves the rectangle horizontally, leaving the rectangle's right edge at the given finite x coordinate. The rectangle's size is unchanged.
        #[rust_name = "move_right"]
        fn moveRight(self: &mut QRectF, x: f64);

        /// Moves the rectangle, leaving the top-left corner at the given position.
        #[rust_name = "move_to"]
        fn moveTo(self: &mut QRectF, position: &QPointF);

        /// Moves the rectangle vertically, leaving the rectangle's top line at the given finite y coordinate. The rectangle's size is unchanged.
        #[rust_name = "move_top"]
        fn moveTop(self: &mut QRectF, y: f64);

        /// Moves the rectangle, leaving the top-left corner at the given position. The rectangle's size is unchanged.
        #[rust_name = "move_top_left"]
        fn moveTopLeft(self: &mut QRectF, position: &QPointF);

        /// Moves the rectangle, leaving the top-right corner at the given position. The rectangle's size is unchanged.
        #[rust_name = "move_top_right"]
        fn moveTopRight(self: &mut QRectF, position: &QPointF);

        /// Returns a normalized rectangle; i.e., a rectangle that has a non-negative width and height.
        fn normalized(self: &QRectF) -> QRectF;

        /// Returns the x-coordinate of the rectangle's right edge.
        fn right(self: &QRectF) -> f64;

        /// Sets the bottom edge of the rectangle to the given finite y coordinate.
        /// May change the height, but will never change the top edge of the rectangle.
        #[rust_name = "set_bottom"]
        fn setBottom(self: &mut QRectF, y: f64);

        /// Set the bottom-left corner of the rectangle to the given position.
        /// May change the size, but will never change the top-right corner of the rectangle.
        #[rust_name = "set_bottom_left"]
        fn setBottomLeft(self: &mut QRectF, position: &QPointF);

        /// Set the bottom-right corner of the rectangle to the given position.
        /// May change the size, but will never change the top-left corner of the rectangle.
        #[rust_name = "set_bottom_right"]
        fn setBottomRight(self: &mut QRectF, position: &QPointF);

        /// Sets the coordinates of the rectangle's top-left corner to (x1, y1),
        /// and the coordinates of its bottom-right corner to (x2, y2). All parameters must be finite.
        #[rust_name = "set_coords"]
        fn setCoords(self: &mut QRectF, x1: f64, y1: f64, x2: f64, y2: f64);

        /// Sets the height of the rectangle to the given finite height. The bottom edge is changed, but not the top one.
        #[rust_name = "set_height"]
        fn setHeight(self: &mut QRectF, h: f64);

        /// Sets the left edge of the rectangle to the given finite x coordinate.
        /// May change the width, but will never change the right edge of the rectangle.
        #[rust_name = "set_left"]
        fn setLeft(self: &mut QRectF, x: f64);

        /// 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.
        #[rust_name = "set_rect"]
        fn setRect(self: &mut QRectF, x: f64, y: f64, width: f64, height: f64);

        /// Sets the right edge of the rectangle to the given finite x coordinate.
        /// May change the width, but will never change the left edge of the rectangle.
        #[rust_name = "set_right"]
        fn setRight(self: &mut QRectF, x: f64);

        /// Sets the size of the rectangle to the given finite size. The top-left corner is not moved.
        #[rust_name = "set_size"]
        fn setSize(self: &mut QRectF, size: &QSizeF);

        /// Sets the top edge of the rectangle to the given finite y coordinate.
        /// May change the height, but will never change the bottom edge of the rectangle.
        #[rust_name = "set_top"]
        fn setTop(self: &mut QRectF, y: f64);

        /// 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.
        #[rust_name = "set_top_left"]
        fn setTopLeft(self: &mut QRectF, position: &QPointF);

        /// 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.
        #[rust_name = "set_top_right"]
        fn setTopRight(self: &mut QRectF, position: &QPointF);

        /// Sets the width of the rectangle to the given finite width. The right edge is changed, but not the left one.
        #[rust_name = "set_width"]
        fn setWidth(self: &mut QRectF, w: f64);

        /// Sets the left edge of the rectangle to the given finite x coordinate.
        /// May change the width, but will never change the right edge of the rectangle.
        #[rust_name = "set_x"]
        fn setX(self: &mut QRectF, x: f64);

        /// Sets the top edge of the rectangle to the given finite y coordinate.
        /// May change the height, but will never change the bottom edge of the rectangle.
        #[rust_name = "set_y"]
        fn setY(self: &mut QRectF, y: f64);

        /// Returns the size of the rectangle.
        fn size(self: &QRectF) -> QSizeF;

        /// Returns a QRect based on the values of this rectangle that is the smallest possible integer rectangle that completely contains this rectangle.
        #[rust_name = "to_aligned_rect"]
        fn toAlignedRect(self: &QRectF) -> QRect;

        /// Returns a QRect based on the values of this rectangle.
        /// Note that the coordinates in the returned rectangle are rounded to the nearest integer.
        #[rust_name = "to_rect"]
        fn toRect(self: &QRectF) -> QRect;

        /// Returns the y-coordinate of the rectangle's top edge. Equivalent to y().
        fn top(self: &QRectF) -> f64;

        /// Returns the position of the rectangle's top-left corner.
        #[rust_name = "top_left"]
        fn topLeft(self: &QRectF) -> QPointF;

        /// Returns the position of the rectangle's top-right corner.
        #[rust_name = "top_right"]
        fn topRight(self: &QRectF) -> QPointF;

        /// Moves the rectangle offset.x() along the x axis and offset.y() along the y axis, relative to the current position.
        fn translate(self: &mut QRectF, offset: &QPointF);

        /// 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.
        fn translated(self: &QRectF, offset: &QPointF) -> QRectF;

        /// Returns a copy of the rectangle that has its width and height exchanged.
        fn transposed(self: &QRectF) -> QRectF;

        /// Returns the bounding rectangle of this rectangle and the given rectangle.
        fn united(self: &QRectF, rectangle: &QRectF) -> QRectF;

        /// Returns the width of the rectangle.
        fn width(self: &QRectF) -> f64;

        /// Returns the x-coordinate of the rectangle's left edge.
        fn x(self: &QRectF) -> f64;

        /// Returns the y-coordinate of the rectangle's top edge.
        fn y(self: &QRectF) -> f64;
    }

    #[namespace = "rust::cxxqtlib1"]
    unsafe extern "C++" {
        include!("cxx-qt-lib/common.h");

        #[doc(hidden)]
        #[rust_name = "qrectf_init_default"]
        fn construct() -> QRectF;
        #[doc(hidden)]
        #[rust_name = "qrectf_init"]
        fn construct(x: f64, y: f64, width: f64, height: f64) -> QRectF;
        #[doc(hidden)]
        #[rust_name = "qrectf_from_qrect"]
        fn construct(rectangle: &QRect) -> QRectF;
        #[doc(hidden)]
        #[rust_name = "qrectf_to_qstring"]
        fn toQString(value: &QRectF) -> QString;
        #[doc(hidden)]
        #[rust_name = "qrectf_plus"]
        fn operatorPlus(a: &QRectF, b: &QMarginsF) -> QRectF;
        #[doc(hidden)]
        #[rust_name = "qrectf_minus"]
        fn operatorMinus(a: &QRectF, b: &QMarginsF) -> QRectF;
    }
}

/// The QRectF struct defines a rectangle in the plane using floating point precision.
#[derive(Debug, Clone, PartialEq)]
#[repr(C)]
pub struct QRectF {
    xp: f64,
    yp: f64,
    w: f64,
    h: f64,
}

impl QRectF {
    /// Constructs a rectangle with (x, y) as its top-left corner and the given width and height.
    pub fn new(x: f64, y: f64, width: f64, height: f64) -> Self {
        ffi::qrectf_init(x, y, width, height)
    }
}

impl Default for QRectF {
    /// Constructs a null rectangle.
    fn default() -> Self {
        ffi::qrectf_init_default()
    }
}

impl fmt::Display for QRectF {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", ffi::qrectf_to_qstring(self))
    }
}

impl From<&ffi::QRect> for QRectF {
    /// Constructs a QRectF rectangle from the given QRect rectangle.
    fn from(rectangle: &ffi::QRect) -> Self {
        ffi::qrectf_from_qrect(rectangle)
    }
}

impl From<&QRectF> for ffi::QRect {
    /// Returns a QRect based on the values of this rectangle.
    /// Note that the coordinates in the returned rectangle are rounded to the nearest integer.
    fn from(value: &QRectF) -> Self {
        value.to_rect()
    }
}

impl std::ops::Add<ffi::QMarginsF> for QRectF {
    type Output = Self;
    fn add(self, other: ffi::QMarginsF) -> Self {
        ffi::qrectf_plus(&self, &other)
    }
}

impl std::ops::Sub<ffi::QMarginsF> for QRectF {
    type Output = Self;
    fn sub(self, other: ffi::QMarginsF) -> Self {
        ffi::qrectf_minus(&self, &other)
    }
}

// Safety:
//
// Static checks on the C++ side ensure that QRectF is trivial.
unsafe impl ExternType for QRectF {
    type Id = type_id!("QRectF");
    type Kind = cxx::kind::Trivial;
}