cxx_qt_lib/core/
qline.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::QPoint;
7use cxx::{type_id, ExternType};
8use std::fmt;
9
10#[cxx::bridge]
11mod ffi {
12    unsafe extern "C++" {
13        include!("cxx-qt-lib/qline.h");
14        type QLine = super::QLine;
15        include!("cxx-qt-lib/qpoint.h");
16        type QPoint = crate::QPoint;
17        include!("cxx-qt-lib/qstring.h");
18        type QString = crate::QString;
19        include!("cxx-qt-lib/qlinef.h");
20        #[allow(dead_code)]
21        type QLineF = crate::QLineF;
22
23        /// Returns the line's start point.
24        fn p1(self: &QLine) -> QPoint;
25
26        /// Returns the line's end point.
27        fn p2(self: &QLine) -> QPoint;
28
29        /// Returns the x-coordinate of the line's start point.
30        fn x1(self: &QLine) -> i32;
31
32        /// Returns the x-coordinate of the line's end point.
33        fn x2(self: &QLine) -> i32;
34
35        /// Returns the y-coordinate of the line's start point.
36        fn y1(self: &QLine) -> i32;
37
38        /// Returns the y-coordinate of the line's end point.
39        fn y2(self: &QLine) -> i32;
40
41        /// Returns the center point of this line. This is equivalent to (p1() + p2()) / 2, except it will never overflow.
42        fn center(self: &QLine) -> QPoint;
43
44        /// Returns the horizontal component of the line's vector.
45        fn dx(self: &QLine) -> i32;
46
47        /// Returns the vertical component of the line's vector.
48        fn dy(self: &QLine) -> i32;
49
50        /// Returns true if the line does not have distinct start and end points; otherwise returns false.
51        #[rust_name = "is_null"]
52        fn isNull(self: &QLine) -> bool;
53
54        /// Sets the starting point of this line to p1.
55        #[rust_name = "set_p1"]
56        fn setP1(self: &mut QLine, p1: &QPoint);
57
58        /// Sets the end point of this line to p2.
59        #[rust_name = "set_p2"]
60        fn setP2(self: &mut QLine, p1: &QPoint);
61
62        /// Sets this line to the start in x1, y1 and end in x2, y2.
63        #[rust_name = "set_line"]
64        fn setLine(self: &mut QLine, x1: i32, y1: i32, x2: i32, y2: i32);
65
66        /// Sets the start point of this line to p1 and the end point of this line to p2.
67        #[rust_name = "set_points"]
68        fn setPoints(self: &mut QLine, p1: &QPoint, p2: &QPoint);
69
70        /// Returns this line as a line with floating point accuracy.
71        /// since Qt 6.4.
72        #[cfg(any(cxxqt_qt_version_at_least_7, cxxqt_qt_version_at_least_6_4))]
73        #[rust_name = "to_linef"]
74        fn toLineF(self: &QLine) -> QLineF;
75
76        /// Translates this line by the given offset.
77        fn translate(self: &mut QLine, offset: &QPoint);
78
79        /// Returns this line translated by the given offset.
80        fn translated(self: &QLine, offset: &QPoint) -> QLine;
81    }
82
83    #[namespace = "rust::cxxqtlib1"]
84    unsafe extern "C++" {
85        include!("cxx-qt-lib/common.h");
86
87        #[doc(hidden)]
88        #[rust_name = "qline_default"]
89        fn construct() -> QLine;
90
91        #[doc(hidden)]
92        #[rust_name = "qline_new"]
93        fn construct(pt1: QPoint, pt2: QPoint) -> QLine;
94
95        #[doc(hidden)]
96        #[rust_name = "qline_to_qstring"]
97        fn toQString(value: &QLine) -> QString;
98    }
99}
100
101/// The QLine class provides a two-dimensional vector using integer precision
102#[derive(Debug, Clone, PartialEq, Eq)]
103#[repr(C)]
104pub struct QLine {
105    pt1: QPoint,
106    pt2: QPoint,
107}
108
109impl QLine {
110    /// Constructs a line object that represents the line between p1 and p2.
111    pub fn new(pt1: QPoint, pt2: QPoint) -> Self {
112        ffi::qline_new(pt1, pt2)
113    }
114}
115
116impl Default for QLine {
117    /// Constructs a null line.
118    fn default() -> Self {
119        ffi::qline_default()
120    }
121}
122
123impl fmt::Display for QLine {
124    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125        write!(f, "{}", ffi::qline_to_qstring(self))
126    }
127}
128
129// Safety:
130//
131// Static checks on the C++ side ensure that QLine is trivial.
132unsafe impl ExternType for QLine {
133    type Id = type_id!("QLine");
134    type Kind = cxx::kind::Trivial;
135}