Second step towards using only generated models in Demo

master
Jos van den Oever 2017-08-14 00:26:49 +02:00
parent cb5f7d6ac1
commit 7a63362f86
13 changed files with 277 additions and 185 deletions

View File

@ -13,7 +13,6 @@ add_custom_command(
add_custom_command(
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/rust/src/interface.rs"
"${CMAKE_CURRENT_SOURCE_DIR}/rust/src/types.rs"
"${CMAKE_CURRENT_SOURCE_DIR}/src/Tree.h"
# if the cpp file is marked GENERATED, CMake will not check it for moc
# "${CMAKE_CURRENT_SOURCE_DIR}/src/tmp.cpp"
@ -34,7 +33,7 @@ add_custom_command(
)
add_custom_target(rust_target DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/rust/${RUST_TARGET_DIR}/librust.a")
set(Demo_SRCS src/main.cpp src/Tree.cpp src/Bindings.cpp
set(Demo_SRCS src/main.cpp src/Tree.cpp src/Bindings.cpp src/modeltest.cpp
resource_file.qrc)
add_executable(Demo ${Demo_SRCS})

View File

@ -35,33 +35,33 @@
}],
"roles": [
[{
"name": "FileName",
"name": "fileName",
"value": "Qt::DisplayRole",
"type": "QString"
}, {
"name": "FileIcon",
"name": "fileIcon",
"value": "Qt::DecorationRole",
"type": "QByteArray"
}, {
"name": "FilePath",
"name": "filePath",
"value": "Qt::UserRole + 1",
"type": "QString"
}, {
"name": "FileName",
"name": "fileName",
"value": "Qt::UserRole + 2",
"type": "QString"
}, {
"name": "FilePermissions",
"name": "filePermissions",
"value": "Qt::UserRole + 3",
"type": "int"
}],
[{
"name": "FilePath",
"name": "filePath",
"value": "Qt::DisplayRole",
"type": "QString"
}],
[{
"name": "FilePermissions",
"name": "filePermissions",
"value": "Qt::DisplayRole",
"type": "int"
}]
@ -76,33 +76,33 @@
}],
"roles": [
[{
"name": "FileName",
"name": "fileName",
"value": "Qt::DisplayRole",
"type": "QString"
}, {
"name": "FileIcon",
"name": "fileIcon",
"value": "Qt::DecorationRole",
"type": "QByteArray"
}, {
"name": "FilePath",
"name": "filePath",
"value": "Qt::UserRole + 1",
"type": "QString"
}, {
"name": "FileName",
"name": "fileName",
"value": "Qt::UserRole + 2",
"type": "QString"
}, {
"name": "FilePermissions",
"name": "filePermissions",
"value": "Qt::UserRole + 3",
"type": "int"
}],
[{
"name": "FilePath",
"name": "filePath",
"value": "Qt::DisplayRole",
"type": "QString"
}],
[{
"name": "FilePermissions",
"name": "filePermissions",
"value": "Qt::DisplayRole",
"type": "int"
}]

View File

@ -22,11 +22,11 @@ ApplicationWindow {
model: directory
TableViewColumn {
title: "Name"
role: "FileName"
role: "fileName"
}
TableViewColumn {
title: "Permissions"
role: "FilePermissions"
role: "filePermissions"
}
}
TreeView {
@ -35,12 +35,12 @@ ApplicationWindow {
//selectionMode: SelectionMode.SingleSelection
TableViewColumn {
title: "Name"
role: "FileName"
role: "fileName"
width: 300
}
TableViewColumn {
title: "Permissions"
role: "FilePermissions"
role: "filePermissions"
width: 100
}
itemDelegate: Item {

View File

@ -11,13 +11,10 @@ pub struct DirEntry {
name: OsString,
}
impl DirEntry {
pub fn create(name: &str) -> DirEntry {
impl Item for DirEntry {
fn create(name: &str) -> DirEntry {
DirEntry { name: OsString::from(name) }
}
}
impl Item for DirEntry {
fn file_name(&self) -> String {
self.name.to_string_lossy().to_string()
}
@ -45,6 +42,7 @@ impl Default for DirEntry {
}
pub trait Item: Default {
fn create(name: &str) -> Self;
fn retrieve(&self, parents: Vec<&Self>) -> Vec<Self>;
fn file_name(&self) -> String;
fn file_permissions(&self) -> c_int;
@ -61,29 +59,60 @@ struct Entry<T: Item> {
pub struct RGeneralItemModel<T: Item> {
emit: TreeEmitter,
model: TreeUniformTree,
entries: Vec<Entry<T>>,
path: String,
}
impl<T: Item> RGeneralItemModel<T> {
fn get(&mut self, row: c_int, parent: usize) -> usize {
let p = if parent > 0 {
self.entries[parent].children.as_ref().unwrap()[row as usize]
} else {
1
fn reset(&mut self) {
self.entries.clear();
let none0 = Entry {
parent: 0,
row: 0,
children: Some(vec![2]),
data: T::default(),
};
if self.entries[p].children.is_none() {
self.retrieve(p);
let emit = self.emit.clone();
// thread::spawn(move || { emit.new_data_ready(); });
self.entries.push(none0);
let none1 = Entry {
parent: 0,
row: 0,
children: Some(vec![2]),
data: T::default(),
};
self.entries.push(none1);
let root = Entry {
parent: 1,
row: 0,
children: None,
data: T::create(&self.path),
};
self.entries.push(root);
}
fn get_index(&self, mut row: c_int, parent: usize) -> Option<usize> {
if parent == 0 || row < 0 {
return Some(1);
}
p
let r = self.entries.get(parent)
.and_then(|i| i.children.as_ref())
.and_then(|i| i.get(row as usize))
.map(|i| *i);
if r.is_some() {
println!("get_index {} {} {}", row, parent, r.unwrap());}
r
}
fn get(&self, row: c_int, parent: usize) -> Option<&Entry<T>> {
println!("get entries {}", self.entries.len());
self.get_index(row, parent)
.map(|i| &self.entries[i])
}
fn retrieve(&mut self, id: usize) {
let mut new_entries = Vec::new();
let mut children = Vec::new();
{
let parents = self.get_parents(id);
let entries = self.entries[id].data.retrieve(parents);
let entry = &self.entries[id];
let entries = entry.data.retrieve(parents);
for (row, d) in entries.into_iter().enumerate() {
let e = Entry {
parent: id,
@ -94,9 +123,13 @@ impl<T: Item> RGeneralItemModel<T> {
children.push(self.entries.len() + row);
new_entries.push(e);
}
println!("begin_insert_rows {} {} {} {}", entry.row, id, 0, new_entries.len() - 1);
self.model.begin_insert_rows(entry.row as c_int, id, 0,
(new_entries.len() - 1) as c_int);
}
self.entries[id].children = Some(children);
self.entries.append(&mut new_entries);
self.model.end_insert_rows();
}
fn get_parents(&self, id: usize) -> Vec<&T> {
let mut pos = id;
@ -111,45 +144,72 @@ impl<T: Item> RGeneralItemModel<T> {
impl<T: Item> TreeTrait for RGeneralItemModel<T> {
fn create(emit: TreeEmitter, model: TreeUniformTree) -> Self {
let none = Entry {
parent: 0,
row: 0,
children: None,
data: T::default(),
};
RGeneralItemModel {
let mut tree = RGeneralItemModel {
emit: emit,
entries: vec![none],
}
model: model,
entries: Vec::new(),
path: String::new()
};
tree.reset();
tree
}
fn emit(&self) -> &TreeEmitter {
&self.emit
}
fn get_path(&self) -> String {
String::new()
self.path.clone()
}
fn set_path(&mut self, value: String) {
if self.path != value {
self.path = value;
self.emit.path_changed();
self.reset();
}
}
fn can_fetch_more(&self, row: c_int, parent: usize) -> bool {
println!("entries {}", self.entries.len());
let r = self.get(row, parent)
.map(|entry| entry.children.is_none())
.unwrap_or(false);
println!("can_fetch_more {} {} {}", row, parent, r);
r
}
fn fetch_more(&mut self, row: c_int, parent: usize) {
if !self.can_fetch_more(row, parent) {
return;
}
println!("fetch more! {} {}", row, parent);
let p = self.get_index(row, parent).unwrap();
self.retrieve(p);
}
fn row_count(&self, row: c_int, parent: usize) -> c_int {
//let i = self.get(row, parent);
//self.entries[i].children.as_ref().unwrap().len() as i32
0
let r = self.get(row, parent)
.and_then(|entry| entry.children.as_ref())
.map(|i| i.len())
.unwrap_or(0) as c_int;
println!("rrow_count {} {} {}", row, parent, r);
r
}
fn index(&self, row: c_int, parent: usize) -> usize {
//self.get(row, parent)
0
let r = self.get_index(row, parent)
.unwrap_or(0);
println!("index {} {} {}", row, parent, r);
r
}
fn parent(&self, row: c_int, index: usize) -> QModelIndex {
if index < 2 {
return QModelIndex::invalid();
println!("parent {} {}", row, index);
if index > 1 {
if let Some(entry) = self.get(row, index) {
return QModelIndex::create(entry.row as i32, entry.parent);
}
}
let e = &self.entries[index];
QModelIndex::create(e.row as i32, e.parent)
QModelIndex::invalid()
}
fn file_name(&self, row: c_int, parent: usize) -> String {
//let i = self.get(row, parent);
//self.entries[i].data.file_name()
String::new()
println!("file_name {} {}", row, parent);
self.get(row, parent)
.map(|entry| entry.data.file_name())
.unwrap_or_default()
}
fn file_permissions(&self, row: c_int, parent: usize) -> c_int {
//let i = self.get(row,parent);

View File

@ -33,24 +33,24 @@ impl TreeEmitter {
pub struct TreeUniformTree {
qobject: *const TreeQObject,
tree_begin_insert_rows: fn(*const TreeQObject,row: c_int, parent: usize, c_int, c_int),
tree_end_insert_rows: fn(*const TreeQObject),
tree_begin_remove_rows: fn(*const TreeQObject,row: c_int, parent: usize, c_int, c_int),
tree_end_remove_rows: fn(*const TreeQObject),
begin_insert_rows: fn(*const TreeQObject,row: c_int, parent: usize, c_int, c_int),
end_insert_rows: fn(*const TreeQObject),
begin_remove_rows: fn(*const TreeQObject,row: c_int, parent: usize, c_int, c_int),
end_remove_rows: fn(*const TreeQObject),
}
impl TreeUniformTree {
pub fn tree_begin_insert_rows(&self,row: c_int, parent: usize, first: c_int, last: c_int) {
(self.tree_begin_insert_rows)(self.qobject,row, parent, first, last);
pub fn begin_insert_rows(&self,row: c_int, parent: usize, first: c_int, last: c_int) {
(self.begin_insert_rows)(self.qobject,row, parent, first, last);
}
pub fn tree_end_insert_rows(&self) {
(self.tree_end_insert_rows)(self.qobject);
pub fn end_insert_rows(&self) {
(self.end_insert_rows)(self.qobject);
}
pub fn tree_begin_remove_rows(&self,row: c_int, parent: usize, first: c_int, last: c_int) {
(self.tree_begin_remove_rows)(self.qobject,row, parent, first, last);
pub fn begin_remove_rows(&self,row: c_int, parent: usize, first: c_int, last: c_int) {
(self.begin_remove_rows)(self.qobject,row, parent, first, last);
}
pub fn tree_end_remove_rows(&self) {
(self.tree_end_remove_rows)(self.qobject);
pub fn end_remove_rows(&self) {
(self.end_remove_rows)(self.qobject);
}
}
@ -61,7 +61,7 @@ pub trait TreeTrait {
fn set_path(&mut self, value: String);
fn row_count(&self, row: c_int, parent: usize) -> c_int;
fn can_fetch_more(&self, row: c_int, parent: usize) -> bool { false }
fn fetch_more(&self, row: c_int, parent: usize) {}
fn fetch_more(&mut self, row: c_int, parent: usize) {}
fn file_name(&self, row: c_int, parent: usize) -> String;
fn file_icon(&self, row: c_int, parent: usize) -> Vec<u8>;
fn file_path(&self, row: c_int, parent: usize) -> String;
@ -73,14 +73,14 @@ pub trait TreeTrait {
#[no_mangle]
pub extern "C" fn tree_new(qobject: *const TreeQObject,
path_changed: fn(*const TreeQObject),
tree_begin_insert_rows: fn(*const TreeQObject,row: c_int, parent: usize,
begin_insert_rows: fn(*const TreeQObject,row: c_int, parent: usize,
c_int,
c_int),
tree_end_insert_rows: fn(*const TreeQObject),
tree_begin_remove_rows: fn(*const TreeQObject,row: c_int, parent: usize,
end_insert_rows: fn(*const TreeQObject),
begin_remove_rows: fn(*const TreeQObject,row: c_int, parent: usize,
c_int,
c_int),
tree_end_remove_rows: fn(*const TreeQObject))
end_remove_rows: fn(*const TreeQObject))
-> *mut Tree {
let emit = TreeEmitter {
qobject: Arc::new(Mutex::new(qobject)),
@ -88,10 +88,10 @@ pub extern "C" fn tree_new(qobject: *const TreeQObject,
};
let model = TreeUniformTree {
qobject: qobject,
tree_begin_insert_rows: tree_begin_insert_rows,
tree_end_insert_rows: tree_end_insert_rows,
tree_begin_remove_rows: tree_begin_remove_rows,
tree_end_remove_rows: tree_end_remove_rows,
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 = Tree::create(emit, model);
Box::into_raw(Box::new(d))

View File

@ -152,24 +152,24 @@ impl DirectoryEmitter {
pub struct DirectoryList {
qobject: *const DirectoryQObject,
directory_begin_insert_rows: fn(*const DirectoryQObject, c_int, c_int),
directory_end_insert_rows: fn(*const DirectoryQObject),
directory_begin_remove_rows: fn(*const DirectoryQObject, c_int, c_int),
directory_end_remove_rows: fn(*const DirectoryQObject),
begin_insert_rows: fn(*const DirectoryQObject, c_int, c_int),
end_insert_rows: fn(*const DirectoryQObject),
begin_remove_rows: fn(*const DirectoryQObject, c_int, c_int),
end_remove_rows: fn(*const DirectoryQObject),
}
impl DirectoryList {
pub fn directory_begin_insert_rows(&self, first: c_int, last: c_int) {
(self.directory_begin_insert_rows)(self.qobject, first, last);
pub fn begin_insert_rows(&self, first: c_int, last: c_int) {
(self.begin_insert_rows)(self.qobject, first, last);
}
pub fn directory_end_insert_rows(&self) {
(self.directory_end_insert_rows)(self.qobject);
pub fn end_insert_rows(&self) {
(self.end_insert_rows)(self.qobject);
}
pub fn directory_begin_remove_rows(&self, first: c_int, last: c_int) {
(self.directory_begin_remove_rows)(self.qobject, first, last);
pub fn begin_remove_rows(&self, first: c_int, last: c_int) {
(self.begin_remove_rows)(self.qobject, first, last);
}
pub fn directory_end_remove_rows(&self) {
(self.directory_end_remove_rows)(self.qobject);
pub fn end_remove_rows(&self) {
(self.end_remove_rows)(self.qobject);
}
}
@ -180,7 +180,7 @@ pub trait DirectoryTrait {
fn set_path(&mut self, value: String);
fn row_count(&self) -> c_int;
fn can_fetch_more(&self) -> bool { false }
fn fetch_more(&self) {}
fn fetch_more(&mut self) {}
fn file_name(&self, row: c_int) -> String;
fn file_icon(&self, row: c_int) -> Vec<u8>;
fn file_path(&self, row: c_int) -> String;
@ -190,14 +190,14 @@ pub trait DirectoryTrait {
#[no_mangle]
pub extern "C" fn directory_new(qobject: *const DirectoryQObject,
path_changed: fn(*const DirectoryQObject),
directory_begin_insert_rows: fn(*const DirectoryQObject,
begin_insert_rows: fn(*const DirectoryQObject,
c_int,
c_int),
directory_end_insert_rows: fn(*const DirectoryQObject),
directory_begin_remove_rows: fn(*const DirectoryQObject,
end_insert_rows: fn(*const DirectoryQObject),
begin_remove_rows: fn(*const DirectoryQObject,
c_int,
c_int),
directory_end_remove_rows: fn(*const DirectoryQObject))
end_remove_rows: fn(*const DirectoryQObject))
-> *mut Directory {
let emit = DirectoryEmitter {
qobject: Arc::new(Mutex::new(qobject)),
@ -205,10 +205,10 @@ pub extern "C" fn directory_new(qobject: *const DirectoryQObject,
};
let model = DirectoryList {
qobject: qobject,
directory_begin_insert_rows: directory_begin_insert_rows,
directory_end_insert_rows: directory_end_insert_rows,
directory_begin_remove_rows: directory_begin_remove_rows,
directory_end_remove_rows: directory_end_remove_rows,
begin_insert_rows: begin_insert_rows,
end_insert_rows: end_insert_rows,
begin_remove_rows: begin_remove_rows,
end_remove_rows: end_remove_rows,
};
let d = Directory::create(emit, model);
Box::into_raw(Box::new(d))
@ -301,24 +301,24 @@ impl TestTreeEmitter {
pub struct TestTreeUniformTree {
qobject: *const TestTreeQObject,
test_tree_begin_insert_rows: fn(*const TestTreeQObject,row: c_int, parent: usize, c_int, c_int),
test_tree_end_insert_rows: fn(*const TestTreeQObject),
test_tree_begin_remove_rows: fn(*const TestTreeQObject,row: c_int, parent: usize, c_int, c_int),
test_tree_end_remove_rows: fn(*const TestTreeQObject),
begin_insert_rows: fn(*const TestTreeQObject,row: c_int, parent: usize, c_int, c_int),
end_insert_rows: fn(*const TestTreeQObject),
begin_remove_rows: fn(*const TestTreeQObject,row: c_int, parent: usize, c_int, c_int),
end_remove_rows: fn(*const TestTreeQObject),
}
impl TestTreeUniformTree {
pub fn test_tree_begin_insert_rows(&self,row: c_int, parent: usize, first: c_int, last: c_int) {
(self.test_tree_begin_insert_rows)(self.qobject,row, parent, first, last);
pub fn begin_insert_rows(&self,row: c_int, parent: usize, first: c_int, last: c_int) {
(self.begin_insert_rows)(self.qobject,row, parent, first, last);
}
pub fn test_tree_end_insert_rows(&self) {
(self.test_tree_end_insert_rows)(self.qobject);
pub fn end_insert_rows(&self) {
(self.end_insert_rows)(self.qobject);
}
pub fn test_tree_begin_remove_rows(&self,row: c_int, parent: usize, first: c_int, last: c_int) {
(self.test_tree_begin_remove_rows)(self.qobject,row, parent, first, last);
pub fn begin_remove_rows(&self,row: c_int, parent: usize, first: c_int, last: c_int) {
(self.begin_remove_rows)(self.qobject,row, parent, first, last);
}
pub fn test_tree_end_remove_rows(&self) {
(self.test_tree_end_remove_rows)(self.qobject);
pub fn end_remove_rows(&self) {
(self.end_remove_rows)(self.qobject);
}
}
@ -329,7 +329,7 @@ pub trait TestTreeTrait {
fn set_path(&mut self, value: String);
fn row_count(&self, row: c_int, parent: usize) -> c_int;
fn can_fetch_more(&self, row: c_int, parent: usize) -> bool { false }
fn fetch_more(&self, row: c_int, parent: usize) {}
fn fetch_more(&mut self, row: c_int, parent: usize) {}
fn file_name(&self, row: c_int, parent: usize) -> String;
fn file_icon(&self, row: c_int, parent: usize) -> Vec<u8>;
fn file_path(&self, row: c_int, parent: usize) -> String;
@ -341,14 +341,14 @@ pub trait TestTreeTrait {
#[no_mangle]
pub extern "C" fn test_tree_new(qobject: *const TestTreeQObject,
path_changed: fn(*const TestTreeQObject),
test_tree_begin_insert_rows: fn(*const TestTreeQObject,row: c_int, parent: usize,
begin_insert_rows: fn(*const TestTreeQObject,row: c_int, parent: usize,
c_int,
c_int),
test_tree_end_insert_rows: fn(*const TestTreeQObject),
test_tree_begin_remove_rows: fn(*const TestTreeQObject,row: c_int, parent: usize,
end_insert_rows: fn(*const TestTreeQObject),
begin_remove_rows: fn(*const TestTreeQObject,row: c_int, parent: usize,
c_int,
c_int),
test_tree_end_remove_rows: fn(*const TestTreeQObject))
end_remove_rows: fn(*const TestTreeQObject))
-> *mut TestTree {
let emit = TestTreeEmitter {
qobject: Arc::new(Mutex::new(qobject)),
@ -356,10 +356,10 @@ pub extern "C" fn test_tree_new(qobject: *const TestTreeQObject,
};
let model = TestTreeUniformTree {
qobject: qobject,
test_tree_begin_insert_rows: test_tree_begin_insert_rows,
test_tree_end_insert_rows: test_tree_end_insert_rows,
test_tree_begin_remove_rows: test_tree_begin_remove_rows,
test_tree_end_remove_rows: test_tree_end_remove_rows,
begin_insert_rows: begin_insert_rows,
end_insert_rows: end_insert_rows,
begin_remove_rows: begin_remove_rows,
end_remove_rows: end_remove_rows,
};
let d = TestTree::create(emit, model);
Box::into_raw(Box::new(d))

View File

@ -233,10 +233,10 @@ QVariant Directory::data(const QModelIndex &index, int role) const
}
QHash<int, QByteArray> Directory::roleNames() const {
QHash<int, QByteArray> names;
names.insert(Qt::DisplayRole, "FileName");
names.insert(Qt::DecorationRole, "FileIcon");
names.insert(Qt::UserRole + 1, "FilePath");
names.insert(Qt::UserRole + 3, "FilePermissions");
names.insert(Qt::DisplayRole, "fileName");
names.insert(Qt::DecorationRole, "fileIcon");
names.insert(Qt::UserRole + 1, "filePath");
names.insert(Qt::UserRole + 3, "filePermissions");
return names;
}
@ -294,6 +294,9 @@ int TestTree::rowCount(const QModelIndex &parent) const
QModelIndex TestTree::index(int row, int column, const QModelIndex &parent) const
{
if (row < 0 || column < 0 || column >= 3) {
return QModelIndex();
}
const quintptr id = test_tree_index(d, parent.row(), parent.internalId());
return id ?createIndex(row, column, id) :QModelIndex();
}
@ -366,10 +369,10 @@ QVariant TestTree::data(const QModelIndex &index, int role) const
}
QHash<int, QByteArray> TestTree::roleNames() const {
QHash<int, QByteArray> names;
names.insert(Qt::DisplayRole, "FileName");
names.insert(Qt::DecorationRole, "FileIcon");
names.insert(Qt::UserRole + 1, "FilePath");
names.insert(Qt::UserRole + 3, "FilePermissions");
names.insert(Qt::DisplayRole, "fileName");
names.insert(Qt::DecorationRole, "fileIcon");
names.insert(Qt::UserRole + 1, "filePath");
names.insert(Qt::UserRole + 3, "filePermissions");
return names;
}

View File

@ -48,11 +48,11 @@ public:
QString path() const;
void setPath(const QString& v);
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QModelIndex index(int row, int column, const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
bool canFetchMore(const QModelIndex &parent) const;
void fetchMore(const QModelIndex &parent);
QHash<int, QByteArray> roleNames() const;
@ -76,11 +76,11 @@ public:
QString path() const;
void setPath(const QString& v);
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QModelIndex index(int row, int column, const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
bool canFetchMore(const QModelIndex &parent) const;
void fetchMore(const QModelIndex &parent);
QHash<int, QByteArray> roleNames() const;

View File

@ -106,6 +106,9 @@ int Tree::rowCount(const QModelIndex &parent) const
QModelIndex Tree::index(int row, int column, const QModelIndex &parent) const
{
if (row < 0 || column < 0 || column >= 3) {
return QModelIndex();
}
const quintptr id = tree_index(d, parent.row(), parent.internalId());
return id ?createIndex(row, column, id) :QModelIndex();
}
@ -178,10 +181,10 @@ QVariant Tree::data(const QModelIndex &index, int role) const
}
QHash<int, QByteArray> Tree::roleNames() const {
QHash<int, QByteArray> names;
names.insert(Qt::DisplayRole, "FileName");
names.insert(Qt::DecorationRole, "FileIcon");
names.insert(Qt::UserRole + 1, "FilePath");
names.insert(Qt::UserRole + 3, "FilePermissions");
names.insert(Qt::DisplayRole, "fileName");
names.insert(Qt::DecorationRole, "fileIcon");
names.insert(Qt::UserRole + 1, "filePath");
names.insert(Qt::UserRole + 3, "filePermissions");
return names;
}

View File

@ -17,11 +17,11 @@ public:
QString path() const;
void setPath(const QString& v);
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QModelIndex index(int row, int column, const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
bool canFetchMore(const QModelIndex &parent) const;
void fetchMore(const QModelIndex &parent);
QHash<int, QByteArray> roleNames() const;

View File

@ -12,6 +12,9 @@
#include <QtQml/qqml.h>
#include <QQmlContext>
#include <QDebug>
#include <QFileSystemModel>
#include <QStandardItemModel>
#include "modeltest.h"
int main (int argc, char *argv[])
{
@ -53,16 +56,37 @@ int main (int argc, char *argv[])
qmlRegisterType<Directory>("rust", 1, 0, "Directory");
qmlRegisterType<Person>("rust", 1, 0, "Person");
QQmlApplicationEngine engine;
QFileSystemModel m;
m.setRootPath("/");
qDebug() << m.rowCount();
QModelIndex i = m.index(0,0);
qDebug() << i;
qDebug() << m.parent(i);
qDebug() << m.data(i);
qDebug() << m.rowCount(i) << m.canFetchMore(i);
qDebug() << "---";
/*
QStandardItemModel sm;
sm.appendRow(new QStandardItem());
*/
Tree model;
ModelTest test(&model);
model.setPath("/");
qDebug() << model.rowCount();
i = model.index(0,0);
qDebug() << i;
qDebug() << model.parent(i);
qDebug() << model.data(i);
qDebug() << model.rowCount(i) << model.canFetchMore(i);
QTreeView view;
view.setUniformRowHeights(true);
view.setModel(&model);
view.show();
/*
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("fsModel", &model);
engine.load(QUrl(QStringLiteral("qrc:///demo.qml")));
*/
return app.exec();
}

View File

@ -16,33 +16,33 @@
}],
"roles": [
[{
"name": "FileName",
"name": "fileName",
"value": "Qt::DisplayRole",
"type": "QString"
}, {
"name": "FileIcon",
"name": "fileIcon",
"value": "Qt::DecorationRole",
"type": "QByteArray"
}, {
"name": "FilePath",
"name": "filePath",
"value": "Qt::UserRole + 1",
"type": "QString"
}, {
"name": "FileName",
"name": "fileName",
"value": "Qt::UserRole + 2",
"type": "QString"
}, {
"name": "FilePermissions",
"name": "filePermissions",
"value": "Qt::UserRole + 3",
"type": "int"
}],
[{
"name": "FilePath",
"name": "filePath",
"value": "Qt::DisplayRole",
"type": "QString"
}],
[{
"name": "FilePermissions",
"name": "filePermissions",
"value": "Qt::DisplayRole",
"type": "int"
}]

View File

@ -264,11 +264,11 @@ QString baseType(const Object& o) {
void writeHeaderItemModel(QTextStream& h, const Object&) {
h << QString(R"(
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QModelIndex index(int row, int column, const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
bool canFetchMore(const QModelIndex &parent) const;
void fetchMore(const QModelIndex &parent);
QHash<int, QByteArray> roleNames() const;
@ -357,6 +357,9 @@ int %1::rowCount(const QModelIndex &parent) const
QModelIndex %1::index(int row, int column, const QModelIndex &parent) const
{
if (row < 0 || column < 0 || column >= %3) {
return QModelIndex();
}
const quintptr id = %2_index(d, parent.row(), parent.internalId());
return id ?createIndex(row, column, id) :QModelIndex();
}
@ -729,28 +732,28 @@ impl %1Emitter {
}
r << QString(R"(}
pub struct %1%3 {
pub struct %1%2 {
qobject: *const %1QObject,
%2_begin_insert_rows: fn(*const %1QObject,%4 c_int, c_int),
%2_end_insert_rows: fn(*const %1QObject),
%2_begin_remove_rows: fn(*const %1QObject,%4 c_int, c_int),
%2_end_remove_rows: fn(*const %1QObject),
begin_insert_rows: fn(*const %1QObject,%3 c_int, c_int),
end_insert_rows: fn(*const %1QObject),
begin_remove_rows: fn(*const %1QObject,%3 c_int, c_int),
end_remove_rows: fn(*const %1QObject),
}
impl %1%3 {
pub fn %2_begin_insert_rows(&self,%4 first: c_int, last: c_int) {
(self.%2_begin_insert_rows)(self.qobject,%5 first, last);
impl %1%2 {
pub fn begin_insert_rows(&self,%3 first: c_int, last: c_int) {
(self.begin_insert_rows)(self.qobject,%4 first, last);
}
pub fn %2_end_insert_rows(&self) {
(self.%2_end_insert_rows)(self.qobject);
pub fn end_insert_rows(&self) {
(self.end_insert_rows)(self.qobject);
}
pub fn %2_begin_remove_rows(&self,%4 first: c_int, last: c_int) {
(self.%2_begin_remove_rows)(self.qobject,%5 first, last);
pub fn begin_remove_rows(&self,%3 first: c_int, last: c_int) {
(self.begin_remove_rows)(self.qobject,%4 first, last);
}
pub fn %2_end_remove_rows(&self) {
(self.%2_end_remove_rows)(self.qobject);
pub fn end_remove_rows(&self) {
(self.end_remove_rows)(self.qobject);
}
)").arg(o.name, lcname, type, indexDecl, index);
)").arg(o.name, type, indexDecl, index);
}
r << QString(R"(}
@ -773,7 +776,7 @@ pub trait %1Trait {
}
r << QString(R"( fn row_count(&self%1) -> c_int;
fn can_fetch_more(&self%1) -> bool { false }
fn fetch_more(&self%1) {}
fn fetch_more(&mut self%1) {}
)").arg(index);
if (o.type == ObjectType::List) {
index = ", row: c_int";
@ -802,14 +805,14 @@ pub extern "C" fn %2_new(qobject: *const %1QObject)").arg(o.name, lcname);
indexDecl = "row: c_int, parent: usize,";
}
r << QString(R"(,
%2_begin_insert_rows: fn(*const %1QObject,%3
begin_insert_rows: fn(*const %1QObject,%2
c_int,
c_int),
%2_end_insert_rows: fn(*const %1QObject),
%2_begin_remove_rows: fn(*const %1QObject,%3
end_insert_rows: fn(*const %1QObject),
begin_remove_rows: fn(*const %1QObject,%2
c_int,
c_int),
%2_end_remove_rows: fn(*const %1QObject))").arg(o.name, lcname, indexDecl);
end_remove_rows: fn(*const %1QObject))").arg(o.name, indexDecl);
}
r << QString(R"()
-> *mut %1 {
@ -824,13 +827,13 @@ pub extern "C" fn %2_new(qobject: *const %1QObject)").arg(o.name, lcname);
const QString type = o.type == ObjectType::List ? "List" : "UniformTree";
model = ", model";
r << QString(R"( };
let model = %1%3 {
let model = %1%2 {
qobject: qobject,
%2_begin_insert_rows: %2_begin_insert_rows,
%2_end_insert_rows: %2_end_insert_rows,
%2_begin_remove_rows: %2_begin_remove_rows,
%2_end_remove_rows: %2_end_remove_rows,
)").arg(o.name, lcname, type);
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);
}
r << QString(R"( };
let d = %1::create(emit%3);