diff --git a/demo/CMakeLists.txt b/demo/CMakeLists.txt index 1cfa171..e19cbb4 100644 --- a/demo/CMakeLists.txt +++ b/demo/CMakeLists.txt @@ -1,43 +1,13 @@ set(CMAKE_AUTORCC ON) -# generate c++ and rust code from fibonacci.json -add_custom_command( - OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/rust/src/fibonacci_interface.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/Fibonacci.cpp" - COMMAND ${CMAKE_BINARY_DIR}/src/rust_qt_binding_generator "${CMAKE_CURRENT_SOURCE_DIR}/fibonacci.json" - DEPENDS rust_qt_binding_generator fibonacci.json -) - -# generate c++ and rust code from tree.json +# generate c++ and rust code from bindings.json add_custom_command( OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/rust/src/interface.rs" - "${CMAKE_CURRENT_SOURCE_DIR}/src/Tree.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/Tree.cpp" - COMMAND ${CMAKE_BINARY_DIR}/src/rust_qt_binding_generator "${CMAKE_CURRENT_SOURCE_DIR}/tree.json" - DEPENDS rust_qt_binding_generator tree.json -) - -# generate c++ and rust code from processes.json -add_custom_command( - OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/rust/src/processes_interface.rs" - "${CMAKE_CURRENT_SOURCE_DIR}/src/Processes.h" - # if the cpp file is marked GENERATED, CMake will not check it for moc - # "${CMAKE_CURRENT_SOURCE_DIR}/src/Processes.cpp" - COMMAND ${CMAKE_BINARY_DIR}/src/rust_qt_binding_generator "${CMAKE_CURRENT_SOURCE_DIR}/processes.json" - DEPENDS rust_qt_binding_generator processes.json -) - -# generate c++ and rust code from time_series.json -add_custom_command( - OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/rust/src/time_series_interface.rs" - "${CMAKE_CURRENT_SOURCE_DIR}/src/TimeSeries.h" - # if the cpp file is marked GENERATED, CMake will not check it for moc - # "${CMAKE_CURRENT_SOURCE_DIR}/src/TimeSeries.cpp" - COMMAND ${CMAKE_BINARY_DIR}/src/rust_qt_binding_generator "${CMAKE_CURRENT_SOURCE_DIR}/time_series.json" - DEPENDS rust_qt_binding_generator time_series.json + # "${CMAKE_CURRENT_SOURCE_DIR}/src/Bindings.cpp" + COMMAND ${CMAKE_BINARY_DIR}/src/rust_qt_binding_generator "${CMAKE_CURRENT_SOURCE_DIR}/bindings.json" + DEPENDS rust_qt_binding_generator bindings.json ) # compile the rust code into a static library @@ -45,14 +15,11 @@ 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/fibonacci_interface.rs - rust/src/fibonacci_implementation.rs - rust/src/implementation.rs + rust/src/implementation/mod.rs + rust/src/implementation/time_series.rs + rust/src/implementation/fibonacci.rs + rust/src/implementation/processes.rs rust/src/interface.rs - rust/src/time_series_interface.rs - rust/src/time_series_implementation.rs - rust/src/processes_interface.rs - rust/src/processes_implementation.rs WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/rust" ) add_custom_target(rust_target DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/rust/${RUST_TARGET_DIR}/librust.a") @@ -70,8 +37,8 @@ if (Qt5Charts_FOUND) list(APPEND DemoLibs Qt5::Charts) endif() -set(Demo_SRCS src/main.cpp src/Fibonacci.cpp src/Tree.cpp src/TimeSeries.cpp - src/Processes.cpp src/SortedModel.cpp resource_file.qrc) +set(Demo_SRCS src/main.cpp src/Bindings.cpp src/SortedModel.cpp + resource_file.qrc) add_executable(Demo ${Demo_SRCS}) add_dependencies(Demo rust_target) diff --git a/demo/bindings.json b/demo/bindings.json new file mode 100644 index 0000000..71ba97f --- /dev/null +++ b/demo/bindings.json @@ -0,0 +1,143 @@ +{ + "cppFile": "src/Bindings.cpp", + "rust": { + "dir": "rust", + "interfaceModule": "interface", + "implementationModule": "implementation" + }, + "objects": { + "Demo": { + "type": "Object", + "properties": { + "fileSystemTree": { + "type": "FileSystemTree" + }, + "fibonacci": { + "type": "Fibonacci" + }, + "fibonacciList": { + "type": "FibonacciList" + }, + "processes": { + "type": "Processes" + }, + "timeSeries": { + "type": "TimeSeries" + } + } + }, + "FileSystemTree": { + "type": "UniformTree", + "properties": { + "path": { + "type": "QString", + "write": true, + "optional": true + } + }, + "itemProperties": { + "fileName": { + "type": "QString", + "roles": [ ["display"] ], + "rustByValue": true + }, + "fileIcon": { + "type": "QByteArray", + "roles": [ ["decoration"] ] + }, + "fileSize": { + "type": "quint64", + "optional": true, + "roles": [ [], ["display"] ] + }, + "filePath": { + "type": "QString", + "optional": true, + "roles": [ [], [], ["display"] ], + "rustByValue": true + }, + "filePermissions": { + "type": "qint32", + "roles": [ [], [], [], ["display"] ] + }, + "fileType": { + "type": "qint32", + "roles": [ [], [], [], [],["display"] ] + } + } + }, + "Fibonacci": { + "type": "Object", + "properties": { + "input": { + "type": "quint32", + "write": true + }, + "result": { + "type": "quint64" + } + } + }, + "FibonacciList": { + "type": "List", + "itemProperties": { + "result": { + "type": "quint64", + "roles": [ [ "display" ] ] + } + } + }, + "Processes": { + "type": "UniformTree", + "properties": { + "active": { + "type": "bool", + "write": true + } + }, + "itemProperties": { + "pid": { + "type": "quint32", + "roles": [ ["toolTip"] ] + }, + "name": { + "type": "QString", + "roles": [ ["display"] ] + }, + "cpuUsage": { + "type": "float", + "roles": [ [], ["display"] ] + }, + "memory": { + "type": "quint64", + "roles": [ [], [], ["display"] ] + }, + "uid": { + "type": "quint32" + }, + "cpuPercentage": { + "type": "quint8" + }, + "cmd": { + "type": "QString", + "rustByValue": true + } + } + }, + "TimeSeries": { + "type": "List", + "itemProperties": { + "input": { + "type": "quint32", + "write": true, + "roles": [ [ "display", "edit" ] ] + }, + "result": { + "type": "quint32", + "write": true, + "roles": [ [], [ "display", "edit" ] ] + } + } + } + } +} diff --git a/demo/fibonacci.json b/demo/fibonacci.json deleted file mode 100644 index b3ee885..0000000 --- a/demo/fibonacci.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "cppFile": "src/Fibonacci.cpp", - "rust": { - "dir": "rust", - "interfaceModule": "fibonacci_interface", - "implementationModule": "fibonacci_implementation" - }, - "objects": { - "Fibonacci": { - "type": "Object", - "properties": { - "input": { - "type": "quint32", - "write": true - }, - "result": { - "type": "quint64" - } - } - }, - "FibonacciList": { - "type": "List", - "itemProperties": { - "result": { - "type": "quint64", - "roles": [ [ "display" ] ] - } - } - } - } -} diff --git a/demo/processes.json b/demo/processes.json deleted file mode 100644 index ee2c816..0000000 --- a/demo/processes.json +++ /dev/null @@ -1,47 +0,0 @@ -{ - "cppFile": "src/Processes.cpp", - "rust": { - "dir": "rust", - "interfaceModule": "processes_interface", - "implementationModule": "processes_implementation" - }, - "objects": { - "Processes": { - "type": "UniformTree", - "properties": { - "active": { - "type": "bool", - "write": true - } - }, - "itemProperties": { - "pid": { - "type": "quint32", - "roles": [ ["toolTip"] ] - }, - "name": { - "type": "QString", - "roles": [ ["display"] ] - }, - "cpuUsage": { - "type": "float", - "roles": [ [], ["display"] ] - }, - "memory": { - "type": "quint64", - "roles": [ [], [], ["display"] ] - }, - "uid": { - "type": "quint32" - }, - "cpuPercentage": { - "type": "quint8" - }, - "cmd": { - "type": "QString", - "rustByValue": true - } - } - } - } -} diff --git a/demo/rust/src/fibonacci_interface.rs b/demo/rust/src/fibonacci_interface.rs deleted file mode 100644 index 023eafb..0000000 --- a/demo/rust/src/fibonacci_interface.rs +++ /dev/null @@ -1,251 +0,0 @@ -/* generated by rust_qt_binding_generator */ -#![allow(unknown_lints)] -#![allow(mutex_atomic, needless_pass_by_value)] -use libc::{c_int, c_void, uint8_t, uint16_t}; -use std::slice; - -use std::sync::{Arc, Mutex}; -use std::ptr::null; - -use fibonacci_implementation::*; - - -#[repr(C)] -pub struct COption { - data: T, - some: bool, -} - -impl From> for COption -where - T: Default, -{ - fn from(t: Option) -> COption { - if let Some(v) = t { - COption { - data: v, - some: true, - } - } else { - COption { - data: T::default(), - some: false, - } - } - } -} - - -#[repr(C)] -pub enum SortOrder { - Ascending = 0, - Descending = 1, -} - -#[repr(C)] -pub struct QModelIndex { - row: c_int, - internal_id: usize, -} - -pub struct FibonacciQObject {} - -#[derive(Clone)] -pub struct FibonacciEmitter { - qobject: Arc>, - 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 input(&self) -> u32; - fn set_input(&mut self, value: u32); - fn result(&self) -> u64; -} - -#[no_mangle] -pub extern "C" fn fibonacci_new( - fibonacci: *mut FibonacciQObject, - input_changed: fn(*const FibonacciQObject), - result_changed: fn(*const FibonacciQObject), -) -> *mut Fibonacci { - let fibonacci_emit = FibonacciEmitter { - qobject: Arc::new(Mutex::new(fibonacci)), - input_changed: input_changed, - result_changed: result_changed, - }; - let d_fibonacci = Fibonacci::create(fibonacci_emit); - Box::into_raw(Box::new(d_fibonacci)) -} - -#[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).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).result() -} - -pub struct FibonacciListQObject {} - -#[derive(Clone)] -pub struct FibonacciListEmitter { - qobject: Arc>, - 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, - data_changed: fn(*const FibonacciListQObject, usize, usize), - begin_reset_model: fn(*const FibonacciListQObject), - end_reset_model: fn(*const FibonacciListQObject), - begin_insert_rows: fn(*const FibonacciListQObject, usize, usize), - end_insert_rows: fn(*const FibonacciListQObject), - begin_remove_rows: fn(*const FibonacciListQObject, usize, usize), - end_remove_rows: fn(*const FibonacciListQObject), -} - -impl FibonacciListList { - pub fn data_changed(&self, first: usize, last: usize) { - (self.data_changed)(self.qobject, first, last); - } - 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: usize, last: usize) { - (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: usize, last: usize) { - (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) -> usize; - fn can_fetch_more(&self) -> bool { - false - } - fn fetch_more(&mut self) {} - fn sort(&mut self, u8, SortOrder) {} - fn result(&self, item: usize) -> u64; -} - -#[no_mangle] -pub extern "C" fn fibonacci_list_new( - fibonacci_list: *mut FibonacciListQObject, - new_data_ready: fn(*const FibonacciListQObject), - data_changed: fn(*const FibonacciListQObject, usize, usize), - begin_reset_model: fn(*const FibonacciListQObject), - end_reset_model: fn(*const FibonacciListQObject), - begin_insert_rows: fn(*const FibonacciListQObject, usize, usize), - end_insert_rows: fn(*const FibonacciListQObject), - begin_remove_rows: fn(*const FibonacciListQObject, usize, usize), - end_remove_rows: fn(*const FibonacciListQObject), -) -> *mut FibonacciList { - let fibonacci_list_emit = FibonacciListEmitter { - qobject: Arc::new(Mutex::new(fibonacci_list)), - new_data_ready: new_data_ready, - }; - let model = FibonacciListList { - qobject: fibonacci_list, - data_changed: data_changed, - 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_fibonacci_list = FibonacciList::create(fibonacci_list_emit, model); - Box::into_raw(Box::new(d_fibonacci_list)) -} - -#[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() as c_int -} -#[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: u8, - 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 as usize).into() -} diff --git a/demo/rust/src/implementation/demo.rs b/demo/rust/src/implementation/demo.rs new file mode 100644 index 0000000..02920a4 --- /dev/null +++ b/demo/rust/src/implementation/demo.rs @@ -0,0 +1,62 @@ +use interface::*; +use super::*; + +pub struct Demo { + emit: DemoEmitter, + fibonacci: Fibonacci, + fibonacci_list: FibonacciList, + file_system_tree: FileSystemTree, + processes: Processes, + time_series: TimeSeries +} + +impl DemoTrait for Demo { + fn create(emit: DemoEmitter, + fibonacci: Fibonacci, + fibonacci_list: FibonacciList, + file_system_tree: FileSystemTree, + processes: Processes, + time_series: TimeSeries) -> Self { + Demo { + emit: emit, + fibonacci: fibonacci, + fibonacci_list: fibonacci_list, + file_system_tree: file_system_tree, + processes: processes, + time_series: time_series + } + } + fn emit(&self) -> &DemoEmitter { + &self.emit + } + fn fibonacci(&self) -> &Fibonacci { + &self.fibonacci + } + fn fibonacci_mut(&mut self) -> &mut Fibonacci { + &mut self.fibonacci + } + fn fibonacci_list(&self) -> &FibonacciList { + &self.fibonacci_list + } + fn fibonacci_list_mut(&mut self) -> &mut FibonacciList { + &mut self.fibonacci_list + } + fn file_system_tree(&self) -> &FileSystemTree { + &self.file_system_tree + } + fn file_system_tree_mut(&mut self) -> &mut FileSystemTree { + &mut self.file_system_tree + } + fn processes(&self) -> &Processes { + &self.processes + } + fn processes_mut(&mut self) -> &mut Processes { + &mut self.processes + } + fn time_series(&self) -> &TimeSeries { + &self.time_series + } + fn time_series_mut(&mut self) -> &mut TimeSeries { + &mut self.time_series + } +} diff --git a/demo/rust/src/fibonacci_implementation.rs b/demo/rust/src/implementation/fibonacci.rs similarity index 98% rename from demo/rust/src/fibonacci_implementation.rs rename to demo/rust/src/implementation/fibonacci.rs index 611090c..77981b6 100644 --- a/demo/rust/src/fibonacci_implementation.rs +++ b/demo/rust/src/implementation/fibonacci.rs @@ -1,5 +1,5 @@ use std::thread; -use fibonacci_interface::*; +use interface::*; use std::sync::atomic::AtomicUsize; use std::sync::atomic::Ordering; use std::sync::Arc; diff --git a/demo/rust/src/implementation.rs b/demo/rust/src/implementation/file_system_tree.rs similarity index 95% rename from demo/rust/src/implementation.rs rename to demo/rust/src/implementation/file_system_tree.rs index 4a5d82d..c9b2cf7 100644 --- a/demo/rust/src/implementation.rs +++ b/demo/rust/src/implementation/file_system_tree.rs @@ -48,7 +48,7 @@ impl Item for DirEntry { fn icon(&self) -> &[u8] { &self.icon } - fn retrieve(id: usize, parents: Vec<&DirEntry>, q: Incoming, emit: TreeEmitter) { + fn retrieve(id: usize, parents: Vec<&DirEntry>, q: Incoming, emit: FileSystemTreeEmitter) { let mut v = Vec::new(); let path: PathBuf = parents.into_iter().map(|e| &e.name).collect(); thread::spawn(move || { @@ -87,7 +87,7 @@ impl Default for DirEntry { pub trait Item: Default { fn create(name: &str) -> Self; fn can_fetch_more(&self) -> bool; - fn retrieve(id: usize, parents: Vec<&Self>, q: Incoming, emit: TreeEmitter); + fn retrieve(id: usize, parents: Vec<&Self>, q: Incoming, emit: FileSystemTreeEmitter); fn file_name(&self) -> String; fn file_path(&self) -> Option; fn file_permissions(&self) -> i32; @@ -96,7 +96,7 @@ pub trait Item: Default { fn icon(&self) -> &[u8]; } -pub type Tree = RGeneralItemModel; +pub type FileSystemTree = RGeneralItemModel; struct Entry { parent: Option, @@ -106,8 +106,8 @@ struct Entry { } pub struct RGeneralItemModel { - emit: TreeEmitter, - model: TreeUniformTree, + emit: FileSystemTreeEmitter, + model: FileSystemTreeUniformTree, entries: Vec>, path: Option, incoming: Incoming, @@ -185,11 +185,11 @@ where } } -impl TreeTrait for RGeneralItemModel +impl FileSystemTreeTrait for RGeneralItemModel where T: Sync + Send, { - fn create(emit: TreeEmitter, model: TreeUniformTree) -> Self { + fn create(emit: FileSystemTreeEmitter, model: FileSystemTreeUniformTree) -> Self { let mut tree = RGeneralItemModel { emit: emit, model: model, @@ -200,7 +200,7 @@ where tree.reset(); tree } - fn emit(&self) -> &TreeEmitter { + fn emit(&self) -> &FileSystemTreeEmitter { &self.emit } fn path(&self) -> Option<&str> { diff --git a/demo/rust/src/implementation/mod.rs b/demo/rust/src/implementation/mod.rs new file mode 100644 index 0000000..a7935b5 --- /dev/null +++ b/demo/rust/src/implementation/mod.rs @@ -0,0 +1,11 @@ +mod demo; +mod fibonacci; +mod file_system_tree; +mod processes; +mod time_series; + +pub use self::demo::*; +pub use self::fibonacci::*; +pub use self::file_system_tree::*; +pub use self::processes::*; +pub use self::time_series::*; diff --git a/demo/rust/src/processes_implementation.rs b/demo/rust/src/implementation/processes.rs similarity index 86% rename from demo/rust/src/processes_implementation.rs rename to demo/rust/src/implementation/processes.rs index 55515f9..1a9221f 100644 --- a/demo/rust/src/processes_implementation.rs +++ b/demo/rust/src/implementation/processes.rs @@ -1,9 +1,11 @@ -use processes_interface::*; +use interface::*; use sysinfo::*; use std::sync::{Arc, Mutex}; use std::collections::HashMap; use libc::pid_t; -use std::{thread, time}; +use std::thread; +use std::time::Duration; +use std::sync::mpsc::{channel, Sender, Receiver, RecvTimeoutError}; struct ProcessItem { row: usize, @@ -18,12 +20,19 @@ struct ProcessTree { cpusum: f32, } +enum ChangeState { + Active, + Inactive, + Quit +} + pub struct Processes { emit: ProcessesEmitter, model: ProcessesUniformTree, p: ProcessTree, incoming: Arc>>, active: bool, + channel: Sender } fn check_process_hierarchy(parent: Option, processes: &HashMap) { @@ -101,13 +110,27 @@ fn update() -> ProcessTree { p } -fn update_thread(emit: ProcessesEmitter, incoming: Arc>>) { +fn update_thread( + emit: ProcessesEmitter, + incoming: Arc>>, + mut active: bool, + statusChannel: Receiver, +) { thread::spawn(move || { - let second = time::Duration::new(1, 0); loop { - *incoming.lock().unwrap() = Some(update()); - emit.new_data_ready(None); - thread::sleep(second); + let mut timeout = Duration::from_secs(10000); + if active { + *incoming.lock().unwrap() = Some(update()); + emit.new_data_ready(None); + timeout = Duration::from_secs(1); + } + match statusChannel.recv_timeout(timeout) { + Err(RecvTimeoutError::Timeout) => {}, + Err(RecvTimeoutError::Disconnected) => { return; }, + Ok(ChangeState::Active) => { active = true; }, + Ok(ChangeState::Inactive) => { active = false; }, + Ok(ChangeState::Quit) => { return; }, + } } }); } @@ -279,14 +302,16 @@ fn sync_tree( impl ProcessesTrait for Processes { fn create(emit: ProcessesEmitter, model: ProcessesUniformTree) -> Processes { + let (tx, rx) = channel(); let p = Processes { emit: emit.clone(), model: model, p: ProcessTree::default(), incoming: Arc::new(Mutex::new(None)), - active: true, + active: false, + channel: tx, }; - update_thread(emit, p.incoming.clone()); + update_thread(emit, p.incoming.clone(), p.active, rx); p } fn emit(&self) -> &ProcessesEmitter { @@ -371,6 +396,19 @@ impl ProcessesTrait for Processes { self.active } fn set_active(&mut self, active: bool) { - self.active = active + if self.active != active { + self.active = active; + if active { + self.channel.send(ChangeState::Active); + } else { + self.channel.send(ChangeState::Inactive); + } + } + } +} + +impl Drop for Processes { + fn drop(&mut self) { + self.channel.send(ChangeState::Quit); } } diff --git a/demo/rust/src/time_series_implementation.rs b/demo/rust/src/implementation/time_series.rs similarity index 97% rename from demo/rust/src/time_series_implementation.rs rename to demo/rust/src/implementation/time_series.rs index f970e4b..3b3d412 100644 --- a/demo/rust/src/time_series_implementation.rs +++ b/demo/rust/src/implementation/time_series.rs @@ -1,4 +1,4 @@ -use time_series_interface::*; +use interface::*; #[derive(Default, Clone)] struct TimeSeriesItem { diff --git a/demo/rust/src/interface.rs b/demo/rust/src/interface.rs index ea6977d..7332f51 100644 --- a/demo/rust/src/interface.rs +++ b/demo/rust/src/interface.rs @@ -109,18 +109,410 @@ pub struct QModelIndex { internal_id: usize, } -pub struct TreeQObject {} +pub struct DemoQObject {} #[derive(Clone)] -pub struct TreeEmitter { - qobject: Arc>, - path_changed: fn(*const TreeQObject), - new_data_ready: fn(*const TreeQObject, item: usize, valid: bool), +pub struct DemoEmitter { + qobject: Arc>, } -unsafe impl Send for TreeEmitter {} +unsafe impl Send for DemoEmitter {} -impl TreeEmitter { +impl DemoEmitter { + fn clear(&self) { + *self.qobject.lock().unwrap() = null(); + } +} + +pub trait DemoTrait { + fn create(emit: DemoEmitter, + fibonacci: Fibonacci, + fibonacci_list: FibonacciList, + file_system_tree: FileSystemTree, + processes: Processes, + time_series: TimeSeries) -> Self; + fn emit(&self) -> &DemoEmitter; + fn fibonacci(&self) -> &Fibonacci; + fn fibonacci_mut(&mut self) -> &mut Fibonacci; + fn fibonacci_list(&self) -> &FibonacciList; + fn fibonacci_list_mut(&mut self) -> &mut FibonacciList; + fn file_system_tree(&self) -> &FileSystemTree; + fn file_system_tree_mut(&mut self) -> &mut FileSystemTree; + fn processes(&self) -> &Processes; + fn processes_mut(&mut self) -> &mut Processes; + fn time_series(&self) -> &TimeSeries; + fn time_series_mut(&mut self) -> &mut TimeSeries; +} + +#[no_mangle] +pub extern "C" fn demo_new( + demo: *mut DemoQObject, + fibonacci: *mut FibonacciQObject, + input_changed: fn(*const FibonacciQObject), + result_changed: fn(*const FibonacciQObject), + fibonacci_list: *mut FibonacciListQObject, + fibonacci_list_new_data_ready: fn(*const FibonacciListQObject), + fibonacci_list_data_changed: fn(*const FibonacciListQObject, usize, usize), + fibonacci_list_begin_reset_model: fn(*const FibonacciListQObject), + fibonacci_list_end_reset_model: fn(*const FibonacciListQObject), + fibonacci_list_begin_insert_rows: fn(*const FibonacciListQObject, usize, usize), + fibonacci_list_end_insert_rows: fn(*const FibonacciListQObject), + fibonacci_list_begin_remove_rows: fn(*const FibonacciListQObject, usize, usize), + fibonacci_list_end_remove_rows: fn(*const FibonacciListQObject), + file_system_tree: *mut FileSystemTreeQObject, + path_changed: fn(*const FileSystemTreeQObject), + file_system_tree_new_data_ready: fn(*const FileSystemTreeQObject, item: usize, valid: bool), + file_system_tree_data_changed: fn(*const FileSystemTreeQObject, usize, usize), + file_system_tree_begin_reset_model: fn(*const FileSystemTreeQObject), + file_system_tree_end_reset_model: fn(*const FileSystemTreeQObject), + file_system_tree_begin_insert_rows: fn(*const FileSystemTreeQObject, item: usize, valid: bool, usize, usize), + file_system_tree_end_insert_rows: fn(*const FileSystemTreeQObject), + file_system_tree_begin_remove_rows: fn(*const FileSystemTreeQObject, item: usize, valid: bool, usize, usize), + file_system_tree_end_remove_rows: fn(*const FileSystemTreeQObject), + processes: *mut ProcessesQObject, + active_changed: fn(*const ProcessesQObject), + processes_new_data_ready: fn(*const ProcessesQObject, item: usize, valid: bool), + processes_data_changed: fn(*const ProcessesQObject, usize, usize), + processes_begin_reset_model: fn(*const ProcessesQObject), + processes_end_reset_model: fn(*const ProcessesQObject), + processes_begin_insert_rows: fn(*const ProcessesQObject, item: usize, valid: bool, usize, usize), + processes_end_insert_rows: fn(*const ProcessesQObject), + processes_begin_remove_rows: fn(*const ProcessesQObject, item: usize, valid: bool, usize, usize), + processes_end_remove_rows: fn(*const ProcessesQObject), + time_series: *mut TimeSeriesQObject, + time_series_new_data_ready: fn(*const TimeSeriesQObject), + time_series_data_changed: fn(*const TimeSeriesQObject, usize, usize), + time_series_begin_reset_model: fn(*const TimeSeriesQObject), + time_series_end_reset_model: fn(*const TimeSeriesQObject), + time_series_begin_insert_rows: fn(*const TimeSeriesQObject, usize, usize), + time_series_end_insert_rows: fn(*const TimeSeriesQObject), + time_series_begin_remove_rows: fn(*const TimeSeriesQObject, usize, usize), + time_series_end_remove_rows: fn(*const TimeSeriesQObject), +) -> *mut Demo { + let fibonacci_emit = FibonacciEmitter { + qobject: Arc::new(Mutex::new(fibonacci)), + input_changed: input_changed, + result_changed: result_changed, + }; + let d_fibonacci = Fibonacci::create(fibonacci_emit); + let fibonacci_list_emit = FibonacciListEmitter { + qobject: Arc::new(Mutex::new(fibonacci_list)), + new_data_ready: fibonacci_list_new_data_ready, + }; + let model = FibonacciListList { + qobject: fibonacci_list, + data_changed: fibonacci_list_data_changed, + begin_reset_model: fibonacci_list_begin_reset_model, + end_reset_model: fibonacci_list_end_reset_model, + begin_insert_rows: fibonacci_list_begin_insert_rows, + end_insert_rows: fibonacci_list_end_insert_rows, + begin_remove_rows: fibonacci_list_begin_remove_rows, + end_remove_rows: fibonacci_list_end_remove_rows, + }; + let d_fibonacci_list = FibonacciList::create(fibonacci_list_emit, model); + let file_system_tree_emit = FileSystemTreeEmitter { + qobject: Arc::new(Mutex::new(file_system_tree)), + path_changed: path_changed, + new_data_ready: file_system_tree_new_data_ready, + }; + let model = FileSystemTreeUniformTree { + qobject: file_system_tree, + data_changed: file_system_tree_data_changed, + begin_reset_model: file_system_tree_begin_reset_model, + end_reset_model: file_system_tree_end_reset_model, + begin_insert_rows: file_system_tree_begin_insert_rows, + end_insert_rows: file_system_tree_end_insert_rows, + begin_remove_rows: file_system_tree_begin_remove_rows, + end_remove_rows: file_system_tree_end_remove_rows, + }; + let d_file_system_tree = FileSystemTree::create(file_system_tree_emit, model); + let processes_emit = ProcessesEmitter { + qobject: Arc::new(Mutex::new(processes)), + active_changed: active_changed, + new_data_ready: processes_new_data_ready, + }; + let model = ProcessesUniformTree { + qobject: processes, + data_changed: processes_data_changed, + begin_reset_model: processes_begin_reset_model, + end_reset_model: processes_end_reset_model, + begin_insert_rows: processes_begin_insert_rows, + end_insert_rows: processes_end_insert_rows, + begin_remove_rows: processes_begin_remove_rows, + end_remove_rows: processes_end_remove_rows, + }; + let d_processes = Processes::create(processes_emit, model); + let time_series_emit = TimeSeriesEmitter { + qobject: Arc::new(Mutex::new(time_series)), + new_data_ready: time_series_new_data_ready, + }; + let model = TimeSeriesList { + qobject: time_series, + data_changed: time_series_data_changed, + begin_reset_model: time_series_begin_reset_model, + end_reset_model: time_series_end_reset_model, + begin_insert_rows: time_series_begin_insert_rows, + end_insert_rows: time_series_end_insert_rows, + begin_remove_rows: time_series_begin_remove_rows, + end_remove_rows: time_series_end_remove_rows, + }; + let d_time_series = TimeSeries::create(time_series_emit, model); + let demo_emit = DemoEmitter { + qobject: Arc::new(Mutex::new(demo)), + }; + let d_demo = Demo::create(demo_emit, + d_fibonacci, + d_fibonacci_list, + d_file_system_tree, + d_processes, + d_time_series); + Box::into_raw(Box::new(d_demo)) +} + +#[no_mangle] +pub unsafe extern "C" fn demo_free(ptr: *mut Demo) { + Box::from_raw(ptr).emit().clear(); +} + +#[no_mangle] +pub unsafe extern "C" fn demo_fibonacci_get(ptr: *mut Demo) -> *mut Fibonacci { + (&mut *ptr).fibonacci_mut() +} + +#[no_mangle] +pub unsafe extern "C" fn demo_fibonacci_list_get(ptr: *mut Demo) -> *mut FibonacciList { + (&mut *ptr).fibonacci_list_mut() +} + +#[no_mangle] +pub unsafe extern "C" fn demo_file_system_tree_get(ptr: *mut Demo) -> *mut FileSystemTree { + (&mut *ptr).file_system_tree_mut() +} + +#[no_mangle] +pub unsafe extern "C" fn demo_processes_get(ptr: *mut Demo) -> *mut Processes { + (&mut *ptr).processes_mut() +} + +#[no_mangle] +pub unsafe extern "C" fn demo_time_series_get(ptr: *mut Demo) -> *mut TimeSeries { + (&mut *ptr).time_series_mut() +} + +pub struct FibonacciQObject {} + +#[derive(Clone)] +pub struct FibonacciEmitter { + qobject: Arc>, + 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 input(&self) -> u32; + fn set_input(&mut self, value: u32); + fn result(&self) -> u64; +} + +#[no_mangle] +pub extern "C" fn fibonacci_new( + fibonacci: *mut FibonacciQObject, + input_changed: fn(*const FibonacciQObject), + result_changed: fn(*const FibonacciQObject), +) -> *mut Fibonacci { + let fibonacci_emit = FibonacciEmitter { + qobject: Arc::new(Mutex::new(fibonacci)), + input_changed: input_changed, + result_changed: result_changed, + }; + let d_fibonacci = Fibonacci::create(fibonacci_emit); + Box::into_raw(Box::new(d_fibonacci)) +} + +#[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).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).result() +} + +pub struct FibonacciListQObject {} + +#[derive(Clone)] +pub struct FibonacciListEmitter { + qobject: Arc>, + 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, + data_changed: fn(*const FibonacciListQObject, usize, usize), + begin_reset_model: fn(*const FibonacciListQObject), + end_reset_model: fn(*const FibonacciListQObject), + begin_insert_rows: fn(*const FibonacciListQObject, usize, usize), + end_insert_rows: fn(*const FibonacciListQObject), + begin_remove_rows: fn(*const FibonacciListQObject, usize, usize), + end_remove_rows: fn(*const FibonacciListQObject), +} + +impl FibonacciListList { + pub fn data_changed(&self, first: usize, last: usize) { + (self.data_changed)(self.qobject, first, last); + } + 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: usize, last: usize) { + (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: usize, last: usize) { + (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) -> usize; + fn can_fetch_more(&self) -> bool { + false + } + fn fetch_more(&mut self) {} + fn sort(&mut self, u8, SortOrder) {} + fn result(&self, item: usize) -> u64; +} + +#[no_mangle] +pub extern "C" fn fibonacci_list_new( + fibonacci_list: *mut FibonacciListQObject, + fibonacci_list_new_data_ready: fn(*const FibonacciListQObject), + fibonacci_list_data_changed: fn(*const FibonacciListQObject, usize, usize), + fibonacci_list_begin_reset_model: fn(*const FibonacciListQObject), + fibonacci_list_end_reset_model: fn(*const FibonacciListQObject), + fibonacci_list_begin_insert_rows: fn(*const FibonacciListQObject, usize, usize), + fibonacci_list_end_insert_rows: fn(*const FibonacciListQObject), + fibonacci_list_begin_remove_rows: fn(*const FibonacciListQObject, usize, usize), + fibonacci_list_end_remove_rows: fn(*const FibonacciListQObject), +) -> *mut FibonacciList { + let fibonacci_list_emit = FibonacciListEmitter { + qobject: Arc::new(Mutex::new(fibonacci_list)), + new_data_ready: fibonacci_list_new_data_ready, + }; + let model = FibonacciListList { + qobject: fibonacci_list, + data_changed: fibonacci_list_data_changed, + begin_reset_model: fibonacci_list_begin_reset_model, + end_reset_model: fibonacci_list_end_reset_model, + begin_insert_rows: fibonacci_list_begin_insert_rows, + end_insert_rows: fibonacci_list_end_insert_rows, + begin_remove_rows: fibonacci_list_begin_remove_rows, + end_remove_rows: fibonacci_list_end_remove_rows, + }; + let d_fibonacci_list = FibonacciList::create(fibonacci_list_emit, model); + Box::into_raw(Box::new(d_fibonacci_list)) +} + +#[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() as c_int +} +#[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: u8, + 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 as usize).into() +} + +pub struct FileSystemTreeQObject {} + +#[derive(Clone)] +pub struct FileSystemTreeEmitter { + qobject: Arc>, + path_changed: fn(*const FileSystemTreeQObject), + new_data_ready: fn(*const FileSystemTreeQObject, item: usize, valid: bool), +} + +unsafe impl Send for FileSystemTreeEmitter {} + +impl FileSystemTreeEmitter { fn clear(&self) { *self.qobject.lock().unwrap() = null(); } @@ -138,18 +530,18 @@ impl TreeEmitter { } } -pub struct TreeUniformTree { - qobject: *const TreeQObject, - data_changed: fn(*const TreeQObject, usize, usize), - begin_reset_model: fn(*const TreeQObject), - end_reset_model: fn(*const TreeQObject), - begin_insert_rows: fn(*const TreeQObject, item: usize, valid: bool, usize, usize), - end_insert_rows: fn(*const TreeQObject), - begin_remove_rows: fn(*const TreeQObject, item: usize, valid: bool, usize, usize), - end_remove_rows: fn(*const TreeQObject), +pub struct FileSystemTreeUniformTree { + qobject: *const FileSystemTreeQObject, + data_changed: fn(*const FileSystemTreeQObject, usize, usize), + begin_reset_model: fn(*const FileSystemTreeQObject), + end_reset_model: fn(*const FileSystemTreeQObject), + begin_insert_rows: fn(*const FileSystemTreeQObject, item: usize, valid: bool, usize, usize), + end_insert_rows: fn(*const FileSystemTreeQObject), + begin_remove_rows: fn(*const FileSystemTreeQObject, item: usize, valid: bool, usize, usize), + end_remove_rows: fn(*const FileSystemTreeQObject), } -impl TreeUniformTree { +impl FileSystemTreeUniformTree { pub fn data_changed(&self, first: usize, last: usize) { (self.data_changed)(self.qobject, first, last); } @@ -173,9 +565,9 @@ impl TreeUniformTree { } } -pub trait TreeTrait { - fn create(emit: TreeEmitter, model: TreeUniformTree) -> Self; - fn emit(&self) -> &TreeEmitter; +pub trait FileSystemTreeTrait { + fn create(emit: FileSystemTreeEmitter, model: FileSystemTreeUniformTree) -> Self; + fn emit(&self) -> &FileSystemTreeEmitter; fn path(&self) -> Option<&str>; fn set_path(&mut self, value: Option); fn row_count(&self, Option) -> usize; @@ -196,45 +588,45 @@ pub trait TreeTrait { } #[no_mangle] -pub extern "C" fn tree_new( - tree: *mut TreeQObject, - path_changed: fn(*const TreeQObject), - new_data_ready: fn(*const TreeQObject, item: usize, valid: bool), - data_changed: fn(*const TreeQObject, usize, usize), - begin_reset_model: fn(*const TreeQObject), - end_reset_model: fn(*const TreeQObject), - begin_insert_rows: fn(*const TreeQObject, item: usize, valid: bool, usize, usize), - end_insert_rows: fn(*const TreeQObject), - begin_remove_rows: fn(*const TreeQObject, item: usize, valid: bool, usize, usize), - end_remove_rows: fn(*const TreeQObject), -) -> *mut Tree { - let tree_emit = TreeEmitter { - qobject: Arc::new(Mutex::new(tree)), +pub extern "C" fn file_system_tree_new( + file_system_tree: *mut FileSystemTreeQObject, + path_changed: fn(*const FileSystemTreeQObject), + file_system_tree_new_data_ready: fn(*const FileSystemTreeQObject, item: usize, valid: bool), + file_system_tree_data_changed: fn(*const FileSystemTreeQObject, usize, usize), + file_system_tree_begin_reset_model: fn(*const FileSystemTreeQObject), + file_system_tree_end_reset_model: fn(*const FileSystemTreeQObject), + file_system_tree_begin_insert_rows: fn(*const FileSystemTreeQObject, item: usize, valid: bool, usize, usize), + file_system_tree_end_insert_rows: fn(*const FileSystemTreeQObject), + file_system_tree_begin_remove_rows: fn(*const FileSystemTreeQObject, item: usize, valid: bool, usize, usize), + file_system_tree_end_remove_rows: fn(*const FileSystemTreeQObject), +) -> *mut FileSystemTree { + let file_system_tree_emit = FileSystemTreeEmitter { + qobject: Arc::new(Mutex::new(file_system_tree)), path_changed: path_changed, - new_data_ready: new_data_ready, + new_data_ready: file_system_tree_new_data_ready, }; - let model = TreeUniformTree { - qobject: tree, - data_changed: data_changed, - 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 model = FileSystemTreeUniformTree { + qobject: file_system_tree, + data_changed: file_system_tree_data_changed, + begin_reset_model: file_system_tree_begin_reset_model, + end_reset_model: file_system_tree_end_reset_model, + begin_insert_rows: file_system_tree_begin_insert_rows, + end_insert_rows: file_system_tree_end_insert_rows, + begin_remove_rows: file_system_tree_begin_remove_rows, + end_remove_rows: file_system_tree_end_remove_rows, }; - let d_tree = Tree::create(tree_emit, model); - Box::into_raw(Box::new(d_tree)) + let d_file_system_tree = FileSystemTree::create(file_system_tree_emit, model); + Box::into_raw(Box::new(d_file_system_tree)) } #[no_mangle] -pub unsafe extern "C" fn tree_free(ptr: *mut Tree) { +pub unsafe extern "C" fn file_system_tree_free(ptr: *mut FileSystemTree) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] -pub unsafe extern "C" fn tree_path_get( - ptr: *const Tree, +pub unsafe extern "C" fn file_system_tree_path_get( + ptr: *const FileSystemTree, p: *mut c_void, set: fn(*mut c_void, QString), ) { @@ -245,17 +637,17 @@ pub unsafe extern "C" fn tree_path_get( } #[no_mangle] -pub unsafe extern "C" fn tree_path_set(ptr: *mut Tree, v: QStringIn) { +pub unsafe extern "C" fn file_system_tree_path_set(ptr: *mut FileSystemTree, v: QStringIn) { (&mut *ptr).set_path(Some(v.convert())); } #[no_mangle] -pub unsafe extern "C" fn tree_path_set_none(ptr: *mut Tree) { +pub unsafe extern "C" fn file_system_tree_path_set_none(ptr: *mut FileSystemTree) { (&mut *ptr).set_path(None); } #[no_mangle] -pub unsafe extern "C" fn tree_row_count( - ptr: *const Tree, +pub unsafe extern "C" fn file_system_tree_row_count( + ptr: *const FileSystemTree, item: usize, valid: bool, ) -> c_int { @@ -266,8 +658,8 @@ pub unsafe extern "C" fn tree_row_count( } } #[no_mangle] -pub unsafe extern "C" fn tree_can_fetch_more( - ptr: *const Tree, +pub unsafe extern "C" fn file_system_tree_can_fetch_more( + ptr: *const FileSystemTree, item: usize, valid: bool, ) -> bool { @@ -278,7 +670,7 @@ pub unsafe extern "C" fn tree_can_fetch_more( } } #[no_mangle] -pub unsafe extern "C" fn tree_fetch_more(ptr: *mut Tree, item: usize, valid: bool) { +pub unsafe extern "C" fn file_system_tree_fetch_more(ptr: *mut FileSystemTree, item: usize, valid: bool) { if valid { (&mut *ptr).fetch_more(Some(item)) } else { @@ -286,16 +678,16 @@ pub unsafe extern "C" fn tree_fetch_more(ptr: *mut Tree, item: usize, valid: boo } } #[no_mangle] -pub unsafe extern "C" fn tree_sort( - ptr: *mut Tree, +pub unsafe extern "C" fn file_system_tree_sort( + ptr: *mut FileSystemTree, column: u8, order: SortOrder ) { (&mut *ptr).sort(column, order) } #[no_mangle] -pub unsafe extern "C" fn tree_index( - ptr: *const Tree, +pub unsafe extern "C" fn file_system_tree_index( + ptr: *const FileSystemTree, item: usize, valid: bool, row: c_int, @@ -307,7 +699,7 @@ pub unsafe extern "C" fn tree_index( } } #[no_mangle] -pub unsafe extern "C" fn tree_parent(ptr: *const Tree, index: usize) -> QModelIndex { +pub unsafe extern "C" fn file_system_tree_parent(ptr: *const FileSystemTree, index: usize) -> QModelIndex { if let Some(parent) = (&*ptr).parent(index) { QModelIndex { row: (&*ptr).row(parent) as c_int, @@ -321,13 +713,13 @@ pub unsafe extern "C" fn tree_parent(ptr: *const Tree, index: usize) -> QModelIn } } #[no_mangle] -pub unsafe extern "C" fn tree_row(ptr: *const Tree, item: usize) -> c_int { +pub unsafe extern "C" fn file_system_tree_row(ptr: *const FileSystemTree, item: usize) -> c_int { (&*ptr).row(item) as c_int } #[no_mangle] -pub unsafe extern "C" fn tree_data_file_icon( - ptr: *const Tree, item: usize, +pub unsafe extern "C" fn file_system_tree_data_file_icon( + ptr: *const FileSystemTree, item: usize, d: *mut c_void, set: fn(*mut c_void, QByteArray), ) { @@ -336,8 +728,8 @@ pub unsafe extern "C" fn tree_data_file_icon( } #[no_mangle] -pub unsafe extern "C" fn tree_data_file_name( - ptr: *const Tree, item: usize, +pub unsafe extern "C" fn file_system_tree_data_file_name( + ptr: *const FileSystemTree, item: usize, d: *mut c_void, set: fn(*mut c_void, QString), ) { @@ -346,8 +738,8 @@ pub unsafe extern "C" fn tree_data_file_name( } #[no_mangle] -pub unsafe extern "C" fn tree_data_file_path( - ptr: *const Tree, item: usize, +pub unsafe extern "C" fn file_system_tree_data_file_path( + ptr: *const FileSystemTree, item: usize, d: *mut c_void, set: fn(*mut c_void, QString), ) { @@ -358,16 +750,421 @@ pub unsafe extern "C" fn tree_data_file_path( } #[no_mangle] -pub unsafe extern "C" fn tree_data_file_permissions(ptr: *const Tree, item: usize) -> i32 { +pub unsafe extern "C" fn file_system_tree_data_file_permissions(ptr: *const FileSystemTree, item: usize) -> i32 { (&*ptr).file_permissions(item).into() } #[no_mangle] -pub unsafe extern "C" fn tree_data_file_size(ptr: *const Tree, item: usize) -> COption { +pub unsafe extern "C" fn file_system_tree_data_file_size(ptr: *const FileSystemTree, item: usize) -> COption { (&*ptr).file_size(item).into() } #[no_mangle] -pub unsafe extern "C" fn tree_data_file_type(ptr: *const Tree, item: usize) -> i32 { +pub unsafe extern "C" fn file_system_tree_data_file_type(ptr: *const FileSystemTree, item: usize) -> i32 { (&*ptr).file_type(item).into() } + +pub struct ProcessesQObject {} + +#[derive(Clone)] +pub struct ProcessesEmitter { + qobject: Arc>, + active_changed: fn(*const ProcessesQObject), + new_data_ready: fn(*const ProcessesQObject, item: usize, valid: bool), +} + +unsafe impl Send for ProcessesEmitter {} + +impl ProcessesEmitter { + fn clear(&self) { + *self.qobject.lock().unwrap() = null(); + } + pub fn active_changed(&self) { + let ptr = *self.qobject.lock().unwrap(); + if !ptr.is_null() { + (self.active_changed)(ptr); + } + } + pub fn new_data_ready(&self, item: Option) { + let ptr = *self.qobject.lock().unwrap(); + if !ptr.is_null() { + (self.new_data_ready)(ptr, item.unwrap_or(13), item.is_some()); + } + } +} + +pub struct ProcessesUniformTree { + qobject: *const ProcessesQObject, + data_changed: fn(*const ProcessesQObject, usize, usize), + begin_reset_model: fn(*const ProcessesQObject), + end_reset_model: fn(*const ProcessesQObject), + begin_insert_rows: fn(*const ProcessesQObject, item: usize, valid: bool, usize, usize), + end_insert_rows: fn(*const ProcessesQObject), + begin_remove_rows: fn(*const ProcessesQObject, item: usize, valid: bool, usize, usize), + end_remove_rows: fn(*const ProcessesQObject), +} + +impl ProcessesUniformTree { + pub fn data_changed(&self, first: usize, last: usize) { + (self.data_changed)(self.qobject, first, last); + } + 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, item: Option, first: usize, last: usize) { + (self.begin_insert_rows)(self.qobject, item.unwrap_or(13), item.is_some(), first, last); + } + pub fn end_insert_rows(&self) { + (self.end_insert_rows)(self.qobject); + } + pub fn begin_remove_rows(&self, item: Option, first: usize, last: usize) { + (self.begin_remove_rows)(self.qobject, item.unwrap_or(13), item.is_some(), first, last); + } + pub fn end_remove_rows(&self) { + (self.end_remove_rows)(self.qobject); + } +} + +pub trait ProcessesTrait { + fn create(emit: ProcessesEmitter, model: ProcessesUniformTree) -> Self; + fn emit(&self) -> &ProcessesEmitter; + fn active(&self) -> bool; + fn set_active(&mut self, value: bool); + fn row_count(&self, Option) -> usize; + fn can_fetch_more(&self, Option) -> bool { + false + } + fn fetch_more(&mut self, Option) {} + fn sort(&mut self, u8, SortOrder) {} + fn index(&self, item: Option, row: usize) -> usize; + fn parent(&self, item: usize) -> Option; + fn row(&self, item: usize) -> usize; + fn cmd(&self, item: usize) -> String; + fn cpu_percentage(&self, item: usize) -> u8; + fn cpu_usage(&self, item: usize) -> f32; + fn memory(&self, item: usize) -> u64; + fn name(&self, item: usize) -> &str; + fn pid(&self, item: usize) -> u32; + fn uid(&self, item: usize) -> u32; +} + +#[no_mangle] +pub extern "C" fn processes_new( + processes: *mut ProcessesQObject, + active_changed: fn(*const ProcessesQObject), + processes_new_data_ready: fn(*const ProcessesQObject, item: usize, valid: bool), + processes_data_changed: fn(*const ProcessesQObject, usize, usize), + processes_begin_reset_model: fn(*const ProcessesQObject), + processes_end_reset_model: fn(*const ProcessesQObject), + processes_begin_insert_rows: fn(*const ProcessesQObject, item: usize, valid: bool, usize, usize), + processes_end_insert_rows: fn(*const ProcessesQObject), + processes_begin_remove_rows: fn(*const ProcessesQObject, item: usize, valid: bool, usize, usize), + processes_end_remove_rows: fn(*const ProcessesQObject), +) -> *mut Processes { + let processes_emit = ProcessesEmitter { + qobject: Arc::new(Mutex::new(processes)), + active_changed: active_changed, + new_data_ready: processes_new_data_ready, + }; + let model = ProcessesUniformTree { + qobject: processes, + data_changed: processes_data_changed, + begin_reset_model: processes_begin_reset_model, + end_reset_model: processes_end_reset_model, + begin_insert_rows: processes_begin_insert_rows, + end_insert_rows: processes_end_insert_rows, + begin_remove_rows: processes_begin_remove_rows, + end_remove_rows: processes_end_remove_rows, + }; + let d_processes = Processes::create(processes_emit, model); + Box::into_raw(Box::new(d_processes)) +} + +#[no_mangle] +pub unsafe extern "C" fn processes_free(ptr: *mut Processes) { + Box::from_raw(ptr).emit().clear(); +} + +#[no_mangle] +pub unsafe extern "C" fn processes_active_get(ptr: *const Processes) -> bool { + (&*ptr).active() +} + +#[no_mangle] +pub unsafe extern "C" fn processes_active_set(ptr: *mut Processes, v: bool) { + (&mut *ptr).set_active(v); +} + +#[no_mangle] +pub unsafe extern "C" fn processes_row_count( + ptr: *const Processes, + item: usize, + valid: bool, +) -> c_int { + if valid { + (&*ptr).row_count(Some(item)) as c_int + } else { + (&*ptr).row_count(None) as c_int + } +} +#[no_mangle] +pub unsafe extern "C" fn processes_can_fetch_more( + ptr: *const Processes, + item: usize, + valid: bool, +) -> bool { + if valid { + (&*ptr).can_fetch_more(Some(item)) + } else { + (&*ptr).can_fetch_more(None) + } +} +#[no_mangle] +pub unsafe extern "C" fn processes_fetch_more(ptr: *mut Processes, item: usize, valid: bool) { + if valid { + (&mut *ptr).fetch_more(Some(item)) + } else { + (&mut *ptr).fetch_more(None) + } +} +#[no_mangle] +pub unsafe extern "C" fn processes_sort( + ptr: *mut Processes, + column: u8, + order: SortOrder +) { + (&mut *ptr).sort(column, order) +} +#[no_mangle] +pub unsafe extern "C" fn processes_index( + ptr: *const Processes, + item: usize, + valid: bool, + row: c_int, +) -> usize { + if !valid { + (&*ptr).index(None, row as usize) + } else { + (&*ptr).index(Some(item), row as usize) + } +} +#[no_mangle] +pub unsafe extern "C" fn processes_parent(ptr: *const Processes, index: usize) -> QModelIndex { + if let Some(parent) = (&*ptr).parent(index) { + QModelIndex { + row: (&*ptr).row(parent) as c_int, + internal_id: parent, + } + } else { + QModelIndex { + row: -1, + internal_id: 0, + } + } +} +#[no_mangle] +pub unsafe extern "C" fn processes_row(ptr: *const Processes, item: usize) -> c_int { + (&*ptr).row(item) as c_int +} + +#[no_mangle] +pub unsafe extern "C" fn processes_data_cmd( + ptr: *const Processes, item: usize, + d: *mut c_void, + set: fn(*mut c_void, QString), +) { + let data = (&*ptr).cmd(item); + set(d, (&data).into()); +} + +#[no_mangle] +pub unsafe extern "C" fn processes_data_cpu_percentage(ptr: *const Processes, item: usize) -> u8 { + (&*ptr).cpu_percentage(item).into() +} + +#[no_mangle] +pub unsafe extern "C" fn processes_data_cpu_usage(ptr: *const Processes, item: usize) -> f32 { + (&*ptr).cpu_usage(item).into() +} + +#[no_mangle] +pub unsafe extern "C" fn processes_data_memory(ptr: *const Processes, item: usize) -> u64 { + (&*ptr).memory(item).into() +} + +#[no_mangle] +pub unsafe extern "C" fn processes_data_name( + ptr: *const Processes, item: usize, + d: *mut c_void, + set: fn(*mut c_void, QString), +) { + let data = (&*ptr).name(item); + set(d, (data).into()); +} + +#[no_mangle] +pub unsafe extern "C" fn processes_data_pid(ptr: *const Processes, item: usize) -> u32 { + (&*ptr).pid(item).into() +} + +#[no_mangle] +pub unsafe extern "C" fn processes_data_uid(ptr: *const Processes, item: usize) -> u32 { + (&*ptr).uid(item).into() +} + +pub struct TimeSeriesQObject {} + +#[derive(Clone)] +pub struct TimeSeriesEmitter { + qobject: Arc>, + new_data_ready: fn(*const TimeSeriesQObject), +} + +unsafe impl Send for TimeSeriesEmitter {} + +impl TimeSeriesEmitter { + 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 TimeSeriesList { + qobject: *const TimeSeriesQObject, + data_changed: fn(*const TimeSeriesQObject, usize, usize), + begin_reset_model: fn(*const TimeSeriesQObject), + end_reset_model: fn(*const TimeSeriesQObject), + begin_insert_rows: fn(*const TimeSeriesQObject, usize, usize), + end_insert_rows: fn(*const TimeSeriesQObject), + begin_remove_rows: fn(*const TimeSeriesQObject, usize, usize), + end_remove_rows: fn(*const TimeSeriesQObject), +} + +impl TimeSeriesList { + pub fn data_changed(&self, first: usize, last: usize) { + (self.data_changed)(self.qobject, first, last); + } + 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: usize, last: usize) { + (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: usize, last: usize) { + (self.begin_remove_rows)(self.qobject, first, last); + } + pub fn end_remove_rows(&self) { + (self.end_remove_rows)(self.qobject); + } +} + +pub trait TimeSeriesTrait { + fn create(emit: TimeSeriesEmitter, model: TimeSeriesList) -> Self; + fn emit(&self) -> &TimeSeriesEmitter; + fn row_count(&self) -> usize; + fn can_fetch_more(&self) -> bool { + false + } + fn fetch_more(&mut self) {} + fn sort(&mut self, u8, SortOrder) {} + fn input(&self, item: usize) -> u32; + fn set_input(&mut self, item: usize, u32) -> bool; + fn result(&self, item: usize) -> u32; + fn set_result(&mut self, item: usize, u32) -> bool; +} + +#[no_mangle] +pub extern "C" fn time_series_new( + time_series: *mut TimeSeriesQObject, + time_series_new_data_ready: fn(*const TimeSeriesQObject), + time_series_data_changed: fn(*const TimeSeriesQObject, usize, usize), + time_series_begin_reset_model: fn(*const TimeSeriesQObject), + time_series_end_reset_model: fn(*const TimeSeriesQObject), + time_series_begin_insert_rows: fn(*const TimeSeriesQObject, usize, usize), + time_series_end_insert_rows: fn(*const TimeSeriesQObject), + time_series_begin_remove_rows: fn(*const TimeSeriesQObject, usize, usize), + time_series_end_remove_rows: fn(*const TimeSeriesQObject), +) -> *mut TimeSeries { + let time_series_emit = TimeSeriesEmitter { + qobject: Arc::new(Mutex::new(time_series)), + new_data_ready: time_series_new_data_ready, + }; + let model = TimeSeriesList { + qobject: time_series, + data_changed: time_series_data_changed, + begin_reset_model: time_series_begin_reset_model, + end_reset_model: time_series_end_reset_model, + begin_insert_rows: time_series_begin_insert_rows, + end_insert_rows: time_series_end_insert_rows, + begin_remove_rows: time_series_begin_remove_rows, + end_remove_rows: time_series_end_remove_rows, + }; + let d_time_series = TimeSeries::create(time_series_emit, model); + Box::into_raw(Box::new(d_time_series)) +} + +#[no_mangle] +pub unsafe extern "C" fn time_series_free(ptr: *mut TimeSeries) { + Box::from_raw(ptr).emit().clear(); +} + +#[no_mangle] +pub unsafe extern "C" fn time_series_row_count(ptr: *const TimeSeries) -> c_int { + (&*ptr).row_count() as c_int +} +#[no_mangle] +pub unsafe extern "C" fn time_series_can_fetch_more(ptr: *const TimeSeries) -> bool { + (&*ptr).can_fetch_more() +} +#[no_mangle] +pub unsafe extern "C" fn time_series_fetch_more(ptr: *mut TimeSeries) { + (&mut *ptr).fetch_more() +} +#[no_mangle] +pub unsafe extern "C" fn time_series_sort( + ptr: *mut TimeSeries, + column: u8, + order: SortOrder, +) { + (&mut *ptr).sort(column, order) +} + +#[no_mangle] +pub unsafe extern "C" fn time_series_data_input(ptr: *const TimeSeries, row: c_int) -> u32 { + (&*ptr).input(row as usize).into() +} + +#[no_mangle] +pub unsafe extern "C" fn time_series_set_data_input( + ptr: *mut TimeSeries, row: c_int, + v: u32, +) -> bool { + (&mut *ptr).set_input(row as usize, v) +} + +#[no_mangle] +pub unsafe extern "C" fn time_series_data_result(ptr: *const TimeSeries, row: c_int) -> u32 { + (&*ptr).result(row as usize).into() +} + +#[no_mangle] +pub unsafe extern "C" fn time_series_set_data_result( + ptr: *mut TimeSeries, row: c_int, + v: u32, +) -> bool { + (&mut *ptr).set_result(row as usize, v) +} diff --git a/demo/rust/src/lib.rs b/demo/rust/src/lib.rs index 7b854f6..78763bf 100644 --- a/demo/rust/src/lib.rs +++ b/demo/rust/src/lib.rs @@ -1,14 +1,5 @@ extern crate libc; extern crate sysinfo; -mod fibonacci_implementation; -pub mod fibonacci_interface; - mod implementation; pub mod interface; - -mod time_series_implementation; -pub mod time_series_interface; - -mod processes_implementation; -pub mod processes_interface; diff --git a/demo/rust/src/processes_interface.rs b/demo/rust/src/processes_interface.rs deleted file mode 100644 index 57003b6..0000000 --- a/demo/rust/src/processes_interface.rs +++ /dev/null @@ -1,338 +0,0 @@ -/* generated by rust_qt_binding_generator */ -#![allow(unknown_lints)] -#![allow(mutex_atomic, needless_pass_by_value)] -use libc::{c_int, c_void, uint8_t, uint16_t}; -use std::slice; - -use std::sync::{Arc, Mutex}; -use std::ptr::null; - -use processes_implementation::*; - - -#[repr(C)] -pub struct COption { - data: T, - some: bool, -} - -impl From> for COption -where - T: Default, -{ - fn from(t: Option) -> COption { - if let Some(v) = t { - COption { - data: v, - some: true, - } - } else { - COption { - data: T::default(), - some: false, - } - } - } -} - - -#[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 { - 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 str> for QString { - fn from(string: &'a str) -> QString { - QString { - len: string.len() as c_int, - data: string.as_ptr(), - } - } -} - -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 enum SortOrder { - Ascending = 0, - Descending = 1, -} - -#[repr(C)] -pub struct QModelIndex { - row: c_int, - internal_id: usize, -} - -pub struct ProcessesQObject {} - -#[derive(Clone)] -pub struct ProcessesEmitter { - qobject: Arc>, - active_changed: fn(*const ProcessesQObject), - new_data_ready: fn(*const ProcessesQObject, item: usize, valid: bool), -} - -unsafe impl Send for ProcessesEmitter {} - -impl ProcessesEmitter { - fn clear(&self) { - *self.qobject.lock().unwrap() = null(); - } - pub fn active_changed(&self) { - let ptr = *self.qobject.lock().unwrap(); - if !ptr.is_null() { - (self.active_changed)(ptr); - } - } - pub fn new_data_ready(&self, item: Option) { - let ptr = *self.qobject.lock().unwrap(); - if !ptr.is_null() { - (self.new_data_ready)(ptr, item.unwrap_or(13), item.is_some()); - } - } -} - -pub struct ProcessesUniformTree { - qobject: *const ProcessesQObject, - data_changed: fn(*const ProcessesQObject, usize, usize), - begin_reset_model: fn(*const ProcessesQObject), - end_reset_model: fn(*const ProcessesQObject), - begin_insert_rows: fn(*const ProcessesQObject, item: usize, valid: bool, usize, usize), - end_insert_rows: fn(*const ProcessesQObject), - begin_remove_rows: fn(*const ProcessesQObject, item: usize, valid: bool, usize, usize), - end_remove_rows: fn(*const ProcessesQObject), -} - -impl ProcessesUniformTree { - pub fn data_changed(&self, first: usize, last: usize) { - (self.data_changed)(self.qobject, first, last); - } - 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, item: Option, first: usize, last: usize) { - (self.begin_insert_rows)(self.qobject, item.unwrap_or(13), item.is_some(), first, last); - } - pub fn end_insert_rows(&self) { - (self.end_insert_rows)(self.qobject); - } - pub fn begin_remove_rows(&self, item: Option, first: usize, last: usize) { - (self.begin_remove_rows)(self.qobject, item.unwrap_or(13), item.is_some(), first, last); - } - pub fn end_remove_rows(&self) { - (self.end_remove_rows)(self.qobject); - } -} - -pub trait ProcessesTrait { - fn create(emit: ProcessesEmitter, model: ProcessesUniformTree) -> Self; - fn emit(&self) -> &ProcessesEmitter; - fn active(&self) -> bool; - fn set_active(&mut self, value: bool); - fn row_count(&self, Option) -> usize; - fn can_fetch_more(&self, Option) -> bool { - false - } - fn fetch_more(&mut self, Option) {} - fn sort(&mut self, u8, SortOrder) {} - fn index(&self, item: Option, row: usize) -> usize; - fn parent(&self, item: usize) -> Option; - fn row(&self, item: usize) -> usize; - fn cmd(&self, item: usize) -> String; - fn cpu_percentage(&self, item: usize) -> u8; - fn cpu_usage(&self, item: usize) -> f32; - fn memory(&self, item: usize) -> u64; - fn name(&self, item: usize) -> &str; - fn pid(&self, item: usize) -> u32; - fn uid(&self, item: usize) -> u32; -} - -#[no_mangle] -pub extern "C" fn processes_new( - processes: *mut ProcessesQObject, - active_changed: fn(*const ProcessesQObject), - new_data_ready: fn(*const ProcessesQObject, item: usize, valid: bool), - data_changed: fn(*const ProcessesQObject, usize, usize), - begin_reset_model: fn(*const ProcessesQObject), - end_reset_model: fn(*const ProcessesQObject), - begin_insert_rows: fn(*const ProcessesQObject, item: usize, valid: bool, usize, usize), - end_insert_rows: fn(*const ProcessesQObject), - begin_remove_rows: fn(*const ProcessesQObject, item: usize, valid: bool, usize, usize), - end_remove_rows: fn(*const ProcessesQObject), -) -> *mut Processes { - let processes_emit = ProcessesEmitter { - qobject: Arc::new(Mutex::new(processes)), - active_changed: active_changed, - new_data_ready: new_data_ready, - }; - let model = ProcessesUniformTree { - qobject: processes, - data_changed: data_changed, - 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_processes = Processes::create(processes_emit, model); - Box::into_raw(Box::new(d_processes)) -} - -#[no_mangle] -pub unsafe extern "C" fn processes_free(ptr: *mut Processes) { - Box::from_raw(ptr).emit().clear(); -} - -#[no_mangle] -pub unsafe extern "C" fn processes_active_get(ptr: *const Processes) -> bool { - (&*ptr).active() -} - -#[no_mangle] -pub unsafe extern "C" fn processes_active_set(ptr: *mut Processes, v: bool) { - (&mut *ptr).set_active(v); -} - -#[no_mangle] -pub unsafe extern "C" fn processes_row_count( - ptr: *const Processes, - item: usize, - valid: bool, -) -> c_int { - if valid { - (&*ptr).row_count(Some(item)) as c_int - } else { - (&*ptr).row_count(None) as c_int - } -} -#[no_mangle] -pub unsafe extern "C" fn processes_can_fetch_more( - ptr: *const Processes, - item: usize, - valid: bool, -) -> bool { - if valid { - (&*ptr).can_fetch_more(Some(item)) - } else { - (&*ptr).can_fetch_more(None) - } -} -#[no_mangle] -pub unsafe extern "C" fn processes_fetch_more(ptr: *mut Processes, item: usize, valid: bool) { - if valid { - (&mut *ptr).fetch_more(Some(item)) - } else { - (&mut *ptr).fetch_more(None) - } -} -#[no_mangle] -pub unsafe extern "C" fn processes_sort( - ptr: *mut Processes, - column: u8, - order: SortOrder -) { - (&mut *ptr).sort(column, order) -} -#[no_mangle] -pub unsafe extern "C" fn processes_index( - ptr: *const Processes, - item: usize, - valid: bool, - row: c_int, -) -> usize { - if !valid { - (&*ptr).index(None, row as usize) - } else { - (&*ptr).index(Some(item), row as usize) - } -} -#[no_mangle] -pub unsafe extern "C" fn processes_parent(ptr: *const Processes, index: usize) -> QModelIndex { - if let Some(parent) = (&*ptr).parent(index) { - QModelIndex { - row: (&*ptr).row(parent) as c_int, - internal_id: parent, - } - } else { - QModelIndex { - row: -1, - internal_id: 0, - } - } -} -#[no_mangle] -pub unsafe extern "C" fn processes_row(ptr: *const Processes, item: usize) -> c_int { - (&*ptr).row(item) as c_int -} - -#[no_mangle] -pub unsafe extern "C" fn processes_data_cmd( - ptr: *const Processes, item: usize, - d: *mut c_void, - set: fn(*mut c_void, QString), -) { - let data = (&*ptr).cmd(item); - set(d, (&data).into()); -} - -#[no_mangle] -pub unsafe extern "C" fn processes_data_cpu_percentage(ptr: *const Processes, item: usize) -> u8 { - (&*ptr).cpu_percentage(item).into() -} - -#[no_mangle] -pub unsafe extern "C" fn processes_data_cpu_usage(ptr: *const Processes, item: usize) -> f32 { - (&*ptr).cpu_usage(item).into() -} - -#[no_mangle] -pub unsafe extern "C" fn processes_data_memory(ptr: *const Processes, item: usize) -> u64 { - (&*ptr).memory(item).into() -} - -#[no_mangle] -pub unsafe extern "C" fn processes_data_name( - ptr: *const Processes, item: usize, - d: *mut c_void, - set: fn(*mut c_void, QString), -) { - let data = (&*ptr).name(item); - set(d, (data).into()); -} - -#[no_mangle] -pub unsafe extern "C" fn processes_data_pid(ptr: *const Processes, item: usize) -> u32 { - (&*ptr).pid(item).into() -} - -#[no_mangle] -pub unsafe extern "C" fn processes_data_uid(ptr: *const Processes, item: usize) -> u32 { - (&*ptr).uid(item).into() -} diff --git a/demo/rust/src/time_series_interface.rs b/demo/rust/src/time_series_interface.rs deleted file mode 100644 index f37506a..0000000 --- a/demo/rust/src/time_series_interface.rs +++ /dev/null @@ -1,203 +0,0 @@ -/* generated by rust_qt_binding_generator */ -#![allow(unknown_lints)] -#![allow(mutex_atomic, needless_pass_by_value)] -use libc::{c_int, c_void, uint8_t, uint16_t}; -use std::slice; - -use std::sync::{Arc, Mutex}; -use std::ptr::null; - -use time_series_implementation::*; - - -#[repr(C)] -pub struct COption { - data: T, - some: bool, -} - -impl From> for COption -where - T: Default, -{ - fn from(t: Option) -> COption { - if let Some(v) = t { - COption { - data: v, - some: true, - } - } else { - COption { - data: T::default(), - some: false, - } - } - } -} - - -#[repr(C)] -pub enum SortOrder { - Ascending = 0, - Descending = 1, -} - -#[repr(C)] -pub struct QModelIndex { - row: c_int, - internal_id: usize, -} - -pub struct TimeSeriesQObject {} - -#[derive(Clone)] -pub struct TimeSeriesEmitter { - qobject: Arc>, - new_data_ready: fn(*const TimeSeriesQObject), -} - -unsafe impl Send for TimeSeriesEmitter {} - -impl TimeSeriesEmitter { - 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 TimeSeriesList { - qobject: *const TimeSeriesQObject, - data_changed: fn(*const TimeSeriesQObject, usize, usize), - begin_reset_model: fn(*const TimeSeriesQObject), - end_reset_model: fn(*const TimeSeriesQObject), - begin_insert_rows: fn(*const TimeSeriesQObject, usize, usize), - end_insert_rows: fn(*const TimeSeriesQObject), - begin_remove_rows: fn(*const TimeSeriesQObject, usize, usize), - end_remove_rows: fn(*const TimeSeriesQObject), -} - -impl TimeSeriesList { - pub fn data_changed(&self, first: usize, last: usize) { - (self.data_changed)(self.qobject, first, last); - } - 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: usize, last: usize) { - (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: usize, last: usize) { - (self.begin_remove_rows)(self.qobject, first, last); - } - pub fn end_remove_rows(&self) { - (self.end_remove_rows)(self.qobject); - } -} - -pub trait TimeSeriesTrait { - fn create(emit: TimeSeriesEmitter, model: TimeSeriesList) -> Self; - fn emit(&self) -> &TimeSeriesEmitter; - fn row_count(&self) -> usize; - fn can_fetch_more(&self) -> bool { - false - } - fn fetch_more(&mut self) {} - fn sort(&mut self, u8, SortOrder) {} - fn input(&self, item: usize) -> u32; - fn set_input(&mut self, item: usize, u32) -> bool; - fn result(&self, item: usize) -> u32; - fn set_result(&mut self, item: usize, u32) -> bool; -} - -#[no_mangle] -pub extern "C" fn time_series_new( - time_series: *mut TimeSeriesQObject, - new_data_ready: fn(*const TimeSeriesQObject), - data_changed: fn(*const TimeSeriesQObject, usize, usize), - begin_reset_model: fn(*const TimeSeriesQObject), - end_reset_model: fn(*const TimeSeriesQObject), - begin_insert_rows: fn(*const TimeSeriesQObject, usize, usize), - end_insert_rows: fn(*const TimeSeriesQObject), - begin_remove_rows: fn(*const TimeSeriesQObject, usize, usize), - end_remove_rows: fn(*const TimeSeriesQObject), -) -> *mut TimeSeries { - let time_series_emit = TimeSeriesEmitter { - qobject: Arc::new(Mutex::new(time_series)), - new_data_ready: new_data_ready, - }; - let model = TimeSeriesList { - qobject: time_series, - data_changed: data_changed, - 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_time_series = TimeSeries::create(time_series_emit, model); - Box::into_raw(Box::new(d_time_series)) -} - -#[no_mangle] -pub unsafe extern "C" fn time_series_free(ptr: *mut TimeSeries) { - Box::from_raw(ptr).emit().clear(); -} - -#[no_mangle] -pub unsafe extern "C" fn time_series_row_count(ptr: *const TimeSeries) -> c_int { - (&*ptr).row_count() as c_int -} -#[no_mangle] -pub unsafe extern "C" fn time_series_can_fetch_more(ptr: *const TimeSeries) -> bool { - (&*ptr).can_fetch_more() -} -#[no_mangle] -pub unsafe extern "C" fn time_series_fetch_more(ptr: *mut TimeSeries) { - (&mut *ptr).fetch_more() -} -#[no_mangle] -pub unsafe extern "C" fn time_series_sort( - ptr: *mut TimeSeries, - column: u8, - order: SortOrder, -) { - (&mut *ptr).sort(column, order) -} - -#[no_mangle] -pub unsafe extern "C" fn time_series_data_input(ptr: *const TimeSeries, row: c_int) -> u32 { - (&*ptr).input(row as usize).into() -} - -#[no_mangle] -pub unsafe extern "C" fn time_series_set_data_input( - ptr: *mut TimeSeries, row: c_int, - v: u32, -) -> bool { - (&mut *ptr).set_input(row as usize, v) -} - -#[no_mangle] -pub unsafe extern "C" fn time_series_data_result(ptr: *const TimeSeries, row: c_int) -> u32 { - (&*ptr).result(row as usize).into() -} - -#[no_mangle] -pub unsafe extern "C" fn time_series_set_data_result( - ptr: *mut TimeSeries, row: c_int, - v: u32, -) -> bool { - (&mut *ptr).set_result(row as usize, v) -} diff --git a/demo/src/Bindings.cpp b/demo/src/Bindings.cpp new file mode 100644 index 0000000..5a4d9bd --- /dev/null +++ b/demo/src/Bindings.cpp @@ -0,0 +1,1317 @@ +/* generated by rust_qt_binding_generator */ +#include "Bindings.h" + +namespace { + template + struct option { + public: + T value; + bool some; + operator QVariant() const { + if (some) { + return QVariant(value); + } + return QVariant(); + } + }; + 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(v.utf16())), + len(v.size()) { + } + operator QString() const { + return QString::fromUtf8(static_cast(data), len); + } + }; + struct qmodelindex_t { + int row; + quintptr id; + }; + inline void fibonacciInputChanged(Fibonacci* o) + { + emit o->inputChanged(); + } + inline void fibonacciResultChanged(Fibonacci* o) + { + emit o->resultChanged(); + } + inline void fileSystemTreePathChanged(FileSystemTree* o) + { + emit o->pathChanged(); + } + inline void processesActiveChanged(Processes* o) + { + emit o->activeChanged(); + } + +} +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" { + Demo::Private* demo_new(Demo*, Fibonacci*, void (*)(Fibonacci*), void (*)(Fibonacci*), FibonacciList*, + void (*)(const FibonacciList*), + void (*)(FibonacciList*, quintptr, quintptr), + void (*)(FibonacciList*), + void (*)(FibonacciList*), + void (*)(FibonacciList*, int, int), + void (*)(FibonacciList*), + void (*)(FibonacciList*, int, int), + void (*)(FibonacciList*), FileSystemTree*, void (*)(FileSystemTree*), + void (*)(const FileSystemTree*, quintptr, bool), + void (*)(FileSystemTree*, quintptr, quintptr), + void (*)(FileSystemTree*), + void (*)(FileSystemTree*), + void (*)(FileSystemTree*, option, int, int), + void (*)(FileSystemTree*), + void (*)(FileSystemTree*, option, int, int), + void (*)(FileSystemTree*), Processes*, void (*)(Processes*), + void (*)(const Processes*, quintptr, bool), + void (*)(Processes*, quintptr, quintptr), + void (*)(Processes*), + void (*)(Processes*), + void (*)(Processes*, option, int, int), + void (*)(Processes*), + void (*)(Processes*, option, int, int), + void (*)(Processes*), TimeSeries*, + void (*)(const TimeSeries*), + void (*)(TimeSeries*, quintptr, quintptr), + void (*)(TimeSeries*), + void (*)(TimeSeries*), + void (*)(TimeSeries*, int, int), + void (*)(TimeSeries*), + void (*)(TimeSeries*, int, int), + void (*)(TimeSeries*)); + void demo_free(Demo::Private*); + Fibonacci::Private* demo_fibonacci_get(const Demo::Private*); + FibonacciList::Private* demo_fibonacci_list_get(const Demo::Private*); + FileSystemTree::Private* demo_file_system_tree_get(const Demo::Private*); + Processes::Private* demo_processes_get(const Demo::Private*); + TimeSeries::Private* demo_time_series_get(const Demo::Private*); +}; + +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*); +}; + +extern "C" { + quint64 fibonacci_list_data_result(const FibonacciList::Private*, int); + void fibonacci_list_sort(FibonacciList::Private*, unsigned char 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(m_d); +} + +QModelIndex FibonacciList::index(int row, int column, const QModelIndex &parent) const +{ + if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < 1) { + return createIndex(row, column, (quintptr)row); + } + 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(m_d); +} + +void FibonacciList::fetchMore(const QModelIndex &parent) +{ + if (!parent.isValid()) { + fibonacci_list_fetch_more(m_d); + } +} + +void FibonacciList::sort(int column, Qt::SortOrder order) +{ + fibonacci_list_sort(m_d, column, order); +} +Qt::ItemFlags FibonacciList::flags(const QModelIndex &i) const +{ + auto flags = QAbstractItemModel::flags(i); + return flags; +} +QVariant FibonacciList::data(const QModelIndex &index, int role) const +{ + QVariant v; + Q_ASSERT(rowCount(index.parent()) > index.row()); + QString s; + QByteArray b; + switch (index.column()) { + case 0: + switch (role) { + case Qt::DisplayRole: + case Qt::UserRole + 0: + v = fibonacci_list_data_result(m_d, index.row()); + break; + } + break; + } + return v; +} +QHash FibonacciList::roleNames() const { + QHash names = QAbstractItemModel::roleNames(); + names.insert(Qt::UserRole + 0, "result"); + return names; +} +QVariant FibonacciList::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (orientation != Qt::Horizontal) { + return QVariant(); + } + return m_headerData.value(qMakePair(section, (Qt::ItemDataRole)role), role == Qt::DisplayRole ?QString::number(section + 1) :QVariant()); +} + +bool FibonacciList::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) +{ + if (orientation != Qt::Horizontal) { + return false; + } + m_headerData.insert(qMakePair(section, (Qt::ItemDataRole)role), value); + return true; +} + +bool FibonacciList::setData(const QModelIndex &index, const QVariant &value, int role) +{ + bool set = false; + if (set) { + emit dataChanged(index, index, QVector() << role); + } + return set; +} +extern "C" { + FibonacciList::Private* fibonacci_list_new(FibonacciList*, + void (*)(const FibonacciList*), + void (*)(FibonacciList*, quintptr, quintptr), + void (*)(FibonacciList*), + void (*)(FibonacciList*), + void (*)(FibonacciList*, int, int), + void (*)(FibonacciList*), + void (*)(FibonacciList*, int, int), + void (*)(FibonacciList*)); + void fibonacci_list_free(FibonacciList::Private*); +}; + +extern "C" { + void file_system_tree_data_file_icon(const FileSystemTree::Private*, quintptr, QByteArray*, qbytearray_set); + void file_system_tree_data_file_name(const FileSystemTree::Private*, quintptr, QString*, qstring_set); + void file_system_tree_data_file_path(const FileSystemTree::Private*, quintptr, QString*, qstring_set); + qint32 file_system_tree_data_file_permissions(const FileSystemTree::Private*, quintptr); + option file_system_tree_data_file_size(const FileSystemTree::Private*, quintptr); + qint32 file_system_tree_data_file_type(const FileSystemTree::Private*, quintptr); + void file_system_tree_sort(FileSystemTree::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder); + + int file_system_tree_row_count(const FileSystemTree::Private*, quintptr, bool); + bool file_system_tree_can_fetch_more(const FileSystemTree::Private*, quintptr, bool); + void file_system_tree_fetch_more(FileSystemTree::Private*, quintptr, bool); + quintptr file_system_tree_index(const FileSystemTree::Private*, quintptr, bool, int); + qmodelindex_t file_system_tree_parent(const FileSystemTree::Private*, quintptr); + int file_system_tree_row(const FileSystemTree::Private*, quintptr); +} +int FileSystemTree::columnCount(const QModelIndex &) const +{ + return 5; +} + +bool FileSystemTree::hasChildren(const QModelIndex &parent) const +{ + return rowCount(parent) > 0; +} + +int FileSystemTree::rowCount(const QModelIndex &parent) const +{ + if (parent.isValid() && parent.column() != 0) { + return 0; + } + return file_system_tree_row_count(m_d, parent.internalId(), parent.isValid()); +} + +QModelIndex FileSystemTree::index(int row, int column, const QModelIndex &parent) const +{ + if (row < 0 || column < 0 || column >= 5) { + return QModelIndex(); + } + if (parent.isValid() && parent.column() != 0) { + return QModelIndex(); + } + if (row >= rowCount(parent)) { + return QModelIndex(); + } + const quintptr id = file_system_tree_index(m_d, parent.internalId(), parent.isValid(), row); + return createIndex(row, column, id); +} + +QModelIndex FileSystemTree::parent(const QModelIndex &index) const +{ + if (!index.isValid()) { + return QModelIndex(); + } + const qmodelindex_t parent = file_system_tree_parent(m_d, index.internalId()); + return parent.row >= 0 ?createIndex(parent.row, 0, parent.id) :QModelIndex(); +} + +bool FileSystemTree::canFetchMore(const QModelIndex &parent) const +{ + if (parent.isValid() && parent.column() != 0) { + return false; + } + return file_system_tree_can_fetch_more(m_d, parent.internalId(), parent.isValid()); +} + +void FileSystemTree::fetchMore(const QModelIndex &parent) +{ + file_system_tree_fetch_more(m_d, parent.internalId(), parent.isValid()); +} + +void FileSystemTree::sort(int column, Qt::SortOrder order) +{ + file_system_tree_sort(m_d, column, order); +} +Qt::ItemFlags FileSystemTree::flags(const QModelIndex &i) const +{ + auto flags = QAbstractItemModel::flags(i); + return flags; +} +QVariant FileSystemTree::data(const QModelIndex &index, int role) const +{ + QVariant v; + Q_ASSERT(rowCount(index.parent()) > index.row()); + QString s; + QByteArray b; + switch (index.column()) { + case 0: + switch (role) { + case Qt::DecorationRole: + case Qt::UserRole + 0: + file_system_tree_data_file_icon(m_d, index.internalId(), &b, set_qbytearray); + if (!b.isNull()) v.setValue(b); + break; + case Qt::DisplayRole: + case Qt::UserRole + 1: + file_system_tree_data_file_name(m_d, index.internalId(), &s, set_qstring); + if (!s.isNull()) v.setValue(s); + break; + case Qt::UserRole + 2: + file_system_tree_data_file_path(m_d, index.internalId(), &s, set_qstring); + if (!s.isNull()) v.setValue(s); + break; + case Qt::UserRole + 3: + v = file_system_tree_data_file_permissions(m_d, index.internalId()); + break; + case Qt::UserRole + 4: + v = file_system_tree_data_file_size(m_d, index.internalId()); + break; + case Qt::UserRole + 5: + v = file_system_tree_data_file_type(m_d, index.internalId()); + break; + } + break; + case 1: + switch (role) { + case Qt::DisplayRole: + case Qt::UserRole + 4: + v = file_system_tree_data_file_size(m_d, index.internalId()); + break; + } + break; + case 2: + switch (role) { + case Qt::DisplayRole: + case Qt::UserRole + 2: + file_system_tree_data_file_path(m_d, index.internalId(), &s, set_qstring); + if (!s.isNull()) v.setValue(s); + break; + } + break; + case 3: + switch (role) { + case Qt::DisplayRole: + case Qt::UserRole + 3: + v = file_system_tree_data_file_permissions(m_d, index.internalId()); + break; + } + break; + case 4: + switch (role) { + case Qt::DisplayRole: + case Qt::UserRole + 5: + v = file_system_tree_data_file_type(m_d, index.internalId()); + break; + } + break; + } + return v; +} +QHash FileSystemTree::roleNames() const { + QHash names = QAbstractItemModel::roleNames(); + names.insert(Qt::UserRole + 0, "fileIcon"); + names.insert(Qt::UserRole + 1, "fileName"); + names.insert(Qt::UserRole + 2, "filePath"); + names.insert(Qt::UserRole + 3, "filePermissions"); + names.insert(Qt::UserRole + 4, "fileSize"); + names.insert(Qt::UserRole + 5, "fileType"); + return names; +} +QVariant FileSystemTree::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (orientation != Qt::Horizontal) { + return QVariant(); + } + return m_headerData.value(qMakePair(section, (Qt::ItemDataRole)role), role == Qt::DisplayRole ?QString::number(section + 1) :QVariant()); +} + +bool FileSystemTree::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) +{ + if (orientation != Qt::Horizontal) { + return false; + } + m_headerData.insert(qMakePair(section, (Qt::ItemDataRole)role), value); + return true; +} + +bool FileSystemTree::setData(const QModelIndex &index, const QVariant &value, int role) +{ + bool set = false; + if (set) { + emit dataChanged(index, index, QVector() << role); + } + return set; +} +extern "C" { + FileSystemTree::Private* file_system_tree_new(FileSystemTree*, void (*)(FileSystemTree*), + void (*)(const FileSystemTree*, quintptr, bool), + void (*)(FileSystemTree*, quintptr, quintptr), + void (*)(FileSystemTree*), + void (*)(FileSystemTree*), + void (*)(FileSystemTree*, option, int, int), + void (*)(FileSystemTree*), + void (*)(FileSystemTree*, option, int, int), + void (*)(FileSystemTree*)); + void file_system_tree_free(FileSystemTree::Private*); + void file_system_tree_path_get(const FileSystemTree::Private*, QString*, qstring_set); + void file_system_tree_path_set(FileSystemTree::Private*, qstring_t); + void file_system_tree_path_set_none(FileSystemTree::Private*); +}; + +extern "C" { + void processes_data_cmd(const Processes::Private*, quintptr, QString*, qstring_set); + quint8 processes_data_cpu_percentage(const Processes::Private*, quintptr); + float processes_data_cpu_usage(const Processes::Private*, quintptr); + quint64 processes_data_memory(const Processes::Private*, quintptr); + void processes_data_name(const Processes::Private*, quintptr, QString*, qstring_set); + uint processes_data_pid(const Processes::Private*, quintptr); + uint processes_data_uid(const Processes::Private*, quintptr); + void processes_sort(Processes::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder); + + int processes_row_count(const Processes::Private*, quintptr, bool); + bool processes_can_fetch_more(const Processes::Private*, quintptr, bool); + void processes_fetch_more(Processes::Private*, quintptr, bool); + quintptr processes_index(const Processes::Private*, quintptr, bool, int); + qmodelindex_t processes_parent(const Processes::Private*, quintptr); + int processes_row(const Processes::Private*, quintptr); +} +int Processes::columnCount(const QModelIndex &) const +{ + return 3; +} + +bool Processes::hasChildren(const QModelIndex &parent) const +{ + return rowCount(parent) > 0; +} + +int Processes::rowCount(const QModelIndex &parent) const +{ + if (parent.isValid() && parent.column() != 0) { + return 0; + } + return processes_row_count(m_d, parent.internalId(), parent.isValid()); +} + +QModelIndex Processes::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(); + } + if (row >= rowCount(parent)) { + return QModelIndex(); + } + const quintptr id = processes_index(m_d, parent.internalId(), parent.isValid(), row); + return createIndex(row, column, id); +} + +QModelIndex Processes::parent(const QModelIndex &index) const +{ + if (!index.isValid()) { + return QModelIndex(); + } + const qmodelindex_t parent = processes_parent(m_d, index.internalId()); + return parent.row >= 0 ?createIndex(parent.row, 0, parent.id) :QModelIndex(); +} + +bool Processes::canFetchMore(const QModelIndex &parent) const +{ + if (parent.isValid() && parent.column() != 0) { + return false; + } + return processes_can_fetch_more(m_d, parent.internalId(), parent.isValid()); +} + +void Processes::fetchMore(const QModelIndex &parent) +{ + processes_fetch_more(m_d, parent.internalId(), parent.isValid()); +} + +void Processes::sort(int column, Qt::SortOrder order) +{ + processes_sort(m_d, column, order); +} +Qt::ItemFlags Processes::flags(const QModelIndex &i) const +{ + auto flags = QAbstractItemModel::flags(i); + return flags; +} +QVariant Processes::data(const QModelIndex &index, int role) const +{ + QVariant v; + Q_ASSERT(rowCount(index.parent()) > index.row()); + QString s; + QByteArray b; + switch (index.column()) { + case 0: + switch (role) { + case Qt::UserRole + 0: + processes_data_cmd(m_d, index.internalId(), &s, set_qstring); + if (!s.isNull()) v.setValue(s); + break; + case Qt::UserRole + 1: + v = processes_data_cpu_percentage(m_d, index.internalId()); + break; + case Qt::UserRole + 2: + v = processes_data_cpu_usage(m_d, index.internalId()); + break; + case Qt::UserRole + 3: + v = processes_data_memory(m_d, index.internalId()); + break; + case Qt::DisplayRole: + case Qt::UserRole + 4: + processes_data_name(m_d, index.internalId(), &s, set_qstring); + if (!s.isNull()) v.setValue(s); + break; + case Qt::ToolTipRole: + case Qt::UserRole + 5: + v = processes_data_pid(m_d, index.internalId()); + break; + case Qt::UserRole + 6: + v = processes_data_uid(m_d, index.internalId()); + break; + } + break; + case 1: + switch (role) { + case Qt::DisplayRole: + case Qt::UserRole + 2: + v = processes_data_cpu_usage(m_d, index.internalId()); + break; + } + break; + case 2: + switch (role) { + case Qt::DisplayRole: + case Qt::UserRole + 3: + v = processes_data_memory(m_d, index.internalId()); + break; + } + break; + } + return v; +} +QHash Processes::roleNames() const { + QHash names = QAbstractItemModel::roleNames(); + names.insert(Qt::UserRole + 0, "cmd"); + names.insert(Qt::UserRole + 1, "cpuPercentage"); + names.insert(Qt::UserRole + 2, "cpuUsage"); + names.insert(Qt::UserRole + 3, "memory"); + names.insert(Qt::UserRole + 4, "name"); + names.insert(Qt::UserRole + 5, "pid"); + names.insert(Qt::UserRole + 6, "uid"); + return names; +} +QVariant Processes::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (orientation != Qt::Horizontal) { + return QVariant(); + } + return m_headerData.value(qMakePair(section, (Qt::ItemDataRole)role), role == Qt::DisplayRole ?QString::number(section + 1) :QVariant()); +} + +bool Processes::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) +{ + if (orientation != Qt::Horizontal) { + return false; + } + m_headerData.insert(qMakePair(section, (Qt::ItemDataRole)role), value); + return true; +} + +bool Processes::setData(const QModelIndex &index, const QVariant &value, int role) +{ + bool set = false; + if (set) { + emit dataChanged(index, index, QVector() << role); + } + return set; +} +extern "C" { + Processes::Private* processes_new(Processes*, void (*)(Processes*), + void (*)(const Processes*, quintptr, bool), + void (*)(Processes*, quintptr, quintptr), + void (*)(Processes*), + void (*)(Processes*), + void (*)(Processes*, option, int, int), + void (*)(Processes*), + void (*)(Processes*, option, int, int), + void (*)(Processes*)); + void processes_free(Processes::Private*); + bool processes_active_get(const Processes::Private*); + void processes_active_set(Processes::Private*, bool); +}; + +extern "C" { + uint time_series_data_input(const TimeSeries::Private*, int); + bool time_series_set_data_input(TimeSeries::Private*, int, uint); + uint time_series_data_result(const TimeSeries::Private*, int); + bool time_series_set_data_result(TimeSeries::Private*, int, uint); + void time_series_sort(TimeSeries::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder); + + int time_series_row_count(const TimeSeries::Private*); + bool time_series_can_fetch_more(const TimeSeries::Private*); + void time_series_fetch_more(TimeSeries::Private*); +} +int TimeSeries::columnCount(const QModelIndex &parent) const +{ + return (parent.isValid()) ? 0 : 2; +} + +bool TimeSeries::hasChildren(const QModelIndex &parent) const +{ + return rowCount(parent) > 0; +} + +int TimeSeries::rowCount(const QModelIndex &parent) const +{ + return (parent.isValid()) ? 0 : time_series_row_count(m_d); +} + +QModelIndex TimeSeries::index(int row, int column, const QModelIndex &parent) const +{ + if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < 2) { + return createIndex(row, column, (quintptr)row); + } + return QModelIndex(); +} + +QModelIndex TimeSeries::parent(const QModelIndex &) const +{ + return QModelIndex(); +} + +bool TimeSeries::canFetchMore(const QModelIndex &parent) const +{ + return (parent.isValid()) ? 0 : time_series_can_fetch_more(m_d); +} + +void TimeSeries::fetchMore(const QModelIndex &parent) +{ + if (!parent.isValid()) { + time_series_fetch_more(m_d); + } +} + +void TimeSeries::sort(int column, Qt::SortOrder order) +{ + time_series_sort(m_d, column, order); +} +Qt::ItemFlags TimeSeries::flags(const QModelIndex &i) const +{ + auto flags = QAbstractItemModel::flags(i); + if (i.column() == 0) { + flags |= Qt::ItemIsEditable; + } + if (i.column() == 1) { + flags |= Qt::ItemIsEditable; + } + return flags; +} +QVariant TimeSeries::data(const QModelIndex &index, int role) const +{ + QVariant v; + Q_ASSERT(rowCount(index.parent()) > index.row()); + QString s; + QByteArray b; + switch (index.column()) { + case 0: + switch (role) { + case Qt::DisplayRole: + case Qt::EditRole: + case Qt::UserRole + 0: + v = time_series_data_input(m_d, index.row()); + break; + case Qt::UserRole + 1: + v = time_series_data_result(m_d, index.row()); + break; + } + break; + case 1: + switch (role) { + case Qt::DisplayRole: + case Qt::EditRole: + case Qt::UserRole + 1: + v = time_series_data_result(m_d, index.row()); + break; + } + break; + } + return v; +} +QHash TimeSeries::roleNames() const { + QHash names = QAbstractItemModel::roleNames(); + names.insert(Qt::UserRole + 0, "input"); + names.insert(Qt::UserRole + 1, "result"); + return names; +} +QVariant TimeSeries::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (orientation != Qt::Horizontal) { + return QVariant(); + } + return m_headerData.value(qMakePair(section, (Qt::ItemDataRole)role), role == Qt::DisplayRole ?QString::number(section + 1) :QVariant()); +} + +bool TimeSeries::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) +{ + if (orientation != Qt::Horizontal) { + return false; + } + m_headerData.insert(qMakePair(section, (Qt::ItemDataRole)role), value); + return true; +} + +bool TimeSeries::setData(const QModelIndex &index, const QVariant &value, int role) +{ + bool set = false; + if (index.column() == 0) { + if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 0) { + set = time_series_set_data_input(m_d, index.row(), value.value()); + } + if (role == Qt::UserRole + 1) { + set = time_series_set_data_result(m_d, index.row(), value.value()); + } + } + if (index.column() == 1) { + if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 1) { + set = time_series_set_data_result(m_d, index.row(), value.value()); + } + } + if (set) { + emit dataChanged(index, index, QVector() << role); + } + return set; +} +extern "C" { + TimeSeries::Private* time_series_new(TimeSeries*, + void (*)(const TimeSeries*), + void (*)(TimeSeries*, quintptr, quintptr), + void (*)(TimeSeries*), + void (*)(TimeSeries*), + void (*)(TimeSeries*, int, int), + void (*)(TimeSeries*), + void (*)(TimeSeries*, int, int), + void (*)(TimeSeries*)); + void time_series_free(TimeSeries::Private*); +}; + +Demo::Demo(bool /*owned*/, QObject *parent): + QObject(parent), + m_fibonacci(new Fibonacci(false, this)), + m_fibonacciList(new FibonacciList(false, this)), + m_fileSystemTree(new FileSystemTree(false, this)), + m_processes(new Processes(false, this)), + m_timeSeries(new TimeSeries(false, this)), + m_d(0), + m_ownsPrivate(false) +{ +} + +Demo::Demo(QObject *parent): + QObject(parent), + m_fibonacci(new Fibonacci(false, this)), + m_fibonacciList(new FibonacciList(false, this)), + m_fileSystemTree(new FileSystemTree(false, this)), + m_processes(new Processes(false, this)), + m_timeSeries(new TimeSeries(false, this)), + m_d(demo_new(this, m_fibonacci, + fibonacciInputChanged, + fibonacciResultChanged, m_fibonacciList, + [](const FibonacciList* o) { + emit o->newDataReady(QModelIndex()); + }, + [](FibonacciList* o, quintptr first, quintptr last) { + o->dataChanged(o->createIndex(first, 0, first), + o->createIndex(last, 0, last)); + }, + [](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(); + } +, m_fileSystemTree, + fileSystemTreePathChanged, + [](const FileSystemTree* o, quintptr id, bool valid) { + if (valid) { + int row = file_system_tree_row(o->m_d, id); + emit o->newDataReady(o->createIndex(row, 0, id)); + } else { + emit o->newDataReady(QModelIndex()); + } + }, + [](FileSystemTree* o, quintptr first, quintptr last) { + quintptr frow = file_system_tree_row(o->m_d, first); + quintptr lrow = file_system_tree_row(o->m_d, first); + o->dataChanged(o->createIndex(frow, 0, first), + o->createIndex(lrow, 4, last)); + }, + [](FileSystemTree* o) { + o->beginResetModel(); + }, + [](FileSystemTree* o) { + o->endResetModel(); + }, + [](FileSystemTree* o, option id, int first, int last) { + if (id.some) { + int row = file_system_tree_row(o->m_d, id.value); + o->beginInsertRows(o->createIndex(row, 0, id.value), first, last); + } else { + o->beginInsertRows(QModelIndex(), first, last); + } + }, + [](FileSystemTree* o) { + o->endInsertRows(); + }, + [](FileSystemTree* o, option id, int first, int last) { + if (id.some) { + int row = file_system_tree_row(o->m_d, id.value); + o->beginRemoveRows(o->createIndex(row, 0, id.value), first, last); + } else { + o->beginRemoveRows(QModelIndex(), first, last); + } + }, + [](FileSystemTree* o) { + o->endRemoveRows(); + } +, m_processes, + processesActiveChanged, + [](const Processes* o, quintptr id, bool valid) { + if (valid) { + int row = processes_row(o->m_d, id); + emit o->newDataReady(o->createIndex(row, 0, id)); + } else { + emit o->newDataReady(QModelIndex()); + } + }, + [](Processes* o, quintptr first, quintptr last) { + quintptr frow = processes_row(o->m_d, first); + quintptr lrow = processes_row(o->m_d, first); + o->dataChanged(o->createIndex(frow, 0, first), + o->createIndex(lrow, 2, last)); + }, + [](Processes* o) { + o->beginResetModel(); + }, + [](Processes* o) { + o->endResetModel(); + }, + [](Processes* o, option id, int first, int last) { + if (id.some) { + int row = processes_row(o->m_d, id.value); + o->beginInsertRows(o->createIndex(row, 0, id.value), first, last); + } else { + o->beginInsertRows(QModelIndex(), first, last); + } + }, + [](Processes* o) { + o->endInsertRows(); + }, + [](Processes* o, option id, int first, int last) { + if (id.some) { + int row = processes_row(o->m_d, id.value); + o->beginRemoveRows(o->createIndex(row, 0, id.value), first, last); + } else { + o->beginRemoveRows(QModelIndex(), first, last); + } + }, + [](Processes* o) { + o->endRemoveRows(); + } +, m_timeSeries, + [](const TimeSeries* o) { + emit o->newDataReady(QModelIndex()); + }, + [](TimeSeries* o, quintptr first, quintptr last) { + o->dataChanged(o->createIndex(first, 0, first), + o->createIndex(last, 1, last)); + }, + [](TimeSeries* o) { + o->beginResetModel(); + }, + [](TimeSeries* o) { + o->endResetModel(); + }, + [](TimeSeries* o, int first, int last) { + o->beginInsertRows(QModelIndex(), first, last); + }, + [](TimeSeries* o) { + o->endInsertRows(); + }, + [](TimeSeries* o, int first, int last) { + o->beginRemoveRows(QModelIndex(), first, last); + }, + [](TimeSeries* o) { + o->endRemoveRows(); + } +)), + m_ownsPrivate(true) +{ + m_fibonacci->m_d = demo_fibonacci_get(m_d); + m_fibonacciList->m_d = demo_fibonacci_list_get(m_d); + m_fileSystemTree->m_d = demo_file_system_tree_get(m_d); + m_processes->m_d = demo_processes_get(m_d); + m_timeSeries->m_d = demo_time_series_get(m_d); + connect(this->m_fibonacciList, &FibonacciList::newDataReady, this->m_fibonacciList, [this](const QModelIndex& i) { + this->m_fibonacciList->fetchMore(i); + }, Qt::QueuedConnection); + connect(this->m_fileSystemTree, &FileSystemTree::newDataReady, this->m_fileSystemTree, [this](const QModelIndex& i) { + this->m_fileSystemTree->fetchMore(i); + }, Qt::QueuedConnection); + connect(this->m_processes, &Processes::newDataReady, this->m_processes, [this](const QModelIndex& i) { + this->m_processes->fetchMore(i); + }, Qt::QueuedConnection); + connect(this->m_timeSeries, &TimeSeries::newDataReady, this->m_timeSeries, [this](const QModelIndex& i) { + this->m_timeSeries->fetchMore(i); + }, Qt::QueuedConnection); +} + +Demo::~Demo() { + if (m_ownsPrivate) { + demo_free(m_d); + } +} +const Fibonacci* Demo::fibonacci() const +{ + return m_fibonacci; +} +Fibonacci* Demo::fibonacci() +{ + return m_fibonacci; +} +const FibonacciList* Demo::fibonacciList() const +{ + return m_fibonacciList; +} +FibonacciList* Demo::fibonacciList() +{ + return m_fibonacciList; +} +const FileSystemTree* Demo::fileSystemTree() const +{ + return m_fileSystemTree; +} +FileSystemTree* Demo::fileSystemTree() +{ + return m_fileSystemTree; +} +const Processes* Demo::processes() const +{ + return m_processes; +} +Processes* Demo::processes() +{ + return m_processes; +} +const TimeSeries* Demo::timeSeries() const +{ + return m_timeSeries; +} +TimeSeries* Demo::timeSeries() +{ + return m_timeSeries; +} +Fibonacci::Fibonacci(bool /*owned*/, QObject *parent): + QObject(parent), + m_d(0), + m_ownsPrivate(false) +{ +} + +Fibonacci::Fibonacci(QObject *parent): + QObject(parent), + m_d(fibonacci_new(this, + fibonacciInputChanged, + fibonacciResultChanged)), + m_ownsPrivate(true) +{ +} + +Fibonacci::~Fibonacci() { + if (m_ownsPrivate) { + fibonacci_free(m_d); + } +} +quint32 Fibonacci::input() const +{ + return fibonacci_input_get(m_d); +} +void Fibonacci::setInput(uint v) { + fibonacci_input_set(m_d, v); +} +quint64 Fibonacci::result() const +{ + return fibonacci_result_get(m_d); +} +FibonacciList::FibonacciList(bool /*owned*/, QObject *parent): + QAbstractItemModel(parent), + m_d(0), + m_ownsPrivate(false) +{ + initHeaderData(); +} + +FibonacciList::FibonacciList(QObject *parent): + QAbstractItemModel(parent), + m_d(fibonacci_list_new(this, + [](const FibonacciList* o) { + emit o->newDataReady(QModelIndex()); + }, + [](FibonacciList* o, quintptr first, quintptr last) { + o->dataChanged(o->createIndex(first, 0, first), + o->createIndex(last, 0, last)); + }, + [](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(); + } +)), + m_ownsPrivate(true) +{ + connect(this, &FibonacciList::newDataReady, this, [this](const QModelIndex& i) { + this->fetchMore(i); + }, Qt::QueuedConnection); + initHeaderData(); +} + +FibonacciList::~FibonacciList() { + if (m_ownsPrivate) { + fibonacci_list_free(m_d); + } +} +void FibonacciList::initHeaderData() { + m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("result")); + } +FileSystemTree::FileSystemTree(bool /*owned*/, QObject *parent): + QAbstractItemModel(parent), + m_d(0), + m_ownsPrivate(false) +{ + initHeaderData(); +} + +FileSystemTree::FileSystemTree(QObject *parent): + QAbstractItemModel(parent), + m_d(file_system_tree_new(this, + fileSystemTreePathChanged, + [](const FileSystemTree* o, quintptr id, bool valid) { + if (valid) { + int row = file_system_tree_row(o->m_d, id); + emit o->newDataReady(o->createIndex(row, 0, id)); + } else { + emit o->newDataReady(QModelIndex()); + } + }, + [](FileSystemTree* o, quintptr first, quintptr last) { + quintptr frow = file_system_tree_row(o->m_d, first); + quintptr lrow = file_system_tree_row(o->m_d, first); + o->dataChanged(o->createIndex(frow, 0, first), + o->createIndex(lrow, 4, last)); + }, + [](FileSystemTree* o) { + o->beginResetModel(); + }, + [](FileSystemTree* o) { + o->endResetModel(); + }, + [](FileSystemTree* o, option id, int first, int last) { + if (id.some) { + int row = file_system_tree_row(o->m_d, id.value); + o->beginInsertRows(o->createIndex(row, 0, id.value), first, last); + } else { + o->beginInsertRows(QModelIndex(), first, last); + } + }, + [](FileSystemTree* o) { + o->endInsertRows(); + }, + [](FileSystemTree* o, option id, int first, int last) { + if (id.some) { + int row = file_system_tree_row(o->m_d, id.value); + o->beginRemoveRows(o->createIndex(row, 0, id.value), first, last); + } else { + o->beginRemoveRows(QModelIndex(), first, last); + } + }, + [](FileSystemTree* o) { + o->endRemoveRows(); + } +)), + m_ownsPrivate(true) +{ + connect(this, &FileSystemTree::newDataReady, this, [this](const QModelIndex& i) { + this->fetchMore(i); + }, Qt::QueuedConnection); + initHeaderData(); +} + +FileSystemTree::~FileSystemTree() { + if (m_ownsPrivate) { + file_system_tree_free(m_d); + } +} +void FileSystemTree::initHeaderData() { + m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("fileName")); + m_headerData.insert(qMakePair(1, Qt::DisplayRole), QVariant("fileSize")); + m_headerData.insert(qMakePair(2, Qt::DisplayRole), QVariant("filePath")); + m_headerData.insert(qMakePair(3, Qt::DisplayRole), QVariant("filePermissions")); + m_headerData.insert(qMakePair(4, Qt::DisplayRole), QVariant("fileType")); + } +QString FileSystemTree::path() const +{ + QString v; + file_system_tree_path_get(m_d, &v, set_qstring); + return v; +} +void FileSystemTree::setPath(const QString& v) { + if (v.isNull()) { + file_system_tree_path_set_none(m_d); + } else { + file_system_tree_path_set(m_d, v); + } +} +Processes::Processes(bool /*owned*/, QObject *parent): + QAbstractItemModel(parent), + m_d(0), + m_ownsPrivate(false) +{ + initHeaderData(); +} + +Processes::Processes(QObject *parent): + QAbstractItemModel(parent), + m_d(processes_new(this, + processesActiveChanged, + [](const Processes* o, quintptr id, bool valid) { + if (valid) { + int row = processes_row(o->m_d, id); + emit o->newDataReady(o->createIndex(row, 0, id)); + } else { + emit o->newDataReady(QModelIndex()); + } + }, + [](Processes* o, quintptr first, quintptr last) { + quintptr frow = processes_row(o->m_d, first); + quintptr lrow = processes_row(o->m_d, first); + o->dataChanged(o->createIndex(frow, 0, first), + o->createIndex(lrow, 2, last)); + }, + [](Processes* o) { + o->beginResetModel(); + }, + [](Processes* o) { + o->endResetModel(); + }, + [](Processes* o, option id, int first, int last) { + if (id.some) { + int row = processes_row(o->m_d, id.value); + o->beginInsertRows(o->createIndex(row, 0, id.value), first, last); + } else { + o->beginInsertRows(QModelIndex(), first, last); + } + }, + [](Processes* o) { + o->endInsertRows(); + }, + [](Processes* o, option id, int first, int last) { + if (id.some) { + int row = processes_row(o->m_d, id.value); + o->beginRemoveRows(o->createIndex(row, 0, id.value), first, last); + } else { + o->beginRemoveRows(QModelIndex(), first, last); + } + }, + [](Processes* o) { + o->endRemoveRows(); + } +)), + m_ownsPrivate(true) +{ + connect(this, &Processes::newDataReady, this, [this](const QModelIndex& i) { + this->fetchMore(i); + }, Qt::QueuedConnection); + initHeaderData(); +} + +Processes::~Processes() { + if (m_ownsPrivate) { + processes_free(m_d); + } +} +void Processes::initHeaderData() { + m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("name")); + m_headerData.insert(qMakePair(1, Qt::DisplayRole), QVariant("cpuUsage")); + m_headerData.insert(qMakePair(2, Qt::DisplayRole), QVariant("memory")); + } +bool Processes::active() const +{ + return processes_active_get(m_d); +} +void Processes::setActive(bool v) { + processes_active_set(m_d, v); +} +TimeSeries::TimeSeries(bool /*owned*/, QObject *parent): + QAbstractItemModel(parent), + m_d(0), + m_ownsPrivate(false) +{ + initHeaderData(); +} + +TimeSeries::TimeSeries(QObject *parent): + QAbstractItemModel(parent), + m_d(time_series_new(this, + [](const TimeSeries* o) { + emit o->newDataReady(QModelIndex()); + }, + [](TimeSeries* o, quintptr first, quintptr last) { + o->dataChanged(o->createIndex(first, 0, first), + o->createIndex(last, 1, last)); + }, + [](TimeSeries* o) { + o->beginResetModel(); + }, + [](TimeSeries* o) { + o->endResetModel(); + }, + [](TimeSeries* o, int first, int last) { + o->beginInsertRows(QModelIndex(), first, last); + }, + [](TimeSeries* o) { + o->endInsertRows(); + }, + [](TimeSeries* o, int first, int last) { + o->beginRemoveRows(QModelIndex(), first, last); + }, + [](TimeSeries* o) { + o->endRemoveRows(); + } +)), + m_ownsPrivate(true) +{ + connect(this, &TimeSeries::newDataReady, this, [this](const QModelIndex& i) { + this->fetchMore(i); + }, Qt::QueuedConnection); + initHeaderData(); +} + +TimeSeries::~TimeSeries() { + if (m_ownsPrivate) { + time_series_free(m_d); + } +} +void TimeSeries::initHeaderData() { + m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("input")); + m_headerData.insert(qMakePair(1, Qt::DisplayRole), QVariant("result")); + } diff --git a/demo/src/Bindings.h b/demo/src/Bindings.h new file mode 100644 index 0000000..1d33a51 --- /dev/null +++ b/demo/src/Bindings.h @@ -0,0 +1,228 @@ +/* generated by rust_qt_binding_generator */ +#ifndef BINDINGS_H +#define BINDINGS_H + +#include +#include + +class Demo; +class Fibonacci; +class FibonacciList; +class FileSystemTree; +class Processes; +class TimeSeries; + +class Demo : public QObject +{ + Q_OBJECT +public: + class Private; +private: + Fibonacci* const m_fibonacci; + FibonacciList* const m_fibonacciList; + FileSystemTree* const m_fileSystemTree; + Processes* const m_processes; + TimeSeries* const m_timeSeries; + Private * m_d; + bool m_ownsPrivate; + Q_PROPERTY(Fibonacci* fibonacci READ fibonacci FINAL) + Q_PROPERTY(FibonacciList* fibonacciList READ fibonacciList FINAL) + Q_PROPERTY(FileSystemTree* fileSystemTree READ fileSystemTree FINAL) + Q_PROPERTY(Processes* processes READ processes FINAL) + Q_PROPERTY(TimeSeries* timeSeries READ timeSeries FINAL) + explicit Demo(bool owned, QObject *parent); +public: + explicit Demo(QObject *parent = nullptr); + ~Demo(); + const Fibonacci* fibonacci() const; + Fibonacci* fibonacci(); + const FibonacciList* fibonacciList() const; + FibonacciList* fibonacciList(); + const FileSystemTree* fileSystemTree() const; + FileSystemTree* fileSystemTree(); + const Processes* processes() const; + Processes* processes(); + const TimeSeries* timeSeries() const; + TimeSeries* timeSeries(); +signals: +}; + +class Fibonacci : public QObject +{ + Q_OBJECT + friend class Demo; +public: + class Private; +private: + Private * m_d; + bool m_ownsPrivate; + Q_PROPERTY(quint32 input READ input WRITE setInput NOTIFY inputChanged FINAL) + Q_PROPERTY(quint64 result READ result NOTIFY resultChanged FINAL) + explicit Fibonacci(bool owned, QObject *parent); +public: + explicit Fibonacci(QObject *parent = nullptr); + ~Fibonacci(); + quint32 input() const; + void setInput(uint v); + quint64 result() const; +signals: + void inputChanged(); + void resultChanged(); +}; + +class FibonacciList : public QAbstractItemModel +{ + Q_OBJECT + friend class Demo; +public: + class Private; +private: + Private * m_d; + bool m_ownsPrivate; + explicit FibonacciList(bool owned, QObject *parent); +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; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) 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; + Qt::ItemFlags flags(const QModelIndex &index) const override; + void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; + QHash roleNames() const override; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole); +signals: + // new data is ready to be made available to the model with fetchMore() + void newDataReady(const QModelIndex &parent) const; +private: + QHash, QVariant> m_headerData; + void initHeaderData(); +signals: +}; + +class FileSystemTree : public QAbstractItemModel +{ + Q_OBJECT + friend class Demo; +public: + class Private; +private: + Private * m_d; + bool m_ownsPrivate; + Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged FINAL) + explicit FileSystemTree(bool owned, QObject *parent); +public: + explicit FileSystemTree(QObject *parent = nullptr); + ~FileSystemTree(); + 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; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) 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; + Qt::ItemFlags flags(const QModelIndex &index) const override; + void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; + QHash roleNames() const override; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole); +signals: + // new data is ready to be made available to the model with fetchMore() + void newDataReady(const QModelIndex &parent) const; +private: + QHash, QVariant> m_headerData; + void initHeaderData(); +signals: + void pathChanged(); +}; + +class Processes : public QAbstractItemModel +{ + Q_OBJECT + friend class Demo; +public: + class Private; +private: + Private * m_d; + bool m_ownsPrivate; + Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged FINAL) + explicit Processes(bool owned, QObject *parent); +public: + explicit Processes(QObject *parent = nullptr); + ~Processes(); + bool active() const; + void setActive(bool v); + + int columnCount(const QModelIndex &parent = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) 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; + Qt::ItemFlags flags(const QModelIndex &index) const override; + void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; + QHash roleNames() const override; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole); +signals: + // new data is ready to be made available to the model with fetchMore() + void newDataReady(const QModelIndex &parent) const; +private: + QHash, QVariant> m_headerData; + void initHeaderData(); +signals: + void activeChanged(); +}; + +class TimeSeries : public QAbstractItemModel +{ + Q_OBJECT + friend class Demo; +public: + class Private; +private: + Private * m_d; + bool m_ownsPrivate; + explicit TimeSeries(bool owned, QObject *parent); +public: + explicit TimeSeries(QObject *parent = nullptr); + ~TimeSeries(); + + int columnCount(const QModelIndex &parent = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) 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; + Qt::ItemFlags flags(const QModelIndex &index) const override; + void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; + QHash roleNames() const override; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole); +signals: + // new data is ready to be made available to the model with fetchMore() + void newDataReady(const QModelIndex &parent) const; +private: + QHash, QVariant> m_headerData; + void initHeaderData(); +signals: +}; +#endif // BINDINGS_H diff --git a/demo/src/Fibonacci.cpp b/demo/src/Fibonacci.cpp deleted file mode 100644 index 3030a8f..0000000 --- a/demo/src/Fibonacci.cpp +++ /dev/null @@ -1,253 +0,0 @@ -/* generated by rust_qt_binding_generator */ -#include "Fibonacci.h" - -namespace { - template - struct option { - public: - T value; - bool some; - operator QVariant() const { - if (some) { - return QVariant(value); - } - return QVariant(); - } - }; - 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(v.utf16())), - len(v.size()) { - } - operator QString() const { - return QString::fromUtf8(static_cast(data), len); - } - }; - struct qmodelindex_t { - int row; - quintptr id; - }; - inline void fibonacciInputChanged(Fibonacci* o) - { - emit o->inputChanged(); - } - inline void fibonacciResultChanged(Fibonacci* o) - { - emit o->resultChanged(); - } - -} -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*); -}; - -extern "C" { - quint64 fibonacci_list_data_result(const FibonacciList::Private*, int); - void fibonacci_list_sort(FibonacciList::Private*, unsigned char 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(m_d); -} - -QModelIndex FibonacciList::index(int row, int column, const QModelIndex &parent) const -{ - if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < 1) { - return createIndex(row, column, (quintptr)row); - } - 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(m_d); -} - -void FibonacciList::fetchMore(const QModelIndex &parent) -{ - if (!parent.isValid()) { - fibonacci_list_fetch_more(m_d); - } -} - -void FibonacciList::sort(int column, Qt::SortOrder order) -{ - fibonacci_list_sort(m_d, column, order); -} -Qt::ItemFlags FibonacciList::flags(const QModelIndex &i) const -{ - auto flags = QAbstractItemModel::flags(i); - return flags; -} -QVariant FibonacciList::data(const QModelIndex &index, int role) const -{ - QVariant v; - Q_ASSERT(rowCount(index.parent()) > index.row()); - QString s; - QByteArray b; - switch (index.column()) { - case 0: - switch (role) { - case Qt::DisplayRole: - case Qt::UserRole + 0: - v = fibonacci_list_data_result(m_d, index.row()); - break; - } - break; - } - return v; -} -QHash FibonacciList::roleNames() const { - QHash names = QAbstractItemModel::roleNames(); - names.insert(Qt::UserRole + 0, "result"); - return names; -} -bool FibonacciList::setData(const QModelIndex &index, const QVariant &value, int role) -{ - bool set = false; - if (set) { - emit dataChanged(index, index, QVector() << role); - } - return set; -} -extern "C" { - FibonacciList::Private* fibonacci_list_new(FibonacciList*, - void (*)(const FibonacciList*), - void (*)(FibonacciList*, quintptr, quintptr), - void (*)(FibonacciList*), - void (*)(FibonacciList*), - void (*)(FibonacciList*, int, int), - void (*)(FibonacciList*), - void (*)(FibonacciList*, int, int), - void (*)(FibonacciList*)); - void fibonacci_list_free(FibonacciList::Private*); -}; - -Fibonacci::Fibonacci(bool /*owned*/, QObject *parent): - QObject(parent), - m_d(0), - m_ownsPrivate(false) -{ -} - -Fibonacci::Fibonacci(QObject *parent): - QObject(parent), - m_d(fibonacci_new(this, - fibonacciInputChanged, - fibonacciResultChanged)), - m_ownsPrivate(true) -{ -} - -Fibonacci::~Fibonacci() { - if (m_ownsPrivate) { - fibonacci_free(m_d); - } -} -quint32 Fibonacci::input() const -{ - return fibonacci_input_get(m_d); -} -void Fibonacci::setInput(uint v) { - fibonacci_input_set(m_d, v); -} -quint64 Fibonacci::result() const -{ - return fibonacci_result_get(m_d); -} -FibonacciList::FibonacciList(bool /*owned*/, QObject *parent): - QAbstractItemModel(parent), - m_d(0), - m_ownsPrivate(false) -{ -} - -FibonacciList::FibonacciList(QObject *parent): - QAbstractItemModel(parent), - m_d(fibonacci_list_new(this, - [](const FibonacciList* o) { - emit o->newDataReady(QModelIndex()); - }, - [](FibonacciList* o, quintptr first, quintptr last) { - o->dataChanged(o->createIndex(first, 0, first), - o->createIndex(last, 0, last)); - }, - [](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(); - } -)), - m_ownsPrivate(true) -{ - connect(this, &FibonacciList::newDataReady, this, [this](const QModelIndex& i) { - fetchMore(i); - }, Qt::QueuedConnection); -} - -FibonacciList::~FibonacciList() { - if (m_ownsPrivate) { - fibonacci_list_free(m_d); - } -} diff --git a/demo/src/Fibonacci.h b/demo/src/Fibonacci.h deleted file mode 100644 index 94d37b0..0000000 --- a/demo/src/Fibonacci.h +++ /dev/null @@ -1,63 +0,0 @@ -/* generated by rust_qt_binding_generator */ -#ifndef FIBONACCI_H -#define FIBONACCI_H - -#include -#include - -class Fibonacci; -class FibonacciList; - -class Fibonacci : public QObject -{ - Q_OBJECT -public: - class Private; -private: - Private * m_d; - bool m_ownsPrivate; - Q_PROPERTY(quint32 input READ input WRITE setInput NOTIFY inputChanged FINAL) - Q_PROPERTY(quint64 result READ result NOTIFY resultChanged FINAL) - explicit Fibonacci(bool owned, QObject *parent); -public: - explicit Fibonacci(QObject *parent = nullptr); - ~Fibonacci(); - quint32 input() const; - void setInput(uint v); - quint64 result() const; -signals: - void inputChanged(); - void resultChanged(); -}; - -class FibonacciList : public QAbstractItemModel -{ - Q_OBJECT -public: - class Private; -private: - Private * m_d; - bool m_ownsPrivate; - explicit FibonacciList(bool owned, QObject *parent); -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; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) 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; - Qt::ItemFlags flags(const QModelIndex &index) const override; - void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; - QHash roleNames() const override; -signals: - // new data is ready to be made available to the model with fetchMore() - void newDataReady(const QModelIndex &parent) const; -signals: -}; -#endif // FIBONACCI_H diff --git a/demo/src/Processes.cpp b/demo/src/Processes.cpp deleted file mode 100644 index 60fbb2e..0000000 --- a/demo/src/Processes.cpp +++ /dev/null @@ -1,303 +0,0 @@ -/* generated by rust_qt_binding_generator */ -#include "Processes.h" - -namespace { - template - struct option { - public: - T value; - bool some; - operator QVariant() const { - if (some) { - return QVariant(value); - } - return QVariant(); - } - }; - 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(v.utf16())), - len(v.size()) { - } - operator QString() const { - return QString::fromUtf8(static_cast(data), len); - } - }; - struct qmodelindex_t { - int row; - quintptr id; - }; - inline void processesActiveChanged(Processes* o) - { - emit o->activeChanged(); - } - -} -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" { - void processes_data_cmd(const Processes::Private*, quintptr, QString*, qstring_set); - quint8 processes_data_cpu_percentage(const Processes::Private*, quintptr); - float processes_data_cpu_usage(const Processes::Private*, quintptr); - quint64 processes_data_memory(const Processes::Private*, quintptr); - void processes_data_name(const Processes::Private*, quintptr, QString*, qstring_set); - uint processes_data_pid(const Processes::Private*, quintptr); - uint processes_data_uid(const Processes::Private*, quintptr); - void processes_sort(Processes::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder); - - int processes_row_count(const Processes::Private*, quintptr, bool); - bool processes_can_fetch_more(const Processes::Private*, quintptr, bool); - void processes_fetch_more(Processes::Private*, quintptr, bool); - quintptr processes_index(const Processes::Private*, quintptr, bool, int); - qmodelindex_t processes_parent(const Processes::Private*, quintptr); - int processes_row(const Processes::Private*, quintptr); -} -int Processes::columnCount(const QModelIndex &) const -{ - return 3; -} - -bool Processes::hasChildren(const QModelIndex &parent) const -{ - return rowCount(parent) > 0; -} - -int Processes::rowCount(const QModelIndex &parent) const -{ - if (parent.isValid() && parent.column() != 0) { - return 0; - } - return processes_row_count(m_d, parent.internalId(), parent.isValid()); -} - -QModelIndex Processes::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(); - } - if (row >= rowCount(parent)) { - return QModelIndex(); - } - const quintptr id = processes_index(m_d, parent.internalId(), parent.isValid(), row); - return createIndex(row, column, id); -} - -QModelIndex Processes::parent(const QModelIndex &index) const -{ - if (!index.isValid()) { - return QModelIndex(); - } - const qmodelindex_t parent = processes_parent(m_d, index.internalId()); - return parent.row >= 0 ?createIndex(parent.row, 0, parent.id) :QModelIndex(); -} - -bool Processes::canFetchMore(const QModelIndex &parent) const -{ - if (parent.isValid() && parent.column() != 0) { - return false; - } - return processes_can_fetch_more(m_d, parent.internalId(), parent.isValid()); -} - -void Processes::fetchMore(const QModelIndex &parent) -{ - processes_fetch_more(m_d, parent.internalId(), parent.isValid()); -} - -void Processes::sort(int column, Qt::SortOrder order) -{ - processes_sort(m_d, column, order); -} -Qt::ItemFlags Processes::flags(const QModelIndex &i) const -{ - auto flags = QAbstractItemModel::flags(i); - return flags; -} -QVariant Processes::data(const QModelIndex &index, int role) const -{ - QVariant v; - Q_ASSERT(rowCount(index.parent()) > index.row()); - QString s; - QByteArray b; - switch (index.column()) { - case 0: - switch (role) { - case Qt::UserRole + 0: - processes_data_cmd(m_d, index.internalId(), &s, set_qstring); - if (!s.isNull()) v.setValue(s); - break; - case Qt::UserRole + 1: - v = processes_data_cpu_percentage(m_d, index.internalId()); - break; - case Qt::UserRole + 2: - v = processes_data_cpu_usage(m_d, index.internalId()); - break; - case Qt::UserRole + 3: - v = processes_data_memory(m_d, index.internalId()); - break; - case Qt::DisplayRole: - case Qt::UserRole + 4: - processes_data_name(m_d, index.internalId(), &s, set_qstring); - if (!s.isNull()) v.setValue(s); - break; - case Qt::ToolTipRole: - case Qt::UserRole + 5: - v = processes_data_pid(m_d, index.internalId()); - break; - case Qt::UserRole + 6: - v = processes_data_uid(m_d, index.internalId()); - break; - } - break; - case 1: - switch (role) { - case Qt::DisplayRole: - case Qt::UserRole + 2: - v = processes_data_cpu_usage(m_d, index.internalId()); - break; - } - break; - case 2: - switch (role) { - case Qt::DisplayRole: - case Qt::UserRole + 3: - v = processes_data_memory(m_d, index.internalId()); - break; - } - break; - } - return v; -} -QHash Processes::roleNames() const { - QHash names = QAbstractItemModel::roleNames(); - names.insert(Qt::UserRole + 0, "cmd"); - names.insert(Qt::UserRole + 1, "cpuPercentage"); - names.insert(Qt::UserRole + 2, "cpuUsage"); - names.insert(Qt::UserRole + 3, "memory"); - names.insert(Qt::UserRole + 4, "name"); - names.insert(Qt::UserRole + 5, "pid"); - names.insert(Qt::UserRole + 6, "uid"); - return names; -} -bool Processes::setData(const QModelIndex &index, const QVariant &value, int role) -{ - bool set = false; - if (set) { - emit dataChanged(index, index, QVector() << role); - } - return set; -} -extern "C" { - Processes::Private* processes_new(Processes*, void (*)(Processes*), - void (*)(const Processes*, quintptr, bool), - void (*)(Processes*, quintptr, quintptr), - void (*)(Processes*), - void (*)(Processes*), - void (*)(Processes*, option, int, int), - void (*)(Processes*), - void (*)(Processes*, option, int, int), - void (*)(Processes*)); - void processes_free(Processes::Private*); - bool processes_active_get(const Processes::Private*); - void processes_active_set(Processes::Private*, bool); -}; - -Processes::Processes(bool /*owned*/, QObject *parent): - QAbstractItemModel(parent), - m_d(0), - m_ownsPrivate(false) -{ -} - -Processes::Processes(QObject *parent): - QAbstractItemModel(parent), - m_d(processes_new(this, - processesActiveChanged, - [](const Processes* o, quintptr id, bool valid) { - if (valid) { - int row = processes_row(o->m_d, id); - emit o->newDataReady(o->createIndex(row, 0, id)); - } else { - emit o->newDataReady(QModelIndex()); - } - }, - [](Processes* o, quintptr first, quintptr last) { - quintptr frow = processes_row(o->m_d, first); - quintptr lrow = processes_row(o->m_d, first); - o->dataChanged(o->createIndex(frow, 0, first), - o->createIndex(lrow, 2, last)); - }, - [](Processes* o) { - o->beginResetModel(); - }, - [](Processes* o) { - o->endResetModel(); - }, - [](Processes* o, option id, int first, int last) { - if (id.some) { - int row = processes_row(o->m_d, id.value); - o->beginInsertRows(o->createIndex(row, 0, id.value), first, last); - } else { - o->beginInsertRows(QModelIndex(), first, last); - } - }, - [](Processes* o) { - o->endInsertRows(); - }, - [](Processes* o, option id, int first, int last) { - if (id.some) { - int row = processes_row(o->m_d, id.value); - o->beginRemoveRows(o->createIndex(row, 0, id.value), first, last); - } else { - o->beginRemoveRows(QModelIndex(), first, last); - } - }, - [](Processes* o) { - o->endRemoveRows(); - } -)), - m_ownsPrivate(true) -{ - connect(this, &Processes::newDataReady, this, [this](const QModelIndex& i) { - fetchMore(i); - }, Qt::QueuedConnection); -} - -Processes::~Processes() { - if (m_ownsPrivate) { - processes_free(m_d); - } -} -bool Processes::active() const -{ - return processes_active_get(m_d); -} -void Processes::setActive(bool v) { - processes_active_set(m_d, v); -} diff --git a/demo/src/Processes.h b/demo/src/Processes.h deleted file mode 100644 index 6d77b87..0000000 --- a/demo/src/Processes.h +++ /dev/null @@ -1,44 +0,0 @@ -/* generated by rust_qt_binding_generator */ -#ifndef PROCESSES_H -#define PROCESSES_H - -#include -#include - -class Processes; - -class Processes : public QAbstractItemModel -{ - Q_OBJECT -public: - class Private; -private: - Private * m_d; - bool m_ownsPrivate; - Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged FINAL) - explicit Processes(bool owned, QObject *parent); -public: - explicit Processes(QObject *parent = nullptr); - ~Processes(); - bool active() const; - void setActive(bool v); - - int columnCount(const QModelIndex &parent = QModelIndex()) const override; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) 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; - Qt::ItemFlags flags(const QModelIndex &index) const override; - void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; - QHash 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 activeChanged(); -}; -#endif // PROCESSES_H diff --git a/demo/src/TimeSeries.cpp b/demo/src/TimeSeries.cpp deleted file mode 100644 index 388c71b..0000000 --- a/demo/src/TimeSeries.cpp +++ /dev/null @@ -1,241 +0,0 @@ -/* generated by rust_qt_binding_generator */ -#include "TimeSeries.h" - -namespace { - template - struct option { - public: - T value; - bool some; - operator QVariant() const { - if (some) { - return QVariant(value); - } - return QVariant(); - } - }; - 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(v.utf16())), - len(v.size()) { - } - operator QString() const { - return QString::fromUtf8(static_cast(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" { - uint time_series_data_input(const TimeSeries::Private*, int); - bool time_series_set_data_input(TimeSeries::Private*, int, uint); - uint time_series_data_result(const TimeSeries::Private*, int); - bool time_series_set_data_result(TimeSeries::Private*, int, uint); - void time_series_sort(TimeSeries::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder); - - int time_series_row_count(const TimeSeries::Private*); - bool time_series_can_fetch_more(const TimeSeries::Private*); - void time_series_fetch_more(TimeSeries::Private*); -} -int TimeSeries::columnCount(const QModelIndex &parent) const -{ - return (parent.isValid()) ? 0 : 2; -} - -bool TimeSeries::hasChildren(const QModelIndex &parent) const -{ - return rowCount(parent) > 0; -} - -int TimeSeries::rowCount(const QModelIndex &parent) const -{ - return (parent.isValid()) ? 0 : time_series_row_count(m_d); -} - -QModelIndex TimeSeries::index(int row, int column, const QModelIndex &parent) const -{ - if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < 2) { - return createIndex(row, column, (quintptr)row); - } - return QModelIndex(); -} - -QModelIndex TimeSeries::parent(const QModelIndex &) const -{ - return QModelIndex(); -} - -bool TimeSeries::canFetchMore(const QModelIndex &parent) const -{ - return (parent.isValid()) ? 0 : time_series_can_fetch_more(m_d); -} - -void TimeSeries::fetchMore(const QModelIndex &parent) -{ - if (!parent.isValid()) { - time_series_fetch_more(m_d); - } -} - -void TimeSeries::sort(int column, Qt::SortOrder order) -{ - time_series_sort(m_d, column, order); -} -Qt::ItemFlags TimeSeries::flags(const QModelIndex &i) const -{ - auto flags = QAbstractItemModel::flags(i); - if (i.column() == 0) { - flags |= Qt::ItemIsEditable; - } - if (i.column() == 1) { - flags |= Qt::ItemIsEditable; - } - return flags; -} -QVariant TimeSeries::data(const QModelIndex &index, int role) const -{ - QVariant v; - Q_ASSERT(rowCount(index.parent()) > index.row()); - QString s; - QByteArray b; - switch (index.column()) { - case 0: - switch (role) { - case Qt::DisplayRole: - case Qt::EditRole: - case Qt::UserRole + 0: - v = time_series_data_input(m_d, index.row()); - break; - case Qt::UserRole + 1: - v = time_series_data_result(m_d, index.row()); - break; - } - break; - case 1: - switch (role) { - case Qt::DisplayRole: - case Qt::EditRole: - case Qt::UserRole + 1: - v = time_series_data_result(m_d, index.row()); - break; - } - break; - } - return v; -} -QHash TimeSeries::roleNames() const { - QHash names = QAbstractItemModel::roleNames(); - names.insert(Qt::UserRole + 0, "input"); - names.insert(Qt::UserRole + 1, "result"); - return names; -} -bool TimeSeries::setData(const QModelIndex &index, const QVariant &value, int role) -{ - bool set = false; - if (index.column() == 0) { - if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 0) { - set = time_series_set_data_input(m_d, index.row(), value.value()); - } - if (role == Qt::UserRole + 1) { - set = time_series_set_data_result(m_d, index.row(), value.value()); - } - } - if (index.column() == 1) { - if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 1) { - set = time_series_set_data_result(m_d, index.row(), value.value()); - } - } - if (set) { - emit dataChanged(index, index, QVector() << role); - } - return set; -} -extern "C" { - TimeSeries::Private* time_series_new(TimeSeries*, - void (*)(const TimeSeries*), - void (*)(TimeSeries*, quintptr, quintptr), - void (*)(TimeSeries*), - void (*)(TimeSeries*), - void (*)(TimeSeries*, int, int), - void (*)(TimeSeries*), - void (*)(TimeSeries*, int, int), - void (*)(TimeSeries*)); - void time_series_free(TimeSeries::Private*); -}; - -TimeSeries::TimeSeries(bool /*owned*/, QObject *parent): - QAbstractItemModel(parent), - m_d(0), - m_ownsPrivate(false) -{ -} - -TimeSeries::TimeSeries(QObject *parent): - QAbstractItemModel(parent), - m_d(time_series_new(this, - [](const TimeSeries* o) { - emit o->newDataReady(QModelIndex()); - }, - [](TimeSeries* o, quintptr first, quintptr last) { - o->dataChanged(o->createIndex(first, 0, first), - o->createIndex(last, 1, last)); - }, - [](TimeSeries* o) { - o->beginResetModel(); - }, - [](TimeSeries* o) { - o->endResetModel(); - }, - [](TimeSeries* o, int first, int last) { - o->beginInsertRows(QModelIndex(), first, last); - }, - [](TimeSeries* o) { - o->endInsertRows(); - }, - [](TimeSeries* o, int first, int last) { - o->beginRemoveRows(QModelIndex(), first, last); - }, - [](TimeSeries* o) { - o->endRemoveRows(); - } -)), - m_ownsPrivate(true) -{ - connect(this, &TimeSeries::newDataReady, this, [this](const QModelIndex& i) { - fetchMore(i); - }, Qt::QueuedConnection); -} - -TimeSeries::~TimeSeries() { - if (m_ownsPrivate) { - time_series_free(m_d); - } -} diff --git a/demo/src/TimeSeries.h b/demo/src/TimeSeries.h deleted file mode 100644 index 81d727d..0000000 --- a/demo/src/TimeSeries.h +++ /dev/null @@ -1,40 +0,0 @@ -/* generated by rust_qt_binding_generator */ -#ifndef TIMESERIES_H -#define TIMESERIES_H - -#include -#include - -class TimeSeries; - -class TimeSeries : public QAbstractItemModel -{ - Q_OBJECT -public: - class Private; -private: - Private * m_d; - bool m_ownsPrivate; - explicit TimeSeries(bool owned, QObject *parent); -public: - explicit TimeSeries(QObject *parent = nullptr); - ~TimeSeries(); - - int columnCount(const QModelIndex &parent = QModelIndex()) const override; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) 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; - Qt::ItemFlags flags(const QModelIndex &index) const override; - void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; - QHash roleNames() const override; -signals: - // new data is ready to be made available to the model with fetchMore() - void newDataReady(const QModelIndex &parent) const; -signals: -}; -#endif // TIMESERIES_H diff --git a/demo/src/Tree.cpp b/demo/src/Tree.cpp deleted file mode 100644 index b2e103f..0000000 --- a/demo/src/Tree.cpp +++ /dev/null @@ -1,323 +0,0 @@ -/* generated by rust_qt_binding_generator */ -#include "Tree.h" - -namespace { - template - struct option { - public: - T value; - bool some; - operator QVariant() const { - if (some) { - return QVariant(value); - } - return QVariant(); - } - }; - 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(v.utf16())), - len(v.size()) { - } - operator QString() const { - return QString::fromUtf8(static_cast(data), len); - } - }; - struct qmodelindex_t { - int row; - quintptr id; - }; - inline void treePathChanged(Tree* o) - { - emit o->pathChanged(); - } - -} -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" { - void tree_data_file_icon(const Tree::Private*, quintptr, QByteArray*, qbytearray_set); - void tree_data_file_name(const Tree::Private*, quintptr, QString*, qstring_set); - void tree_data_file_path(const Tree::Private*, quintptr, QString*, qstring_set); - qint32 tree_data_file_permissions(const Tree::Private*, quintptr); - option tree_data_file_size(const Tree::Private*, quintptr); - qint32 tree_data_file_type(const Tree::Private*, quintptr); - void tree_sort(Tree::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder); - - int tree_row_count(const Tree::Private*, quintptr, bool); - bool tree_can_fetch_more(const Tree::Private*, quintptr, bool); - void tree_fetch_more(Tree::Private*, quintptr, bool); - quintptr tree_index(const Tree::Private*, quintptr, bool, int); - qmodelindex_t tree_parent(const Tree::Private*, quintptr); - int tree_row(const Tree::Private*, quintptr); -} -int Tree::columnCount(const QModelIndex &) const -{ - return 5; -} - -bool Tree::hasChildren(const QModelIndex &parent) const -{ - return rowCount(parent) > 0; -} - -int Tree::rowCount(const QModelIndex &parent) const -{ - if (parent.isValid() && parent.column() != 0) { - return 0; - } - return tree_row_count(m_d, parent.internalId(), parent.isValid()); -} - -QModelIndex Tree::index(int row, int column, const QModelIndex &parent) const -{ - if (row < 0 || column < 0 || column >= 5) { - return QModelIndex(); - } - if (parent.isValid() && parent.column() != 0) { - return QModelIndex(); - } - if (row >= rowCount(parent)) { - return QModelIndex(); - } - const quintptr id = tree_index(m_d, parent.internalId(), parent.isValid(), row); - return createIndex(row, column, id); -} - -QModelIndex Tree::parent(const QModelIndex &index) const -{ - if (!index.isValid()) { - return QModelIndex(); - } - const qmodelindex_t parent = tree_parent(m_d, index.internalId()); - return parent.row >= 0 ?createIndex(parent.row, 0, parent.id) :QModelIndex(); -} - -bool Tree::canFetchMore(const QModelIndex &parent) const -{ - if (parent.isValid() && parent.column() != 0) { - return false; - } - return tree_can_fetch_more(m_d, parent.internalId(), parent.isValid()); -} - -void Tree::fetchMore(const QModelIndex &parent) -{ - tree_fetch_more(m_d, parent.internalId(), parent.isValid()); -} - -void Tree::sort(int column, Qt::SortOrder order) -{ - tree_sort(m_d, column, order); -} -Qt::ItemFlags Tree::flags(const QModelIndex &i) const -{ - auto flags = QAbstractItemModel::flags(i); - return flags; -} -QVariant Tree::data(const QModelIndex &index, int role) const -{ - QVariant v; - Q_ASSERT(rowCount(index.parent()) > index.row()); - QString s; - QByteArray b; - switch (index.column()) { - case 0: - switch (role) { - case Qt::DecorationRole: - case Qt::UserRole + 0: - tree_data_file_icon(m_d, index.internalId(), &b, set_qbytearray); - if (!b.isNull()) v.setValue(b); - break; - case Qt::DisplayRole: - case Qt::UserRole + 1: - tree_data_file_name(m_d, index.internalId(), &s, set_qstring); - if (!s.isNull()) v.setValue(s); - break; - case Qt::UserRole + 2: - tree_data_file_path(m_d, index.internalId(), &s, set_qstring); - if (!s.isNull()) v.setValue(s); - break; - case Qt::UserRole + 3: - v = tree_data_file_permissions(m_d, index.internalId()); - break; - case Qt::UserRole + 4: - v = tree_data_file_size(m_d, index.internalId()); - break; - case Qt::UserRole + 5: - v = tree_data_file_type(m_d, index.internalId()); - break; - } - break; - case 1: - switch (role) { - case Qt::DisplayRole: - case Qt::UserRole + 4: - v = tree_data_file_size(m_d, index.internalId()); - break; - } - break; - case 2: - switch (role) { - case Qt::DisplayRole: - case Qt::UserRole + 2: - tree_data_file_path(m_d, index.internalId(), &s, set_qstring); - if (!s.isNull()) v.setValue(s); - break; - } - break; - case 3: - switch (role) { - case Qt::DisplayRole: - case Qt::UserRole + 3: - v = tree_data_file_permissions(m_d, index.internalId()); - break; - } - break; - case 4: - switch (role) { - case Qt::DisplayRole: - case Qt::UserRole + 5: - v = tree_data_file_type(m_d, index.internalId()); - break; - } - break; - } - return v; -} -QHash Tree::roleNames() const { - QHash names = QAbstractItemModel::roleNames(); - names.insert(Qt::UserRole + 0, "fileIcon"); - names.insert(Qt::UserRole + 1, "fileName"); - names.insert(Qt::UserRole + 2, "filePath"); - names.insert(Qt::UserRole + 3, "filePermissions"); - names.insert(Qt::UserRole + 4, "fileSize"); - names.insert(Qt::UserRole + 5, "fileType"); - return names; -} -bool Tree::setData(const QModelIndex &index, const QVariant &value, int role) -{ - bool set = false; - if (set) { - emit dataChanged(index, index, QVector() << role); - } - return set; -} -extern "C" { - Tree::Private* tree_new(Tree*, void (*)(Tree*), - void (*)(const Tree*, quintptr, bool), - void (*)(Tree*, quintptr, quintptr), - void (*)(Tree*), - void (*)(Tree*), - void (*)(Tree*, option, int, int), - void (*)(Tree*), - void (*)(Tree*, option, int, int), - void (*)(Tree*)); - void tree_free(Tree::Private*); - void tree_path_get(const Tree::Private*, QString*, qstring_set); - void tree_path_set(Tree::Private*, qstring_t); - void tree_path_set_none(Tree::Private*); -}; - -Tree::Tree(bool /*owned*/, QObject *parent): - QAbstractItemModel(parent), - m_d(0), - m_ownsPrivate(false) -{ -} - -Tree::Tree(QObject *parent): - QAbstractItemModel(parent), - m_d(tree_new(this, - treePathChanged, - [](const Tree* o, quintptr id, bool valid) { - if (valid) { - int row = tree_row(o->m_d, id); - emit o->newDataReady(o->createIndex(row, 0, id)); - } else { - emit o->newDataReady(QModelIndex()); - } - }, - [](Tree* o, quintptr first, quintptr last) { - quintptr frow = tree_row(o->m_d, first); - quintptr lrow = tree_row(o->m_d, first); - o->dataChanged(o->createIndex(frow, 0, first), - o->createIndex(lrow, 4, last)); - }, - [](Tree* o) { - o->beginResetModel(); - }, - [](Tree* o) { - o->endResetModel(); - }, - [](Tree* o, option id, int first, int last) { - if (id.some) { - int row = tree_row(o->m_d, id.value); - o->beginInsertRows(o->createIndex(row, 0, id.value), first, last); - } else { - o->beginInsertRows(QModelIndex(), first, last); - } - }, - [](Tree* o) { - o->endInsertRows(); - }, - [](Tree* o, option id, int first, int last) { - if (id.some) { - int row = tree_row(o->m_d, id.value); - o->beginRemoveRows(o->createIndex(row, 0, id.value), first, last); - } else { - o->beginRemoveRows(QModelIndex(), first, last); - } - }, - [](Tree* o) { - o->endRemoveRows(); - } -)), - m_ownsPrivate(true) -{ - connect(this, &Tree::newDataReady, this, [this](const QModelIndex& i) { - fetchMore(i); - }, Qt::QueuedConnection); -} - -Tree::~Tree() { - if (m_ownsPrivate) { - tree_free(m_d); - } -} -QString Tree::path() const -{ - QString v; - tree_path_get(m_d, &v, set_qstring); - return v; -} -void Tree::setPath(const QString& v) { - if (v.isNull()) { - tree_path_set_none(m_d); - } else { - tree_path_set(m_d, v); - } -} diff --git a/demo/src/Tree.h b/demo/src/Tree.h deleted file mode 100644 index dc9e8ec..0000000 --- a/demo/src/Tree.h +++ /dev/null @@ -1,44 +0,0 @@ -/* generated by rust_qt_binding_generator */ -#ifndef TREE_H -#define TREE_H - -#include -#include - -class Tree; - -class Tree : public QAbstractItemModel -{ - Q_OBJECT -public: - class Private; -private: - Private * m_d; - bool m_ownsPrivate; - Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged FINAL) - explicit Tree(bool owned, QObject *parent); -public: - explicit Tree(QObject *parent = nullptr); - ~Tree(); - 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; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) 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; - Qt::ItemFlags flags(const QModelIndex &index) const override; - void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; - QHash 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(); -}; -#endif // TREE_H diff --git a/demo/src/main.cpp b/demo/src/main.cpp index 4cc9d16..b94069b 100644 --- a/demo/src/main.cpp +++ b/demo/src/main.cpp @@ -1,7 +1,4 @@ -#include "Tree.h" -#include "Fibonacci.h" -#include "TimeSeries.h" -#include "Processes.h" +#include "Bindings.h" #include "SortedModel.h" #ifdef QT_CHARTS_LIB @@ -39,15 +36,11 @@ #include -struct Models { +struct Model { QStringListModel styles; - Fibonacci fibonacci; - FibonacciList fibonacciList; - Tree fileSystem; + Demo demo; SortedModel sortedFileSystem; - Processes processes; SortedModel sortedProcesses; - TimeSeries timeSeries; }; void setStyle(QWidget* w, QStyle* style) { @@ -80,20 +73,18 @@ void copyWindowGeometry(QWidget* w, QQmlContext* c) { } } -void createQtQuick(const QString& name, const QString& qml, Models* models, +void createQtQuick(const QString& name, const QString& qml, Model* model, QWidget* widgets, const QString& initialTab) { QQmlApplicationEngine* engine = new QQmlApplicationEngine(); QQmlContext* c = engine->rootContext(); - c->setContextProperty("styles", &models->styles); - c->setContextProperty("fibonacci", &models->fibonacci); - c->setContextProperty("fibonacciList", &models->fibonacciList); - c->setContextProperty("sortedFileSystem", &models->sortedFileSystem); - c->setContextProperty("processes", &models->sortedProcesses); - c->setContextProperty("timeSeries", &models->timeSeries); + c->setContextProperty("styles", &model->styles); + c->setContextProperty("demo", &model->demo); + c->setContextProperty("sortedFileSystem", &model->sortedFileSystem); + c->setContextProperty("processes", &model->sortedProcesses); c->setContextProperty("widgets", widgets); c->setContextProperty("qtquickIndex", - QVariant(models->styles.stringList().indexOf(name))); + QVariant(model->styles.stringList().indexOf(name))); c->setContextProperty("initialTab", initialTab); copyWindowGeometry(widgets, engine->rootContext()); engine->load(QUrl(qml)); @@ -101,9 +92,9 @@ void createQtQuick(const QString& name, const QString& qml, Models* models, #endif -QComboBox* createStyleComboBox(Models* models) { +QComboBox* createStyleComboBox(Model* model) { QComboBox* box = new QComboBox(); - box->setModel(&models->styles); + box->setModel(&model->styles); auto styles = QStyleFactory::keys(); QString currentStyle = QApplication::style()->objectName().toLower(); for (auto v: styles) { @@ -121,10 +112,10 @@ QComboBox* createStyleComboBox(Models* models) { return box; } -QWidget* createStyleTab(Models* models, QWidget* tabs, QComboBox* box, +QWidget* createStyleTab(Model* model, QWidget* tabs, QComboBox* box, const QString& initialTab) { QRect windowRect; - auto f = [windowRect, box, tabs, models, initialTab](const QString &text) mutable { + auto f = [windowRect, box, tabs, model, initialTab](const QString &text) mutable { QWindow* window = getWindow(tabs); bool visible = tabs->isVisible(); if (text.startsWith("QWidgets ")) { @@ -148,16 +139,16 @@ QWidget* createStyleTab(Models* models, QWidget* tabs, QComboBox* box, #ifdef QTQUICKCONTROLS2 if (text == "QtQuick Controls 2") { createQtQuick("QtQuick Controls 2", "qrc:///demo-qtquick2.qml", - models, box, initialTab); + model, box, initialTab); } else #endif - createQtQuick("QtQuick", "qrc:///demo.qml", models, box, initialTab); + createQtQuick("QtQuick", "qrc:///demo.qml", model, box, initialTab); #endif } }; box->connect(box, &QComboBox::currentTextChanged, box, f); - QSvgWidget* logo = new QSvgWidget("/home/oever/src/rust_qt_binding_generator/logo.svg"); + QSvgWidget* logo = new QSvgWidget(":/logo.svg"); logo->setFixedSize(logo->renderer()->defaultSize()); QWidget* tab = new QWidget; @@ -168,9 +159,9 @@ QWidget* createStyleTab(Models* models, QWidget* tabs, QComboBox* box, return tab; } -QWidget* createObjectTab(Models* models) { +QWidget* createObjectTab(Model* model) { QWidget* view = new QWidget; - Fibonacci* fibonacci = &models->fibonacci; + Fibonacci* fibonacci = model->demo.fibonacci(); QLineEdit* input = new QLineEdit; input->setPlaceholderText("Your number"); @@ -198,31 +189,31 @@ QWidget* createObjectTab(Models* models) { return view; } -QWidget* createListTab(Models* models) { +QWidget* createListTab(Model* model) { QListView* view = new QListView(); - view->setModel(&models->fibonacciList); + view->setModel(model->demo.fibonacciList()); return view; } -QWidget* createTreeTab(Models* models) { +QWidget* createTreeTab(Model* model) { QTreeView* view = new QTreeView(); view->setUniformRowHeights(true); view->setSortingEnabled(true); - view->setModel(&models->sortedFileSystem); - auto root = models->sortedFileSystem.index(0, 0); + view->setModel(&model->sortedFileSystem); + auto root = model->sortedFileSystem.index(0, 0); view->expand(root); view->sortByColumn(0, Qt::AscendingOrder); view->header()->setSectionResizeMode(QHeaderView::ResizeToContents); return view; } -QWidget* createProcessesTab(Models* models) { +QWidget* createProcessesTab(Model* model) { QTreeView* view = new QTreeView(); view->setUniformRowHeights(true); view->setSortingEnabled(true); - view->setModel(&models->sortedProcesses); + view->setModel(&model->sortedProcesses); // expand when the model is populated - view->connect(&models->sortedProcesses, &QAbstractItemModel::rowsInserted, + view->connect(&model->sortedProcesses, &QAbstractItemModel::rowsInserted, view, [view](const QModelIndex& index) { if (!index.isValid()) { view->expandAll(); @@ -237,14 +228,14 @@ QWidget* createProcessesTab(Models* models) { using namespace QtCharts; -QWidget* createChartTab(Models* models) { +QWidget* createChartTab(Model* model) { QLineSeries *series = new QLineSeries(); series->setName("Line 1"); QVXYModelMapper *mapper = new QVXYModelMapper(series); mapper->setXColumn(0); mapper->setYColumn(1); mapper->setSeries(series); - mapper->setModel(&models->timeSeries); + mapper->setModel(model->demo.timeSeries()); QChart* chart = new QChart; chart->addSeries(series); @@ -263,7 +254,7 @@ QWidget* createChartTab(Models* models) { QWidget* view = new QWidget; QTableView* data = new QTableView; - data->setModel(&models->timeSeries); + data->setModel(model->demo.timeSeries()); QChartView *chartView = new QChartView(chart); QHBoxLayout *layout = new QHBoxLayout; @@ -274,18 +265,22 @@ QWidget* createChartTab(Models* models) { } #endif -void createWidgets(Models* models, const QString& initialStyle, +void createWidgets(Model* model, const QString& initialStyle, const QString& initialTab) { QTabWidget* tabs = new QTabWidget(); - QComboBox* box = createStyleComboBox(models); - tabs->addTab(createStyleTab(models, tabs, box, initialTab), "style"); - tabs->addTab(createObjectTab(models), "object"); - tabs->addTab(createListTab(models), "list"); - tabs->addTab(createTreeTab(models), "tree"); - tabs->addTab(createProcessesTab(models), "processes"); + QComboBox* box = createStyleComboBox(model); + tabs->addTab(createStyleTab(model, tabs, box, initialTab), "style"); + tabs->addTab(createObjectTab(model), "object"); + tabs->addTab(createListTab(model), "list"); + tabs->addTab(createTreeTab(model), "tree"); + const int procTab = tabs->addTab(createProcessesTab(model), "processes"); + tabs->connect(tabs, &QTabWidget::currentChanged, model->demo.processes(), + [model, procTab](int current) { + model->demo.processes()->setActive(current == procTab); + }); #ifdef QT_CHARTS_LIB - tabs->addTab(createChartTab(models), "chart"); + tabs->addTab(createChartTab(model), "chart"); #endif tabs->setMinimumSize(QSize(500, 500)); tabs->show(); @@ -326,14 +321,14 @@ int main (int argc, char *argv[]) parser.addOption(initialTabOption); parser.process(app); - Models models; - models.fileSystem.setPath("/"); - models.sortedFileSystem.setSourceModel(&models.fileSystem); - models.sortedFileSystem.setDynamicSortFilter(true); - models.sortedProcesses.setSourceModel(&models.processes); - models.sortedProcesses.setDynamicSortFilter(true); + Model model; + model.demo.fileSystemTree()->setPath("/"); + model.sortedFileSystem.setSourceModel(model.demo.fileSystemTree()); + model.sortedFileSystem.setDynamicSortFilter(true); + model.sortedProcesses.setSourceModel(model.demo.processes()); + model.sortedProcesses.setDynamicSortFilter(true); - createWidgets(&models, parser.value(initialStyleOption), + createWidgets(&model, parser.value(initialStyleOption), parser.value(initialTabOption)); return app.exec(); diff --git a/src/cpp.cpp b/src/cpp.cpp index 773c3e6..fad872a 100644 --- a/src/cpp.cpp +++ b/src/cpp.cpp @@ -49,9 +49,14 @@ void writeHeaderItemModel(QTextStream& h) { Qt::ItemFlags flags(const QModelIndex &index) const override; void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; QHash roleNames() const override; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole); signals: // new data is ready to be made available to the model with fetchMore() void newDataReady(const QModelIndex &parent) const; +private: + QHash, QVariant> m_headerData; + void initHeaderData(); )"); } @@ -270,6 +275,23 @@ QVariant %1::data(const QModelIndex &index, int role) const } cpp << " return names;\n"; cpp << QString(R"(} +QVariant %1::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (orientation != Qt::Horizontal) { + return QVariant(); + } + return m_headerData.value(qMakePair(section, (Qt::ItemDataRole)role), role == Qt::DisplayRole ?QString::number(section + 1) :QVariant()); +} + +bool %1::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) +{ + if (orientation != Qt::Horizontal) { + return false; + } + m_headerData.insert(qMakePair(section, (Qt::ItemDataRole)role), value); + return true; +} + bool %1::setData(const QModelIndex &index, const QVariant &value, int role) { )").arg(o.name); @@ -545,7 +567,7 @@ void initializeMembersEmpty(QTextStream& cpp, const Object& o, const Configurati } } -void initializeMembersZero(QTextStream& cpp, const Object& o, const Configuration& conf) +void initializeMembersZero(QTextStream& cpp, const Object& o) { for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { @@ -559,14 +581,28 @@ void initializeMembers(QTextStream& cpp, const QString& prefix, const Object& o, { for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { - cpp << QString(" %1m_%2->m_d = %3_%2_get(%1m_d);\n") - .arg(prefix, p.name, snakeCase(o.name)); + cpp << QString(" %1m_%2->m_d = %3_%4_get(%1m_d);\n") + .arg(prefix, p.name, snakeCase(o.name), snakeCase(p.name)); initializeMembers(cpp, "m_" + p.name + "->", conf.findObject(p.type.name), conf); } } } +void connect(QTextStream& cpp, const QString& d, const Object& o, const Configuration& conf) { + for (auto p: o.properties) { + if (p.type.type == BindingType::Object) { + connect(cpp, d + "->m_" + p.name, conf.findObject(p.type.name), conf); + } + } + if (o.type != ObjectType::Object) { + cpp << QString(R"( connect(%2, &%1::newDataReady, %2, [this](const QModelIndex& i) { + %2->fetchMore(i); + }, Qt::QueuedConnection); +)").arg(o.name, d); + } +} + void writeCppObject(QTextStream& cpp, const Object& o, const Configuration& conf) { const QString lcname(snakeCase(o.name)); cpp << QString("%1::%1(bool /*owned*/, QObject *parent):\n %2(parent),") @@ -577,19 +613,20 @@ void writeCppObject(QTextStream& cpp, const Object& o, const Configuration& conf .arg(p.name, p.type.name); } } - cpp << " m_d(0),\n m_ownsPrivate(false)\n{\n}\n\n"; - cpp << QString("%1::%1(QObject *parent):\n %2(parent),") + cpp << " m_d(0),\n m_ownsPrivate(false)\n{\n"; + if (o.type != ObjectType::Object) { + cpp << " initHeaderData();\n"; + } + cpp << QString("}\n\n%1::%1(QObject *parent):\n %2(parent),") .arg(o.name, baseType(o)) << endl; - initializeMembersZero(cpp, o, conf); + initializeMembersZero(cpp, o); cpp << QString(" m_d(%1_new(this").arg(lcname); constructorArgs(cpp, "", o, conf); cpp << ")),\n m_ownsPrivate(true)\n{\n"; initializeMembers(cpp, "", o, conf); + connect(cpp, "this", o, conf); if (o.type != ObjectType::Object) { - cpp << QString(R"( connect(this, &%1::newDataReady, this, [this](const QModelIndex& i) { - fetchMore(i); - }, Qt::QueuedConnection); -)").arg(o.name); + cpp << " initHeaderData();\n"; } cpp << QString(R"(} @@ -600,18 +637,32 @@ void writeCppObject(QTextStream& cpp, const Object& o, const Configuration& conf } )").arg(o.name, lcname); + if (o.type != ObjectType::Object) { + cpp << QString("void %1::initHeaderData() {\n").arg(o.name); + + for (int col = 0; col < o.columnCount; ++col) { + for (auto ip: o.itemProperties) { + auto roles = ip.roles.value(col); + if (roles.contains(Qt::DisplayRole)) { + cpp << QString(" m_headerData.insert(qMakePair(%1, Qt::DisplayRole), QVariant(\"%2\"));\n").arg(QString::number(col), ip.name); + } + } + } + cpp << " }\n"; + } + for (const Property& p: o.properties) { const QString base = QString("%1_%2").arg(lcname, snakeCase(p.name)); if (p.type.type == BindingType::Object) { cpp << QString(R"(const %3* %1::%2() const { - return m_%4; + return m_%2; } %3* %1::%2() { - return m_%4; + return m_%2; } -)").arg(o.name, p.name, p.type.name, snakeCase(p.name)); +)").arg(o.name, p.name, p.type.name); } else if (p.type.isComplex()) { cpp << QString("%3 %1::%2() const\n{\n").arg(o.name, p.name, p.type.name); cpp << " " << p.type.name << " v;\n"; diff --git a/src/rust.cpp b/src/rust.cpp index b5130ce..98cd9b5 100644 --- a/src/rust.cpp +++ b/src/rust.cpp @@ -48,7 +48,7 @@ QString rustTypeInit(const T& p) } void rConstructorArgsDecl(QTextStream& r, const QString& name, const Object& o, const Configuration& conf) { - r << QString(" %2: *mut %1QObject").arg(o.name, name); + r << QString(" %2: *mut %1QObject").arg(o.name, snakeCase(name)); for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { r << QString(",\n"); @@ -59,11 +59,11 @@ void rConstructorArgsDecl(QTextStream& r, const QString& name, const Object& o, } } if (o.type == ObjectType::List) { - r << QString(",\n new_data_ready: fn(*const %1QObject)") - .arg(o.name); + r << QString(",\n %2_new_data_ready: fn(*const %1QObject)") + .arg(o.name, snakeCase(o.name)); } else if (o.type == ObjectType::UniformTree) { - r << QString(",\n new_data_ready: fn(*const %1QObject, item: usize, valid: bool)") - .arg(o.name); + r << QString(",\n %2_new_data_ready: fn(*const %1QObject, item: usize, valid: bool)") + .arg(o.name, snakeCase(o.name)); } if (o.type != ObjectType::Object) { QString indexDecl; @@ -71,13 +71,14 @@ void rConstructorArgsDecl(QTextStream& r, const QString& name, const Object& o, indexDecl = " item: usize, valid: bool,"; } r << QString(R"(, - data_changed: fn(*const %1QObject, usize, usize), - begin_reset_model: fn(*const %1QObject), - end_reset_model: fn(*const %1QObject), - begin_insert_rows: fn(*const %1QObject,%2 usize, usize), - end_insert_rows: fn(*const %1QObject), - begin_remove_rows: fn(*const %1QObject,%2 usize, usize), - end_remove_rows: fn(*const %1QObject))").arg(o.name, indexDecl); + %3_data_changed: fn(*const %1QObject, usize, usize), + %3_begin_reset_model: fn(*const %1QObject), + %3_end_reset_model: fn(*const %1QObject), + %3_begin_insert_rows: fn(*const %1QObject,%2 usize, usize), + %3_end_insert_rows: fn(*const %1QObject), + %3_begin_remove_rows: fn(*const %1QObject,%2 usize, usize), + %3_end_remove_rows: fn(*const %1QObject))").arg(o.name, indexDecl, + snakeCase(o.name)); } } @@ -90,13 +91,14 @@ void rConstructorArgs(QTextStream& r, const QString& name, const Object& o, cons } r << QString(R"( let %2_emit = %1Emitter { qobject: Arc::new(Mutex::new(%2)), -)").arg(o.name, name); +)").arg(o.name, snakeCase(name)); for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) continue; r << QString(" %1_changed: %1_changed,\n").arg(snakeCase(p.name)); } if (o.type != ObjectType::Object) { - r << QString(" new_data_ready: new_data_ready,\n"); + r << QString(" new_data_ready: %1_new_data_ready,\n") + .arg(snakeCase(o.name)); } QString model = ""; if (o.type != ObjectType::Object) { @@ -105,20 +107,20 @@ void rConstructorArgs(QTextStream& r, const QString& name, const Object& o, cons r << QString(R"( }; let model = %1%2 { qobject: %3, - data_changed: data_changed, - 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, -)").arg(o.name, type, name); + data_changed: %4_data_changed, + begin_reset_model: %4_begin_reset_model, + end_reset_model: %4_end_reset_model, + begin_insert_rows: %4_begin_insert_rows, + end_insert_rows: %4_end_insert_rows, + begin_remove_rows: %4_begin_remove_rows, + end_remove_rows: %4_end_remove_rows, +)").arg(o.name, type, snakeCase(name), snakeCase(o.name)); } r << QString(" };\n let d_%3 = %1::create(%3_emit%2") - .arg(o.name, model, name); + .arg(o.name, model, snakeCase(name)); for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { - r << ",\n d_" << p.name; + r << ",\n d_" << snakeCase(p.name); } } r << ");\n"; @@ -560,8 +562,11 @@ pub unsafe extern "C" fn %2_set_data_%3_none(ptr: *mut %1, row: c_int%4) -> bool QString rustFile(const QDir rustdir, const QString& module) { QDir src(rustdir.absoluteFilePath("src")); - QString path = src.absoluteFilePath(module + ".rs"); - return path; + QString modulePath = src.absoluteFilePath(module + "/mod.rs"); + if (QFile::exists(modulePath)) { + return modulePath; + } + return src.absoluteFilePath(module + ".rs"); } void writeRustTypes(const Configuration& conf, QTextStream& r) { diff --git a/tests/rust_list/src/interface.rs b/tests/rust_list/src/interface.rs index 81b85a7..52bd878 100644 --- a/tests/rust_list/src/interface.rs +++ b/tests/rust_list/src/interface.rs @@ -159,28 +159,28 @@ pub trait PersonsTrait { #[no_mangle] pub extern "C" fn persons_new( persons: *mut PersonsQObject, - new_data_ready: fn(*const PersonsQObject), - data_changed: fn(*const PersonsQObject, usize, usize), - begin_reset_model: fn(*const PersonsQObject), - end_reset_model: fn(*const PersonsQObject), - begin_insert_rows: fn(*const PersonsQObject, usize, usize), - end_insert_rows: fn(*const PersonsQObject), - begin_remove_rows: fn(*const PersonsQObject, usize, usize), - end_remove_rows: fn(*const PersonsQObject), + persons_new_data_ready: fn(*const PersonsQObject), + persons_data_changed: fn(*const PersonsQObject, usize, usize), + persons_begin_reset_model: fn(*const PersonsQObject), + persons_end_reset_model: fn(*const PersonsQObject), + persons_begin_insert_rows: fn(*const PersonsQObject, usize, usize), + persons_end_insert_rows: fn(*const PersonsQObject), + persons_begin_remove_rows: fn(*const PersonsQObject, usize, usize), + persons_end_remove_rows: fn(*const PersonsQObject), ) -> *mut Persons { let persons_emit = PersonsEmitter { qobject: Arc::new(Mutex::new(persons)), - new_data_ready: new_data_ready, + new_data_ready: persons_new_data_ready, }; let model = PersonsList { qobject: persons, - data_changed: data_changed, - 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, + data_changed: persons_data_changed, + begin_reset_model: persons_begin_reset_model, + end_reset_model: persons_end_reset_model, + begin_insert_rows: persons_begin_insert_rows, + end_insert_rows: persons_end_insert_rows, + begin_remove_rows: persons_begin_remove_rows, + end_remove_rows: persons_end_remove_rows, }; let d_persons = Persons::create(persons_emit, model); Box::into_raw(Box::new(d_persons)) diff --git a/tests/rust_tree/src/interface.rs b/tests/rust_tree/src/interface.rs index d3dbc2e..e4d17b1 100644 --- a/tests/rust_tree/src/interface.rs +++ b/tests/rust_tree/src/interface.rs @@ -162,28 +162,28 @@ pub trait PersonsTrait { #[no_mangle] pub extern "C" fn persons_new( persons: *mut PersonsQObject, - new_data_ready: fn(*const PersonsQObject, item: usize, valid: bool), - data_changed: fn(*const PersonsQObject, usize, usize), - begin_reset_model: fn(*const PersonsQObject), - end_reset_model: fn(*const PersonsQObject), - begin_insert_rows: fn(*const PersonsQObject, item: usize, valid: bool, usize, usize), - end_insert_rows: fn(*const PersonsQObject), - begin_remove_rows: fn(*const PersonsQObject, item: usize, valid: bool, usize, usize), - end_remove_rows: fn(*const PersonsQObject), + persons_new_data_ready: fn(*const PersonsQObject, item: usize, valid: bool), + persons_data_changed: fn(*const PersonsQObject, usize, usize), + persons_begin_reset_model: fn(*const PersonsQObject), + persons_end_reset_model: fn(*const PersonsQObject), + persons_begin_insert_rows: fn(*const PersonsQObject, item: usize, valid: bool, usize, usize), + persons_end_insert_rows: fn(*const PersonsQObject), + persons_begin_remove_rows: fn(*const PersonsQObject, item: usize, valid: bool, usize, usize), + persons_end_remove_rows: fn(*const PersonsQObject), ) -> *mut Persons { let persons_emit = PersonsEmitter { qobject: Arc::new(Mutex::new(persons)), - new_data_ready: new_data_ready, + new_data_ready: persons_new_data_ready, }; let model = PersonsUniformTree { qobject: persons, - data_changed: data_changed, - 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, + data_changed: persons_data_changed, + begin_reset_model: persons_begin_reset_model, + end_reset_model: persons_end_reset_model, + begin_insert_rows: persons_begin_insert_rows, + end_insert_rows: persons_end_insert_rows, + begin_remove_rows: persons_begin_remove_rows, + end_remove_rows: persons_end_remove_rows, }; let d_persons = Persons::create(persons_emit, model); Box::into_raw(Box::new(d_persons)) diff --git a/tests/test_list_rust.cpp b/tests/test_list_rust.cpp index 25ac48b..c6742d2 100644 --- a/tests/test_list_rust.cpp +++ b/tests/test_list_rust.cpp @@ -140,6 +140,23 @@ QHash Persons::roleNames() const { names.insert(Qt::UserRole + 0, "userName"); return names; } +QVariant Persons::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (orientation != Qt::Horizontal) { + return QVariant(); + } + return m_headerData.value(qMakePair(section, (Qt::ItemDataRole)role), role == Qt::DisplayRole ?QString::number(section + 1) :QVariant()); +} + +bool Persons::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) +{ + if (orientation != Qt::Horizontal) { + return false; + } + m_headerData.insert(qMakePair(section, (Qt::ItemDataRole)role), value); + return true; +} + bool Persons::setData(const QModelIndex &index, const QVariant &value, int role) { bool set = false; @@ -171,6 +188,7 @@ Persons::Persons(bool /*owned*/, QObject *parent): m_d(0), m_ownsPrivate(false) { + initHeaderData(); } Persons::Persons(QObject *parent): @@ -205,8 +223,9 @@ Persons::Persons(QObject *parent): m_ownsPrivate(true) { connect(this, &Persons::newDataReady, this, [this](const QModelIndex& i) { - fetchMore(i); + this->fetchMore(i); }, Qt::QueuedConnection); + initHeaderData(); } Persons::~Persons() { @@ -214,3 +233,6 @@ Persons::~Persons() { persons_free(m_d); } } +void Persons::initHeaderData() { + m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("userName")); + } diff --git a/tests/test_list_rust.h b/tests/test_list_rust.h index c9f7c69..d6cf4de 100644 --- a/tests/test_list_rust.h +++ b/tests/test_list_rust.h @@ -32,9 +32,14 @@ public: Qt::ItemFlags flags(const QModelIndex &index) const override; void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; QHash roleNames() const override; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole); signals: // new data is ready to be made available to the model with fetchMore() void newDataReady(const QModelIndex &parent) const; +private: + QHash, QVariant> m_headerData; + void initHeaderData(); signals: }; #endif // TEST_LIST_RUST_H diff --git a/tests/test_tree_rust.cpp b/tests/test_tree_rust.cpp index 9396581..9a9ed94 100644 --- a/tests/test_tree_rust.cpp +++ b/tests/test_tree_rust.cpp @@ -158,6 +158,23 @@ QHash Persons::roleNames() const { names.insert(Qt::UserRole + 0, "userName"); return names; } +QVariant Persons::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (orientation != Qt::Horizontal) { + return QVariant(); + } + return m_headerData.value(qMakePair(section, (Qt::ItemDataRole)role), role == Qt::DisplayRole ?QString::number(section + 1) :QVariant()); +} + +bool Persons::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) +{ + if (orientation != Qt::Horizontal) { + return false; + } + m_headerData.insert(qMakePair(section, (Qt::ItemDataRole)role), value); + return true; +} + bool Persons::setData(const QModelIndex &index, const QVariant &value, int role) { bool set = false; @@ -189,6 +206,7 @@ Persons::Persons(bool /*owned*/, QObject *parent): m_d(0), m_ownsPrivate(false) { + initHeaderData(); } Persons::Persons(QObject *parent): @@ -240,8 +258,9 @@ Persons::Persons(QObject *parent): m_ownsPrivate(true) { connect(this, &Persons::newDataReady, this, [this](const QModelIndex& i) { - fetchMore(i); + this->fetchMore(i); }, Qt::QueuedConnection); + initHeaderData(); } Persons::~Persons() { @@ -249,3 +268,6 @@ Persons::~Persons() { persons_free(m_d); } } +void Persons::initHeaderData() { + m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("userName")); + } diff --git a/tests/test_tree_rust.h b/tests/test_tree_rust.h index fe703dc..cae5111 100644 --- a/tests/test_tree_rust.h +++ b/tests/test_tree_rust.h @@ -32,9 +32,14 @@ public: Qt::ItemFlags flags(const QModelIndex &index) const override; void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; QHash roleNames() const override; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole); signals: // new data is ready to be made available to the model with fetchMore() void newDataReady(const QModelIndex &parent) const; +private: + QHash, QVariant> m_headerData; + void initHeaderData(); signals: }; #endif // TEST_TREE_RUST_H