cxx_qt_lib/core/
qsizef.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// SPDX-FileContributor: Gerhard de Clercq <gerhard.declercq@kdab.com>
4//
5// SPDX-License-Identifier: MIT OR Apache-2.0
6
7use cxx::{type_id, ExternType};
8use std::fmt;
9
10#[cxx::bridge]
11mod ffi {
12    #[namespace = "Qt"]
13    unsafe extern "C++" {
14        include!("cxx-qt-lib/qt.h");
15        type AspectRatioMode = crate::AspectRatioMode;
16    }
17
18    unsafe extern "C++" {
19        include!("cxx-qt-lib/qmarginsf.h");
20        type QMarginsF = crate::QMarginsF;
21        include!("cxx-qt-lib/qsize.h");
22        type QSize = crate::QSize;
23        include!("cxx-qt-lib/qsizef.h");
24        type QSizeF = super::QSizeF;
25        include!("cxx-qt-lib/qstring.h");
26        type QString = crate::QString;
27
28        /// Returns a size holding the minimum width and height of this size and the given otherSize.
29        #[rust_name = "bounded_to"]
30        fn boundedTo(self: &QSizeF, other_size: &QSizeF) -> QSizeF;
31
32        /// Returns a size holding the maximum width and height of this size and the given otherSize.
33        #[rust_name = "expanded_to"]
34        fn expandedTo(self: &QSizeF, other_size: &QSizeF) -> QSizeF;
35
36        /// Returns the height.
37        fn height(self: &QSizeF) -> f64;
38
39        /// Returns true if either of the width and height is less than or equal to 0; otherwise returns false.
40        #[rust_name = "is_empty"]
41        fn isEmpty(self: &QSizeF) -> bool;
42
43        /// Returns true if both the width and height are 0.0 (ignoring the sign); otherwise returns false.
44        #[rust_name = "is_null"]
45        fn isNull(self: &QSizeF) -> bool;
46
47        /// Returns true if both the width and height are equal to or greater than 0; otherwise returns false.
48        #[rust_name = "is_valid"]
49        fn isValid(self: &QSizeF) -> bool;
50
51        /// Returns the size that results from growing this size by margins.
52        #[rust_name = "grown_by"]
53        fn grownBy(self: &QSizeF, margins: QMarginsF) -> QSizeF;
54
55        /// Scales the size to a rectangle with the given size, according to the specified mode.
56        fn scale(self: &mut QSizeF, size: &QSizeF, mode: AspectRatioMode);
57
58        /// Returns a size scaled to a rectangle with the given size s, according to the specified mode.
59        fn scaled(self: &QSizeF, s: &QSizeF, mode: AspectRatioMode) -> QSizeF;
60
61        /// Sets the height to the given finite height.
62        #[rust_name = "set_height"]
63        fn setHeight(self: &mut QSizeF, height: f64);
64
65        /// Sets the width to the given finite width.
66        #[rust_name = "set_width"]
67        fn setWidth(self: &mut QSizeF, width: f64);
68
69        /// Returns the size that results from shrinking this size by margins.
70        #[rust_name = "shrunk_by"]
71        fn shrunkBy(self: &QSizeF, margins: QMarginsF) -> QSizeF;
72
73        /// Returns an integer based copy of this size.
74        ///
75        /// Note that the coordinates in the returned size will be rounded to the nearest integer.
76        #[rust_name = "to_size"]
77        fn toSize(self: &QSizeF) -> QSize;
78
79        /// Swaps the width and height values.
80        fn transpose(self: &mut QSizeF);
81
82        /// Returns the size with width and height values swapped.
83        fn transposed(self: &QSizeF) -> QSizeF;
84
85        /// Returns the width.
86        fn width(self: &QSizeF) -> f64;
87    }
88
89    #[namespace = "rust::cxxqtlib1"]
90    unsafe extern "C++" {
91        include!("cxx-qt-lib/common.h");
92
93        #[doc(hidden)]
94        #[rust_name = "qsizef_init_default"]
95        fn construct() -> QSizeF;
96        #[doc(hidden)]
97        #[rust_name = "qsizef_init"]
98        fn construct(w: f64, h: f64) -> QSizeF;
99        #[doc(hidden)]
100        #[rust_name = "qsizef_from_qsize"]
101        fn construct(size: &QSize) -> QSizeF;
102        #[doc(hidden)]
103        #[rust_name = "qsizef_to_qstring"]
104        fn toQString(value: &QSizeF) -> QString;
105        #[doc(hidden)]
106        #[rust_name = "qsizef_plus"]
107        fn operatorPlus(a: &QSizeF, b: &QSizeF) -> QSizeF;
108        #[doc(hidden)]
109        #[rust_name = "qsizef_minus"]
110        fn operatorMinus(a: &QSizeF, b: &QSizeF) -> QSizeF;
111        #[doc(hidden)]
112        #[rust_name = "qsizef_mul"]
113        fn operatorMul(a: f64, b: &QSizeF) -> QSizeF;
114        #[doc(hidden)]
115        #[rust_name = "qsizef_div"]
116        fn operatorDiv(a: f64, b: &QSizeF) -> QSizeF;
117    }
118}
119
120/// The QSizeF class defines the size of a two-dimensional object using floating point precision.
121#[derive(Debug, Clone, PartialEq)]
122#[repr(C)]
123pub struct QSizeF {
124    width: f64,
125    height: f64,
126}
127
128impl QSizeF {
129    /// Constructs a size with the given width and height.
130    pub fn new(w: f64, h: f64) -> Self {
131        ffi::qsizef_init(w, h)
132    }
133}
134
135impl Default for QSizeF {
136    /// Constructs an invalid size.
137    fn default() -> Self {
138        ffi::qsizef_init_default()
139    }
140}
141
142impl fmt::Display for QSizeF {
143    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
144        write!(f, "{}", ffi::qsizef_to_qstring(self))
145    }
146}
147
148impl From<&ffi::QSize> for QSizeF {
149    /// Constructs a size with floating point accuracy from the given size.
150    fn from(size: &ffi::QSize) -> Self {
151        ffi::qsizef_from_qsize(size)
152    }
153}
154
155impl From<QSizeF> for ffi::QSize {
156    /// Returns an integer based copy of this size.
157    ///
158    /// Note that the coordinates in the returned size will be rounded to the nearest integer.
159    fn from(size: QSizeF) -> Self {
160        size.to_size()
161    }
162}
163
164impl std::ops::Add for QSizeF {
165    type Output = Self;
166    fn add(self, other: Self) -> Self {
167        ffi::qsizef_plus(&self, &other)
168    }
169}
170
171impl std::ops::Sub for QSizeF {
172    type Output = Self;
173    fn sub(self, other: Self) -> Self {
174        ffi::qsizef_minus(&self, &other)
175    }
176}
177
178impl std::ops::Mul<f64> for QSizeF {
179    type Output = Self;
180    fn mul(self, rhs: f64) -> Self {
181        ffi::qsizef_mul(rhs, &self)
182    }
183}
184
185impl std::ops::Div<f64> for QSizeF {
186    type Output = Self;
187    fn div(self, rhs: f64) -> Self {
188        ffi::qsizef_div(rhs, &self)
189    }
190}
191
192// Safety:
193//
194// Static checks on the C++ side ensure that QSizeF is trivial.
195unsafe impl ExternType for QSizeF {
196    type Id = type_id!("QSizeF");
197    type Kind = cxx::kind::Trivial;
198}