Third step towards using only generated models in Demo

master
Jos van den Oever 2017-08-14 08:54:05 +02:00
parent 7a63362f86
commit 48941e9d29
12 changed files with 192 additions and 55 deletions

View File

@ -18,6 +18,7 @@ ApplicationWindow {
SplitView {
anchors.fill: parent
orientation: Qt.Horizontal
/*
TreeView {
model: directory
TableViewColumn {
@ -29,6 +30,7 @@ ApplicationWindow {
role: "filePermissions"
}
}
*/
TreeView {
model: fsModel
selection: selectionModel
@ -36,13 +38,14 @@ ApplicationWindow {
TableViewColumn {
title: "Name"
role: "fileName"
width: 300
width: 200
}
TableViewColumn {
title: "Permissions"
role: "filePermissions"
title: "Size"
role: "fileSize"
width: 100
}
/*
itemDelegate: Item {
Text {
anchors.verticalCenter: parent.verticalCenter
@ -54,7 +57,6 @@ ApplicationWindow {
onClicked: {
selectionModel.setCurrentIndex(index, ItemSelectionModel.Select)
}
/*
rowDelegate: Item {
anchors.fill: parent
Text {

View File

@ -1,6 +1,7 @@
use interface::*;
use types::*;
use libc::c_int;
use std::fs::*;
use libc::{c_int, c_ulonglong};
use std::fs::read_dir;
use std::path::PathBuf;
use std::ffi::OsString;
@ -9,11 +10,17 @@ use std::thread;
pub struct DirEntry {
name: OsString,
file_type: c_int,
file_size: u64,
}
impl Item for DirEntry {
fn create(name: &str) -> DirEntry {
DirEntry { name: OsString::from(name) }
DirEntry {
name: OsString::from(name),
file_type: 0,
file_size: 0
}
}
fn file_name(&self) -> String {
self.name.to_string_lossy().to_string()
@ -21,13 +28,25 @@ impl Item for DirEntry {
fn file_permissions(&self) -> c_int {
42
}
fn file_type(&self) -> c_int {
self.file_type
}
fn file_size(&self) -> u64 {
self.file_size
}
fn retrieve(&self, parents: Vec<&DirEntry>) -> Vec<DirEntry> {
let path: PathBuf = parents.into_iter().map(|e| &e.name).collect();
let mut v = Vec::new();
if let Ok(it) = read_dir(path) {
for i in it.filter_map(|v| v.ok()) {
let de = DirEntry { name: i.file_name() };
v.push(de);
if let Ok(metadata) = i.metadata() {
let de = DirEntry {
name: i.file_name(),
file_type: 0,
file_size: metadata.len()
};
v.push(de);
}
}
}
v.sort_by(|a, b| a.name.cmp(&b.name));
@ -37,7 +56,11 @@ impl Item for DirEntry {
impl Default for DirEntry {
fn default() -> DirEntry {
DirEntry { name: OsString::new() }
DirEntry {
name: OsString::new(),
file_type: 0,
file_size: 0
}
}
}
@ -46,6 +69,8 @@ pub trait Item: Default {
fn retrieve(&self, parents: Vec<&Self>) -> Vec<Self>;
fn file_name(&self) -> String;
fn file_permissions(&self) -> c_int;
fn file_type(&self) -> c_int;
fn file_size(&self) -> u64;
}
pub type Tree = RGeneralItemModel<DirEntry>;
@ -106,30 +131,35 @@ println!("get entries {}", self.entries.len());
self.get_index(row, parent)
.map(|i| &self.entries[i])
}
fn retrieve(&mut self, id: usize) {
fn retrieve(&mut self, row: c_int, parent: usize) {
let id = self.get_index(row, parent).unwrap();
let mut new_entries = Vec::new();
let mut children = Vec::new();
{
let parents = self.get_parents(id);
let entry = &self.entries[id];
let entries = entry.data.retrieve(parents);
for (row, d) in entries.into_iter().enumerate() {
for (r, d) in entries.into_iter().enumerate() {
let e = Entry {
parent: id,
row: row,
row: r,
children: None,
data: d,
};
children.push(self.entries.len() + row);
children.push(self.entries.len() + r);
new_entries.push(e);
}
if new_entries.len() > 0 {
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.model.begin_insert_rows(row, parent, 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();
if new_entries.len() > 0 {
self.entries.append(&mut new_entries);
self.model.end_insert_rows();
}
}
fn get_parents(&self, id: usize) -> Vec<&T> {
let mut pos = id;
@ -178,9 +208,7 @@ println!("entries {}", self.entries.len());
if !self.can_fetch_more(row, parent) {
return;
}
println!("fetch more! {} {}", row, parent);
let p = self.get_index(row, parent).unwrap();
self.retrieve(p);
self.retrieve(row, parent);
}
fn row_count(&self, row: c_int, parent: usize) -> c_int {
let r = self.get(row, parent)
@ -196,13 +224,14 @@ println!("fetch more! {} {}", row, parent);
println!("index {} {} {}", row, parent, r);
r
}
fn parent(&self, row: c_int, index: usize) -> QModelIndex {
println!("parent {} {}", row, index);
fn parent(&self, index: usize) -> QModelIndex {
if index > 1 {
if let Some(entry) = self.get(row, index) {
if let Some(entry) = self.entries.get(index) {
println!("parent {} {} {}", index, entry.row, entry.parent);
return QModelIndex::create(entry.row as i32, entry.parent);
}
}
println!("parent {} invalid", index);
QModelIndex::invalid()
}
fn file_name(&self, row: c_int, parent: usize) -> String {
@ -222,4 +251,12 @@ println!("parent {} {}", row, index);
fn file_path(&self, row: c_int, parent: usize) -> String {
String::new()
}
fn file_type(&self, row: c_int, parent: usize) -> c_int {
0
}
fn file_size(&self, row: c_int, parent: usize) -> c_ulonglong {
self.get(row, parent)
.map(|entry| entry.data.file_size())
.unwrap_or_default()
}
}

View File

@ -2,7 +2,7 @@
#![allow(unknown_lints)]
#![allow(mutex_atomic, needless_pass_by_value)]
#![allow(unused_imports)]
use libc::{c_int, c_uint, c_void};
use libc::{c_int, c_uint, c_ulonglong, c_void};
use types::*;
use std::sync::{Arc, Mutex};
use std::ptr::null;
@ -66,8 +66,10 @@ pub trait TreeTrait {
fn file_icon(&self, row: c_int, parent: usize) -> Vec<u8>;
fn file_path(&self, row: c_int, parent: usize) -> String;
fn file_permissions(&self, row: c_int, parent: usize) -> c_int;
fn file_type(&self, row: c_int, parent: usize) -> c_int;
fn file_size(&self, row: c_int, parent: usize) -> c_ulonglong;
fn index(&self, row: c_int, parent: usize) -> usize;
fn parent(&self, row: c_int, parent: usize) -> QModelIndex;
fn parent(&self, parent: usize) -> QModelIndex;
}
#[no_mangle]
@ -160,11 +162,21 @@ pub unsafe extern "C" fn tree_data_file_permissions(ptr: *const Tree, row: c_int
(&*ptr).file_permissions(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn tree_data_file_type(ptr: *const Tree, row: c_int, parent: usize) -> c_int {
(&*ptr).file_type(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn tree_data_file_size(ptr: *const Tree, row: c_int, parent: usize) -> c_ulonglong {
(&*ptr).file_size(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn tree_index(ptr: *const Tree, row: c_int, parent: usize) -> usize {
(&*ptr).index(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn tree_parent(ptr: *const Tree, row: c_int, parent: usize) -> QModelIndex {
(&*ptr).parent(row, parent)
pub unsafe extern "C" fn tree_parent(ptr: *const Tree, parent: usize) -> QModelIndex {
(&*ptr).parent(parent)
}

View File

@ -134,7 +134,7 @@ impl TestTreeTrait for TestTree {
fn index(&self, row: c_int, parent: usize) -> usize {
0
}
fn parent(&self, row: c_int, parent: usize) -> QModelIndex {
fn parent(&self, parent: usize) -> QModelIndex {
QModelIndex::create(0, 0)
}
}

View File

@ -2,7 +2,7 @@
#![allow(unknown_lints)]
#![allow(mutex_atomic, needless_pass_by_value)]
#![allow(unused_imports)]
use libc::{c_int, c_uint, c_void};
use libc::{c_int, c_uint, c_ulonglong, c_void};
use types::*;
use std::sync::{Arc, Mutex};
use std::ptr::null;
@ -335,7 +335,7 @@ pub trait TestTreeTrait {
fn file_path(&self, row: c_int, parent: usize) -> String;
fn file_permissions(&self, row: c_int, parent: usize) -> c_int;
fn index(&self, row: c_int, parent: usize) -> usize;
fn parent(&self, row: c_int, parent: usize) -> QModelIndex;
fn parent(&self, parent: usize) -> QModelIndex;
}
#[no_mangle]
@ -433,6 +433,6 @@ pub unsafe extern "C" fn test_tree_index(ptr: *const TestTree, row: c_int, paren
(&*ptr).index(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn test_tree_parent(ptr: *const TestTree, row: c_int, parent: usize) -> QModelIndex {
(&*ptr).parent(row, parent)
pub unsafe extern "C" fn test_tree_parent(ptr: *const TestTree, parent: usize) -> QModelIndex {
(&*ptr).parent(parent)
}

View File

@ -280,7 +280,7 @@ extern "C" {
bool test_tree_can_fetch_more(const TestTreeInterface*, int, quintptr);
void test_tree_fetch_more(TestTreeInterface*, int, quintptr);
quintptr test_tree_index(const TestTreeInterface*, int, quintptr);
qmodelindex_t test_tree_parent(const TestTreeInterface*, int, quintptr);
qmodelindex_t test_tree_parent(const TestTreeInterface*, quintptr);
}
int TestTree::columnCount(const QModelIndex &) const
{
@ -289,6 +289,9 @@ int TestTree::columnCount(const QModelIndex &) const
int TestTree::rowCount(const QModelIndex &parent) const
{
if (parent.isValid() && parent.column() != 0) {
return 0;
}
return test_tree_row_count(d, parent.row(), parent.internalId());
}
@ -306,7 +309,7 @@ QModelIndex TestTree::parent(const QModelIndex &index) const
if (!index.isValid()) {
return QModelIndex();
}
const qmodelindex_t parent = test_tree_parent(d, index.row(), index.internalId());
const qmodelindex_t parent = test_tree_parent(d, index.internalId());
return parent.id ?createIndex(parent.row, 0, parent.id) :QModelIndex();
}

View File

@ -87,26 +87,31 @@ extern "C" {
void tree_data_file_icon(const TreeInterface*, int, quintptr, QByteArray*, qbytearray_set);
void tree_data_file_path(const TreeInterface*, int, quintptr, QString*, qstring_set);
int tree_data_file_permissions(const TreeInterface*, int, quintptr);
int tree_data_file_type(const TreeInterface*, int, quintptr);
qulonglong tree_data_file_size(const TreeInterface*, int, quintptr);
int tree_row_count(const TreeInterface*, int, quintptr);
bool tree_can_fetch_more(const TreeInterface*, int, quintptr);
void tree_fetch_more(TreeInterface*, int, quintptr);
quintptr tree_index(const TreeInterface*, int, quintptr);
qmodelindex_t tree_parent(const TreeInterface*, int, quintptr);
qmodelindex_t tree_parent(const TreeInterface*, quintptr);
}
int Tree::columnCount(const QModelIndex &) const
{
return 3;
return 5;
}
int Tree::rowCount(const QModelIndex &parent) const
{
if (parent.isValid() && parent.column() != 0) {
return 0;
}
return tree_row_count(d, parent.row(), parent.internalId());
}
QModelIndex Tree::index(int row, int column, const QModelIndex &parent) const
{
if (row < 0 || column < 0 || column >= 3) {
if (row < 0 || column < 0 || column >= 5) {
return QModelIndex();
}
const quintptr id = tree_index(d, parent.row(), parent.internalId());
@ -118,7 +123,7 @@ QModelIndex Tree::parent(const QModelIndex &index) const
if (!index.isValid()) {
return QModelIndex();
}
const qmodelindex_t parent = tree_parent(d, index.row(), index.internalId());
const qmodelindex_t parent = tree_parent(d, index.internalId());
return parent.id ?createIndex(parent.row, 0, parent.id) :QModelIndex();
}
@ -159,6 +164,12 @@ QVariant Tree::data(const QModelIndex &index, int role) const
case Qt::UserRole + 3:
v.setValue<int>(tree_data_file_permissions(d, index.row(), index.internalId()));
break;
case Qt::UserRole + 4:
v.setValue<int>(tree_data_file_type(d, index.row(), index.internalId()));
break;
case Qt::UserRole + 5:
v.setValue<qulonglong>(tree_data_file_size(d, index.row(), index.internalId()));
break;
}
break;
case 1:
@ -176,6 +187,20 @@ QVariant Tree::data(const QModelIndex &index, int role) const
break;
}
break;
case 3:
switch (role) {
case Qt::DisplayRole:
v.setValue<int>(tree_data_file_type(d, index.row(), index.internalId()));
break;
}
break;
case 4:
switch (role) {
case Qt::DisplayRole:
v.setValue<qulonglong>(tree_data_file_size(d, index.row(), index.internalId()));
break;
}
break;
}
return v;
}
@ -185,6 +210,8 @@ QHash<int, QByteArray> Tree::roleNames() const {
names.insert(Qt::DecorationRole, "fileIcon");
names.insert(Qt::UserRole + 1, "filePath");
names.insert(Qt::UserRole + 3, "filePermissions");
names.insert(Qt::UserRole + 4, "fileType");
names.insert(Qt::UserRole + 5, "fileSize");
return names;
}

View File

@ -14,7 +14,7 @@
#include <QDebug>
#include <QFileSystemModel>
#include <QStandardItemModel>
#include "modeltest.h"
//#include "modeltest.h"
int main (int argc, char *argv[])
{
@ -71,22 +71,49 @@ int main (int argc, char *argv[])
*/
Tree model;
ModelTest test(&model);
//ModelTest test(&model);
model.setPath("/");
qDebug() << model.rowCount();
qDebug() << "rowCount()" << model.rowCount();
i = model.index(0,0);
qDebug() << i;
qDebug() << model.parent(i);
qDebug() << model.data(i);
qDebug() << model.rowCount(i) << model.canFetchMore(i);
qDebug() << "rowCount(i)" << model.rowCount(i) << model.canFetchMore(i);
model.fetchMore(i);
model.fetchMore(model.index(1,0,i));
model.fetchMore(model.index(2,0,i));
model.fetchMore(model.index(3,0,i));
model.fetchMore(model.index(4,0,i));
model.fetchMore(model.index(5,0,i));
qDebug() << "rowCount(i)" << model.rowCount(i) << model.canFetchMore(i);
i = model.index(0, 0, i);
model.fetchMore(model.index(1,0,i));
model.fetchMore(model.index(2,0,i));
model.fetchMore(model.index(3,0,i));
model.fetchMore(model.index(4,0,i));
model.fetchMore(model.index(5,0,i));
qDebug() << "rowCount(i)" << model.data(i) << model.rowCount(i) << model.canFetchMore(i);
model.fetchMore(i);
model.fetchMore(model.index(1,0,i));
model.fetchMore(model.index(2,0,i));
model.fetchMore(model.index(3,0,i));
model.fetchMore(model.index(4,0,i));
model.fetchMore(model.index(5,0,i));
qDebug() << "rowCount(i)" << model.data(i) << model.rowCount(i) << model.canFetchMore(i);
i = model.index(0, 0, i);
qDebug() << "rowCount(i)" << model.data(i) << model.rowCount(i) << model.canFetchMore(i);
model.fetchMore(i);
//qDebug() << "rowCount(i)" << model.data(i) << model.rowCount(i) << model.canFetchMore(i);
QTreeView view;
view.setUniformRowHeights(true);
view.setModel(&model);
view.expandAll();
view.show();
/*
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("fsModel", &model);
engine.load(QUrl(QStringLiteral("qrc:///demo.qml")));
*/
/**/
return app.exec();
}

View File

@ -35,6 +35,14 @@
"name": "filePermissions",
"value": "Qt::UserRole + 3",
"type": "int"
}, {
"name": "fileType",
"value": "Qt::UserRole + 4",
"type": "int"
}, {
"name": "fileSize",
"value": "Qt::UserRole + 5",
"type": "ulonglong"
}],
[{
"name": "filePath",
@ -45,6 +53,16 @@
"name": "filePermissions",
"value": "Qt::DisplayRole",
"type": "int"
}],
[{
"name": "fileType",
"value": "Qt::DisplayRole",
"type": "int"
}],
[{
"name": "fileSize",
"value": "Qt::DisplayRole",
"type": "ulonglong"
}]
]
}]

