cxx_qt_lib/core/
qsize.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    #[namespace = "Qt"]
12    unsafe extern "C++" {
13        include!("cxx-qt-lib/qt.h");
14        type AspectRatioMode = crate::AspectRatioMode;
15    }
16
17    unsafe extern "C++" {
18        include!("cxx-qt-lib/qmargins.h");
19        type QMargins = crate::QMargins;
20        include!("cxx-qt-lib/qsize.h");
21        type QSize = super::QSize;
22        include!("cxx-qt-lib/qstring.h");
23        type QString = crate::QString;
24        include!("cxx-qt-lib/qsizef.h");
25        #[allow(dead_code)]
26        type QSizeF = crate::QSizeF;
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: &QSize, other_size: &QSize) -> QSize;
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: &QSize, other_size: &QSize) -> QSize;
35
36        /// Returns the height.
37        fn height(self: &QSize) -> i32;
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: &QSize) -> bool;
42
43        /// Returns true if both the width and height is 0; otherwise returns false.
44        #[rust_name = "is_null"]
45        fn isNull(self: &QSize) -> bool;
46
47        /// Returns true if both the width and height is equal to or greater than 0; otherwise returns false.
48        #[rust_name = "is_valid"]
49        fn isValid(self: &QSize) -> bool;
50
51        /// Returns the size that results from growing this size by margins.
52        #[rust_name = "grown_by"]
53        fn grownBy(self: &QSize, margins: QMargins) -> QSize;
54
55        /// Scales the size to a rectangle with the given size, according to the specified mode.
56        fn scale(self: &mut QSize, size: &QSize, mode: AspectRatioMode);
57
58        /// Return a size scaled to a rectangle with the given size s, according to the specified mode.
59        fn scaled(self: &QSize, s: &QSize, mode: AspectRatioMode) -> QSize;
60
61        /// Sets the height to the given height.
62        #[rust_name = "set_height"]
63        fn setHeight(self: &mut QSize, height: i32);
64
65        /// Sets the width to the given width.
66        #[rust_name = "set_width"]
67        fn setWidth(self: &mut QSize, width: i32);
68
69        /// Returns the size that results from shrinking this size by margins.
70        #[rust_name = "shrunk_by"]
71        fn shrunkBy(self: &QSize, margins: QMargins) -> QSize;
72
73        /// Returns this size as a size with floating point accuracy.
74        /// since 6.4
75        #[cfg(any(cxxqt_qt_version_at_least_7, cxxqt_qt_version_at_least_6_4))]
76        #[rust_name = "to_sizef"]
77        fn toSizeF(self: &QSize) -> QSizeF;
78
79        /// Swaps the width and height values.
80        fn transpose(self: &mut QSize);
81
82        /// Returns a QSize with width and height swapped.
83        fn transposed(self: &QSize) -> QSize;
84
85        /// Returns the width.
86        fn width(self: &QSize) -> i32;
87    }
88
89    #[namespace = "rust::cxxqtlib1"]
90    unsafe extern "C++" {
91        include!("cxx-qt-lib/common.h");
92
93        #[doc(hidden)]
94        #[rust_name = "qsize_init_default"]
95        fn construct() -> QSize;
96        #[doc(hidden)]
97        #[rust_name = "qsize_init"]
98        fn construct(w: i32, h: i32) -> QSize;
99        #[doc(hidden)]
100        #[rust_name = "qsize_to_qstring"]
101        fn toQString(value: &QSize) -> QString;
102        #[doc(hidden)]
103        #[rust_name = "qsize_plus"]
104        fn operatorPlus(a: &QSize, b: &QSize) -> QSize;
105        #[doc(hidden)]
106        #[rust_name = "qsize_minus"]
107        fn operatorMinus(a: &QSize, b: &QSize) -> QSize;
108        #[doc(hidden)]
109        #[rust_name = "qsize_mul"]
110        fn operatorMul(a: f64, b: &QSize) -> QSize;
111        #[doc(hidden)]
112        #[rust_name = "qsize_div"]
113        fn operatorDiv(a: f64, b: &QSize) -> QSize;
114    }
115}
116
117/// The QSize struct defines the size of a two-dimensional object using integer point precision.
118#[derive(Debug, Clone, PartialEq, Eq)]
119#[repr(C)]
120pub struct QSize {
121    width: i32,
122    height: i32,
123}
124
125impl QSize {
126    /// Constructs a size with the given width and height.
127    pub fn new(width: i32, height: i32) -> Self {
128        ffi::qsize_init(width, height)
129    }
130}
131
132impl Default for QSize {
133    /// Constructs a size with an invalid width and height
134    fn default() -> Self {
135        ffi::qsize_init_default()
136    }
137}
138
139impl fmt::Display for QSize {
140    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141        write!(f, "{}", ffi::qsize_to_qstring(self))
142    }
143}
144
145impl std::ops::Add for QSize {
146    type Output = Self;
147    fn add(self, other: Self) -> Self {
148        ffi::qsize_plus(&self, &other)
149    }
150}
151
152impl std::ops::Sub for QSize {
153    type Output = Self;
154    fn sub(self, other: Self) -> Self {
155        ffi::qsize_minus(&self, &other)
156    }
157}
158
159impl std::ops::Mul<f64> for QSize {
160    type Output = Self;
161    fn mul(self, rhs: f64) -> Self {
162        ffi::qsize_mul(rhs, &self)
163    }
164}
165
166impl std::ops::Div<f64> for QSize {
167    type Output = Self;
168    fn div(self, rhs: f64) -> Self {
169        ffi::qsize_div(rhs, &self)
170    }
171}
172
173// Safety:
174//
175// Static checks on the C++ side ensure that QSize is trivial.
176unsafe impl ExternType for QSize {
177    type Id = type_id!("QSize");
178    type Kind = cxx::kind::Trivial;
179}