Add LineCount module.

This commit is contained in:
2025-03-29 16:55:26 -04:00
parent a6d2fb9e31
commit 13a405a801
7 changed files with 386 additions and 98 deletions

View File

@@ -1,77 +1,103 @@
// SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// SPDX-FileContributor: Leon Matthes <leon.matthes@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
// TODO: Header
#[cxx_qt::bridge]
mod qobject {
pub mod qobject {
unsafe extern "C++" {
include!("cxx-qt-lib/qstring.h");
type QString = cxx_qt_lib::QString;
}
#[qenum(Greeter)]
pub enum Language {
English,
German,
French,
}
#[qenum(Greeter)]
pub enum Greeting {
Hello,
Bye,
include!("cxx-qt-lib/qvariant.h");
type QVariant = cxx_qt_lib::QVariant;
include!(<QtCore/QAbstractListModel>);
type QModelIndex = cxx_qt_lib::QModelIndex;
type QAbstractListModel;
}
unsafe extern "RustQt" {
#[qobject]
#[base = QAbstractListModel]
type AbstractListModel = super::AbstractListModelRust;
#[qobject]
#[base = AbstractListModel]
#[qml_element]
#[qproperty(Greeting, greeting)]
#[qproperty(Language, language)]
type Greeter = super::GreeterRust;
#[qproperty(i32, count)]
type LineCount = super::LineCountRust;
#[cxx_name = "beginInsertRows"]
#[inherit]
fn beginInsertRows(self: Pin<&mut LineCount>, parent: &QModelIndex, first: i32, last: i32);
#[cxx_name = "endInsertRows"]
#[inherit]
fn endInsertRows(self: Pin<&mut LineCount>);
#[cxx_name = "beginRemoveRows"]
#[inherit]
fn beginRemoveRows(self: Pin<&mut LineCount>, parent: &QModelIndex, first: i32, last: i32);
#[cxx_name = "endRemoveRows"]
#[inherit]
fn endRemoveRows(self: Pin<&mut LineCount>);
#[qinvokable]
fn greet(self: &Greeter) -> QString;
pub fn set_line_count(self: Pin<&mut LineCount>, line_count: i32);
#[qinvokable]
#[cxx_override]
fn data(self: &LineCount, index: &QModelIndex, role: i32) -> QVariant;
#[qinvokable]
#[cxx_override]
#[cxx_name = "rowCount"]
fn row_count(self: &LineCount, _parent: &QModelIndex) -> i32;
}
}
use qobject::*;
use cxx_qt::CxxQtType;
use cxx_qt_lib::{QModelIndex, QVariant};
impl Greeting {
fn translate(&self, language: Language) -> String {
match (self, language) {
(&Greeting::Hello, Language::English) => "Hello, World!",
(&Greeting::Hello, Language::German) => "Hallo, Welt!",
(&Greeting::Hello, Language::French) => "Bonjour, le monde!",
(&Greeting::Bye, Language::English) => "Bye!",
(&Greeting::Bye, Language::German) => "Auf Wiedersehen!",
(&Greeting::Bye, Language::French) => "Au revoir!",
_ => "🤯",
impl qobject::LineCount {
pub fn set_line_count(mut self: std::pin::Pin<&mut Self>, line_count: i32) {
let current_count = self.as_mut().rust_mut().count;
if line_count < 0 || current_count == line_count {
log::warn!(
"Can't set line count: {}; Current count: {}",
line_count,
current_count
);
return;
}
.to_string()
}
}
pub struct GreeterRust {
greeting: Greeting,
language: Language,
}
impl Default for GreeterRust {
fn default() -> Self {
Self {
greeting: Greeting::Hello,
language: Language::English,
if current_count < line_count {
self.as_mut()
.beginInsertRows(&QModelIndex::default(), current_count, line_count - 1);
self.as_mut().endInsertRows();
} else if current_count > line_count {
self.as_mut()
.beginRemoveRows(&QModelIndex::default(), line_count, current_count - 1);
self.as_mut().endRemoveRows();
}
self.as_mut().rust_mut().count = line_count;
log::warn!(
"Line count changed from {} to {}",
current_count,
line_count
);
}
pub fn row_count(self: &Self, _parent: &QModelIndex) -> i32 {
*self.count()
}
pub fn data(self: &Self, _index: &QModelIndex, _role: i32) -> QVariant {
QVariant::default()
}
}
use cxx_qt_lib::QString;
/// A struct which inherits from QAbstractListModel
#[derive(Default)]
pub struct AbstractListModelRust {}
impl qobject::Greeter {
fn greet(&self) -> QString {
QString::from(self.greeting.translate(self.language))
}
#[derive(Default)]
pub struct LineCountRust {
pub count: i32,
}
fn main() {