View File

@ -21,6 +21,7 @@ enum class BindingType {
Bool,
Int,
UInt,
ULongLong,
QString,
QByteArray
};
@ -65,6 +66,13 @@ const QMap<BindingType, BindingTypeProperties>& bindingTypeProperties() {
.rustType = "c_uint",
.rustTypeInit = "0"
});
f.insert(BindingType::ULongLong, {
.name = "ulonglong",
.cppSetType = "qulonglong",
.cSetType = "qulonglong",
.rustType = "c_ulonglong",
.rustTypeInit = "0"
});
f.insert(BindingType::QString, {
.name = "QString",
.cppSetType = "const QString&",
@ -293,7 +301,7 @@ void writeCppModel(QTextStream& cpp, const Object& o) {
.arg(o.name, lcname, snakeCase(role.name), cGetType(role.type), indexDecl);
} else {
cpp << QString(" %4 %2_data_%3(const %1Interface*%5);\n")
.arg(o.name, lcname, snakeCase(role.name), role.type.name, indexDecl);
.arg(o.name, lcname, snakeCase(role.name), role.type.cppSetType, indexDecl);
}
}
if (o.type == ObjectType::List) {
@ -343,7 +351,7 @@ void %1::fetchMore(const QModelIndex &parent)
bool %2_can_fetch_more(const %1Interface*, int, quintptr);
void %2_fetch_more(%1Interface*, int, quintptr);
quintptr %2_index(const %1Interface*, int, quintptr);
qmodelindex_t %2_parent(const %1Interface*, int, quintptr);
qmodelindex_t %2_parent(const %1Interface*, quintptr);
}
int %1::columnCount(const QModelIndex &) const
{
@ -352,6 +360,9 @@ int %1::columnCount(const QModelIndex &) const
int %1::rowCount(const QModelIndex &parent) const
{
if (parent.isValid() && parent.column() != 0) {
return 0;
}
return %2_row_count(d, parent.row(), parent.internalId());
}
@ -369,7 +380,7 @@ QModelIndex %1::parent(const QModelIndex &index) const
if (!index.isValid()) {
return QModelIndex();
}
const qmodelindex_t parent = %2_parent(d, index.row(), index.internalId());
const qmodelindex_t parent = %2_parent(d, index.internalId());
return parent.id ?createIndex(parent.row, 0, parent.id) :QModelIndex();
}
@ -410,7 +421,7 @@ QVariant %1::data(const QModelIndex &index, int role) const
cpp << " v.setValue<QByteArray>(b);\n";
} else {
cpp << QString(" v.setValue<%3>(%1_data_%2(d%5));\n")
.arg(lcname, snakeCase(role.name), role.type.name, index);
.arg(lcname, snakeCase(role.name), role.type.cppSetType, index);
}
cpp << " break;\n";
}
@ -788,7 +799,7 @@ pub trait %1Trait {
}
if (o.type == ObjectType::UniformTree) {
r << " fn index(&self, row: c_int, parent: usize) -> usize;\n";
r << " fn parent(&self, row: c_int, parent: usize) -> QModelIndex;\n";
r << " fn parent(&self, parent: usize) -> QModelIndex;\n";
}
r << QString(R"(}
@ -938,8 +949,8 @@ pub unsafe extern "C" fn %2_index(ptr: *const %1, row: c_int, parent: usize) ->
(&*ptr).index(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn %2_parent(ptr: *const %1, row: c_int, parent: usize) -> QModelIndex {
(&*ptr).parent(row, parent)
pub unsafe extern "C" fn %2_parent(ptr: *const %1, parent: usize) -> QModelIndex {
(&*ptr).parent(parent)
}
)").arg(o.name, lcname);
@ -959,7 +970,7 @@ void writeRustInterface(const Configuration& conf) {
#![allow(unknown_lints)]
#![allow(mutex_atomic, needless_pass_by_value)]
#![allow(unused_imports)]
use libc::{c_int, c_uint, c_void};
use libc::{c_int, c_uint, c_ulonglong, c_void};
use types::*;
use std::sync::{Arc, Mutex};
use std::ptr::null;
@ -1043,7 +1054,7 @@ void writeRustImplementationObject(QTextStream& r, const Object& o) {
r << R"( fn index(&self, row: c_int, parent: usize) -> usize {
0
}
fn parent(&self, row: c_int, parent: usize) -> QModelIndex {
fn parent(&self, parent: usize) -> QModelIndex {
QModelIndex::create(0, 0)
}
)";

View File

@ -2,7 +2,7 @@
#![allow(unknown_lints)]
#![allow(mutex_atomic, needless_pass_by_value)]
#![allow(unused_imports)]
use libc::{c_int, c_uint, c_void};
use libc::{c_int, c_uint, c_ulonglong, c_void};
use types::*;
use std::sync::{Arc, Mutex};
use std::ptr::null;

View File

@ -2,7 +2,7 @@
#![allow(unknown_lints)]
#![allow(mutex_atomic, needless_pass_by_value)]
#![allow(unused_imports)]
use libc::{c_int, c_uint, c_void};
use libc::{c_int, c_uint, c_ulonglong, c_void};
use types::*;
use std::sync::{Arc, Mutex};
use std::ptr::null;