forked from KDAB/cxx-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cxx-qt-lib: add support for QModelIndex
Related to KDAB#291
- Loading branch information
1 parent
79190d2
commit 9faecdd
Showing
14 changed files
with
180 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// clang-format off | ||
// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// clang-format on | ||
// SPDX-FileContributor: Andrew Hayzen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
#pragma once | ||
|
||
#include <QModelIndex> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// clang-format off | ||
// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// clang-format on | ||
// SPDX-FileContributor: Andrew Hayzen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
#include "cxx-qt-lib/qmodelindex.h" | ||
|
||
#include "assertion_utils.h" | ||
|
||
// QModelIndex has two ints, a uint pointer, and a pointer. | ||
// This results in 4 + 4 + 4 + 8 = 20, then due to compiler padding this results in | ||
// size of 24 or three pointers. | ||
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/itemmodels/qabstractitemmodel.h?h=v5.15.6-lts-lgpl#n93 | ||
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/itemmodels/qabstractitemmodel.h?h=v6.2.4#n195 | ||
assert_alignment_and_size(QModelIndex, | ||
alignof(std::size_t), | ||
sizeof(std::size_t[3])); | ||
|
||
static_assert(std::is_trivially_copyable<QModelIndex>::value); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// SPDX-FileContributor: Andrew Hayzen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
use cxx::{type_id, ExternType}; | ||
use std::mem::MaybeUninit; | ||
|
||
#[cxx::bridge] | ||
mod ffi { | ||
unsafe extern "C++" { | ||
include!("cxx-qt-lib/qmodelindex.h"); | ||
|
||
type QModelIndex = super::QModelIndex; | ||
|
||
/// Returns the column this model index refers to. | ||
fn column(self: &QModelIndex) -> i32; | ||
/// Returns true if this model index is valid; otherwise returns false. | ||
/// | ||
/// A valid index belongs to a model, and has non-negative row and column numbers. | ||
#[rust_name = "is_valid"] | ||
fn isValid(self: &QModelIndex) -> bool; | ||
/// Returns the parent of the model index, or QModelIndex() if it has no parent. | ||
fn parent(self: &QModelIndex) -> QModelIndex; | ||
/// Returns the row this model index refers to. | ||
fn row(self: &QModelIndex) -> i32; | ||
/// Returns the sibling at row and column. If there is no sibling at this position, an invalid QModelIndex is returned. | ||
fn sibling(self: &QModelIndex, row: i32, column: i32) -> QModelIndex; | ||
/// Returns the sibling at column for the current row. If there is no sibling at this position, an invalid QModelIndex is returned. | ||
#[rust_name = "sibling_at_column"] | ||
fn siblingAtColumn(self: &QModelIndex, column: i32) -> QModelIndex; | ||
/// Returns the sibling at row for the current column. If there is no sibling at this position, an invalid QModelIndex is returned. | ||
#[rust_name = "sibling_at_row"] | ||
fn siblingAtRow(self: &QModelIndex, row: i32) -> QModelIndex; | ||
} | ||
|
||
#[namespace = "rust::cxxqtlib1"] | ||
unsafe extern "C++" { | ||
include!("cxx-qt-lib/common.h"); | ||
|
||
#[doc(hidden)] | ||
#[rust_name = "qmodelindex_init_default"] | ||
fn construct() -> QModelIndex; | ||
} | ||
} | ||
|
||
/// The QModelIndex class is used to locate data in a data model. | ||
#[derive(Clone)] | ||
#[repr(C)] | ||
pub struct QModelIndex { | ||
_space: MaybeUninit<[usize; 3]>, | ||
} | ||
|
||
impl Default for QModelIndex { | ||
/// Creates a new empty model index. This type of model index is used to indicate that the position in the model is invalid. | ||
fn default() -> Self { | ||
ffi::qmodelindex_init_default() | ||
} | ||
} | ||
|
||
// Safety: | ||
// | ||
// Static checks on the C++ side to ensure the size is the same. | ||
unsafe impl ExternType for QModelIndex { | ||
type Id = type_id!("QModelIndex"); | ||
type Kind = cxx::kind::Trivial; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,25 +2,6 @@ | |
// SPDX-FileContributor: Andrew Hayzen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
use cxx::{type_id, ExternType}; | ||
use std::mem::MaybeUninit; | ||
|
||
/// Define a QModelIndex that is trivial for CXX | ||
/// | ||
/// TODO: later this will likely be in cxx-qt-lib | ||
#[repr(C)] | ||
pub struct QModelIndex { | ||
_space: MaybeUninit<[usize; 3]>, | ||
} | ||
|
||
// Safety: | ||
// | ||
// Static checks on the C++ side to ensure the size is the same. | ||
// TODO: later this will likely be in cxx-qt-lib | ||
unsafe impl ExternType for QModelIndex { | ||
type Id = type_id!("QModelIndex"); | ||
type Kind = cxx::kind::Trivial; | ||
} | ||
|
||
// ANCHOR: book_macro_code | ||
#[cxx_qt::bridge(cxx_file_stem = "custom_base_class")] | ||
|
@@ -33,9 +14,8 @@ mod ffi { | |
include!("cxx-qt-lib/qvariant.h"); | ||
type QVariant = cxx_qt_lib::QVariant; | ||
|
||
// Define the interface of the QModelIndex | ||
type QModelIndex = super::QModelIndex; | ||
fn row(self: &QModelIndex) -> i32; | ||
include!("cxx-qt-lib/qmodelindex.h"); | ||
type QModelIndex = cxx_qt_lib::QModelIndex; | ||
|
||
#[cxx_name = "beginInsertRows"] | ||
fn begin_insert_rows(self: Pin<&mut CustomBaseClassQt>, first: i32, last: i32); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// clang-format off | ||
// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// clang-format on | ||
// SPDX-FileContributor: Andrew Hayzen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
#pragma once | ||
|
||
#include <QtCore/QModelIndex> | ||
#include <QtCore/QStringListModel> | ||
#include <QtTest/QTest> | ||
|
||
#include "cxx-qt-gen/qmodelindex_cxx.cxx.h" | ||
|
||
class QModelIndexTest : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
private Q_SLOTS: | ||
void construct() | ||
{ | ||
const auto i = construct_qmodelindex(); | ||
QCOMPARE(i.isValid(), false); | ||
} | ||
|
||
void read() | ||
{ | ||
const auto model = QStringListModel(QStringList() << QStringLiteral("kdab")); | ||
QVERIFY(read_qmodelindex(model.index(0))); | ||
} | ||
|
||
void clone() | ||
{ | ||
const auto model = QStringListModel(QStringList() << QStringLiteral("kdab")); | ||
const auto c = clone_qmodelindex(model.index(0)); | ||
QCOMPARE(c.isValid(), true); | ||
QCOMPARE(c.row(), 0); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ mod qcolor; | |
mod qdate; | ||
mod qdatetime; | ||
mod qhash; | ||
mod qmodelindex; | ||
mod qpoint; | ||
mod qpointf; | ||
mod qrect; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// SPDX-FileContributor: Andrew Hayzen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
use cxx_qt_lib::QModelIndex; | ||
|
||
#[cxx::bridge] | ||
mod qmodelindex_cxx { | ||
unsafe extern "C++" { | ||
include!("cxx-qt-lib/qmodelindex.h"); | ||
type QModelIndex = cxx_qt_lib::QModelIndex; | ||
} | ||
|
||
extern "Rust" { | ||
fn construct_qmodelindex() -> QModelIndex; | ||
fn read_qmodelindex(i: &QModelIndex) -> bool; | ||
fn clone_qmodelindex(i: &QModelIndex) -> QModelIndex; | ||
} | ||
} | ||
|
||
fn construct_qmodelindex() -> QModelIndex { | ||
QModelIndex::default() | ||
} | ||
|
||
fn read_qmodelindex(i: &QModelIndex) -> bool { | ||
i.is_valid() && i.row() == 0 | ||
} | ||
|
||
fn clone_qmodelindex(i: &QModelIndex) -> QModelIndex { | ||
i.clone() | ||
} |