cxx_qt_lib/core/
qpersistentmodelindex.rs

1// SPDX-FileCopyrightText: 2022 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
5use cxx::{type_id, ExternType};
6use std::fmt;
7use std::mem::MaybeUninit;
8
9#[cxx::bridge]
10mod ffi {
11    unsafe extern "C++" {
12        include!("cxx-qt-lib/qpersistentmodelindex.h");
13        include!("cxx-qt-lib/qstring.h");
14
15        type QPersistentModelIndex = super::QPersistentModelIndex;
16        type QString = crate::QString;
17
18        include!("cxx-qt-lib/qmodelindex.h");
19        type QModelIndex = crate::QModelIndex;
20
21        /// Returns the column this persistent model index refers to.
22        fn column(self: &QPersistentModelIndex) -> i32;
23        /// Returns true if this persistent model index is valid; otherwise returns false.
24        ///
25        /// A valid index belongs to a model, and has non-negative row and column numbers.
26        #[rust_name = "is_valid"]
27        fn isValid(self: &QPersistentModelIndex) -> bool;
28        /// Returns the parent QModelIndex for this persistent index, or an invalid QModelIndex if it has no parent.
29        fn parent(self: &QPersistentModelIndex) -> QModelIndex;
30        /// Returns the row this persistent model index refers to.
31        fn row(self: &QPersistentModelIndex) -> i32;
32        /// Returns the sibling at row and column or an invalid QModelIndex if there is no sibling at this position.
33        fn sibling(self: &QPersistentModelIndex, row: i32, column: i32) -> QModelIndex;
34
35        /// Swaps this persistent modelindex with other. This function is very fast and never fails.
36        fn swap(self: &mut QPersistentModelIndex, other: &mut QPersistentModelIndex);
37    }
38
39    #[namespace = "rust::cxxqtlib1"]
40    unsafe extern "C++" {
41        include!("cxx-qt-lib/common.h");
42
43        #[doc(hidden)]
44        #[rust_name = "qpersistentmodelindex_drop"]
45        fn drop(string: &mut QPersistentModelIndex);
46
47        #[doc(hidden)]
48        #[rust_name = "qpersistentmodelindex_from_qmodelindex"]
49        fn construct(index: &QModelIndex) -> QPersistentModelIndex;
50        #[doc(hidden)]
51        #[rust_name = "qpersistentmodelindex_clone"]
52        fn construct(other: &QPersistentModelIndex) -> QPersistentModelIndex;
53        #[doc(hidden)]
54        #[rust_name = "qpersistentmodelindex_eq"]
55        fn operatorEq(a: &QPersistentModelIndex, b: &QPersistentModelIndex) -> bool;
56        #[doc(hidden)]
57        #[rust_name = "qpersistentmodelindex_to_qstring"]
58        fn toQString(value: &QPersistentModelIndex) -> QString;
59    }
60}
61
62/// The QPersistentModelIndex class is used to locate data in a data model.
63#[repr(C)]
64pub struct QPersistentModelIndex {
65    _space: MaybeUninit<usize>,
66}
67
68impl Clone for QPersistentModelIndex {
69    /// Creates a new QPersistentModelIndex that is a copy of the other persistent model index.
70    fn clone(&self) -> Self {
71        ffi::qpersistentmodelindex_clone(self)
72    }
73}
74
75impl Drop for QPersistentModelIndex {
76    /// Destroys the persistent model index.
77    fn drop(&mut self) {
78        ffi::qpersistentmodelindex_drop(self)
79    }
80}
81
82impl From<&crate::QModelIndex> for QPersistentModelIndex {
83    /// Creates a new QPersistentModelIndex that is a copy of the model index.
84    fn from(index: &crate::QModelIndex) -> Self {
85        ffi::qpersistentmodelindex_from_qmodelindex(index)
86    }
87}
88
89impl std::cmp::PartialEq for QPersistentModelIndex {
90    fn eq(&self, other: &Self) -> bool {
91        ffi::qpersistentmodelindex_eq(self, other)
92    }
93}
94
95impl std::cmp::Eq for QPersistentModelIndex {}
96
97impl fmt::Display for QPersistentModelIndex {
98    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
99        write!(f, "{}", ffi::qpersistentmodelindex_to_qstring(self))
100    }
101}
102
103impl fmt::Debug for QPersistentModelIndex {
104    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105        write!(f, "{self}")
106    }
107}
108
109// Safety:
110//
111// Static checks on the C++ side to ensure the size is the same.
112unsafe impl ExternType for QPersistentModelIndex {
113    type Id = type_id!("QPersistentModelIndex");
114    type Kind = cxx::kind::Trivial;
115}