2025-03-29 16:55:26 -04:00
|
|
|
// TODO: Header
|
2025-03-29 08:01:13 -04:00
|
|
|
|
|
|
|
|
#[cxx_qt::bridge]
|
2025-03-29 16:55:26 -04:00
|
|
|
pub mod qobject {
|
2025-03-29 08:01:13 -04:00
|
|
|
unsafe extern "C++" {
|
2025-03-29 16:55:26 -04:00
|
|
|
include!("cxx-qt-lib/qvariant.h");
|
|
|
|
|
type QVariant = cxx_qt_lib::QVariant;
|
|
|
|
|
include!(<QtCore/QAbstractListModel>);
|
|
|
|
|
type QModelIndex = cxx_qt_lib::QModelIndex;
|
|
|
|
|
type QAbstractListModel;
|
2025-03-29 08:01:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe extern "RustQt" {
|
|
|
|
|
#[qobject]
|
2025-03-29 16:55:26 -04:00
|
|
|
#[base = QAbstractListModel]
|
|
|
|
|
type AbstractListModel = super::AbstractListModelRust;
|
|
|
|
|
|
|
|
|
|
#[qobject]
|
|
|
|
|
#[base = AbstractListModel]
|
2025-03-29 08:01:13 -04:00
|
|
|
#[qml_element]
|
2025-03-29 16:55:26 -04:00
|
|
|
#[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>);
|
2025-03-29 08:01:13 -04:00
|
|
|
|
|
|
|
|
#[qinvokable]
|
2025-03-29 16:55:26 -04:00
|
|
|
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;
|
2025-03-29 08:01:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-29 16:55:26 -04:00
|
|
|
use cxx_qt::CxxQtType;
|
|
|
|
|
use cxx_qt_lib::{QModelIndex, QVariant};
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
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();
|
2025-03-29 08:01:13 -04:00
|
|
|
}
|
2025-03-29 16:55:26 -04:00
|
|
|
self.as_mut().rust_mut().count = line_count;
|
|
|
|
|
log::warn!(
|
|
|
|
|
"Line count changed from {} to {}",
|
|
|
|
|
current_count,
|
|
|
|
|
line_count
|
|
|
|
|
);
|
2025-03-29 08:01:13 -04:00
|
|
|
}
|
|
|
|
|
|
2025-03-29 16:55:26 -04:00
|
|
|
pub fn row_count(self: &Self, _parent: &QModelIndex) -> i32 {
|
|
|
|
|
*self.count()
|
|
|
|
|
}
|
2025-03-29 08:01:13 -04:00
|
|
|
|
2025-03-29 16:55:26 -04:00
|
|
|
pub fn data(self: &Self, _index: &QModelIndex, _role: i32) -> QVariant {
|
|
|
|
|
QVariant::default()
|
2025-03-29 08:01:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-29 16:55:26 -04:00
|
|
|
/// A struct which inherits from QAbstractListModel
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
pub struct AbstractListModelRust {}
|
2025-03-29 08:01:13 -04:00
|
|
|
|
2025-03-29 16:55:26 -04:00
|
|
|
#[derive(Default)]
|
|
|
|
|
pub struct LineCountRust {
|
|
|
|
|
pub count: i32,
|
2025-03-29 08:01:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
use cxx_qt_lib::{QGuiApplication, QQmlApplicationEngine, QUrl};
|
|
|
|
|
|
|
|
|
|
let mut app = QGuiApplication::new();
|
|
|
|
|
let mut engine = QQmlApplicationEngine::new();
|
|
|
|
|
|
|
|
|
|
if let Some(engine) = engine.as_mut() {
|
|
|
|
|
engine.load(&QUrl::from("qml/main.qml"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(app) = app.as_mut() {
|
|
|
|
|
app.exec();
|
|
|
|
|
}
|
|
|
|
|
}
|