Only use QModelIndex in binding as a return type

master
Jos van den Oever 2017-08-13 01:10:13 +02:00
parent 4f39f27200
commit 85df6f2031
7 changed files with 73 additions and 90 deletions

View File

@ -156,22 +156,19 @@ impl<T: Item> RItemModelTrait<T> for RGeneralItemModel<T> {
fn emit(&self) -> &RItemModelEmitter {
&self.emit
}
fn column_count(&mut self, _: QModelIndex) -> c_int {
2
}
fn row_count(&mut self, parent: QModelIndex) -> c_int {
let i = self.get(parent.row(), parent.id());
fn row_count(&mut self, row: c_int, parent: usize) -> c_int {
let i = self.get(row, parent);
self.entries[i].children.as_ref().unwrap().len() as i32
}
fn index(&mut self, row: i32, column: i32, parent: QModelIndex) -> QModelIndex {
QModelIndex::create(row, column, self.get(parent.row(), parent.id()))
fn index(&mut self, row: c_int, parent: usize) -> usize {
self.get(row, parent)
}
fn parent(&self, index: QModelIndex) -> QModelIndex {
if !index.is_valid() || index.id() == 1 {
fn parent(&self, row: c_int, index: usize) -> QModelIndex {
if index < 2 {
return QModelIndex::invalid();
}
let e = &self.entries[index.id()];
QModelIndex::create(e.row as i32, 0, e.parent)
let e = &self.entries[index];
QModelIndex::create(e.row as i32, e.parent)
}
fn file_name(&mut self, row: c_int, parent: usize) -> String {
let i = self.get(row, parent);

View File

@ -84,10 +84,9 @@ impl RItemModelEmitter {
pub trait RItemModelTrait<T> {
fn create(emit: RItemModelEmitter, root: T) -> Self;
fn emit(&self) -> &RItemModelEmitter;
fn column_count(&mut self, parent: QModelIndex) -> c_int;
fn row_count(&mut self, parent: QModelIndex) -> c_int;
fn index(&mut self, row: c_int, column: c_int, parent: QModelIndex) -> QModelIndex;
fn parent(&self, index: QModelIndex) -> QModelIndex;
fn row_count(&mut self, row: c_int, parent: usize) -> c_int;
fn index(&mut self, row: c_int, parent: usize) -> usize;
fn parent(&self, row: c_int, parent: usize) -> QModelIndex;
fn file_name(&mut self, row: c_int, parent: usize) -> String;
fn file_permissions(&mut self, row: c_int, parent: usize) -> c_int;
}
@ -110,29 +109,22 @@ pub unsafe extern "C" fn ritemmodel_free(ptr: *mut RItemModel) {
}
#[no_mangle]
pub unsafe extern "C" fn ritemmodel_column_count(ptr: *mut RItemModel, parent: QModelIndex) -> i32 {
(&mut *ptr).column_count(parent)
}
#[no_mangle]
pub unsafe extern "C" fn ritemmodel_row_count(ptr: *mut RItemModel, parent: QModelIndex) -> i32 {
(&mut *ptr).row_count(parent)
pub unsafe extern "C" fn ritemmodel_row_count(ptr: *mut RItemModel, row: c_int, parent: usize) -> i32 {
(&mut *ptr).row_count(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn ritemmodel_index(ptr: *mut RItemModel,
row: i32,
column: i32,
parent: QModelIndex)
-> QModelIndex {
(&mut *ptr).index(row, column, parent)
row: c_int, parent: usize)
-> usize {
(&mut *ptr).index(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn ritemmodel_parent(ptr: *const RItemModel,
index: QModelIndex)
row: c_int, parent: usize)
-> QModelIndex {
(&*ptr).parent(index)
(&*ptr).parent(row, parent)
}
#[no_mangle]

View File

@ -56,7 +56,6 @@ impl<'a> From<&'a Vec<u8>> for QByteArray {
#[repr(C)]
pub struct QModelIndex {
row: c_int,
column: c_int,
internal_id: usize,
}
@ -64,35 +63,14 @@ impl QModelIndex {
pub fn invalid() -> QModelIndex {
QModelIndex {
row: -1,
column: -1,
internal_id: 0,
}
}
pub fn create(row: c_int, column: c_int, id: usize) -> QModelIndex {
pub fn create(row: c_int, id: usize) -> QModelIndex {
QModelIndex {
row: row,
column: column,
internal_id: id,
}
}
pub fn flat(row: c_int, column: c_int) -> QModelIndex {
QModelIndex {
row: row,
column: column,
internal_id: 1,
}
}
pub fn is_valid(&self) -> bool {
self.internal_id != 0 && self.row >= 0 && self.column >= 0
}
pub fn row(&self) -> c_int {
self.row
}
pub fn column(&self) -> c_int {
self.column
}
pub fn id(&self) -> usize {
self.internal_id
}
}

View File

@ -25,10 +25,7 @@ namespace {
};
struct qmodelindex_t {
int row;
int column;
uint64_t id;
qmodelindex_t(const QModelIndex& m):
row(m.row()), column(m.column()), id(m.internalId()) {}
quintptr id;
};
}
@ -45,10 +42,9 @@ extern "C" {
RItemModelInterface* ritemmodel_new(RItemModel*, void (*)(RItemModel*));
void ritemmodel_free(RItemModelInterface*);
int ritemmodel_column_count(RItemModelInterface*, qmodelindex_t parent);
int ritemmodel_row_count(RItemModelInterface*, qmodelindex_t parent);
qmodelindex_t ritemmodel_index(RItemModelInterface*, int row, int column, qmodelindex_t parent);
qmodelindex_t ritemmodel_parent(RItemModelInterface*, qmodelindex_t);
int ritemmodel_row_count(RItemModelInterface*, int, quintptr);
quintptr ritemmodel_index(RItemModelInterface*, int, quintptr);
qmodelindex_t ritemmodel_parent(RItemModelInterface*, int, quintptr);
void ritemmodel_data_file_name(RItemModelInterface*, int, quintptr, QString*, qstring_set);
int ritemmodel_data_file_permissions(RItemModelInterface*, int, quintptr);
}
@ -105,13 +101,13 @@ void RItemModel::handleNewData() {
qDebug() << "new data!";
}
int RItemModel::columnCount(const QModelIndex &parent) const
int RItemModel::columnCount(const QModelIndex &) const
{
return ritemmodel_column_count(d, parent);
return 2;
}
int RItemModel::rowCount(const QModelIndex &parent) const
{
return ritemmodel_row_count(d, parent);
return ritemmodel_row_count(d, parent.row(), parent.internalId());
}
QVariant RItemModel::data(const QModelIndex &index, int role) const
@ -130,14 +126,14 @@ QVariant RItemModel::data(const QModelIndex &index, int role) const
}
QModelIndex RItemModel::index(int row, int column, const QModelIndex &parent) const
{
const qmodelindex_t i = ritemmodel_index(d, row, column, parent);
return i.id ?createIndex(i.row, i.column, i.id) :QModelIndex();
const quintptr id = ritemmodel_index(d, parent.row(), parent.internalId());
return id ?createIndex(row, column, id) :QModelIndex();
}
QModelIndex RItemModel::parent(const QModelIndex &index) const
{
if (!index.isValid()) {
return QModelIndex();
}
const qmodelindex_t parent = ritemmodel_parent(d, index);
return parent.id ?createIndex(parent.row, parent.column, parent.id) :QModelIndex();
const qmodelindex_t parent = ritemmodel_parent(d, index.row(), index.internalId());
return parent.id ?createIndex(parent.row, 0, parent.id) :QModelIndex();
}

View File

@ -939,7 +939,6 @@ impl<'a> From<&'a Vec<u8>> for QByteArray {
#[repr(C)]
pub struct QModelIndex {
row: c_int,
column: c_int,
internal_id: usize,
}
@ -947,36 +946,15 @@ impl QModelIndex {
pub fn invalid() -> QModelIndex {
QModelIndex {
row: -1,
column: -1,
internal_id: 0,
}
}
pub fn create(row: c_int, column: c_int, id: usize) -> QModelIndex {
pub fn create(row: c_int, id: usize) -> QModelIndex {
QModelIndex {
row: row,
column: column,
internal_id: id,
}
}
pub fn flat(row: c_int, column: c_int) -> QModelIndex {
QModelIndex {
row: row,
column: column,
internal_id: 1,
}
}
pub fn is_valid(&self) -> bool {
self.internal_id != 0 && self.row >= 0 && self.column >= 0
}
pub fn row(&self) -> c_int {
self.row
}
pub fn column(&self) -> c_int {
self.column
}
pub fn id(&self) -> usize {
self.internal_id
}
}
)");

View File

@ -53,3 +53,24 @@ impl<'a> From<&'a Vec<u8>> for QByteArray {
}
}
#[repr(C)]
pub struct QModelIndex {
row: c_int,
internal_id: usize,
}
impl QModelIndex {
pub fn invalid() -> QModelIndex {
QModelIndex {
row: -1,
internal_id: 0,
}
}
pub fn create(row: c_int, id: usize) -> QModelIndex {
QModelIndex {
row: row,
internal_id: id,
}
}
}

View File

@ -53,3 +53,24 @@ impl<'a> From<&'a Vec<u8>> for QByteArray {
}
}
#[repr(C)]
pub struct QModelIndex {
row: c_int,
internal_id: usize,
}
impl QModelIndex {
pub fn invalid() -> QModelIndex {
QModelIndex {
row: -1,
internal_id: 0,
}
}
pub fn create(row: c_int, id: usize) -> QModelIndex {
QModelIndex {
row: row,
internal_id: id,
}
}
}