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
// SPDX-FileCopyrightText: 2021 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
// SPDX-FileContributor: Gerhard de Clercq <gerhard.declercq@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");
        type QPoint = crate::QPoint;
        include!("cxx-qt-lib/qpointf.h");
        type QPointF = super::QPointF;
        include!("cxx-qt-lib/qstring.h");
        type QString = crate::QString;

        /// Returns true if both the x and y coordinates are set to 0.0 (ignoring the sign); otherwise returns false.
        #[rust_name = "is_null"]
        fn isNull(self: &QPointF) -> 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: &QPointF) -> f64;

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

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

        /// Rounds the coordinates of this point to the nearest integer,
        /// and returns a QPoint object with the rounded coordinates.
        #[rust_name = "to_point"]
        fn toPoint(self: &QPointF) -> QPoint;

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

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

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

    #[namespace = "rust::cxxqtlib1"]
    unsafe extern "C++" {
        #[doc(hidden)]
        #[rust_name = "qpointf_dot_product"]
        fn qpointfDotProduct(p1: &QPointF, p2: &QPointF) -> f64;
    }

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

        #[doc(hidden)]
        #[rust_name = "qpointf_init_default"]
        fn construct() -> QPointF;
        #[doc(hidden)]
        #[rust_name = "qpointf_init"]
        fn construct(x: f64, y: f64) -> QPointF;
        #[doc(hidden)]
        #[rust_name = "qpointf_from_qpoint"]
        fn construct(point: &QPoint) -> QPointF;
        #[doc(hidden)]
        #[rust_name = "qpointf_to_qstring"]
        fn toQString(value: &QPointF) -> QString;
        #[doc(hidden)]
        #[rust_name = "qpointf_plus"]
        fn operatorPlus(a: &QPointF, b: &QPointF) -> QPointF;
        #[doc(hidden)]
        #[rust_name = "qpointf_minus"]
        fn operatorMinus(a: &QPointF, b: &QPointF) -> QPointF;
        #[doc(hidden)]
        #[rust_name = "qpointf_mul"]
        fn operatorMul(a: f64, b: &QPointF) -> QPointF;
        #[doc(hidden)]
        #[rust_name = "qpointf_div"]
        fn operatorDiv(a: f64, b: &QPointF) -> QPointF;
    }
}

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

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

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

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

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

impl From<&ffi::QPoint> for QPointF {
    /// Constructs a copy of the given point.
    fn from(point: &ffi::QPoint) -> Self {
        ffi::qpointf_from_qpoint(point)
    }
}

impl From<QPointF> for ffi::QPoint {
    /// Rounds the coordinates of this point to the nearest integer,
    /// and returns a QPoint object with the rounded coordinates.
    fn from(value: QPointF) -> Self {
        value.to_point()
    }
}

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

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

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

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

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