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
// SPDX-FileCopyrightText: 2022 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 {
    #[namespace = "Qt"]
    unsafe extern "C++" {
        include!("cxx-qt-lib/qt.h");
        type AspectRatioMode = crate::AspectRatioMode;
    }

    unsafe extern "C++" {
        include!("cxx-qt-lib/qmargins.h");
        type QMargins = crate::QMargins;
        include!("cxx-qt-lib/qsize.h");
        type QSize = super::QSize;
        include!("cxx-qt-lib/qstring.h");
        type QString = crate::QString;

        /// Returns a size holding the minimum width and height of this size and the given otherSize.
        #[rust_name = "bounded_to"]
        fn boundedTo(self: &QSize, other_size: &QSize) -> QSize;

        /// Returns a size holding the maximum width and height of this size and the given otherSize.
        #[rust_name = "expanded_to"]
        fn expandedTo(self: &QSize, other_size: &QSize) -> QSize;

        /// Returns the height.
        fn height(self: &QSize) -> i32;

        /// Returns true if either of the width and height is less than or equal to 0; otherwise returns false.
        #[rust_name = "is_empty"]
        fn isEmpty(self: &QSize) -> bool;

        /// Returns true if both the width and height is 0; otherwise returns false.
        #[rust_name = "is_null"]
        fn isNull(self: &QSize) -> bool;

        /// Returns true if both the width and height is equal to or greater than 0; otherwise returns false.
        #[rust_name = "is_valid"]
        fn isValid(self: &QSize) -> bool;

        /// Returns the size that results from growing this size by margins.
        #[rust_name = "grown_by"]
        fn grownBy(self: &QSize, margins: QMargins) -> QSize;

        /// Scales the size to a rectangle with the given size, according to the specified mode.
        fn scale(self: &mut QSize, size: &QSize, mode: AspectRatioMode);

        /// Return a size scaled to a rectangle with the given size s, according to the specified mode.
        fn scaled(self: &QSize, s: &QSize, mode: AspectRatioMode) -> QSize;

        /// Sets the height to the given height.
        #[rust_name = "set_height"]
        fn setHeight(self: &mut QSize, height: i32);

        /// Sets the width to the given width.
        #[rust_name = "set_width"]
        fn setWidth(self: &mut QSize, width: i32);

        /// Returns the size that results from shrinking this size by margins.
        #[rust_name = "shrunk_by"]
        fn shrunkBy(self: &QSize, margins: QMargins) -> QSize;

        /// Swaps the width and height values.
        fn transpose(self: &mut QSize);

        /// Returns a QSize with width and height swapped.
        fn transposed(self: &QSize) -> QSize;

        /// Returns the width.
        fn width(self: &QSize) -> i32;
    }

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

        #[doc(hidden)]
        #[rust_name = "qsize_init_default"]
        fn construct() -> QSize;
        #[doc(hidden)]
        #[rust_name = "qsize_init"]
        fn construct(w: i32, h: i32) -> QSize;
        #[doc(hidden)]
        #[rust_name = "qsize_to_qstring"]
        fn toQString(value: &QSize) -> QString;
        #[doc(hidden)]
        #[rust_name = "qsize_plus"]
        fn operatorPlus(a: &QSize, b: &QSize) -> QSize;
        #[doc(hidden)]
        #[rust_name = "qsize_minus"]
        fn operatorMinus(a: &QSize, b: &QSize) -> QSize;
        #[doc(hidden)]
        #[rust_name = "qsize_mul"]
        fn operatorMul(a: f64, b: &QSize) -> QSize;
        #[doc(hidden)]
        #[rust_name = "qsize_div"]
        fn operatorDiv(a: f64, b: &QSize) -> QSize;
    }
}

/// The QSize struct defines the size of a two-dimensional object using integer point precision.
#[derive(Debug, Clone, PartialEq, Eq)]
#[repr(C)]
pub struct QSize {
    width: i32,
    height: i32,
}

impl QSize {
    /// Constructs a size with the given width and height.
    pub fn new(width: i32, height: i32) -> Self {
        ffi::qsize_init(width, height)
    }
}

impl Default for QSize {
    /// Constructs a size with an invalid width and height
    fn default() -> Self {
        ffi::qsize_init_default()
    }
}

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

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

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

impl std::ops::Mul<f64> for QSize {
    type Output = Self;
    fn mul(self, rhs: f64) -> Self {
        ffi::qsize_mul(rhs, &self)
    }
}

impl std::ops::Div<f64> for QSize {
    type Output = Self;
    fn div(self, rhs: f64) -> Self {
        ffi::qsize_div(rhs, &self)
    }
}

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