Add tabs with example to the Demo app

master
Jos van den Oever 2017-08-17 00:11:08 +02:00
parent 106c7ae468
commit 822319764f
24 changed files with 790 additions and 1203 deletions

View File

@ -1,18 +1,18 @@
set(CMAKE_AUTORCC ON)
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/Bindings.h"
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/rust/src/fibonacci_interface.rs"
"${CMAKE_CURRENT_SOURCE_DIR}/rust/src/fibonacci_types.rs"
"${CMAKE_CURRENT_SOURCE_DIR}/src/Fibonacci.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
COMMAND ${CMAKE_BINARY_DIR}/rust_qt_binding_generator/rust_qt_binding_generator "${CMAKE_CURRENT_SOURCE_DIR}/fibonacci.json"
DEPENDS rust_qt_binding_generator fibonacci.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"
@ -24,8 +24,9 @@ add_custom_command(
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/rust/${RUST_TARGET_DIR}/librust.a"
COMMAND cargo build ${RUST_BUILD_FLAG}
DEPENDS rust/src/lib.rs
rust/src/testimplementation.rs
rust/src/testinterface.rs
rust/src/fibonacci_interface.rs
rust/src/fibonacci_implementation.rs
rust/src/fibonacci_types.rs
rust/src/implementation.rs
rust/src/interface.rs
rust/src/types.rs
@ -33,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/Tree.cpp src/Bindings.cpp
set(Demo_SRCS src/main.cpp src/Tree.cpp src/Fibonacci.cpp
resource_file.qrc)
add_executable(Demo ${Demo_SRCS})

View File

@ -7,17 +7,6 @@
"typesModule": "types"
},
"objects": [{
"name": "Fibonacci",
"type": "Object",
"properties": [{
"name": "input",
"type": "uint",
"write": true
}, {
"name": "result",
"type": "uint64_t"
}]
}, {
"name": "Directory",
"type": "List",
"properties": [{
@ -45,7 +34,7 @@
}, {
"name": "filePermissions",
"value": "Qt::UserRole + 3",
"type": "int"
"type": "qint32"
}],
[{
"name": "filePath",
@ -55,7 +44,7 @@
[{
"name": "filePermissions",
"value": "Qt::DisplayRole",
"type": "int"
"type": "qint32"
}]
]
}, {
@ -86,7 +75,7 @@
}, {
"name": "filePermissions",
"value": "Qt::UserRole + 3",
"type": "int"
"type": "qint32"
}],
[{
"name": "filePath",
@ -96,7 +85,7 @@
[{
"name": "filePermissions",
"value": "Qt::DisplayRole",
"type": "int"
"type": "qint32"
}]
]
}]

View File

