cxx_qt_lib/core/
qpointf.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    unsafe extern "C++" {
13        include!("cxx-qt-lib/qpoint.h");
14        type QPoint = crate::QPoint;
15        include!("cxx-qt-lib/qpointf.h");
16        type QPointF = super::QPointF;
17        include!("cxx-qt-lib/qstring.h");
18        type QString = crate::QString;
19
20        /// Returns true if both the x and y coordinates are set to 0.0 (ignoring the sign); otherwise returns false.
21        #[rust_name = "is_null"]
22        fn isNull(self: &QPointF) -> bool;
23
24        /// Returns the sum of the absolute values of x() and y(),
25        /// traditionally known as the "Manhattan length" of the vector from the origin to the point.
26        #[rust_name = "manhattan_length"]
27        fn manhattanLength(self: &QPointF) -> f64;
28
29        /// Sets the x coordinate of this point to the given finite x coordinate.
30        #[rust_name = "set_x"]
31        fn setX(self: &mut QPointF, x: f64);
32
33        /// Sets the y coordinate of this point to the given finite y coordinate.
34        #[rust_name = "set_y"]
35        fn setY(self: &mut QPointF, y: f64);
36
37        /// Rounds the coordinates of this point to the nearest integer,
38        /// and returns a QPoint object with the rounded coordinates.
39        #[rust_name = "to_point"]
40        fn toPoint(self: &QPointF) -> QPoint;
41
42        /// Returns a point with x and y coordinates exchanged
43        fn transposed(self: &QPointF) -> QPointF;
44
45        /// Returns the x coordinate of this point.
46        fn x(self: &QPointF) -> f64;
47
48        /// Returns the y coordinate of this point.
49        fn y(self: &QPointF) -> f64;
50    }
51
52    #[namespace = "rust::cxxqtlib1"]
53    unsafe extern "C++" {
54        #[doc(hidden)]
55        #[rust_name = "qpointf_dot_product"]
56        fn qpointfDotProduct(p1: &QPointF, p2: &QPointF) -> f64;
57    }
58
59    #[namespace = "rust::cxxqtlib1"]
60    unsafe extern "C++" {
61        include!("cxx-qt-lib/common.h");
62
63        #[doc(hidden)]
64        #[rust_name = "qpointf_init_default"]
65        fn construct() -> QPointF;
66        #[doc(hidden)]
67        #[rust_name = "qpointf_init"]
68        fn construct(x: f64, y: f64) -> QPointF;
69        #[doc(hidden)]
70        #[rust_name = "qpointf_from_qpoint"]
71        fn construct(point: &QPoint) -> QPointF;
72        #[doc(hidden)]
73        #[rust_name = "qpointf_to_qstring"]
74        fn toQString(value: &QPointF) -> QString;
75        #[doc(hidden)]
76        #[rust_name = "qpointf_plus"]
77        fn operatorPlus(a: &QPointF, b: &QPointF) -> QPointF;
78        #[doc(hidden)]
79        #[rust_name = "qpointf_minus"]
80        fn operatorMinus(a: &QPointF, b: &QPointF) -> QPointF;
81        #[doc(hidden)]
82        #[rust_name = "qpointf_mul"]
83        fn operatorMul(a: f64, b: &QPointF) -> QPointF;
84        #[doc(hidden)]
85        #[rust_name = "qpointf_div"]
86        fn operatorDiv(a: f64, b: &QPointF) -> QPointF;
87    }
88}
89
90/// The QPointF struct defines a point in the plane using floating point precision.
91#[derive(Debug, Clone, PartialEq)]
92#[repr(C)]
93pub struct QPointF {
94    x: f64,
95    y: f64,
96}
97
98impl QPointF {
99    /// Returns the dot product of p1 and p2.
100    pub fn dot_product(p1: &QPointF, p2: &QPointF) -> f64 {
101        ffi::qpointf_dot_product(p1, p2)
102    }
103
104    /// Constructs a point with the given coordinates (x, y).
105    pub fn new(x: f64, y: f64) -> Self {
106        ffi::qpointf_init(x, y)
107    }
108}
109
110impl Default for QPointF {
111    /// Constructs a null point, i.e. with coordinates (0.0, 0.0)
112    fn default() -> Self {
113        ffi::qpointf_init_default()
114    }
115}
116
117impl fmt::Display for QPointF {
118    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
119        write!(f, "{}", ffi::qpointf_to_qstring(self))
120    }
121}
122
123impl From<&ffi::QPoint> for QPointF {
124    /// Constructs a copy of the given point.
125    fn from(point: &ffi::QPoint) -> Self {
126        ffi::qpointf_from_qpoint(point)
127    }
128}
129
130impl From<QPointF> for ffi::QPoint {
131    /// Rounds the coordinates of this point to the nearest integer,
132    /// and returns a QPoint object with the rounded coordinates.
133    fn from(value: QPointF) -> Self {
134        value.to_point()
135    }
136}
137
138impl std::ops::Add for QPointF {
139    type Output = Self;
140    fn add(self, other: Self) -> Self {
141        ffi::qpointf_plus(&self, &other)
142    }
143}
144
145impl std::ops::Sub for QPointF {
146    type Output = Self;
147    fn sub(self, other: Self) -> Self {
148        ffi::qpointf_minus(&self, &other)
149    }
150}
151
152impl std::ops::Mul<f64> for QPointF {
153    type Output = Self;
154    fn mul(self, rhs: f64) -> Self {
155        ffi::qpointf_mul(rhs, &self)
156    }
157}
158
159impl std::ops::Div<f64> for QPointF {
160    type Output = Self;
161    fn div(self, rhs: f64) -> Self {
162        ffi::qpointf_div(rhs, &self)
163    }
164}
165
166// Safety:
167//
168// Static checks on the C++ side ensure that QPointF is trivial.
169unsafe impl ExternType for QPointF {
170    type Id = type_id!("QPointF");
171    type Kind = cxx::kind::Trivial;
172}