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