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;
89#[cxx::bridge]
10mod ffi {
11unsafe extern "C++" {
12include!("cxx-qt-lib/qmodelindex.h");
13include!("cxx-qt-lib/qstring.h");
1415type QModelIndex = super::QModelIndex;
16type QString = crate::QString;
1718/// Returns the column this model index refers to.
19fn 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"]
24fn isValid(self: &QModelIndex) -> bool;
25/// Returns the parent of the model index, or QModelIndex() if it has no parent.
26fn parent(self: &QModelIndex) -> QModelIndex;
27/// Returns the row this model index refers to.
28fn 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.
30fn 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"]
33fn 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"]
36fn siblingAtRow(self: &QModelIndex, row: i32) -> QModelIndex;
3738/// 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"]
40fn internalPointer(self: &QModelIndex) -> *mut c_void;
41 }
4243#[namespace = "rust::cxxqtlib1"]
44unsafe extern "C++" {
45include!("cxx-qt-lib/common.h");
46type c_void = crate::c_void;
4748#[doc(hidden)]
49 #[rust_name = "qmodelindex_init_default"]
50fn construct() -> QModelIndex;
51#[doc(hidden)]
52 #[rust_name = "qmodelindex_eq"]
53fn operatorEq(a: &QModelIndex, b: &QModelIndex) -> bool;
54#[doc(hidden)]
55 #[rust_name = "qmodelindex_to_qstring"]
56fn toQString(value: &QModelIndex) -> QString;
5758#[doc(hidden)]
59 #[rust_name = "qmodelindex_internal_id"]
60fn qmodelindexInternalId(index: &QModelIndex) -> usize;
61 }
62}
6364/// 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}
7374impl 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
78pub fn internal_id(&self) -> usize {
79 ffi::qmodelindex_internal_id(self)
80 }
81}
8283impl 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.
85fn default() -> Self {
86 ffi::qmodelindex_init_default()
87 }
88}
8990impl std::cmp::PartialEq for QModelIndex {
91fn eq(&self, other: &Self) -> bool {
92 ffi::qmodelindex_eq(self, other)
93 }
94}
9596impl std::cmp::Eq for QModelIndex {}
9798impl fmt::Display for QModelIndex {
99fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
100write!(f, "{}", ffi::qmodelindex_to_qstring(self))
101 }
102}
103104impl fmt::Debug for QModelIndex {
105fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
106write!(f, "{self}")
107 }
108}
109110// Safety:
111//
112// Static checks on the C++ side to ensure the size is the same.
113unsafe impl ExternType for QModelIndex {
114type Id = type_id!("QModelIndex");
115type Kind = cxx::kind::Trivial;
116}