@ -1,49 +1,70 @@
import QtQuick 2.6
import QtQml.Models 2.2
import QtQuick.Controls 1.5
import QtQuick.Layouts 1.3
import rust 1.0
ApplicationWindow {
width: 300
width: 500
height: 480
visible: true
Directory {
id: directory
path: "/"
}
ItemSelectionModel {
id: selectionModel
model: fsModel
}
SplitView {
FibonacciList {
id: fibonacciList
}
TabView {
anchors.fill: parent
orientation: Qt.Horizontal
/*
TreeView {
model: directory
TableViewColumn {
title: "Name"
role: "fileName"
}
TableViewColumn {
title: "Permissions"
role: "filePermissions"
Tab {
title: "object"
RowLayout {
TextField {
id: fibonacciInput
placeholderText: "Your number"
validator: IntValidator {bottom: 0; top: 100;}
}
Text {
text: "The Fibonacci number: " + fibonacci.result
}
Fibonacci {
id: fibonacci
input: parseInt(fibonacciInput.text, 10)
}
}
}
*/
TreeView {
model: sortedFsModel
selection: selectionModel
//selectionMode: SelectionMode.SingleSelection
TableViewColumn {
title: "Name"
role: "fileName"
width: 200
Tab {
title: "list"
TableView {
id: listView
model: fibonacciList
TableViewColumn {
role: "display"
title: "Row"
width: 100
}
}
TableViewColumn {
title: "Size"
role: "fileSize"
width: 100
}
Tab {
title: "tree"
TreeView {
id: treeView
model: sortedFsModel
selection: selectionModel
TableViewColumn {
title: "Name"
role: "fileName"
}
TableViewColumn {
title: "Size"
role: "fileSize"
}
Component.onCompleted: {
var root = treeView.rootIndex;
var first = sortedFsModel.index(0, 0, root);
treeView.expand(first);
}
}
}
}

31
demo/fibonacci.json Normal file
View File

@ -0,0 +1,31 @@
{
"cppFile": "src/Fibonacci.cpp",
"rust": {
"dir": "rust",
"interfaceModule": "fibonacci_interface",
"implementationModule": "fibonacci_implementation",
"typesModule": "fibonacci_types"
},
"objects": [{
"name": "Fibonacci",
"type": "Object",
"properties": [{
"name": "input",
"type": "quint32",
"write": true
}, {
"name": "result",
"type": "quint64"
}]
}, {
"name": "FibonacciList",
"type": "List",
"roles": [
[{
"name": "result",
"value": "Qt::DisplayRole",
"type": "quint64"
}]
]
}]
}

View File

@ -0,0 +1,89 @@
#![allow(unused_imports)]
#![allow(unused_variables)]
#![allow(dead_code)]
use libc::c_int;
use libc::c_uint;
use types::*;
use std::thread;
use fibonacci_interface::*;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::Arc;
fn fibonacci(input: u32) -> usize {
if input <= 1 {
return input as usize
}
let mut i = 0;
let mut sum = 0;
let mut last = 0;
let mut cur = 1;
while i < input - 1 {
sum = last + cur;
last = cur;
cur = sum;
i += 1;
}
sum
}
pub struct Fibonacci {
emit: FibonacciEmitter,
input: u32,
result: Arc<AtomicUsize>,
}
impl FibonacciTrait for Fibonacci {
fn create(emit: FibonacciEmitter) -> Fibonacci {
Fibonacci {
emit: emit,
input: 0,
result: Arc::new(AtomicUsize::new(0)),
}
}
fn emit(&self) -> &FibonacciEmitter {
&self.emit
}
fn get_input(&self) -> u32 {
self.input
}
fn set_input(&mut self, value: u32) {
self.input = value;
self.emit.input_changed();
let emit = self.emit.clone();
let result = self.result.clone();
result.swap(0, Ordering::SeqCst);
emit.result_changed();
thread::spawn(move || {
let r = fibonacci(value);
result.swap(r, Ordering::SeqCst);
emit.result_changed();
});
}
fn get_result(&self) -> u64 {
self.result.fetch_add(0, Ordering::SeqCst) as u64
}
}
pub struct FibonacciList {
emit: FibonacciListEmitter,
model: FibonacciListList,
}
impl FibonacciListTrait for FibonacciList {
fn create(emit: FibonacciListEmitter, model: FibonacciListList) -> FibonacciList {
FibonacciList {
emit: emit,
model: model,
}
}
fn emit(&self) -> &FibonacciListEmitter {
&self.emit
}
fn row_count(&self) -> c_int {
94
}
fn result(&self, row: c_int) -> u64 {
fibonacci(row as u32) as u64
}
}

View File

@ -0,0 +1,202 @@
/* generated by rust_qt_binding_generator */
#![allow(unknown_lints)]
#![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 fibonacci_implementation::*;
pub struct FibonacciQObject {}
#[derive (Clone)]
pub struct FibonacciEmitter {
qobject: Arc<Mutex<*const FibonacciQObject>>,
input_changed: fn(*const FibonacciQObject),
result_changed: fn(*const FibonacciQObject),
}
unsafe impl Send for FibonacciEmitter {}
impl FibonacciEmitter {
fn clear(&self) {
*self.qobject.lock().unwrap() = null();
}
pub fn input_changed(&self) {
let ptr = *self.qobject.lock().unwrap();
if !ptr.is_null() {
(self.input_changed)(ptr);
}
}
pub fn result_changed(&self) {
let ptr = *self.qobject.lock().unwrap();
if !ptr.is_null() {
(self.result_changed)(ptr);
}
}
}
pub trait FibonacciTrait {
fn create(emit: FibonacciEmitter) -> Self;
fn emit(&self) -> &FibonacciEmitter;
fn get_input(&self) -> u32;
fn set_input(&mut self, value: u32);
fn get_result(&self) -> u64;
}
#[no_mangle]
pub extern "C" fn fibonacci_new(qobject: *const FibonacciQObject,
input_changed: fn(*const FibonacciQObject),
result_changed: fn(*const FibonacciQObject))
-> *mut Fibonacci {
let emit = FibonacciEmitter {
qobject: Arc::new(Mutex::new(qobject)),
input_changed: input_changed,
result_changed: result_changed,
};
let d = Fibonacci::create(emit);
Box::into_raw(Box::new(d))
}
#[no_mangle]
pub unsafe extern "C" fn fibonacci_free(ptr: *mut Fibonacci) {
Box::from_raw(ptr).emit().clear();
}
#[no_mangle]
pub unsafe extern "C" fn fibonacci_input_get(ptr: *const Fibonacci) -> u32 {
(&*ptr).get_input()
}
#[no_mangle]
pub unsafe extern "C" fn fibonacci_input_set(ptr: *mut Fibonacci, v: u32) {
(&mut *ptr).set_input(v);
}
#[no_mangle]
pub unsafe extern "C" fn fibonacci_result_get(ptr: *const Fibonacci) -> u64 {
(&*ptr).get_result()
}
pub struct FibonacciListQObject {}
#[derive (Clone)]
pub struct FibonacciListEmitter {
qobject: Arc<Mutex<*const FibonacciListQObject>>,
new_data_ready: fn(*const FibonacciListQObject),
}
unsafe impl Send for FibonacciListEmitter {}
impl FibonacciListEmitter {
fn clear(&self) {
*self.qobject.lock().unwrap() = null();
}
pub fn new_data_ready(&self) {
let ptr = *self.qobject.lock().unwrap();
if !ptr.is_null() {
(self.new_data_ready)(ptr);
}
}
}
pub struct FibonacciListList {
qobject: *const FibonacciListQObject,
begin_reset_model: fn(*const FibonacciListQObject),
end_reset_model: fn(*const FibonacciListQObject),
begin_insert_rows: fn(*const FibonacciListQObject, c_int, c_int),
end_insert_rows: fn(*const FibonacciListQObject),
begin_remove_rows: fn(*const FibonacciListQObject, c_int, c_int),
end_remove_rows: fn(*const FibonacciListQObject),
}
impl FibonacciListList {
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, first: c_int, last: c_int) {
(self.begin_insert_rows)(self.qobject, first, last);
}
pub fn end_insert_rows(&self) {
(self.end_insert_rows)(self.qobject);
}
pub fn begin_remove_rows(&self, first: c_int, last: c_int) {
(self.begin_remove_rows)(self.qobject, first, last);
}
pub fn end_remove_rows(&self) {
(self.end_remove_rows)(self.qobject);
}
}
pub trait FibonacciListTrait {
fn create(emit: FibonacciListEmitter, model: FibonacciListList) -> Self;
fn emit(&self) -> &FibonacciListEmitter;
fn row_count(&self) -> c_int;
fn can_fetch_more(&self) -> bool { false }
fn fetch_more(&mut self) {}
fn sort(&mut self, c_int, SortOrder) {}
fn result(&self, row: c_int) -> u64;
}
#[no_mangle]
pub extern "C" fn fibonacci_list_new(qobject: *const FibonacciListQObject,
new_data_ready: fn(*const FibonacciListQObject),
begin_reset_model: fn(*const FibonacciListQObject),
end_reset_model: fn(*const FibonacciListQObject),
begin_insert_rows: fn(*const FibonacciListQObject,
c_int,
c_int),
end_insert_rows: fn(*const FibonacciListQObject),
begin_remove_rows: fn(*const FibonacciListQObject,
c_int,
c_int),
end_remove_rows: fn(*const FibonacciListQObject))
-> *mut FibonacciList {
let emit = FibonacciListEmitter {
qobject: Arc::new(Mutex::new(qobject)),
new_data_ready: new_data_ready,
};
let model = FibonacciListList {
qobject: qobject,
begin_reset_model: begin_reset_model,
end_reset_model: end_reset_model,
begin_insert_rows: begin_insert_rows,
end_insert_rows: end_insert_rows,
begin_remove_rows: begin_remove_rows,
end_remove_rows: end_remove_rows,
};
let d = FibonacciList::create(emit, model);
Box::into_raw(Box::new(d))
}
#[no_mangle]
pub unsafe extern "C" fn fibonacci_list_free(ptr: *mut FibonacciList) {
Box::from_raw(ptr).emit().clear();
}
#[no_mangle]
pub unsafe extern "C" fn fibonacci_list_row_count(ptr: *const FibonacciList) -> c_int {
(&*ptr).row_count()
}
#[no_mangle]
pub unsafe extern "C" fn fibonacci_list_can_fetch_more(ptr: *const FibonacciList) -> bool {
(&*ptr).can_fetch_more()
}
#[no_mangle]
pub unsafe extern "C" fn fibonacci_list_fetch_more(ptr: *mut FibonacciList) {
(&mut *ptr).fetch_more()
}
#[no_mangle]
pub unsafe extern "C" fn fibonacci_list_sort(ptr: *mut FibonacciList, column: c_int, order: SortOrder) {
(&mut *ptr).sort(column, order)
}
#[no_mangle]
pub unsafe extern "C" fn fibonacci_list_data_result(ptr: *const FibonacciList, row: c_int) -> u64 {
(&*ptr).result(row)
}

View File

@ -0,0 +1,82 @@
/* generated by rust_qt_binding_generator */
#![allow(dead_code)]
use std::slice;
use libc::{c_int, uint8_t, uint16_t};
#[repr(C)]
pub struct QString {
data: *const uint8_t,
len: c_int,
}
#[repr(C)]
pub struct QStringIn {
data: *const uint16_t,
len: c_int,
}
impl QStringIn {
pub fn convert(&self) -> String {
let data = unsafe { slice::from_raw_parts(self.data, self.len as usize) };
String::from_utf16_lossy(data)
}
}
impl<'a> From<&'a String> for QString {
fn from(string: &'a String) -> QString {
QString {
len: string.len() as c_int,
data: string.as_ptr(),
}
}
}
#[repr(C)]
pub struct QByteArray {
data: *const uint8_t,
len: c_int,
}
impl QByteArray {
pub fn convert(&self) -> Vec<u8> {
let data = unsafe { slice::from_raw_parts(self.data, self.len as usize) };
Vec::from(data)
}
}
impl<'a> From<&'a Vec<u8>> for QByteArray {
fn from(value: &'a Vec<u8>) -> QByteArray {
QByteArray {
len: value.len() as c_int,
data: value.as_ptr(),
}
}
}
#[repr(C)]
pub struct QModelIndex {
row: c_int,
internal_id: usize,
}
impl QModelIndex {
pub fn invalid() -> QModelIndex {
QModelIndex {
row: -1,
internal_id: 0,
}
}
pub fn create(row: c_int, id: usize) -> QModelIndex {
QModelIndex {
row: row,
internal_id: id,
}
}
}
#[repr(C)]
pub enum SortOrder {
Ascending = 0,
Descending = 1
}

View File

@ -81,8 +81,8 @@ pub trait TreeTrait {
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 file_type(&self, row: c_int, parent: usize) -> c_int;
fn file_permissions(&self, row: c_int, parent: usize) -> i32;
fn file_type(&self, row: c_int, parent: usize) -> i32;
fn file_size(&self, row: c_int, parent: usize) -> u64;
fn index(&self, row: c_int, parent: usize) -> usize;
fn parent(&self, parent: usize) -> QModelIndex;
@ -184,12 +184,12 @@ 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 tree_data_file_permissions(ptr: *const Tree, row: c_int, parent: usize) -> i32 {
(&*ptr).file_permissions(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn tree_data_file_type(ptr: *const Tree, row: c_int, parent: usize) -> c_int {
pub unsafe extern "C" fn tree_data_file_type(ptr: *const Tree, row: c_int, parent: usize) -> i32 {
(&*ptr).file_type(row, parent)
}

View File

@ -1,7 +1,9 @@
extern crate libc;
mod fibonacci_types;
mod fibonacci_implementation;
pub mod fibonacci_interface;
mod types;
pub mod testinterface;
mod testimplementation;
pub mod interface;
mod implementation;
pub mod interface;

View File

@ -1,122 +0,0 @@
#![allow(unused_imports)]
#![allow(unused_variables)]
#![allow(dead_code)]
use libc::c_int;
use libc::c_uint;
use types::*;
use testinterface::*;
pub struct Fibonacci {
emit: FibonacciEmitter,
input: c_uint,
result: u64,
}
impl FibonacciTrait for Fibonacci {
fn create(emit: FibonacciEmitter) -> Fibonacci {
Fibonacci {
emit: emit,
input: 0,
result: 0,
}
}
fn emit(&self) -> &FibonacciEmitter {
&self.emit
}
fn get_input(&self) -> c_uint {
self.input
}
fn set_input(&mut self, value: c_uint) {
self.input = value;
self.emit.input_changed();
}
fn get_result(&self) -> u64 {
self.result
}
}
pub struct Directory {
emit: DirectoryEmitter,
model: DirectoryList,
path: String,
}
impl DirectoryTrait for Directory {
fn create(emit: DirectoryEmitter, model: DirectoryList) -> Directory {
Directory {
emit: emit,
model: model,
path: String::new(),
}
}
fn emit(&self) -> &DirectoryEmitter {
&self.emit
}
fn get_path(&self) -> String {
self.path.clone()
}
fn set_path(&mut self, value: String) {
self.path = value;
self.emit.path_changed();
}
fn row_count(&self) -> c_int {
10
}
fn file_name(&self, row: c_int) -> String {
String::new()
}
fn file_icon(&self, row: c_int) -> Vec<u8> {
Vec::new()
}
fn file_path(&self, row: c_int) -> String {
String::new()
}
fn file_permissions(&self, row: c_int) -> c_int {
0
}
}
pub struct TestTree {
emit: TestTreeEmitter,
model: TestTreeUniformTree,
path: String,
}
impl TestTreeTrait for TestTree {
fn create(emit: TestTreeEmitter, model: TestTreeUniformTree) -> TestTree {
TestTree {
emit: emit,
model: model,
path: String::new(),
}
}
fn emit(&self) -> &TestTreeEmitter {
&self.emit
}
fn get_path(&self) -> String {
self.path.clone()
}
fn set_path(&mut self, value: String) {
self.path = value;
self.emit.path_changed();
}
fn row_count(&self, row: c_int, parent: usize) -> c_int {
10
}
fn file_name(&self, row: c_int, parent: usize) -> String {
String::new()
}
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()
}
fn file_permissions(&self, row: c_int, parent: usize) -> c_int {
0
}
fn index(&self, row: c_int, parent: usize) -> usize {
0
}
fn parent(&self, parent: usize) -> QModelIndex {
QModelIndex::create(0, 0)
}
}

View File

@ -1,442 +0,0 @@
/* generated by rust_qt_binding_generator */
#![allow(unknown_lints)]
#![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 testimplementation::*;
pub struct FibonacciQObject {}
#[derive (Clone)]
pub struct FibonacciEmitter {
qobject: Arc<Mutex<*const FibonacciQObject>>,
input_changed: fn(*const FibonacciQObject),
result_changed: fn(*const FibonacciQObject),
}
unsafe impl Send for FibonacciEmitter {}
impl FibonacciEmitter {
fn clear(&self) {
*self.qobject.lock().unwrap() = null();
}
pub fn input_changed(&self) {
let ptr = *self.qobject.lock().unwrap();
if !ptr.is_null() {
(self.input_changed)(ptr);
}
}
pub fn result_changed(&self) {
let ptr = *self.qobject.lock().unwrap();
if !ptr.is_null() {
(self.result_changed)(ptr);
}
}
}
pub trait FibonacciTrait {
fn create(emit: FibonacciEmitter) -> Self;
fn emit(&self) -> &FibonacciEmitter;
fn get_input(&self) -> c_uint;
fn set_input(&mut self, value: c_uint);
fn get_result(&self) -> u64;
}
#[no_mangle]
pub extern "C" fn fibonacci_new(qobject: *const FibonacciQObject,
input_changed: fn(*const FibonacciQObject),
result_changed: fn(*const FibonacciQObject))
-> *mut Fibonacci {
let emit = FibonacciEmitter {
qobject: Arc::new(Mutex::new(qobject)),
input_changed: input_changed,
result_changed: result_changed,
};
let d = Fibonacci::create(emit);
Box::into_raw(Box::new(d))
}
#[no_mangle]
pub unsafe extern "C" fn fibonacci_free(ptr: *mut Fibonacci) {
Box::from_raw(ptr).emit().clear();
}
#[no_mangle]
pub unsafe extern "C" fn fibonacci_input_get(ptr: *const Fibonacci) -> c_uint {
(&*ptr).get_input()
}
#[no_mangle]
pub unsafe extern "C" fn fibonacci_input_set(ptr: *mut Fibonacci, v: c_uint) {
(&mut *ptr).set_input(v);
}
#[no_mangle]
pub unsafe extern "C" fn fibonacci_result_get(ptr: *const Fibonacci) -> u64 {
(&*ptr).get_result()
}
pub struct DirectoryQObject {}
#[derive (Clone)]
pub struct DirectoryEmitter {
qobject: Arc<Mutex<*const DirectoryQObject>>,
path_changed: fn(*const DirectoryQObject),
new_data_ready: fn(*const DirectoryQObject),
}
unsafe impl Send for DirectoryEmitter {}
impl DirectoryEmitter {
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 fn new_data_ready(&self) {
let ptr = *self.qobject.lock().unwrap();
if !ptr.is_null() {
(self.new_data_ready)(ptr);
}
}
}
pub struct DirectoryList {
qobject: *const DirectoryQObject,
begin_reset_model: fn(*const DirectoryQObject),
end_reset_model: fn(*const DirectoryQObject),
begin_insert_rows: fn(*const DirectoryQObject, c_int, c_int),
end_insert_rows: fn(*const DirectoryQObject),
begin_remove_rows: fn(*const DirectoryQObject, c_int, c_int),
end_remove_rows: fn(*const DirectoryQObject),
}
impl DirectoryList {
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, first: c_int, last: c_int) {
(self.begin_insert_rows)(self.qobject, first, last);
}
pub fn end_insert_rows(&self) {
(self.end_insert_rows)(self.qobject);
}
pub fn begin_remove_rows(&self, first: c_int, last: c_int) {
(self.begin_remove_rows)(self.qobject, first, last);
}
pub fn end_remove_rows(&self) {
(self.end_remove_rows)(self.qobject);
}
}
pub trait DirectoryTrait {
fn create(emit: DirectoryEmitter, model: DirectoryList) -> Self;
fn emit(&self) -> &DirectoryEmitter;
fn get_path(&self) -> String;
fn set_path(&mut self, value: String);
fn row_count(&self) -> c_int;
fn can_fetch_more(&self) -> bool { false }
fn fetch_more(&mut self) {}
fn sort(&mut self, c_int, SortOrder) {}
fn file_name(&self, row: c_int) -> String;
fn file_icon(&self, row: c_int) -> Vec<u8>;
fn file_path(&self, row: c_int) -> String;
fn file_permissions(&self, row: c_int) -> c_int;
}
#[no_mangle]
pub extern "C" fn directory_new(qobject: *const DirectoryQObject,
path_changed: fn(*const DirectoryQObject),
new_data_ready: fn(*const DirectoryQObject),
begin_reset_model: fn(*const DirectoryQObject),
end_reset_model: fn(*const DirectoryQObject),
begin_insert_rows: fn(*const DirectoryQObject,
c_int,
c_int),
end_insert_rows: fn(*const DirectoryQObject),
begin_remove_rows: fn(*const DirectoryQObject,
c_int,
c_int),
end_remove_rows: fn(*const DirectoryQObject))
-> *mut Directory {
let emit = DirectoryEmitter {
qobject: Arc::new(Mutex::new(qobject)),
path_changed: path_changed,
new_data_ready: new_data_ready,
};
let model = DirectoryList {
qobject: qobject,
begin_reset_model: begin_reset_model,
end_reset_model: end_reset_model,
begin_insert_rows: begin_insert_rows,
end_insert_rows: end_insert_rows,
begin_remove_rows: begin_remove_rows,
end_remove_rows: end_remove_rows,
};
let d = Directory::create(emit, model);
Box::into_raw(Box::new(d))
}
#[no_mangle]
pub unsafe extern "C" fn directory_free(ptr: *mut Directory) {
Box::from_raw(ptr).emit().clear();
}
#[no_mangle]
pub unsafe extern "C" fn directory_path_get(ptr: *const Directory,
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 directory_path_set(ptr: *mut Directory, v: QStringIn) {
(&mut *ptr).set_path(v.convert());
}
#[no_mangle]
pub unsafe extern "C" fn directory_row_count(ptr: *const Directory) -> c_int {
(&*ptr).row_count()
}
#[no_mangle]
pub unsafe extern "C" fn directory_can_fetch_more(ptr: *const Directory) -> bool {
(&*ptr).can_fetch_more()
}
#[no_mangle]
pub unsafe extern "C" fn directory_fetch_more(ptr: *mut Directory) {
(&mut *ptr).fetch_more()
}
#[no_mangle]
pub unsafe extern "C" fn directory_sort(ptr: *mut Directory, column: c_int, order: SortOrder) {
(&mut *ptr).sort(column, order)
}
#[no_mangle]
pub unsafe extern "C" fn directory_data_file_name(ptr: *const Directory,
row: c_int,
d: *mut c_void,
set: fn(*mut c_void, QString)) {
let data = (&*ptr).file_name(row);
set(d, QString::from(&data));
}
#[no_mangle]
pub unsafe extern "C" fn directory_data_file_icon(ptr: *const Directory,
row: c_int,
d: *mut c_void,
set: fn(*mut c_void, QByteArray)) {
let data = (&*ptr).file_icon(row);
set(d, QByteArray::from(&data));
}
#[no_mangle]
pub unsafe extern "C" fn directory_data_file_path(ptr: *const Directory,
row: c_int,
d: *mut c_void,
set: fn(*mut c_void, QString)) {
let data = (&*ptr).file_path(row);
set(d, QString::from(&data));
}
#[no_mangle]
pub unsafe extern "C" fn directory_data_file_permissions(ptr: *const Directory, row: c_int) -> c_int {
(&*ptr).file_permissions(row)
}
pub struct TestTreeQObject {}
#[derive (Clone)]
pub struct TestTreeEmitter {
qobject: Arc<Mutex<*const TestTreeQObject>>,
path_changed: fn(*const TestTreeQObject),
new_data_ready: fn(*const TestTreeQObject, row: c_int, parent: usize),
}
unsafe impl Send for TestTreeEmitter {}
impl TestTreeEmitter {
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 fn new_data_ready(&self, row: c_int, parent: usize) {
let ptr = *self.qobject.lock().unwrap();
if !ptr.is_null() {
(self.new_data_ready)(ptr, row, parent);
}
}
}
pub struct TestTreeUniformTree {
qobject: *const TestTreeQObject,
begin_reset_model: fn(*const TestTreeQObject),
end_reset_model: fn(*const TestTreeQObject),
begin_insert_rows: fn(*const TestTreeQObject,row: c_int, parent: usize, c_int, c_int),
end_insert_rows: fn(*const TestTreeQObject),
begin_remove_rows: fn(*const TestTreeQObject,row: c_int, parent: usize, c_int, c_int),
end_remove_rows: fn(*const TestTreeQObject),
}
impl TestTreeUniformTree {
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,row: c_int, parent: usize, first: c_int, last: c_int) {
(self.begin_insert_rows)(self.qobject,row, parent, first, last);
}
pub fn end_insert_rows(&self) {
(self.end_insert_rows)(self.qobject);
}
pub fn begin_remove_rows(&self,row: c_int, parent: usize, first: c_int, last: c_int) {
(self.begin_remove_rows)(self.qobject,row, parent, first, last);
}
pub fn end_remove_rows(&self) {
(self.end_remove_rows)(self.qobject);
}
}
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;
fn can_fetch_more(&self, c_int, usize) -> bool { false }
fn fetch_more(&mut self, c_int, usize) {}
fn sort(&mut self, c_int, SortOrder) {}
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, parent: usize) -> QModelIndex;
}
#[no_mangle]
pub extern "C" fn test_tree_new(qobject: *const TestTreeQObject,
path_changed: fn(*const TestTreeQObject),
new_data_ready: fn(*const TestTreeQObject, row: c_int, parent: usize),
begin_reset_model: fn(*const TestTreeQObject),
end_reset_model: fn(*const TestTreeQObject),
begin_insert_rows: fn(*const TestTreeQObject,row: c_int, parent: usize,
c_int,
c_int),
end_insert_rows: fn(*const TestTreeQObject),
begin_remove_rows: fn(*const TestTreeQObject,row: c_int, parent: usize,
c_int,
c_int),
end_remove_rows: fn(*const TestTreeQObject))
-> *mut TestTree {
let emit = TestTreeEmitter {
qobject: Arc::new(Mutex::new(qobject)),
path_changed: path_changed,
new_data_ready: new_data_ready,
};
let model = TestTreeUniformTree {
qobject: qobject,
begin_reset_model: begin_reset_model,
end_reset_model: end_reset_model,
begin_insert_rows: begin_insert_rows,
end_insert_rows: end_insert_rows,
begin_remove_rows: begin_remove_rows,
end_remove_rows: end_remove_rows,
};
let d = TestTree::create(emit, model);
Box::into_raw(Box::new(d))
}
#[no_mangle]
pub unsafe extern "C" fn test_tree_free(ptr: *mut TestTree) {
Box::from_raw(ptr).emit().clear();
}
#[no_mangle]
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();
set(p, QString::from(&data));
}
#[no_mangle]
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 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 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 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 test_tree_sort(ptr: *mut TestTree, column: c_int, order: SortOrder) {
(&mut *ptr).sort(column, order)
}
#[no_mangle]
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)) {
let data = (&*ptr).file_name(row, parent);
set(d, QString::from(&data));
}
#[no_mangle]
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)) {
let data = (&*ptr).file_icon(row, parent);
set(d, QByteArray::from(&data));
}
#[no_mangle]
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)) {
let data = (&*ptr).file_path(row, parent);
set(d, QString::from(&data));
}
#[no_mangle]
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 test_tree_index(ptr: *const TestTree, row: c_int, parent: usize) -> usize {
(&*ptr).index(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn test_tree_parent(ptr: *const TestTree, parent: usize) -> QModelIndex {
(&*ptr).parent(parent)
}

View File

@ -1,417 +0,0 @@
/* generated by rust_qt_binding_generator */
#include "Bindings.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" {
Fibonacci::Private* fibonacci_new(Fibonacci*, void (*)(Fibonacci*), void (*)(Fibonacci*));
void fibonacci_free(Fibonacci::Private*);
uint fibonacci_input_get(const Fibonacci::Private*);
void fibonacci_input_set(Fibonacci::Private*, uint);
uint64_t fibonacci_result_get(const Fibonacci::Private*);
Directory::Private* directory_new(Directory*, void (*)(Directory*),
void (*)(const Directory*),
void (*)(Directory*),
void (*)(Directory*),
void (*)(Directory*, int, int),
void (*)(Directory*),
void (*)(Directory*, int, int),
void (*)(Directory*));
void directory_free(Directory::Private*);
void directory_path_get(const Directory::Private*, QString*, qstring_set);
void directory_path_set(Directory::Private*, qstring_t);
TestTree::Private* test_tree_new(TestTree*, void (*)(TestTree*),
void (*)(const TestTree*, int, quintptr),
void (*)(TestTree*),
void (*)(TestTree*),
void (*)(TestTree*, int, quintptr, int, int),
void (*)(TestTree*),
void (*)(TestTree*, int, quintptr, int, int),
void (*)(TestTree*));
void test_tree_free(TestTree::Private*);
void test_tree_path_get(const TestTree::Private*, QString*, qstring_set);
void test_tree_path_set(TestTree::Private*, qstring_t);
};
Fibonacci::Fibonacci(QObject *parent):
QObject(parent),
d(fibonacci_new(this,
[](Fibonacci* o) { emit o->inputChanged(); },
[](Fibonacci* o) { emit o->resultChanged(); })) {}
Fibonacci::~Fibonacci() {
fibonacci_free(d);
}
uint Fibonacci::input() const
{
return fibonacci_input_get(d);
}
void Fibonacci::setInput(uint v) {
fibonacci_input_set(d, v);
}
uint64_t Fibonacci::result() const
{
return fibonacci_result_get(d);
}
Directory::Directory(QObject *parent):
QAbstractItemModel(parent),
d(directory_new(this,
[](Directory* o) { emit o->pathChanged(); },
[](const Directory* o) {
emit o->newDataReady(QModelIndex());
},
[](Directory* o) {
o->beginResetModel();
},
[](Directory* o) {
o->endResetModel();
},
[](Directory* o, int first, int last) {
o->beginInsertRows(QModelIndex(), first, last);
},
[](Directory* o) {
o->endInsertRows();
},
[](Directory* o, int first, int last) {
o->beginRemoveRows(QModelIndex(), first, last);
},
[](Directory* o) {
o->endRemoveRows();
}
)) {
connect(this, &Directory::newDataReady, this, [this](const QModelIndex& i) {
fetchMore(i);
}, Qt::QueuedConnection);
}
Directory::~Directory() {
directory_free(d);
}
QString Directory::path() const
{
QString v;
directory_path_get(d, &v, set_qstring);
return v;
}
void Directory::setPath(const QString& v) {
directory_path_set(d, v);
}
extern "C" {
void directory_data_file_name(const Directory::Private*, int, QString*, qstring_set);
void directory_data_file_icon(const Directory::Private*, int, QByteArray*, qbytearray_set);
void directory_data_file_path(const Directory::Private*, int, QString*, qstring_set);
int directory_data_file_permissions(const Directory::Private*, int);
void directory_sort(Directory::Private*, int column, Qt::SortOrder order = Qt::AscendingOrder);
int directory_row_count(const Directory::Private*);
bool directory_can_fetch_more(const Directory::Private*);
void directory_fetch_more(Directory::Private*);
}
int Directory::columnCount(const QModelIndex &parent) const
{
return (parent.isValid()) ? 0 : 3;
}
bool Directory::hasChildren(const QModelIndex &parent) const
{
return rowCount(parent) > 0;
}
int Directory::rowCount(const QModelIndex &parent) const
{
return (parent.isValid()) ? 0 : directory_row_count(d);
}
QModelIndex Directory::index(int row, int column, const QModelIndex &parent) const
{
if (!parent.isValid() && column == 0) {
return createIndex(row, 0, (quintptr)0);
}
return QModelIndex();
}
QModelIndex Directory::parent(const QModelIndex &) const
{
return QModelIndex();
}
bool Directory::canFetchMore(const QModelIndex &parent) const
{
return (parent.isValid()) ? 0 : directory_can_fetch_more(d);
}
void Directory::fetchMore(const QModelIndex &parent)
{
if (!parent.isValid()) {
directory_fetch_more(d);
}
}
void Directory::sort(int column, Qt::SortOrder order)
{
directory_sort(d, column, order);
}
QVariant Directory::data(const QModelIndex &index, int role) const
{
QVariant v;
QString s;
QByteArray b;
switch (index.column()) {
case 0:
switch (role) {
case Qt::DisplayRole:
directory_data_file_name(d, index.row(), &s, set_qstring);
v.setValue<QString>(s);
break;
case Qt::DecorationRole:
directory_data_file_icon(d, index.row(), &b, set_qbytearray);
v.setValue<QByteArray>(b);
break;
case Qt::UserRole + 1:
directory_data_file_path(d, index.row(), &s, set_qstring);
v.setValue<QString>(s);
break;
case Qt::UserRole + 2:
directory_data_file_name(d, index.row(), &s, set_qstring);
v.setValue<QString>(s);
break;
case Qt::UserRole + 3:
v.setValue<int>(directory_data_file_permissions(d, index.row()));
break;
}
break;
case 1:
switch (role) {
case Qt::DisplayRole:
directory_data_file_path(d, index.row(), &s, set_qstring);
v.setValue<QString>(s);
break;
}
break;
case 2:
switch (role) {
case Qt::DisplayRole:
v.setValue<int>(directory_data_file_permissions(d, index.row()));
break;
}
break;
}
return v;
}
QHash<int, QByteArray> Directory::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;
}
TestTree::TestTree(QObject *parent):
QAbstractItemModel(parent),
d(test_tree_new(this,
[](TestTree* o) { emit o->pathChanged(); },
[](const TestTree* o, int row, quintptr id) {
emit o->newDataReady(o->createIndex(row, 0, id));
},
[](TestTree* o) {
o->beginResetModel();
},
[](TestTree* o) {
o->endResetModel();
},
[](TestTree* o, int row, quintptr id, int first, int last) {
o->beginInsertRows(o->createIndex(row, 0, id), first, last);
},
[](TestTree* o) {
o->endInsertRows();
},
[](TestTree* o, int row, quintptr id, int first, int last) {
o->beginRemoveRows(o->createIndex(row, 0, id), first, last);
},
[](TestTree* o) {
o->endRemoveRows();
}
)) {
connect(this, &TestTree::newDataReady, this, [this](const QModelIndex& i) {
fetchMore(i);
}, Qt::QueuedConnection);
}
TestTree::~TestTree() {
test_tree_free(d);
}
QString TestTree::path() const
{
QString v;
test_tree_path_get(d, &v, set_qstring);
return v;
}
void TestTree::setPath(const QString& v) {
test_tree_path_set(d, v);
}
extern "C" {
void test_tree_data_file_name(const TestTree::Private*, int, quintptr, QString*, qstring_set);
void test_tree_data_file_icon(const TestTree::Private*, int, quintptr, QByteArray*, qbytearray_set);
void test_tree_data_file_path(const TestTree::Private*, int, quintptr, QString*, qstring_set);
int test_tree_data_file_permissions(const TestTree::Private*, int, quintptr);
void test_tree_sort(TestTree::Private*, int column, Qt::SortOrder order = Qt::AscendingOrder);
int test_tree_row_count(const TestTree::Private*, int, quintptr);
bool test_tree_can_fetch_more(const TestTree::Private*, int, quintptr);
void test_tree_fetch_more(TestTree::Private*, int, quintptr);
quintptr test_tree_index(const TestTree::Private*, int, quintptr);
qmodelindex_t test_tree_parent(const TestTree::Private*, quintptr);
}
int TestTree::columnCount(const QModelIndex &) const
{
return 3;
}
bool TestTree::hasChildren(const QModelIndex &parent) const
{
return rowCount(parent) > 0;
}
int TestTree::rowCount(const QModelIndex &parent) const
{
if (parent.isValid() && parent.column() != 0) {
return 0;
}
return test_tree_row_count(d, parent.row(), parent.internalId());
}
QModelIndex TestTree::index(int row, int column, const QModelIndex &parent) const
{
if (row < 0 || column < 0 || column >= 3) {
return QModelIndex();
}
if (parent.isValid() && parent.column() != 0) {
return QModelIndex();
}
const quintptr id = test_tree_index(d, parent.row(), parent.internalId());
return createIndex(row, column, id);
}
QModelIndex TestTree::parent(const QModelIndex &index) const
{
if (!index.isValid()) {
return QModelIndex();
}
const qmodelindex_t parent = test_tree_parent(d, index.internalId());
return parent.id ?createIndex(parent.row, 0, parent.id) :QModelIndex();
}
bool TestTree::canFetchMore(const QModelIndex &parent) const
{
if (parent.isValid() && parent.column() != 0) {
return false;
}
return test_tree_can_fetch_more(d, parent.row(), parent.internalId());
}
void TestTree::fetchMore(const QModelIndex &parent)
{
test_tree_fetch_more(d, parent.row(), parent.internalId());
}
void TestTree::sort(int column, Qt::SortOrder order)
{
test_tree_sort(d, column, order);
}
QVariant TestTree::data(const QModelIndex &index, int role) const
{
QVariant v;
QString s;
QByteArray b;
switch (index.column()) {
case 0:
switch (role) {
case Qt::DisplayRole:
test_tree_data_file_name(d, index.row(), index.internalId(), &s, set_qstring);
v.setValue<QString>(s);
break;
case Qt::DecorationRole:
test_tree_data_file_icon(d, index.row(), index.internalId(), &b, set_qbytearray);
v.setValue<QByteArray>(b);
break;
case Qt::UserRole + 1:
test_tree_data_file_path(d, index.row(), index.internalId(), &s, set_qstring);
v.setValue<QString>(s);
break;
case Qt::UserRole + 2:
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>(test_tree_data_file_permissions(d, index.row(), index.internalId()));
break;
}
break;
case 1:
switch (role) {
case Qt::DisplayRole:
test_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>(test_tree_data_file_permissions(d, index.row(), index.internalId()));
break;
}
break;
}
return v;
}
QHash<int, QByteArray> TestTree::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;
}

