rust-qt-binding-generator/tests/rust_tree/src/interface.rs

311 lines
8.9 KiB
Rust
Raw Normal View History

2017-08-20 08:15:27 -07:00
/* generated by rust_qt_binding_generator */
#![allow(unknown_lints)]
#![allow(mutex_atomic, needless_pass_by_value)]
2018-05-16 00:12:03 -07:00
use libc::{c_char, c_ushort, c_int};
use std::slice;
2018-05-02 13:18:37 -07:00
use std::char::decode_utf16;
2017-08-20 08:15:27 -07:00
use std::sync::{Arc, Mutex};
use std::ptr::null;
use implementation::*;
#[repr(C)]
pub struct COption<T> {
data: T,
some: bool,
}
2018-09-08 11:47:48 -07:00
impl<T> COption<T> {
#![allow(dead_code)]
fn into(self) -> Option<T> {
if self.some {
Some(self.data)
} else {
None
}
}
}
2017-08-29 11:37:51 -07:00
impl<T> From<Option<T>> for COption<T>
where
T: Default,
{
fn from(t: Option<T>) -> COption<T> {
if let Some(v) = t {
COption {
data: v,
2017-08-29 11:37:51 -07:00
some: true,
}
} else {
COption {
data: T::default(),
2017-08-29 11:37:51 -07:00
some: false,
}
}
}
}
2018-05-02 13:18:37 -07:00
pub enum QString {}
2018-05-02 13:18:37 -07:00
fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) {
let utf16 = unsafe { slice::from_raw_parts(str, to_usize(len)) };
2018-05-02 13:18:37 -07:00
let characters = decode_utf16(utf16.iter().cloned())
.into_iter()
.map(|r| r.unwrap());
s.clear();
s.extend(characters);
}
#[repr(C)]
2018-06-30 11:25:39 -07:00
#[derive(PartialEq, Eq, Debug)]
pub enum SortOrder {
Ascending = 0,
2017-08-29 11:37:51 -07:00
Descending = 1,
}
#[repr(C)]
pub struct QModelIndex {
row: c_int,
internal_id: usize,
}
fn to_usize(n: c_int) -> usize {
if n < 0 {
panic!("Cannot cast {} to usize", n);
}
n as usize
}
fn to_c_int(n: usize) -> c_int {
if n > c_int::max_value() as usize {
panic!("Cannot cast {} to c_int", n);
}
n as c_int
}
2017-08-20 08:15:27 -07:00
pub struct PersonsQObject {}
2017-08-29 11:37:51 -07:00
#[derive(Clone)]
2017-08-20 08:15:27 -07:00
pub struct PersonsEmitter {
qobject: Arc<Mutex<*const PersonsQObject>>,
2018-09-08 11:47:48 -07:00
new_data_ready: fn(*const PersonsQObject, index: COption<usize>),
2017-08-20 08:15:27 -07:00
}
unsafe impl Send for PersonsEmitter {}
impl PersonsEmitter {
fn clear(&self) {
*self.qobject.lock().unwrap() = null();
}
pub fn new_data_ready(&self, item: Option<usize>) {
2017-08-20 08:15:27 -07:00
let ptr = *self.qobject.lock().unwrap();
if !ptr.is_null() {
2018-09-08 11:47:48 -07:00
(self.new_data_ready)(ptr, item.into());
2017-08-20 08:15:27 -07:00
}
}
}
#[derive(Clone)]
pub struct PersonsTree {
2017-08-20 08:15:27 -07:00
qobject: *const PersonsQObject,
layout_about_to_be_changed: fn(*const PersonsQObject),
layout_changed: fn(*const PersonsQObject),
data_changed: fn(*const PersonsQObject, usize, usize),
2017-08-20 08:15:27 -07:00
begin_reset_model: fn(*const PersonsQObject),
end_reset_model: fn(*const PersonsQObject),
2018-09-08 11:47:48 -07:00
begin_insert_rows: fn(*const PersonsQObject, index: COption<usize>, usize, usize),
2017-08-20 08:15:27 -07:00
end_insert_rows: fn(*const PersonsQObject),
begin_move_rows: fn(*const PersonsQObject, index: COption<usize>, usize, usize, dest: COption<usize>, usize),
end_move_rows: fn(*const PersonsQObject),
2018-09-08 11:47:48 -07:00
begin_remove_rows: fn(*const PersonsQObject, index: COption<usize>, usize, usize),
2017-08-20 08:15:27 -07:00
end_remove_rows: fn(*const PersonsQObject),
}
impl PersonsTree {
pub fn layout_about_to_be_changed(&self) {
(self.layout_about_to_be_changed)(self.qobject);
}
pub fn layout_changed(&self) {
(self.layout_changed)(self.qobject);
}
2017-08-28 12:38:57 -07:00
pub fn data_changed(&self, first: usize, last: usize) {
(self.data_changed)(self.qobject, first, last);
}
2017-08-20 08:15:27 -07:00
pub fn begin_reset_model(&self) {
(self.begin_reset_model)(self.qobject);
}
pub fn end_reset_model(&self) {
(self.end_reset_model)(self.qobject);
}
pub fn begin_insert_rows(&self, index: Option<usize>, first: usize, last: usize) {
2018-09-08 11:47:48 -07:00
(self.begin_insert_rows)(self.qobject, index.into(), first, last);
2017-08-20 08:15:27 -07:00
}
pub fn end_insert_rows(&self) {
(self.end_insert_rows)(self.qobject);
}
pub fn begin_move_rows(&self, index: Option<usize>, first: usize, last: usize, dest: Option<usize>, destination: usize) {
(self.begin_move_rows)(self.qobject, index.into(), first, last, dest.into(), destination);
}
pub fn end_move_rows(&self) {
(self.end_move_rows)(self.qobject);
}
pub fn begin_remove_rows(&self, index: Option<usize>, first: usize, last: usize) {
2018-09-08 11:47:48 -07:00
(self.begin_remove_rows)(self.qobject, index.into(), first, last);
2017-08-20 08:15:27 -07:00
}
pub fn end_remove_rows(&self) {
(self.end_remove_rows)(self.qobject);
}
}
pub trait PersonsTrait {
fn new(emit: PersonsEmitter, model: PersonsTree) -> Self;
2017-08-20 08:15:27 -07:00
fn emit(&self) -> &PersonsEmitter;
fn row_count(&self, Option<usize>) -> usize;
2017-08-29 11:37:51 -07:00
fn can_fetch_more(&self, Option<usize>) -> bool {
false
}
fn fetch_more(&mut self, Option<usize>) {}
2017-08-22 04:45:34 -07:00
fn sort(&mut self, u8, SortOrder) {}
fn check_row(&self, index: usize, row: usize) -> Option<usize>;
fn index(&self, item: Option<usize>, row: usize) -> usize;
fn parent(&self, index: usize) -> Option<usize>;
fn row(&self, index: usize) -> usize;
fn user_name(&self, index: usize) -> &str;
fn set_user_name(&mut self, index: usize, String) -> bool;
2017-08-20 08:15:27 -07:00
}
#[no_mangle]
2017-08-29 11:37:51 -07:00
pub extern "C" fn persons_new(
persons: *mut PersonsQObject,
2018-09-08 11:47:48 -07:00
persons_new_data_ready: fn(*const PersonsQObject, index: COption<usize>),
persons_layout_about_to_be_changed: fn(*const PersonsQObject),
persons_layout_changed: fn(*const PersonsQObject),
persons_data_changed: fn(*const PersonsQObject, usize, usize),
persons_begin_reset_model: fn(*const PersonsQObject),
persons_end_reset_model: fn(*const PersonsQObject),
2018-09-08 11:47:48 -07:00
persons_begin_insert_rows: fn(*const PersonsQObject, index: COption<usize>, usize, usize),
persons_end_insert_rows: fn(*const PersonsQObject),
persons_begin_move_rows: fn(*const PersonsQObject, index: COption<usize>, usize, usize, index: COption<usize>, usize),
persons_end_move_rows: fn(*const PersonsQObject),
2018-09-08 11:47:48 -07:00
persons_begin_remove_rows: fn(*const PersonsQObject, index: COption<usize>, usize, usize),
persons_end_remove_rows: fn(*const PersonsQObject),
2017-08-29 11:37:51 -07:00
) -> *mut Persons {
2017-08-26 10:10:18 -07:00
let persons_emit = PersonsEmitter {
qobject: Arc::new(Mutex::new(persons)),
new_data_ready: persons_new_data_ready,
2017-08-20 08:15:27 -07:00
};
let model = PersonsTree {
2017-08-26 10:10:18 -07:00
qobject: persons,
layout_about_to_be_changed: persons_layout_about_to_be_changed,
layout_changed: persons_layout_changed,
data_changed: persons_data_changed,
begin_reset_model: persons_begin_reset_model,
end_reset_model: persons_end_reset_model,
begin_insert_rows: persons_begin_insert_rows,
end_insert_rows: persons_end_insert_rows,
begin_move_rows: persons_begin_move_rows,
end_move_rows: persons_end_move_rows,
begin_remove_rows: persons_begin_remove_rows,
end_remove_rows: persons_end_remove_rows,
2017-08-20 08:15:27 -07:00
};
let d_persons = Persons::new(persons_emit, model);
2017-08-26 10:10:18 -07:00
Box::into_raw(Box::new(d_persons))
2017-08-20 08:15:27 -07:00
}
#[no_mangle]
pub unsafe extern "C" fn persons_free(ptr: *mut Persons) {
Box::from_raw(ptr).emit().clear();
}
#[no_mangle]
2017-08-29 11:37:51 -07:00
pub unsafe extern "C" fn persons_row_count(
ptr: *const Persons,
2018-09-08 11:47:48 -07:00
index: COption<usize>,
2017-08-29 11:37:51 -07:00
) -> c_int {
2018-09-08 11:47:48 -07:00
to_c_int((&*ptr).row_count(index.into()))
2017-08-20 08:15:27 -07:00
}
#[no_mangle]
2017-08-29 11:37:51 -07:00
pub unsafe extern "C" fn persons_can_fetch_more(
ptr: *const Persons,
2018-09-08 11:47:48 -07:00
index: COption<usize>,
2017-08-29 11:37:51 -07:00
) -> bool {
2018-09-08 11:47:48 -07:00
(&*ptr).can_fetch_more(index.into())
2017-08-20 08:15:27 -07:00
}
#[no_mangle]
2018-09-08 11:47:48 -07:00
pub unsafe extern "C" fn persons_fetch_more(ptr: *mut Persons, index: COption<usize>) {
(&mut *ptr).fetch_more(index.into())
2017-08-20 08:15:27 -07:00
}
#[no_mangle]
2017-08-29 11:37:51 -07:00
pub unsafe extern "C" fn persons_sort(
ptr: *mut Persons,
column: u8,
order: SortOrder
) {
2017-08-20 08:15:27 -07:00
(&mut *ptr).sort(column, order)
}
#[no_mangle]
pub unsafe extern "C" fn persons_check_row(
ptr: *const Persons,
index: usize,
row: c_int,
) -> COption<usize> {
(&*ptr).check_row(index.into(), to_usize(row)).into()
}
#[no_mangle]
2017-08-29 11:37:51 -07:00
pub unsafe extern "C" fn persons_index(
ptr: *const Persons,
2018-09-08 11:47:48 -07:00
index: COption<usize>,
2017-08-29 11:37:51 -07:00
row: c_int,
) -> usize {
2018-09-08 11:47:48 -07:00
(&*ptr).index(index.into(), to_usize(row))
}
#[no_mangle]
pub unsafe extern "C" fn persons_parent(ptr: *const Persons, index: usize) -> QModelIndex {
if let Some(parent) = (&*ptr).parent(index) {
2017-08-29 11:37:51 -07:00
QModelIndex {
row: to_c_int((&*ptr).row(parent)),
2017-08-29 11:37:51 -07:00
internal_id: parent,
}
} else {
2017-08-29 11:37:51 -07:00
QModelIndex {
row: -1,
internal_id: 0,
}
}
2017-08-20 08:15:27 -07:00
}
#[no_mangle]
pub unsafe extern "C" fn persons_row(ptr: *const Persons, index: usize) -> c_int {
to_c_int((&*ptr).row(index))
2017-08-20 08:15:27 -07:00
}
#[no_mangle]
2018-05-02 13:18:37 -07:00
pub extern "C" fn persons_data_user_name(
ptr: *const Persons, index: usize,
2018-05-02 13:18:37 -07:00
d: *mut QString,
set: fn(*mut QString, *const c_char, len: c_int),
2017-08-29 11:37:51 -07:00
) {
2018-05-02 13:18:37 -07:00
let o = unsafe { &*ptr };
let data = o.user_name(index);
2018-05-02 13:18:37 -07:00
let s: *const c_char = data.as_ptr() as (*const c_char);
set(d, s, to_c_int(data.len()));
2017-08-20 08:15:27 -07:00
}
2017-08-20 08:15:27 -07:00
#[no_mangle]
2018-05-02 13:18:37 -07:00
pub extern "C" fn persons_set_data_user_name(
ptr: *mut Persons, index: usize,
2018-05-02 13:18:37 -07:00
s: *const c_ushort, len: c_int,
2017-08-29 11:37:51 -07:00
) -> bool {
2018-05-02 13:18:37 -07:00
let o = unsafe { &mut *ptr };
let mut v = String::new();
set_string_from_utf16(&mut v, s, len);
o.set_user_name(index, v)
2017-08-20 08:15:27 -07:00
}