First step towards using only generated models in Demo

master
Jos van den Oever 2017-08-13 20:01:53 +02:00
parent 0a634c027a
commit cb5f7d6ac1
15 changed files with 583 additions and 522 deletions

View File

@ -4,13 +4,23 @@ add_custom_command(
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/rust/src/testinterface.rs"
"${CMAKE_CURRENT_SOURCE_DIR}/rust/src/testimplementation.rs"
"${CMAKE_CURRENT_SOURCE_DIR}/rust/src/types.rs"
"${CMAKE_CURRENT_SOURCE_DIR}/src/tmp.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/Bindings.h"
# if the cpp file is marked GENERATED, CMake will not check it for moc
# "${CMAKE_CURRENT_SOURCE_DIR}/src/tmp.cpp"
COMMAND ${CMAKE_BINARY_DIR}/rust_qt_binding_generator/rust_qt_binding_generator --overwrite-implementation "${CMAKE_CURRENT_SOURCE_DIR}/bindings.json"
DEPENDS rust_qt_binding_generator bindings.json
)
add_custom_command(
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/rust/src/interface.rs"
"${CMAKE_CURRENT_SOURCE_DIR}/rust/src/types.rs"
"${CMAKE_CURRENT_SOURCE_DIR}/src/Tree.h"
# if the cpp file is marked GENERATED, CMake will not check it for moc
# "${CMAKE_CURRENT_SOURCE_DIR}/src/tmp.cpp"
COMMAND ${CMAKE_BINARY_DIR}/rust_qt_binding_generator/rust_qt_binding_generator "${CMAKE_CURRENT_SOURCE_DIR}/tree.json"
DEPENDS rust_qt_binding_generator tree.json
)
add_custom_command(
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/rust/${RUST_TARGET_DIR}/librust.a"
COMMAND cargo build ${RUST_BUILD_FLAG}
@ -24,7 +34,7 @@ add_custom_command(
)
add_custom_target(rust_target DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/rust/${RUST_TARGET_DIR}/librust.a")
set(Demo_SRCS src/main.cpp src/DemoObject.cpp src/tmp.cpp
set(Demo_SRCS src/main.cpp src/Tree.cpp src/Bindings.cpp
resource_file.qrc)
add_executable(Demo ${Demo_SRCS})

View File

@ -1,5 +1,5 @@
{
"cppFile": "src/tmp.cpp",
"cppFile": "src/Bindings.cpp",
"rust": {
"dir": "rust",
"interfaceModule": "testinterface",
@ -67,7 +67,7 @@
}]
]
}, {
"name": "Tree",
"name": "TestTree",
"type": "UniformTree",
"properties": [{
"name": "path",

View File

@ -7,31 +7,6 @@ use std::ffi::OsString;
use std::default::Default;
use std::thread;
pub struct Hello {
emit: HelloEmitter,
hello: String,
}
impl HelloTrait for Hello {
fn create(emit: HelloEmitter) -> Self {
Hello {
emit: emit,
hello: String::new(),
}
}
fn get_hello(&self) -> &String {
&self.hello
}
fn set_hello(&mut self, value: String) {
self.hello = value;
self.emit.hello_changed();
}
}
impl Drop for Hello {
fn drop(&mut self) {}
}
pub struct DirEntry {
name: OsString,
}
@ -75,7 +50,7 @@ pub trait Item: Default {
fn file_permissions(&self) -> c_int;
}
pub type RItemModel = RGeneralItemModel<DirEntry>;
pub type Tree = RGeneralItemModel<DirEntry>;
struct Entry<T: Item> {
parent: usize,
@ -85,7 +60,7 @@ struct Entry<T: Item> {
}
pub struct RGeneralItemModel<T: Item> {
emit: RItemModelEmitter,
emit: TreeEmitter,
entries: Vec<Entry<T>>,
}
@ -99,7 +74,7 @@ impl<T: Item> RGeneralItemModel<T> {
if self.entries[p].children.is_none() {
self.retrieve(p);
let emit = self.emit.clone();
thread::spawn(move || { emit.new_data_ready(); });
// thread::spawn(move || { emit.new_data_ready(); });
}
p
}
@ -134,34 +109,35 @@ impl<T: Item> RGeneralItemModel<T> {
}
}
impl<T: Item> RItemModelTrait<T> for RGeneralItemModel<T> {
fn create(emit: RItemModelEmitter, root: T) -> Self {
impl<T: Item> TreeTrait for RGeneralItemModel<T> {
fn create(emit: TreeEmitter, model: TreeUniformTree) -> Self {
let none = Entry {
parent: 0,
row: 0,
children: None,
data: T::default(),
};
let root = Entry {
parent: 0,
row: 0,
children: None,
data: root,
};
RGeneralItemModel {
emit: emit,
entries: vec![none, root],
entries: vec![none],
}
}
fn emit(&self) -> &RItemModelEmitter {
fn emit(&self) -> &TreeEmitter {
&self.emit
}
fn row_count(&mut self, row: c_int, parent: usize) -> c_int {
let i = self.get(row, parent);
self.entries[i].children.as_ref().unwrap().len() as i32
fn get_path(&self) -> String {
String::new()
}
fn index(&mut self, row: c_int, parent: usize) -> usize {
self.get(row, parent)
fn set_path(&mut self, value: String) {
}
fn row_count(&self, row: c_int, parent: usize) -> c_int {
//let i = self.get(row, parent);
//self.entries[i].children.as_ref().unwrap().len() as i32
0
}
fn index(&self, row: c_int, parent: usize) -> usize {
//self.get(row, parent)
0
}
fn parent(&self, row: c_int, index: usize) -> QModelIndex {
if index < 2 {
@ -170,12 +146,20 @@ impl<T: Item> RItemModelTrait<T> for RGeneralItemModel<T> {
let e = &self.entries[index];
QModelIndex::create(e.row as i32, e.parent)
}
fn file_name(&mut self, row: c_int, parent: usize) -> String {
let i = self.get(row, parent);
self.entries[i].data.file_name()
fn file_name(&self, row: c_int, parent: usize) -> String {
//let i = self.get(row, parent);
//self.entries[i].data.file_name()
String::new()
}
fn file_permissions(&mut self, row: c_int, parent: usize) -> c_int {
let i = self.get(row,parent);
self.entries[i].data.file_permissions()
fn file_permissions(&self, row: c_int, parent: usize) -> c_int {
//let i = self.get(row,parent);
//self.entries[i].data.file_permissions()
0
}
fn file_icon(&self, row: c_int, parent: usize) -> Vec<u8> {
Vec::new()
}
fn file_path(&self, row: c_int, parent: usize) -> String {
String::new()
}
}

View File

@ -1,143 +1,170 @@
/* generated by rust_qt_binding_generator */
#![allow(unknown_lints)]
#![allow(mutex_atomic)]
use std::slice;
use libc::{c_int, uint16_t, size_t, c_void};
#![allow(mutex_atomic, needless_pass_by_value)]
#![allow(unused_imports)]
use libc::{c_int, c_uint, c_void};
use types::*;
use std::sync::{Arc, Mutex};
use std::ptr::null;
use implementation::*;
pub struct HelloQObject {}
pub struct HelloEmitter {
qobject: *const HelloQObject,
hello_changed: fn(*const HelloQObject),
}
impl HelloEmitter {
pub fn hello_changed(&self) {
(self.hello_changed)(self.qobject);
}
}
pub trait HelloTrait {
fn create(emit: HelloEmitter) -> Self;
fn get_hello(&self) -> &String;
fn set_hello(&mut self, value: String);
}
#[no_mangle]
pub extern "C" fn hello_new(qobject: *const HelloQObject,
changed: fn(*const HelloQObject))
-> *mut Hello {
let emit = HelloEmitter {
qobject: qobject,
hello_changed: changed,
};
let hello = Hello::create(emit);
Box::into_raw(Box::new(hello))
}
#[no_mangle]
pub unsafe extern "C" fn hello_free(ptr: *mut Hello) {
if ptr.is_null() {
return;
}
Box::from_raw(ptr);
}
#[no_mangle]
pub unsafe extern "C" fn hello_set(ptr: *mut Hello, s: *const uint16_t, len: size_t) {
let data = slice::from_raw_parts(s, len as usize);
(&mut *ptr).set_hello(String::from_utf16_lossy(data));
}
#[no_mangle]
pub unsafe extern "C" fn hello_get(ptr: *const Hello) -> QString {
QString::from((&*ptr).get_hello())
}
pub struct RItemModelQObject {}
pub struct TreeQObject {}
#[derive (Clone)]
pub struct RItemModelEmitter {
qobject: Arc<Mutex<*const RItemModelQObject>>,
new_data_ready: fn(*const RItemModelQObject),
pub struct TreeEmitter {
qobject: Arc<Mutex<*const TreeQObject>>,
path_changed: fn(*const TreeQObject),
}
unsafe impl Send for RItemModelEmitter {}
unsafe impl Send for TreeEmitter {}
impl RItemModelEmitter {
pub fn new_data_ready(&self) {
let ptr = *self.qobject.lock().unwrap();
if !ptr.is_null() {
(self.new_data_ready)(ptr);
}
}
impl TreeEmitter {
fn clear(&self) {
*self.qobject.lock().unwrap() = null();
}
pub fn path_changed(&self) {
let ptr = *self.qobject.lock().unwrap();
if !ptr.is_null() {
(self.path_changed)(ptr);
}
}
}
pub trait RItemModelTrait<T> {
fn create(emit: RItemModelEmitter, root: T) -> Self;
fn emit(&self) -> &RItemModelEmitter;
fn row_count(&mut self, row: c_int, parent: usize) -> c_int;
fn index(&mut self, row: c_int, parent: usize) -> usize;
pub struct TreeUniformTree {
qobject: *const TreeQObject,
tree_begin_insert_rows: fn(*const TreeQObject,row: c_int, parent: usize, c_int, c_int),
tree_end_insert_rows: fn(*const TreeQObject),
tree_begin_remove_rows: fn(*const TreeQObject,row: c_int, parent: usize, c_int, c_int),
tree_end_remove_rows: fn(*const TreeQObject),
}
impl TreeUniformTree {
pub fn tree_begin_insert_rows(&self,row: c_int, parent: usize, first: c_int, last: c_int) {
(self.tree_begin_insert_rows)(self.qobject,row, parent, first, last);
}
pub fn tree_end_insert_rows(&self) {
(self.tree_end_insert_rows)(self.qobject);
}
pub fn tree_begin_remove_rows(&self,row: c_int, parent: usize, first: c_int, last: c_int) {
(self.tree_begin_remove_rows)(self.qobject,row, parent, first, last);
}
pub fn tree_end_remove_rows(&self) {
(self.tree_end_remove_rows)(self.qobject);
}
}
pub trait TreeTrait {
fn create(emit: TreeEmitter, model: TreeUniformTree) -> Self;
fn emit(&self) -> &TreeEmitter;
fn get_path(&self) -> String;
fn set_path(&mut self, value: String);
fn row_count(&self, row: c_int, parent: usize) -> c_int;
fn can_fetch_more(&self, row: c_int, parent: usize) -> bool { false }
fn fetch_more(&self, row: c_int, parent: usize) {}
fn file_name(&self, row: c_int, parent: usize) -> String;
fn file_icon(&self, row: c_int, parent: usize) -> Vec<u8>;
fn file_path(&self, row: c_int, parent: usize) -> String;
fn file_permissions(&self, row: c_int, parent: usize) -> c_int;
fn index(&self, row: c_int, parent: usize) -> usize;
fn parent(&self, row: c_int, parent: usize) -> QModelIndex;
fn file_name(&mut self, row: c_int, parent: usize) -> String;
fn file_permissions(&mut self, row: c_int, parent: usize) -> c_int;
}
#[no_mangle]
pub extern "C" fn ritemmodel_new(qobject: *const RItemModelQObject,
new_data_ready: fn(*const RItemModelQObject))
-> *mut RItemModel {
let emit = RItemModelEmitter {
pub extern "C" fn tree_new(qobject: *const TreeQObject,
path_changed: fn(*const TreeQObject),
tree_begin_insert_rows: fn(*const TreeQObject,row: c_int, parent: usize,
c_int,
c_int),
tree_end_insert_rows: fn(*const TreeQObject),
tree_begin_remove_rows: fn(*const TreeQObject,row: c_int, parent: usize,
c_int,
c_int),
tree_end_remove_rows: fn(*const TreeQObject))
-> *mut Tree {
let emit = TreeEmitter {
qobject: Arc::new(Mutex::new(qobject)),
new_data_ready: new_data_ready,
path_changed: path_changed,
};
let ritemmodel = RItemModel::create(emit, DirEntry::create("/"));
Box::into_raw(Box::new(ritemmodel))
let model = TreeUniformTree {
qobject: qobject,
tree_begin_insert_rows: tree_begin_insert_rows,
tree_end_insert_rows: tree_end_insert_rows,
tree_begin_remove_rows: tree_begin_remove_rows,
tree_end_remove_rows: tree_end_remove_rows,
};
let d = Tree::create(emit, model);
Box::into_raw(Box::new(d))
}
#[no_mangle]
pub unsafe extern "C" fn ritemmodel_free(ptr: *mut RItemModel) {
pub unsafe extern "C" fn tree_free(ptr: *mut Tree) {
Box::from_raw(ptr).emit().clear();
}
#[no_mangle]
pub unsafe extern "C" fn ritemmodel_row_count(ptr: *mut RItemModel, row: c_int, parent: usize) -> i32 {
(&mut *ptr).row_count(row, parent)
pub unsafe extern "C" fn tree_path_get(ptr: *const Tree,
p: *mut c_void,
set: fn(*mut c_void, QString)) {
let data = (&*ptr).get_path();
set(p, QString::from(&data));
}
#[no_mangle]
pub unsafe extern "C" fn ritemmodel_index(ptr: *mut RItemModel,
row: c_int, parent: usize)
-> usize {
(&mut *ptr).index(row, parent)
pub unsafe extern "C" fn tree_path_set(ptr: *mut Tree, v: QStringIn) {
(&mut *ptr).set_path(v.convert());
}
#[no_mangle]
pub unsafe extern "C" fn ritemmodel_parent(ptr: *const RItemModel,
row: c_int, parent: usize)
-> QModelIndex {
(&*ptr).parent(row, parent)
pub unsafe extern "C" fn tree_row_count(ptr: *const Tree, row: c_int, parent: usize) -> c_int {
(&*ptr).row_count(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn tree_can_fetch_more(ptr: *const Tree, row: c_int, parent: usize) -> bool {
(&*ptr).can_fetch_more(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn tree_fetch_more(ptr: *mut Tree, row: c_int, parent: usize) {
(&mut *ptr).fetch_more(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn ritemmodel_data_file_name(ptr: *mut RItemModel,
pub unsafe extern "C" fn tree_data_file_name(ptr: *const Tree,
row: c_int, parent: usize,
d: *mut c_void,
set: fn(*mut c_void, QString)) {
let data = (&mut *ptr).file_name(row, parent);
let data = (&*ptr).file_name(row, parent);
set(d, QString::from(&data));
}
#[no_mangle]
pub unsafe extern "C" fn ritemmodel_data_file_permissions(ptr: *mut RItemModel,
row: c_int, parent: usize) -> c_int {
(&mut *ptr).file_permissions(row, parent)
pub unsafe extern "C" fn tree_data_file_icon(ptr: *const Tree,
row: c_int, parent: usize,
d: *mut c_void,
set: fn(*mut c_void, QByteArray)) {
let data = (&*ptr).file_icon(row, parent);
set(d, QByteArray::from(&data));
}
#[no_mangle]
pub unsafe extern "C" fn tree_data_file_path(ptr: *const Tree,
row: c_int, parent: usize,
d: *mut c_void,
set: fn(*mut c_void, QString)) {
let data = (&*ptr).file_path(row, parent);
set(d, QString::from(&data));
}
#[no_mangle]
pub unsafe extern "C" fn tree_data_file_permissions(ptr: *const Tree, row: c_int, parent: usize) -> c_int {
(&*ptr).file_permissions(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn tree_index(ptr: *const Tree, row: c_int, parent: usize) -> usize {
(&*ptr).index(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn tree_parent(ptr: *const Tree, row: c_int, parent: usize) -> QModelIndex {
(&*ptr).parent(row, parent)
}

View File

@ -92,21 +92,21 @@ impl DirectoryTrait for Directory {
0
}
}
pub struct Tree {
emit: TreeEmitter,
model: TreeUniformTree,
pub struct TestTree {
emit: TestTreeEmitter,
model: TestTreeUniformTree,
path: String,
}
impl TreeTrait for Tree {
fn create(emit: TreeEmitter, model: TreeUniformTree) -> Tree {
Tree {
impl TestTreeTrait for TestTree {
fn create(emit: TestTreeEmitter, model: TestTreeUniformTree) -> TestTree {
TestTree {
emit: emit,
model: model,
path: String::new(),
}
}
fn emit(&self) -> &TreeEmitter {
fn emit(&self) -> &TestTreeEmitter {
&self.emit
}
fn get_path(&self) -> String {
@ -131,7 +131,7 @@ impl TreeTrait for Tree {
fn file_permissions(&self, row: c_int, parent: usize) -> c_int {
0
}
fn index(&mut self, row: c_int, parent: usize) -> usize {
fn index(&self, row: c_int, parent: usize) -> usize {
0
}
fn parent(&self, row: c_int, parent: usize) -> QModelIndex {

View File

@ -277,17 +277,17 @@ pub unsafe extern "C" fn directory_data_file_permissions(ptr: *const Directory,
(&*ptr).file_permissions(row)
}
pub struct TreeQObject {}
pub struct TestTreeQObject {}
#[derive (Clone)]
pub struct TreeEmitter {
qobject: Arc<Mutex<*const TreeQObject>>,
path_changed: fn(*const TreeQObject),
pub struct TestTreeEmitter {
qobject: Arc<Mutex<*const TestTreeQObject>>,
path_changed: fn(*const TestTreeQObject),
}
unsafe impl Send for TreeEmitter {}
unsafe impl Send for TestTreeEmitter {}
impl TreeEmitter {
impl TestTreeEmitter {
fn clear(&self) {
*self.qobject.lock().unwrap() = null();
}
@ -299,32 +299,32 @@ impl TreeEmitter {
}
}
pub struct TreeUniformTree {
qobject: *const TreeQObject,
tree_begin_insert_rows: fn(*const TreeQObject,row: c_int, parent: usize, c_int, c_int),
tree_end_insert_rows: fn(*const TreeQObject),
tree_begin_remove_rows: fn(*const TreeQObject,row: c_int, parent: usize, c_int, c_int),
tree_end_remove_rows: fn(*const TreeQObject),
pub struct TestTreeUniformTree {
qobject: *const TestTreeQObject,
test_tree_begin_insert_rows: fn(*const TestTreeQObject,row: c_int, parent: usize, c_int, c_int),
test_tree_end_insert_rows: fn(*const TestTreeQObject),
test_tree_begin_remove_rows: fn(*const TestTreeQObject,row: c_int, parent: usize, c_int, c_int),
test_tree_end_remove_rows: fn(*const TestTreeQObject),
}
impl TreeUniformTree {
pub fn tree_begin_insert_rows(&self,row: c_int, parent: usize, first: c_int, last: c_int) {
(self.tree_begin_insert_rows)(self.qobject,row, parent, first, last);
impl TestTreeUniformTree {
pub fn test_tree_begin_insert_rows(&self,row: c_int, parent: usize, first: c_int, last: c_int) {
(self.test_tree_begin_insert_rows)(self.qobject,row, parent, first, last);
}
pub fn tree_end_insert_rows(&self) {
(self.tree_end_insert_rows)(self.qobject);
pub fn test_tree_end_insert_rows(&self) {
(self.test_tree_end_insert_rows)(self.qobject);
}
pub fn tree_begin_remove_rows(&self,row: c_int, parent: usize, first: c_int, last: c_int) {
(self.tree_begin_remove_rows)(self.qobject,row, parent, first, last);
pub fn test_tree_begin_remove_rows(&self,row: c_int, parent: usize, first: c_int, last: c_int) {
(self.test_tree_begin_remove_rows)(self.qobject,row, parent, first, last);
}
pub fn tree_end_remove_rows(&self) {
(self.tree_end_remove_rows)(self.qobject);
pub fn test_tree_end_remove_rows(&self) {
(self.test_tree_end_remove_rows)(self.qobject);
}
}
pub trait TreeTrait {
fn create(emit: TreeEmitter, model: TreeUniformTree) -> Self;
fn emit(&self) -> &TreeEmitter;
pub trait TestTreeTrait {
fn create(emit: TestTreeEmitter, model: TestTreeUniformTree) -> Self;
fn emit(&self) -> &TestTreeEmitter;
fn get_path(&self) -> String;
fn set_path(&mut self, value: String);
fn row_count(&self, row: c_int, parent: usize) -> c_int;
@ -334,44 +334,44 @@ pub trait TreeTrait {
fn file_icon(&self, row: c_int, parent: usize) -> Vec<u8>;
fn file_path(&self, row: c_int, parent: usize) -> String;
fn file_permissions(&self, row: c_int, parent: usize) -> c_int;
fn index(&mut self, row: c_int, parent: usize) -> usize;
fn index(&self, row: c_int, parent: usize) -> usize;
fn parent(&self, row: c_int, parent: usize) -> QModelIndex;
}
#[no_mangle]
pub extern "C" fn tree_new(qobject: *const TreeQObject,
path_changed: fn(*const TreeQObject),
tree_begin_insert_rows: fn(*const TreeQObject,row: c_int, parent: usize,
pub extern "C" fn test_tree_new(qobject: *const TestTreeQObject,
path_changed: fn(*const TestTreeQObject),
test_tree_begin_insert_rows: fn(*const TestTreeQObject,row: c_int, parent: usize,
c_int,
c_int),
tree_end_insert_rows: fn(*const TreeQObject),
tree_begin_remove_rows: fn(*const TreeQObject,row: c_int, parent: usize,
test_tree_end_insert_rows: fn(*const TestTreeQObject),
test_tree_begin_remove_rows: fn(*const TestTreeQObject,row: c_int, parent: usize,
c_int,
c_int),
tree_end_remove_rows: fn(*const TreeQObject))
-> *mut Tree {
let emit = TreeEmitter {
test_tree_end_remove_rows: fn(*const TestTreeQObject))
-> *mut TestTree {
let emit = TestTreeEmitter {
qobject: Arc::new(Mutex::new(qobject)),
path_changed: path_changed,
};
let model = TreeUniformTree {
let model = TestTreeUniformTree {
qobject: qobject,
tree_begin_insert_rows: tree_begin_insert_rows,
tree_end_insert_rows: tree_end_insert_rows,
tree_begin_remove_rows: tree_begin_remove_rows,
tree_end_remove_rows: tree_end_remove_rows,
test_tree_begin_insert_rows: test_tree_begin_insert_rows,
test_tree_end_insert_rows: test_tree_end_insert_rows,
test_tree_begin_remove_rows: test_tree_begin_remove_rows,
test_tree_end_remove_rows: test_tree_end_remove_rows,
};
let d = Tree::create(emit, model);
let d = TestTree::create(emit, model);
Box::into_raw(Box::new(d))
}
#[no_mangle]
pub unsafe extern "C" fn tree_free(ptr: *mut Tree) {
pub unsafe extern "C" fn test_tree_free(ptr: *mut TestTree) {
Box::from_raw(ptr).emit().clear();
}
#[no_mangle]
pub unsafe extern "C" fn tree_path_get(ptr: *const Tree,
pub unsafe extern "C" fn test_tree_path_get(ptr: *const TestTree,
p: *mut c_void,
set: fn(*mut c_void, QString)) {
let data = (&*ptr).get_path();
@ -379,25 +379,25 @@ pub unsafe extern "C" fn tree_path_get(ptr: *const Tree,
}
#[no_mangle]
pub unsafe extern "C" fn tree_path_set(ptr: *mut Tree, v: QStringIn) {
pub unsafe extern "C" fn test_tree_path_set(ptr: *mut TestTree, v: QStringIn) {
(&mut *ptr).set_path(v.convert());
}
#[no_mangle]
pub unsafe extern "C" fn tree_row_count(ptr: *const Tree, row: c_int, parent: usize) -> c_int {
pub unsafe extern "C" fn test_tree_row_count(ptr: *const TestTree, row: c_int, parent: usize) -> c_int {
(&*ptr).row_count(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn tree_can_fetch_more(ptr: *const Tree, row: c_int, parent: usize) -> bool {
pub unsafe extern "C" fn test_tree_can_fetch_more(ptr: *const TestTree, row: c_int, parent: usize) -> bool {
(&*ptr).can_fetch_more(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn tree_fetch_more(ptr: *mut Tree, row: c_int, parent: usize) {
pub unsafe extern "C" fn test_tree_fetch_more(ptr: *mut TestTree, row: c_int, parent: usize) {
(&mut *ptr).fetch_more(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn tree_data_file_name(ptr: *const Tree,
pub unsafe extern "C" fn test_tree_data_file_name(ptr: *const TestTree,
row: c_int, parent: usize,
d: *mut c_void,
set: fn(*mut c_void, QString)) {
@ -406,7 +406,7 @@ pub unsafe extern "C" fn tree_data_file_name(ptr: *const Tree,
}
#[no_mangle]
pub unsafe extern "C" fn tree_data_file_icon(ptr: *const Tree,
pub unsafe extern "C" fn test_tree_data_file_icon(ptr: *const TestTree,
row: c_int, parent: usize,
d: *mut c_void,
set: fn(*mut c_void, QByteArray)) {
@ -415,7 +415,7 @@ pub unsafe extern "C" fn tree_data_file_icon(ptr: *const Tree,
}
#[no_mangle]
pub unsafe extern "C" fn tree_data_file_path(ptr: *const Tree,
pub unsafe extern "C" fn test_tree_data_file_path(ptr: *const TestTree,
row: c_int, parent: usize,
d: *mut c_void,
set: fn(*mut c_void, QString)) {
@ -424,15 +424,15 @@ pub unsafe extern "C" fn tree_data_file_path(ptr: *const Tree,
}
#[no_mangle]
pub unsafe extern "C" fn tree_data_file_permissions(ptr: *const Tree, row: c_int, parent: usize) -> c_int {
pub unsafe extern "C" fn test_tree_data_file_permissions(ptr: *const TestTree, row: c_int, parent: usize) -> c_int {
(&*ptr).file_permissions(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn tree_index(ptr: *mut Tree, row: c_int, parent: usize) -> usize {
(&mut *ptr).index(row, parent)
pub unsafe extern "C" fn test_tree_index(ptr: *const TestTree, row: c_int, parent: usize) -> usize {
(&*ptr).index(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn tree_parent(ptr: *const Tree, row: c_int, parent: usize) -> QModelIndex {
pub unsafe extern "C" fn test_tree_parent(ptr: *const TestTree, row: c_int, parent: usize) -> QModelIndex {
(&*ptr).parent(row, parent)
}

View File

@ -1,5 +1,5 @@
/* generated by rust_qt_binding_generator */
#include "tmp.h"
#include "Bindings.h"
namespace {
struct qbytearray_t {
@ -60,14 +60,14 @@ extern "C" {
void directory_free(DirectoryInterface*);
void directory_path_get(DirectoryInterface*, QString*, qstring_set);
void directory_path_set(void*, qstring_t);
TreeInterface* tree_new(Tree*, void (*)(Tree*),
void (*)(Tree*, int, quintptr, int, int),
void (*)(Tree*),
void (*)(Tree*, int, quintptr, int, int),
void (*)(Tree*));
void tree_free(TreeInterface*);
void tree_path_get(TreeInterface*, QString*, qstring_set);
void tree_path_set(void*, qstring_t);
TestTreeInterface* test_tree_new(TestTree*, void (*)(TestTree*),
void (*)(TestTree*, int, quintptr, int, int),
void (*)(TestTree*),
void (*)(TestTree*, int, quintptr, int, int),
void (*)(TestTree*));
void test_tree_free(TestTreeInterface*);
void test_tree_path_get(TestTreeInterface*, QString*, qstring_set);
void test_tree_path_set(void*, qstring_t);
};
Person::Person(QObject *parent):
QObject(parent),
@ -140,13 +140,13 @@ void Directory::setPath(const QString& v) {
directory_path_set(d, v);
}
extern "C" {
void directory_data_file_name(DirectoryInterface*, int, QString*, qstring_set);
void directory_data_file_icon(DirectoryInterface*, int, QByteArray*, qbytearray_set);
void directory_data_file_path(DirectoryInterface*, int, QString*, qstring_set);
int directory_data_file_permissions(DirectoryInterface*, int);
void directory_data_file_name(const DirectoryInterface*, int, QString*, qstring_set);
void directory_data_file_icon(const DirectoryInterface*, int, QByteArray*, qbytearray_set);
void directory_data_file_path(const DirectoryInterface*, int, QString*, qstring_set);
int directory_data_file_permissions(const DirectoryInterface*, int);
int directory_row_count(DirectoryInterface*);
bool directory_can_fetch_more(DirectoryInterface*);
int directory_row_count(const DirectoryInterface*);
bool directory_can_fetch_more(const DirectoryInterface*);
void directory_fetch_more(DirectoryInterface*);
}
int Directory::columnCount(const QModelIndex &parent) const
@ -240,84 +240,84 @@ QHash<int, QByteArray> Directory::roleNames() const {
return names;
}
Tree::Tree(QObject *parent):
TestTree::TestTree(QObject *parent):
QAbstractItemModel(parent),
d(tree_new(this,
[](Tree* o) { emit o->pathChanged(); },
[](Tree* o, int row, quintptr id, int first, int last) {
d(test_tree_new(this,
[](TestTree* o) { emit o->pathChanged(); },
[](TestTree* o, int row, quintptr id, int first, int last) {
emit o->beginInsertRows(o->createIndex(row, 0, id), first, last);
},
[](Tree* o) {
[](TestTree* o) {
emit o->endInsertRows();
},
[](Tree* o, int row, quintptr id, int first, int last) {
[](TestTree* o, int row, quintptr id, int first, int last) {
emit o->beginRemoveRows(o->createIndex(row, 0, id), first, last);
},
[](Tree* o) {
[](TestTree* o) {
emit o->endRemoveRows();
}
)) {}
Tree::~Tree() {
tree_free(d);
TestTree::~TestTree() {
test_tree_free(d);
}
QString Tree::path() const
QString TestTree::path() const
{
QString v;
tree_path_get(d, &v, set_qstring);
test_tree_path_get(d, &v, set_qstring);
return v;
}
void Tree::setPath(const QString& v) {
tree_path_set(d, v);
void TestTree::setPath(const QString& v) {
test_tree_path_set(d, v);
}
extern "C" {
void tree_data_file_name(TreeInterface*, int, quintptr, QString*, qstring_set);
void tree_data_file_icon(TreeInterface*, int, quintptr, QByteArray*, qbytearray_set);
void tree_data_file_path(TreeInterface*, int, quintptr, QString*, qstring_set);
int tree_data_file_permissions(TreeInterface*, int, quintptr);
void test_tree_data_file_name(const TestTreeInterface*, int, quintptr, QString*, qstring_set);
void test_tree_data_file_icon(const TestTreeInterface*, int, quintptr, QByteArray*, qbytearray_set);
void test_tree_data_file_path(const TestTreeInterface*, int, quintptr, QString*, qstring_set);
int test_tree_data_file_permissions(const TestTreeInterface*, int, quintptr);
int tree_row_count(TreeInterface*, int, quintptr);
bool tree_can_fetch_more(TreeInterface*, int, quintptr);
void tree_fetch_more(TreeInterface*, int, quintptr);
quintptr tree_index(TreeInterface*, int, quintptr);
qmodelindex_t tree_parent(TreeInterface*, int, quintptr);
int test_tree_row_count(const TestTreeInterface*, int, quintptr);
bool test_tree_can_fetch_more(const TestTreeInterface*, int, quintptr);
void test_tree_fetch_more(TestTreeInterface*, int, quintptr);
quintptr test_tree_index(const TestTreeInterface*, int, quintptr);
qmodelindex_t test_tree_parent(const TestTreeInterface*, int, quintptr);
}
int Tree::columnCount(const QModelIndex &) const
int TestTree::columnCount(const QModelIndex &) const
{
return 3;
}
int Tree::rowCount(const QModelIndex &parent) const
int TestTree::rowCount(const QModelIndex &parent) const
{
return tree_row_count(d, parent.row(), parent.internalId());
return test_tree_row_count(d, parent.row(), parent.internalId());
}
QModelIndex Tree::index(int row, int column, const QModelIndex &parent) const
QModelIndex TestTree::index(int row, int column, const QModelIndex &parent) const
{
const quintptr id = tree_index(d, parent.row(), parent.internalId());
const quintptr id = test_tree_index(d, parent.row(), parent.internalId());
return id ?createIndex(row, column, id) :QModelIndex();
}
QModelIndex Tree::parent(const QModelIndex &index) const
QModelIndex TestTree::parent(const QModelIndex &index) const
{
if (!index.isValid()) {
return QModelIndex();
}
const qmodelindex_t parent = tree_parent(d, index.row(), index.internalId());
const qmodelindex_t parent = test_tree_parent(d, index.row(), index.internalId());
return parent.id ?createIndex(parent.row, 0, parent.id) :QModelIndex();
}
bool Tree::canFetchMore(const QModelIndex &parent) const
bool TestTree::canFetchMore(const QModelIndex &parent) const
{
return tree_can_fetch_more(d, parent.row(), parent.internalId());
return test_tree_can_fetch_more(d, parent.row(), parent.internalId());
}
void Tree::fetchMore(const QModelIndex &parent)
void TestTree::fetchMore(const QModelIndex &parent)
{
tree_fetch_more(d, parent.row(), parent.internalId());
test_tree_fetch_more(d, parent.row(), parent.internalId());
}
QVariant Tree::data(const QModelIndex &index, int role) const
QVariant TestTree::data(const QModelIndex &index, int role) const
{
QVariant v;
QString s;
@ -326,30 +326,30 @@ QVariant Tree::data(const QModelIndex &index, int role) const
case 0:
switch (role) {
case Qt::DisplayRole:
tree_data_file_name(d, index.row(), index.internalId(), &s, set_qstring);
test_tree_data_file_name(d, index.row(), index.internalId(), &s, set_qstring);
v.setValue<QString>(s);
break;
case Qt::DecorationRole:
tree_data_file_icon(d, index.row(), index.internalId(), &b, set_qbytearray);
test_tree_data_file_icon(d, index.row(), index.internalId(), &b, set_qbytearray);
v.setValue<QByteArray>(b);
break;
case Qt::UserRole + 1:
tree_data_file_path(d, index.row(), index.internalId(), &s, set_qstring);
test_tree_data_file_path(d, index.row(), index.internalId(), &s, set_qstring);
v.setValue<QString>(s);
break;
case Qt::UserRole + 2:
tree_data_file_name(d, index.row(), index.internalId(), &s, set_qstring);
test_tree_data_file_name(d, index.row(), index.internalId(), &s, set_qstring);
v.setValue<QString>(s);
break;
case Qt::UserRole + 3:
v.setValue<int>(tree_data_file_permissions(d, index.row(), index.internalId()));
v.setValue<int>(test_tree_data_file_permissions(d, index.row(), index.internalId()));
break;
}
break;
case 1:
switch (role) {
case Qt::DisplayRole:
tree_data_file_path(d, index.row(), index.internalId(), &s, set_qstring);
test_tree_data_file_path(d, index.row(), index.internalId(), &s, set_qstring);
v.setValue<QString>(s);
break;
}
@ -357,14 +357,14 @@ QVariant Tree::data(const QModelIndex &index, int role) const
case 2:
switch (role) {
case Qt::DisplayRole:
v.setValue<int>(tree_data_file_permissions(d, index.row(), index.internalId()));
v.setValue<int>(test_tree_data_file_permissions(d, index.row(), index.internalId()));
break;
}
break;
}
return v;
}
QHash<int, QByteArray> Tree::roleNames() const {
QHash<int, QByteArray> TestTree::roleNames() const {
QHash<int, QByteArray> names;
names.insert(Qt::DisplayRole, "FileName");
names.insert(Qt::DecorationRole, "FileIcon");

View File

@ -1,6 +1,6 @@
/* generated by rust_qt_binding_generator */
#ifndef TMP_H
#define TMP_H
#ifndef BINDINGS_H
#define BINDINGS_H
#include <QObject>
#include <QAbstractItemModel>
@ -64,15 +64,15 @@ private:
QString m_path;
};
class TreeInterface;
class Tree : public QAbstractItemModel
class TestTreeInterface;
class TestTree : public QAbstractItemModel
{
Q_OBJECT
TreeInterface * const d;
TestTreeInterface * const d;
Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged FINAL)
public:
explicit Tree(QObject *parent = nullptr);
~Tree();
explicit TestTree(QObject *parent = nullptr);
~TestTree();
QString path() const;
void setPath(const QString& v);
@ -91,4 +91,4 @@ signals:
private:
QString m_path;
};
#endif // TMP_H
#endif // BINDINGS_H

View File

@ -1,175 +0,0 @@
#include "DemoObject.h"
#include <QSize>
#include <QColor>
#include <QDebug>
#include <cstdint>
#include <unistd.h>
namespace {
struct qbytearray_t {
private:
const char* data;
int len;
public:
operator QByteArray() const {
return QByteArray(data, len);
}
};
struct qstring_t {
private:
const char* data;
int len;
public:
operator QString() const {
return QString::fromUtf8(data, len);
}
};
struct qmodelindex_t {
int row;
quintptr id;
};
}
typedef void (*qstring_set)(QString*, qstring_t*);
void set_qstring(QString* v, qstring_t* val) {
*v = *val;
}
extern "C" {
DemoObjectInterface* hello_new(DemoObject*, void (*)(DemoObject*));
void hello_free(DemoObjectInterface*);
void hello_set(DemoObjectInterface*, const uint16_t *, size_t);
qstring_t hello_get(DemoObjectInterface*);
RItemModelInterface* ritemmodel_new(RItemModel*, void (*)(RItemModel*));
void ritemmodel_free(RItemModelInterface*);
int ritemmodel_row_count(RItemModelInterface*, int, quintptr);
quintptr ritemmodel_index(RItemModelInterface*, int, quintptr);
qmodelindex_t ritemmodel_parent(RItemModelInterface*, int, quintptr);
void ritemmodel_data_file_name(RItemModelInterface*, int, quintptr, QString*, qstring_set);
int ritemmodel_data_file_permissions(RItemModelInterface*, int, quintptr);
}
DemoObject::DemoObject(QObject *parent):
QObject(parent),
d(hello_new(this,
[](DemoObject* o) { emit o->userNameChanged(); }
))
{
}
DemoObject::~DemoObject() {
hello_free(d);
}
QString
DemoObject::userName() const {
return hello_get(d);
}
void
DemoObject::setUserName(const QString& name) {
hello_set(d, name.utf16(), name.size());
}
const QVariantMap&
DemoObject::tree() const {
return m_tree;
}
void
DemoObject::setTree(const QVariantMap& tree) {
m_tree = tree;
emit treeChanged();
}
RItemModel::RItemModel(QObject *parent):
QAbstractItemModel(parent),
d(ritemmodel_new(this,
[](RItemModel* o) { emit o->newDataReady(); }
))
{
connect(this, &RItemModel::newDataReady, this, &RItemModel::handleNewData,
Qt::QueuedConnection);
qDebug() << sizeof(QObject) << sizeof(RItemModel);
qDebug() << setHeaderData(0, Qt::Horizontal, "FileIcon", Qt::DecorationRole);
setHeaderData(0, Qt::Horizontal, "FilePath", Qt::UserRole + 1);
setHeaderData(0, Qt::Horizontal, "FileName", Qt::UserRole + 2);
setHeaderData(0, Qt::Horizontal, "FilePermissions", Qt::UserRole + 3);
}
QVariant RItemModel::headerData(int section, Qt::Orientation orientation, int role) const {
if (role == Qt::DisplayRole) {
return QVariant("hello");
}
if (role == Qt::BackgroundRole) {
return QVariant(QColor(Qt::green));
}
if (role == Qt::FontRole) {
return QVariant("Times");
}
return QVariant();
}
RItemModel::~RItemModel() {
ritemmodel_free(d);
}
void RItemModel::handleNewData() {
qDebug() << "new data!";
}
int RItemModel::columnCount(const QModelIndex &) const
{
return 2;
}
int RItemModel::rowCount(const QModelIndex &parent) const
{
return ritemmodel_row_count(d, parent.row(), parent.internalId());
}
QVariant RItemModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::BackgroundRole) {
return QVariant(QColor(Qt::green));
}
if (role == Qt::FontRole) {
return QVariant("Times");
}
QVariant v;
if (index.column() == 0) {
if (role == Qt::DisplayRole || role == Qt::UserRole + 2) {
QString s;
ritemmodel_data_file_name(d, index.row(), index.internalId(), &s, set_qstring);
v = s;
} else if (role == Qt::UserRole + 3) {
v.setValue<int>(ritemmodel_data_file_permissions(d, index.row(), index.internalId()));
}
} else if (index.column() == 1) {
if (role == Qt::DisplayRole) {
v.setValue<int>(ritemmodel_data_file_permissions(d, index.row(), index.internalId()));
}
}
return v;
}
QModelIndex RItemModel::index(int row, int column, const QModelIndex &parent) const
{
const quintptr id = ritemmodel_index(d, parent.row(), parent.internalId());
return id ?createIndex(row, column, id) :QModelIndex();
}
QModelIndex RItemModel::parent(const QModelIndex &index) const
{
if (!index.isValid()) {
return QModelIndex();
}
const qmodelindex_t parent = ritemmodel_parent(d, index.row(), index.internalId());
return parent.id ?createIndex(parent.row, 0, parent.id) :QModelIndex();
}
QHash<int, QByteArray> RItemModel::roleNames() const {
QHash<int, QByteArray> names;
names.insert(Qt::DecorationRole, "FileIcon");
names.insert(Qt::UserRole + 1, "FilePath");
names.insert(Qt::UserRole + 2, "FileName");
names.insert(Qt::UserRole + 3, "FilePermissions");
return names;
}

View File

@ -1,58 +0,0 @@
#ifndef DEMO_OBJECT_H
#define DEMO_OBJECT_H
#include <QObject>
#include <QString>
#include <QVariantMap>
#include <QAbstractItemModel>
class DemoObjectInterface;
class DemoObject : public QObject
{
Q_OBJECT
DemoObjectInterface* d;
QVariantMap m_tree;
Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged FINAL)
Q_PROPERTY(QVariantMap tree READ tree WRITE setTree NOTIFY treeChanged FINAL)
public:
explicit DemoObject(QObject *parent = nullptr);
~DemoObject();
QString userName() const;
void setUserName(const QString &userName);
const QVariantMap& tree() const;
void setTree(const QVariantMap &tree);
signals:
void userNameChanged();
void treeChanged();
private:
QString m_userName;
};
class RItemModelInterface;
class RItemModel : public QAbstractItemModel {
Q_OBJECT
RItemModelInterface* d;
public:
explicit RItemModel(QObject *parent = nullptr);
~RItemModel();
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QModelIndex index(int row, int column, const QModelIndex &parent) const;
QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
QHash<int, QByteArray> roleNames() const;
private slots:
void handleNewData();
signals:
void newDataReady();
};
#endif // DEMO_OBJECT_H

187
demo/src/Tree.cpp Normal file
View File

@ -0,0 +1,187 @@
/* generated by rust_qt_binding_generator */
#include "Tree.h"
namespace {
struct qbytearray_t {
private:
const char* data;
int len;
public:
qbytearray_t(const QByteArray& v):
data(v.data()),
len(v.size()) {
}
operator QByteArray() const {
return QByteArray(data, len);
}
};
struct qstring_t {
private:
const void* data;
int len;
public:
qstring_t(const QString& v):
data(static_cast<const void*>(v.utf16())),
len(v.size()) {
}
operator QString() const {
return QString::fromUtf8(static_cast<const char*>(data), len);
}
};
struct qmodelindex_t {
int row;
quintptr id;
};
}
typedef void (*qstring_set)(QString*, qstring_t*);
void set_qstring(QString* v, qstring_t* val) {
*v = *val;
}
typedef void (*qbytearray_set)(QByteArray*, qbytearray_t*);
void set_qbytearray(QByteArray* v, qbytearray_t* val) {
*v = *val;
}
extern "C" {
TreeInterface* tree_new(Tree*, void (*)(Tree*),
void (*)(Tree*, int, quintptr, int, int),
void (*)(Tree*),
void (*)(Tree*, int, quintptr, int, int),
void (*)(Tree*));
void tree_free(TreeInterface*);
void tree_path_get(TreeInterface*, QString*, qstring_set);
void tree_path_set(void*, qstring_t);
};
Tree::Tree(QObject *parent):
QAbstractItemModel(parent),
d(tree_new(this,
[](Tree* o) { emit o->pathChanged(); },
[](Tree* o, int row, quintptr id, int first, int last) {
emit o->beginInsertRows(o->createIndex(row, 0, id), first, last);
},
[](Tree* o) {
emit o->endInsertRows();
},
[](Tree* o, int row, quintptr id, int first, int last) {
emit o->beginRemoveRows(o->createIndex(row, 0, id), first, last);
},
[](Tree* o) {
emit o->endRemoveRows();
}
)) {}
Tree::~Tree() {
tree_free(d);
}
QString Tree::path() const
{
QString v;
tree_path_get(d, &v, set_qstring);
return v;
}
void Tree::setPath(const QString& v) {
tree_path_set(d, v);
}
extern "C" {
void tree_data_file_name(const TreeInterface*, int, quintptr, QString*, qstring_set);
void tree_data_file_icon(const TreeInterface*, int, quintptr, QByteArray*, qbytearray_set);
void tree_data_file_path(const TreeInterface*, int, quintptr, QString*, qstring_set);
int tree_data_file_permissions(const TreeInterface*, int, quintptr);
int tree_row_count(const TreeInterface*, int, quintptr);
bool tree_can_fetch_more(const TreeInterface*, int, quintptr);
void tree_fetch_more(TreeInterface*, int, quintptr);
quintptr tree_index(const TreeInterface*, int, quintptr);
qmodelindex_t tree_parent(const TreeInterface*, int, quintptr);
}
int Tree::columnCount(const QModelIndex &) const
{
return 3;
}
int Tree::rowCount(const QModelIndex &parent) const
{
return tree_row_count(d, parent.row(), parent.internalId());
}
QModelIndex Tree::index(int row, int column, const QModelIndex &parent) const
{
const quintptr id = tree_index(d, parent.row(), parent.internalId());
return id ?createIndex(row, column, id) :QModelIndex();
}
QModelIndex Tree::parent(const QModelIndex &index) const
{
if (!index.isValid()) {
return QModelIndex();
}
const qmodelindex_t parent = tree_parent(d, index.row(), index.internalId());
return parent.id ?createIndex(parent.row, 0, parent.id) :QModelIndex();
}
bool Tree::canFetchMore(const QModelIndex &parent) const
{
return tree_can_fetch_more(d, parent.row(), parent.internalId());
}
void Tree::fetchMore(const QModelIndex &parent)
{
tree_fetch_more(d, parent.row(), parent.internalId());
}
QVariant Tree::data(const QModelIndex &index, int role) const
{
QVariant v;
QString s;
QByteArray b;
switch (index.column()) {
case 0:
switch (role) {
case Qt::DisplayRole:
tree_data_file_name(d, index.row(), index.internalId(), &s, set_qstring);
v.setValue<QString>(s);
break;
case Qt::DecorationRole:
tree_data_file_icon(d, index.row(), index.internalId(), &b, set_qbytearray);
v.setValue<QByteArray>(b);
break;
case Qt::UserRole + 1:
tree_data_file_path(d, index.row(), index.internalId(), &s, set_qstring);
v.setValue<QString>(s);
break;
case Qt::UserRole + 2:
tree_data_file_name(d, index.row(), index.internalId(), &s, set_qstring);
v.setValue<QString>(s);
break;
case Qt::UserRole + 3:
v.setValue<int>(tree_data_file_permissions(d, index.row(), index.internalId()));
break;
}
break;
case 1:
switch (role) {
case Qt::DisplayRole:
tree_data_file_path(d, index.row(), index.internalId(), &s, set_qstring);
v.setValue<QString>(s);
break;
}
break;
case 2:
switch (role) {
case Qt::DisplayRole:
v.setValue<int>(tree_data_file_permissions(d, index.row(), index.internalId()));
break;
}
break;
}
return v;
}
QHash<int, QByteArray> Tree::roleNames() const {
QHash<int, QByteArray> names;
names.insert(Qt::DisplayRole, "FileName");
names.insert(Qt::DecorationRole, "FileIcon");
names.insert(Qt::UserRole + 1, "FilePath");
names.insert(Qt::UserRole + 3, "FilePermissions");
return names;
}

35
demo/src/Tree.h Normal file
View File

@ -0,0 +1,35 @@
/* generated by rust_qt_binding_generator */
#ifndef TREE_H
#define TREE_H
#include <QObject>
#include <QAbstractItemModel>
class TreeInterface;
class Tree : public QAbstractItemModel
{
Q_OBJECT
TreeInterface * const d;
Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged FINAL)
public:
explicit Tree(QObject *parent = nullptr);
~Tree();
QString path() const;
void setPath(const QString& v);
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QModelIndex index(int row, int column, const QModelIndex &parent) const;
QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent) const;
bool canFetchMore(const QModelIndex &parent) const;
void fetchMore(const QModelIndex &parent);
QHash<int, QByteArray> roleNames() const;
signals:
void newDataReady();
signals:
void pathChanged();
private:
QString m_path;
};
#endif // TREE_H

View File

@ -1,5 +1,5 @@
#include "DemoObject.h"
#include "tmp.h"
#include "Tree.h"
#include "Bindings.h"
#include <cstdlib>
#include <KAboutData>
@ -55,7 +55,7 @@ int main (int argc, char *argv[])
QQmlApplicationEngine engine;
RItemModel model;
Tree model;
QTreeView view;
view.setUniformRowHeights(true);
view.setModel(&model);

51
demo/tree.json Normal file
View File

@ -0,0 +1,51 @@
{
"cppFile": "src/Tree.cpp",
"rust": {
"dir": "rust",
"interfaceModule": "interface",
"implementationModule": "implementation",
"typesModule": "types"
},
"objects": [{
"name": "Tree",
"type": "UniformTree",
"properties": [{
"name": "path",
"type": "QString",
"write": true
}],
"roles": [
[{
"name": "FileName",
"value": "Qt::DisplayRole",
"type": "QString"
}, {
"name": "FileIcon",
"value": "Qt::DecorationRole",
"type": "QByteArray"
}, {
"name": "FilePath",
"value": "Qt::UserRole + 1",
"type": "QString"
}, {
"name": "FileName",
"value": "Qt::UserRole + 2",
"type": "QString"
}, {
"name": "FilePermissions",
"value": "Qt::UserRole + 3",
"type": "int"
}],
[{
"name": "FilePath",
"value": "Qt::DisplayRole",
"type": "QString"
}],
[{
"name": "FilePermissions",
"value": "Qt::DisplayRole",
"type": "int"
}]
]
}]
}

View File

@ -289,17 +289,17 @@ void writeCppModel(QTextStream& cpp, const Object& o) {
cpp << "extern \"C\" {\n";
for (auto role: o.allRoles) {
if (role.type.isComplex()) {
cpp << QString(" void %2_data_%3(%1Interface*%5, %4);\n")
cpp << QString(" void %2_data_%3(const %1Interface*%5, %4);\n")
.arg(o.name, lcname, snakeCase(role.name), cGetType(role.type), indexDecl);
} else {
cpp << QString(" %4 %2_data_%3(%1Interface*%5);\n")
cpp << QString(" %4 %2_data_%3(const %1Interface*%5);\n")
.arg(o.name, lcname, snakeCase(role.name), role.type.name, indexDecl);
}
}
if (o.type == ObjectType::List) {
cpp << QString(R"(
int %2_row_count(%1Interface*);
bool %2_can_fetch_more(%1Interface*);
int %2_row_count(const %1Interface*);
bool %2_can_fetch_more(const %1Interface*);
void %2_fetch_more(%1Interface*);
}
int %1::columnCount(const QModelIndex &parent) const
@ -339,11 +339,11 @@ void %1::fetchMore(const QModelIndex &parent)
)").arg(o.name, lcname, QString::number(o.columnRoles.size()));
} else {
cpp << QString(R"(
int %2_row_count(%1Interface*, int, quintptr);
bool %2_can_fetch_more(%1Interface*, int, quintptr);
int %2_row_count(const %1Interface*, int, quintptr);
bool %2_can_fetch_more(const %1Interface*, int, quintptr);
void %2_fetch_more(%1Interface*, int, quintptr);
quintptr %2_index(%1Interface*, int, quintptr);
qmodelindex_t %2_parent(%1Interface*, int, quintptr);
quintptr %2_index(const %1Interface*, int, quintptr);
qmodelindex_t %2_parent(const %1Interface*, int, quintptr);
}
int %1::columnCount(const QModelIndex &) const
{
@ -784,7 +784,7 @@ pub trait %1Trait {
}
}
if (o.type == ObjectType::UniformTree) {
r << " fn index(&mut self, row: c_int, parent: usize) -> usize;\n";
r << " fn index(&self, row: c_int, parent: usize) -> usize;\n";
r << " fn parent(&self, row: c_int, parent: usize) -> QModelIndex;\n";
}
@ -931,8 +931,8 @@ pub unsafe extern "C" fn %2_data_%3(ptr: *const %1, row: c_int%5) -> %4 {
if (o.type == ObjectType::UniformTree) {
r << QString(R"(
#[no_mangle]
pub unsafe extern "C" fn %2_index(ptr: *mut %1, row: c_int, parent: usize) -> usize {
(&mut *ptr).index(row, parent)
pub unsafe extern "C" fn %2_index(ptr: *const %1, row: c_int, parent: usize) -> usize {
(&*ptr).index(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn %2_parent(ptr: *const %1, row: c_int, parent: usize) -> QModelIndex {
@ -1037,7 +1037,7 @@ void writeRustImplementationObject(QTextStream& r, const Object& o) {
}
}
if (o.type == ObjectType::UniformTree) {
r << R"( fn index(&mut self, row: c_int, parent: usize) -> usize {
r << R"( fn index(&self, row: c_int, parent: usize) -> usize {
0
}
fn parent(&self, row: c_int, parent: usize) -> QModelIndex {