View File

@ -1,96 +0,0 @@
/* generated by rust_qt_binding_generator */
#ifndef BINDINGS_H
#define BINDINGS_H
#include <QObject>
#include <QAbstractItemModel>
class Fibonacci : public QObject
{
Q_OBJECT
public:
class Private;
private:
Private * const d;
Q_PROPERTY(uint input READ input WRITE setInput NOTIFY inputChanged FINAL)
Q_PROPERTY(uint64_t result READ result NOTIFY resultChanged FINAL)
public:
explicit Fibonacci(QObject *parent = nullptr);
~Fibonacci();
uint input() const;
void setInput(uint v);
uint64_t result() const;
signals:
void inputChanged();
void resultChanged();
private:
uint m_input;
uint64_t m_result;
};
class Directory : public QAbstractItemModel
{
Q_OBJECT
public:
class Private;
private:
Private * const d;
Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged FINAL)
public:
explicit Directory(QObject *parent = nullptr);
~Directory();
QString path() const;
void setPath(const QString& v);
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
bool canFetchMore(const QModelIndex &parent) const override;
void fetchMore(const QModelIndex &parent) override;
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
QHash<int, QByteArray> roleNames() const override;
signals:
// new data is ready to be made available to the model with fetchMore()
void newDataReady(const QModelIndex &parent) const;
signals:
void pathChanged();
private:
QString m_path;
};
class TestTree : public QAbstractItemModel
{
Q_OBJECT
public:
class Private;
private:
Private * const d;
Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged FINAL)
public:
explicit TestTree(QObject *parent = nullptr);
~TestTree();
QString path() const;
void setPath(const QString& v);
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
bool canFetchMore(const QModelIndex &parent) const override;
void fetchMore(const QModelIndex &parent) override;
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
QHash<int, QByteArray> roleNames() const override;
signals:
// new data is ready to be made available to the model with fetchMore()
void newDataReady(const QModelIndex &parent) const;
signals:
void pathChanged();
private:
QString m_path;
};
#endif // BINDINGS_H

