cxx_qt_lib/core/
qmodelindex.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/qmodelindex.h");
13        include!("cxx-qt-lib/qstring.h");
14
15        type QModelIndex = super::QModelIndex;
16        type QString = crate::QString;
17
18        /// Returns the column this model index refers to.
19        fn column(self: &QModelIndex) -> i32;
20        /// Returns true if this model index is valid; otherwise returns false.
21        ///
22        /// A valid index belongs to a model, and has non-negative row and column numbers.
23        #[rust_name = "is_valid"]
24        fn isValid(self: &QModelIndex) -> bool;
25        /// Returns the parent of the model index, or QModelIndex() if it has no parent.
26        fn parent(self: &QModelIndex) -> QModelIndex;
27        /// Returns the row this model index refers to.
28        fn row(self: &QModelIndex) -> i32;
29        /// Returns the sibling at row and column. If there is no sibling at this position, an invalid QModelIndex is returned.
30        fn sibling(self: &QModelIndex, row: i32, column: i32) -> QModelIndex;
31        /// Returns the sibling at column for the current row. If there is no sibling at this position, an invalid QModelIndex is returned.
32        #[rust_name = "sibling_at_column"]
33        fn siblingAtColumn(self: &QModelIndex, column: i32) -> QModelIndex;
34        /// Returns the sibling at row for the current column. If there is no sibling at this position, an invalid QModelIndex is returned.
35        #[rust_name = "sibling_at_row"]
36        fn siblingAtRow(self: &QModelIndex, row: i32) -> QModelIndex;
37
38        /// Returns a `*mut c_void` pointer used by the model to associate the index with the internal data structure.
39        #[rust_name = "internal_pointer_mut"]
40        fn internalPointer(self: &QModelIndex) -> *mut c_void;
41    }
42
43    #[namespace = "rust::cxxqtlib1"]
44    unsafe extern "C++" {
45        include!("cxx-qt-lib/common.h");
46        type c_void = crate::c_void;
47
48        #[doc(hidden)]
49        #[rust_name = "qmodelindex_init_default"]
50        fn construct() -> QModelIndex;
51        #[doc(hidden)]
52        #[rust_name = "qmodelindex_eq"]
53        fn operatorEq(a: &QModelIndex, b: &QModelIndex) -> bool;
54        #[doc(hidden)]
55        #[rust_name = "qmodelindex_to_qstring"]
56        fn toQString(value: &QModelIndex) -> QString;
57
58        #[doc(hidden)]
59        #[rust_name = "qmodelindex_internal_id"]
60        fn qmodelindexInternalId(index: &QModelIndex) -> usize;
61    }
62}
63
64/// The QModelIndex class is used to locate data in a data model.
65#[derive(Clone)]
66#[repr(C)]
67pub struct QModelIndex {
68    _r: MaybeUninit<i32>,
69    _c: MaybeUninit<i32>,
70    _i: MaybeUninit<usize>,
71    _m: MaybeUninit<usize>,
72}
73
74impl QModelIndex {
75    /// Returns a `usize` used by the model to associate the index with the internal data structure.
76    //
77    // TODO: need to add support for quintptr
78    pub fn internal_id(&self) -> usize {
79        ffi::qmodelindex_internal_id(self)
80    }
81}
82
83impl Default for QModelIndex {
84    /// Creates a new empty model index. This type of model index is used to indicate that the position in the model is invalid.
85    fn default() -> Self {
86        ffi::qmodelindex_init_default()
87    }
88}
89
90impl std::cmp::PartialEq for QModelIndex {
91    fn eq(&self, other: &Self) -> bool {
92        ffi::qmodelindex_eq(self, other)
93    }
94}
95
96impl std::cmp::Eq for QModelIndex {}
97
98impl fmt::Display for QModelIndex {
99    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
100        write!(f, "{}", ffi::qmodelindex_to_qstring(self))
101    }
102}
103
104impl fmt::Debug for QModelIndex {
105    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
106        write!(f, "{self}")
107    }
108}
109
110// Safety:
111//
112// Static checks on the C++ side to ensure the size is the same.
113unsafe impl ExternType for QModelIndex {
114    type Id = type_id!("QModelIndex");
115    type Kind = cxx::kind::Trivial;
116}