cxx_qt_lib/core/
qlinef.rs

1// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
2// SPDX-FileContributor: Laurent Montel <laurent.montel@kdab.com>
3//
4// SPDX-License-Identifier: MIT OR Apache-2.0
5
6use crate::QLine;
7use crate::QPointF;
8use cxx::{type_id, ExternType};
9use std::fmt;
10
11#[cxx::bridge]
12mod ffi {
13    unsafe extern "C++" {
14        include!("cxx-qt-lib/qlinef.h");
15        type QLineF = super::QLineF;
16        include!("cxx-qt-lib/qline.h");
17        type QLine = crate::QLine;
18        include!("cxx-qt-lib/qpointf.h");
19        type QPointF = crate::QPointF;
20        include!("cxx-qt-lib/qstring.h");
21        type QString = crate::QString;
22
23        /// Returns the angle of the line in degrees.
24        fn angle(self: &QLineF) -> f64;
25
26        /// Returns the angle (in degrees) from this line to the given line, taking the direction of the lines into account.
27        /// If the lines do not intersect within their range, it is the intersection point of the extended lines that serves as origin (see QLineF::UnboundedIntersection).
28        #[rust_name = "angle_to"]
29        fn angleTo(self: &QLineF, line: &QLineF) -> f64;
30
31        /// Returns the line's start point.
32        fn p1(self: &QLineF) -> QPointF;
33
34        /// Returns the line's end point.
35        fn p2(self: &QLineF) -> QPointF;
36
37        /// Returns the x-coordinate of the line's start point.
38        fn x1(self: &QLineF) -> f64;
39
40        /// Returns the x-coordinate of the line's end point.
41        fn x2(self: &QLineF) -> f64;
42
43        /// Returns the y-coordinate of the line's start point.
44        fn y1(self: &QLineF) -> f64;
45
46        /// Returns the y-coordinate of the line's end point.
47        fn y2(self: &QLineF) -> f64;
48
49        /// Returns the center point of this line. This is equivalent to (p1() + p2()) / 2, except it will never overflow.
50        fn center(self: &QLineF) -> QPointF;
51
52        /// Returns the horizontal component of the line's vector.
53        fn dx(self: &QLineF) -> f64;
54
55        /// Returns the vertical component of the line's vector.
56        fn dy(self: &QLineF) -> f64;
57
58        /// Returns true if the line does not have distinct start and end points; otherwise returns false.
59        #[rust_name = "is_null"]
60        fn isNull(self: &QLineF) -> bool;
61
62        /// Returns the length of the line.
63        fn length(self: &QLineF) -> f64;
64
65        /// Returns a line that is perpendicular to this line with the same starting point and length.
66        #[rust_name = "normal_vector"]
67        fn normalVector(self: &QLineF) -> QLineF;
68
69        /// Returns the point at the parameterized position specified by t. The function returns the line's start point if t = 0, and its end point if t = 1.
70        #[rust_name = "point_at"]
71        fn pointAt(self: &QLineF, t: f64) -> QPointF;
72
73        /// Sets the angle of the line to the given angle (in degrees). This will change the position of the second point of the line such that the line has the given angle.
74        #[rust_name = "set_angle"]
75        fn setAngle(self: &mut QLineF, angle: f64);
76
77        /// Sets the length of the line to the given length. QLineF will move the end point - p2() - of the line to give the line its new length, unless length() was previously zero, i
78        /// in which case no scaling is attempted. For lines with very short lengths (represented by denormal floating-point values), results may be imprecise.
79        #[rust_name = "set_length"]
80        fn setLength(self: &mut QLineF, length: f64);
81
82        /// Sets the starting point of this line to p1.
83        #[rust_name = "set_p1"]
84        fn setP1(self: &mut QLineF, p1: &QPointF);
85
86        /// Sets the end point of this line to p2.
87        #[rust_name = "set_p2"]
88        fn setP2(self: &mut QLineF, p1: &QPointF);
89
90        /// Sets this line to the start in x1, y1 and end in x2, y2.
91        #[rust_name = "set_line"]
92        fn setLine(self: &mut QLineF, x1: f64, y1: f64, x2: f64, y2: f64);
93
94        /// Sets the start point of this line to p1 and the end point of this line to p2.
95        #[rust_name = "set_points"]
96        fn setPoints(self: &mut QLineF, p1: &QPointF, p2: &QPointF);
97
98        /// Returns an integer based copy of this line.
99        #[rust_name = "to_line"]
100        fn toLine(self: &QLineF) -> QLine;
101
102        /// Translates this line by the given offset.
103        fn translate(self: &mut QLineF, offset: &QPointF);
104
105        /// Returns this line translated by the given offset.
106        fn translated(self: &QLineF, offset: &QPointF) -> QLineF;
107
108        /// Returns the unit vector for this line, i.e a line starting at the same point as this line with a length of 1.0, provided the line is non-null.
109        #[rust_name = "unit_vector"]
110        fn unitVector(self: &QLineF) -> QLineF;
111    }
112
113    #[namespace = "rust::cxxqtlib1"]
114    unsafe extern "C++" {
115        include!("cxx-qt-lib/common.h");
116
117        #[doc(hidden)]
118        #[rust_name = "qlinef_default"]
119        fn construct() -> QLineF;
120
121        #[doc(hidden)]
122        #[rust_name = "qlinef_new"]
123        fn construct(pt1: QPointF, pt2: QPointF) -> QLineF;
124        #[doc(hidden)]
125        #[rust_name = "qlinef_from_qline"]
126        fn construct(line: &QLine) -> QLineF;
127
128        #[doc(hidden)]
129        #[rust_name = "qlinef_to_qstring"]
130        fn toQString(value: &QLineF) -> QString;
131    }
132}
133
134/// The QLineF class provides a two-dimensional vector using floating point precision.
135#[derive(Debug, Clone, PartialEq)]
136#[repr(C)]
137pub struct QLineF {
138    pt1: QPointF,
139    pt2: QPointF,
140}
141
142impl QLineF {
143    /// Constructs a line object that represents the line between pt1 and pt2.
144    pub fn new(pt1: QPointF, pt2: QPointF) -> Self {
145        ffi::qlinef_new(pt1, pt2)
146    }
147}
148
149impl Default for QLineF {
150    /// Constructs a default qlinef
151    fn default() -> Self {
152        ffi::qlinef_default()
153    }
154}
155
156impl From<&ffi::QLine> for QLineF {
157    /// Construct a QLineF object from the given integer-based line.
158    fn from(line: &QLine) -> Self {
159        ffi::qlinef_from_qline(line)
160    }
161}
162
163impl From<QLineF> for ffi::QLine {
164    /// Returns an integer-based copy of this line.
165    /// Note that the returned line's start and end points are rounded to the nearest integer.
166    fn from(value: QLineF) -> Self {
167        value.to_line()
168    }
169}
170
171impl fmt::Display for QLineF {
172    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
173        write!(f, "{}", ffi::qlinef_to_qstring(self))
174    }
175}
176
177// Safety:
178//
179// Static checks on the C++ side ensure that QLineF is trivial.
180unsafe impl ExternType for QLineF {
181    type Id = type_id!("QLineF");
182    type Kind = cxx::kind::Trivial;
183}