188
demo/src/Fibonacci.cpp Normal file
View File

@ -0,0 +1,188 @@
/* generated by rust_qt_binding_generator */
#include "Fibonacci.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" {
Fibonacci::Private* fibonacci_new(Fibonacci*, void (*)(Fibonacci*), void (*)(Fibonacci*));
void fibonacci_free(Fibonacci::Private*);
quint32 fibonacci_input_get(const Fibonacci::Private*);
void fibonacci_input_set(Fibonacci::Private*, uint);
quint64 fibonacci_result_get(const Fibonacci::Private*);
FibonacciList::Private* fibonacci_list_new(FibonacciList*,
void (*)(const FibonacciList*),
void (*)(FibonacciList*),
void (*)(FibonacciList*),
void (*)(FibonacciList*, int, int),
void (*)(FibonacciList*),
void (*)(FibonacciList*, int, int),
void (*)(FibonacciList*));
void fibonacci_list_free(FibonacciList::Private*);
};
Fibonacci::Fibonacci(QObject *parent):
QObject(parent),
d(fibonacci_new(this,
[](Fibonacci* o) { emit o->inputChanged(); },
[](Fibonacci* o) { emit o->resultChanged(); })) {}
Fibonacci::~Fibonacci() {
fibonacci_free(d);
}
quint32 Fibonacci::input() const
{
return fibonacci_input_get(d);
}
void Fibonacci::setInput(uint v) {
fibonacci_input_set(d, v);
}
quint64 Fibonacci::result() const
{
return fibonacci_result_get(d);
}
FibonacciList::FibonacciList(QObject *parent):
QAbstractItemModel(parent),
d(fibonacci_list_new(this,
[](const FibonacciList* o) {
emit o->newDataReady(QModelIndex());
},
[](FibonacciList* o) {
o->beginResetModel();
},
[](FibonacciList* o) {
o->endResetModel();
},
[](FibonacciList* o, int first, int last) {
o->beginInsertRows(QModelIndex(), first, last);
},
[](FibonacciList* o) {
o->endInsertRows();
},
[](FibonacciList* o, int first, int last) {
o->beginRemoveRows(QModelIndex(), first, last);
},
[](FibonacciList* o) {
o->endRemoveRows();
}
)) {
connect(this, &FibonacciList::newDataReady, this, [this](const QModelIndex& i) {
fetchMore(i);
}, Qt::QueuedConnection);
}
FibonacciList::~FibonacciList() {
fibonacci_list_free(d);
}
extern "C" {
quint64 fibonacci_list_data_result(const FibonacciList::Private*, int);
void fibonacci_list_sort(FibonacciList::Private*, int column, Qt::SortOrder order = Qt::AscendingOrder);
int fibonacci_list_row_count(const FibonacciList::Private*);
bool fibonacci_list_can_fetch_more(const FibonacciList::Private*);
void fibonacci_list_fetch_more(FibonacciList::Private*);
}
int FibonacciList::columnCount(const QModelIndex &parent) const
{
return (parent.isValid()) ? 0 : 1;
}
bool FibonacciList::hasChildren(const QModelIndex &parent) const
{
return rowCount(parent) > 0;
}
int FibonacciList::rowCount(const QModelIndex &parent) const
{
return (parent.isValid()) ? 0 : fibonacci_list_row_count(d);
}
QModelIndex FibonacciList::index(int row, int column, const QModelIndex &parent) const
{
if (!parent.isValid() && column == 0) {
return createIndex(row, 0, (quintptr)0);
}
return QModelIndex();
}
QModelIndex FibonacciList::parent(const QModelIndex &) const
{
return QModelIndex();
}
bool FibonacciList::canFetchMore(const QModelIndex &parent) const
{
return (parent.isValid()) ? 0 : fibonacci_list_can_fetch_more(d);
}
void FibonacciList::fetchMore(const QModelIndex &parent)
{
if (!parent.isValid()) {
fibonacci_list_fetch_more(d);
}
}
void FibonacciList::sort(int column, Qt::SortOrder order)
{
fibonacci_list_sort(d, column, order);
}
QVariant FibonacciList::data(const QModelIndex &index, int role) const
{
QVariant v;
QString s;
QByteArray b;
switch (index.column()) {
case 0:
switch (role) {
case Qt::DisplayRole:
v.setValue<quint64>(fibonacci_list_data_result(d, index.row()));
break;
}
break;
}
return v;
}
QHash<int, QByteArray> FibonacciList::roleNames() const {
QHash<int, QByteArray> names;
names.insert(Qt::DisplayRole, "result");
return names;
}

