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
// 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/qpoint.h");
        include!("cxx-qt-lib/qstring.h");

        type QPoint = super::QPoint;
        type QString = crate::QString;

        /// Returns true if both the x and y coordinates are set to 0, otherwise returns false.
        #[rust_name = "is_null"]
        fn isNull(self: &QPoint) -> bool;

        /// Returns the sum of the absolute values of x() and y(),
        /// traditionally known as the "Manhattan length" of the vector from the origin to the point.
        #[rust_name = "manhattan_length"]
        fn manhattanLength(self: &QPoint) -> i32;

        /// Sets the x coordinate of this point to the given x coordinate.
        #[rust_name = "set_x"]
        fn setX(self: &mut QPoint, x: i32);

        /// Sets the y coordinate of this point to the given y coordinate.
        #[rust_name = "set_y"]
        fn setY(self: &mut QPoint, y: i32);

        /// Returns a point with x and y coordinates exchanged
        fn transposed(self: &QPoint) -> QPoint;

        /// Returns the x coordinate of this point.
        fn x(self: &QPoint) -> i32;

        /// Returns the y coordinate of this point.
        fn y(self: &QPoint) -> i32;
    }

    #[namespace = "rust::cxxqtlib1"]
    unsafe extern "C++" {
        #[doc(hidden)]
        #[rust_name = "qpoint_dot_product"]
        fn qpointDotProduct(p1: &QPoint, p2: &QPoint) -> i32;
    }

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

        #[doc(hidden)]
        #[rust_name = "qpoint_init_default"]
        fn construct() -> QPoint;
        #[doc(hidden)]
        #[rust_name = "qpoint_init"]
        fn construct(x: i32, y: i32) -> QPoint;
        #[doc(hidden)]
        #[rust_name = "qpoint_to_qstring"]
        fn toQString(value: &QPoint) -> QString;
        #[doc(hidden)]
        #[rust_name = "qpoint_plus"]
        fn operatorPlus(a: &QPoint, b: &QPoint) -> QPoint;
        #[doc(hidden)]
        #[rust_name = "qpoint_minus"]
        fn operatorMinus(a: &QPoint, b: &QPoint) -> QPoint;
        #[doc(hidden)]
        #[rust_name = "qpoint_mul_f32"]
        fn operatorMul(a: f32, b: &QPoint) -> QPoint;
        #[doc(hidden)]
        #[rust_name = "qpoint_mul_f64"]
        fn operatorMul(a: f64, b: &QPoint) -> QPoint;
        #[doc(hidden)]
        #[rust_name = "qpoint_mul_i32"]
        fn operatorMul(a: i32, b: &QPoint) -> QPoint;
        #[doc(hidden)]
        #[rust_name = "qpoint_div"]
        fn operatorDiv(a: f64, b: &QPoint) -> QPoint;
    }
}

/// The QPoint struct defines a point in the plane using integer precision.
#[derive(Debug, Clone, PartialEq, Eq)]
#[repr(C)]
pub struct QPoint {
    x: i32,
    y: i32,
}

impl QPoint {
    /// Returns the dot product of p1 and p2.
    pub fn dot_product(p1: &QPoint, p2: &QPoint) -> i32 {
        ffi::qpoint_dot_product(p1, p2)
    }

    /// Constructs a point with the given coordinates (x, y).
    pub fn new(x: i32, y: i32) -> Self {
        ffi::qpoint_init(x, y)
    }
}

impl Default for QPoint {
    /// Constructs a null point, i.e. with coordinates (0, 0)
    fn default() -> Self {
        ffi::qpoint_init_default()
    }
}

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

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

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

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

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

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

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

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