58
demo/src/Fibonacci.h Normal file
View File

@ -0,0 +1,58 @@
/* generated by rust_qt_binding_generator */
#ifndef FIBONACCI_H
#define FIBONACCI_H
#include <QObject>
#include <QAbstractItemModel>
class Fibonacci : public QObject
{
Q_OBJECT
public:
class Private;
private:
Private * const d;
Q_PROPERTY(quint32 input READ input WRITE setInput NOTIFY inputChanged FINAL)
Q_PROPERTY(quint64 result READ result NOTIFY resultChanged FINAL)
public:
explicit Fibonacci(QObject *parent = nullptr);
~Fibonacci();
quint32 input() const;
void setInput(uint v);
quint64 result() const;
signals:
void inputChanged();
void resultChanged();
private:
quint32 m_input;
quint64 m_result;
};
class FibonacciList : public QAbstractItemModel
{
Q_OBJECT
public:
class Private;
private:
Private * const d;
public:
explicit FibonacciList(QObject *parent = nullptr);
~FibonacciList();
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
bool canFetchMore(const QModelIndex &parent) const override;
void fetchMore(const QModelIndex &parent) override;
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
QHash<int, QByteArray> roleNames() const override;
signals:
// new data is ready to be made available to the model with fetchMore()
void newDataReady(const QModelIndex &parent) const;
signals:
private:
};
#endif // FIBONACCI_H

View File

@ -103,9 +103,9 @@ extern "C" {
void tree_data_file_name(const Tree::Private*, int, quintptr, QString*, qstring_set);
void tree_data_file_icon(const Tree::Private*, int, quintptr, QByteArray*, qbytearray_set);
void tree_data_file_path(const Tree::Private*, int, quintptr, QString*, qstring_set);
int tree_data_file_permissions(const Tree::Private*, int, quintptr);
int tree_data_file_type(const Tree::Private*, int, quintptr);
uint64_t tree_data_file_size(const Tree::Private*, int, quintptr);
qint32 tree_data_file_permissions(const Tree::Private*, int, quintptr);
qint32 tree_data_file_type(const Tree::Private*, int, quintptr);
quint64 tree_data_file_size(const Tree::Private*, int, quintptr);
void tree_sort(Tree::Private*, int column, Qt::SortOrder order = Qt::AscendingOrder);
int tree_row_count(const Tree::Private*, int, quintptr);
@ -195,13 +195,13 @@ QVariant Tree::data(const QModelIndex &index, int role) const
v.setValue<QString>(s);
break;
case Qt::UserRole + 3:
v.setValue<int>(tree_data_file_permissions(d, index.row(), index.internalId()));
v.setValue<qint32>(tree_data_file_permissions(d, index.row(), index.internalId()));
break;
case Qt::UserRole + 4:
v.setValue<int>(tree_data_file_type(d, index.row(), index.internalId()));
v.setValue<qint32>(tree_data_file_type(d, index.row(), index.internalId()));
break;
case Qt::UserRole + 5:
v.setValue<uint64_t>(tree_data_file_size(d, index.row(), index.internalId()));
v.setValue<quint64>(tree_data_file_size(d, index.row(), index.internalId()));
break;
}
break;
@ -216,21 +216,21 @@ 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<qint32>(tree_data_file_permissions(d, index.row(), index.internalId()));
break;
}
break;
case 3:
switch (role) {
case Qt::DisplayRole:
v.setValue<int>(tree_data_file_type(d, index.row(), index.internalId()));
v.setValue<qint32>(tree_data_file_type(d, index.row(), index.internalId()));
break;
}
break;
case 4:
switch (role) {
case Qt::DisplayRole:
v.setValue<uint64_t>(tree_data_file_size(d, index.row(), index.internalId()));
v.setValue<quint64>(tree_data_file_size(d, index.row(), index.internalId()));
break;
}
break;

View File

@ -1,5 +1,5 @@
#include "Tree.h"
#include "Bindings.h"
#include "Fibonacci.h"
#include <cstdlib>
#include <KAboutData>
@ -51,8 +51,9 @@ int main (int argc, char *argv[])
parser.process(app);
aboutData.processCommandLine(&parser);
qmlRegisterType<Directory>("rust", 1, 0, "Directory");
qmlRegisterType<QSortFilterProxyModel>("org.qtproject.example", 1, 0, "SortFilterProxyModel");
qmlRegisterType<Fibonacci>("rust", 1, 0, "Fibonacci");
qmlRegisterType<FibonacciList>("rust", 1, 0, "FibonacciList");
Tree model;
model.setPath("/");
@ -66,7 +67,7 @@ int main (int argc, char *argv[])
auto root = sortedModel.index(0, 0);
view.expand(root);
view.sortByColumn(0, Qt::AscendingOrder);
view.show();
// view.show();
view.header()->setSectionResizeMode(QHeaderView::ResizeToContents);
QQmlApplicationEngine engine;

View File

@ -34,15 +34,15 @@
}, {
"name": "filePermissions",
"value": "Qt::UserRole + 3",
"type": "int"
"type": "qint32"
}, {
"name": "fileType",
"value": "Qt::UserRole + 4",
"type": "int"
"type": "qint32"
}, {
"name": "fileSize",
"value": "Qt::UserRole + 5",
"type": "uint64_t"
"type": "quint64"
}],
[{
"name": "filePath",
@ -52,17 +52,17 @@
[{
"name": "filePermissions",
"value": "Qt::DisplayRole",
"type": "int"
"type": "qint32"
}],
[{
"name": "fileType",
"value": "Qt::DisplayRole",
"type": "int"
"type": "qint32"
}],
[{
"name": "fileSize",
"value": "Qt::DisplayRole",
"type": "uint64_t"
"type": "quint64"
}]
]
}]

View File

@ -53,23 +53,23 @@ const QMap<BindingType, BindingTypeProperties>& bindingTypeProperties() {
QMap<BindingType, BindingTypeProperties> f;
f.insert(BindingType::Bool, simpleType("bool", "true"));
f.insert(BindingType::Int, {
.name = "int",
.cppSetType = "int",
.cSetType = "int",
.rustType = "c_int",
.name = "qint32",
.cppSetType = "qint32",
.cSetType = "qint32",
.rustType = "i32",
.rustTypeInit = "0"
});
f.insert(BindingType::UInt, {
.name = "uint",
.name = "quint32",
.cppSetType = "uint",
.cSetType = "uint",
.rustType = "c_uint",
.rustType = "u32",
.rustTypeInit = "0"
});
f.insert(BindingType::ULongLong, {
.name = "uint64_t",
.cppSetType = "uint64_t",
.cSetType = "uint64_t",
.name = "quint64",
.cppSetType = "quint64",
.cSetType = "quint64",
.rustType = "u64",
.rustTypeInit = "0"
});

View File

@ -9,8 +9,8 @@ use interface::*;
pub struct Object {
emit: ObjectEmitter,
boolean: bool,
integer: c_int,
uinteger: c_uint,
integer: i32,
uinteger: u32,
u64: u64,
string: String,
bytearray: Vec<u8>,
@ -38,17 +38,17 @@ impl ObjectTrait for Object {
self.boolean = value;
self.emit.boolean_changed();
}
fn get_integer(&self) -> c_int {
fn get_integer(&self) -> i32 {
self.integer
}
fn set_integer(&mut self, value: c_int) {
fn set_integer(&mut self, value: i32) {
self.integer = value;
self.emit.integer_changed();
}
fn get_uinteger(&self) -> c_uint {
fn get_uinteger(&self) -> u32 {
self.uinteger
}
fn set_uinteger(&mut self, value: c_uint) {
fn set_uinteger(&mut self, value: u32) {
self.uinteger = value;
self.emit.uinteger_changed();
}

View File

@ -71,10 +71,10 @@ pub trait ObjectTrait {
fn emit(&self) -> &ObjectEmitter;
fn get_boolean(&self) -> bool;
fn set_boolean(&mut self, value: bool);
fn get_integer(&self) -> c_int;
fn set_integer(&mut self, value: c_int);
fn get_uinteger(&self) -> c_uint;
fn set_uinteger(&mut self, value: c_uint);
fn get_integer(&self) -> i32;
fn set_integer(&mut self, value: i32);
fn get_uinteger(&self) -> u32;
fn set_uinteger(&mut self, value: u32);
fn get_u64(&self) -> u64;
fn set_u64(&mut self, value: u64);
fn get_string(&self) -> String;
@ -121,22 +121,22 @@ pub unsafe extern "C" fn object_boolean_set(ptr: *mut Object, v: bool) {
}
#[no_mangle]
pub unsafe extern "C" fn object_integer_get(ptr: *const Object) -> c_int {
pub unsafe extern "C" fn object_integer_get(ptr: *const Object) -> i32 {
(&*ptr).get_integer()
}
#[no_mangle]
pub unsafe extern "C" fn object_integer_set(ptr: *mut Object, v: c_int) {
pub unsafe extern "C" fn object_integer_set(ptr: *mut Object, v: i32) {
(&mut *ptr).set_integer(v);
}
#[no_mangle]
pub unsafe extern "C" fn object_uinteger_get(ptr: *const Object) -> c_uint {
pub unsafe extern "C" fn object_uinteger_get(ptr: *const Object) -> u32 {
(&*ptr).get_uinteger()
}
#[no_mangle]
pub unsafe extern "C" fn object_uinteger_set(ptr: *mut Object, v: c_uint) {
pub unsafe extern "C" fn object_uinteger_set(ptr: *mut Object, v: u32) {
(&mut *ptr).set_uinteger(v);
}

View File

@ -15,15 +15,15 @@
"write": true
}, {
"name": "integer",
"type": "int",
"type": "qint32",
"write": true
}, {
"name": "uinteger",
"type": "uint",
"type": "quint32",
"write": true
}, {
"name": "u64",
"type": "uint64_t",
"type": "quint64",
"write": true
}, {
"name": "string",

View File

@ -47,12 +47,12 @@ extern "C" {
void object_free(Object::Private*);
bool object_boolean_get(const Object::Private*);
void object_boolean_set(Object::Private*, bool);
int object_integer_get(const Object::Private*);
void object_integer_set(Object::Private*, int);
uint object_uinteger_get(const Object::Private*);
qint32 object_integer_get(const Object::Private*);
void object_integer_set(Object::Private*, qint32);
quint32 object_uinteger_get(const Object::Private*);
void object_uinteger_set(Object::Private*, uint);
uint64_t object_u64_get(const Object::Private*);
void object_u64_set(Object::Private*, uint64_t);
quint64 object_u64_get(const Object::Private*);
void object_u64_set(Object::Private*, quint64);
void object_string_get(const Object::Private*, QString*, qstring_set);
void object_string_set(Object::Private*, qstring_t);
void object_bytearray_get(const Object::Private*, QByteArray*, qbytearray_set);
@ -78,25 +78,25 @@ bool Object::boolean() const
void Object::setBoolean(bool v) {
object_boolean_set(d, v);
}
int Object::integer() const
qint32 Object::integer() const
{
return object_integer_get(d);
}
void Object::setInteger(int v) {
void Object::setInteger(qint32 v) {
object_integer_set(d, v);
}
uint Object::uinteger() const
quint32 Object::uinteger() const
{
return object_uinteger_get(d);
}
void Object::setUinteger(uint v) {
object_uinteger_set(d, v);
}
uint64_t Object::u64() const
quint64 Object::u64() const
{
return object_u64_get(d);
}
void Object::setU64(uint64_t v) {
void Object::setU64(quint64 v) {
object_u64_set(d, v);
}
QString Object::string() const

View File

@ -13,9 +13,9 @@ public:
private:
Private * const d;
Q_PROPERTY(bool boolean READ boolean WRITE setBoolean NOTIFY booleanChanged FINAL)
Q_PROPERTY(int integer READ integer WRITE setInteger NOTIFY integerChanged FINAL)
Q_PROPERTY(uint uinteger READ uinteger WRITE setUinteger NOTIFY uintegerChanged FINAL)
Q_PROPERTY(uint64_t u64 READ u64 WRITE setU64 NOTIFY u64Changed FINAL)
Q_PROPERTY(qint32 integer READ integer WRITE setInteger NOTIFY integerChanged FINAL)
Q_PROPERTY(quint32 uinteger READ uinteger WRITE setUinteger NOTIFY uintegerChanged FINAL)
Q_PROPERTY(quint64 u64 READ u64 WRITE setU64 NOTIFY u64Changed FINAL)
Q_PROPERTY(QString string READ string WRITE setString NOTIFY stringChanged FINAL)
Q_PROPERTY(QByteArray bytearray READ bytearray WRITE setBytearray NOTIFY bytearrayChanged FINAL)
public:
@ -23,12 +23,12 @@ public:
~Object();
bool boolean() const;
void setBoolean(bool v);
int integer() const;
void setInteger(int v);
uint uinteger() const;
qint32 integer() const;
void setInteger(qint32 v);
quint32 uinteger() const;
void setUinteger(uint v);
uint64_t u64() const;
void setU64(uint64_t v);
quint64 u64() const;
void setU64(quint64 v);
QString string() const;
void setString(const QString& v);
QByteArray bytearray() const;
@ -42,9 +42,9 @@ signals:
void bytearrayChanged();
private:
bool m_boolean;
int m_integer;
uint m_uinteger;
uint64_t m_u64;
qint32 m_integer;
quint32 m_uinteger;
quint64 m_u64;
QString m_string;
QByteArray m_bytearray;
};